nashorn/src/jdk/nashorn/internal/runtime/linker/Bootstrap.java
author sundar
Tue, 12 Mar 2013 18:12:42 +0530
changeset 16522 d643e3ee819c
parent 16234 86cb162cec6c
child 18841 9bbc4b8832b2
permissions -rw-r--r--
8009757: Package access clean up and refactoring Reviewed-by: jlaskey, lagergren, 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.linker;
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.staticCallNoLookup;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
import java.lang.invoke.CallSite;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
import java.lang.invoke.MethodHandle;
16226
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
    32
import java.lang.invoke.MethodHandles;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
import java.lang.invoke.MethodHandles.Lookup;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
import java.lang.invoke.MethodType;
16234
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16226
diff changeset
    35
import jdk.internal.dynalink.CallSiteDescriptor;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16226
diff changeset
    36
import jdk.internal.dynalink.DynamicLinker;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16226
diff changeset
    37
import jdk.internal.dynalink.DynamicLinkerFactory;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16226
diff changeset
    38
import jdk.internal.dynalink.beans.BeansLinker;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16226
diff changeset
    39
import jdk.internal.dynalink.linker.GuardedInvocation;
86cb162cec6c 8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents: 16226
diff changeset
    40
import jdk.internal.dynalink.linker.LinkerServices;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
import jdk.nashorn.internal.codegen.CompilerConstants.Call;
16226
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
    42
import jdk.nashorn.internal.codegen.RuntimeCallSite;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
import jdk.nashorn.internal.runtime.options.Options;
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
 * This class houses bootstrap method for invokedynamic instructions generated by compiler.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
