src/jdk/nashorn/internal/runtime/Context.java

Print this page




 473      * Get the PropertyMap of the current global scope
 474      * @return the property map of the current global scope
 475      */
 476     public static PropertyMap getGlobalMap() {
 477         return Context.getGlobal().getMap();
 478     }
 479 
 480     /**
 481      * Compile a top level script.
 482      *
 483      * @param source the source
 484      * @param scope  the scope
 485      *
 486      * @return top level function for script
 487      */
 488     public ScriptFunction compileScript(final Source source, final ScriptObject scope) {
 489         return compileScript(source, scope, this.errors);
 490     }
 491 
 492     /**

































 493      * Entry point for {@code eval}
 494      *
 495      * @param initialScope The scope of this eval call
 496      * @param string       Evaluated code as a String
 497      * @param callThis     "this" to be passed to the evaluated code
 498      * @param location     location of the eval call
 499      * @param strict       is this {@code eval} call from a strict mode code?
 500      *
 501      * @return the return value of the {@code eval}
 502      */
 503     public Object eval(final ScriptObject initialScope, final String string, final Object callThis, final Object location, final boolean strict) {
 504         final String  file       = (location == UNDEFINED || location == null) ? "<eval>" : location.toString();
 505         final Source  source     = sourceFor(file, string);
 506         final boolean directEval = location != UNDEFINED; // is this direct 'eval' call or indirectly invoked eval?
 507         final Global  global = Context.getGlobal();
 508 
 509         ScriptObject scope = initialScope;
 510 
 511         // ECMA section 10.1.1 point 2 says eval code is strict if it begins
 512         // with "use strict" directive or eval direct call itself is made


 932             return classPathLoader.getResource(resName);
 933         } else if (appLoader != null) {
 934             return appLoader.getResource(resName);
 935         }
 936 
 937         return null;
 938     }
 939 
 940     private Object evaluateSource(final Source source, final ScriptObject scope, final ScriptObject thiz) {
 941         ScriptFunction script = null;
 942 
 943         try {
 944             script = compileScript(source, scope, new Context.ThrowErrorManager());
 945         } catch (final ParserException e) {
 946             e.throwAsEcmaException();
 947         }
 948 
 949         return ScriptRuntime.apply(script, thiz);
 950     }
 951 
 952     private static ScriptFunction getRunScriptFunction(final Class<?> script, final ScriptObject scope) {
 953         if (script == null) {
 954             return null;
 955         }
 956 
 957         // Get run method - the entry point to the script
 958         final MethodHandle runMethodHandle =
 959                 MH.findStatic(
 960                     MethodHandles.lookup(),
 961                     script,
 962                     RUN_SCRIPT.symbolName(),
 963                     MH.type(
 964                         Object.class,
 965                         ScriptFunction.class,
 966                         Object.class));

 967 
 968         boolean strict;
 969 
 970         try {
 971             strict = script.getField(STRICT_MODE.symbolName()).getBoolean(null);
 972         } catch (final NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
 973             strict = false;
 974         }










 975 
 976         // Package as a JavaScript function and pass function back to shell.
 977         return Context.getGlobal().newScriptFunction(RUN_SCRIPT.symbolName(), runMethodHandle, scope, strict);
 978     }
 979 
 980     private ScriptFunction compileScript(final Source source, final ScriptObject scope, final ErrorManager errMan) {
 981         return getRunScriptFunction(compile(source, errMan, this._strict), scope);
 982     }
 983 
 984     private synchronized Class<?> compile(final Source source, final ErrorManager errMan, final boolean strict) {
 985         // start with no errors, no warnings.
 986         errMan.reset();
 987 
 988         Class<?> script = findCachedClass(source);
 989         if (script != null) {
 990             Compiler.LOG.fine("Code cache hit for ", source, " avoiding recompile.");
 991             return script;
 992         }
 993 
 994         CompiledScript compiledScript = null;




 473      * Get the PropertyMap of the current global scope
 474      * @return the property map of the current global scope
 475      */
 476     public static PropertyMap getGlobalMap() {
 477         return Context.getGlobal().getMap();
 478     }
 479     
 480     /**
 481      * Compile a top level script.
 482      *
 483      * @param source the source
 484      * @param scope  the scope
 485      *
 486      * @return top level function for script
 487      */
 488     public ScriptFunction compileScript(final Source source, final ScriptObject scope) {
 489         return compileScript(source, scope, this.errors);
 490     }
 491     
 492     /**
 493      * Interface to represent compiled code that can be re-used across many
 494      * global scope instances
 495      */
 496     public static interface MultiGlobalCompiledScript {
 497         /**
 498          * Obtain script function object for a specific global scope object.
 499          * 
 500          * @param newGlobal global scope for which function object is obtained
 501          * @return script function for script level expressions
 502          */
 503         public ScriptFunction getFunction(final Global newGlobal);
 504     }
 505     
 506     /**
 507      * Compile a top level script.
 508      * 
 509      * @param source the script source
 510      * @return reusable compiled script across many global scopes.
 511      */
 512     public MultiGlobalCompiledScript compileScript(final Source source) {
 513         final Class<?> clazz = compile(source, this.errors, this._strict);
 514         final MethodHandle runMethodHandle = getRunScriptHandle(clazz);
 515         final boolean strict = isStrict(clazz);
 516         
 517         return new MultiGlobalCompiledScript() {           
 518             @Override
 519             public ScriptFunction getFunction(final Global newGlobal) {
 520                 return Context.getGlobal().newScriptFunction(RUN_SCRIPT.symbolName(), runMethodHandle, newGlobal, strict);
 521             }
 522         };
 523     }    
 524     
 525     /**
 526      * Entry point for {@code eval}
 527      *
 528      * @param initialScope The scope of this eval call
 529      * @param string       Evaluated code as a String
 530      * @param callThis     "this" to be passed to the evaluated code
 531      * @param location     location of the eval call
 532      * @param strict       is this {@code eval} call from a strict mode code?
 533      *
 534      * @return the return value of the {@code eval}
 535      */
 536     public Object eval(final ScriptObject initialScope, final String string, final Object callThis, final Object location, final boolean strict) {
 537         final String  file       = (location == UNDEFINED || location == null) ? "<eval>" : location.toString();
 538         final Source  source     = sourceFor(file, string);
 539         final boolean directEval = location != UNDEFINED; // is this direct 'eval' call or indirectly invoked eval?
 540         final Global  global = Context.getGlobal();
 541 
 542         ScriptObject scope = initialScope;
 543 
 544         // ECMA section 10.1.1 point 2 says eval code is strict if it begins
 545         // with "use strict" directive or eval direct call itself is made


 965             return classPathLoader.getResource(resName);
 966         } else if (appLoader != null) {
 967             return appLoader.getResource(resName);
 968         }
 969 
 970         return null;
 971     }
 972 
 973     private Object evaluateSource(final Source source, final ScriptObject scope, final ScriptObject thiz) {
 974         ScriptFunction script = null;
 975 
 976         try {
 977             script = compileScript(source, scope, new Context.ThrowErrorManager());
 978         } catch (final ParserException e) {
 979             e.throwAsEcmaException();
 980         }
 981 
 982         return ScriptRuntime.apply(script, thiz);
 983     }
 984 
 985     private static MethodHandle getRunScriptHandle(final Class<?> script) {
 986         return MH.findStatic(






 987                     MethodHandles.lookup(),
 988                     script,
 989                     RUN_SCRIPT.symbolName(),
 990                     MH.type(
 991                         Object.class,
 992                         ScriptFunction.class,
 993                         Object.class));
 994     }
 995     
 996     private static boolean isStrict(final Class<?> script) {

 997         try {
 998             return script.getField(STRICT_MODE.symbolName()).getBoolean(null);
 999         } catch (final NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
1000             return false;
1001         }
1002     }
1003     
1004     private static ScriptFunction getRunScriptFunction(final Class<?> script, final ScriptObject scope) {
1005         if (script == null) {
1006             return null;
1007         }
1008 
1009         // Get run method - the entry point to the script
1010         final MethodHandle runMethodHandle = getRunScriptHandle(script);
1011         boolean strict = isStrict(script);
1012 
1013         // Package as a JavaScript function and pass function back to shell.
1014         return Context.getGlobal().newScriptFunction(RUN_SCRIPT.symbolName(), runMethodHandle, scope, strict);
1015     }
1016 
1017     private ScriptFunction compileScript(final Source source, final ScriptObject scope, final ErrorManager errMan) {
1018         return getRunScriptFunction(compile(source, errMan, this._strict), scope);
1019     }
1020 
1021     private synchronized Class<?> compile(final Source source, final ErrorManager errMan, final boolean strict) {
1022         // start with no errors, no warnings.
1023         errMan.reset();
1024 
1025         Class<?> script = findCachedClass(source);
1026         if (script != null) {
1027             Compiler.LOG.fine("Code cache hit for ", source, " avoiding recompile.");
1028             return script;
1029         }
1030 
1031         CompiledScript compiledScript = null;