98 public static final Call TO_PROPERTY_ITERATOR = staticCallNoLookup(ScriptRuntime.class, "toPropertyIterator", Iterator.class, Object.class); 99 100 /** 101 * Return an appropriate iterator for the elements in a for-each construct 102 */ 103 public static final Call TO_VALUE_ITERATOR = staticCallNoLookup(ScriptRuntime.class, "toValueIterator", Iterator.class, Object.class); 104 105 /** 106 * Method handle for apply. Used from {@link ScriptFunction} for looking up calls to 107 * call sites that are known to be megamorphic. Using an invoke dynamic here would 108 * lead to the JVM deoptimizing itself to death 109 */ 110 public static final Call APPLY = staticCall(MethodHandles.lookup(), ScriptRuntime.class, "apply", Object.class, ScriptFunction.class, Object.class, Object[].class); 111 112 /** 113 * Throws a reference error for an undefined variable. 114 */ 115 public static final Call THROW_REFERENCE_ERROR = staticCall(MethodHandles.lookup(), ScriptRuntime.class, "throwReferenceError", void.class, String.class); 116 117 /** 118 * Used to invalidate builtin names, e.g "Function" mapping to all properties in Function.prototype and Function.prototype itself. 119 */ 120 public static final Call INVALIDATE_RESERVED_BUILTIN_NAME = staticCallNoLookup(ScriptRuntime.class, "invalidateReservedBuiltinName", void.class, String.class); 121 122 /** 123 * Converts a switch tag value to a simple integer. deflt value if it can't. 124 * 125 * @param tag Switch statement tag value. 126 * @param deflt default to use if not convertible. 127 * @return int tag value (or deflt.) 128 */ 129 public static int switchTagAsInt(final Object tag, final int deflt) { 130 if (tag instanceof Number) { 131 final double d = ((Number)tag).doubleValue(); 132 if (isRepresentableAsInt(d)) { 133 return (int)d; 134 } 135 } 136 return deflt; 137 } 383 * @param args Call arguments. 384 * @return Call result. 385 */ 386 public static Object apply(final ScriptFunction target, final Object self, final Object... args) { 387 try { 388 return target.invoke(self, args); 389 } catch (final RuntimeException | Error e) { 390 throw e; 391 } catch (final Throwable t) { 392 throw new RuntimeException(t); 393 } 394 } 395 396 /** 397 * Throws a reference error for an undefined variable. 398 * 399 * @param name the variable name 400 */ 401 public static void throwReferenceError(final String name) { 402 throw referenceError("not.defined", name); 403 } 404 405 /** 406 * Call a script function as a constructor with given args. 407 * 408 * @param target ScriptFunction object. 409 * @param args Call arguments. 410 * @return Constructor call result. 411 */ 412 public static Object construct(final ScriptFunction target, final Object... args) { 413 try { 414 return target.construct(args); 415 } catch (final RuntimeException | Error e) { 416 throw e; 417 } catch (final Throwable t) { 418 throw new RuntimeException(t); 419 } 420 } 421 422 /** | 98 public static final Call TO_PROPERTY_ITERATOR = staticCallNoLookup(ScriptRuntime.class, "toPropertyIterator", Iterator.class, Object.class); 99 100 /** 101 * Return an appropriate iterator for the elements in a for-each construct 102 */ 103 public static final Call TO_VALUE_ITERATOR = staticCallNoLookup(ScriptRuntime.class, "toValueIterator", Iterator.class, Object.class); 104 105 /** 106 * Method handle for apply. Used from {@link ScriptFunction} for looking up calls to 107 * call sites that are known to be megamorphic. Using an invoke dynamic here would 108 * lead to the JVM deoptimizing itself to death 109 */ 110 public static final Call APPLY = staticCall(MethodHandles.lookup(), ScriptRuntime.class, "apply", Object.class, ScriptFunction.class, Object.class, Object[].class); 111 112 /** 113 * Throws a reference error for an undefined variable. 114 */ 115 public static final Call THROW_REFERENCE_ERROR = staticCall(MethodHandles.lookup(), ScriptRuntime.class, "throwReferenceError", void.class, String.class); 116 117 /** 118 * Throws a reference error for an undefined variable. 119 */ 120 public static final Call THROW_CONST_TYPE_ERROR = staticCall(MethodHandles.lookup(), ScriptRuntime.class, "throwConstTypeError", void.class, String.class); 121 122 /** 123 * Used to invalidate builtin names, e.g "Function" mapping to all properties in Function.prototype and Function.prototype itself. 124 */ 125 public static final Call INVALIDATE_RESERVED_BUILTIN_NAME = staticCallNoLookup(ScriptRuntime.class, "invalidateReservedBuiltinName", void.class, String.class); 126 127 /** 128 * Converts a switch tag value to a simple integer. deflt value if it can't. 129 * 130 * @param tag Switch statement tag value. 131 * @param deflt default to use if not convertible. 132 * @return int tag value (or deflt.) 133 */ 134 public static int switchTagAsInt(final Object tag, final int deflt) { 135 if (tag instanceof Number) { 136 final double d = ((Number)tag).doubleValue(); 137 if (isRepresentableAsInt(d)) { 138 return (int)d; 139 } 140 } 141 return deflt; 142 } 388 * @param args Call arguments. 389 * @return Call result. 390 */ 391 public static Object apply(final ScriptFunction target, final Object self, final Object... args) { 392 try { 393 return target.invoke(self, args); 394 } catch (final RuntimeException | Error e) { 395 throw e; 396 } catch (final Throwable t) { 397 throw new RuntimeException(t); 398 } 399 } 400 401 /** 402 * Throws a reference error for an undefined variable. 403 * 404 * @param name the variable name 405 */ 406 public static void throwReferenceError(final String name) { 407 throw referenceError("not.defined", name); 408 } 409 410 /** 411 * Throws a type error for an assignment to a const. 412 * 413 * @param name the const name 414 */ 415 public static void throwConstTypeError(final String name) { 416 throw typeError("assign.constant", name); 417 } 418 419 /** 420 * Call a script function as a constructor with given args. 421 * 422 * @param target ScriptFunction object. 423 * @param args Call arguments. 424 * @return Constructor call result. 425 */ 426 public static Object construct(final ScriptFunction target, final Object... args) { 427 try { 428 return target.construct(args); 429 } catch (final RuntimeException | Error e) { 430 throw e; 431 } catch (final Throwable t) { 432 throw new RuntimeException(t); 433 } 434 } 435 436 /** |