public final class Bootstrap {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
    /** Reference to the seed boostrap function */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
    public static final Call BOOTSTRAP = staticCallNoLookup(Bootstrap.class, "bootstrap", CallSite.class, Lookup.class, String.class, MethodType.class, int.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    51
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
    // do not create me!!
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
    private Bootstrap() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    55
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
    private static final DynamicLinker dynamicLinker;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
    static {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
        final DynamicLinkerFactory factory = new DynamicLinkerFactory();
16184
bea4e75d192c 8006525: Give StaticClass objects their own linker
attila
parents: 16173
diff changeset
    59
        factory.setPrioritizedLinkers(new NashornLinker(), new NashornPrimitiveLinker(), new NashornStaticClassLinker(),
16522
d643e3ee819c 8009757: Package access clean up and refactoring
sundar
parents: 16234
diff changeset
    60
                new JSObjectLinker(), new ReflectionCheckLinker());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
        factory.setFallbackLinkers(new BeansLinker(), new NashornBottomLinker());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
        factory.setSyncOnRelink(true);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
        final int relinkThreshold = Options.getIntProperty("nashorn.unstable.relink.threshold", -1);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
        if (relinkThreshold > -1) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
            factory.setUnstableRelinkThreshold(relinkThreshold);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
        dynamicLinker = factory.createLinker();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
     * Create a call site and link it for Nashorn. This version of the method conforms to the invokedynamic bootstrap
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
     * method expected signature and is referenced from Nashorn generated bytecode as the bootstrap method for all
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
     * invokedynamic instructions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
     * @param lookup MethodHandle lookup. Ignored as Nashorn only uses public lookup.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
     * @param opDesc Dynalink dynamic operation descriptor.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
     * @param type   Method type.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
     * @param flags  flags for call type, trace/profile etc.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    78
     * @return CallSite with MethodHandle to appropriate method or null if not found.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    79
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    80
    public static CallSite bootstrap(final Lookup lookup, final String opDesc, final MethodType type, final int flags) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
        return dynamicLinker.link(LinkerCallSite.newLinkerCallSite(opDesc, type, flags));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
    /**
16226
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
    85
     * Bootstrapper for a specialized Runtime call
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
    86
     *
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
    87
     * @param lookup       lookup
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
    88
     * @param initialName  initial name for callsite
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
    89
     * @param type         method type for call site
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
    90
     *
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
    91
     * @return callsite for a runtime node
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
    92
     */
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
    93
    public static CallSite runtimeBootstrap(final MethodHandles.Lookup lookup, final String initialName, final MethodType type) {
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
    94
        return new RuntimeCallSite(type, initialName);
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
    95
    }
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
    96
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
    97
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
    98
    /**
16173
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
    99
     * Returns a dynamic invoker for a specified dynamic operation. You can use this method to create a method handle
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   100
     * that when invoked acts completely as if it were a Nashorn-linked call site. An overview of available dynamic
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   101
     * operations can be found in the <a href="https://github.com/szegedi/dynalink/wiki/User-Guide-0.4">Dynalink User Guide</a>,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   102
     * but we'll show few examples here:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   103
     * <ul>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   104
     *   <li>Get a named property with fixed name:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
     *     <pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   106
     * MethodHandle getColor = Boostrap.createDynamicInvoker("dyn:getProp:color", Object.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   107
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
     * Object color = getColor.invokeExact(obj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   109
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   110
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   111
     *   <li>Get a named property with variable name:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   112
     *     <pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   113
     * MethodHandle getProperty = Boostrap.createDynamicInvoker("dyn:getElem", Object.class, Object.class, String.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   114
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
     * Object color = getProperty.invokeExact(obj, "color");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   116
     * Object shape = getProperty.invokeExact(obj, "shape");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
     * MethodHandle getNumProperty = Boostrap.createDynamicInvoker("dyn:getElem", Object.class, Object.class, int.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
     * Object elem42 = getNumProperty.invokeExact(obj, 42);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
     *   <li>Set a named property with fixed name:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
     *     <pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   123
     * MethodHandle setColor = Boostrap.createDynamicInvoker("dyn:setProp:color", void.class, Object.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   125
     * setColor.invokeExact(obj, Color.BLUE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   128
     *   <li>Set a property with variable name:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
     *     <pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
     * MethodHandle setProperty = Boostrap.createDynamicInvoker("dyn:setElem", void.class, Object.class, String.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
     * setProperty.invokeExact(obj, "color", Color.BLUE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
     * setProperty.invokeExact(obj, "shape", Shape.CIRCLE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
     *   <li>Call a function on an object; two-step variant. This is the actual variant used by Nashorn-generated code:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
     *     <pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   138
     * MethodHandle findFooFunction = Boostrap.createDynamicInvoker("dyn:getMethod:foo", Object.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   139
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   140
     * Object foo_fn = findFooFunction.invokeExact(obj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   141
     * MethodHandle callFunctionWithTwoArgs = Boostrap.createDynamicInvoker("dyn:call", Object.class, Object.class, Object.class, Object.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   142
     * // Note: "call" operation takes a function, then a "this" value, then the arguments:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   143
     * Object foo_retval = callFunctionWithTwoArgs.invokeExact(foo_fn, obj, arg1, arg2);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   145
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
     *   <li>Call a function on an object; single-step variant. Although Nashorn doesn't use this variant and never
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   147
     *   emits any INVOKEDYNAMIC instructions with {@code dyn:getMethod}, it still supports this standard Dynalink
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
     *   operation:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
     *     <pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
     * MethodHandle callFunctionFooWithTwoArgs = Boostrap.createDynamicInvoker("dyn:callMethod:foo", Object.class, Object.class, Object.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
     * Object foo_retval = callFunctionFooWithTwoArgs.invokeExact(obj, arg1, arg2);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   154
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   155
     * </ul>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   156
     * Few additional remarks:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
     * <ul>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   158
     * <li>Just as Nashorn works with any Java object, the invokers returned from this method can also be applied to
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
     * arbitrary Java objects in addition to Nashorn JavaScript objects.</li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
     * <li>For invoking a named function on an object, you can also use the {@link InvokeByName} convenience class.</li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   161
     * <li>For Nashorn objects {@code getElem}, {@code getProp}, and {@code getMethod} are handled almost identically,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   162
     * since JavaScript doesn't distinguish between different kinds of properties on an object. Either can be used with
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
     * fixed property name or a variable property name. The only significant difference is handling of missing
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   164
     * properties: {@code getMethod} for a missing member will link to a potential invocation of
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   165
     * {@code __noSuchMethod__} on the object, {@code getProp} for a missing member will link to a potential invocation
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   166
     * of {@code __noSuchProperty__}, while {@code getElem} for a missing member will link to an empty getter.</li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   167
     * <li>In similar vein, {@code setElem} and {@code setProp} are handled identically on Nashorn objects.</li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   168
     * <li>There's no rule that the variable property identifier has to be a {@code String} for {@code getProp/setProp}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   169
     * and {@code int} for {@code getElem/setElem}. You can declare their type to be {@code int}, {@code double},
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   170
     * {@code Object}, and so on regardless of the kind of the operation.</li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   171
     * <li>You can be as specific in parameter types as you want. E.g. if you know that the receiver of the operation
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   172
     * will always be {@code ScriptObject}, you can pass {@code ScriptObject.class} as its parameter type. If you happen
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   173
     * to link to a method that expects different types, (you can use these invokers on POJOs too, after all, and end up
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   174
     * linking with their methods that have strongly-typed signatures), all necessary conversions allowed by either Java
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
     * or JavaScript will be applied: if invoked methods specify either primitive or wrapped Java numeric types, or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
     * {@code String} or {@code boolean/Boolean}, then the parameters might be subjected to standard ECMAScript
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
     * {@code ToNumber}, {@code ToString}, and {@code ToBoolean} conversion, respectively. Less obviously, if the
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
     * expected parameter type is a SAM type, and you pass a JavaScript function, a proxy object implementing the SAM
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
     * type and delegating to the function will be passed. Linkage can often be optimized when linkers have more
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   180
     * specific type information than "everything can be an object".</li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   181
     * <li>You can also be as specific in return types as you want. For return types any necessary type conversion
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   182
     * available in either Java or JavaScript will be automatically applied, similar to the process described for
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
     * parameters, only in reverse direction:  if you specify any either primitive or wrapped Java numeric type, or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   184
     * {@code String} or {@code boolean/Boolean}, then the return values will be subjected to standard ECMAScript
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   185
     * {@code ToNumber}, {@code ToString}, and {@code ToBoolean} conversion, respectively. Less obviously, if the return
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   186
     * type is a SAM type, and the return value is a JavaScript function, a proxy object implementing the SAM type and
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
     * delegating to the function will be returned.</li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
     * </ul>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   189
     * @param opDesc Dynalink dynamic operation descriptor.
16173
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   190
     * @param rtype the return type for the operation
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   191
     * @param ptypes the parameter types for the operation
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   192
     * @return MethodHandle for invoking the operation.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   193
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   194
    public static MethodHandle createDynamicInvoker(final String opDesc, final Class<?> rtype, final Class<?>... ptypes) {
16173
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   195
        return createDynamicInvoker(opDesc, MethodType.methodType(rtype, ptypes));
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   196
    }
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   197
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   198
    /**
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   199
     * Returns a dynamic invoker for a specified dynamic operation. Similar to
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   200
     * {@link #createDynamicInvoker(String, Class, Class...)} but with return and parameter types composed into a
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   201
     * method type in the signature. See the discussion of that method for details.
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   202
     * @param opDesc Dynalink dynamic operation descriptor.
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   203
     * @param type the method type for the operation
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   204
     * @return MethodHandle for invoking the operation.
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   205
     */
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   206
    public static MethodHandle createDynamicInvoker(final String opDesc, final MethodType type) {
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   207
        return bootstrap(null, opDesc, type, 0).dynamicInvoker();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   208
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   209
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   210
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   211
     * Returns the Nashorn's internally used dynamic linker's services object. Note that in code that is processing a
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   212
     * linking request, you will normally use the {@code LinkerServices} object passed by whatever top-level linker
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   213
     * invoked the linking (if the call site is in Nashorn-generated code, you'll get this object anyway). You should
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   214
     * only resort to retrieving a linker services object using this method when you need some linker services (e.g.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   215
     * type converter method handles) outside of a code path that is linking a call site.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   216
     * @return Nashorn's internal dynamic linker's services object.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   217
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   218
    public static LinkerServices getLinkerServices() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   219
        return dynamicLinker.getLinkerServices();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   220
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   221
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   222
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   223
     * Takes a guarded invocation, and ensures its method and guard conform to the type of the call descriptor, using
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   224
     * all type conversions allowed by the linker's services. This method is used by Nashorn's linkers as a last step
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   225
     * before returning guarded invocations to the callers. Most of the code used to produce the guarded invocations
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   226
     * does not make an effort to coordinate types of the methods, and so a final type adjustment before a guarded
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   227
     * invocation is returned is the responsibility of the linkers themselves.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   228
     * @param inv the guarded invocation that needs to be type-converted. Can be null.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   229
     * @param linkerServices the linker services object providing the type conversions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   230
     * @param desc the call site descriptor to whose method type the invocation needs to conform.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   231
     * @return the type-converted guarded invocation. If input is null, null is returned. If the input invocation
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   232
     * already conforms to the requested type, it is returned unchanged.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   233
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   234
    static GuardedInvocation asType(final GuardedInvocation inv, final LinkerServices linkerServices, final CallSiteDescriptor desc) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   235
        return inv == null ? null : inv.asType(linkerServices, desc.getMethodType());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   236
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   237
}