author | sundar |
Mon, 11 Feb 2013 21:26:06 +0530 | |
changeset 16226 | 0e4f37e6cc40 |
parent 16195 | 3f6c0ab2597a |
child 16234 | 86cb162cec6c |
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.objects; |
|
27 |
||
28 |
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; |
|
29 |
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; |
|
30 |
import static jdk.nashorn.internal.runtime.linker.Lookup.MH; |
|
31 |
||
32 |
import java.lang.invoke.MethodHandle; |
|
33 |
import java.lang.invoke.MethodHandles; |
|
34 |
import java.lang.invoke.MethodType; |
|
35 |
import java.util.ArrayList; |
|
36 |
import java.util.Iterator; |
|
37 |
import java.util.List; |
|
38 |
import jdk.nashorn.internal.objects.annotations.Constructor; |
|
39 |
import jdk.nashorn.internal.objects.annotations.ScriptClass; |
|
40 |
import jdk.nashorn.internal.runtime.FindProperty; |
|
41 |
import jdk.nashorn.internal.runtime.JSType; |
|
42 |
import jdk.nashorn.internal.runtime.ScriptFunction; |
|
43 |
import jdk.nashorn.internal.runtime.ScriptObject; |
|
44 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
|
45 |
import jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator; |
|
46 |
import jdk.nashorn.internal.runtime.linker.Lookup; |
|
16181
f6a125580f62
8006562: findOwnMH in nashorn "objects" package should be cleaned up
sundar
parents:
16180
diff
changeset
|
47 |
import jdk.nashorn.internal.runtime.linker.MethodHandleFactory; |
16147 | 48 |
import org.dynalang.dynalink.CallSiteDescriptor; |
49 |
import org.dynalang.dynalink.linker.GuardedInvocation; |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16188
diff
changeset
|
50 |
import org.dynalang.dynalink.linker.LinkRequest; |
16147 | 51 |
|
52 |
/** |
|
53 |
* This class is the implementation of the Nashorn-specific global object named {@code JSAdapter}. It can be |
|
54 |
* thought of as the {@link java.lang.reflect.Proxy} equivalent for JavaScript. NativeJSAdapter calls specially named |
|
55 |
* JavaScript methods on an adaptee object when property access/update/call/new/delete is attempted on it. Example: |
|
56 |
*<pre> |
|
57 |
* var y = { |
|
58 |
* __get__ : function (name) { ... } |
|
59 |
* __has__ : function (name) { ... } |
|
60 |
* __put__ : function (name, value) {...} |
|
61 |
* __call__ : function (name, arg1, arg2) {...} |
|
62 |
* __new__ : function (arg1, arg2) {...} |
|
63 |
* __delete__ : function (name) { ... } |
|
64 |
* __getIds__ : function () { ... } |
|
65 |
* }; |
|
66 |
* |
|
67 |
* var x = new JSAdapter(y); |
|
68 |
* |
|
69 |
* x.i; // calls y.__get__ |
|
70 |
* x.foo(); // calls y.__call__ |
|
71 |
* new x(); // calls y.__new__ |
|
72 |
* i in x; // calls y.__has__ |
|
73 |
* x.p = 10; // calls y.__put__ |
|
74 |
* delete x.p; // calls y.__delete__ |
|
75 |
* for (i in x) { print(i); } // calls y.__getIds__ |
|
76 |
* </pre> |
|
77 |
* JavaScript caller of adapter object is isolated from the fact that the property access/mutation/deletion are really |
|
78 |
* calls to JavaScript methods on adaptee. |
|
79 |
* </p><p> |
|
80 |
* JSAdapter constructor can optionally receive an "overrides" object. Properties of overrides object is copied to |
|
81 |
* JSAdapter instance. When user accessed property is one of these, then adaptee's methods like {@code __get__}, |
|
82 |
* {@code __put__} etc. are not called for those. This can be used to make certain "preferred" properties that can be |
|
83 |
* accessed in the usual/faster way avoiding proxy mechanism. Example: |
|
84 |
* <pre> |
|
85 |
* var x = new JSAdapter({ foo: 444, bar: 6546 }) { |
|
86 |
* __get__: function(name) { return name; } |
|
87 |
* }; |
|
88 |
* |
|
89 |
* x.foo; // 444 directly retrieved without __get__ call |
|
90 |
* x.bar = 'hello'; // "bar" directly set without __put__ call |
|
91 |
* x.prop // calls __get__("prop") as 'prop' is not overridden |
|
92 |
* </pre> |
|
93 |
* It is possible to pass a specific prototype for JSAdapter instance by passing three arguments to JSAdapter |
|
94 |
* constructor. So exact signature of JSAdapter constructor is as follows: |
|
95 |
* <pre> |
|
96 |
* JSAdapter([proto], [overrides], adaptee); |
|
97 |
* </pre> |
|
98 |
* Both proto and overrides are optional - but adaptee is not. When proto is not passed {@code JSAdapter.prototype} is |
|
99 |
* used. |
|
100 |
*/ |
|
101 |
@ScriptClass("JSAdapter") |
|
102 |
public final class NativeJSAdapter extends ScriptObject { |
|
103 |
/** object get operation */ |
|
104 |
public static final String __get__ = "__get__"; |
|
105 |
/** object out operation */ |
|
106 |
public static final String __put__ = "__put__"; |
|
107 |
/** object call operation */ |
|
108 |
public static final String __call__ = "__call__"; |
|
109 |
/** object new operation */ |
|
110 |
public static final String __new__ = "__new__"; |
|
111 |
/** object getIds operation */ |
|
112 |
public static final String __getIds__ = "__getIds__"; |
|
113 |
/** object getKeys operation */ |
|
114 |
public static final String __getKeys__ = "__getKeys__"; |
|
115 |
/** object getValues operation */ |
|
116 |
public static final String __getValues__ = "__getValues__"; |
|
117 |
/** object has operation */ |
|
118 |
public static final String __has__ = "__has__"; |
|
119 |
/** object delete operation */ |
|
120 |
public static final String __delete__ = "__delete__"; |
|
121 |
||
122 |
// the new extensibility, sealing and freezing operations |
|
123 |
||
124 |
/** prevent extensions operation */ |
|
125 |
public static final String __preventExtensions__ = "__preventExtensions__"; |
|
126 |
/** isExtensible extensions operation */ |
|
127 |
public static final String __isExtensible__ = "__isExtensible__"; |
|
128 |
/** seal operation */ |
|
129 |
public static final String __seal__ = "__seal__"; |
|
130 |
/** isSealed extensions operation */ |
|
131 |
public static final String __isSealed__ = "__isSealed__"; |
|
132 |
/** freeze operation */ |
|
133 |
public static final String __freeze__ = "__freeze__"; |
|
134 |
/** isFrozen extensions operation */ |
|
135 |
public static final String __isFrozen__ = "__isFrozen__"; |
|
136 |
||
137 |
private final ScriptObject adaptee; |
|
138 |
private final boolean overrides; |
|
139 |
||
140 |
private static final MethodHandle IS_JSADAPTOR = findOwnMH("isJSAdaptor", boolean.class, Object.class, Object.class, MethodHandle.class, Object.class, ScriptFunction.class); |
|
141 |
||
142 |
NativeJSAdapter(final ScriptObject proto, final Object overrides, final ScriptObject adaptee) { |
|
143 |
this.adaptee = wrapAdaptee(adaptee); |
|
144 |
this.setProto(proto); |
|
145 |
if (overrides instanceof ScriptObject) { |
|
146 |
this.overrides = true; |
|
147 |
final ScriptObject sobj = (ScriptObject)overrides; |
|
148 |
final Iterator<String> iter = sobj.propertyIterator(); |
|
149 |
while (iter.hasNext()) { |
|
150 |
final String prop = iter.next(); |
|
151 |
super.set(prop, sobj.get(prop), false); |
|
152 |
} |
|
153 |
} else { |
|
154 |
this.overrides = false; |
|
155 |
} |
|
156 |
} |
|
157 |
||
158 |
private static ScriptObject wrapAdaptee(final ScriptObject adaptee) { |
|
159 |
final ScriptObject sobj = new jdk.nashorn.internal.scripts.JO$(); |
|
160 |
sobj.setProto(adaptee); |
|
161 |
return sobj; |
|
162 |
} |
|
163 |
||
164 |
@Override |
|
165 |
public String getClassName() { |
|
166 |
return "JSAdapter"; |
|
167 |
} |
|
168 |
||
169 |
@Override |
|
170 |
public int getInt(final Object key) { |
|
171 |
return (overrides && super.hasOwnProperty(key)) ? super.getInt(key) : callAdapteeInt(__get__, key); |
|
172 |
} |
|
173 |
||
174 |
@Override |
|
175 |
public int getInt(final double key) { |
|
176 |
return (overrides && super.hasOwnProperty(key)) ? super.getInt(key) : callAdapteeInt(__get__, key); |
|
177 |
} |
|
178 |
||
179 |
@Override |
|
180 |
public int getInt(final long key) { |
|
181 |
return (overrides && super.hasOwnProperty(key)) ? super.getInt(key) : callAdapteeInt(__get__, key); |
|
182 |
} |
|
183 |
||
184 |
@Override |
|
185 |
public int getInt(final int key) { |
|
186 |
return (overrides && super.hasOwnProperty(key)) ? super.getInt(key) : callAdapteeInt(__get__, key); |
|
187 |
} |
|
188 |
||
189 |
@Override |
|
190 |
public long getLong(final Object key) { |
|
191 |
return (overrides && super.hasOwnProperty(key)) ? super.getLong(key) : callAdapteeLong(__get__, key); |
|
192 |
} |
|
193 |
||
194 |
@Override |
|
195 |
public long getLong(final double key) { |
|
196 |
return (overrides && super.hasOwnProperty(key)) ? super.getLong(key) : callAdapteeLong(__get__, key); |
|
197 |
} |
|
198 |
||
199 |
@Override |
|
200 |
public long getLong(final long key) { |
|
201 |
return (overrides && super.hasOwnProperty(key)) ? super.getLong(key) : callAdapteeLong(__get__, key); |
|
202 |
} |
|
203 |
||
204 |
@Override |
|
205 |
public long getLong(final int key) { |
|
206 |
return (overrides && super.hasOwnProperty(key)) ? super.getLong(key) : callAdapteeLong(__get__, key); |
|
207 |
} |
|
208 |
||
209 |
@Override |
|
210 |
public double getDouble(final Object key) { |
|
211 |
return (overrides && super.hasOwnProperty(key)) ? super.getDouble(key) : callAdapteeDouble(__get__, key); |
|
212 |
} |
|
213 |
||
214 |
@Override |
|
215 |
public double getDouble(final double key) { |
|
216 |
return (overrides && super.hasOwnProperty(key)) ? super.getDouble(key) : callAdapteeDouble(__get__, key); |
|
217 |
} |
|
218 |
||
219 |
@Override |
|
220 |
public double getDouble(final long key) { |
|
221 |
return (overrides && super.hasOwnProperty(key)) ? super.getDouble(key) : callAdapteeDouble(__get__, key); |
|
222 |
} |
|
223 |
||
224 |
@Override |
|
225 |
public double getDouble(final int key) { |
|
226 |
return (overrides && super.hasOwnProperty(key)) ? super.getDouble(key) : callAdapteeDouble(__get__, key); |
|
227 |
} |
|
228 |
||
229 |
@Override |
|
230 |
public Object get(final Object key) { |
|
231 |
return (overrides && super.hasOwnProperty(key)) ? super.get(key) : callAdaptee(__get__, key); |
|
232 |
} |
|
233 |
||
234 |
@Override |
|
235 |
public Object get(final double key) { |
|
236 |
return (overrides && super.hasOwnProperty(key)) ? super.get(key) : callAdaptee(__get__, key); |
|
237 |
} |
|
238 |
||
239 |
@Override |
|
240 |
public Object get(final long key) { |
|
241 |
return (overrides && super.hasOwnProperty(key)) ? super.get(key) : callAdaptee(__get__, key); |
|
242 |
} |
|
243 |
||
244 |
@Override |
|
245 |
public Object get(final int key) { |
|
246 |
return (overrides && super.hasOwnProperty(key)) ? super.get(key) : callAdaptee(__get__, key); |
|
247 |
} |
|
248 |
||
249 |
@Override |
|
250 |
public void set(final Object key, final int value, final boolean strict) { |
|
251 |
if (overrides && super.hasOwnProperty(key)) { |
|
252 |
super.set(key, value, strict); |
|
253 |
} else { |
|
254 |
callAdaptee(__put__, key, value, strict); |
|
255 |
} |
|
256 |
} |
|
257 |
||
258 |
@Override |
|
259 |
public void set(final Object key, final long value, final boolean strict) { |
|
260 |
if (overrides && super.hasOwnProperty(key)) { |
|
261 |
super.set(key, value, strict); |
|
262 |
} else { |
|
263 |
callAdaptee(__put__, key, value, strict); |
|
264 |
} |
|
265 |
} |
|
266 |
||
267 |
@Override |
|
268 |
public void set(final Object key, final double value, final boolean strict) { |
|
269 |
if (overrides && super.hasOwnProperty(key)) { |
|
270 |
super.set(key, value, strict); |
|
271 |
} else { |
|
272 |
callAdaptee(__put__, key, value, strict); |
|
273 |
} |
|
274 |
} |
|
275 |
||
276 |
@Override |
|
277 |
public void set(final Object key, final Object value, final boolean strict) { |
|
278 |
if (overrides && super.hasOwnProperty(key)) { |
|
279 |
super.set(key, value, strict); |
|
280 |
} else { |
|
281 |
callAdaptee(__put__, key, value, strict); |
|
282 |
} |
|
283 |
} |
|
284 |
||
285 |
@Override |
|
286 |
public void set(final double key, final int value, final boolean strict) { |
|
287 |
if (overrides && super.hasOwnProperty(key)) { |
|
288 |
super.set(key, value, strict); |
|
289 |
} else { |
|
290 |
callAdaptee(__put__, key, value, strict); |
|
291 |
} |
|
292 |
} |
|
293 |
||
294 |
@Override |
|
295 |
public void set(final double key, final long value, final boolean strict) { |
|
296 |
if (overrides && super.hasOwnProperty(key)) { |
|
297 |
super.set(key, value, strict); |
|
298 |
} else { |
|
299 |
callAdaptee(__put__, key, value, strict); |
|
300 |
} |
|
301 |
} |
|
302 |
||
303 |
@Override |
|
304 |
public void set(final double key, final double value, final boolean strict) { |
|
305 |
if (overrides && super.hasOwnProperty(key)) { |
|
306 |
super.set(key, value, strict); |
|
307 |
} else { |
|
308 |
callAdaptee(__put__, key, value, strict); |
|
309 |
} |
|
310 |
} |
|
311 |
||
312 |
@Override |
|
313 |
public void set(final double key, final Object value, final boolean strict) { |
|
314 |
if (overrides && super.hasOwnProperty(key)) { |
|
315 |
super.set(key, value, strict); |
|
316 |
} else { |
|
317 |
callAdaptee(__put__, key, value, strict); |
|
318 |
} |
|
319 |
} |
|
320 |
||
321 |
@Override |
|
322 |
public void set(final long key, final int value, final boolean strict) { |
|
323 |
if (overrides && super.hasOwnProperty(key)) { |
|
324 |
super.set(key, value, strict); |
|
325 |
} else { |
|
326 |
callAdaptee(__put__, key, value, strict); |
|
327 |
} |
|
328 |
} |
|
329 |
||
330 |
@Override |
|
331 |
public void set(final long key, final long value, final boolean strict) { |
|
332 |
if (overrides && super.hasOwnProperty(key)) { |
|
333 |
super.set(key, value, strict); |
|
334 |
} else { |
|
335 |
callAdaptee(__put__, key, value, strict); |
|
336 |
} |
|
337 |
} |
|
338 |
||
339 |
@Override |
|
340 |
public void set(final long key, final double value, final boolean strict) { |
|
341 |
if (overrides && super.hasOwnProperty(key)) { |
|
342 |
super.set(key, value, strict); |
|
343 |
} else { |
|
344 |
callAdaptee(__put__, key, value, strict); |
|
345 |
} |
|
346 |
} |
|
347 |
||
348 |
@Override |
|
349 |
public void set(final long key, final Object value, final boolean strict) { |
|
350 |
if (overrides && super.hasOwnProperty(key)) { |
|
351 |
super.set(key, value, strict); |
|
352 |
} else { |
|
353 |
callAdaptee(__put__, key, value, strict); |
|
354 |
} |
|
355 |
} |
|
356 |
||
357 |
@Override |
|
358 |
public void set(final int key, final int value, final boolean strict) { |
|
359 |
if (overrides && super.hasOwnProperty(key)) { |
|
360 |
super.set(key, value, strict); |
|
361 |
} else { |
|
362 |
callAdaptee(__put__, key, value, strict); |
|
363 |
} |
|
364 |
} |
|
365 |
||
366 |
@Override |
|
367 |
public void set(final int key, final long value, final boolean strict) { |
|
368 |
if (overrides && super.hasOwnProperty(key)) { |
|
369 |
super.set(key, value, strict); |
|
370 |
} else { |
|
371 |
callAdaptee(__put__, key, value, strict); |
|
372 |
} |
|
373 |
} |
|
374 |
||
375 |
@Override |
|
376 |
public void set(final int key, final double value, final boolean strict) { |
|
377 |
if (overrides && super.hasOwnProperty(key)) { |
|
378 |
super.set(key, value, strict); |
|
379 |
} else { |
|
380 |
callAdaptee(__put__, key, value, strict); |
|
381 |
} |
|
382 |
} |
|
383 |
||
384 |
@Override |
|
385 |
public void set(final int key, final Object value, final boolean strict) { |
|
386 |
if (overrides && super.hasOwnProperty(key)) { |
|
387 |
super.set(key, value, strict); |
|
388 |
} else { |
|
389 |
callAdaptee(__put__, key, value, strict); |
|
390 |
} |
|
391 |
} |
|
392 |
||
393 |
@Override |
|
394 |
public boolean has(final Object key) { |
|
395 |
if (overrides && super.hasOwnProperty(key)) { |
|
396 |
return true; |
|
397 |
} |
|
398 |
||
399 |
return JSType.toBoolean(callAdaptee(Boolean.FALSE, __has__, key)); |
|
400 |
} |
|
401 |
||
402 |
@Override |
|
403 |
public boolean has(final int key) { |
|
404 |
if (overrides && super.hasOwnProperty(key)) { |
|
405 |
return true; |
|
406 |
} |
|
407 |
||
408 |
return JSType.toBoolean(callAdaptee(Boolean.FALSE, __has__, key)); |
|
409 |
} |
|
410 |
||
411 |
@Override |
|
412 |
public boolean has(final long key) { |
|
413 |
if (overrides && super.hasOwnProperty(key)) { |
|
414 |
return true; |
|
415 |
} |
|
416 |
||
417 |
return JSType.toBoolean(callAdaptee(Boolean.FALSE, __has__, key)); |
|
418 |
} |
|
419 |
||
420 |
@Override |
|
421 |
public boolean has(final double key) { |
|
422 |
if (overrides && super.hasOwnProperty(key)) { |
|
423 |
return true; |
|
424 |
} |
|
425 |
||
426 |
return JSType.toBoolean(callAdaptee(Boolean.FALSE, __has__, key)); |
|
427 |
} |
|
428 |
||
429 |
@Override |
|
430 |
public boolean delete(final int key, final boolean strict) { |
|
431 |
if (overrides && super.hasOwnProperty(key)) { |
|
432 |
return super.delete(key, strict); |
|
433 |
} |
|
434 |
||
435 |
return JSType.toBoolean(callAdaptee(Boolean.TRUE, __delete__, key, strict)); |
|
436 |
} |
|
437 |
||
438 |
@Override |
|
439 |
public boolean delete(final long key, final boolean strict) { |
|
440 |
if (overrides && super.hasOwnProperty(key)) { |
|
441 |
return super.delete(key, strict); |
|
442 |
} |
|
443 |
||
444 |
return JSType.toBoolean(callAdaptee(Boolean.TRUE, __delete__, key, strict)); |
|
445 |
} |
|
446 |
||
447 |
@Override |
|
448 |
public boolean delete(final double key, final boolean strict) { |
|
449 |
if (overrides && super.hasOwnProperty(key)) { |
|
450 |
return super.delete(key, strict); |
|
451 |
} |
|
452 |
||
453 |
return JSType.toBoolean(callAdaptee(Boolean.TRUE, __delete__, key, strict)); |
|
454 |
} |
|
455 |
||
456 |
@Override |
|
457 |
public boolean delete(final Object key, final boolean strict) { |
|
458 |
if (overrides && super.hasOwnProperty(key)) { |
|
459 |
return super.delete(key, strict); |
|
460 |
} |
|
461 |
||
462 |
return JSType.toBoolean(callAdaptee(Boolean.TRUE, __delete__, key, strict)); |
|
463 |
} |
|
464 |
||
465 |
@Override |
|
466 |
public Iterator<String> propertyIterator() { |
|
467 |
// Try __getIds__ first, if not found then try __getKeys__ |
|
468 |
// In jdk6, we had added "__getIds__" so this is just for compatibility. |
|
469 |
Object func = adaptee.get(__getIds__); |
|
470 |
if (!(func instanceof ScriptFunction)) { |
|
471 |
func = adaptee.get(__getKeys__); |
|
472 |
} |
|
473 |
||
474 |
Object obj; |
|
475 |
if (func instanceof ScriptFunction) { |
|
476 |
obj = ScriptRuntime.apply((ScriptFunction)func, adaptee); |
|
477 |
} else { |
|
478 |
obj = new NativeArray(0); |
|
479 |
} |
|
480 |
||
481 |
final List<String> array = new ArrayList<>(); |
|
482 |
for (final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(obj); iter.hasNext(); ) { |
|
483 |
array.add((String)iter.next()); |
|
484 |
} |
|
485 |
||
486 |
return array.iterator(); |
|
487 |
} |
|
488 |
||
489 |
||
490 |
@Override |
|
491 |
public Iterator<Object> valueIterator() { |
|
492 |
final Object obj = callAdaptee(new NativeArray(0), __getValues__); |
|
493 |
return ArrayLikeIterator.arrayLikeIterator(obj); |
|
494 |
} |
|
495 |
||
496 |
@Override |
|
497 |
public ScriptObject preventExtensions() { |
|
498 |
callAdaptee(__preventExtensions__); |
|
499 |
return this; |
|
500 |
} |
|
501 |
||
502 |
@Override |
|
503 |
public boolean isExtensible() { |
|
504 |
return JSType.toBoolean(callAdaptee(Boolean.TRUE, __isExtensible__)); |
|
505 |
} |
|
506 |
||
507 |
@Override |
|
508 |
public ScriptObject seal() { |
|
509 |
callAdaptee(__seal__); |
|
510 |
return this; |
|
511 |
} |
|
512 |
||
513 |
@Override |
|
514 |
public boolean isSealed() { |
|
515 |
return JSType.toBoolean(callAdaptee(Boolean.FALSE, __isSealed__)); |
|
516 |
} |
|
517 |
||
518 |
@Override |
|
519 |
public ScriptObject freeze() { |
|
520 |
callAdaptee(__freeze__); |
|
521 |
return this; |
|
522 |
} |
|
523 |
||
524 |
@Override |
|
525 |
public boolean isFrozen() { |
|
526 |
return JSType.toBoolean(callAdaptee(Boolean.FALSE, __isFrozen__)); |
|
527 |
} |
|
528 |
||
529 |
/** |
|
530 |
* Constructor |
|
531 |
* |
|
532 |
* @param isNew is this NativeJSAdapter instantiated with the new operator |
|
533 |
* @param self self reference |
|
534 |
* @param args arguments ([adaptee], [overrides, adaptee] or [proto, overrides, adaptee] |
|
535 |
* @return new NativeJSAdapter |
|
536 |
*/ |
|
537 |
@Constructor |
|
538 |
public static Object construct(final boolean isNew, final Object self, final Object... args) { |
|
539 |
Object proto = UNDEFINED; |
|
540 |
Object overrides = UNDEFINED; |
|
541 |
Object adaptee; |
|
542 |
||
543 |
if (args == null || args.length == 0) { |
|
16188 | 544 |
typeError("not.an.object", "null"); |
16147 | 545 |
return null; //won't reach, but fixed warning |
546 |
} |
|
547 |
||
548 |
switch (args.length) { |
|
549 |
case 1: |
|
550 |
adaptee = args[0]; |
|
551 |
break; |
|
552 |
||
553 |
case 2: |
|
554 |
overrides = args[0]; |
|
555 |
adaptee = args[1]; |
|
556 |
break; |
|
557 |
||
558 |
default: |
|
559 |
//fallthru |
|
560 |
case 3: |
|
561 |
proto = args[0]; |
|
562 |
overrides = args[1]; |
|
563 |
adaptee = args[2]; |
|
564 |
break; |
|
565 |
} |
|
566 |
||
567 |
if (!(adaptee instanceof ScriptObject)) { |
|
16188 | 568 |
typeError("not.an.object", ScriptRuntime.safeToString(adaptee)); |
16147 | 569 |
} |
570 |
||
571 |
if (proto != null && !(proto instanceof ScriptObject)) { |
|
572 |
proto = Global.instance().getJSAdapterPrototype(); |
|
573 |
} |
|
574 |
||
575 |
return new NativeJSAdapter((ScriptObject)proto, overrides, (ScriptObject)adaptee); |
|
576 |
} |
|
577 |
||
578 |
@Override |
|
579 |
protected GuardedInvocation findNewMethod(final CallSiteDescriptor desc) { |
|
580 |
return findHook(desc, __new__, false); |
|
581 |
} |
|
582 |
||
583 |
@Override |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16188
diff
changeset
|
584 |
protected GuardedInvocation findCallMethodMethod(final CallSiteDescriptor desc, final LinkRequest request) { |
16147 | 585 |
if (overrides && super.hasOwnProperty(desc.getNameToken(2))) { |
586 |
try { |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16188
diff
changeset
|
587 |
final GuardedInvocation inv = super.findCallMethodMethod(desc, request); |
16147 | 588 |
if (inv != null) { |
589 |
return inv; |
|
590 |
} |
|
591 |
} catch (final Exception e) { |
|
592 |
//ignored |
|
593 |
} |
|
594 |
} |
|
595 |
||
596 |
return findHook(desc, __call__); |
|
597 |
} |
|
598 |
||
599 |
@Override |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16188
diff
changeset
|
600 |
protected GuardedInvocation findGetMethod(final CallSiteDescriptor desc, final LinkRequest request, final String operation) { |
16147 | 601 |
final String name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND); |
602 |
if (overrides && super.hasOwnProperty(name)) { |
|
603 |
try { |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16188
diff
changeset
|
604 |
final GuardedInvocation inv = super.findGetMethod(desc, request, operation); |
16147 | 605 |
if (inv != null) { |
606 |
return inv; |
|
607 |
} |
|
608 |
} catch (final Exception e) { |
|
609 |
//ignored |
|
610 |
} |
|
611 |
} |
|
612 |
||
613 |
switch(operation) { |
|
614 |
case "getProp": |
|
615 |
case "getElem": |
|
616 |
return findHook(desc, __get__); |
|
617 |
case "getMethod": |
|
618 |
final FindProperty find = adaptee.findProperty(__call__, true); |
|
619 |
if (find != null) { |
|
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16195
diff
changeset
|
620 |
final ScriptFunctionImpl func = (ScriptFunctionImpl)getObjectValue(find); |
16147 | 621 |
// TODO: It's a shame we need to produce a function bound to this and name, when we'd only need it bound |
622 |
// to name. Probably not a big deal, but if we can ever make it leaner, it'd be nice. |
|
623 |
return new GuardedInvocation(MH.dropArguments(MH.constant(Object.class, |
|
624 |
func.makeBoundFunction(this, new Object[] { name })), 0, Object.class), |
|
625 |
adaptee.getMap().getProtoGetSwitchPoint(__call__), testJSAdaptor(adaptee, null, null, null)); |
|
626 |
} |
|
16188 | 627 |
typeError("no.such.function", desc.getNameToken(2), ScriptRuntime.safeToString(this)); |
16147 | 628 |
break; |
629 |
default: |
|
630 |
break; |
|
631 |
} |
|
632 |
||
633 |
throw new AssertionError("should not reach here"); |
|
634 |
} |
|
635 |
||
636 |
@Override |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16188
diff
changeset
|
637 |
protected GuardedInvocation findSetMethod(final CallSiteDescriptor desc, final LinkRequest request) { |
16147 | 638 |
if (overrides && super.hasOwnProperty(desc.getNameToken(CallSiteDescriptor.NAME_OPERAND))) { |
639 |
try { |
|
16195
3f6c0ab2597a
8006766: Array-like access to characters of a string is slow
hannesw
parents:
16188
diff
changeset
|
640 |
final GuardedInvocation inv = super.findSetMethod(desc, request); |
16147 | 641 |
if (inv != null) { |
642 |
return inv; |
|
643 |
} |
|
644 |
} catch (final Exception e) { |
|
645 |
//ignored |
|
646 |
} |
|
647 |
} |
|
648 |
||
649 |
return findHook(desc, __put__); |
|
650 |
} |
|
651 |
||
652 |
// -- Internals only below this point |
|
653 |
private Object callAdaptee(final String name, final Object... args) { |
|
654 |
return callAdaptee(UNDEFINED, name, args); |
|
655 |
} |
|
656 |
||
657 |
private double callAdapteeDouble(final String name, final Object... args) { |
|
658 |
return JSType.toNumber(callAdaptee(name, args)); |
|
659 |
} |
|
660 |
||
661 |
private long callAdapteeLong(final String name, final Object... args) { |
|
662 |
return JSType.toLong(callAdaptee(name, args)); |
|
663 |
} |
|
664 |
||
665 |
private int callAdapteeInt(final String name, final Object... args) { |
|
666 |
return JSType.toInt32(callAdaptee(name, args)); |
|
667 |
} |
|
668 |
||
669 |
private Object callAdaptee(final Object retValue, final String name, final Object... args) { |
|
670 |
final Object func = adaptee.get(name); |
|
671 |
if (func instanceof ScriptFunction) { |
|
672 |
return ScriptRuntime.apply((ScriptFunction)func, adaptee, args); |
|
673 |
} |
|
674 |
return retValue; |
|
675 |
} |
|
676 |
||
677 |
private GuardedInvocation findHook(final CallSiteDescriptor desc, final String hook) { |
|
678 |
return findHook(desc, hook, true); |
|
679 |
} |
|
680 |
||
681 |
private GuardedInvocation findHook(final CallSiteDescriptor desc, final String hook, final boolean useName) { |
|
682 |
final FindProperty findData = adaptee.findProperty(hook, true); |
|
683 |
final MethodType type = desc.getMethodType(); |
|
684 |
if (findData != null) { |
|
685 |
final String name = desc.getNameTokenCount() > 2 ? desc.getNameToken(2) : null; |
|
686 |
final ScriptFunction func = (ScriptFunction)getObjectValue(findData); |
|
687 |
||
688 |
final MethodHandle methodHandle = getCallMethodHandle(findData, type, |
|
689 |
useName ? name : null); |
|
690 |
if (methodHandle != null) { |
|
691 |
return new GuardedInvocation( |
|
692 |
methodHandle, |
|
693 |
adaptee.getMap().getProtoGetSwitchPoint(hook), |
|
694 |
testJSAdaptor(adaptee, findData.getGetter(Object.class), findData.getOwner(), func)); |
|
695 |
} |
|
696 |
} |
|
697 |
||
698 |
switch (hook) { |
|
699 |
case __call__: |
|
16188 | 700 |
typeError("no.such.function", desc.getNameToken(2), ScriptRuntime.safeToString(this)); |
16147 | 701 |
throw new AssertionError("should not reach here"); |
702 |
default: |
|
703 |
final MethodHandle methodHandle = hook.equals(__put__) ? |
|
704 |
MH.asType(Lookup.EMPTY_SETTER, type) : |
|
705 |
Lookup.emptyGetter(type.returnType()); |
|
706 |
return new GuardedInvocation(methodHandle, adaptee.getMap().getProtoGetSwitchPoint(hook), testJSAdaptor(adaptee, null, null, null)); |
|
707 |
} |
|
708 |
} |
|
709 |
||
710 |
private static MethodHandle testJSAdaptor(final Object adaptee, final MethodHandle getter, final Object where, final ScriptFunction func) { |
|
711 |
return MH.insertArguments(IS_JSADAPTOR, 1, adaptee, getter, where, func); |
|
712 |
} |
|
713 |
||
714 |
@SuppressWarnings("unused") |
|
715 |
private static boolean isJSAdaptor(final Object self, final Object adaptee, final MethodHandle getter, final Object where, final ScriptFunction func) { |
|
716 |
final boolean res = self instanceof NativeJSAdapter && ((NativeJSAdapter)self).getAdaptee() == adaptee; |
|
717 |
if (res && getter != null) { |
|
718 |
try { |
|
719 |
return getter.invokeExact(where) == func; |
|
720 |
} catch (final RuntimeException | Error e) { |
|
721 |
throw e; |
|
722 |
} catch (final Throwable t) { |
|
723 |
throw new RuntimeException(t); |
|
724 |
} |
|
725 |
} |
|
726 |
||
727 |
return res; |
|
728 |
} |
|
729 |
||
730 |
/** |
|
731 |
* Get the adaptee |
|
732 |
* @return adaptee ScriptObject |
|
733 |
*/ |
|
734 |
public ScriptObject getAdaptee() { |
|
735 |
return adaptee; |
|
736 |
} |
|
737 |
||
738 |
private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) { |
|
16180
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16151
diff
changeset
|
739 |
try { |
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16151
diff
changeset
|
740 |
return MethodHandles.lookup().findStatic(NativeJSAdapter.class, name, MH.type(rtype, types)); |
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16151
diff
changeset
|
741 |
} catch (final NoSuchMethodException | IllegalAccessException e) { |
16181
f6a125580f62
8006562: findOwnMH in nashorn "objects" package should be cleaned up
sundar
parents:
16180
diff
changeset
|
742 |
throw new MethodHandleFactory.LookupException(e); |
16180
374e36bd1357
8006527: nashorn jsr223 engine does not work in sandbox
sundar
parents:
16151
diff
changeset
|
743 |
} |
16147 | 744 |
} |
745 |
} |