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

Print this page




  29 import static jdk.nashorn.internal.codegen.CompilerConstants.STRICT_MODE;
  30 import static jdk.nashorn.internal.lookup.Lookup.MH;
  31 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
  32 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
  33 
  34 import java.io.File;
  35 import java.io.IOException;
  36 import java.io.PrintWriter;
  37 import java.lang.invoke.MethodHandle;
  38 import java.lang.invoke.MethodHandles;
  39 import java.lang.reflect.Constructor;
  40 import java.net.MalformedURLException;
  41 import java.net.URL;
  42 import java.security.AccessControlContext;
  43 import java.security.AccessController;
  44 import java.security.CodeSigner;
  45 import java.security.CodeSource;
  46 import java.security.Permissions;
  47 import java.security.PrivilegedAction;
  48 import java.security.ProtectionDomain;

  49 import jdk.internal.org.objectweb.asm.ClassReader;
  50 import jdk.internal.org.objectweb.asm.util.CheckClassAdapter;
  51 import jdk.nashorn.api.scripting.ScriptObjectMirror;
  52 import jdk.nashorn.internal.codegen.Compiler;
  53 import jdk.nashorn.internal.codegen.ObjectClassGenerator;
  54 import jdk.nashorn.internal.ir.FunctionNode;
  55 import jdk.nashorn.internal.ir.debug.ASTWriter;
  56 import jdk.nashorn.internal.ir.debug.PrintVisitor;
  57 import jdk.nashorn.internal.parser.Parser;
  58 import jdk.nashorn.internal.runtime.options.Options;
  59 
  60 /**
  61  * This class manages the global state of execution. Context is immutable.
  62  */
  63 public final class Context {
  64 
  65     /**
  66      * ContextCodeInstaller that has the privilege of installing classes in the Context.
  67      * Can only be instantiated from inside the context and is opaque to other classes
  68      */


 463                         url = new URL(srcStr);
 464                     } catch (final MalformedURLException e) {
 465                         url = file.toURI().toURL();
 466                     }
 467                     source = new Source(url.toString(), url);
 468                 }
 469             } else if (file.isFile()) {
 470                 source = new Source(srcStr, file);
 471             }
 472         } else if (src instanceof File && ((File)src).isFile()) {
 473             final File file = (File)src;
 474             source = new Source(file.getName(), file);
 475         } else if (src instanceof URL) {
 476             final URL url = (URL)src;
 477             source = new Source(url.toString(), url);
 478         } else if (src instanceof ScriptObject) {
 479             final ScriptObject sobj = (ScriptObject)src;
 480             if (sobj.has("script") && sobj.has("name")) {
 481                 final String script = JSType.toString(sobj.get("script"));
 482                 final String name   = JSType.toString(sobj.get("name"));







 483                 source = new Source(name, script);
 484             }
 485         }
 486 
 487         if (source != null) {
 488             return evaluateSource(source, scope, scope);
 489         }
 490 
 491         throw typeError("cant.load.script", ScriptRuntime.safeToString(from));
 492     }
 493 
 494     /**
 495      * Implementation of {@code loadWithNewGlobal} Nashorn extension. Load a script file from a source
 496      * expression, after creating a new global scope.
 497      *
 498      * @param from source expression for script
 499      * @param args (optional) arguments to be passed to the loaded script
 500      *
 501      * @return return value for load call (undefined)
 502      *




  29 import static jdk.nashorn.internal.codegen.CompilerConstants.STRICT_MODE;
  30 import static jdk.nashorn.internal.lookup.Lookup.MH;
  31 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
  32 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
  33 
  34 import java.io.File;
  35 import java.io.IOException;
  36 import java.io.PrintWriter;
  37 import java.lang.invoke.MethodHandle;
  38 import java.lang.invoke.MethodHandles;
  39 import java.lang.reflect.Constructor;
  40 import java.net.MalformedURLException;
  41 import java.net.URL;
  42 import java.security.AccessControlContext;
  43 import java.security.AccessController;
  44 import java.security.CodeSigner;
  45 import java.security.CodeSource;
  46 import java.security.Permissions;
  47 import java.security.PrivilegedAction;
  48 import java.security.ProtectionDomain;
  49 import java.util.Map;
  50 import jdk.internal.org.objectweb.asm.ClassReader;
  51 import jdk.internal.org.objectweb.asm.util.CheckClassAdapter;
  52 import jdk.nashorn.api.scripting.ScriptObjectMirror;
  53 import jdk.nashorn.internal.codegen.Compiler;
  54 import jdk.nashorn.internal.codegen.ObjectClassGenerator;
  55 import jdk.nashorn.internal.ir.FunctionNode;
  56 import jdk.nashorn.internal.ir.debug.ASTWriter;
  57 import jdk.nashorn.internal.ir.debug.PrintVisitor;
  58 import jdk.nashorn.internal.parser.Parser;
  59 import jdk.nashorn.internal.runtime.options.Options;
  60 
  61 /**
  62  * This class manages the global state of execution. Context is immutable.
  63  */
  64 public final class Context {
  65 
  66     /**
  67      * ContextCodeInstaller that has the privilege of installing classes in the Context.
  68      * Can only be instantiated from inside the context and is opaque to other classes
  69      */


 464                         url = new URL(srcStr);
 465                     } catch (final MalformedURLException e) {
 466                         url = file.toURI().toURL();
 467                     }
 468                     source = new Source(url.toString(), url);
 469                 }
 470             } else if (file.isFile()) {
 471                 source = new Source(srcStr, file);
 472             }
 473         } else if (src instanceof File && ((File)src).isFile()) {
 474             final File file = (File)src;
 475             source = new Source(file.getName(), file);
 476         } else if (src instanceof URL) {
 477             final URL url = (URL)src;
 478             source = new Source(url.toString(), url);
 479         } else if (src instanceof ScriptObject) {
 480             final ScriptObject sobj = (ScriptObject)src;
 481             if (sobj.has("script") && sobj.has("name")) {
 482                 final String script = JSType.toString(sobj.get("script"));
 483                 final String name   = JSType.toString(sobj.get("name"));
 484                 source = new Source(name, script);
 485             }
 486         } else if (src instanceof Map) {
 487             final Map map = (Map)src;
 488             if (map.containsKey("script") && map.containsKey("name")) {
 489                 final String script = JSType.toString(map.get("script"));
 490                 final String name   = JSType.toString(map.get("name"));
 491                 source = new Source(name, script);
 492             }
 493         }
 494 
 495         if (source != null) {
 496             return evaluateSource(source, scope, scope);
 497         }
 498 
 499         throw typeError("cant.load.script", ScriptRuntime.safeToString(from));
 500     }
 501 
 502     /**
 503      * Implementation of {@code loadWithNewGlobal} Nashorn extension. Load a script file from a source
 504      * expression, after creating a new global scope.
 505      *
 506      * @param from source expression for script
 507      * @param args (optional) arguments to be passed to the loaded script
 508      *
 509      * @return return value for load call (undefined)
 510      *