356 if (obj instanceof Iterable) { 357 return ((Iterable<?>)obj).iterator(); 358 } 359 360 final Object wrapped = Global.instance().wrapAsObject(obj); 361 if (wrapped instanceof ScriptObject) { 362 return ((ScriptObject)wrapped).valueIterator(); 363 } 364 365 return Collections.emptyIterator(); 366 } 367 368 /** 369 * Merge a scope into its prototype's map. 370 * Merge a scope into its prototype. 371 * 372 * @param scope Scope to merge. 373 * @return prototype object after merge 374 */ 375 public static ScriptObject mergeScope(final ScriptObject scope) { 376 final ScriptObject global = scope.getProto(); 377 global.addBoundProperties(scope); 378 return global; 379 } 380 381 /** 382 * Call a function given self and args. If the number of the arguments is known in advance, you can likely achieve 383 * better performance by {@link Bootstrap#createDynamicInvoker(String, Class, Class...) creating a dynamic invoker} 384 * for operation {@code "dyn:call"}, then using its {@link MethodHandle#invokeExact(Object...)} method instead. 385 * 386 * @param target ScriptFunction object. 387 * @param self Receiver in call. 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 } | 356 if (obj instanceof Iterable) { 357 return ((Iterable<?>)obj).iterator(); 358 } 359 360 final Object wrapped = Global.instance().wrapAsObject(obj); 361 if (wrapped instanceof ScriptObject) { 362 return ((ScriptObject)wrapped).valueIterator(); 363 } 364 365 return Collections.emptyIterator(); 366 } 367 368 /** 369 * Merge a scope into its prototype's map. 370 * Merge a scope into its prototype. 371 * 372 * @param scope Scope to merge. 373 * @return prototype object after merge 374 */ 375 public static ScriptObject mergeScope(final ScriptObject scope) { 376 final ScriptObject parentScope = scope.getProto(); 377 parentScope.addBoundProperties(scope); 378 return parentScope; 379 } 380 381 /** 382 * Call a function given self and args. If the number of the arguments is known in advance, you can likely achieve 383 * better performance by {@link Bootstrap#createDynamicInvoker(String, Class, Class...) creating a dynamic invoker} 384 * for operation {@code "dyn:call"}, then using its {@link MethodHandle#invokeExact(Object...)} method instead. 385 * 386 * @param target ScriptFunction object. 387 * @param self Receiver in call. 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 } |