nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/StoredScript.java
author hannesw
Fri, 19 Sep 2014 13:13:20 +0200
changeset 26764 c777787a937d
parent 26068 5488f52c2788
child 26768 751b0f427090
permissions -rw-r--r--
8046202: Make persistent code store more flexible Reviewed-by: lagergren, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
     1
/*
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
     2
 * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
     4
 *
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    10
 *
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    15
 * accompanied this code).
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    16
 *
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    20
 *
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    23
 * questions.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    24
 */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    25
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    26
package jdk.nashorn.internal.runtime;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    27
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    28
import java.io.Serializable;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    29
import java.util.Arrays;
26764
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26068
diff changeset
    30
import java.util.LinkedHashMap;
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    31
import java.util.Map;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    32
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    33
/**
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    34
 * Class representing a persistent compiled script.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    35
 */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    36
public final class StoredScript implements Serializable {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    37
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    38
    /** Compilation id */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    39
    private final int compilationId;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    40
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    41
    /** Main class name. */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    42
    private final String mainClassName;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    43
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    44
    /** Map of class names to class bytes. */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    45
    private final Map<String, byte[]> classBytes;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    46
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    47
    /** Constants array. */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    48
    private final Object[] constants;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    49
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    50
    /** Function initializers */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    51
    private final Map<Integer, FunctionInitializer> initializers;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    52
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    53
    private static final long serialVersionUID = 2958227232195298340L;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    54
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    55
    /**
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    56
     * Constructor.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    57
     *
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    58
     * @param mainClassName main class name
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    59
     * @param classBytes map of class names to class bytes
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    60
     * @param constants constants array
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    61
     */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    62
    public StoredScript(final int compilationId, final String mainClassName, final Map<String, byte[]> classBytes, final Map<Integer, FunctionInitializer> initializers, final Object[] constants) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    63
        this.compilationId = compilationId;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    64
        this.mainClassName = mainClassName;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    65
        this.classBytes = classBytes;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    66
        this.constants = constants;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    67
        this.initializers = initializers;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    68
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    69
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    70
    public int getCompilationId() {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    71
        return compilationId;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    72
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    73
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    74
    /**
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    75
     * Returns the main class name.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    76
     * @return the main class name
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    77
     */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    78
    public String getMainClassName() {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    79
        return mainClassName;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    80
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    81
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    82
    /**
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    83
     * Returns a map of class names to class bytes.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    84
     * @return map of class bytes
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    85
     */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    86
    public Map<String, byte[]> getClassBytes() {
26764
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26068
diff changeset
    87
        final Map<String, byte[]> clonedMap = new LinkedHashMap<>();
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26068
diff changeset
    88
        for (final Map.Entry<String, byte[]> entry : classBytes.entrySet()) {
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26068
diff changeset
    89
            clonedMap.put(entry.getKey(), entry.getValue().clone());
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26068
diff changeset
    90
        }
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26068
diff changeset
    91
        return clonedMap;
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    92
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    93
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    94
    /**
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    95
     * Returns the constants array.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    96
     * @return constants array
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    97
     */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    98
    public Object[] getConstants() {
26764
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26068
diff changeset
    99
        return constants.clone();
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   100
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   101
26764
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26068
diff changeset
   102
    /**
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26068
diff changeset
   103
     * Returns the function initializers map.
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26068
diff changeset
   104
     * @return The initializers map.
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26068
diff changeset
   105
     */
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26068
diff changeset
   106
    public Map<Integer, FunctionInitializer> getInitializers() {
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26068
diff changeset
   107
        final Map<Integer, FunctionInitializer> clonedMap = new LinkedHashMap<>();
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26068
diff changeset
   108
        for (final Map.Entry<Integer, FunctionInitializer> entry : initializers.entrySet()) {
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26068
diff changeset
   109
            clonedMap.put(entry.getKey(), new FunctionInitializer(entry.getValue()));
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26068
diff changeset
   110
        }
c777787a937d 8046202: Make persistent code store more flexible
hannesw
parents: 26068
diff changeset
   111
        return clonedMap;
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   112
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   113
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   114
    @Override
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   115
    public int hashCode() {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   116
        int hash = mainClassName.hashCode();
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   117
        hash = 31 * hash + classBytes.hashCode();
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   118
        hash = 31 * hash + Arrays.hashCode(constants);
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   119
        return hash;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   120
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   121
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   122
    @Override
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   123
    public boolean equals(final Object obj) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   124
        if (obj == this) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   125
            return true;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   126
        }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   127
        if (!(obj instanceof StoredScript)) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   128
            return false;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   129
        }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   130
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   131
        final StoredScript cs = (StoredScript) obj;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   132
        return mainClassName.equals(cs.mainClassName)
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   133
                && classBytes.equals(cs.classBytes)
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   134
                && Arrays.equals(constants, cs.constants);
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   135
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   136
}