author | attila |
Wed, 20 Dec 2017 17:36:50 +0100 | |
changeset 48411 | 4ff5c5206427 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
36024
1a7614804158
8148140: arguments are handled differently in apply for JS functions and AbstractJSObjects
mhaupt
parents:
34447
diff
changeset
|
2 |
* Copyright (c) 2010, 2016, 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 |
||
29282 | 28 |
import static jdk.nashorn.internal.runtime.JSType.isString; |
29 |
||
16147 | 30 |
import java.lang.invoke.MethodHandle; |
31 |
import java.lang.invoke.MethodHandles; |
|
36024
1a7614804158
8148140: arguments are handled differently in apply for JS functions and AbstractJSObjects
mhaupt
parents:
34447
diff
changeset
|
32 |
import java.lang.invoke.MethodType; |
21449 | 33 |
import java.util.Map; |
48411 | 34 |
import java.util.Objects; |
34447
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33343
diff
changeset
|
35 |
import jdk.dynalink.CallSiteDescriptor; |
41842
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
36 |
import jdk.dynalink.Operation; |
34447
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33343
diff
changeset
|
37 |
import jdk.dynalink.StandardOperation; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33343
diff
changeset
|
38 |
import jdk.dynalink.linker.GuardedInvocation; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33343
diff
changeset
|
39 |
import jdk.dynalink.linker.LinkRequest; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33343
diff
changeset
|
40 |
import jdk.dynalink.linker.LinkerServices; |
ec4c069f9436
8141338: Move jdk.internal.dynalink package to jdk.dynalink
attila
parents:
33343
diff
changeset
|
41 |
import jdk.dynalink.linker.TypeBasedGuardingDynamicLinker; |
21449 | 42 |
import jdk.nashorn.api.scripting.JSObject; |
42382
e56aaa43d9f4
8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents:
41842
diff
changeset
|
43 |
import jdk.nashorn.api.scripting.ScriptObjectMirror; |
21449 | 44 |
import jdk.nashorn.internal.lookup.MethodHandleFactory; |
45 |
import jdk.nashorn.internal.lookup.MethodHandleFunctionality; |
|
42382
e56aaa43d9f4
8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents:
41842
diff
changeset
|
46 |
import jdk.nashorn.internal.runtime.Context; |
16147 | 47 |
import jdk.nashorn.internal.runtime.JSType; |
42382
e56aaa43d9f4
8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents:
41842
diff
changeset
|
48 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
e56aaa43d9f4
8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents:
41842
diff
changeset
|
49 |
import jdk.nashorn.internal.objects.Global; |
16147 | 50 |
|
51 |
/** |
|
52 |
* A Dynalink linker to handle web browser built-in JS (DOM etc.) objects as well |
|
53 |
* as ScriptObjects from other Nashorn contexts. |
|
54 |
*/ |
|
29133
1cd7d8af99ba
8072426: Can't compare Java objects to strings or numbers
attila
parents:
28125
diff
changeset
|
55 |
final class JSObjectLinker implements TypeBasedGuardingDynamicLinker { |
24591
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
56 |
private final 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 |
JSObjectLinker(final NashornBeansLinker nashornBeansLinker) { |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
59 |
this.nashornBeansLinker = nashornBeansLinker; |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
60 |
} |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
61 |
|
16147 | 62 |
@Override |
63 |
public boolean canLinkType(final Class<?> type) { |
|
64 |
return canLinkTypeStatic(type); |
|
65 |
} |
|
66 |
||
48411 | 67 |
private static boolean canLinkTypeStatic(final Class<?> type) { |
68 |
// can link JSObject also handles Map (this includes Bindings) to make |
|
24591
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
69 |
// sure those are not JSObjects. |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
70 |
return Map.class.isAssignableFrom(type) || |
67e22e0e8473
8044000: Access to undefined property yields "null" instead of "undefined"
sundar
parents:
22669
diff
changeset
|
71 |
JSObject.class.isAssignableFrom(type); |
16147 | 72 |
} |
73 |
||
74 |
@Override |
|
75 |
public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices) throws Exception { |
|
33332
f180be6368d8
8139588: Remove concept of runtime context arguments, call site tokens, and link counts
attila
parents:
33330
diff
changeset
|
76 |
final Object self = request.getReceiver(); |
f180be6368d8
8139588: Remove concept of runtime context arguments, call site tokens, and link counts
attila
parents:
33330
diff
changeset
|
77 |
final CallSiteDescriptor desc = request.getCallSiteDescriptor(); |
37833
ae7b3314b209
8150731: Nashorn JSObject linker should be exposed as a service provider
sundar
parents:
36024
diff
changeset
|
78 |
if (self == null || !canLinkTypeStatic(self.getClass())) { |
ae7b3314b209
8150731: Nashorn JSObject linker should be exposed as a service provider
sundar
parents:
36024
diff
changeset
|
79 |
return null; |
ae7b3314b209
8150731: Nashorn JSObject linker should be exposed as a service provider
sundar
parents:
36024
diff
changeset
|
80 |
} |
16147 | 81 |
|
32889
aef39bbfac15
8137258: JSObjectLinker and BrowserJSObjectLinker should not expose internal JS objects
sundar
parents:
29282
diff
changeset
|
82 |
GuardedInvocation inv; |
16147 | 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); |
32889
aef39bbfac15
8137258: JSObjectLinker and BrowserJSObjectLinker should not expose internal JS objects
sundar
parents:
29282
diff
changeset
|
85 |
inv = inv.replaceMethods(linkerServices.filterInternalObjects(inv.getInvocation()), inv.getGuard()); |
48411 | 86 |
} else if (self instanceof Map) { |
24591
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 { |
37833
ae7b3314b209
8150731: Nashorn JSObject linker should be exposed as a service provider
sundar
parents:
36024
diff
changeset
|
92 |
throw new AssertionError("got instanceof: " + self.getClass()); // Should never reach here. |
16147 | 93 |
} |
94 |
||
24719 | 95 |
return Bootstrap.asTypeSafeReturn(inv, linkerServices, desc); |
16147 | 96 |
} |
97 |
||
26237
b6509e37ce64
8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents:
25865
diff
changeset
|
98 |
private GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request, final LinkerServices linkerServices) throws Exception { |
41842
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
99 |
final Operation op = NashornCallSiteDescriptor.getBaseOperation(desc); |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
100 |
if (op instanceof StandardOperation) { |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
101 |
final String name = NashornCallSiteDescriptor.getOperand(desc); |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
102 |
switch ((StandardOperation)op) { |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
103 |
case GET: |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
104 |
if (NashornCallSiteDescriptor.hasStandardNamespace(desc)) { |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
105 |
if (name != null) { |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
106 |
return findGetMethod(name); |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
107 |
} |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
108 |
// For indexed get, we want get GuardedInvocation beans linker and pass it. |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
109 |
// JSObjectLinker.get uses this fallback getter for explicit signature method access. |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
110 |
return findGetIndexMethod(nashornBeansLinker.getGuardedInvocation(request, linkerServices)); |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
111 |
} |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
112 |
break; |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
113 |
case SET: |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
114 |
if (NashornCallSiteDescriptor.hasStandardNamespace(desc)) { |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
115 |
return name != null ? findSetMethod(name) : findSetIndexMethod(); |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
116 |
} |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
117 |
break; |
48411 | 118 |
case REMOVE: |
119 |
if (NashornCallSiteDescriptor.hasStandardNamespace(desc)) { |
|
120 |
return new GuardedInvocation( |
|
121 |
name == null ? JSOBJECTLINKER_DEL : MH.insertArguments(JSOBJECTLINKER_DEL, 1, name), |
|
122 |
IS_JSOBJECT_GUARD); |
|
123 |
} |
|
124 |
break; |
|
41842
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
125 |
case CALL: |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
126 |
return findCallMethod(desc); |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
127 |
case NEW: |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
128 |
return findNewMethod(desc); |
50202a344d28
8168005: Introduce namespaces for GET, SET Dynalink operations
attila
parents:
37833
diff
changeset
|
129 |
default: |
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33332
diff
changeset
|
130 |
} |
16147 | 131 |
} |
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33332
diff
changeset
|
132 |
return null; |
16147 | 133 |
} |
134 |
||
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33332
diff
changeset
|
135 |
private static GuardedInvocation findGetMethod(final String name) { |
24738
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
136 |
final MethodHandle getter = MH.insertArguments(JSOBJECT_GETMEMBER, 1, name); |
24719 | 137 |
return new GuardedInvocation(getter, IS_JSOBJECT_GUARD); |
16147 | 138 |
} |
139 |
||
26237
b6509e37ce64
8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents:
25865
diff
changeset
|
140 |
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
|
141 |
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
|
142 |
return inv.replaceMethods(getter, inv.getGuard()); |
16147 | 143 |
} |
144 |
||
33343
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33332
diff
changeset
|
145 |
private static GuardedInvocation findSetMethod(final String name) { |
23abd10384a5
8139931: Introduce Operation objects in Dynalink instead of string encoding
attila
parents:
33332
diff
changeset
|
146 |
final MethodHandle getter = MH.insertArguments(JSOBJECT_SETMEMBER, 1, name); |
24719 | 147 |
return new GuardedInvocation(getter, IS_JSOBJECT_GUARD); |
16147 | 148 |
} |
149 |
||
150 |
private static GuardedInvocation findSetIndexMethod() { |
|
24719 | 151 |
return new GuardedInvocation(JSOBJECTLINKER_PUT, IS_JSOBJECT_GUARD); |
16147 | 152 |
} |
153 |
||
21449 | 154 |
private static GuardedInvocation findCallMethod(final CallSiteDescriptor desc) { |
42382
e56aaa43d9f4
8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents:
41842
diff
changeset
|
155 |
MethodHandle mh = NashornCallSiteDescriptor.isScope(desc)? JSOBJECT_SCOPE_CALL : JSOBJECT_CALL; |
24738
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
156 |
if (NashornCallSiteDescriptor.isApplyToCall(desc)) { |
42382
e56aaa43d9f4
8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents:
41842
diff
changeset
|
157 |
mh = MH.insertArguments(JSOBJECT_CALL_TO_APPLY, 0, mh); |
24738
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
158 |
} |
36024
1a7614804158
8148140: arguments are handled differently in apply for JS functions and AbstractJSObjects
mhaupt
parents:
34447
diff
changeset
|
159 |
final MethodType type = desc.getMethodType(); |
1a7614804158
8148140: arguments are handled differently in apply for JS functions and AbstractJSObjects
mhaupt
parents:
34447
diff
changeset
|
160 |
mh = type.parameterType(type.parameterCount() - 1) == Object[].class ? |
1a7614804158
8148140: arguments are handled differently in apply for JS functions and AbstractJSObjects
mhaupt
parents:
34447
diff
changeset
|
161 |
mh : |
1a7614804158
8148140: arguments are handled differently in apply for JS functions and AbstractJSObjects
mhaupt
parents:
34447
diff
changeset
|
162 |
MH.asCollector(mh, Object[].class, type.parameterCount() - 2); |
1a7614804158
8148140: arguments are handled differently in apply for JS functions and AbstractJSObjects
mhaupt
parents:
34447
diff
changeset
|
163 |
return new GuardedInvocation(mh, IS_JSOBJECT_GUARD); |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
18614
diff
changeset
|
164 |
} |
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
18614
diff
changeset
|
165 |
|
18614 | 166 |
private static GuardedInvocation findNewMethod(final CallSiteDescriptor desc) { |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
18614
diff
changeset
|
167 |
final MethodHandle func = MH.asCollector(JSOBJECT_NEW, Object[].class, desc.getMethodType().parameterCount() - 1); |
24719 | 168 |
return new GuardedInvocation(func, IS_JSOBJECT_GUARD); |
16147 | 169 |
} |
170 |
||
171 |
@SuppressWarnings("unused") |
|
172 |
private static boolean isJSObject(final Object self) { |
|
173 |
return self instanceof JSObject; |
|
174 |
} |
|
175 |
||
176 |
@SuppressWarnings("unused") |
|
26237
b6509e37ce64
8055796: JSObject and browser JSObject linkers should provide fallback to call underlying Java methods directly
sundar
parents:
25865
diff
changeset
|
177 |
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
|
178 |
throws Throwable { |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
18614
diff
changeset
|
179 |
if (key instanceof Integer) { |
20928
3ff39d5c8c08
8026137: Fix Issues with Binary Evaluation Order
lagergren
parents:
19889
diff
changeset
|
180 |
return ((JSObject)jsobj).getSlot((Integer)key); |
16147 | 181 |
} else if (key instanceof Number) { |
182 |
final int index = getIndex((Number)key); |
|
183 |
if (index > -1) { |
|
184 |
return ((JSObject)jsobj).getSlot(index); |
|
44168
604022687602
8176511: JSObject property access is broken for numeric keys outside the int range
hannesw
parents:
42382
diff
changeset
|
185 |
} else { |
604022687602
8176511: JSObject property access is broken for numeric keys outside the int range
hannesw
parents:
42382
diff
changeset
|
186 |
return ((JSObject)jsobj).getMember(JSType.toString(key)); |
16147 | 187 |
} |
29282 | 188 |
} else if (isString(key)) { |
28125
118973efb1d3
8062030: Nashorn bug retrieving array property after key string concatenation
hannesw
parents:
27361
diff
changeset
|
189 |
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
|
190 |
// 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
|
191 |
if (name.indexOf('(') != -1) { |
28125
118973efb1d3
8062030: Nashorn bug retrieving array property after key string concatenation
hannesw
parents:
27361
diff
changeset
|
192 |
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
|
193 |
} |
27361 | 194 |
return ((JSObject)jsobj).getMember(name); |
16147 | 195 |
} |
196 |
return null; |
|
197 |
} |
|
198 |
||
199 |
@SuppressWarnings("unused") |
|
200 |
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
|
201 |
if (key instanceof Integer) { |
20928
3ff39d5c8c08
8026137: Fix Issues with Binary Evaluation Order
lagergren
parents:
19889
diff
changeset
|
202 |
((JSObject)jsobj).setSlot((Integer)key, value); |
16147 | 203 |
} else if (key instanceof Number) { |
44168
604022687602
8176511: JSObject property access is broken for numeric keys outside the int range
hannesw
parents:
42382
diff
changeset
|
204 |
final int index = getIndex((Number)key); |
604022687602
8176511: JSObject property access is broken for numeric keys outside the int range
hannesw
parents:
42382
diff
changeset
|
205 |
if (index > -1) { |
604022687602
8176511: JSObject property access is broken for numeric keys outside the int range
hannesw
parents:
42382
diff
changeset
|
206 |
((JSObject)jsobj).setSlot(index, value); |
604022687602
8176511: JSObject property access is broken for numeric keys outside the int range
hannesw
parents:
42382
diff
changeset
|
207 |
} else { |
604022687602
8176511: JSObject property access is broken for numeric keys outside the int range
hannesw
parents:
42382
diff
changeset
|
208 |
((JSObject)jsobj).setMember(JSType.toString(key), value); |
604022687602
8176511: JSObject property access is broken for numeric keys outside the int range
hannesw
parents:
42382
diff
changeset
|
209 |
} |
29282 | 210 |
} else if (isString(key)) { |
28125
118973efb1d3
8062030: Nashorn bug retrieving array property after key string concatenation
hannesw
parents:
27361
diff
changeset
|
211 |
((JSObject)jsobj).setMember(key.toString(), value); |
16147 | 212 |
} |
213 |
} |
|
214 |
||
48411 | 215 |
@SuppressWarnings("unused") |
216 |
private static boolean del(final Object jsobj, final Object key) { |
|
217 |
if (jsobj instanceof ScriptObjectMirror) { |
|
218 |
return ((ScriptObjectMirror)jsobj).delete(key); |
|
219 |
} |
|
220 |
((JSObject) jsobj).removeMember(Objects.toString(key)); |
|
221 |
return true; |
|
222 |
} |
|
223 |
||
16147 | 224 |
private static int getIndex(final Number n) { |
225 |
final double value = n.doubleValue(); |
|
226 |
return JSType.isRepresentableAsInt(value) ? (int)value : -1; |
|
227 |
} |
|
228 |
||
24738
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
229 |
@SuppressWarnings("unused") |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
230 |
private static Object callToApply(final MethodHandle mh, final JSObject obj, final Object thiz, final Object... args) { |
47037
07210623c057
8184723: jdk.nashorn.internal.runtime.linker.JSObjectLinker.callToApply erroneously asserts given arguments
hannesw
parents:
44168
diff
changeset
|
231 |
assert args.length >= 1; |
24738
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
232 |
final Object receiver = args[0]; |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
233 |
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
|
234 |
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
|
235 |
try { |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
236 |
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
|
237 |
} catch (final RuntimeException | Error e) { |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
238 |
throw e; |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
239 |
} catch (final Throwable e) { |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
240 |
throw new RuntimeException(e); |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
241 |
} |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
242 |
} |
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
243 |
|
42382
e56aaa43d9f4
8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents:
41842
diff
changeset
|
244 |
// This is used when a JSObject is called as scope call to do undefined -> Global this translation. |
e56aaa43d9f4
8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents:
41842
diff
changeset
|
245 |
@SuppressWarnings("unused") |
e56aaa43d9f4
8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents:
41842
diff
changeset
|
246 |
private static Object jsObjectScopeCall(final JSObject jsObj, final Object thiz, final Object[] args) { |
e56aaa43d9f4
8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents:
41842
diff
changeset
|
247 |
final Object modifiedThiz; |
e56aaa43d9f4
8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents:
41842
diff
changeset
|
248 |
if (thiz == ScriptRuntime.UNDEFINED && !jsObj.isStrictFunction()) { |
e56aaa43d9f4
8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents:
41842
diff
changeset
|
249 |
final Global global = Context.getGlobal(); |
e56aaa43d9f4
8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents:
41842
diff
changeset
|
250 |
modifiedThiz = ScriptObjectMirror.wrap(global, global); |
e56aaa43d9f4
8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents:
41842
diff
changeset
|
251 |
} else { |
e56aaa43d9f4
8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents:
41842
diff
changeset
|
252 |
modifiedThiz = thiz; |
e56aaa43d9f4
8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents:
41842
diff
changeset
|
253 |
} |
e56aaa43d9f4
8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents:
41842
diff
changeset
|
254 |
return jsObj.call(modifiedThiz, args); |
e56aaa43d9f4
8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents:
41842
diff
changeset
|
255 |
} |
e56aaa43d9f4
8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents:
41842
diff
changeset
|
256 |
|
16147 | 257 |
private static final MethodHandleFunctionality MH = MethodHandleFactory.getFunctionality(); |
258 |
||
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
18614
diff
changeset
|
259 |
// method handles of the current class |
24719 | 260 |
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
|
261 |
private static final MethodHandle JSOBJECTLINKER_GET = findOwnMH_S("get", Object.class, MethodHandle.class, Object.class, Object.class); |
24719 | 262 |
private static final MethodHandle JSOBJECTLINKER_PUT = findOwnMH_S("put", Void.TYPE, Object.class, Object.class, Object.class); |
48411 | 263 |
private static final MethodHandle JSOBJECTLINKER_DEL = findOwnMH_S("del", boolean.class, Object.class, Object.class); |
19889
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
18614
diff
changeset
|
264 |
|
63af9358d0dc
8024615: Refactor ScriptObjectMirror and JSObject to support external JSObject implementations
sundar
parents:
18614
diff
changeset
|
265 |
// method handles of JSObject class |
24738
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
266 |
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
|
267 |
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
|
268 |
private static final MethodHandle JSOBJECT_CALL = findJSObjectMH_V("call", Object.class, Object.class, Object[].class); |
42382
e56aaa43d9f4
8170565: JSObject call() is passed undefined for the argument 'thiz'
sundar
parents:
41842
diff
changeset
|
269 |
private static final MethodHandle JSOBJECT_SCOPE_CALL = findOwnMH_S("jsObjectScopeCall", Object.class, JSObject.class, Object.class, Object[].class); |
24738
be2026c9717c
8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents:
24719
diff
changeset
|
270 |
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
|
271 |
private static final MethodHandle JSOBJECT_NEW = findJSObjectMH_V("newObject", Object.class, Object[].class); |
16147 | 272 |
|
24719 | 273 |
private static MethodHandle findJSObjectMH_V(final String name, final Class<?> rtype, final Class<?>... types) { |
274 |
return MH.findVirtual(MethodHandles.lookup(), JSObject.class, name, MH.type(rtype, types)); |
|
21449 | 275 |
} |
276 |
||
24719 | 277 |
private static MethodHandle findOwnMH_S(final String name, final Class<?> rtype, final Class<?>... types) { |
278 |
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
|
279 |
} |
16147 | 280 |
} |