src/jdk/nashorn/api/scripting/NashornScriptEngine.java

Print this page




 304      * @return the value of the named variable
 305      */
 306     public Object __noSuchProperty__(final Object self, final ScriptContext ctxt, final String name) {
 307         final int scope = ctxt.getAttributesScope(name);
 308         final ScriptObject ctxtGlobal = getNashornGlobalFrom(ctxt);
 309         if (scope != -1) {
 310             return ScriptObjectMirror.unwrap(ctxt.getAttribute(name, scope), ctxtGlobal);
 311         }
 312 
 313         if (self == UNDEFINED) {
 314             // scope access and so throw ReferenceError
 315             throw referenceError(ctxtGlobal, "not.defined", name);
 316         }
 317 
 318         return UNDEFINED;
 319     }
 320 
 321     private ScriptObject getNashornGlobalFrom(final ScriptContext ctxt) {
 322         final Bindings bindings = ctxt.getBindings(ScriptContext.ENGINE_SCOPE);
 323         if (bindings instanceof ScriptObjectMirror) {

 324              ScriptObject sobj = ((ScriptObjectMirror)bindings).getScriptObject();
 325              if (sobj instanceof GlobalObject) {
 326                  return sobj;
 327              }
 328         }
 329 
 330         // didn't find global object from context given - return the engine-wide global
 331         return global;
 332     }
 333 
 334     private ScriptObject createNashornGlobal() {
 335         final ScriptObject newGlobal = AccessController.doPrivileged(new PrivilegedAction<ScriptObject>() {
 336             @Override
 337             public ScriptObject run() {
 338                 try {
 339                     return nashornContext.newGlobal();
 340                 } catch (final RuntimeException e) {
 341                     if (Context.DEBUG) {
 342                         e.printStackTrace();
 343                     }
 344                     throw e;
 345                 }


 385             if (Context.DEBUG) {
 386                 e.printStackTrace();
 387             }
 388             throw new ScriptException(e);
 389         } finally {
 390             put(ScriptEngine.FILENAME, null);
 391         }
 392     }
 393 
 394     // scripts should see "context" and "engine" as variables
 395     private void setContextVariables(final ScriptContext ctxt) {
 396         final ScriptObject ctxtGlobal = getNashornGlobalFrom(ctxt);
 397         // set "context" global variable via contextProperty - because this
 398         // property is non-writable
 399         contextProperty.setObjectValue(ctxtGlobal, ctxtGlobal, ctxt, false);
 400         Object args = ScriptObjectMirror.unwrap(ctxt.getAttribute("arguments"), ctxtGlobal);
 401         if (args == null || args == UNDEFINED) {
 402             args = ScriptRuntime.EMPTY_ARRAY;
 403         }
 404         // if no arguments passed, expose it

 405         args = ((GlobalObject)ctxtGlobal).wrapAsObject(args);
 406         ctxtGlobal.set("arguments", args, false);

 407     }
 408 
 409     private Object invokeImpl(final Object selfObject, final String name, final Object... args) throws ScriptException, NoSuchMethodException {
 410         name.getClass(); // null check
 411 
 412         ScriptObjectMirror selfMirror = null;
 413         if (selfObject instanceof ScriptObjectMirror) {
 414             selfMirror = (ScriptObjectMirror)selfObject;
 415             if (! selfMirror.getHomeGlobal().isOfContext(nashornContext)) {
 416                 throw new IllegalArgumentException(getMessage("script.object.from.another.engine"));
 417             }
 418         } else if (selfObject instanceof ScriptObject) {
 419             // invokeMethod called from script code - in which case we may get 'naked' ScriptObject
 420             // Wrap it with oldGlobal to make a ScriptObjectMirror for the same.
 421             final ScriptObject oldGlobal = Context.getGlobal();
 422             if (oldGlobal == null) {
 423                 throw new IllegalArgumentException(getMessage("no.current.nashorn.global"));
 424             }
 425 
 426             if (! oldGlobal.isOfContext(nashornContext)) {




 304      * @return the value of the named variable
 305      */
 306     public Object __noSuchProperty__(final Object self, final ScriptContext ctxt, final String name) {
 307         final int scope = ctxt.getAttributesScope(name);
 308         final ScriptObject ctxtGlobal = getNashornGlobalFrom(ctxt);
 309         if (scope != -1) {
 310             return ScriptObjectMirror.unwrap(ctxt.getAttribute(name, scope), ctxtGlobal);
 311         }
 312 
 313         if (self == UNDEFINED) {
 314             // scope access and so throw ReferenceError
 315             throw referenceError(ctxtGlobal, "not.defined", name);
 316         }
 317 
 318         return UNDEFINED;
 319     }
 320 
 321     private ScriptObject getNashornGlobalFrom(final ScriptContext ctxt) {
 322         final Bindings bindings = ctxt.getBindings(ScriptContext.ENGINE_SCOPE);
 323         if (bindings instanceof ScriptObjectMirror) {
 324             final ScriptObjectMirror mirror = (ScriptObjectMirror)bindings;
 325             ScriptObject sobj = ((ScriptObjectMirror)bindings).getScriptObject();
 326             if (sobj instanceof GlobalObject && sobj.isOfContext(nashornContext)) {
 327                 return sobj;
 328             }
 329         }
 330 
 331         // didn't find global object from context given - return the engine-wide global
 332         return global;
 333     }
 334 
 335     private ScriptObject createNashornGlobal() {
 336         final ScriptObject newGlobal = AccessController.doPrivileged(new PrivilegedAction<ScriptObject>() {
 337             @Override
 338             public ScriptObject run() {
 339                 try {
 340                     return nashornContext.newGlobal();
 341                 } catch (final RuntimeException e) {
 342                     if (Context.DEBUG) {
 343                         e.printStackTrace();
 344                     }
 345                     throw e;
 346                 }


 386             if (Context.DEBUG) {
 387                 e.printStackTrace();
 388             }
 389             throw new ScriptException(e);
 390         } finally {
 391             put(ScriptEngine.FILENAME, null);
 392         }
 393     }
 394 
 395     // scripts should see "context" and "engine" as variables
 396     private void setContextVariables(final ScriptContext ctxt) {
 397         final ScriptObject ctxtGlobal = getNashornGlobalFrom(ctxt);
 398         // set "context" global variable via contextProperty - because this
 399         // property is non-writable
 400         contextProperty.setObjectValue(ctxtGlobal, ctxtGlobal, ctxt, false);
 401         Object args = ScriptObjectMirror.unwrap(ctxt.getAttribute("arguments"), ctxtGlobal);
 402         if (args == null || args == UNDEFINED) {
 403             args = ScriptRuntime.EMPTY_ARRAY;
 404         }
 405         // if no arguments passed, expose it
 406         if (! (args instanceof ScriptObject)) {
 407             args = ((GlobalObject)ctxtGlobal).wrapAsObject(args);
 408             ctxtGlobal.set("arguments", args, false);
 409         }
 410     }
 411 
 412     private Object invokeImpl(final Object selfObject, final String name, final Object... args) throws ScriptException, NoSuchMethodException {
 413         name.getClass(); // null check
 414 
 415         ScriptObjectMirror selfMirror = null;
 416         if (selfObject instanceof ScriptObjectMirror) {
 417             selfMirror = (ScriptObjectMirror)selfObject;
 418             if (! selfMirror.getHomeGlobal().isOfContext(nashornContext)) {
 419                 throw new IllegalArgumentException(getMessage("script.object.from.another.engine"));
 420             }
 421         } else if (selfObject instanceof ScriptObject) {
 422             // invokeMethod called from script code - in which case we may get 'naked' ScriptObject
 423             // Wrap it with oldGlobal to make a ScriptObjectMirror for the same.
 424             final ScriptObject oldGlobal = Context.getGlobal();
 425             if (oldGlobal == null) {
 426                 throw new IllegalArgumentException(getMessage("no.current.nashorn.global"));
 427             }
 428 
 429             if (! oldGlobal.isOfContext(nashornContext)) {