nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptFunctionData.java
author hannesw
Mon, 20 Jun 2016 12:21:51 +0200
changeset 39077 c549268fe94c
parent 34844 31c026e00569
child 47040 4ed4c4eba60c
permissions -rw-r--r--
8156614: Lazy parsing of ES6 shorthand method syntax is broken Reviewed-by: sundar, mhaupt
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
     1
/*
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
     4
 *
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    10
 *
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    15
 * accompanied this code).
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    16
 *
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    20
 *
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    23
 * questions.
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    24
 */
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    25
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    26
package jdk.nashorn.internal.runtime;
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    27
17744
65849f87aa28 8011718: binding already bound function with extra arguments fails
attila
parents: 16523
diff changeset
    28
import static jdk.nashorn.internal.lookup.Lookup.MH;
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
    29
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
    30
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
29282
a8523237b66c 8074031: Canonicalize is-a-JS-string tests
attila
parents: 26768
diff changeset
    31
24769
attila
parents: 24753 23767
diff changeset
    32
import java.io.IOException;
attila
parents: 24753 23767
diff changeset
    33
import java.io.ObjectInputStream;
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
    34
import java.io.Serializable;
16225
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
    35
import java.lang.invoke.MethodHandle;
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
    36
import java.lang.invoke.MethodHandles;
16225
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
    37
import java.lang.invoke.MethodType;
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26503
diff changeset
    38
import java.util.Collection;
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
    39
import java.util.LinkedList;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
    40
import java.util.List;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
    41
import jdk.nashorn.internal.runtime.linker.LinkerCallSite;
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    42
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    43
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    44
/**
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    45
 * A container for data needed to instantiate a specific {@link ScriptFunction} at runtime.
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    46
 * Instances of this class are created during codegen and stored in script classes'
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    47
 * constants array to reduce function instantiation overhead during runtime.
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    48
 */
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
    49
public abstract class ScriptFunctionData implements Serializable {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
    50
    static final int MAX_ARITY = LinkerCallSite.ARGLIMIT;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
    51
    static {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
    52
        // Assert it fits in a byte, as that's what we store it in. It's just a size optimization though, so if needed
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
    53
        // "byte arity" field can be widened.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
    54
        assert MAX_ARITY < 256;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
    55
    }
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    56
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
    57
    /** Name of the function or "" for anonymous functions */
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
    58
    protected final String name;
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
    59
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
    60
    /**
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
    61
     * A list of code versions of a function sorted in ascending order of generic descriptors.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
    62
     */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
    63
    protected transient LinkedList<CompiledFunction> code = new LinkedList<>();
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    64
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
    65
    /** Function flags */
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
    66
    protected int flags;
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    67
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
    68
    // Parameter arity of the function, corresponding to "f.length". E.g. "function f(a, b, c) { ... }" arity is 3, and
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
    69
    // some built-in ECMAScript functions have their arity declared by the specification. Note that regardless of this
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
    70
    // value, the function might still be capable of receiving variable number of arguments, see isVariableArity.
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
    71
    private int arity;
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    72
24753
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
    73
    /**
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
    74
     * A pair of method handles used for generic invoker and constructor. Field is volatile as it can be initialized by
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
    75
     * multiple threads concurrently, but we still tolerate a race condition in it as all values stored into it are
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
    76
     * idempotent.
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
    77
     */
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
    78
    private volatile transient GenericInvokers genericInvokers;
24753
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
    79
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
    80
    private static final MethodHandle BIND_VAR_ARGS = findOwnMH("bindVarArgs", Object[].class, Object[].class, Object[].class);
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
    81
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
    82
    /** Is this a strict mode function? */
30393
0e005bd1de13 8066237: Fuzzing bug: Parser error on optimistic recompilation
hannesw
parents: 29282
diff changeset
    83
    public static final int IS_STRICT            = 1 << 0;
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
    84
    /** Is this a built-in function? */
30393
0e005bd1de13 8066237: Fuzzing bug: Parser error on optimistic recompilation
hannesw
parents: 29282
diff changeset
    85
    public static final int IS_BUILTIN           = 1 << 1;
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
    86
    /** Is this a constructor function? */
30393
0e005bd1de13 8066237: Fuzzing bug: Parser error on optimistic recompilation
hannesw
parents: 29282
diff changeset
    87
    public static final int IS_CONSTRUCTOR       = 1 << 2;
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
    88
    /** Does this function expect a callee argument? */
30393
0e005bd1de13 8066237: Fuzzing bug: Parser error on optimistic recompilation
hannesw
parents: 29282
diff changeset
    89
    public static final int NEEDS_CALLEE         = 1 << 3;
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
    90
    /** Does this function make use of the this-object argument? */
30393
0e005bd1de13 8066237: Fuzzing bug: Parser error on optimistic recompilation
hannesw
parents: 29282
diff changeset
    91
    public static final int USES_THIS            = 1 << 4;
24727
attila
parents: 24720 23375
diff changeset
    92
    /** Is this a variable arity function? */
30393
0e005bd1de13 8066237: Fuzzing bug: Parser error on optimistic recompilation
hannesw
parents: 29282
diff changeset
    93
    public static final int IS_VARIABLE_ARITY    = 1 << 5;
0e005bd1de13 8066237: Fuzzing bug: Parser error on optimistic recompilation
hannesw
parents: 29282
diff changeset
    94
    /** Is this a object literal property getter or setter? */
0e005bd1de13 8066237: Fuzzing bug: Parser error on optimistic recompilation
hannesw
parents: 29282
diff changeset
    95
    public static final int IS_PROPERTY_ACCESSOR = 1 << 6;
39077
c549268fe94c 8156614: Lazy parsing of ES6 shorthand method syntax is broken
hannesw
parents: 34844
diff changeset
    96
    /** Is this an ES6 method? */
c549268fe94c 8156614: Lazy parsing of ES6 shorthand method syntax is broken
hannesw
parents: 34844
diff changeset
    97
    public static final int IS_ES6_METHOD        = 1 << 7;
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
    98
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
    99
    /** Flag for strict or built-in functions */
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   100
    public static final int IS_STRICT_OR_BUILTIN = IS_STRICT | IS_BUILTIN;
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   101
    /** Flag for built-in constructors */
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   102
    public static final int IS_BUILTIN_CONSTRUCTOR = IS_BUILTIN | IS_CONSTRUCTOR;
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   103
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
   104
    private static final long serialVersionUID = 4252901245508769114L;
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23375
diff changeset
   105
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   106
    /**
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   107
     * Constructor
16233
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16228
diff changeset
   108
     *
24727
attila
parents: 24720 23375
diff changeset
   109
     * @param name  script function name
attila
parents: 24720 23375
diff changeset
   110
     * @param arity arity
attila
parents: 24720 23375
diff changeset
   111
     * @param flags the function flags
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   112
     */
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   113
    ScriptFunctionData(final String name, final int arity, final int flags) {
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   114
        this.name  = name;
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   115
        this.flags = flags;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   116
        setArity(arity);
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   117
    }
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   118
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   119
    final int getArity() {
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   120
        return arity;
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   121
    }
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   122
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34736
diff changeset
   123
    String getDocumentation() {
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34736
diff changeset
   124
        return toSource();
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34736
diff changeset
   125
    }
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34736
diff changeset
   126
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34736
diff changeset
   127
    String getDocumentationKey() {
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34736
diff changeset
   128
        return null;
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32695
diff changeset
   129
    }
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32695
diff changeset
   130
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   131
    final boolean isVariableArity() {
24727
attila
parents: 24720 23375
diff changeset
   132
        return (flags & IS_VARIABLE_ARITY) != 0;
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   133
    }
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   134
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   135
    /**
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   136
     * Used from e.g. Native*$Constructors as an explicit call. TODO - make arity immutable and final
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   137
     * @param arity new arity
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   138
     */
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   139
    void setArity(final int arity) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   140
        if(arity < 0 || arity > MAX_ARITY) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   141
            throw new IllegalArgumentException(String.valueOf(arity));
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   142
        }
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   143
        this.arity = arity;
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   144
    }
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   145
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32695
diff changeset
   146
    /**
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32695
diff changeset
   147
     * Used from nasgen generated code.
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32695
diff changeset
   148
     *
39077
c549268fe94c 8156614: Lazy parsing of ES6 shorthand method syntax is broken
hannesw
parents: 34844
diff changeset
   149
     * @param docKey documentation key for this function
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32695
diff changeset
   150
     */
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34736
diff changeset
   151
    void setDocumentationKey(final String docKey) {
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32695
diff changeset
   152
    }
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32695
diff changeset
   153
