author | hannesw |
Mon, 15 Dec 2014 12:32:34 +0100 | |
changeset 28125 | 118973efb1d3 |
parent 27361 | 86c4ddb4797b |
child 29133 | 1cd7d8af99ba |
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 java.lang.invoke.MethodHandle; |
|
29 |
import java.lang.invoke.MethodHandles; |
|
30 |
import java.lang.invoke.MethodType; |
|
21449 | 31 |
import java.util.HashMap; |
32 |
import java.util.Map; |
|
24591
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
33 |
import javax.script.Bindings; |
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16151
diff
changeset
|
34 |
import jdk.internal.dynalink.CallSiteDescriptor; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16151
diff
changeset
|
35 |
import jdk.internal.dynalink.linker.GuardedInvocation; |
22669 | 36 |
import jdk.internal.dynalink.linker.GuardedTypeConversion; |
21449 | 37 |
import jdk.internal.dynalink.linker.GuardingTypeConverterFactory; |
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16151
diff
changeset
|
38 |
import jdk.internal.dynalink.linker.LinkRequest; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16151
diff
changeset
|
39 |
import jdk.internal.dynalink.linker.LinkerServices; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16151
diff
changeset
|
40 |
import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16151
diff
changeset
|
41 |
import jdk.internal.dynalink.support.CallSiteDescriptorFactory; |
21449 | 42 |
import jdk.nashorn.api.scripting.JSObject; |
43 |
import jdk.nashorn.internal.lookup.MethodHandleFactory; |
|
44 |
import jdk.nashorn.internal.lookup.MethodHandleFunctionality; |
|
28125
118973efb1d3
8062030: Nashorn bug retrieving array property after key string concatenation
hannesw
parents:
27361
diff
changeset
|
45 |
import jdk.nashorn.internal.runtime.ConsString; |
16147 | 46 |
import jdk.nashorn.internal.runtime.JSType; |
47 |
||
48 |
/** |
|
49 |
* A Dynalink linker to handle web browser built-in JS (DOM etc.) objects as well |
|
50 |
* as ScriptObjects from other Nashorn contexts. |
|
51 |
*/ |
|
21449 | 52 |
final class JSObjectLinker implements TypeBasedGuardingDynamicLinker, GuardingTypeConverterFactory { |
24591
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
53 |
private final NashornBeansLinker nashornBeansLinker; |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
54 |
|
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
55 |
JSObjectLinker(final NashornBeansLinker nashornBeansLinker) { |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
56 |
this.nashornBeansLinker = nashornBeansLinker; |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
57 |
} |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
58 |
|
16147 | 59 |
@Override |
60 |
public boolean canLinkType(final Class<?> type) { |
|
61 |
return canLinkTypeStatic(type); |
|
62 |
} |
|
63 |
||
64 |
static boolean canLinkTypeStatic(final Class<?> type) { |
|
24591
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
65 |
// can link JSObject also handles Map, Bindings to make |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
66 |
// sure those are not JSObjects. |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
67 |
return Map.class.isAssignableFrom(type) || |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
68 |
Bindings.class.isAssignableFrom(type) || |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
69 |
JSObject.class.isAssignableFrom(type); |
16147 | 70 |
} |
71 |
||
72 |
@Override |
|
73 |
public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices) throws Exception { |
|
74 |
final LinkRequest requestWithoutContext = request.withoutRuntimeContext(); // Nashorn has no runtime context |
|
75 |
final Object self = requestWithoutContext.getReceiver(); |
|
76 |
final CallSiteDescriptor desc = requestWithoutContext.getCallSiteDescriptor(); |
|
77 |
||
78 |
if (desc.getNameTokenCount() < 2 || !"dyn".equals(desc.getNameToken(CallSiteDescriptor.SCHEME))) { |
|
79 |
// We only support standard "dyn:*[:*]" operations |
|
80 |
return null; |
|
81 |
} |
|
82 |
||
83 |
final GuardedInvocation inv; |
|
84 |
if (self instanceof JSObject) { |
|
26237
b6509e37ce64
8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents:
25865
diff
changeset
|
85 |
inv = lookup(desc, request, linkerServices); |
24591
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
86 |
} else if (self instanceof Map || self instanceof Bindings) { |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
87 |
// guard to make sure the Map or Bindings does not turn into JSObject later! |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
88 |
final GuardedInvocation beanInv = nashornBeansLinker.getGuardedInvocation(request, linkerServices); |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
89 |
inv = new GuardedInvocation(beanInv.getInvocation(), |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
90 |
NashornGuards.combineGuards(beanInv.getGuard(), NashornGuards.getNotJSObjectGuard())); |
16147 | 91 |
} else { |
92 |
throw new AssertionError(); // Should never reach here. |
|
93 |
} |
|
94 |
||
24719 | 95 |
return Bootstrap.asTypeSafeReturn(inv, linkerServices, desc); |
16147 | 96 |
} |
97 |
||
21449 | 98 |
@Override |
22669 | 99 |
public GuardedTypeConversion convertToType(final Class<?> sourceType, final Class<?> targetType) throws Exception { |
21450
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
100 |
final boolean sourceIsAlwaysJSObject = JSObject.class.isAssignableFrom(sourceType); |
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
101 |
if(!sourceIsAlwaysJSObject && !sourceType.isAssignableFrom(JSObject.class)) { |
21449 | 102 |
return null; |
103 |
} |
|
104 |
||
21450
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
105 |
final MethodHandle converter = CONVERTERS.get(targetType); |
21449 | 106 |
if(converter == null) { |
107 |
return null; |
|
108 |
} |
|
109 |
||
22669 | 110 |
return new GuardedTypeConversion(new GuardedInvocation(converter, sourceIsAlwaysJSObject ? null : IS_JSOBJECT_GUARD).asType(MethodType.methodType(targetType, sourceType)), true); |
21449 | 111 |
} |
112 |
||
113 |
||
26237
b6509e37ce64
8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents:
25865
diff
changeset
|
114 |
private GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request, final LinkerServices linkerServices) throws Exception { |
16147 | 115 |
final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0); |
116 |
final int c = desc.getNameTokenCount(); |
|
24738
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
117 |
|
16147 | 118 |
switch (operator) { |
119 |
case "getProp": |
|
120 |
case "getElem": |
|
121 |
case "getMethod": |
|
26237
b6509e37ce64
8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents:
25865
diff
changeset
|
122 |
if (c > 2) { |
b6509e37ce64
8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents:
25865
diff
changeset
|
123 |
return findGetMethod(desc); |
b6509e37ce64
8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents:
25865
diff
changeset
|
124 |
} |
27361 | 125 |
// For indexed get, we want get GuardedInvocation beans linker and pass it. |
126 |
// JSObjectLinker.get uses this fallback getter for explicit signature method access. |
|
127 |
return findGetIndexMethod(nashornBeansLinker.getGuardedInvocation(request, linkerServices)); |
|
16147 | 128 |
case "setProp": |
129 |
case "setElem": |
|
130 |
return c > 2 ? findSetMethod(desc) : findSetIndexMethod(); |
|
131 |
case "call": |
|
21449 | 132 |
return findCallMethod(desc); |
16147 | 133 |
case "new": |
18614 | 134 |
return findNewMethod(desc); |
16147 | 135 |
default: |
136 |
return null; |
|
137 |
} |
|
138 |
} |
|
139 |
||
18614 | 140 |
private static GuardedInvocation findGetMethod(final CallSiteDescriptor desc) { |
24738
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
141 |
final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND); |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
142 |
final MethodHandle getter = MH.insertArguments(JSOBJECT_GETMEMBER, 1, name); |
24719 | 143 |
return new GuardedInvocation(getter, IS_JSOBJECT_GUARD); |
16147 | 144 |
} |
145 |
||
26237
b6509e37ce64
8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents:
25865
diff
changeset
|
146 |
private static GuardedInvocation findGetIndexMethod(final GuardedInvocation inv) { |
b6509e37ce64
8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents:
25865
diff
changeset
|
147 |
final MethodHandle getter = MH.insertArguments(JSOBJECTLINKER_GET, 0, inv.getInvocation()); |
b6509e37ce64
8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents:
25865
diff
changeset
|
148 |
return inv.replaceMethods(getter, inv.getGuard()); |
16147 | 149 |
} |
150 |
||
151 |
private static GuardedInvocation findSetMethod(final CallSiteDescriptor desc) { |
|
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
18614
diff
changeset
|
152 |
final MethodHandle getter = MH.insertArguments(JSOBJECT_SETMEMBER, 1, desc.getNameToken(2)); |
24719 | 153 |
return new GuardedInvocation(getter, IS_JSOBJECT_GUARD); |
16147 | 154 |
} |
155 |
||
156 |
private static GuardedInvocation findSetIndexMethod() { |
|
24719 | 157 |
return new GuardedInvocation(JSOBJECTLINKER_PUT, IS_JSOBJECT_GUARD); |
16147 | 158 |
} |
159 |
||
21449 | 160 |
private static GuardedInvocation findCallMethod(final CallSiteDescriptor desc) { |
24719 | 161 |
// TODO: if call site is already a vararg, don't do asCollector |
24738
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
162 |
MethodHandle mh = JSOBJECT_CALL; |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
163 |
if (NashornCallSiteDescriptor.isApplyToCall(desc)) { |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
164 |
mh = MH.insertArguments(JSOBJECT_CALL_TO_APPLY, 0, JSOBJECT_CALL); |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
165 |
} |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
166 |
return new GuardedInvocation(MH.asCollector(mh, Object[].class, desc.getMethodType().parameterCount() - 2), IS_JSOBJECT_GUARD); |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
18614
diff
changeset
|
167 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
18614
diff
changeset
|
168 |
|
18614 | 169 |
private static GuardedInvocation findNewMethod(final CallSiteDescriptor desc) { |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
18614
diff
changeset
|
170 |
final MethodHandle func = MH.asCollector(JSOBJECT_NEW, Object[].class, desc.getMethodType().parameterCount() - 1); |
24719 | 171 |
return new GuardedInvocation(func, IS_JSOBJECT_GUARD); |
16147 | 172 |
} |
173 |
||
174 |
@SuppressWarnings("unused") |
|
175 |
private static boolean isJSObject(final Object self) { |
|
176 |
return self instanceof JSObject; |
|
177 |
} |
|
178 |
||
179 |
@SuppressWarnings("unused") |
|
26237
b6509e37ce64
8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents:
25865
diff
changeset
|
180 |
private static Object get(final MethodHandle fallback, final Object jsobj, final Object key) |
b6509e37ce64
8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents:
25865
diff
changeset
|
181 |
throws Throwable { |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
18614
diff
changeset
|
182 |
if (key instanceof Integer) { |
20928
3ff39d5c8c08
8026137: Fix Issues with Binary Evaluation Order
lagergren
parents:
19889
diff
changeset
|
183 |
return ((JSObject)jsobj).getSlot((Integer)key); |
16147 | 184 |
} else if (key instanceof Number) { |
185 |
final int index = getIndex((Number)key); |
|
186 |
if (index > -1) { |
|
187 |
return ((JSObject)jsobj).getSlot(index); |
|
188 |
} |
|
28125
118973efb1d3
8062030: Nashorn bug retrieving array property after key string concatenation
hannesw
parents:
27361
diff
changeset
|
189 |
} else if (key instanceof String || key instanceof ConsString) { |
118973efb1d3
8062030: Nashorn bug retrieving array property after key string concatenation
hannesw
parents:
27361
diff
changeset
|
190 |
final String name = key.toString(); |
26237
b6509e37ce64
8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents:
25865
diff
changeset
|
191 |
// get with method name and signature. delegate it to beans linker! |
b6509e37ce64
8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents:
25865
diff
changeset
|
192 |
if (name.indexOf('(') != -1) { |
28125
118973efb1d3
8062030: Nashorn bug retrieving array property after key string concatenation
hannesw
parents:
27361
diff
changeset
|
193 |
return fallback.invokeExact(jsobj, (Object) name); |
26237
b6509e37ce64
8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents:
25865
diff
changeset
|
194 |
} |
27361 | 195 |
return ((JSObject)jsobj).getMember(name); |
16147 | 196 |
} |
197 |
return null; |
|
198 |
} |
|
199 |
||
200 |
@SuppressWarnings("unused") |
|
201 |
private static void put(final Object jsobj, final Object key, final Object value) { |
|
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
18614
diff
changeset
|
202 |
if (key instanceof Integer) { |
20928
3ff39d5c8c08
8026137: Fix Issues with Binary Evaluation Order
lagergren
parents:
19889
diff
changeset
|
203 |
((JSObject)jsobj).setSlot((Integer)key, value); |
16147 | 204 |
} else if (key instanceof Number) { |
205 |
((JSObject)jsobj).setSlot(getIndex((Number)key), value); |
|
28125
118973efb1d3
8062030: Nashorn bug retrieving array property after key string concatenation
hannesw
parents:
27361
diff
changeset
|
206 |
} else if (key instanceof String || key instanceof ConsString) { |
118973efb1d3
8062030: Nashorn bug retrieving array property after key string concatenation
hannesw
parents:
27361
diff
changeset
|
207 |
((JSObject)jsobj).setMember(key.toString(), value); |
16147 | 208 |
} |
209 |
} |
|
210 |
||
21450
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
211 |
@SuppressWarnings("unused") |
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
212 |
private static int toInt32(final JSObject obj) { |
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
213 |
return JSType.toInt32(toNumber(obj)); |
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
214 |
} |
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
215 |
|
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
216 |
@SuppressWarnings("unused") |
24719 | 217 |
private static long toLong(final JSObject obj) { |
218 |
return JSType.toLong(toNumber(obj)); |
|
21450
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
219 |
} |
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
220 |
|
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
221 |
private static double toNumber(final JSObject obj) { |
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
222 |
return obj == null ? 0 : obj.toNumber(); |
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
223 |
} |
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
224 |
|
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
225 |
@SuppressWarnings("unused") |
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
226 |
private static boolean toBoolean(final JSObject obj) { |
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
227 |
return obj != null; |
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
228 |
} |
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
229 |
|
16147 | 230 |
private static int getIndex(final Number n) { |
231 |
final double value = n.doubleValue(); |
|
232 |
return JSType.isRepresentableAsInt(value) ? (int)value : -1; |
|
233 |
} |
|
234 |
||
24738
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
235 |
@SuppressWarnings("unused") |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
236 |
private static Object callToApply(final MethodHandle mh, final JSObject obj, final Object thiz, final Object... args) { |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
237 |
assert args.length >= 2; |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
238 |
final Object receiver = args[0]; |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
239 |
final Object[] arguments = new Object[args.length - 1]; |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
240 |
System.arraycopy(args, 1, arguments, 0, arguments.length); |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
241 |
try { |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
242 |
return mh.invokeExact(obj, thiz, new Object[] { receiver, arguments }); |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
243 |
} catch (final RuntimeException | Error e) { |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
244 |
throw e; |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
245 |
} catch (final Throwable e) { |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
246 |
throw new RuntimeException(e); |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
247 |
} |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
248 |
} |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
249 |
|
16147 | 250 |
private static final MethodHandleFunctionality MH = MethodHandleFactory.getFunctionality(); |
251 |
||
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
18614
diff
changeset
|
252 |
// method handles of the current class |
24719 | 253 |
private static final MethodHandle IS_JSOBJECT_GUARD = findOwnMH_S("isJSObject", boolean.class, Object.class); |
26237
b6509e37ce64
8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents:
25865
diff
changeset
|
254 |
private static final MethodHandle JSOBJECTLINKER_GET = findOwnMH_S("get", Object.class, MethodHandle.class, Object.class, Object.class); |
24719 | 255 |
private static final MethodHandle JSOBJECTLINKER_PUT = findOwnMH_S("put", Void.TYPE, Object.class, Object.class, Object.class); |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
18614
diff
changeset
|
256 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
18614
diff
changeset
|
257 |
// method handles of JSObject class |
24738
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
258 |
private static final MethodHandle JSOBJECT_GETMEMBER = findJSObjectMH_V("getMember", Object.class, String.class); |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
259 |
private static final MethodHandle JSOBJECT_SETMEMBER = findJSObjectMH_V("setMember", Void.TYPE, String.class, Object.class); |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
260 |
private static final MethodHandle JSOBJECT_CALL = findJSObjectMH_V("call", Object.class, Object.class, Object[].class); |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
261 |
private static final MethodHandle JSOBJECT_CALL_TO_APPLY = findOwnMH_S("callToApply", Object.class, MethodHandle.class, JSObject.class, Object.class, Object[].class); |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
262 |
private static final MethodHandle JSOBJECT_NEW = findJSObjectMH_V("newObject", Object.class, Object[].class); |
16147 | 263 |
|
21450
419e5d51f319
8027037: Make ScriptObjectMirror conversions work for any JSObject
attila
parents:
21449
diff
changeset
|
264 |
private static final Map<Class<?>, MethodHandle> CONVERTERS = new HashMap<>(); |
21449 | 265 |
static { |
24719 | 266 |
CONVERTERS.put(boolean.class, findOwnMH_S("toBoolean", boolean.class, JSObject.class)); |
267 |
CONVERTERS.put(int.class, findOwnMH_S("toInt32", int.class, JSObject.class)); |
|
268 |
CONVERTERS.put(long.class, findOwnMH_S("toLong", long.class, JSObject.class)); |
|
269 |
CONVERTERS.put(double.class, findOwnMH_S("toNumber", double.class, JSObject.class)); |
|
16147 | 270 |
} |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
18614
diff
changeset
|
271 |
|
24719 | 272 |
private static MethodHandle findJSObjectMH_V(final String name, final Class<?> rtype, final Class<?>... types) { |
273 |
return MH.findVirtual(MethodHandles.lookup(), JSObject.class, name, MH.type(rtype, types)); |
|
21449 | 274 |
} |
275 |
||
24719 | 276 |
private static MethodHandle findOwnMH_S(final String name, final Class<?> rtype, final Class<?>... types) { |
277 |
return MH.findStatic(MethodHandles.lookup(), JSObjectLinker.class, name, MH.type(rtype, types)); |
|
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
18614
diff
changeset
|
278 |
} |
16147 | 279 |
} |