nashorn/src/jdk/nashorn/internal/runtime/linker/Bootstrap.java
author sundar
Tue, 23 Jul 2013 18:17:25 +0530
changeset 19088 153f268bfa72
parent 18878 078eb52cf5bc
child 19090 b5cf0f877326
permissions -rw-r--r--
8021122: Not all callables are handled for toString and other function valued properties Reviewed-by: attila, hannesw, jlaskey
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;
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    41
import jdk.nashorn.api.scripting.ScriptObjectMirror;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
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
    43
import jdk.nashorn.internal.codegen.RuntimeCallSite;
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    44
import jdk.nashorn.internal.runtime.ScriptFunction;
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    45
import jdk.nashorn.internal.runtime.ScriptRuntime;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
import jdk.nashorn.internal.runtime.options.Options;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
 * This class houses bootstrap method for invokedynamic instructions generated by compiler.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    51
public final class Bootstrap {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
    /** Reference to the seed boostrap function */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
    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
    54
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    55
    // do not create me!!
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
    private Bootstrap() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
    private static final DynamicLinker dynamicLinker;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
    static {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
        final DynamicLinkerFactory factory = new DynamicLinkerFactory();
16184
bea4e75d192c 8006525: Give StaticClass objects their own linker
attila
parents: 16173
diff changeset
    62
        factory.setPrioritizedLinkers(new NashornLinker(), new NashornPrimitiveLinker(), new NashornStaticClassLinker(),
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
    63
                new BoundDynamicMethodLinker(), new JSObjectLinker(), new ReflectionCheckLinker());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
        factory.setFallbackLinkers(new BeansLinker(), new NashornBottomLinker());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
        factory.setSyncOnRelink(true);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
        final int relinkThreshold = Options.getIntProperty("nashorn.unstable.relink.threshold", -1);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
        if (relinkThreshold > -1) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
            factory.setUnstableRelinkThreshold(relinkThreshold);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
        dynamicLinker = factory.createLinker();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
    /**
19088
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    74
     * Returns if the given object is a "callable"
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    75
     * @param obj object to be checked for callability
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    76
     * @return true if the obj is callable
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    77
     */
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    78
    public static boolean isCallable(final Object obj) {
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    79
        if (obj == ScriptRuntime.UNDEFINED || obj == null) {
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    80
            return false;
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    81
        }
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    82
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    83
        return obj instanceof ScriptFunction ||
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    84
            ((obj instanceof ScriptObjectMirror) && ((ScriptObjectMirror)obj).isFunction()) ||
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    85
            isDynamicMethod(obj) ||
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    86
            isFunctionalInterfaceObject(obj);
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    87
    }
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    88
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    89
    /**
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    90
     * Returns if the given object is a dynalink Dynamic method
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    91
     * @param obj object to be checked
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    92
     * @return true if the obj is a dynamic method
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    93
     */
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    94
    public static boolean isDynamicMethod(final Object obj) {
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    95
        return obj instanceof BoundDynamicMethod || BeansLinker.isDynamicMethod(obj);
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    96
    }
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    97
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    98
    /**
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
    99
     * Returns if the given object is an instance of an interface annotated with
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   100
     * java.lang.FunctionalInterface
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   101
     * @param obj object to be checked
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   102
     * @return true if the obj is an instance of @FunctionalInterface interface
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   103
     */
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   104
    public static boolean isFunctionalInterfaceObject(final Object obj) {
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   105
        return obj != null && (NashornBottomLinker.getFunctionalInterfaceMethod(obj.getClass()) != null);
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   106
    }
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   107
153f268bfa72 8021122: Not all callables are handled for toString and other function valued properties
sundar
parents: 18878
diff changeset
   108
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   109
     * 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
   110
     * 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
   111
     * invokedynamic instructions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   112
     * @param lookup MethodHandle lookup. Ignored as Nashorn only uses public lookup.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   113
     * @param opDesc Dynalink dynamic operation descriptor.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   114
     * @param type   Method type.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
     * @param flags  flags for call type, trace/profile etc.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   116
     * @return CallSite with MethodHandle to appropriate method or null if not found.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
    public static CallSite bootstrap(final Lookup lookup, final String opDesc, final MethodType type, final int flags) {
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16522
diff changeset
   119
        return dynamicLinker.link(LinkerCallSite.newLinkerCallSite(lookup, opDesc, type, flags));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
    /**
16226
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
   123
     * 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
   124
     *
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
   125
     * @param lookup       lookup
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
   126
     * @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
   127
     * @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
   128
     *
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
   129
     * @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
   130
     */
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
   131
    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
   132
        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
   133
    }
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
   134
0e4f37e6cc40 8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents: 16184
diff changeset
   135
    /**
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16522
diff changeset
   136
     * Returns a dynamic invoker for a specified dynamic operation using the public lookup. You can use this method to
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16522
diff changeset
   137
     * create a method handle that when invoked acts completely as if it were a Nashorn-linked call site. An overview of
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16522
diff changeset
   138
     * available dynamic operations can be found in the
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16522
diff changeset
   139
     * <a href="https://github.com/szegedi/dynalink/wiki/User-Guide-0.6">Dynalink User Guide</a>, but we'll show few
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16522
diff changeset
   140
     * examples here:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   141
     * <ul>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   142
     *   <li>Get a named property with fixed name:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   143
     *     <pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
     * MethodHandle getColor = Boostrap.createDynamicInvoker("dyn:getProp:color", Object.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   145
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
     * Object color = getColor.invokeExact(obj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   147
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
     *   <li>Get a named property with variable name:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
     *     <pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
     * MethodHandle getProperty = Boostrap.createDynamicInvoker("dyn:getElem", Object.class, Object.class, String.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
     * Object color = getProperty.invokeExact(obj, "color");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   154
     * Object shape = getProperty.invokeExact(obj, "shape");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   155
     * MethodHandle getNumProperty = Boostrap.createDynamicInvoker("dyn:getElem", Object.class, Object.class, int.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   156
     * Object elem42 = getNumProperty.invokeExact(obj, 42);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   158
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
     *   <li>Set a named property with fixed name:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
     *     <pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   161
     * MethodHandle setColor = Boostrap.createDynamicInvoker("dyn:setProp:color", void.class, Object.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   162
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
     * setColor.invokeExact(obj, Color.BLUE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   164
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   165
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   166
     *   <li>Set a property with variable name:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   167
     *     <pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   168
     * MethodHandle setProperty = Boostrap.createDynamicInvoker("dyn:setElem", void.class, Object.class, String.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   169
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   170
     * setProperty.invokeExact(obj, "color", Color.BLUE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   171
     * setProperty.invokeExact(obj, "shape", Shape.CIRCLE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   172
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   173
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   174
     *   <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
   175
     *     <pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
     * MethodHandle findFooFunction = Boostrap.createDynamicInvoker("dyn:getMethod:foo", Object.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
     * Object foo_fn = findFooFunction.invokeExact(obj);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
     * 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
   180
     * // Note: "call" operation takes a function, then a "this" value, then the arguments:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   181
     * Object foo_retval = callFunctionWithTwoArgs.invokeExact(foo_fn, obj, arg1, arg2);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   182
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   184
     *   <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
   185
     *   emits any INVOKEDYNAMIC instructions with {@code dyn:getMethod}, it still supports this standard Dynalink
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   186
     *   operation:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
     *     <pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
     * MethodHandle callFunctionFooWithTwoArgs = Boostrap.createDynamicInvoker("dyn:callMethod:foo", Object.class, Object.class, Object.class, Object.class);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   189
     * Object obj = ...; // somehow obtain the object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   190
     * Object foo_retval = callFunctionFooWithTwoArgs.invokeExact(obj, arg1, arg2);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   191
     *     </pre>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   192
     *   </li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   193
     * </ul>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   194
     * Few additional remarks:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   195
     * <ul>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   196
     * <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
   197
     * arbitrary Java objects in addition to Nashorn JavaScript objects.</li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   198
     * <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
   199
     * <li>For Nashorn objects {@code getElem}, {@code getProp}, and {@code getMethod} are handled almost identically,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   200
     * 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
   201
     * 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
   202
     * properties: {@code getMethod} for a missing member will link to a potential invocation of
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   203
     * {@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
   204
     * 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
   205
     * <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
   206
     * <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
   207
     * 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
   208
     * {@code Object}, and so on regardless of the kind of the operation.</li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   209
     * <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
   210
     * 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
   211
     * 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
   212
     * 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
   213
     * 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
   214
     * {@code String} or {@code boolean/Boolean}, then the parameters might be subjected to standard ECMAScript
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   215
     * {@code ToNumber}, {@code ToString}, and {@code ToBoolean} conversion, respectively. Less obviously, if the
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   216
     * 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
   217
     * 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
   218
     * specific type information than "everything can be an object".</li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   219
     * <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
   220
     * 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
   221
     * 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
   222
     * {@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
   223
     * {@code ToNumber}, {@code ToString}, and {@code ToBoolean} conversion, respectively. Less obviously, if the return
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   224
     * 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
   225
     * delegating to the function will be returned.</li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   226
     * </ul>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   227
     * @param opDesc Dynalink dynamic operation descriptor.
16173
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   228
     * @param rtype the return type for the operation
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   229
     * @param ptypes the parameter types for the operation
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   230
     * @return MethodHandle for invoking the operation.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   231
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   232
    public static MethodHandle createDynamicInvoker(final String opDesc, final Class<?> rtype, final Class<?>... ptypes) {
16173
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   233
        return createDynamicInvoker(opDesc, MethodType.methodType(rtype, ptypes));
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   234
    }
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   235
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   236
    /**
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16522
diff changeset
   237
     * Returns a dynamic invoker for a specified dynamic operation using the public lookup. Similar to
16173
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   238
     * {@link #createDynamicInvoker(String, Class, Class...)} but with return and parameter types composed into a
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   239
     * method type in the signature. See the discussion of that method for details.
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   240
     * @param opDesc Dynalink dynamic operation descriptor.
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   241
     * @param type the method type for the operation
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   242
     * @return MethodHandle for invoking the operation.
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   243
     */
c41d062f7d2a 8006293: Reduce ScriptObject.findCallMethodMethod
attila
parents: 16151
diff changeset
   244
    public static MethodHandle createDynamicInvoker(final String opDesc, final MethodType type) {
18841
9bbc4b8832b2 8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents: 16522
diff changeset
   245
        return bootstrap(MethodHandles.publicLookup(), opDesc, type, 0).dynamicInvoker();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   246
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   247
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   248
    /**
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   249
     * Binds a bean dynamic method (returned by invoking {@code dyn:getMethod} on an object linked with
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   250
     * {@code BeansLinker} to a receiver.
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   251
     * @param dynamicMethod the dynamic method to bind
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   252
     * @param boundThis the bound "this" value.
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   253
     * @return a bound dynamic method.
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   254
     */
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   255
    public static Object bindDynamicMethod(Object dynamicMethod, Object boundThis) {
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   256
        return new BoundDynamicMethod(dynamicMethod, boundThis);
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   257
    }
18878
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   258
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   259
    /**
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   260
     * If the given class is a reflection-specific class (anything in {@code java.lang.reflect} and
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   261
     * {@code java.lang.invoke} package, as well a {@link Class} and any subclass of {@link ClassLoader}) and there is
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   262
     * a security manager in the system, then it checks the {@code nashorn.JavaReflection} {@code RuntimePermission}.
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   263
     * @param clazz the class being tested
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   264
     */
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   265
    public static void checkReflectionAccess(Class<?> clazz) {
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   266
        ReflectionCheckLinker.checkReflectionAccess(clazz);
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   267
    }
078eb52cf5bc 8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents: 18876
diff changeset
   268
18876
ada98218aaae 8020324: Implement Object.bindProperties(target, source) for beans
attila
parents: 18841
diff changeset
   269
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   270
     * 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
   271
     * 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
   272
     * 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
   273
     * 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
   274
     * type converter method handles) outside of a code path that is linking a call site.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   275
     * @return Nashorn's internal dynamic linker's services object.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   276
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   277
    public static LinkerServices getLinkerServices() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   278
        return dynamicLinker.getLinkerServices();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   279
    }
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
     * 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
   283
     * 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
   284
     * 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
   285
     * 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
   286
     * invocation is returned is the responsibility of the linkers themselves.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   287
     * @param inv the guarded invocation that needs to be type-converted. Can be null.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   288
     * @param linkerServices the linker services object providing the type conversions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   289
     * @param desc the call site descriptor to whose method type the invocation needs to conform.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   290
     * @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
   291
     * already conforms to the requested type, it is returned unchanged.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   292
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   293
    static GuardedInvocation asType(final GuardedInvocation inv, final LinkerServices linkerServices, final CallSiteDescriptor desc) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   294
        return inv == null ? null : inv.asType(linkerServices, desc.getMethodType());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   295
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   296
}