34844
31c026e00569 8146251: Avoid annotation to specify documentation for JS builtin functions
sundar
parents: 34736
diff changeset
   154
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   155
    CompiledFunction bind(final CompiledFunction originalInv, final ScriptFunction fn, final Object self, final Object[] args) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   156
        final MethodHandle boundInvoker = bindInvokeHandle(originalInv.createComposableInvoker(), fn, self, args);
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   157
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   158
        if (isConstructor()) {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26503
diff changeset
   159
            return new CompiledFunction(boundInvoker, bindConstructHandle(originalInv.createComposableConstructor(), fn, args), null);
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   160
        }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   161
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   162
        return new CompiledFunction(boundInvoker);
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   163
    }
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   164
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   165
    /**
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   166
     * Is this a ScriptFunction generated with strict semantics?
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   167
     * @return true if strict, false otherwise
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   168
     */
32533
47d3ca23ac91 8135332: ScriptFunction constructor should use is bound and is strict check rather than checking for 'arguments' and 'caller'
sundar
parents: 32527
diff changeset
   169
    public final boolean isStrict() {
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   170
        return (flags & IS_STRICT) != 0;
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   171
    }
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   172
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   173
    /**
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   174
     * Return the complete internal function name for this
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   175
     * data, not anonymous or similar. May be identical
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   176
     * @return internal function name
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   177
     */
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   178
    protected String getFunctionName() {
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   179
        return getName();
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   180
    }
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   181
32533
47d3ca23ac91 8135332: ScriptFunction constructor should use is bound and is strict check rather than checking for 'arguments' and 'caller'
sundar
parents: 32527
diff changeset
   182
    final boolean isBuiltin() {
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   183
        return (flags & IS_BUILTIN) != 0;
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   184
    }
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   185
32533
47d3ca23ac91 8135332: ScriptFunction constructor should use is bound and is strict check rather than checking for 'arguments' and 'caller'
sundar
parents: 32527
diff changeset
   186
    final boolean isConstructor() {
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   187
        return (flags & IS_CONSTRUCTOR) != 0;
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   188
    }
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   189
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   190
    abstract boolean needsCallee();
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   191
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   192
    /**
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   193
     * Returns true if this is a non-strict, non-built-in function that requires non-primitive this argument
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   194
     * according to ECMA 10.4.3.
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   195
     * @return true if this argument must be an object
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   196
     */
32533
47d3ca23ac91 8135332: ScriptFunction constructor should use is bound and is strict check rather than checking for 'arguments' and 'caller'
sundar
parents: 32527
diff changeset
   197
    final boolean needsWrappedThis() {
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   198
        return (flags & USES_THIS) != 0 && (flags & IS_STRICT_OR_BUILTIN) == 0;
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   199
    }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   200
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   201
    String toSource() {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   202
        return "function " + (name == null ? "" : name) + "() { [native code] }";
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   203
    }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   204
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   205
    String getName() {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   206
        return name;
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   207
    }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   208
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   209
    /**
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   210
     * Get this function as a String containing its source code. If no source code
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   211
     * exists in this ScriptFunction, its contents will be displayed as {@code [native code]}
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   212
     *
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   213
     * @return string representation of this function
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   214
     */
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   215
    @Override
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   216
    public String toString() {
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24731
diff changeset
   217
        return name.isEmpty() ? "<anonymous>" : name;
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24731
diff changeset
   218
    }
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24731
diff changeset
   219
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24731
diff changeset
   220
    /**
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24731
diff changeset
   221
     * Verbose description of data
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24731
diff changeset
   222
     * @return verbose description
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24731
diff changeset
   223
     */
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24731
diff changeset
   224
    public String toStringVerbose() {
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   225
        final StringBuilder sb = new StringBuilder();
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   226
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   227
        sb.append("name='").
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   228
                append(name.isEmpty() ? "<anonymous>" : name).
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   229
                append("' ").
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   230
                append(code.size()).
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   231
                append(" invokers=").
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   232
                append(code);
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   233
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   234
        return sb.toString();
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   235
    }
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   236
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   237
    /**
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   238
     * Pick the best invoker, i.e. the one version of this method with as narrow and specific
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   239
     * types as possible. If the call site arguments are objects, but boxed primitives we can
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   240
     * also try to get a primitive version of the method and do an unboxing filter, but then
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   241
     * we need to insert a guard that checks the argument is really always a boxed primitive
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   242
     * and not suddenly a "real" object
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   243
     *
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   244
     * @param callSiteType callsite type
26053
8137f95db5e8 8046026: CompiledFunction.relinkComposableInvoker assert is being hit
attila
parents: 24881
diff changeset
   245
     * @return compiled function object representing the best invoker.
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   246
     */
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26503
diff changeset
   247
    final CompiledFunction getBestInvoker(final MethodType callSiteType, final ScriptObject runtimeScope) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26503
