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