8008731: Separate configuration environment (options, error/output writer etc.) from Context

Mon, 25 Feb 2013 16:58:31 +0530

author
sundar
date
Mon, 25 Feb 2013 16:58:31 +0530
changeset 118
927fba6785b0
parent 117
5452f82eb2ce
child 119
5610ac25d8ff

8008731: Separate configuration environment (options, error/output writer etc.) from Context
Reviewed-by: hannesw, lagergren

src/jdk/nashorn/internal/codegen/Attr.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/codegen/ClassEmitter.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/codegen/CodeGenerator.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/codegen/CompilationPhase.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/codegen/Compiler.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/codegen/MethodEmitter.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/codegen/ObjectClassGenerator.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/ir/debug/JSONWriter.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/objects/Global.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/objects/NativeDate.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/objects/NativeString.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/objects/ScriptFunctionTrampolineImpl.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/parser/Parser.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/CodeInstaller.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/Context.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/JSONFunctions.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/OptionsObject.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/ScriptEnvironment.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/ScriptRuntime.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/tools/Shell.java file | annotate | diff | comparison | revisions
test/script/trusted/JDK-8006529.js file | annotate | diff | comparison | revisions
test/src/jdk/nashorn/internal/parser/ParserTest.java file | annotate | diff | comparison | revisions
test/src/jdk/nashorn/internal/test/framework/SharedContextEvaluator.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/jdk/nashorn/internal/codegen/Attr.java	Fri Feb 22 23:33:46 2013 -0400
     1.2 +++ b/src/jdk/nashorn/internal/codegen/Attr.java	Mon Feb 25 16:58:31 2013 +0530
     1.3 @@ -101,9 +101,6 @@
     1.4   */
     1.5  
     1.6  final class Attr extends NodeOperatorVisitor {
     1.7 -    /** Context compiler. */
     1.8 -    private final Context context;
     1.9 -
    1.10      /**
    1.11       * Local definitions in current block (to discriminate from function
    1.12       * declarations always defined in the function scope. This is for
    1.13 @@ -123,11 +120,8 @@
    1.14  
    1.15      /**
    1.16       * Constructor.
    1.17 -     *
    1.18 -     * @param compiler the compiler
    1.19       */
    1.20 -    Attr(final Context context) {
    1.21 -        this.context = context;
    1.22 +    Attr() {
    1.23      }
    1.24  
    1.25      @Override
    1.26 @@ -258,7 +252,7 @@
    1.27          }
    1.28  
    1.29          if (functionNode.isScript()) {
    1.30 -            initFromPropertyMap(context, functionNode);
    1.31 +            initFromPropertyMap(functionNode);
    1.32          }
    1.33  
    1.34          // Add function name as local symbol
    1.35 @@ -1271,10 +1265,9 @@
    1.36  
    1.37      /**
    1.38       * Move any properties from a global map into the scope of this method
    1.39 -     * @param context      context
    1.40       * @param functionNode the function node for which to init scope vars
    1.41       */
    1.42 -    private static void initFromPropertyMap(final Context context, final FunctionNode functionNode) {
    1.43 +    private static void initFromPropertyMap(final FunctionNode functionNode) {
    1.44          // For a script, add scope symbols as defined in the property map
    1.45          assert functionNode.isScript();
    1.46  
     2.1 --- a/src/jdk/nashorn/internal/codegen/ClassEmitter.java	Fri Feb 22 23:33:46 2013 -0400
     2.2 +++ b/src/jdk/nashorn/internal/codegen/ClassEmitter.java	Mon Feb 25 16:58:31 2013 +0530
     2.3 @@ -62,11 +62,10 @@
     2.4  import jdk.internal.org.objectweb.asm.ClassWriter;
     2.5  import jdk.internal.org.objectweb.asm.MethodVisitor;
     2.6  import jdk.internal.org.objectweb.asm.util.TraceClassVisitor;
     2.7 -import jdk.nashorn.internal.codegen.Emitter;
     2.8  import jdk.nashorn.internal.codegen.types.Type;
     2.9  import jdk.nashorn.internal.ir.FunctionNode;
    2.10 -import jdk.nashorn.internal.runtime.Context;
    2.11  import jdk.nashorn.internal.runtime.PropertyMap;
    2.12 +import jdk.nashorn.internal.runtime.ScriptEnvironment;
    2.13  import jdk.nashorn.internal.runtime.ScriptObject;
    2.14  import jdk.nashorn.internal.runtime.Source;
    2.15  
    2.16 @@ -121,8 +120,8 @@
    2.17      /** The ASM classwriter that we use for all bytecode operations */
    2.18      protected final ClassWriter cw;
    2.19  
    2.20 -    /** The context */
    2.21 -    protected final Context context;
    2.22 +    /** The script environment */
    2.23 +    protected final ScriptEnvironment env;
    2.24  
    2.25      /** Default flags for class generation - oublic class */
    2.26      private static final EnumSet<Flag> DEFAULT_METHOD_FLAGS = EnumSet.of(Flag.PUBLIC);
    2.27 @@ -137,13 +136,13 @@
    2.28       * Constructor - only used internally in this class as it breaks
    2.29       * abstraction towards ASM or other code generator below
    2.30       *
    2.31 -     * @param context  context
    2.32 -     * @param cw       ASM classwriter
    2.33 +     * @param env script environment
    2.34 +     * @param cw  ASM classwriter
    2.35       */
    2.36 -    private ClassEmitter(final Context context, final ClassWriter cw) {
    2.37 -        assert context != null;
    2.38 +    private ClassEmitter(final ScriptEnvironment env, final ClassWriter cw) {
    2.39 +        assert env != null;
    2.40  
    2.41 -        this.context        = context;
    2.42 +        this.env            = env;
    2.43          this.cw             = cw;
    2.44          this.methodsStarted = new HashSet<>();
    2.45      }
    2.46 @@ -151,13 +150,13 @@
    2.47      /**
    2.48       * Constructor
    2.49       *
    2.50 -     * @param context         context
    2.51 +     * @param env             script environment
    2.52       * @param className       name of class to weave
    2.53       * @param superClassName  super class name for class
    2.54       * @param interfaceNames  names of interfaces implemented by this class, or null if none
    2.55       */
    2.56 -    ClassEmitter(final Context context, final String className, final String superClassName, final String... interfaceNames) {
    2.57 -        this(context, new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS));
    2.58 +    ClassEmitter(final ScriptEnvironment env, final String className, final String superClassName, final String... interfaceNames) {
    2.59 +        this(env, new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS));
    2.60          cw.visit(V1_7, ACC_PUBLIC | ACC_SUPER, className, null, superClassName, interfaceNames);
    2.61      }
    2.62  
    2.63 @@ -168,8 +167,8 @@
    2.64       * @param unitClassName Compile unit class name.
    2.65       * @param strictMode    Should we generate this method in strict mode
    2.66       */
    2.67 -    ClassEmitter(final Context context, final String sourceName, final String unitClassName, final boolean strictMode) {
    2.68 -        this(context,
    2.69 +    ClassEmitter(final ScriptEnvironment env, final String sourceName, final String unitClassName, final boolean strictMode) {
    2.70 +        this(env,
    2.71               new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS) {
    2.72                  private static final String OBJECT_CLASS  = "java/lang/Object";
    2.73  
    2.74 @@ -372,10 +371,10 @@
    2.75      }
    2.76  
    2.77      /**
    2.78 -     * @return context used for class emission
    2.79 +     * @return env used for class emission
    2.80       */
    2.81 -    Context getContext() {
    2.82 -        return context;
    2.83 +    ScriptEnvironment getEnv() {
    2.84 +        return env;
    2.85      }
    2.86  
    2.87      /**
     3.1 --- a/src/jdk/nashorn/internal/codegen/CodeGenerator.java	Fri Feb 22 23:33:46 2013 -0400
     3.2 +++ b/src/jdk/nashorn/internal/codegen/CodeGenerator.java	Mon Feb 25 16:58:31 2013 +0530
     3.3 @@ -175,7 +175,7 @@
     3.4       */
     3.5      CodeGenerator(final Compiler compiler) {
     3.6          this.compiler      = compiler;
     3.7 -        this.callSiteFlags = compiler.getContext()._callsite_flags;
     3.8 +        this.callSiteFlags = compiler.getEnv()._callsite_flags;
     3.9      }
    3.10  
    3.11      /**
    3.12 @@ -2968,12 +2968,12 @@
    3.13       * @param ident identifier for block or function where applicable
    3.14       */
    3.15      private void printSymbols(final Block block, final String ident) {
    3.16 -        if (!compiler.getContext()._print_symbols) {
    3.17 +        if (!compiler.getEnv()._print_symbols) {
    3.18              return;
    3.19          }
    3.20  
    3.21          @SuppressWarnings("resource")
    3.22 -        final PrintWriter out = compiler.getContext().getErr();
    3.23 +        final PrintWriter out = compiler.getEnv().getErr();
    3.24          out.println("[BLOCK in '" + ident + "']");
    3.25          if (!block.printSymbols(out)) {
    3.26              out.println("<no symbols>");
     4.1 --- a/src/jdk/nashorn/internal/codegen/CompilationPhase.java	Fri Feb 22 23:33:46 2013 -0400
     4.2 +++ b/src/jdk/nashorn/internal/codegen/CompilationPhase.java	Mon Feb 25 16:58:31 2013 +0530
     4.3 @@ -24,8 +24,8 @@
     4.4  import jdk.nashorn.internal.ir.visitor.NodeVisitor;
     4.5  import jdk.nashorn.internal.ir.debug.ASTWriter;
     4.6  import jdk.nashorn.internal.ir.debug.PrintVisitor;
     4.7 -import jdk.nashorn.internal.runtime.Context;
     4.8  import jdk.nashorn.internal.runtime.ECMAErrors;
     4.9 +import jdk.nashorn.internal.runtime.ScriptEnvironment;
    4.10  import jdk.nashorn.internal.runtime.Timing;
    4.11  
    4.12  /**
    4.13 @@ -153,15 +153,15 @@
    4.14      ATTRIBUTION_PHASE(EnumSet.of(INITIALIZED, CONSTANT_FOLDED, LOWERED), ATTR) {
    4.15          @Override
    4.16          void transform(final Compiler compiler, final FunctionNode fn) {
    4.17 -            final Context context = compiler.getContext();
    4.18 +            final ScriptEnvironment env = compiler.getEnv();
    4.19  
    4.20 -            fn.accept(new Attr(context));
    4.21 -            if (context._print_lower_ast) {
    4.22 -                context.getErr().println(new ASTWriter(fn));
    4.23 +            fn.accept(new Attr());
    4.24 +            if (env._print_lower_ast) {
    4.25 +                env.getErr().println(new ASTWriter(fn));
    4.26              }
    4.27  
    4.28 -            if (context._print_lower_parse) {
    4.29 -                context.getErr().println(new PrintVisitor(fn));
    4.30 +            if (env._print_lower_parse) {
    4.31 +                env.getErr().println(new PrintVisitor(fn));
    4.32             }
    4.33          }
    4.34  
    4.35 @@ -232,7 +232,7 @@
    4.36      BYTECODE_GENERATION_PHASE(EnumSet.of(INITIALIZED, CONSTANT_FOLDED, LOWERED, ATTR, SPLIT, FINALIZED), EMITTED) {
    4.37          @Override
    4.38          void transform(final Compiler compiler, final FunctionNode fn) {
    4.39 -            final Context context = compiler.getContext();
    4.40 +            final ScriptEnvironment env = compiler.getEnv();
    4.41  
    4.42              try {
    4.43                  final CodeGenerator codegen = new CodeGenerator(compiler);
    4.44 @@ -240,10 +240,10 @@
    4.45                  codegen.generateScopeCalls();
    4.46  
    4.47              } catch (final VerifyError e) {
    4.48 -                if (context._verify_code || context._print_code) {
    4.49 -                    context.getErr().println(e.getClass().getSimpleName() + ": " + e.getMessage());
    4.50 -                    if (context._dump_on_error) {
    4.51 -                        e.printStackTrace(context.getErr());
    4.52 +                if (env._verify_code || env._print_code) {
    4.53 +                    env.getErr().println(e.getClass().getSimpleName() + ": " + e.getMessage());
    4.54 +                    if (env._dump_on_error) {
    4.55 +                        e.printStackTrace(env.getErr());
    4.56                      }
    4.57                  } else {
    4.58                      throw e;
    4.59 @@ -262,22 +262,22 @@
    4.60                  compiler.addClass(className, bytecode);
    4.61  
    4.62                  //should could be printed to stderr for generate class?
    4.63 -                if (context._print_code) {
    4.64 +                if (env._print_code) {
    4.65                      final StringBuilder sb = new StringBuilder();
    4.66                      sb.append("class: " + className).
    4.67                          append('\n').
    4.68                          append(ClassEmitter.disassemble(bytecode)).
    4.69                          append("=====");
    4.70 -                    context.getErr().println(sb);
    4.71 +                    env.getErr().println(sb);
    4.72                  }
    4.73  
    4.74                  //should we verify the generated code?
    4.75 -                if (context._verify_code) {
    4.76 -                    context.verify(bytecode);
    4.77 +                if (env._verify_code) {
    4.78 +                    compiler.getCodeInstaller().verify(bytecode);
    4.79                  }
    4.80  
    4.81                  //should code be dumped to disk - only valid in compile_only mode?
    4.82 -                if (context._dest_dir != null && context._compile_only) {
    4.83 +                if (env._dest_dir != null && env._compile_only) {
    4.84                      final String fileName = className.replace('.', File.separatorChar) + ".class";
    4.85                      final int    index    = fileName.lastIndexOf(File.separatorChar);
    4.86  
    4.87 @@ -287,7 +287,7 @@
    4.88                              if (!dir.exists() && !dir.mkdirs()) {
    4.89                                  throw new IOException(dir.toString());
    4.90                              }
    4.91 -                            final File file = new File(context._dest_dir, fileName);
    4.92 +                            final File file = new File(env._dest_dir, fileName);
    4.93                              try (final FileOutputStream fos = new FileOutputStream(file)) {
    4.94                                  fos.write(bytecode);
    4.95                              }
     5.1 --- a/src/jdk/nashorn/internal/codegen/Compiler.java	Fri Feb 22 23:33:46 2013 -0400
     5.2 +++ b/src/jdk/nashorn/internal/codegen/Compiler.java	Mon Feb 25 16:58:31 2013 +0530
     5.3 @@ -51,8 +51,8 @@
     5.4  import jdk.nashorn.internal.ir.FunctionNode;
     5.5  import jdk.nashorn.internal.ir.FunctionNode.CompilationState;
     5.6  import jdk.nashorn.internal.runtime.CodeInstaller;
     5.7 -import jdk.nashorn.internal.runtime.Context;
     5.8  import jdk.nashorn.internal.runtime.DebugLogger;
     5.9 +import jdk.nashorn.internal.runtime.ScriptEnvironment;
    5.10  import jdk.nashorn.internal.runtime.Source;
    5.11  import jdk.nashorn.internal.runtime.Timing;
    5.12  import jdk.nashorn.internal.runtime.options.Options;
    5.13 @@ -83,13 +83,13 @@
    5.14  
    5.15      private final CompilationSequence sequence;
    5.16  
    5.17 -    private final Context context;
    5.18 +    private final ScriptEnvironment env;
    5.19  
    5.20      private final String scriptName;
    5.21  
    5.22      private boolean strict;
    5.23  
    5.24 -    private CodeInstaller<Context> installer;
    5.25 +    private CodeInstaller<ScriptEnvironment> installer;
    5.26  
    5.27      /** logger for compiler, trampolines, splits and related code generation events
    5.28       *  that affect classes */
    5.29 @@ -190,14 +190,14 @@
    5.30      /**
    5.31       * Constructor
    5.32       *
    5.33 -     * @param installer    code installer from
    5.34 +     * @param installer    code installer
    5.35       * @param functionNode function node (in any available {@link CompilationState}) to compile
    5.36       * @param sequence     {@link Compiler#CompilationSequence} of {@link CompilationPhase}s to apply as this compilation
    5.37       * @param strict       should this compilation use strict mode semantics
    5.38       */
    5.39      //TODO support an array of FunctionNodes for batch lazy compilation
    5.40 -    Compiler(final Context context, final CodeInstaller<Context> installer, final FunctionNode functionNode, final CompilationSequence sequence, final boolean strict) {
    5.41 -        this.context       = context;
    5.42 +    Compiler(final ScriptEnvironment env, final CodeInstaller<ScriptEnvironment> installer, final FunctionNode functionNode, final CompilationSequence sequence, final boolean strict) {
    5.43 +        this.env           = env;
    5.44          this.functionNode  = functionNode;
    5.45          this.sequence      = sequence;
    5.46          this.installer     = installer;
    5.47 @@ -222,32 +222,32 @@
    5.48      /**
    5.49       * Constructor
    5.50       *
    5.51 -     * @param installer    code installer from context
    5.52 +     * @param installer    code installer
    5.53       * @param functionNode function node (in any available {@link CompilationState}) to compile
    5.54       * @param strict       should this compilation use strict mode semantics
    5.55       */
    5.56 -    public Compiler(final CodeInstaller<Context> installer, final FunctionNode functionNode, final boolean strict) {
    5.57 +    public Compiler(final CodeInstaller<ScriptEnvironment> installer, final FunctionNode functionNode, final boolean strict) {
    5.58          this(installer.getOwner(), installer, functionNode, SEQUENCE_DEFAULT, strict);
    5.59      }
    5.60  
    5.61      /**
    5.62 -     * Constructor - compilation will use the same strict semantics as context
    5.63 +     * Constructor - compilation will use the same strict semantics as in script environment
    5.64       *
    5.65 -     * @param installer    code installer from context
    5.66 +     * @param installer    code installer
    5.67       * @param functionNode function node (in any available {@link CompilationState}) to compile
    5.68       */
    5.69 -    public Compiler(final CodeInstaller<Context> installer, final FunctionNode functionNode) {
    5.70 +    public Compiler(final CodeInstaller<ScriptEnvironment> installer, final FunctionNode functionNode) {
    5.71          this(installer.getOwner(), installer, functionNode, SEQUENCE_DEFAULT, installer.getOwner()._strict);
    5.72      }
    5.73  
    5.74      /**
    5.75 -     * Constructor - compilation needs no installer, but uses a context
    5.76 +     * Constructor - compilation needs no installer, but uses a script environment
    5.77       * Used in "compile only" scenarios
    5.78 -     * @param context a context
    5.79 +     * @param env a script environment
    5.80       * @param functionNode functionNode to compile
    5.81       */
    5.82 -    public Compiler(final Context context, final FunctionNode functionNode) {
    5.83 -        this(context, null, functionNode, SEQUENCE_DEFAULT, context._strict);
    5.84 +    public Compiler(final ScriptEnvironment env, final FunctionNode functionNode) {
    5.85 +        this(env, null, functionNode, SEQUENCE_DEFAULT, env._strict);
    5.86      }
    5.87  
    5.88      /**
    5.89 @@ -345,7 +345,7 @@
    5.90          return constantData;
    5.91      }
    5.92  
    5.93 -    CodeInstaller<Context> getCodeInstaller() {
    5.94 +    CodeInstaller<ScriptEnvironment> getCodeInstaller() {
    5.95          return installer;
    5.96      }
    5.97  
    5.98 @@ -357,8 +357,8 @@
    5.99          bytecode.put(name, code);
   5.100      }
   5.101  
   5.102 -    Context getContext() {
   5.103 -        return this.context;
   5.104 +    ScriptEnvironment getEnv() {
   5.105 +        return this.env;
   5.106      }
   5.107  
   5.108      private static String safeSourceName(final Source source) {
   5.109 @@ -403,7 +403,7 @@
   5.110      }
   5.111  
   5.112      private CompileUnit initCompileUnit(final String unitClassName, final long initialWeight) {
   5.113 -        final ClassEmitter classEmitter = new ClassEmitter(context, functionNode.getSource().getName(), unitClassName, strict);
   5.114 +        final ClassEmitter classEmitter = new ClassEmitter(env, functionNode.getSource().getName(), unitClassName, strict);
   5.115          final CompileUnit  compileUnit  = new CompileUnit(unitClassName, classEmitter, initialWeight);
   5.116  
   5.117          classEmitter.begin();
     6.1 --- a/src/jdk/nashorn/internal/codegen/MethodEmitter.java	Fri Feb 22 23:33:46 2013 -0400
     6.2 +++ b/src/jdk/nashorn/internal/codegen/MethodEmitter.java	Mon Feb 25 16:58:31 2013 +0530
     6.3 @@ -84,10 +84,10 @@
     6.4  import jdk.nashorn.internal.ir.SplitNode;
     6.5  import jdk.nashorn.internal.ir.Symbol;
     6.6  import jdk.nashorn.internal.runtime.ArgumentSetter;
     6.7 -import jdk.nashorn.internal.runtime.Context;
     6.8  import jdk.nashorn.internal.runtime.DebugLogger;
     6.9  import jdk.nashorn.internal.runtime.JSType;
    6.10  import jdk.nashorn.internal.runtime.Scope;
    6.11 +import jdk.nashorn.internal.runtime.ScriptEnvironment;
    6.12  import jdk.nashorn.internal.runtime.ScriptObject;
    6.13  import jdk.nashorn.internal.runtime.linker.Bootstrap;
    6.14  import jdk.nashorn.internal.runtime.options.Options;
    6.15 @@ -121,8 +121,8 @@
    6.16      /** SplitNode representing the current split, or null if none exists */
    6.17      private SplitNode splitNode;
    6.18  
    6.19 -    /** The context */
    6.20 -    private final Context context;
    6.21 +    /** The script environment */
    6.22 +    private final ScriptEnvironment env;
    6.23  
    6.24      /** Threshold in chars for when string constants should be split */
    6.25      static final int LARGE_STRING_THRESHOLD = 32 * 1024;
    6.26 @@ -171,7 +171,7 @@
    6.27       * @param functionNode a function node representing this method
    6.28       */
    6.29      MethodEmitter(final ClassEmitter classEmitter, final MethodVisitor method, final FunctionNode functionNode) {
    6.30 -        this.context      = classEmitter.getContext();
    6.31 +        this.env          = classEmitter.getEnv();
    6.32          this.classEmitter = classEmitter;
    6.33          this.method       = method;
    6.34          this.functionNode = functionNode;
    6.35 @@ -2237,7 +2237,7 @@
    6.36                  sb.append(' ');
    6.37              }
    6.38  
    6.39 -            if (context != null) { //early bootstrap code doesn't have inited context yet
    6.40 +            if (env != null) { //early bootstrap code doesn't have inited context yet
    6.41                  LOG.info(sb.toString());
    6.42                  if (DEBUG_TRACE_LINE == linePrefix) {
    6.43                      new Throwable().printStackTrace(LOG.getOutputStream());
     7.1 --- a/src/jdk/nashorn/internal/codegen/ObjectClassGenerator.java	Fri Feb 22 23:33:46 2013 -0400
     7.2 +++ b/src/jdk/nashorn/internal/codegen/ObjectClassGenerator.java	Mon Feb 25 16:58:31 2013 +0530
     7.3 @@ -53,6 +53,7 @@
     7.4  import jdk.nashorn.internal.runtime.FunctionScope;
     7.5  import jdk.nashorn.internal.runtime.JSType;
     7.6  import jdk.nashorn.internal.runtime.PropertyMap;
     7.7 +import jdk.nashorn.internal.runtime.ScriptEnvironment;
     7.8  import jdk.nashorn.internal.runtime.ScriptObject;
     7.9  import jdk.nashorn.internal.runtime.ScriptRuntime;
    7.10  import jdk.nashorn.internal.runtime.options.Options;
    7.11 @@ -369,7 +370,7 @@
    7.12       * @return Open class emitter.
    7.13       */
    7.14      private ClassEmitter newClassEmitter(final String className, final String superName) {
    7.15 -        final ClassEmitter classEmitter = new ClassEmitter(context, className, superName);
    7.16 +        final ClassEmitter classEmitter = new ClassEmitter(context.getEnv(), className, superName);
    7.17          classEmitter.begin();
    7.18  
    7.19          return classEmitter;
    7.20 @@ -468,12 +469,13 @@
    7.21          classEmitter.end();
    7.22  
    7.23          final byte[] code = classEmitter.toByteArray();
    7.24 +        final ScriptEnvironment env = context.getEnv();
    7.25  
    7.26 -        if (context != null && context._print_code) {
    7.27 -            Context.getCurrentErr().println(ClassEmitter.disassemble(code));
    7.28 +        if (env._print_code) {
    7.29 +            env.getErr().println(ClassEmitter.disassemble(code));
    7.30          }
    7.31  
    7.32 -        if (context != null && context._verify_code) {
    7.33 +        if (env._verify_code) {
    7.34              context.verify(code);
    7.35          }
    7.36  
     8.1 --- a/src/jdk/nashorn/internal/ir/debug/JSONWriter.java	Fri Feb 22 23:33:46 2013 -0400
     8.2 +++ b/src/jdk/nashorn/internal/ir/debug/JSONWriter.java	Mon Feb 25 16:58:31 2013 +0530
     8.3 @@ -68,6 +68,7 @@
     8.4  import jdk.nashorn.internal.parser.TokenType;
     8.5  import jdk.nashorn.internal.runtime.Context;
     8.6  import jdk.nashorn.internal.runtime.ParserException;
     8.7 +import jdk.nashorn.internal.runtime.ScriptEnvironment;
     8.8  import jdk.nashorn.internal.runtime.Source;
     8.9  
    8.10  /**
    8.11 @@ -77,14 +78,14 @@
    8.12      /**
    8.13       * Returns AST as JSON compatible string.
    8.14       *
    8.15 -     * @param context nashorn context to use
    8.16 +     * @param env  script environment to use
    8.17       * @param code code to be parsed
    8.18       * @param name name of the code source (used for location)
    8.19       * @param includeLoc tells whether to include location information for nodes or not
    8.20       * @return JSON string representation of AST of the supplied code
    8.21       */
    8.22 -    public static String parse(final Context context, final String code, final String name, final boolean includeLoc) {
    8.23 -        final Parser       parser     = new Parser(context, new Source(name, code), new Context.ThrowErrorManager(), context._strict);
    8.24 +    public static String parse(final ScriptEnvironment env, final String code, final String name, final boolean includeLoc) {
    8.25 +        final Parser       parser     = new Parser(env, new Source(name, code), new Context.ThrowErrorManager(), env._strict);
    8.26          final JSONWriter   jsonWriter = new JSONWriter(includeLoc);
    8.27          try {
    8.28              final FunctionNode functionNode = parser.parse(CompilerConstants.RUN_SCRIPT.tag());
     9.1 --- a/src/jdk/nashorn/internal/objects/Global.java	Fri Feb 22 23:33:46 2013 -0400
     9.2 +++ b/src/jdk/nashorn/internal/objects/Global.java	Mon Feb 25 16:58:31 2013 +0530
     9.3 @@ -48,7 +48,7 @@
     9.4  import jdk.nashorn.internal.runtime.GlobalObject;
     9.5  import jdk.nashorn.internal.runtime.JSType;
     9.6  import jdk.nashorn.internal.runtime.NativeJavaPackage;
     9.7 -import jdk.nashorn.internal.runtime.OptionsObject;
     9.8 +import jdk.nashorn.internal.runtime.ScriptEnvironment;
     9.9  import jdk.nashorn.internal.runtime.PropertyDescriptor;
    9.10  import jdk.nashorn.internal.runtime.regexp.RegExpResult;
    9.11  import jdk.nashorn.internal.runtime.Scope;
    9.12 @@ -364,7 +364,7 @@
    9.13           */
    9.14          this.setMap(getMap().duplicate());
    9.15  
    9.16 -        final int cacheSize = context._class_cache_size;
    9.17 +        final int cacheSize = context.getEnv()._class_cache_size;
    9.18          if (cacheSize > 0) {
    9.19              classCache = new ClassCache(cacheSize);
    9.20          }
    9.21 @@ -384,6 +384,15 @@
    9.22      }
    9.23  
    9.24      /**
    9.25 +     * Script access to {@link ScriptEnvironment}
    9.26 +     *
    9.27 +     * @return the script environment
    9.28 +     */
    9.29 +    static ScriptEnvironment getEnv() {
    9.30 +        return instance().context.getEnv();
    9.31 +    }
    9.32 +
    9.33 +    /**
    9.34       * Script access to {@link Context}
    9.35       *
    9.36       * @return the context
    9.37 @@ -398,7 +407,7 @@
    9.38       * @return true if strict mode enabled in {@link Global#getThisContext()}
    9.39       */
    9.40      static boolean isStrict() {
    9.41 -        return getThisContext()._strict;
    9.42 +        return getEnv()._strict;
    9.43      }
    9.44  
    9.45      // GlobalObject interface implementation
    9.46 @@ -578,7 +587,7 @@
    9.47      public PropertyDescriptor newAccessorDescriptor(final Object get, final Object set, final boolean configurable, final boolean enumerable) {
    9.48          final AccessorPropertyDescriptor desc = new AccessorPropertyDescriptor(configurable, enumerable, get == null ? UNDEFINED : get, set == null ? UNDEFINED : set);
    9.49  
    9.50 -        final boolean strict = context._strict;
    9.51 +        final boolean strict = context.getEnv()._strict;
    9.52  
    9.53          if (get == null) {
    9.54              desc.delete(PropertyDescriptor.GET, strict);
    9.55 @@ -1333,6 +1342,7 @@
    9.56      private void init() {
    9.57          assert Context.getGlobal() == this : "this global is not set as current";
    9.58  
    9.59 +        final ScriptEnvironment env = context.getEnv();
    9.60          // initialize Function and Object constructor
    9.61          initFunctionAndObject();
    9.62  
    9.63 @@ -1352,7 +1362,7 @@
    9.64          this.decodeURIComponent = ScriptFunctionImpl.makeFunction("decodeURIComponent", GlobalFunctions.DECODE_URICOMPONENT);
    9.65          this.escape             = ScriptFunctionImpl.makeFunction("escape",     GlobalFunctions.ESCAPE);
    9.66          this.unescape           = ScriptFunctionImpl.makeFunction("unescape",   GlobalFunctions.UNESCAPE);
    9.67 -        this.print              = ScriptFunctionImpl.makeFunction("print",      context._print_no_newline ? PRINT : PRINTLN);
    9.68 +        this.print              = ScriptFunctionImpl.makeFunction("print",      env._print_no_newline ? PRINT : PRINTLN);
    9.69          this.load               = ScriptFunctionImpl.makeFunction("load",       LOAD);
    9.70          this.exit               = ScriptFunctionImpl.makeFunction("exit",       EXIT);
    9.71          this.quit               = ScriptFunctionImpl.makeFunction("quit",       EXIT);
    9.72 @@ -1395,7 +1405,7 @@
    9.73  
    9.74          initTypedArray();
    9.75  
    9.76 -        if (context._scripting) {
    9.77 +        if (env._scripting) {
    9.78              initScripting();
    9.79          }
    9.80  
    9.81 @@ -1411,11 +1421,11 @@
    9.82          this.__LINE__ = 0.0;
    9.83  
    9.84          // expose script (command line) arguments as "arguments" property of global
    9.85 -        final List<String> arguments = context.getOptions().getArguments();
    9.86 +        final List<String> arguments = env.getArguments();
    9.87          final Object argsObj = wrapAsObject(arguments.toArray());
    9.88  
    9.89          addOwnProperty("arguments", Attribute.NOT_ENUMERABLE, argsObj);
    9.90 -        if (context._scripting) {
    9.91 +        if (env._scripting) {
    9.92              // synonym for "arguments" in scripting mode
    9.93              addOwnProperty("$ARG", Attribute.NOT_ENUMERABLE, argsObj);
    9.94          }
    9.95 @@ -1493,7 +1503,7 @@
    9.96          addOwnProperty("echo", Attribute.NOT_ENUMERABLE, value);
    9.97  
    9.98          // Nashorn extension: global.$OPTIONS (scripting-mode-only)
    9.99 -        value = new OptionsObject(context);
   9.100 +        value = context.getEnv();
   9.101          addOwnProperty("$OPTIONS", Attribute.NOT_ENUMERABLE, value);
   9.102  
   9.103          // Nashorn extension: global.$ENV (scripting-mode-only)
   9.104 @@ -1568,7 +1578,7 @@
   9.105  
   9.106      @SuppressWarnings("resource")
   9.107      private static Object printImpl(final boolean newLine, final Object... objects) {
   9.108 -        final PrintWriter out = Global.getThisContext().getOut();
   9.109 +        final PrintWriter out = Global.getEnv().getOut();
   9.110  
   9.111          boolean first = true;
   9.112          for (final Object object : objects) {
    10.1 --- a/src/jdk/nashorn/internal/objects/NativeDate.java	Fri Feb 22 23:33:46 2013 -0400
    10.2 +++ b/src/jdk/nashorn/internal/objects/NativeDate.java	Mon Feb 25 16:58:31 2013 +0530
    10.3 @@ -40,8 +40,8 @@
    10.4  import jdk.nashorn.internal.objects.annotations.SpecializedConstructor;
    10.5  import jdk.nashorn.internal.objects.annotations.Where;
    10.6  import jdk.nashorn.internal.runtime.ConsString;
    10.7 -import jdk.nashorn.internal.runtime.Context;
    10.8  import jdk.nashorn.internal.runtime.JSType;
    10.9 +import jdk.nashorn.internal.runtime.ScriptEnvironment;
   10.10  import jdk.nashorn.internal.runtime.ScriptFunction;
   10.11  import jdk.nashorn.internal.runtime.ScriptObject;
   10.12  import jdk.nashorn.internal.runtime.ScriptRuntime;
   10.13 @@ -104,10 +104,10 @@
   10.14      }
   10.15  
   10.16      NativeDate(final double time) {
   10.17 -        final Context context = Global.getThisContext();
   10.18 +        final ScriptEnvironment env = Global.getEnv();
   10.19  
   10.20          this.time = time;
   10.21 -        this.timezone = context.getTimeZone();
   10.22 +        this.timezone = env._timezone;
   10.23          this.setProto(Global.instance().getDatePrototype());
   10.24      }
   10.25  
   10.26 @@ -886,7 +886,7 @@
   10.27              if (fields[DateParser.TIMEZONE] != null) {
   10.28                  d -= fields[DateParser.TIMEZONE] * 60000;
   10.29              } else {
   10.30 -                d = utc(d, Global.getThisContext().getTimeZone());
   10.31 +                d = utc(d, Global.getEnv()._timezone);
   10.32              }
   10.33              d = timeClip(d);
   10.34              return d;
    11.1 --- a/src/jdk/nashorn/internal/objects/NativeString.java	Fri Feb 22 23:33:46 2013 -0400
    11.2 +++ b/src/jdk/nashorn/internal/objects/NativeString.java	Mon Feb 25 16:58:31 2013 +0530
    11.3 @@ -641,7 +641,7 @@
    11.4      public static Object localeCompare(final Object self, final Object that) {
    11.5  
    11.6          final String   str      = checkObjectToString(self);
    11.7 -        final Collator collator = Collator.getInstance(Global.getThisContext().getLocale());
    11.8 +        final Collator collator = Collator.getInstance(Global.getEnv()._locale);
    11.9  
   11.10          collator.setStrength(Collator.IDENTICAL);
   11.11          collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
   11.12 @@ -996,7 +996,7 @@
   11.13       */
   11.14      @Function(attributes = Attribute.NOT_ENUMERABLE)
   11.15      public static Object toLocaleLowerCase(final Object self) {
   11.16 -        return checkObjectToString(self).toLowerCase(Global.getThisContext().getLocale());
   11.17 +        return checkObjectToString(self).toLowerCase(Global.getEnv()._locale);
   11.18      }
   11.19  
   11.20      /**
   11.21 @@ -1016,7 +1016,7 @@
   11.22       */
   11.23      @Function(attributes = Attribute.NOT_ENUMERABLE)
   11.24      public static Object toLocaleUpperCase(final Object self) {
   11.25 -        return checkObjectToString(self).toUpperCase(Global.getThisContext().getLocale());
   11.26 +        return checkObjectToString(self).toUpperCase(Global.getEnv()._locale);
   11.27      }
   11.28  
   11.29      /**
    12.1 --- a/src/jdk/nashorn/internal/objects/ScriptFunctionTrampolineImpl.java	Fri Feb 22 23:33:46 2013 -0400
    12.2 +++ b/src/jdk/nashorn/internal/objects/ScriptFunctionTrampolineImpl.java	Mon Feb 25 16:58:31 2013 +0530
    12.3 @@ -12,6 +12,7 @@
    12.4  import jdk.nashorn.internal.ir.FunctionNode;
    12.5  import jdk.nashorn.internal.runtime.CodeInstaller;
    12.6  import jdk.nashorn.internal.runtime.Context;
    12.7 +import jdk.nashorn.internal.runtime.ScriptEnvironment;
    12.8  import jdk.nashorn.internal.runtime.ScriptFunction;
    12.9  import jdk.nashorn.internal.runtime.ScriptFunctionData;
   12.10  import jdk.nashorn.internal.runtime.ScriptObject;
   12.11 @@ -23,7 +24,7 @@
   12.12   */
   12.13  public final class ScriptFunctionTrampolineImpl extends ScriptFunctionImpl {
   12.14  
   12.15 -    private CodeInstaller<Context> installer;
   12.16 +    private CodeInstaller<ScriptEnvironment> installer;
   12.17  
   12.18      /** Function node to lazily recompile when trampoline is hit */
   12.19      private FunctionNode functionNode;
   12.20 @@ -37,7 +38,7 @@
   12.21       * @param scope        scope
   12.22       * @param allocator    allocator
   12.23       */
   12.24 -    public ScriptFunctionTrampolineImpl(final CodeInstaller<Context> installer, final FunctionNode functionNode, final ScriptFunctionData data, final ScriptObject scope, final MethodHandle allocator) {
   12.25 +    public ScriptFunctionTrampolineImpl(final CodeInstaller<ScriptEnvironment> installer, final FunctionNode functionNode, final ScriptFunctionData data, final ScriptObject scope, final MethodHandle allocator) {
   12.26          super(null, data, scope, allocator);
   12.27  
   12.28          this.installer    = installer;
    13.1 --- a/src/jdk/nashorn/internal/parser/Parser.java	Fri Feb 22 23:33:46 2013 -0400
    13.2 +++ b/src/jdk/nashorn/internal/parser/Parser.java	Mon Feb 25 16:58:31 2013 +0530
    13.3 @@ -101,6 +101,7 @@
    13.4  import jdk.nashorn.internal.runtime.ErrorManager;
    13.5  import jdk.nashorn.internal.runtime.JSErrorType;
    13.6  import jdk.nashorn.internal.runtime.ParserException;
    13.7 +import jdk.nashorn.internal.runtime.ScriptEnvironment;
    13.8  import jdk.nashorn.internal.runtime.ScriptingFunctions;
    13.9  import jdk.nashorn.internal.runtime.Source;
   13.10  import jdk.nashorn.internal.runtime.Timing;
   13.11 @@ -109,8 +110,8 @@
   13.12   * Builds the IR.
   13.13   */
   13.14  public class Parser extends AbstractParser {
   13.15 -    /** Current context. */
   13.16 -    private final Context context;
   13.17 +    /** Current script environment. */
   13.18 +    private final ScriptEnvironment env;
   13.19  
   13.20      /** Is scripting mode. */
   13.21      private final boolean scripting;
   13.22 @@ -132,27 +133,27 @@
   13.23      /**
   13.24       * Constructor
   13.25       *
   13.26 -     * @param context parser context
   13.27 +     * @param env     script environment
   13.28       * @param source  source to parse
   13.29       * @param errors  error manager
   13.30       */
   13.31 -    public Parser(final Context context, final Source source, final ErrorManager errors) {
   13.32 -        this(context, source, errors, context._strict);
   13.33 +    public Parser(final ScriptEnvironment env, final Source source, final ErrorManager errors) {
   13.34 +        this(env, source, errors, env._strict);
   13.35      }
   13.36  
   13.37      /**
   13.38       * Construct a parser.
   13.39       *
   13.40 -     * @param context parser context
   13.41 +     * @param env     script environment
   13.42       * @param source  source to parse
   13.43       * @param errors  error manager
   13.44       * @param strict  parser created with strict mode enabled.
   13.45       */
   13.46 -    public Parser(final Context context, final Source source, final ErrorManager errors, final boolean strict) {
   13.47 +    public Parser(final ScriptEnvironment env, final Source source, final ErrorManager errors, final boolean strict) {
   13.48          super(source, errors, strict);
   13.49 -        this.context   = context;
   13.50 -        this.namespace = new Namespace(context.getNamespace());
   13.51 -        this.scripting = context._scripting;
   13.52 +        this.env   = env;
   13.53 +        this.namespace = new Namespace(env.getNamespace());
   13.54 +        this.scripting = env._scripting;
   13.55      }
   13.56  
   13.57      /**
   13.58 @@ -184,7 +185,7 @@
   13.59  
   13.60          try {
   13.61              stream = new TokenStream();
   13.62 -            lexer  = new Lexer(source, stream, scripting && !context._no_syntax_extensions);
   13.63 +            lexer  = new Lexer(source, stream, scripting && !env._no_syntax_extensions);
   13.64  
   13.65              // Set up first token (skips opening EOL.)
   13.66              k = -1;
   13.67 @@ -209,8 +210,8 @@
   13.68                  errors.error(message);
   13.69              }
   13.70  
   13.71 -            if (context._dump_on_error) {
   13.72 -                e.printStackTrace(context.getErr());
   13.73 +            if (env._dump_on_error) {
   13.74 +                e.printStackTrace(env.getErr());
   13.75              }
   13.76  
   13.77              return null;
   13.78 @@ -246,8 +247,8 @@
   13.79                  errors.error(message);
   13.80              }
   13.81  
   13.82 -            if (context._dump_on_error) {
   13.83 -                e.printStackTrace(context.getErr());
   13.84 +            if (env._dump_on_error) {
   13.85 +                e.printStackTrace(env.getErr());
   13.86              }
   13.87          }
   13.88  
   13.89 @@ -430,7 +431,7 @@
   13.90              if (!(lhs instanceof AccessNode ||
   13.91                    lhs instanceof IndexNode ||
   13.92                    lhs instanceof IdentNode)) {
   13.93 -                if (context._early_lvalue_error) {
   13.94 +                if (env._early_lvalue_error) {
   13.95                      error(JSErrorType.REFERENCE_ERROR, AbstractParser.message("invalid.lvalue"), lhs.getToken());
   13.96                  }
   13.97                  return referenceError(lhs, rhs);
   13.98 @@ -1018,7 +1019,7 @@
   13.99       * Parse an empty statement.
  13.100       */
  13.101      private void emptyStatement() {
  13.102 -        if (context._empty_statements) {
  13.103 +        if (env._empty_statements) {
  13.104              block.addStatement(new EmptyNode(source, token,
  13.105                      Token.descPosition(token) + Token.descLength(token)));
  13.106          }
  13.107 @@ -1126,7 +1127,7 @@
  13.108  
  13.109              // Nashorn extension: for each expression.
  13.110              // iterate property values rather than property names.
  13.111 -            if (!context._no_syntax_extensions && type == IDENT && "each".equals(getValue())) {
  13.112 +            if (!env._no_syntax_extensions && type == IDENT && "each".equals(getValue())) {
  13.113                  forNode.setIsForEach();
  13.114                  next();
  13.115              }
  13.116 @@ -2416,7 +2417,7 @@
  13.117          // The object literal following the "new Constructor()" expresssion
  13.118          // is passed as an additional (last) argument to the constructor.
  13.119  
  13.120 -        if (!context._no_syntax_extensions && type == LBRACE) {
  13.121 +        if (!env._no_syntax_extensions && type == LBRACE) {
  13.122              arguments.add(objectLiteral());
  13.123          }
  13.124  
  13.125 @@ -2572,7 +2573,7 @@
  13.126              verifyStrictIdent(name, "function name");
  13.127          } else if (isStatement) {
  13.128              // Nashorn extension: anonymous function statements
  13.129 -            if (context._no_syntax_extensions || !context._anon_functions) {
  13.130 +            if (env._no_syntax_extensions || !env._anon_functions) {
  13.131                  expect(IDENT);
  13.132              }
  13.133          }
  13.134 @@ -2710,7 +2711,7 @@
  13.135              functionNode.setFirstToken(firstToken);
  13.136  
  13.137              // Nashorn extension: expression closures
  13.138 -            if (!context._no_syntax_extensions && type != LBRACE) {
  13.139 +            if (!env._no_syntax_extensions && type != LBRACE) {
  13.140                  /*
  13.141                   * Example:
  13.142                   *
  13.143 @@ -3072,7 +3073,7 @@
  13.144       * Add a line number node at current position
  13.145       */
  13.146      private LineNumberNode lineNumber() {
  13.147 -        if (context._debug_lines) {
  13.148 +        if (env._debug_lines) {
  13.149              return new LineNumberNode(source, token, line);
  13.150          }
  13.151          return null;
    14.1 --- a/src/jdk/nashorn/internal/runtime/CodeInstaller.java	Fri Feb 22 23:33:46 2013 -0400
    14.2 +++ b/src/jdk/nashorn/internal/runtime/CodeInstaller.java	Mon Feb 25 16:58:31 2013 +0530
    14.3 @@ -51,4 +51,13 @@
    14.4       * @return the installed class
    14.5       */
    14.6      public Class<?> install(final String className, final byte[] bytecode);
    14.7 +
    14.8 +    /*
    14.9 +     * Verify generated bytecode before emission. This is called back from the
   14.10 +     * {@link ClassEmitter} or the {@link Compiler}. If the "--verify-code" parameter
   14.11 +     * hasn't been given, this is a nop
   14.12 +     *
   14.13 +     * @param bytecode bytecode to verify
   14.14 +     */
   14.15 +    public void verify(final byte[] code);
   14.16  }
    15.1 --- a/src/jdk/nashorn/internal/runtime/Context.java	Fri Feb 22 23:33:46 2013 -0400
    15.2 +++ b/src/jdk/nashorn/internal/runtime/Context.java	Mon Feb 25 16:58:31 2013 +0530
    15.3 @@ -43,22 +43,15 @@
    15.4  import java.security.CodeSigner;
    15.5  import java.security.CodeSource;
    15.6  import java.security.PrivilegedAction;
    15.7 -import java.util.Locale;
    15.8 -import java.util.TimeZone;
    15.9  import jdk.internal.org.objectweb.asm.ClassReader;
   15.10  import jdk.internal.org.objectweb.asm.util.CheckClassAdapter;
   15.11 -import jdk.nashorn.internal.codegen.ClassEmitter;
   15.12  import jdk.nashorn.internal.codegen.Compiler;
   15.13 -import jdk.nashorn.internal.codegen.Namespace;
   15.14  import jdk.nashorn.internal.codegen.ObjectClassGenerator;
   15.15  import jdk.nashorn.internal.ir.FunctionNode;
   15.16  import jdk.nashorn.internal.ir.debug.ASTWriter;
   15.17  import jdk.nashorn.internal.ir.debug.PrintVisitor;
   15.18  import jdk.nashorn.internal.parser.Parser;
   15.19  import jdk.nashorn.internal.runtime.linker.JavaAdapterFactory;
   15.20 -import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
   15.21 -import jdk.nashorn.internal.runtime.options.KeyValueOption;
   15.22 -import jdk.nashorn.internal.runtime.options.Option;
   15.23  import jdk.nashorn.internal.runtime.options.Options;
   15.24  import sun.reflect.Reflection;
   15.25  
   15.26 @@ -71,7 +64,7 @@
   15.27       * ContextCodeInstaller that has the privilege of installing classes in the Context.
   15.28       * Can only be instantiated from inside the context and is opaque to other classes
   15.29       */
   15.30 -    public static class ContextCodeInstaller implements CodeInstaller<Context> {
   15.31 +    public static class ContextCodeInstaller implements CodeInstaller<ScriptEnvironment> {
   15.32          private final Context      context;
   15.33          private final ScriptLoader loader;
   15.34          private final CodeSource   codeSource;
   15.35 @@ -87,14 +80,19 @@
   15.36           * @return context
   15.37           */
   15.38          @Override
   15.39 -        public Context getOwner() {
   15.40 -            return context;
   15.41 +        public ScriptEnvironment getOwner() {
   15.42 +            return context.env;
   15.43          }
   15.44  
   15.45          @Override
   15.46          public Class<?> install(final String className, final byte[] bytecode) {
   15.47              return loader.installClass(className, bytecode, codeSource);
   15.48          }
   15.49 +
   15.50 +        @Override
   15.51 +        public void verify(final byte[] code) {
   15.52 +            context.verify(code);
   15.53 +        }
   15.54      }
   15.55  
   15.56      /** Is Context global debug mode enabled ? */
   15.57 @@ -198,6 +196,12 @@
   15.58          }
   15.59      }
   15.60  
   15.61 +    /** Current environment. */
   15.62 +    private final ScriptEnvironment env;
   15.63 +
   15.64 +    /** is this context in strict mode? Cached from env. as this is used heavily. */
   15.65 +    public final boolean _strict;
   15.66 +
   15.67      /** class loader to resolve classes from script. */
   15.68      private final ClassLoader  appLoader;
   15.69  
   15.70 @@ -207,108 +211,12 @@
   15.71      /** Class loader to load classes compiled from scripts. */
   15.72      private final ScriptLoader scriptLoader;
   15.73  
   15.74 -    /** Top level namespace. */
   15.75 -    private final Namespace namespace;
   15.76 -
   15.77 -    /** Current options. */
   15.78 -    private final Options options;
   15.79 -
   15.80      /** Current error manager. */
   15.81      private final ErrorManager errors;
   15.82  
   15.83 -    /** Output writer for this context */
   15.84 -    private final PrintWriter out;
   15.85 -
   15.86 -    /** Error writer for this context */
   15.87 -    private final PrintWriter err;
   15.88 -
   15.89 -    /** Local for error messages */
   15.90 -    private final Locale locale;
   15.91 -
   15.92      /** Empty map used for seed map for JO$ objects */
   15.93      final PropertyMap emptyMap = PropertyMap.newEmptyMap(this);
   15.94  
   15.95 -    // cache fields for "well known" options.
   15.96 -    // see jdk.nashorn.internal.runtime.Resources
   15.97 -
   15.98 -    /** Always allow functions as statements */
   15.99 -    public final boolean _anon_functions;
  15.100 -
  15.101 -    /** Size of the per-global Class cache size */
  15.102 -    public final int     _class_cache_size;
  15.103 -
  15.104 -    /** Only compile script, do not run it or generate other ScriptObjects */
  15.105 -    public final boolean _compile_only;
  15.106 -
  15.107 -    /** Accumulated callsite flags that will be used when boostrapping script callsites */
  15.108 -    public final int     _callsite_flags;
  15.109 -
  15.110 -    /** Genereate line number table in class files */
  15.111 -    public final boolean _debug_lines;
  15.112 -
  15.113 -    /** Package to which generated class files are added */
  15.114 -    public final String  _dest_dir;
  15.115 -
  15.116 -    /** Display stack trace upon error, default is false */
  15.117 -    public final boolean _dump_on_error;
  15.118 -
  15.119 -    /** Invalid lvalue expressions should be reported as early errors */
  15.120 -    public final boolean _early_lvalue_error;
  15.121 -
  15.122 -    /** Empty statements should be preserved in the AST */
  15.123 -    public final boolean _empty_statements;
  15.124 -
  15.125 -    /** Show full Nashorn version */
  15.126 -    public final boolean _fullversion;
  15.127 -
  15.128 -    /** Create a new class loaded for each compilation */
  15.129 -    public final boolean _loader_per_compile;
  15.130 -
  15.131 -    /** Do not support non-standard syntax extensions. */
  15.132 -    public final boolean _no_syntax_extensions;
  15.133 -
  15.134 -    /** Package to which generated class files are added */
  15.135 -    public final String  _package;
  15.136 -
  15.137 -    /** Only parse the source code, do not compile */
  15.138 -    public final boolean _parse_only;
  15.139 -
  15.140 -    /** Print the AST before lowering */
  15.141 -    public final boolean _print_ast;
  15.142 -
  15.143 -    /** Print the AST after lowering */
  15.144 -    public final boolean _print_lower_ast;
  15.145 -
  15.146 -    /** Print resulting bytecode for script */
  15.147 -    public final boolean _print_code;
  15.148 -
  15.149 -    /** Print function will no print newline characters */
  15.150 -    public final boolean _print_no_newline;
  15.151 -
  15.152 -    /** Print AST in more human readable form */
  15.153 -    public final boolean _print_parse;
  15.154 -
  15.155 -    /** Print AST in more human readable form after Lowering */
  15.156 -    public final boolean _print_lower_parse;
  15.157 -
  15.158 -    /** print symbols and their contents for the script */
  15.159 -    public final boolean _print_symbols;
  15.160 -
  15.161 -    /** is this context in scripting mode? */
  15.162 -    public final boolean _scripting;
  15.163 -
  15.164 -    /** is this context in strict mode? */
  15.165 -    public final boolean _strict;
  15.166 -
  15.167 -    /** print version info of Nashorn */
  15.168 -    public final boolean _version;
  15.169 -
  15.170 -    /** should code verification be done of generated bytecode */
  15.171 -    public final boolean _verify_code;
  15.172 -
  15.173 -    /** time zone for this context */
  15.174 -    public final TimeZone _timezone;
  15.175 -
  15.176      private static final ClassLoader myLoader = Context.class.getClassLoader();
  15.177      private static final StructureLoader sharedLoader;
  15.178  
  15.179 @@ -362,6 +270,8 @@
  15.180              sm.checkPermission(new RuntimePermission("createNashornContext"));
  15.181          }
  15.182  
  15.183 +        this.env       = new ScriptEnvironment(options, out, err);
  15.184 +        this._strict   = env._strict;
  15.185          this.appLoader = appLoader;
  15.186          this.scriptLoader = (ScriptLoader)AccessController.doPrivileged(
  15.187               new PrivilegedAction<ClassLoader>() {
  15.188 @@ -371,73 +281,12 @@
  15.189                      return new ScriptLoader(structureLoader, Context.this);
  15.190                  }
  15.191               });
  15.192 -
  15.193 -        this.namespace = new Namespace();
  15.194 -        this.options   = options;
  15.195          this.errors    = errors;
  15.196 -        this.locale    = Locale.getDefault();
  15.197 -        this.out       = out;
  15.198 -        this.err       = err;
  15.199 -
  15.200 -        _anon_functions       = options.getBoolean("anon.functions");
  15.201 -        _class_cache_size     = options.getInteger("class.cache.size");
  15.202 -        _compile_only         = options.getBoolean("compile.only");
  15.203 -        _debug_lines          = options.getBoolean("debug.lines");
  15.204 -        _dest_dir             = options.getString("d");
  15.205 -        _dump_on_error        = options.getBoolean("doe");
  15.206 -        _early_lvalue_error   = options.getBoolean("early.lvalue.error");
  15.207 -        _empty_statements     = options.getBoolean("empty.statements");
  15.208 -        _fullversion          = options.getBoolean("fullversion");
  15.209 -        _loader_per_compile   = options.getBoolean("loader.per.compile");
  15.210 -        _no_syntax_extensions = options.getBoolean("no.syntax.extensions");
  15.211 -        _package              = options.getString("package");
  15.212 -        _parse_only           = options.getBoolean("parse.only");
  15.213 -        _print_ast            = options.getBoolean("print.ast");
  15.214 -        _print_lower_ast      = options.getBoolean("print.lower.ast");
  15.215 -        _print_code           = options.getBoolean("print.code");
  15.216 -        _print_no_newline     = options.getBoolean("print.no.newline");
  15.217 -        _print_parse          = options.getBoolean("print.parse");
  15.218 -        _print_lower_parse    = options.getBoolean("print.lower.parse");
  15.219 -        _print_symbols        = options.getBoolean("print.symbols");
  15.220 -        _scripting            = options.getBoolean("scripting");
  15.221 -        _strict               = options.getBoolean("strict");
  15.222 -        _version              = options.getBoolean("version");
  15.223 -        _verify_code          = options.getBoolean("verify.code");
  15.224 -
  15.225 -        int callSiteFlags = 0;
  15.226 -        if (options.getBoolean("profile.callsites")) {
  15.227 -            callSiteFlags |= NashornCallSiteDescriptor.CALLSITE_PROFILE;
  15.228 -        }
  15.229 -
  15.230 -        if (options.get("trace.callsites") instanceof KeyValueOption) {
  15.231 -            callSiteFlags |= NashornCallSiteDescriptor.CALLSITE_TRACE;
  15.232 -            final KeyValueOption kv = (KeyValueOption)options.get("trace.callsites");
  15.233 -            if (kv.hasValue("miss")) {
  15.234 -                callSiteFlags |= NashornCallSiteDescriptor.CALLSITE_TRACE_MISSES;
  15.235 -            }
  15.236 -            if (kv.hasValue("enterexit") || (callSiteFlags & NashornCallSiteDescriptor.CALLSITE_TRACE_MISSES) == 0) {
  15.237 -                callSiteFlags |= NashornCallSiteDescriptor.CALLSITE_TRACE_ENTEREXIT;
  15.238 -            }
  15.239 -            if (kv.hasValue("objects")) {
  15.240 -                callSiteFlags |= NashornCallSiteDescriptor.CALLSITE_TRACE_VALUES;
  15.241 -            }
  15.242 -            if (kv.hasValue("scope")) {
  15.243 -                callSiteFlags |= NashornCallSiteDescriptor.CALLSITE_TRACE_SCOPE;
  15.244 -            }
  15.245 -        }
  15.246 -        this._callsite_flags = callSiteFlags;
  15.247 -
  15.248 -        final Option<?> option = options.get("timezone");
  15.249 -        if (option != null) {
  15.250 -            this._timezone = (TimeZone)option.getValue();
  15.251 -        } else {
  15.252 -            this._timezone  = TimeZone.getDefault();
  15.253 -        }
  15.254  
  15.255          // if user passed -classpath option, make a class loader with that and set it as
  15.256          // thread context class loader so that script can access classes from that path.
  15.257          final String classPath = options.getString("classpath");
  15.258 -        if (! _compile_only && classPath != null && !classPath.isEmpty()) {
  15.259 +        if (! env._compile_only && classPath != null && !classPath.isEmpty()) {
  15.260              // make sure that caller can create a class loader.
  15.261              if (sm != null) {
  15.262                  sm.checkPermission(new RuntimePermission("createClassLoader"));
  15.263 @@ -448,11 +297,11 @@
  15.264          }
  15.265  
  15.266          // print version info if asked.
  15.267 -        if (_version) {
  15.268 +        if (env._version) {
  15.269              getErr().println("nashorn " + Version.version());
  15.270          }
  15.271  
  15.272 -        if (_fullversion) {
  15.273 +        if (env._fullversion) {
  15.274              getErr().println("nashorn full version " + Version.fullVersion());
  15.275          }
  15.276      }
  15.277 @@ -466,11 +315,19 @@
  15.278      }
  15.279  
  15.280      /**
  15.281 +     * Get the script environment for this context
  15.282 +     * @return script environment
  15.283 +     */
  15.284 +    public ScriptEnvironment getEnv() {
  15.285 +        return env;
  15.286 +    }
  15.287 +
  15.288 +    /**
  15.289       * Get the output stream for this context
  15.290       * @return output print writer
  15.291       */
  15.292      public PrintWriter getOut() {
  15.293 -        return out;
  15.294 +        return env.getOut();
  15.295      }
  15.296  
  15.297      /**
  15.298 @@ -478,39 +335,7 @@
  15.299       * @return error print writer
  15.300       */
  15.301      public PrintWriter getErr() {
  15.302 -        return err;
  15.303 -    }
  15.304 -
  15.305 -    /**
  15.306 -     * Get the namespace for this context
  15.307 -     * @return namespace
  15.308 -     */
  15.309 -    public Namespace getNamespace() {
  15.310 -        return namespace;
  15.311 -    }
  15.312 -
  15.313 -    /**
  15.314 -     * Get the options given to this context
  15.315 -     * @return options
  15.316 -     */
  15.317 -    public Options getOptions() {
  15.318 -        return options;
  15.319 -    }
  15.320 -
  15.321 -    /**
  15.322 -     * Get the locale for this context
  15.323 -     * @return locale
  15.324 -     */
  15.325 -    public Locale getLocale() {
  15.326 -        return locale;
  15.327 -    }
  15.328 -
  15.329 -    /**
  15.330 -     * Get the time zone for this context
  15.331 -     * @return time zone
  15.332 -     */
  15.333 -    public TimeZone getTimeZone() {
  15.334 -        return _timezone;
  15.335 +        return env.getErr();
  15.336      }
  15.337  
  15.338      /**
  15.339 @@ -739,7 +564,7 @@
  15.340  
  15.341      /**
  15.342       * Verify generated bytecode before emission. This is called back from the
  15.343 -     * {@link ClassEmitter} or the {@link Compiler}. If the "--verify-code" parameter
  15.344 +     * {@link ObjectClassGenerator} or the {@link Compiler}. If the "--verify-code" parameter
  15.345       * hasn't been given, this is a nop
  15.346       *
  15.347       * Note that verification may load classes -- we don't want to do that unless
  15.348 @@ -749,7 +574,7 @@
  15.349       * @param bytecode bytecode to verify
  15.350       */
  15.351      public void verify(final byte[] bytecode) {
  15.352 -        if (_verify_code) {
  15.353 +        if (env._verify_code) {
  15.354              // No verification when security manager is around as verifier
  15.355              // may load further classes - which should be avoided.
  15.356              if (System.getSecurityManager() == null) {
  15.357 @@ -792,7 +617,7 @@
  15.358          }
  15.359  
  15.360          // Need only minimal global object, if we are just compiling.
  15.361 -        if (!_compile_only) {
  15.362 +        if (!env._compile_only) {
  15.363              final ScriptObject oldGlobal = Context.getGlobalTrusted();
  15.364              try {
  15.365                  Context.setGlobalTrusted(global);
  15.366 @@ -902,7 +727,7 @@
  15.367          GlobalObject global = null;
  15.368          Class<?> script;
  15.369  
  15.370 -        if (_class_cache_size > 0) {
  15.371 +        if (env._class_cache_size > 0) {
  15.372              global = (GlobalObject)Context.getGlobalTrusted();
  15.373              script = global.findCachedClass(source);
  15.374              if (script != null) {
  15.375 @@ -910,23 +735,23 @@
  15.376              }
  15.377          }
  15.378  
  15.379 -        final FunctionNode functionNode = new Parser(this, source, errMan, strict).parse();
  15.380 -        if (errors.hasErrors() || _parse_only) {
  15.381 +        final FunctionNode functionNode = new Parser(env, source, errMan, strict).parse();
  15.382 +        if (errors.hasErrors() || env._parse_only) {
  15.383              return null;
  15.384          }
  15.385  
  15.386 -        if (_print_ast) {
  15.387 +        if (env._print_ast) {
  15.388              getErr().println(new ASTWriter(functionNode));
  15.389          }
  15.390  
  15.391 -        if (_print_parse) {
  15.392 +        if (env._print_parse) {
  15.393              getErr().println(new PrintVisitor(functionNode));
  15.394          }
  15.395  
  15.396          final URL          url    = source.getURL();
  15.397 -        final ScriptLoader loader = _loader_per_compile ? createNewLoader() : scriptLoader;
  15.398 +        final ScriptLoader loader = env._loader_per_compile ? createNewLoader() : scriptLoader;
  15.399          final CodeSource   cs     = url == null ? null : new CodeSource(url, (CodeSigner[])null);
  15.400 -        final CodeInstaller<Context> installer = new ContextCodeInstaller(this, loader, cs);
  15.401 +        final CodeInstaller<ScriptEnvironment> installer = new ContextCodeInstaller(this, loader, cs);
  15.402  
  15.403          final Compiler compiler = new Compiler(installer, functionNode, strict);
  15.404  
    16.1 --- a/src/jdk/nashorn/internal/runtime/JSONFunctions.java	Fri Feb 22 23:33:46 2013 -0400
    16.2 +++ b/src/jdk/nashorn/internal/runtime/JSONFunctions.java	Mon Feb 25 16:58:31 2013 +0530
    16.3 @@ -71,7 +71,7 @@
    16.4                  new Source("<json>", str),
    16.5                  new Context.ThrowErrorManager(),
    16.6                  (context != null) ?
    16.7 -                    context._strict :
    16.8 +                    context.getEnv()._strict :
    16.9                      false);
   16.10  
   16.11          Node node;
    17.1 --- a/src/jdk/nashorn/internal/runtime/OptionsObject.java	Fri Feb 22 23:33:46 2013 -0400
    17.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.3 @@ -1,141 +0,0 @@
    17.4 -/*
    17.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
    17.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    17.7 - *
    17.8 - * This code is free software; you can redistribute it and/or modify it
    17.9 - * under the terms of the GNU General Public License version 2 only, as
   17.10 - * published by the Free Software Foundation.  Oracle designates this
   17.11 - * particular file as subject to the "Classpath" exception as provided
   17.12 - * by Oracle in the LICENSE file that accompanied this code.
   17.13 - *
   17.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
   17.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   17.16 - * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   17.17 - * version 2 for more details (a copy is included in the LICENSE file that
   17.18 - * accompanied this code).
   17.19 - *
   17.20 - * You should have received a copy of the GNU General Public License version
   17.21 - * 2 along with this work; if not, write to the Free Software Foundation,
   17.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   17.23 - *
   17.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   17.25 - * or visit www.oracle.com if you need additional information or have any
   17.26 - * questions.
   17.27 - */
   17.28 -
   17.29 -package jdk.nashorn.internal.runtime;
   17.30 -
   17.31 -import java.util.TimeZone;
   17.32 -
   17.33 -/**
   17.34 - * A convenience object to expose only command line options from a Context.
   17.35 - */
   17.36 -public final class OptionsObject {
   17.37 -    /** Always allow functions as statements */
   17.38 -    public final boolean _anon_functions;
   17.39 -
   17.40 -    /** Size of the per-global Class cache size */
   17.41 -    public final int     _class_cache_size;
   17.42 -
   17.43 -    /** Only compile script, do not run it or generate other ScriptObjects */
   17.44 -    public final boolean _compile_only;
   17.45 -
   17.46 -    /** Accumulated callsite flags that will be used when bootstrapping script callsites */
   17.47 -    public final int     _callsite_flags;
   17.48 -
   17.49 -    /** Generate line number table in class files */
   17.50 -    public final boolean _debug_lines;
   17.51 -
   17.52 -    /** Package to which generated class files are added */
   17.53 -    public final String  _dest_dir;
   17.54 -
   17.55 -    /** Display stack trace upon error, default is false */
   17.56 -    public final boolean _dump_on_error;
   17.57 -
   17.58 -    /** Invalid lvalue expressions should be reported as early errors */
   17.59 -    public final boolean _early_lvalue_error;
   17.60 -
   17.61 -    /** Empty statements should be preserved in the AST */
   17.62 -    public final boolean _empty_statements;
   17.63 -
   17.64 -    /** Show full Nashorn version */
   17.65 -    public final boolean _fullversion;
   17.66 -
   17.67 -    /** Create a new class loaded for each compilation */
   17.68 -    public final boolean _loader_per_compile;
   17.69 -
   17.70 -    /** Package to which generated class files are added */
   17.71 -    public final String  _package;
   17.72 -
   17.73 -    /** Only parse the source code, do not compile */
   17.74 -    public final boolean _parse_only;
   17.75 -
   17.76 -    /** Print the AST before lowering */
   17.77 -    public final boolean _print_ast;
   17.78 -
   17.79 -    /** Print the AST after lowering */
   17.80 -    public final boolean _print_lower_ast;
   17.81 -
   17.82 -    /** Print resulting bytecode for script */
   17.83 -    public final boolean _print_code;
   17.84 -
   17.85 -    /** Print function will no print newline characters */
   17.86 -    public final boolean _print_no_newline;
   17.87 -
   17.88 -    /** Print AST in more human readable form */
   17.89 -    public final boolean _print_parse;
   17.90 -
   17.91 -    /** Print AST in more human readable form after Lowering */
   17.92 -    public final boolean _print_lower_parse;
   17.93 -
   17.94 -    /** print symbols and their contents for the script */
   17.95 -    public final boolean _print_symbols;
   17.96 -
   17.97 -    /** is this context in scripting mode? */
   17.98 -    public final boolean _scripting;
   17.99 -
  17.100 -    /** is this context in strict mode? */
  17.101 -    public final boolean _strict;
  17.102 -
  17.103 -    /** print version info of Nashorn */
  17.104 -    public final boolean _version;
  17.105 -
  17.106 -    /** should code verification be done of generated bytecode */
  17.107 -    public final boolean _verify_code;
  17.108 -
  17.109 -    /** time zone for this context */
  17.110 -    public final TimeZone _timezone;
  17.111 -
  17.112 -    /**
  17.113 -     * Constructor
  17.114 -     *
  17.115 -     * @param context a context
  17.116 -     */
  17.117 -    public OptionsObject(final Context context) {
  17.118 -        this._anon_functions = context._anon_functions;
  17.119 -        this._callsite_flags = context._callsite_flags;
  17.120 -        this._class_cache_size = context._class_cache_size;
  17.121 -        this._compile_only = context._compile_only;
  17.122 -        this._debug_lines = context._debug_lines;
  17.123 -        this._dest_dir = context._dest_dir;
  17.124 -        this._dump_on_error = context._dump_on_error;
  17.125 -        this._early_lvalue_error = context._early_lvalue_error;
  17.126 -        this._empty_statements = context._empty_statements;
  17.127 -        this._fullversion = context._fullversion;
  17.128 -        this._loader_per_compile = context._loader_per_compile;
  17.129 -        this._package = context._package;
  17.130 -        this._parse_only = context._parse_only;
  17.131 -        this._print_ast = context._print_ast;
  17.132 -        this._print_code = context._print_code;
  17.133 -        this._print_lower_ast = context._print_lower_ast;
  17.134 -        this._print_lower_parse = context._print_lower_parse;
  17.135 -        this._print_no_newline = context._print_no_newline;
  17.136 -        this._print_parse = context._print_parse;
  17.137 -        this._print_symbols = context._print_symbols;
  17.138 -        this._scripting = context._scripting;
  17.139 -        this._strict = context._strict;
  17.140 -        this._timezone = context._timezone;
  17.141 -        this._verify_code = context._verify_code;
  17.142 -        this._version = context._version;
  17.143 -    }
  17.144 -}
    18.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.2 +++ b/src/jdk/nashorn/internal/runtime/ScriptEnvironment.java	Mon Feb 25 16:58:31 2013 +0530
    18.3 @@ -0,0 +1,249 @@
    18.4 +/*
    18.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
    18.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    18.7 + *
    18.8 + * This code is free software; you can redistribute it and/or modify it
    18.9 + * under the terms of the GNU General Public License version 2 only, as
   18.10 + * published by the Free Software Foundation.  Oracle designates this
   18.11 + * particular file as subject to the "Classpath" exception as provided
   18.12 + * by Oracle in the LICENSE file that accompanied this code.
   18.13 + *
   18.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   18.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   18.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   18.17 + * version 2 for more details (a copy is included in the LICENSE file that
   18.18 + * accompanied this code).
   18.19 + *
   18.20 + * You should have received a copy of the GNU General Public License version
   18.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   18.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   18.23 + *
   18.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   18.25 + * or visit www.oracle.com if you need additional information or have any
   18.26 + * questions.
   18.27 + */
   18.28 +
   18.29 +package jdk.nashorn.internal.runtime;
   18.30 +
   18.31 +import java.io.PrintWriter;
   18.32 +import java.util.List;
   18.33 +import java.util.Locale;
   18.34 +import java.util.TimeZone;
   18.35 +import jdk.nashorn.internal.codegen.Namespace;
   18.36 +import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
   18.37 +import jdk.nashorn.internal.runtime.options.KeyValueOption;
   18.38 +import jdk.nashorn.internal.runtime.options.Option;
   18.39 +import jdk.nashorn.internal.runtime.options.Options;
   18.40 +
   18.41 +/**
   18.42 + * Script environment consists of command line options, arguments, script files
   18.43 + * and output and error writers, top level Namespace etc.
   18.44 + */
   18.45 +public final class ScriptEnvironment {
   18.46 +    /** Output writer for this environment */
   18.47 +    private final PrintWriter out;
   18.48 +
   18.49 +    /** Error writer for this environment */
   18.50 +    private final PrintWriter err;
   18.51 +
   18.52 +    /** Top level namespace. */
   18.53 +    private final Namespace namespace;
   18.54 +
   18.55 +    /** Current Options object. */
   18.56 +    private Options options;
   18.57 +
   18.58 +    /** Always allow functions as statements */
   18.59 +    public final boolean _anon_functions;
   18.60 +
   18.61 +    /** Size of the per-global Class cache size */
   18.62 +    public final int     _class_cache_size;
   18.63 +
   18.64 +    /** Only compile script, do not run it or generate other ScriptObjects */
   18.65 +    public final boolean _compile_only;
   18.66 +
   18.67 +    /** Accumulated callsite flags that will be used when bootstrapping script callsites */
   18.68 +    public final int     _callsite_flags;
   18.69 +
   18.70 +    /** Generate line number table in class files */
   18.71 +    public final boolean _debug_lines;
   18.72 +
   18.73 +    /** Package to which generated class files are added */
   18.74 +    public final String  _dest_dir;
   18.75 +
   18.76 +    /** Display stack trace upon error, default is false */
   18.77 +    public final boolean _dump_on_error;
   18.78 +
   18.79 +    /** Invalid lvalue expressions should be reported as early errors */
   18.80 +    public final boolean _early_lvalue_error;
   18.81 +
   18.82 +    /** Empty statements should be preserved in the AST */
   18.83 +    public final boolean _empty_statements;
   18.84 +
   18.85 +    /** Show full Nashorn version */
   18.86 +    public final boolean _fullversion;
   18.87 +
   18.88 +    /** Create a new class loaded for each compilation */
   18.89 +    public final boolean _loader_per_compile;
   18.90 +
   18.91 +    /** Do not support non-standard syntax extensions. */
   18.92 +    public final boolean _no_syntax_extensions;
   18.93 +
   18.94 +    /** Package to which generated class files are added */
   18.95 +    public final String  _package;
   18.96 +
   18.97 +    /** Only parse the source code, do not compile */
   18.98 +    public final boolean _parse_only;
   18.99 +
  18.100 +    /** Print the AST before lowering */
  18.101 +    public final boolean _print_ast;
  18.102 +
  18.103 +    /** Print the AST after lowering */
  18.104 +    public final boolean _print_lower_ast;
  18.105 +
  18.106 +    /** Print resulting bytecode for script */
  18.107 +    public final boolean _print_code;
  18.108 +
  18.109 +    /** Print function will no print newline characters */
  18.110 +    public final boolean _print_no_newline;
  18.111 +
  18.112 +    /** Print AST in more human readable form */
  18.113 +    public final boolean _print_parse;
  18.114 +
  18.115 +    /** Print AST in more human readable form after Lowering */
  18.116 +    public final boolean _print_lower_parse;
  18.117 +
  18.118 +    /** print symbols and their contents for the script */
  18.119 +    public final boolean _print_symbols;
  18.120 +
  18.121 +    /** is this environment in scripting mode? */
  18.122 +    public final boolean _scripting;
  18.123 +
  18.124 +    /** is this environment in strict mode? */
  18.125 +    public final boolean _strict;
  18.126 +
  18.127 +    /** print version info of Nashorn */
  18.128 +    public final boolean _version;
  18.129 +
  18.130 +    /** should code verification be done of generated bytecode */
  18.131 +    public final boolean _verify_code;
  18.132 +
  18.133 +    /** time zone for this environment */
  18.134 +    public final TimeZone _timezone;
  18.135 +
  18.136 +    /** Local for error messages */
  18.137 +    public final Locale _locale;
  18.138 +
  18.139 +    /**
  18.140 +     * Constructor
  18.141 +     *
  18.142 +     * @param options a Options object
  18.143 +     * @param out output print writer
  18.144 +     * @param err error print writer
  18.145 +     */
  18.146 +    ScriptEnvironment(final Options options, final PrintWriter out, final PrintWriter err) {
  18.147 +        this.out = out;
  18.148 +        this.err = err;
  18.149 +        this.namespace = new Namespace();
  18.150 +        this.options = options;
  18.151 +
  18.152 +        _anon_functions       = options.getBoolean("anon.functions");
  18.153 +        _class_cache_size     = options.getInteger("class.cache.size");
  18.154 +        _compile_only         = options.getBoolean("compile.only");
  18.155 +        _debug_lines          = options.getBoolean("debug.lines");
  18.156 +        _dest_dir             = options.getString("d");
  18.157 +        _dump_on_error        = options.getBoolean("doe");
  18.158 +        _early_lvalue_error   = options.getBoolean("early.lvalue.error");
  18.159 +        _empty_statements     = options.getBoolean("empty.statements");
  18.160 +        _fullversion          = options.getBoolean("fullversion");
  18.161 +        _loader_per_compile   = options.getBoolean("loader.per.compile");
  18.162 +        _no_syntax_extensions = options.getBoolean("no.syntax.extensions");
  18.163 +        _package              = options.getString("package");
  18.164 +        _parse_only           = options.getBoolean("parse.only");
  18.165 +        _print_ast            = options.getBoolean("print.ast");
  18.166 +        _print_lower_ast      = options.getBoolean("print.lower.ast");
  18.167 +        _print_code           = options.getBoolean("print.code");
  18.168 +        _print_no_newline     = options.getBoolean("print.no.newline");
  18.169 +        _print_parse          = options.getBoolean("print.parse");
  18.170 +        _print_lower_parse    = options.getBoolean("print.lower.parse");
  18.171 +        _print_symbols        = options.getBoolean("print.symbols");
  18.172 +        _scripting            = options.getBoolean("scripting");
  18.173 +        _strict               = options.getBoolean("strict");
  18.174 +        _version              = options.getBoolean("version");
  18.175 +        _verify_code          = options.getBoolean("verify.code");
  18.176 +
  18.177 +        int callSiteFlags = 0;
  18.178 +        if (options.getBoolean("profile.callsites")) {
  18.179 +            callSiteFlags |= NashornCallSiteDescriptor.CALLSITE_PROFILE;
  18.180 +        }
  18.181 +
  18.182 +        if (options.get("trace.callsites") instanceof KeyValueOption) {
  18.183 +            callSiteFlags |= NashornCallSiteDescriptor.CALLSITE_TRACE;
  18.184 +            final KeyValueOption kv = (KeyValueOption)options.get("trace.callsites");
  18.185 +            if (kv.hasValue("miss")) {
  18.186 +                callSiteFlags |= NashornCallSiteDescriptor.CALLSITE_TRACE_MISSES;
  18.187 +            }
  18.188 +            if (kv.hasValue("enterexit") || (callSiteFlags & NashornCallSiteDescriptor.CALLSITE_TRACE_MISSES) == 0) {
  18.189 +                callSiteFlags |= NashornCallSiteDescriptor.CALLSITE_TRACE_ENTEREXIT;
  18.190 +            }
  18.191 +            if (kv.hasValue("objects")) {
  18.192 +                callSiteFlags |= NashornCallSiteDescriptor.CALLSITE_TRACE_VALUES;
  18.193 +            }
  18.194 +            if (kv.hasValue("scope")) {
  18.195 +                callSiteFlags |= NashornCallSiteDescriptor.CALLSITE_TRACE_SCOPE;
  18.196 +            }
  18.197 +        }
  18.198 +        this._callsite_flags = callSiteFlags;
  18.199 +
  18.200 +        final Option<?> option = options.get("timezone");
  18.201 +        if (option != null) {
  18.202 +            this._timezone = (TimeZone)option.getValue();
  18.203 +        } else {
  18.204 +            this._timezone  = TimeZone.getDefault();
  18.205 +        }
  18.206 +
  18.207 +        this._locale = Locale.getDefault();
  18.208 +    }
  18.209 +
  18.210 +    /**
  18.211 +     * Get the output stream for this environment
  18.212 +     * @return output print writer
  18.213 +     */
  18.214 +    public PrintWriter getOut() {
  18.215 +        return out;
  18.216 +    }
  18.217 +
  18.218 +    /**
  18.219 +     * Get the error stream for this environment
  18.220 +     * @return error print writer
  18.221 +     */
  18.222 +    public PrintWriter getErr() {
  18.223 +        return err;
  18.224 +    }
  18.225 +
  18.226 +    /**
  18.227 +     * Get the namespace for this environment
  18.228 +     * @return namespace
  18.229 +     */
  18.230 +    public Namespace getNamespace() {
  18.231 +        return namespace;
  18.232 +    }
  18.233 +
  18.234 +    /**
  18.235 +     * Return the JavaScript files passed to the program
  18.236 +     *
  18.237 +     * @return a list of files
  18.238 +     */
  18.239 +    public List<String> getFiles() {
  18.240 +        return options.getFiles();
  18.241 +    }
  18.242 +
  18.243 +    /**
  18.244 +     * Return the user arguments to the program, i.e. those trailing "--" after
  18.245 +     * the filename
  18.246 +     *
  18.247 +     * @return a list of user arguments
  18.248 +     */
  18.249 +    public List<String> getArguments() {
  18.250 +        return options.getArguments();
  18.251 +    }
  18.252 +}
    19.1 --- a/src/jdk/nashorn/internal/runtime/ScriptRuntime.java	Fri Feb 22 23:33:46 2013 -0400
    19.2 +++ b/src/jdk/nashorn/internal/runtime/ScriptRuntime.java	Mon Feb 25 16:58:31 2013 +0530
    19.3 @@ -403,7 +403,7 @@
    19.4       * @return JSON string representation of AST of the supplied code
    19.5       */
    19.6      public static String parse(final String code, final String name, final boolean includeLoc) {
    19.7 -        return JSONWriter.parse(Context.getContextTrusted(), code, name, includeLoc);
    19.8 +        return JSONWriter.parse(Context.getContextTrusted().getEnv(), code, name, includeLoc);
    19.9      }
   19.10  
   19.11      /**
    20.1 --- a/src/jdk/nashorn/tools/Shell.java	Fri Feb 22 23:33:46 2013 -0400
    20.2 +++ b/src/jdk/nashorn/tools/Shell.java	Mon Feb 25 16:58:31 2013 +0530
    20.3 @@ -45,6 +45,7 @@
    20.4  import jdk.nashorn.internal.parser.Parser;
    20.5  import jdk.nashorn.internal.runtime.Context;
    20.6  import jdk.nashorn.internal.runtime.ErrorManager;
    20.7 +import jdk.nashorn.internal.runtime.ScriptEnvironment;
    20.8  import jdk.nashorn.internal.runtime.ScriptFunction;
    20.9  import jdk.nashorn.internal.runtime.ScriptObject;
   20.10  import jdk.nashorn.internal.runtime.ScriptRuntime;
   20.11 @@ -157,12 +158,13 @@
   20.12          }
   20.13  
   20.14          final ScriptObject global = context.createGlobal();
   20.15 -        final List<String> files = context.getOptions().getFiles();
   20.16 +        final ScriptEnvironment env = context.getEnv();
   20.17 +        final List<String> files = env.getFiles();
   20.18          if (files.isEmpty()) {
   20.19              return readEvalPrint(context, global);
   20.20          }
   20.21  
   20.22 -        if (context._compile_only) {
   20.23 +        if (env._compile_only) {
   20.24              return compileScripts(context, global, files);
   20.25          }
   20.26  
   20.27 @@ -237,6 +239,7 @@
   20.28      private static int compileScripts(final Context context, final ScriptObject global, final List<String> files) throws IOException {
   20.29          final ScriptObject oldGlobal = Context.getGlobal();
   20.30          final boolean globalChanged = (oldGlobal != global);
   20.31 +        final ScriptEnvironment env = context.getEnv();
   20.32          try {
   20.33              if (globalChanged) {
   20.34                  Context.setGlobal(global);
   20.35 @@ -245,18 +248,18 @@
   20.36  
   20.37              // For each file on the command line.
   20.38              for (final String fileName : files) {
   20.39 -                final FunctionNode functionNode = new Parser(context, new Source(fileName, new File(fileName)), errors).parse();
   20.40 +                final FunctionNode functionNode = new Parser(env, new Source(fileName, new File(fileName)), errors).parse();
   20.41  
   20.42                  if (errors.getNumberOfErrors() != 0) {
   20.43                      return COMPILATION_ERROR;
   20.44                  }
   20.45  
   20.46                  //null - pass no code installer - this is compile only
   20.47 -                new Compiler(context, functionNode).compile();
   20.48 +                new Compiler(env, functionNode).compile();
   20.49              }
   20.50          } finally {
   20.51 -            context.getOut().flush();
   20.52 -            context.getErr().flush();
   20.53 +            env.getOut().flush();
   20.54 +            env.getErr().flush();
   20.55              if (globalChanged) {
   20.56                  Context.setGlobal(oldGlobal);
   20.57              }
   20.58 @@ -296,7 +299,7 @@
   20.59                      apply(script, global);
   20.60                  } catch (final NashornException e) {
   20.61                      errors.error(e.toString());
   20.62 -                    if (context._dump_on_error) {
   20.63 +                    if (context.getEnv()._dump_on_error) {
   20.64                          e.printStackTrace(context.getErr());
   20.65                      }
   20.66  
   20.67 @@ -341,6 +344,7 @@
   20.68          final PrintWriter err = context.getErr();
   20.69          final ScriptObject oldGlobal = Context.getGlobal();
   20.70          final boolean globalChanged = (oldGlobal != global);
   20.71 +        final ScriptEnvironment env = context.getEnv();
   20.72  
   20.73          try {
   20.74              if (globalChanged) {
   20.75 @@ -353,7 +357,7 @@
   20.76                  context.eval(global, source.getString(), global, "<shell.js>", false);
   20.77              } catch (final Exception e) {
   20.78                  err.println(e);
   20.79 -                if (context._dump_on_error) {
   20.80 +                if (env._dump_on_error) {
   20.81                      e.printStackTrace(err);
   20.82                  }
   20.83  
   20.84 @@ -377,10 +381,10 @@
   20.85  
   20.86                  Object res;
   20.87                  try {
   20.88 -                    res = context.eval(global, source, global, "<shell>", context._strict);
   20.89 +                    res = context.eval(global, source, global, "<shell>", env._strict);
   20.90                  } catch (final Exception e) {
   20.91                      err.println(e);
   20.92 -                    if (context._dump_on_error) {
   20.93 +                    if (env._dump_on_error) {
   20.94                          e.printStackTrace(err);
   20.95                      }
   20.96                      continue;
    21.1 --- a/test/script/trusted/JDK-8006529.js	Fri Feb 22 23:33:46 2013 -0400
    21.2 +++ b/test/script/trusted/JDK-8006529.js	Mon Feb 25 16:58:31 2013 +0530
    21.3 @@ -42,6 +42,7 @@
    21.4  var Parser         = Java.type("jdk.nashorn.internal.parser.Parser")
    21.5  var Compiler       = Java.type("jdk.nashorn.internal.codegen.Compiler")
    21.6  var Context        = Java.type("jdk.nashorn.internal.runtime.Context")
    21.7 +var ScriptEnvironment = Java.type("jdk.nashorn.internal.runtime.ScriptEnvironment")
    21.8  var Source         = Java.type("jdk.nashorn.internal.runtime.Source")
    21.9  var FunctionNode   = Java.type("jdk.nashorn.internal.ir.FunctionNode")
   21.10  
   21.11 @@ -89,9 +90,9 @@
   21.12  // representing it.
   21.13  function compile(source) {
   21.14      var source   = new Source("<no name>", source);
   21.15 -    var parser   = new Parser(Context.getContext(), source, null);
   21.16 +    var parser   = new Parser(Context.getContext().getEnv(), source, null);
   21.17      var func     = parseMethod.invoke(parser);
   21.18 -    var compiler = new Compiler(Context.getContext(), func);
   21.19 +    var compiler = new Compiler(Context.getContext().getEnv(), func);
   21.20  
   21.21      compileMethod.invoke(compiler);
   21.22  
    22.1 --- a/test/src/jdk/nashorn/internal/parser/ParserTest.java	Fri Feb 22 23:33:46 2013 -0400
    22.2 +++ b/test/src/jdk/nashorn/internal/parser/ParserTest.java	Mon Feb 25 16:58:31 2013 +0530
    22.3 @@ -154,7 +154,7 @@
    22.4                  Context.setGlobal(global);
    22.5              }
    22.6              final Source   source   = new Source(file.getAbsolutePath(), buffer);
    22.7 -            new Parser(context, source, errors).parse();
    22.8 +            new Parser(context.getEnv(), source, errors).parse();
    22.9              if (errors.getNumberOfErrors() > 0) {
   22.10                  log("Parse failed: " + file.getAbsolutePath());
   22.11                  failed++;
    23.1 --- a/test/src/jdk/nashorn/internal/test/framework/SharedContextEvaluator.java	Fri Feb 22 23:33:46 2013 -0400
    23.2 +++ b/test/src/jdk/nashorn/internal/test/framework/SharedContextEvaluator.java	Mon Feb 25 16:58:31 2013 +0530
    23.3 @@ -135,7 +135,7 @@
    23.4                      ScriptRuntime.apply(script, global);
    23.5                  } catch (final NashornException e) {
    23.6                      errors.error(e.toString());
    23.7 -                    if (context._dump_on_error) {
    23.8 +                    if (context.getEnv()._dump_on_error) {
    23.9                          e.printStackTrace(context.getErr());
   23.10                      }
   23.11  

mercurial