diff changeset
   248
        return getBestInvoker(callSiteType, runtimeScope, CompiledFunction.NO_FUNCTIONS);
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26503
diff changeset
   249
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26503
diff changeset
   250
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26503
diff changeset
   251
    final CompiledFunction getBestInvoker(final MethodType callSiteType, final ScriptObject runtimeScope, final Collection<CompiledFunction> forbidden) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26503
diff changeset
   252
        final CompiledFunction cf = getBest(callSiteType, runtimeScope, forbidden);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   253
        assert cf != null;
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   254
        return cf;
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   255
    }
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   256
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26503
diff changeset
   257
    final CompiledFunction getBestConstructor(final MethodType callSiteType, final ScriptObject runtimeScope, final Collection<CompiledFunction> forbidden) {
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   258
        if (!isConstructor()) {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   259
            throw typeError("not.a.constructor", toSource());
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   260
        }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   261
        // Constructor call sites don't have a "this", but getBest is meant to operate on "callee, this, ..." style
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26503
diff changeset
   262
        final CompiledFunction cf = getBest(callSiteType.insertParameterTypes(1, Object.class), runtimeScope, forbidden);
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24733
diff changeset
   263
        return cf;
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   264
    }
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   265
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   266
    /**
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   267
     * If we can have lazy code generation, this is a hook to ensure that the code has been compiled.
24731
ab0c8fc915ae 8038406: Testability: as a first step of moving loggers away from the process global space, the Debug object now supports logging POJOs from log entries as an event queue, which can be introspected from test scripts. This is way better than screen scraping brittle and subject-to-change log output.
lagergren
parents: 24729
diff changeset
   268
     * This does not guarantee the code been installed in this {@code ScriptFunctionData} instance
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   269
     */
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   270
    protected void ensureCompiled() {
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   271
        //empty
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   272
    }
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   273
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   274
    /**
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   275
     * Return a generic Object/Object invoker for this method. It will ensure code
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   276
     * is generated, get the most generic of all versions of this function and adapt it
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   277
     * to Objects.
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   278
     *
24729
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24727
diff changeset
   279
     * @param runtimeScope the runtime scope. It can be used to evaluate types of scoped variables to guide the
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24727
diff changeset
   280
     * optimistic compilation, should the call to this method trigger code compilation. Can be null if current runtime
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24727
diff changeset
   281
     * scope is not known, but that might cause compilation of code that will need more deoptimization passes.
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   282
     * @return generic invoker of this script function
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   283
     */
24729
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24727
diff changeset
   284
    final MethodHandle getGenericInvoker(final ScriptObject runtimeScope) {
24753
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   285
        // This method has race conditions both on genericsInvoker and genericsInvoker.invoker, but even if invoked
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   286
        // concurrently, they'll create idempotent results, so it doesn't matter. We could alternatively implement this
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   287
        // using java.util.concurrent.AtomicReferenceFieldUpdater, but it's hardly worth it.
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   288
        final GenericInvokers lgenericInvokers = ensureGenericInvokers();
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   289
        MethodHandle invoker = lgenericInvokers.invoker;
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   290
        if(invoker == null) {
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   291
            lgenericInvokers.invoker = invoker = createGenericInvoker(runtimeScope);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   292
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   293
        return invoker;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   294
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   295
24729
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24727
diff changeset
   296
    private MethodHandle createGenericInvoker(final ScriptObject runtimeScope) {
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24727
diff changeset
   297
        return makeGenericMethod(getGeneric(runtimeScope).createComposableInvoker());
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   298
    }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   299
24729
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24727
diff changeset
   300
    final MethodHandle getGenericConstructor(final ScriptObject runtimeScope) {
24753
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   301
        // This method has race conditions both on genericsInvoker and genericsInvoker.constructor, but even if invoked
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   302
        // concurrently, they'll create idempotent results, so it doesn't matter. We could alternatively implement this
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   303
        // using java.util.concurrent.AtomicReferenceFieldUpdater, but it's hardly worth it.
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   304
        final GenericInvokers lgenericInvokers = ensureGenericInvokers();
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   305
        MethodHandle constructor = lgenericInvokers.constructor;
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   306
        if(constructor == null) {
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   307
            lgenericInvokers.constructor = constructor = createGenericConstructor(runtimeScope);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   308
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   309
        return constructor;
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   310
    }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   311
24729
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24727
diff changeset
   312
    private MethodHandle createGenericConstructor(final ScriptObject runtimeScope) {
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24727
diff changeset
   313
        return makeGenericMethod(getGeneric(runtimeScope).createComposableConstructor());
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   314
    }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   315
24753
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   316
    private GenericInvokers ensureGenericInvokers() {
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   317
        GenericInvokers lgenericInvokers = genericInvokers;
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   318
        if(lgenericInvokers == null) {
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   319
            genericInvokers = lgenericInvokers = new GenericInvokers();
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   320
        }
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   321
        return lgenericInvokers;
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   322
    }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   323
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   324
    private static MethodType widen(final MethodType cftype) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   325
        final Class<?>[] paramTypes = new Class<?>[cftype.parameterCount()];
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   326
        for (int i = 0; i < cftype.parameterCount(); i++) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   327
            paramTypes[i] = cftype.parameterType(i).isPrimitive() ? cftype.parameterType(i) : Object.class;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   328
        }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   329
        return MH.type(cftype.returnType(), paramTypes);
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   330
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   331
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   332
    /**
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   333
     * Used to find an apply to call version that fits this callsite.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   334
     * We cannot just, as in the normal matcher case, return e.g. (Object, Object, int)
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   335
     * for (Object, Object, int, int, int) or we will destroy the semantics and get
32534
b3ec7f3b3c2a 8136349: Typos patch for nashorn sources submitted on Sep 10, 2015
sundar
parents: 32533
diff changeset
   336
     * a function that, when padded with undefined values, behaves differently
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   337
     * @param type actual call site type
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   338
     * @return apply to call that perfectly fits this callsite or null if none found
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   339
     */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   340
    CompiledFunction lookupExactApplyToCall(final MethodType type) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   341
        for (final CompiledFunction cf : code) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   342
            if (!cf.isApplyToCall()) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   343
                continue;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   344
            }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   345
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   346
            final MethodType cftype = cf.type();
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   347
            if (cftype.parameterCount() != type.parameterCount()) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   348
                continue;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   349
            }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   350
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   351
            if (widen(cftype).equals(widen(type))) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   352
                return cf;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   353
            }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   354
        }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   355
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   356
        return null;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   357
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   358
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   359
    CompiledFunction pickFunction(final MethodType callSiteType, final boolean canPickVarArg) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   360
        for (final CompiledFunction candidate : code) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   361
            if (candidate.matchesCallSite(callSiteType, canPickVarArg)) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   362
                return candidate;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   363
            }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   364
        }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   365
        return null;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   366
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   367
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   368
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   369
     * Returns the best function for the specified call site type.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   370
     * @param callSiteType The call site type. Call site types are expected to have the form
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   371
     * {@code (callee, this[, args...])}.
