nashorn/src/jdk/nashorn/internal/runtime/ScriptFunction.java
author sundar
Thu, 11 Jul 2013 22:58:37 +0530
changeset 18868 f5359cad148c
parent 18614 addca7a10167
child 19456 8cc345d620c8
permissions -rw-r--r--
8012191: noSuchProperty can't cope with vararg functions Reviewed-by: jlaskey, attila
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    26
package jdk.nashorn.internal.runtime;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    28
import static jdk.nashorn.internal.codegen.CompilerConstants.virtualCallNoLookup;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
16277
fd698c5ee684 8009559: clean up method handle lookup code.
sundar
parents: 16256
diff changeset
    31
import static jdk.nashorn.internal.lookup.Lookup.MH;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
import java.lang.invoke.MethodHandle;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
import java.lang.invoke.MethodHandles;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
import java.lang.invoke.MethodType;
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
    36
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16233
diff changeset
    37
import jdk.internal.dynalink.CallSiteDescriptor;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16233
diff changeset
    38
import jdk.internal.dynalink.linker.GuardedInvocation;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16233
diff changeset
    39
import jdk.internal.dynalink.linker.LinkRequest;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
import jdk.nashorn.internal.codegen.CompilerConstants.Call;
16277
fd698c5ee684 8009559: clean up method handle lookup code.
sundar
parents: 16256
diff changeset
    41
import jdk.nashorn.internal.lookup.MethodHandleFactory;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
import jdk.nashorn.internal.runtime.linker.NashornGuards;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    45
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
 * Runtime representation of a JavaScript function.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
