author | sundar |
Tue, 23 Jul 2013 18:17:25 +0530 | |
changeset 19088 | 153f268bfa72 |
parent 18878 | 078eb52cf5bc |
child 19090 | b5cf0f877326 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.nashorn.internal.runtime.linker; |
|
27 |
||
28 |
import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup; |
|
29 |
||
30 |
import java.lang.invoke.CallSite; |
|
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 | 33 |
import java.lang.invoke.MethodHandles.Lookup; |
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 | 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 | 46 |
import jdk.nashorn.internal.runtime.options.Options; |
47 |
||
48 |
/** |
|
49 |
* This class houses bootstrap method for invokedynamic instructions generated by compiler. |
|
50 |
*/ |
|
51 |
public final class Bootstrap { |
|
52 |
/** Reference to the seed boostrap function */ |
|
53 |
public static final Call BOOTSTRAP = staticCallNoLookup(Bootstrap.class, "bootstrap", CallSite.class, Lookup.class, String.class, MethodType.class, int.class); |
|
54 |
||
55 |
// do not create me!! |
|
56 |
private Bootstrap() { |
|
57 |
} |
|
58 |
||
59 |
private static final DynamicLinker dynamicLinker; |
|
60 |
static { |
|
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 | 64 |
factory.setFallbackLinkers(new BeansLinker(), new NashornBottomLinker()); |
65 |
factory.setSyncOnRelink(true); |
|
66 |
final int relinkThreshold = Options.getIntProperty("nashorn.unstable.relink.threshold", -1); |
|
67 |
if (relinkThreshold > -1) { |
|
68 |
factory.setUnstableRelinkThreshold(relinkThreshold); |
|
69 |
} |
|
70 |
dynamicLinker = factory.createLinker(); |
|
71 |
} |
|
72 |
||
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 | 109 |
* Create a call site and link it for Nashorn. This version of the method conforms to the invokedynamic bootstrap |
110 |
* method expected signature and is referenced from Nashorn generated bytecode as the bootstrap method for all |
|
111 |
* invokedynamic instructions. |
|
112 |
* @param lookup MethodHandle lookup. Ignored as Nashorn only uses public lookup. |
|
113 |
* @param opDesc Dynalink dynamic operation descriptor. |
|
114 |
* @param type Method type. |
|
115 |
* @param flags flags for call type, trace/profile etc. |
|
116 |
* @return CallSite with MethodHandle to appropriate method or null if not found. |
|
117 |
*/ |
|
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 | 120 |
} |
121 |
||
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 | 141 |
* <ul> |
142 |
* <li>Get a named property with fixed name: |
|
143 |
* <pre> |
|
144 |
* MethodHandle getColor = Boostrap.createDynamicInvoker("dyn:getProp:color", Object.class, Object.class); |
|
145 |
* Object obj = ...; // somehow obtain the object |
|
146 |
* Object color = getColor.invokeExact(obj); |
|
147 |
* </pre> |
|
148 |
* </li> |
|
149 |
* <li>Get a named property with variable name: |
|
150 |
* <pre> |
|
151 |
* MethodHandle getProperty = Boostrap.createDynamicInvoker("dyn:getElem", Object.class, Object.class, String.class); |
|
152 |
* Object obj = ...; // somehow obtain the object |
|
153 |
* Object color = getProperty.invokeExact(obj, "color"); |
|
154 |
* Object shape = getProperty.invokeExact(obj, "shape"); |
|
155 |
* MethodHandle getNumProperty = Boostrap.createDynamicInvoker("dyn:getElem", Object.class, Object.class, int.class); |
|
156 |
* Object elem42 = getNumProperty.invokeExact(obj, 42); |
|
157 |
* </pre> |
|
158 |
* </li> |
|
159 |
* <li>Set a named property with fixed name: |
|
160 |
* <pre> |
|
161 |
* MethodHandle setColor = Boostrap.createDynamicInvoker("dyn:setProp:color", void.class, Object.class, Object.class); |
|
162 |
* Object obj = ...; // somehow obtain the object |
|
163 |
* setColor.invokeExact(obj, Color.BLUE); |
|
164 |
* </pre> |
|
165 |
* </li> |
|
166 |
* <li>Set a property with variable name: |
|
167 |
* <pre> |
|
168 |
* MethodHandle setProperty = Boostrap.createDynamicInvoker("dyn:setElem", void.class, Object.class, String.class, Object.class); |
|
169 |
* Object obj = ...; // somehow obtain the object |
|
170 |
* setProperty.invokeExact(obj, "color", Color.BLUE); |
|
171 |
* setProperty.invokeExact(obj, "shape", Shape.CIRCLE); |
|
172 |
* </pre> |
|
173 |
* </li> |
|
174 |
* <li>Call a function on an object; two-step variant. This is the actual variant used by Nashorn-generated code: |
|
175 |
* <pre> |
|
176 |
* MethodHandle findFooFunction = Boostrap.createDynamicInvoker("dyn:getMethod:foo", Object.class, Object.class); |
|
177 |
* Object obj = ...; // somehow obtain the object |
|
178 |
* Object foo_fn = findFooFunction.invokeExact(obj); |
|
179 |
* MethodHandle callFunctionWithTwoArgs = Boostrap.createDynamicInvoker("dyn:call", Object.class, Object.class, Object.class, Object.class, Object.class); |
|
180 |
* // Note: "call" operation takes a function, then a "this" value, then the arguments: |
|
181 |
* Object foo_retval = callFunctionWithTwoArgs.invokeExact(foo_fn, obj, arg1, arg2); |
|
182 |
* </pre> |
|
183 |
* </li> |
|
184 |
* <li>Call a function on an object; single-step variant. Although Nashorn doesn't use this variant and never |
|
185 |
* emits any INVOKEDYNAMIC instructions with {@code dyn:getMethod}, it still supports this standard Dynalink |
|
186 |
* operation: |
|
187 |
* <pre> |
|
188 |
* MethodHandle callFunctionFooWithTwoArgs = Boostrap.createDynamicInvoker("dyn:callMethod:foo", Object.class, Object.class, Object.class, Object.class); |
|
189 |
* Object obj = ...; // somehow obtain the object |
|
190 |
* Object foo_retval = callFunctionFooWithTwoArgs.invokeExact(obj, arg1, arg2); |
|
191 |
* </pre> |
|
192 |
* </li> |
|
193 |
* </ul> |
|
194 |
* Few additional remarks: |
|
195 |
* <ul> |
|
196 |
* <li>Just as Nashorn works with any Java object, the invokers returned from this method can also be applied to |
|
197 |
* arbitrary Java objects in addition to Nashorn JavaScript objects.</li> |
|
198 |
* <li>For invoking a named function on an object, you can also use the {@link InvokeByName} convenience class.</li> |
|
199 |
* <li>For Nashorn objects {@code getElem}, {@code getProp}, and {@code getMethod} are handled almost identically, |
|
200 |
* since JavaScript doesn't distinguish between different kinds of properties on an object. Either can be used with |
|
201 |
* fixed property name or a variable property name. The only significant difference is handling of missing |
|
202 |
* properties: {@code getMethod} for a missing member will link to a potential invocation of |
|
203 |
* {@code __noSuchMethod__} on the object, {@code getProp} for a missing member will link to a potential invocation |
|
204 |
* of {@code __noSuchProperty__}, while {@code getElem} for a missing member will link to an empty getter.</li> |
|
205 |
* <li>In similar vein, {@code setElem} and {@code setProp} are handled identically on Nashorn objects.</li> |
|
206 |
* <li>There's no rule that the variable property identifier has to be a {@code String} for {@code getProp/setProp} |
|
207 |
* and {@code int} for {@code getElem/setElem}. You can declare their type to be {@code int}, {@code double}, |
|
208 |
* {@code Object}, and so on regardless of the kind of the operation.</li> |
|
209 |
* <li>You can be as specific in parameter types as you want. E.g. if you know that the receiver of the operation |
|
210 |
* will always be {@code ScriptObject}, you can pass {@code ScriptObject.class} as its parameter type. If you happen |
|
211 |
* to link to a method that expects different types, (you can use these invokers on POJOs too, after all, and end up |
|
212 |
* linking with their methods that have strongly-typed signatures), all necessary conversions allowed by either Java |
|
213 |
* or JavaScript will be applied: if invoked methods specify either primitive or wrapped Java numeric types, or |
|
214 |
* {@code String} or {@code boolean/Boolean}, then the parameters might be subjected to standard ECMAScript |
|
215 |
* {@code ToNumber}, {@code ToString}, and {@code ToBoolean} conversion, respectively. Less obviously, if the |
|
216 |
* expected parameter type is a SAM type, and you pass a JavaScript function, a proxy object implementing the SAM |
|
217 |
* type and delegating to the function will be passed. Linkage can often be optimized when linkers have more |
|
218 |
* specific type information than "everything can be an object".</li> |
|
219 |
* <li>You can also be as specific in return types as you want. For return types any necessary type conversion |
|
220 |
* available in either Java or JavaScript will be automatically applied, similar to the process described for |
|
221 |
* parameters, only in reverse direction: if you specify any either primitive or wrapped Java numeric type, or |
|
222 |
* {@code String} or {@code boolean/Boolean}, then the return values will be subjected to standard ECMAScript |
|
223 |
* {@code ToNumber}, {@code ToString}, and {@code ToBoolean} conversion, respectively. Less obviously, if the return |
|
224 |
* type is a SAM type, and the return value is a JavaScript function, a proxy object implementing the SAM type and |
|
225 |
* delegating to the function will be returned.</li> |
|
226 |
* </ul> |
|
227 |
* @param opDesc Dynalink dynamic operation descriptor. |
|
16173 | 228 |
* @param rtype the return type for the operation |
229 |
* @param ptypes the parameter types for the operation |
|
16147 | 230 |
* @return MethodHandle for invoking the operation. |
231 |
*/ |
|
232 |
public static MethodHandle createDynamicInvoker(final String opDesc, final Class<?> rtype, final Class<?>... ptypes) { |
|
16173 | 233 |
return createDynamicInvoker(opDesc, MethodType.methodType(rtype, ptypes)); |
234 |
} |
|
235 |
||
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 | 238 |
* {@link #createDynamicInvoker(String, Class, Class...)} but with return and parameter types composed into a |
239 |
* method type in the signature. See the discussion of that method for details. |
|
240 |
* @param opDesc Dynalink dynamic operation descriptor. |
|
241 |
* @param type the method type for the operation |
|
242 |
* @return MethodHandle for invoking the operation. |
|
243 |
*/ |
|
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 | 246 |
} |
247 |
||
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 | 270 |
* Returns the Nashorn's internally used dynamic linker's services object. Note that in code that is processing a |
271 |
* linking request, you will normally use the {@code LinkerServices} object passed by whatever top-level linker |
|
272 |
* invoked the linking (if the call site is in Nashorn-generated code, you'll get this object anyway). You should |
|
273 |
* only resort to retrieving a linker services object using this method when you need some linker services (e.g. |
|
274 |
* type converter method handles) outside of a code path that is linking a call site. |
|
275 |
* @return Nashorn's internal dynamic linker's services object. |
|
276 |
*/ |
|
277 |
public static LinkerServices getLinkerServices() { |
|
278 |
return dynamicLinker.getLinkerServices(); |
|
279 |
} |
|
280 |
||
281 |
/** |
|
282 |
* Takes a guarded invocation, and ensures its method and guard conform to the type of the call descriptor, using |
|
283 |
* all type conversions allowed by the linker's services. This method is used by Nashorn's linkers as a last step |
|
284 |
* before returning guarded invocations to the callers. Most of the code used to produce the guarded invocations |
|
285 |
* does not make an effort to coordinate types of the methods, and so a final type adjustment before a guarded |
|
286 |
* invocation is returned is the responsibility of the linkers themselves. |
|
287 |
* @param inv the guarded invocation that needs to be type-converted. Can be null. |
|
288 |
* @param linkerServices the linker services object providing the type conversions. |
|
289 |
* @param desc the call site descriptor to whose method type the invocation needs to conform. |
|
290 |
* @return the type-converted guarded invocation. If input is null, null is returned. If the input invocation |
|
291 |
* already conforms to the requested type, it is returned unchanged. |
|
292 |
*/ |
|
293 |
static GuardedInvocation asType(final GuardedInvocation inv, final LinkerServices linkerServices, final CallSiteDescriptor desc) { |
|
294 |
return inv == null ? null : inv.asType(linkerServices, desc.getMethodType()); |
|
295 |
} |
|
296 |
} |