24729
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24727
diff changeset
   372
     * @param runtimeScope the runtime scope. It can be used to evaluate types of scoped variables to guide the
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24727
diff changeset
   373
     * optimistic compilation, should the call to this method trigger code compilation. Can be null if current runtime
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24727
diff changeset
   374
     * scope is not known, but that might cause compilation of code that will need more deoptimization passes.
34736
52e9e61d937f 8145550: Megamorphic invoke should use CompiledFunction variants without any LinkLogic
sundar
parents: 34735
diff changeset
   375
     * @param linkLogicOkay is a CompiledFunction with a LinkLogic acceptable?
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   376
     * @return the best function for the specified call site type.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   377
     */
34736
52e9e61d937f 8145550: Megamorphic invoke should use CompiledFunction variants without any LinkLogic
sundar
parents: 34735
diff changeset
   378
    abstract CompiledFunction getBest(final MethodType callSiteType, final ScriptObject runtimeScope, final Collection<CompiledFunction> forbidden, final boolean linkLogicOkay);
52e9e61d937f 8145550: Megamorphic invoke should use CompiledFunction variants without any LinkLogic
sundar
parents: 34735
diff changeset
   379
52e9e61d937f 8145550: Megamorphic invoke should use CompiledFunction variants without any LinkLogic
sundar
parents: 34735
diff changeset
   380
    /**
52e9e61d937f 8145550: Megamorphic invoke should use CompiledFunction variants without any LinkLogic
sundar
parents: 34735
diff changeset
   381
     * Returns the best function for the specified call site type.
52e9e61d937f 8145550: Megamorphic invoke should use CompiledFunction variants without any LinkLogic
sundar
parents: 34735
diff changeset
   382
     * @param callSiteType The call site type. Call site types are expected to have the form
52e9e61d937f 8145550: Megamorphic invoke should use CompiledFunction variants without any LinkLogic
sundar
parents: 34735
diff changeset
   383
     * {@code (callee, this[, args...])}.
52e9e61d937f 8145550: Megamorphic invoke should use CompiledFunction variants without any LinkLogic
sundar
parents: 34735
diff changeset
   384
     * @param runtimeScope the runtime scope. It can be used to evaluate types of scoped variables to guide the
52e9e61d937f 8145550: Megamorphic invoke should use CompiledFunction variants without any LinkLogic
sundar
parents: 34735
diff changeset
   385
     * optimistic compilation, should the call to this method trigger code compilation. Can be null if current runtime
52e9e61d937f 8145550: Megamorphic invoke should use CompiledFunction variants without any LinkLogic
sundar
parents: 34735
diff changeset
   386
     * scope is not known, but that might cause compilation of code that will need more deoptimization passes.
52e9e61d937f 8145550: Megamorphic invoke should use CompiledFunction variants without any LinkLogic
sundar
parents: 34735
diff changeset
   387
     * @return the best function for the specified call site type.
52e9e61d937f 8145550: Megamorphic invoke should use CompiledFunction variants without any LinkLogic
sundar
parents: 34735
diff changeset
   388
     */
