author | attila |
Thu, 05 Mar 2015 15:43:43 +0100 | |
changeset 29283 | fb47e4d25a9f |
parent 28881 | 0008daeef352 |
child 29834 | f678f348c947 |
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; |
|
27815 | 29 |
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; |
16147 | 30 |
|
31 |
import java.lang.invoke.CallSite; |
|
24719 | 32 |
import java.lang.invoke.ConstantCallSite; |
16147 | 33 |
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
|
34 |
import java.lang.invoke.MethodHandles; |
16147 | 35 |
import java.lang.invoke.MethodHandles.Lookup; |
36 |
import java.lang.invoke.MethodType; |
|
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16226
diff
changeset
|
37 |
import jdk.internal.dynalink.CallSiteDescriptor; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16226
diff
changeset
|
38 |
import jdk.internal.dynalink.DynamicLinker; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16226
diff
changeset
|
39 |
import jdk.internal.dynalink.DynamicLinkerFactory; |
24719 | 40 |
import jdk.internal.dynalink.GuardedInvocationFilter; |
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16226
diff
changeset
|
41 |
import jdk.internal.dynalink.beans.BeansLinker; |
19627
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
42 |
import jdk.internal.dynalink.beans.StaticClass; |
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16226
diff
changeset
|
43 |
import jdk.internal.dynalink.linker.GuardedInvocation; |
24719 | 44 |
import jdk.internal.dynalink.linker.LinkRequest; |
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16226
diff
changeset
|
45 |
import jdk.internal.dynalink.linker.LinkerServices; |
27360 | 46 |
import jdk.internal.dynalink.linker.MethodTypeConversionStrategy; |
47 |
import jdk.internal.dynalink.support.TypeUtilities; |
|
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19627
diff
changeset
|
48 |
import jdk.nashorn.api.scripting.JSObject; |
16147 | 49 |
import jdk.nashorn.internal.codegen.CompilerConstants.Call; |
24719 | 50 |
import jdk.nashorn.internal.codegen.ObjectClassGenerator; |
51 |
import jdk.nashorn.internal.lookup.MethodHandleFactory; |
|
52 |
import jdk.nashorn.internal.lookup.MethodHandleFunctionality; |
|
27815 | 53 |
import jdk.nashorn.internal.objects.ScriptFunctionImpl; |
54 |
import jdk.nashorn.internal.runtime.ECMAException; |
|
19090
b5cf0f877326
8021164: REGRESSION: test262 failures after JDK-8021122
sundar
parents:
19088
diff
changeset
|
55 |
import jdk.nashorn.internal.runtime.JSType; |
24719 | 56 |
import jdk.nashorn.internal.runtime.OptimisticReturnFilters; |
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
57 |
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
|
58 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
16147 | 59 |
import jdk.nashorn.internal.runtime.options.Options; |
60 |
||
61 |
/** |
|
62 |
* This class houses bootstrap method for invokedynamic instructions generated by compiler. |
|
63 |
*/ |
|
64 |
public final class Bootstrap { |
|
65 |
/** Reference to the seed boostrap function */ |
|
66 |
public static final Call BOOTSTRAP = staticCallNoLookup(Bootstrap.class, "bootstrap", CallSite.class, Lookup.class, String.class, MethodType.class, int.class); |
|
67 |
||
24719 | 68 |
private static final MethodHandleFunctionality MH = MethodHandleFactory.getFunctionality(); |
69 |
||
28438
f164fc2618a0
8068573: POJO setter using [] syntax throws an exception
attila
parents:
28437
diff
changeset
|
70 |
private static final MethodHandle VOID_TO_OBJECT = MH.constant(Object.class, ScriptRuntime.UNDEFINED); |
f164fc2618a0
8068573: POJO setter using [] syntax throws an exception
attila
parents:
28437
diff
changeset
|
71 |
|
24719 | 72 |
/** |
73 |
* The default dynalink relink threshold for megamorphisism is 8. In the case |
|
74 |
* of object fields only, it is fine. However, with dual fields, in order to get |
|
75 |
* performance on benchmarks with a lot of object instantiation and then field |
|
76 |
* reassignment, it can take slightly more relinks to become stable with type |
|
77 |
* changes swapping out an entire proprety map and making a map guard fail. |
|
78 |
* Therefore the relink threshold is set to 16 for dual fields (now the default). |
|
79 |
* This doesn't seem to have any other negative performance implication. |
|
80 |
* |
|
81 |
* See for example octane.gbemu, run with --log=fields:warning to study |
|
82 |
* megamorphic behavior |
|
83 |
*/ |
|
84 |
private static final int NASHORN_DEFAULT_UNSTABLE_RELINK_THRESHOLD = |
|
85 |
ObjectClassGenerator.OBJECT_FIELDS_ONLY ? |
|
86 |
8 : |
|
87 |
16; |
|
88 |
||
16147 | 89 |
// do not create me!! |
90 |
private Bootstrap() { |
|
91 |
} |
|
92 |
||
93 |
private static final DynamicLinker dynamicLinker; |
|
94 |
static { |
|
95 |
final DynamicLinkerFactory factory = new DynamicLinkerFactory(); |
|
24591
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22668
diff
changeset
|
96 |
final NashornBeansLinker nashornBeansLinker = new NashornBeansLinker(); |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22668
diff
changeset
|
97 |
factory.setPrioritizedLinkers( |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22668
diff
changeset
|
98 |
new NashornLinker(), |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22668
diff
changeset
|
99 |
new NashornPrimitiveLinker(), |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22668
diff
changeset
|
100 |
new NashornStaticClassLinker(), |
27815 | 101 |
new BoundCallableLinker(), |
24591
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22668
diff
changeset
|
102 |
new JavaSuperAdapterLinker(), |
26237
b6509e37ce64
8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents:
26236
diff
changeset
|
103 |
new JSObjectLinker(nashornBeansLinker), |
b6509e37ce64
8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents:
26236
diff
changeset
|
104 |
new BrowserJSObjectLinker(nashornBeansLinker), |
24591
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22668
diff
changeset
|
105 |
new ReflectionCheckLinker()); |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22668
diff
changeset
|
106 |
factory.setFallbackLinkers(nashornBeansLinker, new NashornBottomLinker()); |
16147 | 107 |
factory.setSyncOnRelink(true); |
24719 | 108 |
factory.setPrelinkFilter(new GuardedInvocationFilter() { |
109 |
@Override |
|
24738
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
110 |
public GuardedInvocation filter(final GuardedInvocation inv, final LinkRequest request, final LinkerServices linkerServices) { |
24719 | 111 |
final CallSiteDescriptor desc = request.getCallSiteDescriptor(); |
112 |
return OptimisticReturnFilters.filterOptimisticReturnValue(inv, desc).asType(linkerServices, desc.getMethodType()); |
|
113 |
} |
|
114 |
}); |
|
27360 | 115 |
factory.setAutoConversionStrategy(new MethodTypeConversionStrategy() { |
116 |
@Override |
|
117 |
public MethodHandle asType(final MethodHandle target, final MethodType newType) { |
|
118 |
return unboxReturnType(target, newType); |
|
119 |
} |
|
120 |
}); |
|
28881
0008daeef352
8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents:
28438
diff
changeset
|
121 |
factory.setInternalObjectsFilter(NashornBeansLinker.createHiddenObjectFilter()); |
24719 | 122 |
final int relinkThreshold = Options.getIntProperty("nashorn.unstable.relink.threshold", NASHORN_DEFAULT_UNSTABLE_RELINK_THRESHOLD); |
16147 | 123 |
if (relinkThreshold > -1) { |
124 |
factory.setUnstableRelinkThreshold(relinkThreshold); |
|
125 |
} |
|
19455 | 126 |
|
127 |
// Linkers for any additional language runtimes deployed alongside Nashorn will be picked up by the factory. |
|
128 |
factory.setClassLoader(Bootstrap.class.getClassLoader()); |
|
129 |
||
16147 | 130 |
dynamicLinker = factory.createLinker(); |
131 |
} |
|
132 |
||
133 |
/** |
|
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
134 |
* 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
|
135 |
* @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
|
136 |
* @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
|
137 |
*/ |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
138 |
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
|
139 |
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
|
140 |
return false; |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
141 |
} |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
142 |
|
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
143 |
return obj instanceof ScriptFunction || |
27815 | 144 |
isJSObjectFunction(obj) || |
145 |
BeansLinker.isDynamicMethod(obj) || |
|
146 |
obj instanceof BoundCallable || |
|
19627
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
147 |
isFunctionalInterfaceObject(obj) || |
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
148 |
obj instanceof StaticClass; |
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
149 |
} |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
150 |
|
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
151 |
/** |
27815 | 152 |
* Returns true if the given object is a strict callable |
153 |
* @param callable the callable object to be checked for strictness |
|
154 |
* @return true if the obj is a strict callable, false if it is a non-strict callable. |
|
155 |
* @throws ECMAException with {@code TypeError} if the object is not a callable. |
|
156 |
*/ |
|
157 |
public static boolean isStrictCallable(final Object callable) { |
|
158 |
if (callable instanceof ScriptFunction) { |
|
159 |
return ((ScriptFunction)callable).isStrict(); |
|
160 |
} else if (isJSObjectFunction(callable)) { |
|
161 |
return ((JSObject)callable).isStrictFunction(); |
|
162 |
} else if (callable instanceof BoundCallable) { |
|
163 |
return isStrictCallable(((BoundCallable)callable).getCallable()); |
|
164 |
} else if (BeansLinker.isDynamicMethod(callable) || callable instanceof StaticClass) { |
|
165 |
return false; |
|
166 |
} |
|
167 |
throw notFunction(callable); |
|
168 |
} |
|
169 |
||
170 |
private static ECMAException notFunction(final Object obj) { |
|
171 |
return typeError("not.a.function", ScriptRuntime.safeToString(obj)); |
|
172 |
} |
|
173 |
||
174 |
private static boolean isJSObjectFunction(final Object obj) { |
|
175 |
return obj instanceof JSObject && ((JSObject)obj).isFunction(); |
|
176 |
} |
|
177 |
||
178 |
/** |
|
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
179 |
* 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
|
180 |
* @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
|
181 |
* @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
|
182 |
*/ |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
183 |
public static boolean isDynamicMethod(final Object obj) { |
27815 | 184 |
return BeansLinker.isDynamicMethod(obj instanceof BoundCallable ? ((BoundCallable)obj).getCallable() : obj); |
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
185 |
} |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
186 |
|
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
187 |
/** |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
188 |
* 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
|
189 |
* java.lang.FunctionalInterface |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
190 |
* @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
|
191 |
* @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
|
192 |
*/ |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
193 |
public static boolean isFunctionalInterfaceObject(final Object obj) { |
28437
b9b1042592e6
8068889: Calling a @FunctionalInterface from JS leaks internal objects
attila
parents:
27815
diff
changeset
|
194 |
return !JSType.isPrimitive(obj) && (NashornBeansLinker.getFunctionalInterfaceMethod(obj.getClass()) != null); |
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
195 |
} |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
196 |
|
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
197 |
/** |
16147 | 198 |
* Create a call site and link it for Nashorn. This version of the method conforms to the invokedynamic bootstrap |
199 |
* method expected signature and is referenced from Nashorn generated bytecode as the bootstrap method for all |
|
200 |
* invokedynamic instructions. |
|
201 |
* @param lookup MethodHandle lookup. Ignored as Nashorn only uses public lookup. |
|
202 |
* @param opDesc Dynalink dynamic operation descriptor. |
|
203 |
* @param type Method type. |
|
204 |
* @param flags flags for call type, trace/profile etc. |
|
205 |
* @return CallSite with MethodHandle to appropriate method or null if not found. |
|
206 |
*/ |
|
207 |
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
|
208 |
return dynamicLinker.link(LinkerCallSite.newLinkerCallSite(lookup, opDesc, type, flags)); |
16147 | 209 |
} |
210 |
||
211 |
/** |
|
24719 | 212 |
* Boostrapper for math calls that may overflow |
213 |
* @param lookup lookup |
|
214 |
* @param name name of operation |
|
215 |
* @param type method type |
|
216 |
* @param programPoint program point to bind to callsite |
|
217 |
* |
|
218 |
* @return callsite for a math instrinic node |
|
219 |
*/ |
|
220 |
public static CallSite mathBootstrap(final MethodHandles.Lookup lookup, final String name, final MethodType type, final int programPoint) { |
|
221 |
final MethodHandle mh; |
|
222 |
switch (name) { |
|
223 |
case "iadd": |
|
224 |
mh = JSType.ADD_EXACT.methodHandle(); |
|
225 |
break; |
|
226 |
case "isub": |
|
227 |
mh = JSType.SUB_EXACT.methodHandle(); |
|
228 |
break; |
|
229 |
case "imul": |
|
230 |
mh = JSType.MUL_EXACT.methodHandle(); |
|
231 |
break; |
|
232 |
case "idiv": |
|
233 |
mh = JSType.DIV_EXACT.methodHandle(); |
|
234 |
break; |
|
24751 | 235 |
case "irem": |
236 |
mh = JSType.REM_EXACT.methodHandle(); |
|
237 |
break; |
|
24719 | 238 |
case "ineg": |
239 |
mh = JSType.NEGATE_EXACT.methodHandle(); |
|
240 |
break; |
|
241 |
case "ladd": |
|
242 |
mh = JSType.ADD_EXACT_LONG.methodHandle(); |
|
243 |
break; |
|
244 |
case "lsub": |
|
245 |
mh = JSType.SUB_EXACT_LONG.methodHandle(); |
|
246 |
break; |
|
247 |
case "lmul": |
|
248 |
mh = JSType.MUL_EXACT_LONG.methodHandle(); |
|
249 |
break; |
|
250 |
case "ldiv": |
|
251 |
mh = JSType.DIV_EXACT_LONG.methodHandle(); |
|
252 |
break; |
|
24751 | 253 |
case "lrem": |
254 |
mh = JSType.REM_EXACT_LONG.methodHandle(); |
|
255 |
break; |
|
24719 | 256 |
case "lneg": |
257 |
mh = JSType.NEGATE_EXACT_LONG.methodHandle(); |
|
258 |
break; |
|
259 |
default: |
|
260 |
throw new AssertionError("unsupported math intrinsic"); |
|
261 |
} |
|
262 |
return new ConstantCallSite(MH.insertArguments(mh, mh.type().parameterCount() - 1, programPoint)); |
|
263 |
} |
|
264 |
||
265 |
/** |
|
18841
9bbc4b8832b2
8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents:
16522
diff
changeset
|
266 |
* 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
|
267 |
* 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
|
268 |
* available dynamic operations can be found in the |
9bbc4b8832b2
8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents:
16522
diff
changeset
|
269 |
* <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
|
270 |
* examples here: |
16147 | 271 |
* <ul> |
272 |
* <li>Get a named property with fixed name: |
|
273 |
* <pre> |
|
274 |
* MethodHandle getColor = Boostrap.createDynamicInvoker("dyn:getProp:color", Object.class, Object.class); |
|
275 |
* Object obj = ...; // somehow obtain the object |
|
276 |
* Object color = getColor.invokeExact(obj); |
|
277 |
* </pre> |
|
278 |
* </li> |
|
279 |
* <li>Get a named property with variable name: |
|
280 |
* <pre> |
|
281 |
* MethodHandle getProperty = Boostrap.createDynamicInvoker("dyn:getElem", Object.class, Object.class, String.class); |
|
282 |
* Object obj = ...; // somehow obtain the object |
|
283 |
* Object color = getProperty.invokeExact(obj, "color"); |
|
284 |
* Object shape = getProperty.invokeExact(obj, "shape"); |
|
285 |
* MethodHandle getNumProperty = Boostrap.createDynamicInvoker("dyn:getElem", Object.class, Object.class, int.class); |
|
286 |
* Object elem42 = getNumProperty.invokeExact(obj, 42); |
|
287 |
* </pre> |
|
288 |
* </li> |
|
289 |
* <li>Set a named property with fixed name: |
|
290 |
* <pre> |
|
291 |
* MethodHandle setColor = Boostrap.createDynamicInvoker("dyn:setProp:color", void.class, Object.class, Object.class); |
|
292 |
* Object obj = ...; // somehow obtain the object |
|
293 |
* setColor.invokeExact(obj, Color.BLUE); |
|
294 |
* </pre> |
|
295 |
* </li> |
|
296 |
* <li>Set a property with variable name: |
|
297 |
* <pre> |
|
298 |
* MethodHandle setProperty = Boostrap.createDynamicInvoker("dyn:setElem", void.class, Object.class, String.class, Object.class); |
|
299 |
* Object obj = ...; // somehow obtain the object |
|
300 |
* setProperty.invokeExact(obj, "color", Color.BLUE); |
|
301 |
* setProperty.invokeExact(obj, "shape", Shape.CIRCLE); |
|
302 |
* </pre> |
|
303 |
* </li> |
|
304 |
* <li>Call a function on an object; two-step variant. This is the actual variant used by Nashorn-generated code: |
|
305 |
* <pre> |
|
306 |
* MethodHandle findFooFunction = Boostrap.createDynamicInvoker("dyn:getMethod:foo", Object.class, Object.class); |
|
307 |
* Object obj = ...; // somehow obtain the object |
|
308 |
* Object foo_fn = findFooFunction.invokeExact(obj); |
|
309 |
* MethodHandle callFunctionWithTwoArgs = Boostrap.createDynamicInvoker("dyn:call", Object.class, Object.class, Object.class, Object.class, Object.class); |
|
310 |
* // Note: "call" operation takes a function, then a "this" value, then the arguments: |
|
311 |
* Object foo_retval = callFunctionWithTwoArgs.invokeExact(foo_fn, obj, arg1, arg2); |
|
312 |
* </pre> |
|
313 |
* </li> |
|
314 |
* <li>Call a function on an object; single-step variant. Although Nashorn doesn't use this variant and never |
|
315 |
* emits any INVOKEDYNAMIC instructions with {@code dyn:getMethod}, it still supports this standard Dynalink |
|
316 |
* operation: |
|
317 |
* <pre> |
|
318 |
* MethodHandle callFunctionFooWithTwoArgs = Boostrap.createDynamicInvoker("dyn:callMethod:foo", Object.class, Object.class, Object.class, Object.class); |
|
319 |
* Object obj = ...; // somehow obtain the object |
|
320 |
* Object foo_retval = callFunctionFooWithTwoArgs.invokeExact(obj, arg1, arg2); |
|
321 |
* </pre> |
|
322 |
* </li> |
|
323 |
* </ul> |
|
324 |
* Few additional remarks: |
|
325 |
* <ul> |
|
326 |
* <li>Just as Nashorn works with any Java object, the invokers returned from this method can also be applied to |
|
327 |
* arbitrary Java objects in addition to Nashorn JavaScript objects.</li> |
|
328 |
* <li>For invoking a named function on an object, you can also use the {@link InvokeByName} convenience class.</li> |
|
329 |
* <li>For Nashorn objects {@code getElem}, {@code getProp}, and {@code getMethod} are handled almost identically, |
|
330 |
* since JavaScript doesn't distinguish between different kinds of properties on an object. Either can be used with |
|
331 |
* fixed property name or a variable property name. The only significant difference is handling of missing |
|
332 |
* properties: {@code getMethod} for a missing member will link to a potential invocation of |
|
333 |
* {@code __noSuchMethod__} on the object, {@code getProp} for a missing member will link to a potential invocation |
|
334 |
* of {@code __noSuchProperty__}, while {@code getElem} for a missing member will link to an empty getter.</li> |
|
335 |
* <li>In similar vein, {@code setElem} and {@code setProp} are handled identically on Nashorn objects.</li> |
|
336 |
* <li>There's no rule that the variable property identifier has to be a {@code String} for {@code getProp/setProp} |
|
337 |
* and {@code int} for {@code getElem/setElem}. You can declare their type to be {@code int}, {@code double}, |
|
338 |
* {@code Object}, and so on regardless of the kind of the operation.</li> |
|
339 |
* <li>You can be as specific in parameter types as you want. E.g. if you know that the receiver of the operation |
|
340 |
* will always be {@code ScriptObject}, you can pass {@code ScriptObject.class} as its parameter type. If you happen |
|
341 |
* to link to a method that expects different types, (you can use these invokers on POJOs too, after all, and end up |
|
342 |
* linking with their methods that have strongly-typed signatures), all necessary conversions allowed by either Java |
|
343 |
* or JavaScript will be applied: if invoked methods specify either primitive or wrapped Java numeric types, or |
|
344 |
* {@code String} or {@code boolean/Boolean}, then the parameters might be subjected to standard ECMAScript |
|
345 |
* {@code ToNumber}, {@code ToString}, and {@code ToBoolean} conversion, respectively. Less obviously, if the |
|
346 |
* expected parameter type is a SAM type, and you pass a JavaScript function, a proxy object implementing the SAM |
|
347 |
* type and delegating to the function will be passed. Linkage can often be optimized when linkers have more |
|
348 |
* specific type information than "everything can be an object".</li> |
|
349 |
* <li>You can also be as specific in return types as you want. For return types any necessary type conversion |
|
350 |
* available in either Java or JavaScript will be automatically applied, similar to the process described for |
|
351 |
* parameters, only in reverse direction: if you specify any either primitive or wrapped Java numeric type, or |
|
352 |
* {@code String} or {@code boolean/Boolean}, then the return values will be subjected to standard ECMAScript |
|
353 |
* {@code ToNumber}, {@code ToString}, and {@code ToBoolean} conversion, respectively. Less obviously, if the return |
|
354 |
* type is a SAM type, and the return value is a JavaScript function, a proxy object implementing the SAM type and |
|
355 |
* delegating to the function will be returned.</li> |
|
356 |
* </ul> |
|
357 |
* @param opDesc Dynalink dynamic operation descriptor. |
|
16173 | 358 |
* @param rtype the return type for the operation |
359 |
* @param ptypes the parameter types for the operation |
|
16147 | 360 |
* @return MethodHandle for invoking the operation. |
361 |
*/ |
|
362 |
public static MethodHandle createDynamicInvoker(final String opDesc, final Class<?> rtype, final Class<?>... ptypes) { |
|
16173 | 363 |
return createDynamicInvoker(opDesc, MethodType.methodType(rtype, ptypes)); |
364 |
} |
|
365 |
||
366 |
/** |
|
18841
9bbc4b8832b2
8010946: AccessControl.doPrivileged is broken when called from js script
attila
parents:
16522
diff
changeset
|
367 |
* Returns a dynamic invoker for a specified dynamic operation using the public lookup. Similar to |
27307
62ed492cbe63
8062401: User accessors require boxing and do not support optimistic types
hannesw
parents:
26237
diff
changeset
|
368 |
* {@link #createDynamicInvoker(String, Class, Class...)} but with an additional parameter to |
62ed492cbe63
8062401: User accessors require boxing and do not support optimistic types
hannesw
parents:
26237
diff
changeset
|
369 |
* set the call site flags of the dynamic invoker. |
62ed492cbe63
8062401: User accessors require boxing and do not support optimistic types
hannesw
parents:
26237
diff
changeset
|
370 |
* @param opDesc Dynalink dynamic operation descriptor. |
62ed492cbe63
8062401: User accessors require boxing and do not support optimistic types
hannesw
parents:
26237
diff
changeset
|
371 |
* @param flags the call site flags for the operation |
62ed492cbe63
8062401: User accessors require boxing and do not support optimistic types
hannesw
parents:
26237
diff
changeset
|
372 |
* @param rtype the return type for the operation |
62ed492cbe63
8062401: User accessors require boxing and do not support optimistic types
hannesw
parents:
26237
diff
changeset
|
373 |
* @param ptypes the parameter types for the operation |
62ed492cbe63
8062401: User accessors require boxing and do not support optimistic types
hannesw
parents:
26237
diff
changeset
|
374 |
* @return MethodHandle for invoking the operation. |
62ed492cbe63
8062401: User accessors require boxing and do not support optimistic types
hannesw
parents:
26237
diff
changeset
|
375 |
*/ |
62ed492cbe63
8062401: User accessors require boxing and do not support optimistic types
hannesw
parents:
26237
diff
changeset
|
376 |
public static MethodHandle createDynamicInvoker(final String opDesc, final int flags, final Class<?> rtype, final Class<?>... ptypes) { |
62ed492cbe63
8062401: User accessors require boxing and do not support optimistic types
hannesw
parents:
26237
diff
changeset
|
377 |
return bootstrap(MethodHandles.publicLookup(), opDesc, MethodType.methodType(rtype, ptypes), flags).dynamicInvoker(); |
62ed492cbe63
8062401: User accessors require boxing and do not support optimistic types
hannesw
parents:
26237
diff
changeset
|
378 |
} |
62ed492cbe63
8062401: User accessors require boxing and do not support optimistic types
hannesw
parents:
26237
diff
changeset
|
379 |
|
62ed492cbe63
8062401: User accessors require boxing and do not support optimistic types
hannesw
parents:
26237
diff
changeset
|
380 |
/** |
62ed492cbe63
8062401: User accessors require boxing and do not support optimistic types
hannesw
parents:
26237
diff
changeset
|
381 |
* Returns a dynamic invoker for a specified dynamic operation using the public lookup. Similar to |
16173 | 382 |
* {@link #createDynamicInvoker(String, Class, Class...)} but with return and parameter types composed into a |
383 |
* method type in the signature. See the discussion of that method for details. |
|
384 |
* @param opDesc Dynalink dynamic operation descriptor. |
|
385 |
* @param type the method type for the operation |
|
386 |
* @return MethodHandle for invoking the operation. |
|
387 |
*/ |
|
388 |
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
|
389 |
return bootstrap(MethodHandles.publicLookup(), opDesc, type, 0).dynamicInvoker(); |
16147 | 390 |
} |
391 |
||
392 |
/** |
|
27815 | 393 |
* Binds any object Nashorn can use as a [[Callable]] to a receiver and optionally arguments. |
394 |
* @param callable the callable to bind |
|
18876
ada98218aaae
8020324: Implement Object.bindProperties(target, source) for beans
attila
parents:
18841
diff
changeset
|
395 |
* @param boundThis the bound "this" value. |
27815 | 396 |
* @param boundArgs the bound arguments. Can be either null or empty array to signify no arguments are bound. |
397 |
* @return a bound callable. |
|
398 |
* @throws ECMAException with {@code TypeError} if the object is not a callable. |
|
18876
ada98218aaae
8020324: Implement Object.bindProperties(target, source) for beans
attila
parents:
18841
diff
changeset
|
399 |
*/ |
27815 | 400 |
public static Object bindCallable(final Object callable, final Object boundThis, final Object[] boundArgs) { |
401 |
if (callable instanceof ScriptFunctionImpl) { |
|
402 |
return ((ScriptFunctionImpl)callable).makeBoundFunction(boundThis, boundArgs); |
|
403 |
} else if (callable instanceof BoundCallable) { |
|
404 |
return ((BoundCallable)callable).bind(boundArgs); |
|
405 |
} else if (isCallable(callable)) { |
|
406 |
return new BoundCallable(callable, boundThis, boundArgs); |
|
407 |
} |
|
408 |
throw notFunction(callable); |
|
18876
ada98218aaae
8020324: Implement Object.bindProperties(target, source) for beans
attila
parents:
18841
diff
changeset
|
409 |
} |
18878
078eb52cf5bc
8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents:
18876
diff
changeset
|
410 |
|
078eb52cf5bc
8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents:
18876
diff
changeset
|
411 |
/** |
19627
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
412 |
* Creates a super-adapter for an adapter, that is, an adapter to the adapter that allows invocation of superclass |
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
413 |
* methods on it. |
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
414 |
* @param adapter the original adapter |
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
415 |
* @return a new adapter that can be used to invoke super methods on the original adapter. |
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
416 |
*/ |
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
417 |
public static Object createSuperAdapter(final Object adapter) { |
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
418 |
return new JavaSuperAdapter(adapter); |
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
419 |
} |
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
420 |
|
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
421 |
/** |
18878
078eb52cf5bc
8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents:
18876
diff
changeset
|
422 |
* 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
|
423 |
* {@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
|
424 |
* 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
|
425 |
* @param clazz the class being tested |
22668 | 426 |
* @param isStatic is access checked for static members (or instance members) |
18878
078eb52cf5bc
8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents:
18876
diff
changeset
|
427 |
*/ |
24738
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
428 |
public static void checkReflectionAccess(final Class<?> clazz, final boolean isStatic) { |
22668 | 429 |
ReflectionCheckLinker.checkReflectionAccess(clazz, isStatic); |
18878
078eb52cf5bc
8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents:
18876
diff
changeset
|
430 |
} |
078eb52cf5bc
8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents:
18876
diff
changeset
|
431 |
|
18876
ada98218aaae
8020324: Implement Object.bindProperties(target, source) for beans
attila
parents:
18841
diff
changeset
|
432 |
/** |
16147 | 433 |
* Returns the Nashorn's internally used dynamic linker's services object. Note that in code that is processing a |
434 |
* linking request, you will normally use the {@code LinkerServices} object passed by whatever top-level linker |
|
435 |
* invoked the linking (if the call site is in Nashorn-generated code, you'll get this object anyway). You should |
|
436 |
* only resort to retrieving a linker services object using this method when you need some linker services (e.g. |
|
437 |
* type converter method handles) outside of a code path that is linking a call site. |
|
438 |
* @return Nashorn's internal dynamic linker's services object. |
|
439 |
*/ |
|
440 |
public static LinkerServices getLinkerServices() { |
|
441 |
return dynamicLinker.getLinkerServices(); |
|
442 |
} |
|
443 |
||
444 |
/** |
|
445 |
* Takes a guarded invocation, and ensures its method and guard conform to the type of the call descriptor, using |
|
446 |
* all type conversions allowed by the linker's services. This method is used by Nashorn's linkers as a last step |
|
24719 | 447 |
* before returning guarded invocations. Most of the code used to produce the guarded invocations does not make an |
448 |
* effort to coordinate types of the methods, and so a final type adjustment before a guarded invocation is returned |
|
449 |
* to the aggregating linker is the responsibility of the linkers themselves. |
|
16147 | 450 |
* @param inv the guarded invocation that needs to be type-converted. Can be null. |
451 |
* @param linkerServices the linker services object providing the type conversions. |
|
452 |
* @param desc the call site descriptor to whose method type the invocation needs to conform. |
|
453 |
* @return the type-converted guarded invocation. If input is null, null is returned. If the input invocation |
|
454 |
* already conforms to the requested type, it is returned unchanged. |
|
455 |
*/ |
|
24719 | 456 |
static GuardedInvocation asTypeSafeReturn(final GuardedInvocation inv, final LinkerServices linkerServices, final CallSiteDescriptor desc) { |
457 |
return inv == null ? null : inv.asTypeSafeReturn(linkerServices, desc.getMethodType()); |
|
16147 | 458 |
} |
27360 | 459 |
|
460 |
/** |
|
461 |
* Adapts the return type of the method handle with {@code explicitCastArguments} when it is an unboxing |
|
462 |
* conversion. This will ensure that nulls are unwrapped to false or 0. |
|
463 |
* @param target the target method handle |
|
464 |
* @param newType the desired new type. Note that this method does not adapt the method handle completely to the |
|
465 |
* new type, it only adapts the return type; this is allowed as per |
|
466 |
* {@link DynamicLinkerFactory#setAutoConversionStrategy(MethodTypeConversionStrategy)}, which is what this method |
|
467 |
* is used for. |
|
468 |
* @return the method handle with adapted return type, if it required an unboxing conversion. |
|
469 |
*/ |
|
470 |
private static MethodHandle unboxReturnType(final MethodHandle target, final MethodType newType) { |
|
471 |
final MethodType targetType = target.type(); |
|
472 |
final Class<?> oldReturnType = targetType.returnType(); |
|
28438
f164fc2618a0
8068573: POJO setter using [] syntax throws an exception
attila
parents:
28437
diff
changeset
|
473 |
final Class<?> newReturnType = newType.returnType(); |
27360 | 474 |
if (TypeUtilities.isWrapperType(oldReturnType)) { |
475 |
if (newReturnType.isPrimitive()) { |
|
476 |
// The contract of setAutoConversionStrategy is such that the difference between newType and targetType |
|
477 |
// can only be JLS method invocation conversions. |
|
478 |
assert TypeUtilities.isMethodInvocationConvertible(oldReturnType, newReturnType); |
|
479 |
return MethodHandles.explicitCastArguments(target, targetType.changeReturnType(newReturnType)); |
|
480 |
} |
|
28438
f164fc2618a0
8068573: POJO setter using [] syntax throws an exception
attila
parents:
28437
diff
changeset
|
481 |
} else if (oldReturnType == void.class && newReturnType == Object.class) { |
f164fc2618a0
8068573: POJO setter using [] syntax throws an exception
attila
parents:
28437
diff
changeset
|
482 |
return MethodHandles.filterReturnValue(target, VOID_TO_OBJECT); |
27360 | 483 |
} |
484 |
return target; |
|
485 |
} |
|
16147 | 486 |
} |