public abstract class ScriptFunction extends ScriptObject {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
    /** Method handle for prototype getter for this ScriptFunction */
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
    51
    public static final MethodHandle G$PROTOTYPE = findOwnMH("G$prototype", Object.class, Object.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
    /** Method handle for prototype setter for this ScriptFunction */
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
    54
    public static final MethodHandle S$PROTOTYPE = findOwnMH("S$prototype", void.class, Object.class, Object.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    55
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
    /** Method handle for length getter for this ScriptFunction */
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
    57
    public static final MethodHandle G$LENGTH = findOwnMH("G$length", int.class, Object.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
    /** Method handle for name getter for this ScriptFunction */
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
    60
    public static final MethodHandle G$NAME = findOwnMH("G$name", Object.class, Object.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
    /** Method handle for allocate function for this ScriptFunction */
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
    63
    static final MethodHandle ALLOCATE = findOwnMH("allocate", Object.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
16207
ed4aec2d599c 8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents: 16206
diff changeset
    65
    private static final MethodHandle WRAPFILTER = findOwnMH("wrapFilter", Object.class, Object.class);
ed4aec2d599c 8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents: 16206
diff changeset
    66
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
    /** method handle to scope getter for this ScriptFunction */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
    public static final Call GET_SCOPE = virtualCallNoLookup(ScriptFunction.class, "getScope", ScriptObject.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
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
    70
    private static final MethodHandle IS_FUNCTION_MH  = findOwnMH("isFunctionMH", boolean.class, Object.class, ScriptFunctionData.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
    71
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
    72
    private static final MethodHandle IS_NONSTRICT_FUNCTION = findOwnMH("isNonStrictFunction", boolean.class, Object.class, Object.class, ScriptFunctionData.class);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
18868
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
    74
    private static final MethodHandle ADD_ZEROTH_ELEMENT = findOwnMH("addZerothElement", Object[].class, Object[].class, Object.class);
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
    75
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
    /** The parent scope. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
    private final ScriptObject scope;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    78
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
    79
    private final ScriptFunctionData data;
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
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
     * Constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
     *
16233
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16232
diff changeset
    84
     * @param name          function name
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16232
diff changeset
    85
     * @param methodHandle  method handle to function (if specializations are present, assumed to be most generic)
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16232
diff changeset
    86
     * @param map           property map
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16232
diff changeset
    87
     * @param scope         scope
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16232
diff changeset
    88
     * @param specs         specialized version of this function - other method handles
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16232
diff changeset
    89
     * @param strict        is this a strict mode function?
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16232
diff changeset
    90
     * @param builtin       is this a built in function?
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16232
diff changeset
    91
     * @param isConstructor is this a constructor?
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    92
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    93
    protected ScriptFunction(
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
            final String name,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
            final MethodHandle methodHandle,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
            final PropertyMap map,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
            final ScriptObject scope,
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents: 16210
diff changeset
    98
            final MethodHandle[] specs,
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents: 16210
diff changeset
    99
            final boolean strict,
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   100
            final boolean builtin,
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   101
            final boolean isConstructor) {
16206
83069fa0935b 8006529: Methods always get callee - it should be conditional
attila
parents: 16202
diff changeset
   102
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
   103
        this(new FinalScriptFunctionData(name, methodHandle, specs, strict, builtin, isConstructor), map, scope);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   104
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   106
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   107
     * Constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
     *
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents: 16210
diff changeset
   109
     * @param data          static function data
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   110
     * @param map           property map
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   111
     * @param scope         scope
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   112
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   113
    protected ScriptFunction(
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents: 16210
diff changeset
   114
            final ScriptFunctionData data,
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
            final PropertyMap map,
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents: 16210
diff changeset
   116
            final ScriptObject scope) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
        super(map);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
        if (Context.DEBUG) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
            constructorCount++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   123
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
   124
        this.data  = data;
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
   125
        this.scope = scope;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   128
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
    public String getClassName() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
        return "Function";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
     * ECMA 15.3.5.3 [[HasInstance]] (V)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
     * Step 3 if "prototype" value is not an Object, throw TypeError
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   138
    public boolean isInstance(final ScriptObject instance) {
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   139
        final Object basePrototype = getTargetFunction().getPrototype();
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   140
        if (!(basePrototype instanceof ScriptObject)) {
16256
f2d9a0c49914 8007002: Replace implicit exception throwing methods with explicit throws - simplify control flow and remove useless code
lagergren
parents: 16234
diff changeset
   141
            throw typeError("prototype.not.an.object", ScriptRuntime.safeToString(getTargetFunction()), ScriptRuntime.safeToString(basePrototype));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   142
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   143
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
        for (ScriptObject proto = instance.getProto(); proto != null; proto = proto.getProto()) {
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   145
            if (proto == basePrototype) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
                return true;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   147
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
        return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
    /**
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   154
     * Returns the target function for this function. If the function was not created using
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   155
     * {@link #makeBoundFunction(Object, Object[])}, its target function is itself. If it is bound, its target function
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   156
     * is the target function of the function it was made from (therefore, the target function is always the final,
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   157
     * unbound recipient of the calls).
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   158
     * @return the target function for this function.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
     */
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   160
    protected ScriptFunction getTargetFunction() {
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   161
        return this;
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   162
    }
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   163
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   164
    boolean isBoundFunction() {
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   165
        return getTargetFunction() != this;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   166
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   167
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   168
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   169
     * Set the arity of this ScriptFunction
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   170
     * @param arity arity
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   171
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   172
    public final void setArity(final int arity) {
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents: 16210
diff changeset
   173
        data.setArity(arity);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   174
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
     * Is this a ECMAScript 'use strict' function?
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
     * @return true if function is in strict mode
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
     */
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents: 16210
diff changeset
   180
    public boolean isStrict() {
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents: 16210
diff changeset
   181
        return data.isStrict();
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents: 16210
diff changeset
   182
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   184
    /**
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents: 16210
diff changeset
   185
     * 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: 16210
diff changeset
   186
     * according to ECMA 10.4.3.
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents: 16210
diff changeset
   187
     * @return true if this argument must be an object
16186
91bd9d2aa2f5 8006570: This-value for non-strict functions should be converted to object
hannesw
parents: 16173
diff changeset
   188
     */
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents: 16210
diff changeset
   189
    public boolean needsWrappedThis() {
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents: 16210
diff changeset
   190
        return data.needsWrappedThis();
16186
91bd9d2aa2f5 8006570: This-value for non-strict functions should be converted to object
hannesw
parents: 16173
diff changeset
   191
    }
91bd9d2aa2f5 8006570: This-value for non-strict functions should be converted to object
hannesw
parents: 16173
diff changeset
   192
91bd9d2aa2f5 8006570: This-value for non-strict functions should be converted to object
hannesw
parents: 16173
diff changeset
   193
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   194
     * Execute this script function.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   195
     * @param self  Target object.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   196
     * @param arguments  Call arguments.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   197
     * @return ScriptFunction result.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   198
     * @throws Throwable if there is an exception/error with the invocation or thrown from it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   199
     */
16232
efd57dd90de6 8008197: Cross script engine function calls do not work as expected
sundar
parents: 16228
diff changeset
   200
    Object invoke(final Object self, final Object... arguments) throws Throwable {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   201
        if (Context.DEBUG) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   202
            invokes++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   203
        }
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   204
        return data.invoke(this, self, arguments);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   206
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   207
    /**
18614
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17252
diff changeset
   208
     * Execute this script function as a constructor.
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17252
diff changeset
   209
     * @param arguments  Call arguments.
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17252
diff changeset
   210
     * @return Newly constructed result.
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17252
diff changeset
   211
     * @throws Throwable if there is an exception/error with the invocation or thrown from it
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17252
diff changeset
   212
     */
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17252
diff changeset
   213
    Object construct(final Object... arguments) throws Throwable {
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17252
diff changeset
   214
        return data.construct(this, arguments);
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17252
diff changeset
   215
    }
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17252
diff changeset
   216
addca7a10167 8015959: Can't call foreign constructor
sundar
parents: 17252
diff changeset
   217
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   218
     * Allocate function. Called from generated {@link ScriptObject} code
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   219
     * for allocation as a factory method
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   220
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   221
     * @return a new instance of the {@link ScriptObject} whose allocator this is
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   222
     */
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   223
    @SuppressWarnings("unused")
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   224
    private Object allocate() {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   225
        if (Context.DEBUG) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   226
            allocations++;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   227
        }
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   228
        assert !isBoundFunction(); // allocate never invoked on bound functions
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   229
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   230
        final ScriptObject object = data.allocate();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   231
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   232
        if (object != null) {
17252
9aeb443c4740 8006559: Octane:pdfjs leaks memory, runs slower iteration to iteration
hannesw
parents: 16523
diff changeset
   233
            Object prototype = getPrototype();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   234
            if (prototype instanceof ScriptObject) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   235
                object.setProto((ScriptObject)prototype);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   236
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   237
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   238
            if (object.getProto() == null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   239
                object.setProto(getObjectPrototype());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   240
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   241
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   242
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   243
        return object;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   244
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   245
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   246
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   247
     * Return Object.prototype - used by "allocate"
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   248
     * @return Object.prototype
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   249
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   250
    protected abstract ScriptObject getObjectPrototype();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   251
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   252
    /**
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   253
     * Creates a version of this function bound to a specific "self" and other arguments, as per
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   254
     * {@code Function.prototype.bind} functionality in ECMAScript 5.1 section 15.3.4.5.
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   255
     * @param self the self to bind to this function. Can be null (in which case, null is bound as this).
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   256
     * @param args additional arguments to bind to this function. Can be null or empty to not bind additional arguments.
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   257
     * @return a function with the specified self and parameters bound.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   258
     */
16233
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16232
diff changeset
   259
    protected ScriptFunction makeBoundFunction(final Object self, final Object[] args) {
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   260
        return makeBoundFunction(data.makeBoundFunctionData(this, self, args));
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   261
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   262
16233
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16232
diff changeset
   263
    /**
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16232
diff changeset
   264
     * Create a version of this function as in {@link ScriptFunction#makeBoundFunction(Object, Object[])},
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16232
diff changeset
   265
     * but using a {@link ScriptFunctionData} for the bound data.
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16232
diff changeset
   266
     *
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16232
diff changeset
   267
     * @param boundData ScriptFuntionData for the bound function
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16232
diff changeset
   268
     * @return a function with the bindings performed according to the given data
95d3e01c04c3 8008199: Lazy compilation and trampoline implementation
lagergren
parents: 16232
diff changeset
   269
     */
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   270
    protected abstract ScriptFunction makeBoundFunction(ScriptFunctionData boundData);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   271
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   272
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   273
    public final String safeToString() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   274
        return toSource();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   275
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   276
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   277
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   278
    public String toString() {
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents: 16210
diff changeset
   279
        return data.toString();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   280
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   281
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   282
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   283
     * Get this function as a String containing its source code. If no source code
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   284
     * exists in this ScriptFunction, its contents will be displayed as {@code [native code]}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   285
     * @return string representation of this function's source
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   286
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   287
    public final String toSource() {
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents: 16210
diff changeset
   288
        return data.toSource();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   289
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   290
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   291
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   292
     * Get the prototype object for this function
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   293
     * @return prototype
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   294
     */
17252
9aeb443c4740 8006559: Octane:pdfjs leaks memory, runs slower iteration to iteration
hannesw
parents: 16523
diff changeset
   295
    public abstract Object getPrototype();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   296
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   297
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   298
     * Set the prototype object for this function
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   299
     * @param prototype new prototype object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   300
     */
17252
9aeb443c4740 8006559: Octane:pdfjs leaks memory, runs slower iteration to iteration
hannesw
parents: 16523
diff changeset
   301
    public abstract void setPrototype(Object prototype);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   302
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   303
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   304
     * Return the most appropriate invoke handle if there are specializations
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   305
     * @param type most specific method type to look for invocation with
17252
9aeb443c4740 8006559: Octane:pdfjs leaks memory, runs slower iteration to iteration
hannesw
parents: 16523
diff changeset
   306
     * @param args args for trampoline invocation
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   307
     * @return invoke method handle
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   308
     */
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
   309
    private MethodHandle getBestInvoker(final MethodType type, 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
   310
        return data.getBestInvoker(type, args);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   311
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   312
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   313
    /**
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
   314
     * Return the most appropriate invoke handle if there are 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
   315
     * @param type most specific method type to look for invocation with
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
   316
     * @return invoke method handle
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   317
     */
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
   318
    public MethodHandle getBestInvoker(final MethodType type) {
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
   319
        return getBestInvoker(type, null);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   320
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   321
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   322
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   323
     * Return the invoke handle bound to a given ScriptObject self reference.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   324
     * If callee parameter is required result is rebound to this.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   325
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   326
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   327
     * @return bound invoke handle
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   328
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   329
    public final MethodHandle getBoundInvokeHandle(final ScriptObject self) {
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
   330
        return MH.bindTo(bindToCalleeIfNeeded(data.getGenericInvoker()), self);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   331
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   332
16225
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   333
    /**
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   334
     * Bind the method handle to this {@code ScriptFunction} instance if it needs a callee parameter. If this function's
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   335
     * method handles don't have a callee parameter, the handle is returned unchanged.
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   336
     * @param methodHandle the method handle to potentially bind to this function instance.
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   337
     * @return the potentially bound method handle
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   338
     */
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   339
    private MethodHandle bindToCalleeIfNeeded(final MethodHandle methodHandle) {
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
   340
        return ScriptFunctionData.needsCallee(methodHandle) ? MH.bindTo(methodHandle, this) : methodHandle;
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
   341
16225
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   342
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   343
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   344
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   345
     * Get the name for this function
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   346
     * @return the name
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   347
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   348
    public final String getName() {
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents: 16210
diff changeset
   349
        return data.getName();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   350
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   351
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   352
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   353
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   354
     * Get the scope for this function
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   355
     * @return the scope
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   356
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   357
    public final ScriptObject getScope() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   358
        return scope;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   359
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   360
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   361
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   362
     * Prototype getter for this ScriptFunction - follows the naming convention
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   363
     * used by Nasgen and the code generator
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   364
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   365
     * @param self  self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   366
     * @return self's prototype
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   367
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   368
    public static Object G$prototype(final Object self) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   369
        return (self instanceof ScriptFunction) ?
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   370
            ((ScriptFunction)self).getPrototype() :
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   371
            UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   372
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   373
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   374
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   375
     * Prototype setter for this ScriptFunction - follows the naming convention
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   376
     * used by Nasgen and the code generator
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   377
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   378
     * @param self  self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   379
     * @param prototype prototype to set
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   380
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   381
    public static void S$prototype(final Object self, final Object prototype) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   382
        if (self instanceof ScriptFunction) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   383
            ((ScriptFunction)self).setPrototype(prototype);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   384
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   385
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   386
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   387
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   388
     * Length getter - ECMA 15.3.3.2: Function.length
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   389
     * @param self self reference
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   390
     * @return length
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   391
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   392
    public static int G$length(final Object self) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   393
        if (self instanceof ScriptFunction) {
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   394
            return ((ScriptFunction)self).data.getArity();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   395
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   396
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   397
        return 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   398
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   399
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   400
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   401
     * Name getter - ECMA Function.name
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   402
     * @param self self refence
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   403
     * @return the name, or undefined if none
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   404
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   405
    public static Object G$name(final Object self) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   406
        if (self instanceof ScriptFunction) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   407
            return ((ScriptFunction)self).getName();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   408
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   409
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   410
        return UNDEFINED;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   411
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   412
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   413
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   414
     * Get the prototype for this ScriptFunction
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   415
     * @param constructor constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   416
     * @return prototype, or null if given constructor is not a ScriptFunction
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   417
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   418
    public static ScriptObject getPrototype(final Object constructor) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   419
        if (constructor instanceof ScriptFunction) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   420
            final Object proto = ((ScriptFunction)constructor).getPrototype();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   421
            if (proto instanceof ScriptObject) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   422
                return (ScriptObject)proto;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   423
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   424
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   425
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   426
        return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   427
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   428
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   429
    // These counters are updated only in debug mode.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   430
    private static int constructorCount;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   431
    private static int invokes;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   432
    private static int allocations;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   433
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   434
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   435
     * @return the constructorCount
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   436
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   437
    public static int getConstructorCount() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   438
        return constructorCount;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   439
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   440
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   441
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   442
     * @return the invokes
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   443
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   444
    public static int getInvokes() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   445
        return invokes;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   446
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   447
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   448
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   449
     * @return the allocations
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   450
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   451
    public static int getAllocations() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   452
        return allocations;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   453
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   454
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   455
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   456
    protected GuardedInvocation findNewMethod(final CallSiteDescriptor desc) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   457
        final MethodType type = desc.getMethodType();
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
   458
        return new GuardedInvocation(pairArguments(data.getBestConstructor(type.changeParameterType(0, ScriptFunction.class), null), type), null, getFunctionGuard(this));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   459
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   460
16207
ed4aec2d599c 8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents: 16206
diff changeset
   461
    @SuppressWarnings("unused")
ed4aec2d599c 8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents: 16206
diff changeset
   462
    private static Object wrapFilter(final Object obj) {
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   463
        if (obj instanceof ScriptObject || !ScriptFunctionData.isPrimitiveThis(obj)) {
16207
ed4aec2d599c 8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents: 16206
diff changeset
   464
            return obj;
ed4aec2d599c 8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents: 16206
diff changeset
   465
        }
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
   466
        return ((GlobalObject)Context.getGlobalTrusted()).wrapAsObject(obj);
16207
ed4aec2d599c 8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents: 16206
diff changeset
   467
    }
ed4aec2d599c 8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents: 16206
diff changeset
   468
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   469
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   470
     * dyn:call call site signature: (callee, thiz, [args...])
16225
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   471
     * generated method signature:   (callee, thiz, [args...])
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   472
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   473
     * cases:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   474
     * (a) method has callee parameter
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   475
     *   (1) for local/scope calls, we just bind thiz and drop the second argument.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   476
     *   (2) for normal this-calls, we have to swap thiz and callee to get matching signatures.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   477
     * (b) method doesn't have callee parameter (builtin functions)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   478
     *   (3) for local/scope calls, bind thiz and drop both callee and thiz.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   479
     *   (4) for normal this-calls, drop callee.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   480
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   481
    @Override
16195
3f6c0ab2597a 8006766: Array-like access to characters of a string is slow
hannesw
parents: 16188
diff changeset
   482
    protected GuardedInvocation findCallMethod(final CallSiteDescriptor desc, final LinkRequest request) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   483
        final MethodType type = desc.getMethodType();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   484
16195
3f6c0ab2597a 8006766: Array-like access to characters of a string is slow
hannesw
parents: 16188
diff changeset
   485
        if (request.isCallSiteUnstable()) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   486
            // (this, callee, args...) => (this, callee, args[])
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   487
            final MethodHandle collector = MH.asCollector(ScriptRuntime.APPLY.methodHandle(), Object[].class,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   488
                    type.parameterCount() - 2);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   489
16225
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   490
            // If call site is statically typed to take a ScriptFunction, we don't need a guard, otherwise we need a
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   491
            // generic "is this a ScriptFunction?" guard.
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   492
            return new GuardedInvocation(collector, ScriptFunction.class.isAssignableFrom(desc.getMethodType().parameterType(0))
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   493
                    ? null : NashornGuards.getScriptFunctionGuard());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   494
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   495
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   496
        MethodHandle boundHandle;
16207
ed4aec2d599c 8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents: 16206
diff changeset
   497
        MethodHandle guard = null;
ed4aec2d599c 8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents: 16206
diff changeset
   498
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents: 16210
diff changeset
   499
        if (data.needsCallee()) {
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
   500
            final MethodHandle callHandle = getBestInvoker(type, request.getArguments());
16225
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   501
            if (NashornCallSiteDescriptor.isScope(desc)) {
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   502
                // Make a handle that drops the passed "this" argument and substitutes either Global or Undefined
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   503
                // (callee, this, args...) => (callee, args...)
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   504
                boundHandle = MH.insertArguments(callHandle, 1, needsWrappedThis() ? Context.getGlobalTrusted() : ScriptRuntime.UNDEFINED);
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   505
                // (callee, args...) => (callee, [this], args...)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   506
                boundHandle = MH.dropArguments(boundHandle, 1, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   507
            } else {
16225
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   508
                // It's already (callee, this, args...), just what we need
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   509
                boundHandle = callHandle;
16207
ed4aec2d599c 8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents: 16206
diff changeset
   510
ed4aec2d599c 8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents: 16206
diff changeset
   511
                // For non-strict functions, check whether this-object is primitive type.
ed4aec2d599c 8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents: 16206
diff changeset
   512
                // If so add a to-object-wrapper argument filter.
ed4aec2d599c 8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents: 16206
diff changeset
   513
                // Else install a guard that will trigger a relink when the argument becomes primitive.
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents: 16210
diff changeset
   514
                if (needsWrappedThis()) {
16228
df28320aa080 8007900: Function binding is inefficient
attila
parents: 16226
diff changeset
   515
                    if (ScriptFunctionData.isPrimitiveThis(request.getArguments()[1])) {
16207
ed4aec2d599c 8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents: 16206
diff changeset
   516
                        boundHandle = MH.filterArguments(boundHandle, 1, WRAPFILTER);
ed4aec2d599c 8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents: 16206
diff changeset
   517
                    } else {
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
   518
                        guard = getNonStrictFunctionGuard(this);
16207
ed4aec2d599c 8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents: 16206
diff changeset
   519
                    }
ed4aec2d599c 8007060: Primitive wrap filter throws ClassCastException in test262parallel
hannesw
parents: 16206
diff changeset
   520
                }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   521
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   522
        } else {
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
   523
            final MethodHandle callHandle = getBestInvoker(type.dropParameterTypes(0, 1), request.getArguments());
16225
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   524
            if (NashornCallSiteDescriptor.isScope(desc)) {
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   525
                // Make a handle that drops the passed "this" argument and substitutes either Global or Undefined
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   526
                // (this, args...) => (args...)
16216
46ed48fd84d3 8007273: Creation of ScriptFunctions can be refactored
hannesw
parents: 16210
diff changeset
   527
                boundHandle = MH.bindTo(callHandle, needsWrappedThis() ? Context.getGlobalTrusted() : ScriptRuntime.UNDEFINED);
16225
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   528
                // (args...) => ([callee], [this], args...)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   529
                boundHandle = MH.dropArguments(boundHandle, 0, Object.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   530
            } else {
16225
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   531
                // (this, args...) => ([callee], this, args...)
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   532
                boundHandle = MH.dropArguments(callHandle, 0, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   533
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   534
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   535
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   536
        boundHandle = pairArguments(boundHandle, 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
   537
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
        return new GuardedInvocation(boundHandle, guard == null ? getFunctionGuard(this) : guard);
16188
d6390b0ea32a 8006678: Avoid too many Context.getGlobal() calls
sundar
parents: 16186
diff changeset
   539
   }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   540
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   541
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   542
     * Used for noSuchMethod/noSuchProperty and JSAdapter hooks.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   543
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   544
     * These don't want a callee parameter, so bind that. Name binding is optional.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   545
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   546
    MethodHandle getCallMethodHandle(final MethodType type, final String bindName) {
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
   547
        return pairArguments(bindToNameIfNeeded(bindToCalleeIfNeeded(getBestInvoker(type, null)), bindName), type);
16225
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   548
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   549
16225
81d58c2b9fcf 8006943: Fix order of function method arguments to be (callee, thisObject)
attila
parents: 16216
diff changeset
   550
    private static MethodHandle bindToNameIfNeeded(final MethodHandle methodHandle, final String bindName) {
18868
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   551
        if (bindName == null) {
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   552
            return methodHandle;
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   553
        } else {
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   554
            // if it is vararg method, we need to extend argument array with
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   555
            // a new zeroth element that is set to bindName value.
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   556
            final MethodType methodType = methodHandle.type();
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   557
            final int parameterCount = methodType.parameterCount();
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   558
            final boolean isVarArg = parameterCount > 0 && methodType.parameterType(parameterCount - 1).isArray();
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   559
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   560
            if (isVarArg) {
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   561
                return MH.filterArguments(methodHandle, 1, MH.insertArguments(ADD_ZEROTH_ELEMENT, 1, bindName));
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   562
            } else {
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   563
                return MH.insertArguments(methodHandle, 1, bindName);
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   564
            }
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   565
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   566
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   567
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
   568
    /**
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
     * Get the guard that checks if a {@link ScriptFunction} is equal to
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
     * a known ScriptFunction, using reference comparison
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
     * @param function The ScriptFunction to check against. This will be bound to the guard 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
   573
     *
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
     * @return method handle for guard
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
    private static MethodHandle getFunctionGuard(final ScriptFunction 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
   577
        assert function.data != 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
   578
        return MH.insertArguments(IS_FUNCTION_MH, 1, function.data);
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
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
     * Get a guard that checks if a {@link ScriptFunction} is equal to
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
     * a known ScriptFunction using reference comparison, and whether the type of
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
     * the second argument (this-object) is not a JavaScript primitive type.
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
     *
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
     * @param function The ScriptFunction to check against. This will be bound to the guard 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
   587
     *
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
     * @return method handle for guard
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
    private static MethodHandle getNonStrictFunctionGuard(final ScriptFunction 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
   591
        assert function.data != 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
   592
        return MH.insertArguments(IS_NONSTRICT_FUNCTION, 2, function.data);
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
    }
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
   594
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
   595
    @SuppressWarnings("unused")
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
   596
    private static boolean isFunctionMH(final Object self, final ScriptFunctionData data) {
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
   597
        return self instanceof ScriptFunction && ((ScriptFunction)self).data == data;
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
   598
    }
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
   599
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
   600
    @SuppressWarnings("unused")
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
   601
    private static boolean isNonStrictFunction(final Object self, final Object arg, final ScriptFunctionData data) {
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
   602
        return self instanceof ScriptFunction && ((ScriptFunction)self).data == data && arg instanceof ScriptObject;
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
   603
    }
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
   604
18868
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   605
    @SuppressWarnings("unused")
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   606
    private static Object[] addZerothElement(final Object[] args, final Object value) {
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   607
        // extends input array with by adding new zeroth element
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   608
        final Object[] src = (args == null)? ScriptRuntime.EMPTY_ARRAY : args;
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   609
        final Object[] result = new Object[src.length + 1];
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   610
        System.arraycopy(src, 0, result, 1, src.length);
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   611
        result[0] = value;
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   612
        return result;
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   613
    }
f5359cad148c 8012191: noSuchProperty can't cope with vararg functions
sundar
parents: 18614
diff changeset
   614
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   615
    private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   616
        final Class<?>   own = ScriptFunction.class;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   617
        final MethodType mt  = MH.type(rtype, types);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   618
        try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   619
            return MH.findStatic(MethodHandles.lookup(), own, name, mt);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   620
        } catch (final MethodHandleFactory.LookupException e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   621
            return MH.findVirtual(MethodHandles.lookup(), own, name, mt);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   622
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   623
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   624
}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   625