52e9e61d937f 8145550: Megamorphic invoke should use CompiledFunction variants without any LinkLogic
sundar
parents: 34735
diff changeset
   389
    final CompiledFunction getBest(final MethodType callSiteType, final ScriptObject runtimeScope, final Collection<CompiledFunction> forbidden) {
52e9e61d937f 8145550: Megamorphic invoke should use CompiledFunction variants without any LinkLogic
sundar
parents: 34735
diff changeset
   390
        return getBest(callSiteType, runtimeScope, forbidden, true);
52e9e61d937f 8145550: Megamorphic invoke should use CompiledFunction variants without any LinkLogic
sundar
parents: 34735
diff changeset
   391
    }
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   392
31827
c9984afe4a29 8131340: Varargs function is recompiled each time it is linked
hannesw
parents: 30393
diff changeset
   393
    boolean isValidCallSite(final MethodType callSiteType) {
c9984afe4a29 8131340: Varargs function is recompiled each time it is linked
hannesw
parents: 30393
diff changeset
   394
        return callSiteType.parameterCount() >= 2  && // Must have at least (callee, this)
c9984afe4a29 8131340: Varargs function is recompiled each time it is linked
hannesw
parents: 30393
diff changeset
   395
               callSiteType.parameterType(0).isAssignableFrom(ScriptFunction.class); // Callee must be assignable from script function
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   396
    }
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   397
24729
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24727
diff changeset
   398
    CompiledFunction getGeneric(final ScriptObject runtimeScope) {
34736
52e9e61d937f 8145550: Megamorphic invoke should use CompiledFunction variants without any LinkLogic
sundar
parents: 34735
diff changeset
   399
        return getBest(getGenericType(), runtimeScope, CompiledFunction.NO_FUNCTIONS, false);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   400
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   401
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   402
    /**
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   403
     * Get a method type for a generic invoker.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   404
     * @return the method type for the generic invoker
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   405
     */
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   406
    abstract MethodType getGenericType();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   407
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   408
    /**
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   409
     * Allocates an object using this function's allocator.
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21458
diff changeset
   410
     *
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21458
diff changeset
   411
     * @param map the property map for the allocated object.
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   412
     * @return the object allocated using this function's allocator, or null if the function doesn't have an allocator.
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   413
     */
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21458
diff changeset
   414
    ScriptObject allocate(final PropertyMap map) {
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21458
diff changeset
   415
        return null;
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21458
diff changeset
   416
    }
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21458
diff changeset
   417
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21458
diff changeset
   418
    /**
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21458
diff changeset
   419
     * Get the property map to use for objects allocated by this function.
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21458
diff changeset
   420
     *
32695
9b708b92c695 8134609: Allow constructors with same prototoype map to share the allocator map
hannesw
parents: 32534
diff changeset
   421
     * @param prototype the prototype of the allocated object
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21458
diff changeset
   422
     * @return the property map for allocated objects.
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 21458
diff changeset
   423
     */
32695
9b708b92c695 8134609: Allow constructors with same prototoype map to share the allocator map
hannesw
parents: 32534
diff changeset
   424
    PropertyMap getAllocatorMap(final ScriptObject prototype) {
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   425
        return null;
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   426
    }
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   427
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   428
    /**
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   429
     * This method is used to create the immutable portion of a bound function.
32527
b105632002c5 8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents: 31827
diff changeset
   430
     * See {@link ScriptFunction#createBound(Object, Object[])}
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   431
     *
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   432
     * @param fn the original function being bound
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   433
     * @param self this reference to bind. Can be null.
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   434
     * @param args additional arguments to bind. Can be null.
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   435
     */
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   436
    ScriptFunctionData makeBoundFunctionData(final ScriptFunction fn, final Object self, final Object[] args) {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   437
        final Object[] allArgs = args == null ? ScriptRuntime.EMPTY_ARRAY : args;
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   438
        final int length = args == null ? 0 : args.length;
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   439
        // Clear the callee and this flags
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   440
        final int boundFlags = flags & ~NEEDS_CALLEE & ~USES_THIS;
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   441
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   442
        final List<CompiledFunction> boundList = new LinkedList<>();
24729
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24727
diff changeset
   443
        final ScriptObject runtimeScope = fn.getScope();
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26503
diff changeset
   444
        final CompiledFunction bindTarget = new CompiledFunction(getGenericInvoker(runtimeScope), getGenericConstructor(runtimeScope), null);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   445
        boundList.add(bind(bindTarget, fn, self, allArgs));
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   446
24727
attila
parents: 24720 23375
diff changeset
   447
        return new FinalScriptFunctionData(name, Math.max(0, getArity() - length), boundList, boundFlags);
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   448
    }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   449
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   450
    /**
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   451
     * Convert this argument for non-strict functions according to ES 10.4.3
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   452
     *
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   453
     * @param thiz the this argument
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   454
     *
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   455
     * @return the converted this object
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   456
     */
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   457
    private Object convertThisObject(final Object thiz) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   458
        return needsWrappedThis() ? wrapThis(thiz) : thiz;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   459
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   460
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   461
    static Object wrapThis(final Object thiz) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   462
        if (!(thiz instanceof ScriptObject)) {
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   463
            if (JSType.nullOrUndefined(thiz)) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23372
diff changeset
   464
                return Context.getGlobal();
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   465
            }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   466
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   467
            if (isPrimitiveThis(thiz)) {
23375
a1110f2cbe75 8037400: Remove getInitialMap getters and GlobalObject interface
sundar
parents: 23372
diff changeset
   468
                return Context.getGlobal().wrapAsObject(thiz);
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   469
            }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   470
        }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   471
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   472
        return thiz;
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   473
    }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   474
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   475
    static boolean isPrimitiveThis(final Object obj) {
29282
a8523237b66c 8074031: Canonicalize is-a-JS-string tests
attila
parents: 26768
diff changeset
   476
        return JSType.isString(obj) || obj instanceof Number || obj instanceof Boolean;
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   477
    }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   478
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   479
    /**
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   480
     * Creates an invoker method handle for a bound function.
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   481
     *
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   482
     * @param targetFn the function being bound
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   483
     * @param originalInvoker an original invoker method handle for the function. This can be its generic invoker or
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   484
     * any of its specializations.
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   485
     * @param self the "this" value being bound
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   486
     * @param args additional arguments being bound
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   487
     *
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   488
     * @return a bound invoker method handle that will bind the self value and the specified arguments. The resulting
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   489
     * invoker never needs a callee; if the original invoker needed it, it will be bound to {@code fn}. The resulting
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   490
     * invoker still takes an initial {@code this} parameter, but it is always dropped and the bound {@code self} passed
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   491
     * to the original invoker on invocation.
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   492
     */
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   493
    private MethodHandle bindInvokeHandle(final MethodHandle originalInvoker, final ScriptFunction targetFn, final Object self, final Object[] args) {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   494
        // Is the target already bound? If it is, we won't bother binding either callee or self as they're already bound
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   495
        // in the target and will be ignored anyway.
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   496
        final boolean isTargetBound = targetFn.isBoundFunction();
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   497
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   498
        final boolean needsCallee = needsCallee(originalInvoker);
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   499
        assert needsCallee == needsCallee() : "callee contract violation 2";
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   500
        assert !(isTargetBound && needsCallee); // already bound functions don't need a callee
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   501
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   502
        final Object boundSelf = isTargetBound ? null : convertThisObject(self);
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   503
        final MethodHandle boundInvoker;
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   504
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   505
        if (isVarArg(originalInvoker)) {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   506
            // First, bind callee and this without arguments
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   507
            final MethodHandle noArgBoundInvoker;
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   508
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   509
            if (isTargetBound) {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   510
                // Don't bind either callee or this
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   511
                noArgBoundInvoker = originalInvoker;
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   512
            } else if (needsCallee) {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   513
                // Bind callee and this
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   514
                noArgBoundInvoker = MH.insertArguments(originalInvoker, 0, targetFn, boundSelf);
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   515
            } else {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   516
                // Only bind this
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   517
                noArgBoundInvoker = MH.bindTo(originalInvoker, boundSelf);
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   518
            }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   519
            // Now bind arguments
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   520
            if (args.length > 0) {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   521
                boundInvoker = varArgBinder(noArgBoundInvoker, args);
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   522
            } else {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   523
                boundInvoker = noArgBoundInvoker;
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   524
            }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   525
        } else {
17744
65849f87aa28 8011718: binding already bound function with extra arguments fails
attila
parents: 16523
diff changeset
   526
            // If target is already bound, insert additional bound arguments after "this" argument, at position 1.
65849f87aa28 8011718: binding already bound function with extra arguments fails
attila
parents: 16523
diff changeset
   527
            final int argInsertPos = isTargetBound ? 1 : 0;
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   528
            final Object[] boundArgs = new Object[Math.min(originalInvoker.type().parameterCount() - argInsertPos, args.length + (isTargetBound ? 0 : needsCallee  ? 2 : 1))];
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   529
            int next = 0;
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   530
            if (!isTargetBound) {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   531
                if (needsCallee) {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   532
                    boundArgs[next++] = targetFn;
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   533
                }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   534
                boundArgs[next++] = boundSelf;
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   535
            }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   536
            // If more bound args were specified than the function can take, we'll just drop those.
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   537
            System.arraycopy(args, 0, boundArgs, next, boundArgs.length - next);
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   538
            // If target is already bound, insert additional bound arguments after "this" argument, at position 1;
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   539
            // "this" will get dropped anyway by the target invoker. We previously asserted that already bound functions
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   540
            // don't take a callee parameter, so we can know that the signature is (this[, args...]) therefore args
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   541
            // start at position 1. If the function is not bound, we start inserting arguments at position 0.
17744
65849f87aa28 8011718: binding already bound function with extra arguments fails
attila
parents: 16523
diff changeset
   542
            boundInvoker = MH.insertArguments(originalInvoker, argInsertPos, boundArgs);
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   543
        }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   544
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   545
        if (isTargetBound) {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   546
            return boundInvoker;
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   547
        }
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   548
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   549
        // If the target is not already bound, add a dropArguments that'll throw away the passed this
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   550
        return MH.dropArguments(boundInvoker, 0, Object.class);
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   551
    }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   552
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   553
    /**
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   554
     * Creates a constructor method handle for a bound function using the passed constructor handle.
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   555
     *
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   556
     * @param originalConstructor the constructor handle to bind. It must be a composed constructor.
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   557
     * @param fn the function being bound
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   558
     * @param args arguments being bound
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   559
     *
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   560
     * @return a bound constructor method handle that will bind the specified arguments. The resulting constructor never
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   561
     * needs a callee; if the original constructor needed it, it will be bound to {@code fn}. The resulting constructor
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   562
     * still takes an initial {@code this} parameter and passes it to the underlying original constructor. Finally, if
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   563
     * this script function data object has no constructor handle, null is returned.
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   564
     */
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   565
    private static MethodHandle bindConstructHandle(final MethodHandle originalConstructor, final ScriptFunction fn, final Object[] args) {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   566
        assert originalConstructor != null;
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   567
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   568
        // If target function is already bound, don't bother binding the callee.
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   569
        final MethodHandle calleeBoundConstructor = fn.isBoundFunction() ? originalConstructor :
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   570
            MH.dropArguments(MH.bindTo(originalConstructor, fn), 0, ScriptFunction.class);
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   571
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   572
        if (args.length == 0) {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   573
            return calleeBoundConstructor;
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   574
        }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   575
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   576
        if (isVarArg(calleeBoundConstructor)) {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   577
            return varArgBinder(calleeBoundConstructor, args);
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   578
        }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   579
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   580
        final Object[] boundArgs;
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   581
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   582
        final int maxArgCount = calleeBoundConstructor.type().parameterCount() - 1;
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   583
        if (args.length <= maxArgCount) {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   584
            boundArgs = args;
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   585
        } else {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   586
            boundArgs = new Object[maxArgCount];
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   587
            System.arraycopy(args, 0, boundArgs, 0, maxArgCount);
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   588
        }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   589
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   590
        return MH.insertArguments(calleeBoundConstructor, 1, boundArgs);
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   591
    }
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   592
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   593
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   594
     * Takes a method handle, and returns a potentially different method handle that can be used in
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   595
     * {@code ScriptFunction#invoke(Object, Object...)} or {code ScriptFunction#construct(Object, Object...)}.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   596
     * The returned method handle will be sure to return {@code Object}, and will have all its parameters turned into
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   597
     * {@code Object} as well, except for the following ones:
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   598
     * <ul>
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   599
     *   <li>a last parameter of type {@code Object[]} which is used for vararg functions,</li>
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   600
     *   <li>the first argument, which is forced to be {@link ScriptFunction}, in case the function receives itself
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   601
     *   (callee) as an argument.</li>
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   602
     * </ul>
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   603
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   604
     * @param mh the original method handle
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   605
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   606
     * @return the new handle, conforming to the rules above.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   607
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   608
    private static MethodHandle makeGenericMethod(final MethodHandle mh) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   609
        final MethodType type = mh.type();
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   610
        final MethodType newType = makeGenericType(type);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   611
        return type.equals(newType) ? mh : mh.asType(newType);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   612
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   613
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   614
    private static MethodType makeGenericType(final MethodType type) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   615
        MethodType newType = type.generic();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   616
        if (isVarArg(type)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   617
            newType = newType.changeParameterType(type.parameterCount() - 1, Object[].class);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   618
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   619
        if (needsCallee(type)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   620
            newType = newType.changeParameterType(0, ScriptFunction.class);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   621
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   622
        return newType;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   623
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   624
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   625
    /**
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   626
     * Execute this script function.
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   627
     *
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   628
     * @param self  Target object.
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   629
     * @param arguments  Call arguments.
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   630
     * @return ScriptFunction result.
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   631
     *
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   632
     * @throws Throwable if there is an exception/error with the invocation or thrown from it
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   633
     */
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   634
    Object invoke(final ScriptFunction fn, final Object self, final Object... arguments) throws Throwable {
24729
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24727
diff changeset
   635
        final MethodHandle mh      = getGenericInvoker(fn.getScope());
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   636
        final Object       selfObj = convertThisObject(self);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   637
        final Object[]     args    = arguments == null ? ScriptRuntime.EMPTY_ARRAY : arguments;
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   638
24881
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents: 24769
diff changeset
   639
        DebuggerSupport.notifyInvoke(mh);
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents: 24769
diff changeset
   640
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   641
        if (isVarArg(mh)) {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   642
            if (needsCallee(mh)) {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   643
                return mh.invokeExact(fn, selfObj, args);
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   644
            }
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   645
            return mh.invokeExact(selfObj, args);
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   646
        }
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   647
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   648
        final int paramCount = mh.type().parameterCount();
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   649
        if (needsCallee(mh)) {
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   650
            switch (paramCount) {
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   651
            case 2:
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   652
                return mh.invokeExact(fn, selfObj);
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   653
            case 3:
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   654
                return mh.invokeExact(fn, selfObj, getArg(args, 0));
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   655
            case 4:
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   656
                return mh.invokeExact(fn, selfObj, getArg(args, 0), getArg(args, 1));
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   657
            case 5:
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   658
                return mh.invokeExact(fn, selfObj, getArg(args, 0), getArg(args, 1), getArg(args, 2));
21458
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   659
            case 6:
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   660
                return mh.invokeExact(fn, selfObj, getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3));
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   661
            case 7:
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   662
                return mh.invokeExact(fn, selfObj, getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3), getArg(args, 4));
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   663
            case 8:
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   664
                return mh.invokeExact(fn, selfObj, getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3), getArg(args, 4), getArg(args, 5));
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   665
            default:
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   666
                return mh.invokeWithArguments(withArguments(fn, selfObj, paramCount, args));
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   667
            }
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   668
        }
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   669
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   670
        switch (paramCount) {
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   671
        case 1:
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   672
            return mh.invokeExact(selfObj);
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   673
        case 2:
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   674
            return mh.invokeExact(selfObj, getArg(args, 0));
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   675
        case 3:
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   676
            return mh.invokeExact(selfObj, getArg(args, 0), getArg(args, 1));
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   677
        case 4:
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   678
            return mh.invokeExact(selfObj, getArg(args, 0), getArg(args, 1), getArg(args, 2));
21458
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   679
        case 5:
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   680
            return mh.invokeExact(selfObj, getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3));
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   681
        case 6:
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   682
            return mh.invokeExact(selfObj, getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3), getArg(args, 4));
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   683
        case 7:
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   684
            return mh.invokeExact(selfObj, getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3), getArg(args, 4), getArg(args, 5));
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   685
        default:
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   686
            return mh.invokeWithArguments(withArguments(null, selfObj, paramCount, args));
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   687
        }
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   688
    }
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   689
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   690
    Object construct(final ScriptFunction fn, final Object... arguments) throws Throwable {
24729
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24727
diff changeset
   691
        final MethodHandle mh   = getGenericConstructor(fn.getScope());
21458
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   692
        final Object[]     args = arguments == null ? ScriptRuntime.EMPTY_ARRAY : arguments;
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   693
24881
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents: 24769
diff changeset
   694
        DebuggerSupport.notifyInvoke(mh);
67cf2d94e00a 8044798: API for debugging Nashorn
sundar
parents: 24769
diff changeset
   695
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   696
        if (isVarArg(mh)) {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   697
            if (needsCallee(mh)) {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   698
                return mh.invokeExact(fn, args);
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   699
            }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   700
            return mh.invokeExact(args);
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   701
        }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   702
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   703
        final int paramCount = mh.type().parameterCount();
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   704
        if (needsCallee(mh)) {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   705
            switch (paramCount) {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   706
            case 1:
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   707
                return mh.invokeExact(fn);
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   708
            case 2:
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   709
                return mh.invokeExact(fn, getArg(args, 0));
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   710
            case 3:
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   711
                return mh.invokeExact(fn, getArg(args, 0), getArg(args, 1));
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   712
            case 4:
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   713
                return mh.invokeExact(fn, getArg(args, 0), getArg(args, 1), getArg(args, 2));
21458
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   714
            case 5:
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   715
                return mh.invokeExact(fn, getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3));
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   716
            case 6:
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   717
                return mh.invokeExact(fn, getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3), getArg(args, 4));
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   718
            case 7:
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   719
                return mh.invokeExact(fn, getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3), getArg(args, 4), getArg(args, 5));
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   720
            default:
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   721
                return mh.invokeWithArguments(withArguments(fn, paramCount, args));
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   722
            }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   723
        }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   724
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   725
        switch (paramCount) {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   726
        case 0:
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   727
            return mh.invokeExact();
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   728
        case 1:
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   729
            return mh.invokeExact(getArg(args, 0));
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   730
        case 2:
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   731
            return mh.invokeExact(getArg(args, 0), getArg(args, 1));
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   732
        case 3:
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   733
            return mh.invokeExact(getArg(args, 0), getArg(args, 1), getArg(args, 2));
21458
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   734
        case 4:
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   735
            return mh.invokeExact(getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3));
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   736
        case 5:
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   737
            return mh.invokeExact(getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3), getArg(args, 4));
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   738
        case 6:
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   739
            return mh.invokeExact(getArg(args, 0), getArg(args, 1), getArg(args, 2), getArg(args, 3), getArg(args, 4), getArg(args, 5));
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   740
        default:
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   741
            return mh.invokeWithArguments(withArguments(null, paramCount, args));
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   742
        }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   743
    }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   744
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   745
    private static Object getArg(final Object[] args, final int i) {
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   746
        return i < args.length ? args[i] : UNDEFINED;
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   747
    }
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   748
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   749
    private static Object[] withArguments(final ScriptFunction fn, final int argCount, final Object[] args) {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   750
        final Object[] finalArgs = new Object[argCount];
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   751
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   752
        int nextArg = 0;
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   753
        if (fn != null) {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   754
            //needs callee
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   755
            finalArgs[nextArg++] = fn;
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   756
        }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   757
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   758
        // Don't add more args that there is argCount in the handle (including self and callee).
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   759
        for (int i = 0; i < args.length && nextArg < argCount;) {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   760
            finalArgs[nextArg++] = args[i++];
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   761
        }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   762
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   763
        // If we have fewer args than argCount, pad with undefined.
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   764
        while (nextArg < argCount) {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   765
            finalArgs[nextArg++] = UNDEFINED;
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   766
        }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   767
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   768
        return finalArgs;
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   769
    }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17756
