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

Print this page




 508                 if (cause instanceof NoSuchMethodException) {
 509                     throw (NoSuchMethodException)cause;
 510                 }
 511                 throwAsScriptException(e, invokeGlobal);
 512                 throw new AssertionError("should not reach here");
 513             }
 514         }
 515 
 516         // Non-script object passed as selfObject
 517         throw new IllegalArgumentException(getMessage("interface.on.non.script.object"));
 518     }
 519 
 520     private Object evalImpl(final Source src, final ScriptContext ctxt) throws ScriptException {
 521         return evalImpl(compileImpl(src, ctxt), ctxt);
 522     }
 523 
 524     private Object evalImpl(final ScriptFunction script, final ScriptContext ctxt) throws ScriptException {
 525         return evalImpl(script, ctxt, getNashornGlobalFrom(ctxt));
 526     }
 527 

























 528     private Object evalImpl(final ScriptFunction script, final ScriptContext ctxt, final Global ctxtGlobal) throws ScriptException {
 529         if (script == null) {
 530             return null;
 531         }
 532         final Global oldGlobal = Context.getGlobal();
 533         final boolean globalChanged = (oldGlobal != ctxtGlobal);
 534         try {
 535             if (globalChanged) {
 536                 Context.setGlobal(ctxtGlobal);
 537             }
 538 
 539             // set ScriptContext variables if ctxt is non-null
 540             if (ctxt != null) {
 541                 setContextVariables(ctxtGlobal, ctxt);
 542             }
 543             return ScriptObjectMirror.translateUndefined(ScriptObjectMirror.wrap(ScriptRuntime.apply(script, ctxtGlobal), ctxtGlobal));
 544         } catch (final Exception e) {
 545             throwAsScriptException(e, ctxtGlobal);
 546             throw new AssertionError("should not reach here");
 547         } finally {


 554     private static void throwAsScriptException(final Exception e, final Global global) throws ScriptException {
 555         if (e instanceof ScriptException) {
 556             throw (ScriptException)e;
 557         } else if (e instanceof NashornException) {
 558             final NashornException ne = (NashornException)e;
 559             final ScriptException se = new ScriptException(
 560                 ne.getMessage(), ne.getFileName(),
 561                 ne.getLineNumber(), ne.getColumnNumber());
 562             ne.initEcmaError(global);
 563             se.initCause(e);
 564             throw se;
 565         } else if (e instanceof RuntimeException) {
 566             throw (RuntimeException)e;
 567         } else {
 568             // wrap any other exception as ScriptException
 569             throw new ScriptException(e);
 570         }
 571     }
 572 
 573     private CompiledScript asCompiledScript(final Source source) throws ScriptException {
 574         final ScriptFunction func = compileImpl(source, context);




















 575         return new CompiledScript() {
 576             @Override
 577             public Object eval(final ScriptContext ctxt) throws ScriptException {
 578                 final Global globalObject = getNashornGlobalFrom(ctxt);
 579                 // Are we running the script in the correct global?
 580                 if (func.getScope() == globalObject) {
 581                     return evalImpl(func, ctxt, globalObject);
 582                 }
 583                 // ScriptContext with a different global. Compile again!
 584                 // Note that we may still hit per-global compilation cache.
 585                 return evalImpl(compileImpl(source, ctxt), ctxt, globalObject);
 586             }
 587             @Override
 588             public ScriptEngine getEngine() {
 589                 return NashornScriptEngine.this;
 590             }
 591         };
 592     }
 593 
 594     private ScriptFunction compileImpl(final Source source, final ScriptContext ctxt) throws ScriptException {
 595         return compileImpl(source, getNashornGlobalFrom(ctxt));
 596     }
 597 
 598     private ScriptFunction compileImpl(final Source source, final Global newGlobal) throws ScriptException {
 599         final Global oldGlobal = Context.getGlobal();
 600         final boolean globalChanged = (oldGlobal != newGlobal);
 601         try {
 602             if (globalChanged) {
 603                 Context.setGlobal(newGlobal);
 604             }
 605 




 508                 if (cause instanceof NoSuchMethodException) {
 509                     throw (NoSuchMethodException)cause;
 510                 }
 511                 throwAsScriptException(e, invokeGlobal);
 512                 throw new AssertionError("should not reach here");
 513             }
 514         }
 515 
 516         // Non-script object passed as selfObject
 517         throw new IllegalArgumentException(getMessage("interface.on.non.script.object"));
 518     }
 519 
 520     private Object evalImpl(final Source src, final ScriptContext ctxt) throws ScriptException {
 521         return evalImpl(compileImpl(src, ctxt), ctxt);
 522     }
 523 
 524     private Object evalImpl(final ScriptFunction script, final ScriptContext ctxt) throws ScriptException {
 525         return evalImpl(script, ctxt, getNashornGlobalFrom(ctxt));
 526     }
 527 
 528     private Object evalImpl(final Context.MultiGlobalCompiledScript mgcs, final ScriptContext ctxt, final Global ctxtGlobal) throws ScriptException {
 529         final Global oldGlobal = Context.getGlobal();
 530         final boolean globalChanged = (oldGlobal != ctxtGlobal);
 531         try {
 532             if (globalChanged) {
 533                 Context.setGlobal(ctxtGlobal);
 534             }
 535             
 536             final ScriptFunction script = mgcs.getFunction(ctxtGlobal);
 537 
 538             // set ScriptContext variables if ctxt is non-null
 539             if (ctxt != null) {
 540                 setContextVariables(ctxtGlobal, ctxt);
 541             }
 542             return ScriptObjectMirror.translateUndefined(ScriptObjectMirror.wrap(ScriptRuntime.apply(script, ctxtGlobal), ctxtGlobal));
 543         } catch (final Exception e) {
 544             throwAsScriptException(e, ctxtGlobal);
 545             throw new AssertionError("should not reach here");
 546         } finally {
 547             if (globalChanged) {
 548                 Context.setGlobal(oldGlobal);
 549             }
 550         }
 551     }
 552 
 553     private Object evalImpl(final ScriptFunction script, final ScriptContext ctxt, final Global ctxtGlobal) throws ScriptException {
 554         if (script == null) {
 555             return null;
 556         }
 557         final Global oldGlobal = Context.getGlobal();
 558         final boolean globalChanged = (oldGlobal != ctxtGlobal);
 559         try {
 560             if (globalChanged) {
 561                 Context.setGlobal(ctxtGlobal);
 562             }
 563 
 564             // set ScriptContext variables if ctxt is non-null
 565             if (ctxt != null) {
 566                 setContextVariables(ctxtGlobal, ctxt);
 567             }
 568             return ScriptObjectMirror.translateUndefined(ScriptObjectMirror.wrap(ScriptRuntime.apply(script, ctxtGlobal), ctxtGlobal));
 569         } catch (final Exception e) {
 570             throwAsScriptException(e, ctxtGlobal);
 571             throw new AssertionError("should not reach here");
 572         } finally {


 579     private static void throwAsScriptException(final Exception e, final Global global) throws ScriptException {
 580         if (e instanceof ScriptException) {
 581             throw (ScriptException)e;
 582         } else if (e instanceof NashornException) {
 583             final NashornException ne = (NashornException)e;
 584             final ScriptException se = new ScriptException(
 585                 ne.getMessage(), ne.getFileName(),
 586                 ne.getLineNumber(), ne.getColumnNumber());
 587             ne.initEcmaError(global);
 588             se.initCause(e);
 589             throw se;
 590         } else if (e instanceof RuntimeException) {
 591             throw (RuntimeException)e;
 592         } else {
 593             // wrap any other exception as ScriptException
 594             throw new ScriptException(e);
 595         }
 596     }
 597 
 598     private CompiledScript asCompiledScript(final Source source) throws ScriptException {
 599         final Context.MultiGlobalCompiledScript mgcs;
 600         final ScriptFunction func;
 601         final Global oldGlobal = Context.getGlobal();
 602         final Global newGlobal = getNashornGlobalFrom(context);
 603         final boolean globalChanged = (oldGlobal != newGlobal);
 604         try {
 605             if (globalChanged) {
 606                 Context.setGlobal(newGlobal);
 607             }
 608 
 609             mgcs = nashornContext.compileScript(source);
 610             func = mgcs.getFunction(newGlobal);
 611         } catch (final Exception e) {
 612             throwAsScriptException(e, newGlobal);
 613             throw new AssertionError("should not reach here");
 614         } finally {
 615             if (globalChanged) {
 616                 Context.setGlobal(oldGlobal);
 617             }
 618         }
 619                 
 620         return new CompiledScript() {
 621             @Override
 622             public Object eval(final ScriptContext ctxt) throws ScriptException {
 623                 final Global globalObject = getNashornGlobalFrom(ctxt);
 624                 // Are we running the script in the same global in which it was compiled?
 625                 if (func.getScope() == globalObject) {
 626                     return evalImpl(func, ctxt, globalObject);
 627                 }
 628                 
 629                 // different global
 630                 return evalImpl(mgcs, ctxt, globalObject);
 631             }
 632             @Override
 633             public ScriptEngine getEngine() {
 634                 return NashornScriptEngine.this;
 635             }
 636         };
 637     }
 638 
 639     private ScriptFunction compileImpl(final Source source, final ScriptContext ctxt) throws ScriptException {
 640         return compileImpl(source, getNashornGlobalFrom(ctxt));
 641     }
 642 
 643     private ScriptFunction compileImpl(final Source source, final Global newGlobal) throws ScriptException {
 644         final Global oldGlobal = Context.getGlobal();
 645         final boolean globalChanged = (oldGlobal != newGlobal);
 646         try {
 647             if (globalChanged) {
 648                 Context.setGlobal(newGlobal);
 649             }
 650