src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptRuntime.java

Print this page




 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 proto = scope.getProto();
 377         // Variable declarations go into the first parent scope that is not a with-statement
 378         ScriptObject parentScope = proto;
 379         while (parentScope instanceof WithObject) {
 380             parentScope = parentScope.getProto();
 381         }
 382         parentScope.addBoundProperties(scope);
 383         return proto;
 384     }
 385 
 386     /**
 387      * Call a function given self and args. If the number of the arguments is known in advance, you can likely achieve
 388      * better performance by {@link Bootstrap#createDynamicInvoker(String, Class, Class...) creating a dynamic invoker}
 389      * for operation {@code "dyn:call"}, then using its {@link MethodHandle#invokeExact(Object...)} method instead.
 390      *
 391      * @param target ScriptFunction object.
 392      * @param self   Receiver in call.
 393      * @param args   Call arguments.
 394      * @return Call result.
 395      */
 396     public static Object apply(final ScriptFunction target, final Object self, final Object... args) {
 397         try {
 398             return target.invoke(self, args);
 399         } catch (final RuntimeException | Error e) {
 400             throw e;
 401         } catch (final Throwable t) {
 402             throw new RuntimeException(t);
 403         }