diff changeset
   770
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   771
    private static Object[] withArguments(final ScriptFunction fn, final Object self, final int argCount, final Object[] args) {
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   772
        final Object[] finalArgs = new Object[argCount];
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   773
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   774
        int nextArg = 0;
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   775
        if (fn != null) {
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   776
            //needs callee
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   777
            finalArgs[nextArg++] = fn;
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   778
        }
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   779
        finalArgs[nextArg++] = self;
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   780
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   781
        // Don't add more args that there is argCount in the handle (including self and callee).
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   782
        for (int i = 0; i < args.length && nextArg < argCount;) {
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   783
            finalArgs[nextArg++] = args[i++];
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   784
        }
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   785
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   786
        // If we have fewer args than argCount, pad with undefined.
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   787
        while (nextArg < argCount) {
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   788
            finalArgs[nextArg++] = UNDEFINED;
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   789
        }
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   790
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   791
        return finalArgs;
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   792
    }
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   793
    /**
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   794
     * Takes a variable-arity method and binds a variable number of arguments in it. The returned method will filter the
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   795
     * vararg array and pass a different array that prepends the bound arguments in front of the arguments passed on
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   796
     * invocation
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   797
     *
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   798
     * @param mh the handle
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   799
     * @param args the bound arguments
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   800
     *
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   801
     * @return the bound method handle
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   802
     */
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   803
    private static MethodHandle varArgBinder(final MethodHandle mh, final Object[] args) {
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   804
        assert args != null;
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   805
        assert args.length > 0;
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   806
        return MH.filterArguments(mh, mh.type().parameterCount() - 1, MH.bindTo(BIND_VAR_ARGS, args));
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   807
    }
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   808
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   809
    /**
21458
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   810
     * Heuristic to figure out if the method handle has a callee argument. If it's type is
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   811
     * {@code (ScriptFunction, ...)}, then we'll assume it has a callee argument. We need this as
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   812
     * the constructor above is not passed this information, and can't just blindly assume it's false
eec7878b0dcd 8027301: Optimizations for Function.prototype.apply
hannesw
parents: 20946
diff changeset
   813
     * (notably, it's being invoked for creation of new scripts, and scripts have scopes, therefore
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   814
     * they also always receive a callee).
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   815
     *
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   816
     * @param mh the examined method handle
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   817
     *
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   818
     * @return true if the method handle expects a callee, false otherwise
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   819
     */
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   820
    protected static boolean needsCallee(final MethodHandle mh) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   821
        return needsCallee(mh.type());
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   822
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   823
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   824
    static boolean needsCallee(final MethodType type) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   825
        final int length = type.parameterCount();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   826
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   827
        if (length == 0) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   828
            return false;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   829
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   830
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   831
        final Class<?> param0 = type.parameterType(0);
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   832
        return param0 == ScriptFunction.class || param0 == boolean.class && length > 1 && type.parameterType(1) == ScriptFunction.class;
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   833
    }
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   834
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   835
    /**
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   836
     * Check if a javascript function methodhandle is a vararg handle
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   837
     *
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   838
     * @param mh method handle to check
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   839
     *
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   840
     * @return true if vararg
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   841
     */
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   842
    protected static boolean isVarArg(final MethodHandle mh) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   843
        return isVarArg(mh.type());
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   844
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   845
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 21458
diff changeset
   846
    static boolean isVarArg(final MethodType type) {
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   847
        return type.parameterType(type.parameterCount() - 1).isArray();
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   848
    }
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   849
24749
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24738
diff changeset
   850
    /**
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24738
diff changeset
   851
     * Is this ScriptFunction declared in a dynamic context
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24738
diff changeset
   852
     * @return true if in dynamic context, false if not or irrelevant
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24738
diff changeset
   853
     */
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24738
diff changeset
   854
    public boolean inDynamicContext() {
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24738
diff changeset
   855
        return false;
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24738
diff changeset
   856
    }
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24738
diff changeset
   857
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   858
    @SuppressWarnings("unused")
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   859
    private static Object[] bindVarArgs(final Object[] array1, final Object[] array2) {
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   860
        if (array2 == null) {
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   861
            // Must clone it, as we can't allow the receiving method to alter the array
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   862
            return array1.clone();
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   863
        }
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   864
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   865
        final int l2 = array2.length;
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   866
        if (l2 == 0) {
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   867
            return array1.clone();
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   868
        }
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   869
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   870
        final int l1 = array1.length;
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   871
        final Object[] concat = new Object[l1 + l2];
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   872
        System.arraycopy(array1, 0, concat, 0, l1);
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   873
        System.arraycopy(array2, 0, concat, l1, l2);
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16277
diff changeset
   874
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   875
        return concat;
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   876
    }
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   877
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   878
    private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   879
        return MH.findStatic(MethodHandles.lookup(), ScriptFunctionData.class, name, MH.type(rtype, types));
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   880
    }
24753
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   881
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   882
    /**
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   883
     * This class is used to hold the generic invoker and generic constructor pair. It is structured in this way since
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   884
     * most functions will never use them, so this way ScriptFunctionData only pays storage cost for one null reference
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   885
     * to the GenericInvokers object, instead of two null references for the two method handles.
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   886
     */
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   887
    private static final class GenericInvokers {
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   888
        volatile MethodHandle invoker;
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   889
        volatile MethodHandle constructor;
675feda2f82d 8043003: Use strongly referenced generic invokers
attila
parents: 24749
diff changeset
   890
    }
24769
attila
parents: 24753 23767
diff changeset
   891
attila
parents: 24753 23767
diff changeset
   892
    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
attila
parents: 24753 23767
diff changeset
   893
        in.defaultReadObject();
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 26053
diff changeset
   894
        code = new LinkedList<>();
24769
attila
parents: 24753 23767
diff changeset
   895
    }
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents:
diff changeset
   896
}