author | mhaupt |
Tue, 09 Feb 2016 14:14:06 +0100 | |
changeset 35793 | b89ddacf3104 |
parent 35725 | c7a2c18529b1 |
child 36026 | ad5ff5d5459b |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
35793 | 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; |
|
35793 | 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; |
|
34447
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33686
diff
changeset
|
37 |
import jdk.dynalink.CallSiteDescriptor; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33686
diff
changeset
|
38 |
import jdk.dynalink.DynamicLinker; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33686
diff
changeset
|
39 |
import jdk.dynalink.DynamicLinkerFactory; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33686
diff
changeset
|
40 |
import jdk.dynalink.beans.BeansLinker; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33686
diff
changeset
|
41 |
import jdk.dynalink.beans.StaticClass; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33686
diff
changeset
|
42 |
import jdk.dynalink.linker.GuardedInvocation; |
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
43 |
import jdk.dynalink.linker.GuardingDynamicLinker; |
34447
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33686
diff
changeset
|
44 |
import jdk.dynalink.linker.LinkRequest; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33686
diff
changeset
|
45 |
import jdk.dynalink.linker.LinkerServices; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33686
diff
changeset
|
46 |
import jdk.dynalink.linker.MethodTypeConversionStrategy; |
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
47 |
import jdk.dynalink.linker.TypeBasedGuardingDynamicLinker; |
34447
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33686
diff
changeset
|
48 |
import jdk.dynalink.linker.support.TypeUtilities; |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
19627
diff
changeset
|
49 |
import jdk.nashorn.api.scripting.JSObject; |
16147 | 50 |
import jdk.nashorn.internal.codegen.CompilerConstants.Call; |
24719 | 51 |
import jdk.nashorn.internal.lookup.MethodHandleFactory; |
52 |
import jdk.nashorn.internal.lookup.MethodHandleFunctionality; |
|
33685
343aaf358c21
8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents:
33533
diff
changeset
|
53 |
import jdk.nashorn.internal.runtime.Context; |
27815 | 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 |
|
60 |
/** |
|
61 |
* This class houses bootstrap method for invokedynamic instructions generated by compiler. |
|
62 |
*/ |
|
63 |
public final class Bootstrap { |
|
64 |
/** Reference to the seed boostrap function */ |
|
65 |
public static final Call BOOTSTRAP = staticCallNoLookup(Bootstrap.class, "bootstrap", CallSite.class, Lookup.class, String.class, MethodType.class, int.class); |
|
66 |
||
24719 | 67 |
private static final MethodHandleFunctionality MH = MethodHandleFactory.getFunctionality(); |
68 |
||
28438
f164fc2618a0
8068573: POJO setter using [] syntax throws an exception
attila
parents:
28437
diff
changeset
|
69 |
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
|
70 |
|
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
71 |
private static final BeansLinker beansLinker = new BeansLinker(Bootstrap::createMissingMemberHandler); |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
72 |
private static final GuardingDynamicLinker[] prioritizedLinkers; |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
73 |
private static final GuardingDynamicLinker[] fallbackLinkers; |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
74 |
static { |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
75 |
final NashornBeansLinker nashornBeansLinker = new NashornBeansLinker(beansLinker); |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
76 |
prioritizedLinkers = new GuardingDynamicLinker[] { |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
77 |
new NashornLinker(), |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
78 |
new NashornPrimitiveLinker(), |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
79 |
new NashornStaticClassLinker(beansLinker), |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
80 |
new BoundCallableLinker(), |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
81 |
new JavaSuperAdapterLinker(beansLinker), |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
82 |
new JSObjectLinker(nashornBeansLinker), |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
83 |
new BrowserJSObjectLinker(nashornBeansLinker), |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
84 |
new ReflectionCheckLinker() |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
85 |
}; |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
86 |
fallbackLinkers = new GuardingDynamicLinker[] {nashornBeansLinker, new NashornBottomLinker() }; |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
87 |
} |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
88 |
|
16147 | 89 |
// do not create me!! |
90 |
private Bootstrap() { |
|
91 |
} |
|
92 |
||
33685
343aaf358c21
8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents:
33533
diff
changeset
|
93 |
/** |
343aaf358c21
8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents:
33533
diff
changeset
|
94 |
* Creates a Nashorn dynamic linker with the given app class loader. |
343aaf358c21
8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents:
33533
diff
changeset
|
95 |
* @param appLoader the app class loader. It will be used to discover |
343aaf358c21
8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents:
33533
diff
changeset
|
96 |
* additional language runtime linkers (if any). |
33686
1391474a6405
8141550: Introduce a command line option instead of nashorn.unstable.relink.threshold system property
attila
parents:
33685
diff
changeset
|
97 |
* @param unstableRelinkThreshold the unstable relink threshold |
33685
343aaf358c21
8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents:
33533
diff
changeset
|
98 |
* @return a newly created dynamic linker. |
343aaf358c21
8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents:
33533
diff
changeset
|
99 |
*/ |
33686
1391474a6405
8141550: Introduce a command line option instead of nashorn.unstable.relink.threshold system property
attila
parents:
33685
diff
changeset
|
100 |
public static DynamicLinker createDynamicLinker(final ClassLoader appLoader, |
1391474a6405
8141550: Introduce a command line option instead of nashorn.unstable.relink.threshold system property
attila
parents:
33685
diff
changeset
|
101 |
final int unstableRelinkThreshold) { |
16147 | 102 |
final DynamicLinkerFactory factory = new DynamicLinkerFactory(); |
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
103 |
factory.setPrioritizedLinkers(prioritizedLinkers); |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
104 |
factory.setFallbackLinkers(fallbackLinkers); |
16147 | 105 |
factory.setSyncOnRelink(true); |
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
106 |
factory.setPrelinkTransformer((inv, request, linkerServices) -> { |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
107 |
final CallSiteDescriptor desc = request.getCallSiteDescriptor(); |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
108 |
return OptimisticReturnFilters.filterOptimisticReturnValue(inv, desc).asType(linkerServices, desc.getMethodType()); |
24719 | 109 |
}); |
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
110 |
factory.setAutoConversionStrategy(Bootstrap::unboxReturnType); |
28881
0008daeef352
8072596: Arrays.asList results in ClassCastException with a JS array
attila
parents:
28438
diff
changeset
|
111 |
factory.setInternalObjectsFilter(NashornBeansLinker.createHiddenObjectFilter()); |
33686
1391474a6405
8141550: Introduce a command line option instead of nashorn.unstable.relink.threshold system property
attila
parents:
33685
diff
changeset
|
112 |
factory.setUnstableRelinkThreshold(unstableRelinkThreshold); |
19455 | 113 |
|
114 |
// Linkers for any additional language runtimes deployed alongside Nashorn will be picked up by the factory. |
|
33685
343aaf358c21
8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents:
33533
diff
changeset
|
115 |
factory.setClassLoader(appLoader); |
343aaf358c21
8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents:
33533
diff
changeset
|
116 |
return factory.createLinker(); |
16147 | 117 |
} |
118 |
||
119 |
/** |
|
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
120 |
* Returns a dynamic linker for the specific Java class using beans semantics. |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
121 |
* @param clazz the Java class |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
122 |
* @return a dynamic linker for the specific Java class using beans semantics. |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
123 |
*/ |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
124 |
public static TypeBasedGuardingDynamicLinker getBeanLinkerForClass(final Class<?> clazz) { |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
125 |
return beansLinker.getLinkerForClass(clazz); |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
126 |
} |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
127 |
|
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
128 |
/** |
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
129 |
* 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
|
130 |
* @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
|
131 |
* @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
|
132 |
*/ |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
133 |
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
|
134 |
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
|
135 |
return false; |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
136 |
} |
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 |
return obj instanceof ScriptFunction || |
27815 | 139 |
isJSObjectFunction(obj) || |
140 |
BeansLinker.isDynamicMethod(obj) || |
|
141 |
obj instanceof BoundCallable || |
|
19627
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
142 |
isFunctionalInterfaceObject(obj) || |
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
143 |
obj instanceof StaticClass; |
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
144 |
} |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
145 |
|
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
146 |
/** |
27815 | 147 |
* Returns true if the given object is a strict callable |
148 |
* @param callable the callable object to be checked for strictness |
|
149 |
* @return true if the obj is a strict callable, false if it is a non-strict callable. |
|
150 |
* @throws ECMAException with {@code TypeError} if the object is not a callable. |
|
151 |
*/ |
|
152 |
public static boolean isStrictCallable(final Object callable) { |
|
153 |
if (callable instanceof ScriptFunction) { |
|
154 |
return ((ScriptFunction)callable).isStrict(); |
|
155 |
} else if (isJSObjectFunction(callable)) { |
|
156 |
return ((JSObject)callable).isStrictFunction(); |
|
157 |
} else if (callable instanceof BoundCallable) { |
|
158 |
return isStrictCallable(((BoundCallable)callable).getCallable()); |
|
159 |
} else if (BeansLinker.isDynamicMethod(callable) || callable instanceof StaticClass) { |
|
160 |
return false; |
|
161 |
} |
|
162 |
throw notFunction(callable); |
|
163 |
} |
|
164 |
||
165 |
private static ECMAException notFunction(final Object obj) { |
|
166 |
return typeError("not.a.function", ScriptRuntime.safeToString(obj)); |
|
167 |
} |
|
168 |
||
169 |
private static boolean isJSObjectFunction(final Object obj) { |
|
170 |
return obj instanceof JSObject && ((JSObject)obj).isFunction(); |
|
171 |
} |
|
172 |
||
173 |
/** |
|
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
174 |
* 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
|
175 |
* @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
|
176 |
* @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
|
177 |
*/ |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
178 |
public static boolean isDynamicMethod(final Object obj) { |
27815 | 179 |
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
|
180 |
} |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
181 |
|
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 |
* 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
|
184 |
* java.lang.FunctionalInterface |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
185 |
* @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
|
186 |
* @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
|
187 |
*/ |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
188 |
public static boolean isFunctionalInterfaceObject(final Object obj) { |
32436
a6328e0a8dd6
8068901: Surprising behavior with more than one functional interface on a class
sundar
parents:
31738
diff
changeset
|
189 |
return !JSType.isPrimitive(obj) && (NashornBeansLinker.getFunctionalInterfaceMethodName(obj.getClass()) != null); |
19088
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
190 |
} |
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
191 |
|
153f268bfa72
8021122: Not all callables are handled for toString and other function valued properties
sundar
parents:
18878
diff
changeset
|
192 |
/** |
16147 | 193 |
* Create a call site and link it for Nashorn. This version of the method conforms to the invokedynamic bootstrap |
194 |
* method expected signature and is referenced from Nashorn generated bytecode as the bootstrap method for all |
|
195 |
* invokedynamic instructions. |
|
35330
7cc832bade3c
8133299: Nashorn Java adapters should not early bind to functions
attila
parents:
34979
diff
changeset
|
196 |
* @param lookup MethodHandle lookup. |
16147 | 197 |
* @param opDesc Dynalink dynamic operation descriptor. |
198 |
* @param type Method type. |
|
199 |
* @param flags flags for call type, trace/profile etc. |
|
200 |
* @return CallSite with MethodHandle to appropriate method or null if not found. |
|
201 |
*/ |
|
202 |
public static CallSite bootstrap(final Lookup lookup, final String opDesc, final MethodType type, final int flags) { |
|
33685
343aaf358c21
8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents:
33533
diff
changeset
|
203 |
return Context.getDynamicLinker(lookup.lookupClass()).link(LinkerCallSite.newLinkerCallSite(lookup, opDesc, type, flags)); |
16147 | 204 |
} |
205 |
||
206 |
/** |
|
35793 | 207 |
* Boostrapper for math calls that may overflow |
208 |
* @param lookup lookup |
|
209 |
* @param name name of operation |
|
210 |
* @param type method type |
|
211 |
* @param programPoint program point to bind to callsite |
|
212 |
* |
|
213 |
* @return callsite for a math intrinsic node |
|
214 |
*/ |
|
215 |
public static CallSite mathBootstrap(final Lookup lookup, final String name, final MethodType type, final int programPoint) { |
|
216 |
final MethodHandle mh; |
|
217 |
switch (name) { |
|
218 |
case "iadd": |
|
219 |
mh = JSType.ADD_EXACT.methodHandle(); |
|
220 |
break; |
|
221 |
case "isub": |
|
222 |
mh = JSType.SUB_EXACT.methodHandle(); |
|
223 |
break; |
|
224 |
case "imul": |
|
225 |
mh = JSType.MUL_EXACT.methodHandle(); |
|
226 |
break; |
|
227 |
case "idiv": |
|
228 |
mh = JSType.DIV_EXACT.methodHandle(); |
|
229 |
break; |
|
230 |
case "irem": |
|
231 |
mh = JSType.REM_EXACT.methodHandle(); |
|
232 |
break; |
|
233 |
case "ineg": |
|
234 |
mh = JSType.NEGATE_EXACT.methodHandle(); |
|
235 |
break; |
|
236 |
default: |
|
237 |
throw new AssertionError("unsupported math intrinsic"); |
|
238 |
} |
|
239 |
return new ConstantCallSite(MH.insertArguments(mh, mh.type().parameterCount() - 1, programPoint)); |
|
240 |
} |
|
241 |
||
242 |
/** |
|
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
243 |
* Returns a dynamic invoker for a specified dynamic operation using the |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
244 |
* public lookup. You can use this method to create a method handle that |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
245 |
* when invoked acts completely as if it were a Nashorn-linked call site. |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
246 |
* Note that the available operations are encoded in the flags, see |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
247 |
* {@link NashornCallSiteDescriptor} operation constants. If the operation |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
248 |
* takes a name, it should be set otherwise empty name (not null) should be |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
249 |
* used. All names (including the empty one) should be encoded using |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
250 |
* {@link NameCodec#encode(String)}. Few examples: |
16147 | 251 |
* <ul> |
252 |
* <li>Get a named property with fixed name: |
|
253 |
* <pre> |
|
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
254 |
* MethodHandle getColor = Boostrap.createDynamicInvoker( |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
255 |
* "color", |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
256 |
* NashornCallSiteDescriptor.GET_PROPERTY, |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
257 |
* Object.class, Object.class); |
16147 | 258 |
* Object obj = ...; // somehow obtain the object |
259 |
* Object color = getColor.invokeExact(obj); |
|
260 |
* </pre> |
|
261 |
* </li> |
|
262 |
* <li>Get a named property with variable name: |
|
263 |
* <pre> |
|
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
264 |
* MethodHandle getProperty = Boostrap.createDynamicInvoker( |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
265 |
* NameCodec.encode(""), |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
266 |
* NashornCallSiteDescriptor.GET_PROPERTY, |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
267 |
* Object.class, Object.class, String.class); |
16147 | 268 |
* Object obj = ...; // somehow obtain the object |
269 |
* Object color = getProperty.invokeExact(obj, "color"); |
|
270 |
* Object shape = getProperty.invokeExact(obj, "shape"); |
|
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
271 |
* |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
272 |
* MethodHandle getNumProperty = Boostrap.createDynamicInvoker( |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
273 |
* NameCodec.encode(""), |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
274 |
* NashornCallSiteDescriptor.GET_ELEMENT, |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
275 |
* Object.class, Object.class, int.class); |
16147 | 276 |
* Object elem42 = getNumProperty.invokeExact(obj, 42); |
277 |
* </pre> |
|
278 |
* </li> |
|
279 |
* <li>Set a named property with fixed name: |
|
280 |
* <pre> |
|
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
281 |
* MethodHandle setColor = Boostrap.createDynamicInvoker( |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
282 |
* "color", |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
283 |
* NashornCallSiteDescriptor.SET_PROPERTY, |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
284 |
* void.class, Object.class, Object.class); |
16147 | 285 |
* Object obj = ...; // somehow obtain the object |
286 |
* setColor.invokeExact(obj, Color.BLUE); |
|
287 |
* </pre> |
|
288 |
* </li> |
|
289 |
* <li>Set a property with variable name: |
|
290 |
* <pre> |
|
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
291 |
* MethodHandle setProperty = Boostrap.createDynamicInvoker( |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
292 |
* NameCodec.encode(""), |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
293 |
* NashornCallSiteDescriptor.SET_PROPERTY, |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
294 |
* void.class, Object.class, String.class, Object.class); |
16147 | 295 |
* Object obj = ...; // somehow obtain the object |
296 |
* setProperty.invokeExact(obj, "color", Color.BLUE); |
|
297 |
* setProperty.invokeExact(obj, "shape", Shape.CIRCLE); |
|
298 |
* </pre> |
|
299 |
* </li> |
|
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
300 |
* <li>Call a function on an object; note it's a two-step process: get the |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
301 |
* method, then invoke the method. This is the actual: |
16147 | 302 |
* <pre> |
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
303 |
* MethodHandle findFooFunction = Boostrap.createDynamicInvoker( |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
304 |
* "foo", |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
305 |
* NashornCallSiteDescriptor.GET_METHOD, |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
306 |
* Object.class, Object.class); |
16147 | 307 |
* Object obj = ...; // somehow obtain the object |
308 |
* Object foo_fn = findFooFunction.invokeExact(obj); |
|
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
309 |
* MethodHandle callFunctionWithTwoArgs = Boostrap.createDynamicCallInvoker( |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
310 |
* Object.class, Object.class, Object.class, Object.class, Object.class); |
16147 | 311 |
* // Note: "call" operation takes a function, then a "this" value, then the arguments: |
312 |
* Object foo_retval = callFunctionWithTwoArgs.invokeExact(foo_fn, obj, arg1, arg2); |
|
313 |
* </pre> |
|
314 |
* </li> |
|
315 |
* </ul> |
|
316 |
* Few additional remarks: |
|
317 |
* <ul> |
|
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
318 |
* <li>Just as Nashorn works with any Java object, the invokers returned |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
319 |
* from this method can also be applied to arbitrary Java objects in |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
320 |
* addition to Nashorn JavaScript objects.</li> |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
321 |
* <li>For invoking a named function on an object, you can also use the |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
322 |
* {@link InvokeByName} convenience class.</li> |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
323 |
* <li>There's no rule that the variable property identifier has to be a |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
324 |
* {@code String} for {@code GET_PROPERTY/SET_PROPERTY} and {@code int} for |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
325 |
* {@code GET_ELEMENT/SET_ELEMENT}. You can declare their type to be |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
326 |
* {@code int}, {@code double}, {@code Object}, and so on regardless of the |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
327 |
* kind of the operation.</li> |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
328 |
* <li>You can be as specific in parameter types as you want. E.g. if you |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
329 |
* know that the receiver of the operation will always be |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
330 |
* {@code ScriptObject}, you can pass {@code ScriptObject.class} as its |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
331 |
* parameter type. If you happen to link to a method that expects different |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
332 |
* types, (you can use these invokers on POJOs too, after all, and end up |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
333 |
* linking with their methods that have strongly-typed signatures), all |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
334 |
* necessary conversions allowed by either Java or JavaScript will be |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
335 |
* applied: if invoked methods specify either primitive or wrapped Java |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
336 |
* numeric types, or {@code String} or {@code boolean/Boolean}, then the |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
337 |
* parameters might be subjected to standard ECMAScript {@code ToNumber}, |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
338 |
* {@code ToString}, and {@code ToBoolean} conversion, respectively. Less |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
339 |
* obviously, if the expected parameter type is a SAM type, and you pass a |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
340 |
* JavaScript function, a proxy object implementing the SAM type and |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
341 |
* delegating to the function will be passed. Linkage can often be optimized |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
342 |
* when linkers have more specific type information than "everything can be |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
343 |
* an object".</li> |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
344 |
* <li>You can also be as specific in return types as you want. For return |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
345 |
* types any necessary type conversion available in either Java or |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
346 |
* JavaScript will be automatically applied, similar to the process |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
347 |
* described for parameters, only in reverse direction: if you specify any |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
348 |
* either primitive or wrapped Java numeric type, or {@code String} or |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
349 |
* {@code boolean/Boolean}, then the return values will be subjected to |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
350 |
* standard ECMAScript {@code ToNumber}, {@code ToString}, and |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
351 |
* {@code ToBoolean} conversion, respectively. Less obviously, if the return |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
352 |
* type is a SAM type, and the return value is a JavaScript function, a |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
353 |
* proxy object implementing the SAM type and delegating to the function |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
354 |
* will be returned.</li> |
16147 | 355 |
* </ul> |
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
356 |
* @param name name at the call site. Must not be null. Must be encoded |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
357 |
* using {@link NameCodec#encode(String)}. If the operation does not take a |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
358 |
* name, use empty string (also has to be encoded). |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
359 |
* @param flags the call site flags for the operation; see |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
360 |
* {@link NashornCallSiteDescriptor} for available flags. The most important |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
361 |
* part of the flags are the ones encoding the actual operation. |
16173 | 362 |
* @param rtype the return type for the operation |
363 |
* @param ptypes the parameter types for the operation |
|
16147 | 364 |
* @return MethodHandle for invoking the operation. |
365 |
*/ |
|
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
366 |
public static MethodHandle createDynamicInvoker(final String name, final int flags, final Class<?> rtype, final Class<?>... ptypes) { |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
367 |
return bootstrap(MethodHandles.publicLookup(), name, MethodType.methodType(rtype, ptypes), flags).dynamicInvoker(); |
16173 | 368 |
} |
369 |
||
370 |
/** |
|
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
371 |
* Returns a dynamic invoker for the {@link NashornCallSiteDescriptor#CALL} |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
372 |
* operation using the public lookup. |
27307
62ed492cbe63
8062401: User accessors require boxing and do not support optimistic types
hannesw
parents:
26237
diff
changeset
|
373 |
* @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
|
374 |
* @param ptypes the parameter types for the operation |
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
375 |
* @return a dynamic invoker for the {@code CALL} operation. |
27307
62ed492cbe63
8062401: User accessors require boxing and do not support optimistic types
hannesw
parents:
26237
diff
changeset
|
376 |
*/ |
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
377 |
public static MethodHandle createDynamicCallInvoker(final Class<?> rtype, final Class<?>... ptypes) { |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
378 |
return createDynamicInvoker("", NashornCallSiteDescriptor.CALL, rtype, ptypes); |
27307
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 |
/** |
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
382 |
* Returns a dynamic invoker for a specified dynamic operation using the |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
383 |
* public lookup. Similar to |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
384 |
* {@link #createDynamicInvoker(String, int, Class, Class...)} but with |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
385 |
* already precomposed method type. |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
386 |
* @param name name at the call site. |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
387 |
* @param flags flags at the call site |
16173 | 388 |
* @param type the method type for the operation |
389 |
* @return MethodHandle for invoking the operation. |
|
390 |
*/ |
|
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
391 |
public static MethodHandle createDynamicInvoker(final String name, final int flags, final MethodType type) { |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33337
diff
changeset
|
392 |
return bootstrap(MethodHandles.publicLookup(), name, type, flags).dynamicInvoker(); |
16147 | 393 |
} |
394 |
||
395 |
/** |
|
27815 | 396 |
* Binds any object Nashorn can use as a [[Callable]] to a receiver and optionally arguments. |
397 |
* @param callable the callable to bind |
|
18876
ada98218aaae
8020324: Implement Object.bindProperties(target, source) for beans
attila
parents:
18841
diff
changeset
|
398 |
* @param boundThis the bound "this" value. |
27815 | 399 |
* @param boundArgs the bound arguments. Can be either null or empty array to signify no arguments are bound. |
400 |
* @return a bound callable. |
|
401 |
* @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
|
402 |
*/ |
27815 | 403 |
public static Object bindCallable(final Object callable, final Object boundThis, final Object[] boundArgs) { |
32527
b105632002c5
8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents:
32436
diff
changeset
|
404 |
if (callable instanceof ScriptFunction) { |
b105632002c5
8027137: Merge ScriptFunction and ScriptFunctionImpl
sundar
parents:
32436
diff
changeset
|
405 |
return ((ScriptFunction)callable).createBound(boundThis, boundArgs); |
27815 | 406 |
} else if (callable instanceof BoundCallable) { |
407 |
return ((BoundCallable)callable).bind(boundArgs); |
|
408 |
} else if (isCallable(callable)) { |
|
409 |
return new BoundCallable(callable, boundThis, boundArgs); |
|
410 |
} |
|
411 |
throw notFunction(callable); |
|
18876
ada98218aaae
8020324: Implement Object.bindProperties(target, source) for beans
attila
parents:
18841
diff
changeset
|
412 |
} |
18878
078eb52cf5bc
8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents:
18876
diff
changeset
|
413 |
|
078eb52cf5bc
8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents:
18876
diff
changeset
|
414 |
/** |
19627
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
415 |
* 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
|
416 |
* methods on it. |
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
417 |
* @param adapter the original adapter |
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
418 |
* @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
|
419 |
*/ |
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
420 |
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
|
421 |
return new JavaSuperAdapter(adapter); |
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
422 |
} |
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
423 |
|
90d910ec15a3
8023630: Implement Java.super() as the preferred way to call super methods
attila
parents:
19455
diff
changeset
|
424 |
/** |
18878
078eb52cf5bc
8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents:
18876
diff
changeset
|
425 |
* 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
|
426 |
* {@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
|
427 |
* 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
|
428 |
* @param clazz the class being tested |
22668 | 429 |
* @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
|
430 |
*/ |
24738
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
431 |
public static void checkReflectionAccess(final Class<?> clazz, final boolean isStatic) { |
22668 | 432 |
ReflectionCheckLinker.checkReflectionAccess(clazz, isStatic); |
18878
078eb52cf5bc
8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents:
18876
diff
changeset
|
433 |
} |
078eb52cf5bc
8020508: Enforce reflection access restrictions on Object.bindProperties
attila
parents:
18876
diff
changeset
|
434 |
|
18876
ada98218aaae
8020324: Implement Object.bindProperties(target, source) for beans
attila
parents:
18841
diff
changeset
|
435 |
/** |
16147 | 436 |
* Returns the Nashorn's internally used dynamic linker's services object. Note that in code that is processing a |
437 |
* linking request, you will normally use the {@code LinkerServices} object passed by whatever top-level linker |
|
438 |
* invoked the linking (if the call site is in Nashorn-generated code, you'll get this object anyway). You should |
|
439 |
* only resort to retrieving a linker services object using this method when you need some linker services (e.g. |
|
440 |
* type converter method handles) outside of a code path that is linking a call site. |
|
441 |
* @return Nashorn's internal dynamic linker's services object. |
|
442 |
*/ |
|
443 |
public static LinkerServices getLinkerServices() { |
|
33685
343aaf358c21
8141538: Make DynamicLinker specific to a Context in Nashorn
attila
parents:
33533
diff
changeset
|
444 |
return Context.getDynamicLinker().getLinkerServices(); |
16147 | 445 |
} |
446 |
||
447 |
/** |
|
448 |
* Takes a guarded invocation, and ensures its method and guard conform to the type of the call descriptor, using |
|
449 |
* all type conversions allowed by the linker's services. This method is used by Nashorn's linkers as a last step |
|
24719 | 450 |
* before returning guarded invocations. Most of the code used to produce the guarded invocations does not make an |
451 |
* effort to coordinate types of the methods, and so a final type adjustment before a guarded invocation is returned |
|
452 |
* to the aggregating linker is the responsibility of the linkers themselves. |
|
16147 | 453 |
* @param inv the guarded invocation that needs to be type-converted. Can be null. |
454 |
* @param linkerServices the linker services object providing the type conversions. |
|
455 |
* @param desc the call site descriptor to whose method type the invocation needs to conform. |
|
456 |
* @return the type-converted guarded invocation. If input is null, null is returned. If the input invocation |
|
457 |
* already conforms to the requested type, it is returned unchanged. |
|
458 |
*/ |
|
24719 | 459 |
static GuardedInvocation asTypeSafeReturn(final GuardedInvocation inv, final LinkerServices linkerServices, final CallSiteDescriptor desc) { |
460 |
return inv == null ? null : inv.asTypeSafeReturn(linkerServices, desc.getMethodType()); |
|
16147 | 461 |
} |
27360 | 462 |
|
463 |
/** |
|
464 |
* Adapts the return type of the method handle with {@code explicitCastArguments} when it is an unboxing |
|
465 |
* conversion. This will ensure that nulls are unwrapped to false or 0. |
|
466 |
* @param target the target method handle |
|
467 |
* @param newType the desired new type. Note that this method does not adapt the method handle completely to the |
|
468 |
* new type, it only adapts the return type; this is allowed as per |
|
469 |
* {@link DynamicLinkerFactory#setAutoConversionStrategy(MethodTypeConversionStrategy)}, which is what this method |
|
470 |
* is used for. |
|
471 |
* @return the method handle with adapted return type, if it required an unboxing conversion. |
|
472 |
*/ |
|
473 |
private static MethodHandle unboxReturnType(final MethodHandle target, final MethodType newType) { |
|
474 |
final MethodType targetType = target.type(); |
|
475 |
final Class<?> oldReturnType = targetType.returnType(); |
|
28438
f164fc2618a0
8068573: POJO setter using [] syntax throws an exception
attila
parents:
28437
diff
changeset
|
476 |
final Class<?> newReturnType = newType.returnType(); |
27360 | 477 |
if (TypeUtilities.isWrapperType(oldReturnType)) { |
478 |
if (newReturnType.isPrimitive()) { |
|
479 |
// The contract of setAutoConversionStrategy is such that the difference between newType and targetType |
|
480 |
// can only be JLS method invocation conversions. |
|
481 |
assert TypeUtilities.isMethodInvocationConvertible(oldReturnType, newReturnType); |
|
482 |
return MethodHandles.explicitCastArguments(target, targetType.changeReturnType(newReturnType)); |
|
483 |
} |
|
28438
f164fc2618a0
8068573: POJO setter using [] syntax throws an exception
attila
parents:
28437
diff
changeset
|
484 |
} else if (oldReturnType == void.class && newReturnType == Object.class) { |
f164fc2618a0
8068573: POJO setter using [] syntax throws an exception
attila
parents:
28437
diff
changeset
|
485 |
return MethodHandles.filterReturnValue(target, VOID_TO_OBJECT); |
27360 | 486 |
} |
487 |
return target; |
|
488 |
} |
|
34979
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
489 |
|
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
490 |
private static MethodHandle createMissingMemberHandler( |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
491 |
final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception { |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
492 |
if (BrowserJSObjectLinker.canLinkTypeStatic(linkRequest.getReceiver().getClass())) { |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
493 |
// Don't create missing member handlers for the browser JS objects as they |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
494 |
// have their own logic. |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
495 |
return null; |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
496 |
} |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
497 |
return NashornBottomLinker.linkMissingBeanMember(linkRequest, linkerServices); |
03b189baa361
8144919: Implement missing member handler for BeansLinker
attila
parents:
34732
diff
changeset
|
498 |
} |
16147 | 499 |
} |