jdk.nashorn.internal.runtime.ScriptEnvironment Java Examples

The following examples show how to use jdk.nashorn.internal.runtime.ScriptEnvironment. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Parser.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Construct a parser.
 *
 * @param env     script environment
 * @param source  source to parse
 * @param errors  error manager
 * @param strict  parser created with strict mode enabled.
 */
public Parser(final ScriptEnvironment env, final Source source, final ErrorManager errors, final boolean strict) {
    super(source, errors, strict);
    this.env       = env;
    this.namespace = new Namespace(env.getNamespace());
    this.scripting = env._scripting;
    if (this.scripting) {
        this.lineInfoReceiver = new Lexer.LineInfoReceiver() {
            @Override
            public void lineInfo(final int receiverLine, final int receiverLinePosition) {
                // update the parser maintained line information
                Parser.this.line = receiverLine;
                Parser.this.linePosition = receiverLinePosition;
            }
        };
    } else {
        // non-scripting mode script can't have multi-line literals
        this.lineInfoReceiver = null;
    }
}
 
Example #2
Source File: ObjectClassGenerator.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Collects the byte codes for a generated JavaScript class.
 *
 * @param classEmitter Open class emitter.
 * @return Byte codes for the class.
 */
private byte[] toByteArray(final ClassEmitter classEmitter) {
    classEmitter.end();

    final byte[] code = classEmitter.toByteArray();
    final ScriptEnvironment env = context.getEnv();

    if (env._print_code) {
        env.getErr().println(ClassEmitter.disassemble(code));
    }

    if (env._verify_code) {
        context.verify(code);
    }

    return code;
}
 
Example #3
Source File: Parser.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Construct a parser.
 *
 * @param env     script environment
 * @param source  source to parse
 * @param errors  error manager
 * @param strict  parser created with strict mode enabled.
 * @param lineOffset line offset to start counting lines from
 * @param log debug logger if one is needed
 */
public Parser(final ScriptEnvironment env, final Source source, final ErrorManager errors, final boolean strict, final int lineOffset, final DebugLogger log) {
    super(source, errors, strict, lineOffset);
    this.env = env;
    this.namespace = new Namespace(env.getNamespace());
    this.scripting = env._scripting;
    if (this.scripting) {
        this.lineInfoReceiver = new Lexer.LineInfoReceiver() {
            @Override
            public void lineInfo(final int receiverLine, final int receiverLinePosition) {
                // update the parser maintained line information
                Parser.this.line = receiverLine;
                Parser.this.linePosition = receiverLinePosition;
            }
        };
    } else {
        // non-scripting mode script can't have multi-line literals
        this.lineInfoReceiver = null;
    }

    this.log = log == null ? DebugLogger.DISABLED_LOGGER : log;
}
 
Example #4
Source File: Shell.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Run method logic.
 *
 * @param in input stream for Shell
 * @param out output stream for Shell
 * @param err error stream for Shell
 * @param args arguments to Shell
 *
 * @return exit code
 *
 * @throws IOException if there's a problem setting up the streams
 */
protected final int run(final InputStream in, final OutputStream out, final OutputStream err, final String[] args) throws IOException {
    final Context context = makeContext(in, out, err, args);
    if (context == null) {
        return COMMANDLINE_ERROR;
    }

    final Global global = context.createGlobal();
    final ScriptEnvironment env = context.getEnv();
    final List<String> files = env.getFiles();
    if (files.isEmpty()) {
        return readEvalPrint(context, global);
    }

    if (env._compile_only) {
        return compileScripts(context, global, files);
    }

    if (env._fx) {
        return runFXScripts(context, global, files);
    }

    return runScripts(context, global, files);
}
 
Example #5
Source File: Shell.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Run method logic.
 *
 * @param in input stream for Shell
 * @param out output stream for Shell
 * @param err error stream for Shell
 * @param args arguments to Shell
 *
 * @return exit code
 *
 * @throws IOException if there's a problem setting up the streams
 */
protected final int run(final InputStream in, final OutputStream out, final OutputStream err, final String[] args) throws IOException {
    final Context context = makeContext(in, out, err, args);
    if (context == null) {
        return COMMANDLINE_ERROR;
    }

    final Global global = context.createGlobal();
    final ScriptEnvironment env = context.getEnv();
    final List<String> files = env.getFiles();
    if (files.isEmpty()) {
        return readEvalPrint(context, global);
    }

    if (env._compile_only) {
        return compileScripts(context, global, files);
    }

    if (env._fx) {
        return runFXScripts(context, global, files);
    }

    return runScripts(context, global, files);
}
 
Example #6
Source File: CompilationPhase.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
FunctionNode transform(final Compiler compiler, final CompilationPhases phases, final FunctionNode fn) {
    final FunctionNode newFunctionNode = transformFunction(fn, new LocalVariableTypesCalculator(compiler));
    final ScriptEnvironment senv = compiler.getScriptEnvironment();
    final PrintWriter       err  = senv.getErr();

    //TODO separate phase for the debug printouts for abstraction and clarity
    if (senv._print_lower_ast || fn.getFlag(FunctionNode.IS_PRINT_LOWER_AST)) {
        err.println("Lower AST for: " + quote(newFunctionNode.getName()));
        err.println(new ASTWriter(newFunctionNode));
    }

    if (senv._print_lower_parse || fn.getFlag(FunctionNode.IS_PRINT_LOWER_PARSE)) {
        err.println("Lower AST for: " + quote(newFunctionNode.getName()));
        err.println(new PrintVisitor(newFunctionNode));
    }

    return newFunctionNode;
}
 
Example #7
Source File: Parser.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Construct a parser.
 *
 * @param env     script environment
 * @param source  source to parse
 * @param errors  error manager
 * @param strict  parser created with strict mode enabled.
 * @param lineOffset line offset to start counting lines from
 * @param log debug logger if one is needed
 */
public Parser(final ScriptEnvironment env, final Source source, final ErrorManager errors, final boolean strict, final int lineOffset, final DebugLogger log) {
    super(source, errors, strict, lineOffset);
    this.env = env;
    this.namespace = new Namespace(env.getNamespace());
    this.scripting = env._scripting;
    if (this.scripting) {
        this.lineInfoReceiver = new Lexer.LineInfoReceiver() {
            @Override
            public void lineInfo(final int receiverLine, final int receiverLinePosition) {
                // update the parser maintained line information
                Parser.this.line = receiverLine;
                Parser.this.linePosition = receiverLinePosition;
            }
        };
    } else {
        // non-scripting mode script can't have multi-line literals
        this.lineInfoReceiver = null;
    }

    this.log = log == null ? DebugLogger.DISABLED_LOGGER : log;
}
 
Example #8
Source File: Parser.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Construct a parser.
 *
 * @param env     script environment
 * @param source  source to parse
 * @param errors  error manager
 * @param strict  parser created with strict mode enabled.
 * @param lineOffset line offset to start counting lines from
 * @param log debug logger if one is needed
 */
public Parser(final ScriptEnvironment env, final Source source, final ErrorManager errors, final boolean strict, final int lineOffset, final DebugLogger log) {
    super(source, errors, strict, lineOffset);
    this.env = env;
    this.namespace = new Namespace(env.getNamespace());
    this.scripting = env._scripting;
    if (this.scripting) {
        this.lineInfoReceiver = new Lexer.LineInfoReceiver() {
            @Override
            public void lineInfo(final int receiverLine, final int receiverLinePosition) {
                // update the parser maintained line information
                Parser.this.line = receiverLine;
                Parser.this.linePosition = receiverLinePosition;
            }
        };
    } else {
        // non-scripting mode script can't have multi-line literals
        this.lineInfoReceiver = null;
    }

    this.log = log == null ? DebugLogger.DISABLED_LOGGER : log;
}
 
Example #9
Source File: Shell.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Run method logic.
 *
 * @param in input stream for Shell
 * @param out output stream for Shell
 * @param err error stream for Shell
 * @param args arguments to Shell
 *
 * @return exit code
 *
 * @throws IOException if there's a problem setting up the streams
 */
protected final int run(final InputStream in, final OutputStream out, final OutputStream err, final String[] args) throws IOException {
    final Context context = makeContext(in, out, err, args);
    if (context == null) {
        return COMMANDLINE_ERROR;
    }

    final Global global = context.createGlobal();
    final ScriptEnvironment env = context.getEnv();
    final List<String> files = env.getFiles();
    if (files.isEmpty()) {
        return readEvalPrint(context, global);
    }

    if (env._compile_only) {
        return compileScripts(context, global, files);
    }

    if (env._fx) {
        return runFXScripts(context, global, files);
    }

    return runScripts(context, global, files);
}
 
Example #10
Source File: NashornCompleter.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static Parser createParser(final ScriptEnvironment env) {
    final List<String> args = new ArrayList<>();
    if (env._const_as_var) {
        args.add("--const-as-var");
    }

    if (env._no_syntax_extensions) {
        args.add("-nse");
    }

    if (env._scripting) {
        args.add("-scripting");
    }

    if (env._strict) {
        args.add("-strict");
    }

    if (env._es6) {
        args.add("--language=es6");
    }

    return Parser.create(args.toArray(new String[0]));
}
 
Example #11
Source File: Shell.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Run method logic.
 *
 * @param in input stream for Shell
 * @param out output stream for Shell
 * @param err error stream for Shell
 * @param args arguments to Shell
 *
 * @return exit code
 *
 * @throws IOException if there's a problem setting up the streams
 */
protected final int run(final InputStream in, final OutputStream out, final OutputStream err, final String[] args) throws IOException {
    final Context context = makeContext(in, out, err, args);
    if (context == null) {
        return COMMANDLINE_ERROR;
    }

    final Global global = context.createGlobal();
    final ScriptEnvironment env = context.getEnv();
    final List<String> files = env.getFiles();
    if (files.isEmpty()) {
        return readEvalPrint(context, global);
    }

    if (env._compile_only) {
        return compileScripts(context, global, files);
    }

    if (env._fx) {
        return runFXScripts(context, global, files);
    }

    return runScripts(context, global, files);
}
 
Example #12
Source File: Global.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
private void initScripting(final ScriptEnvironment scriptEnv) {
    Object value;
    value = ScriptFunctionImpl.makeFunction("readLine", ScriptingFunctions.READLINE);
    addOwnProperty("readLine", Attribute.NOT_ENUMERABLE, value);

    value = ScriptFunctionImpl.makeFunction("readFully", ScriptingFunctions.READFULLY);
    addOwnProperty("readFully", Attribute.NOT_ENUMERABLE, value);

    final String execName = ScriptingFunctions.EXEC_NAME;
    value = ScriptFunctionImpl.makeFunction(execName, ScriptingFunctions.EXEC);
    addOwnProperty(execName, Attribute.NOT_ENUMERABLE, value);

    // Nashorn extension: global.echo (scripting-mode-only)
    // alias for "print"
    value = get("print");
    addOwnProperty("echo", Attribute.NOT_ENUMERABLE, value);

    // Nashorn extension: global.$OPTIONS (scripting-mode-only)
    final ScriptObject options = newObject();
    copyOptions(options, scriptEnv);
    addOwnProperty("$OPTIONS", Attribute.NOT_ENUMERABLE, options);

    // Nashorn extension: global.$ENV (scripting-mode-only)
    if (System.getSecurityManager() == null) {
        // do not fill $ENV if we have a security manager around
        // Retrieve current state of ENV variables.
        final ScriptObject env = newObject();
        env.putAll(System.getenv(), scriptEnv._strict);
        addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, env);
    } else {
        addOwnProperty(ScriptingFunctions.ENV_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    }

    // add other special properties for exec support
    addOwnProperty(ScriptingFunctions.OUT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    addOwnProperty(ScriptingFunctions.ERR_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
    addOwnProperty(ScriptingFunctions.EXIT_NAME, Attribute.NOT_ENUMERABLE, UNDEFINED);
}
 
Example #13
Source File: NativeDate.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private NativeDate(final double time, final ScriptObject proto, final PropertyMap map) {
    super(proto, map);
    final ScriptEnvironment env = Global.getEnv();

    this.time = time;
    this.timezone = env._timezone;
}
 
Example #14
Source File: Shell.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compiles the given script files in the command line
 *
 * @param context the nashorn context
 * @param global the global scope
 * @param files the list of script files to compile
 *
 * @return error code
 * @throws IOException when any script file read results in I/O error
 */
private static int compileScripts(final Context context, final ScriptObject global, final List<String> files) throws IOException {
    final ScriptObject oldGlobal = Context.getGlobal();
    final boolean globalChanged = (oldGlobal != global);
    final ScriptEnvironment env = context.getEnv();
    try {
        if (globalChanged) {
            Context.setGlobal(global);
        }
        final ErrorManager errors = context.getErrorManager();

        // For each file on the command line.
        for (final String fileName : files) {
            final FunctionNode functionNode = new Parser(env, new Source(fileName, new File(fileName)), errors).parse();

            if (errors.getNumberOfErrors() != 0) {
                return COMPILATION_ERROR;
            }

            if (env._print_ast) {
                context.getErr().println(new ASTWriter(functionNode));
            }

            if (env._print_parse) {
                context.getErr().println(new PrintVisitor(functionNode));
            }

            //null - pass no code installer - this is compile only
            new Compiler(env).compile(functionNode);
        }
    } finally {
        env.getOut().flush();
        env.getErr().flush();
        if (globalChanged) {
            Context.setGlobal(oldGlobal);
        }
    }

    return SUCCESS;
}
 
Example #15
Source File: ObjectClassGenerator.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Collects the byte codes for a generated JavaScript class.
 *
 * @param classEmitter Open class emitter.
 * @return Byte codes for the class.
 */
private byte[] toByteArray(final String className, final ClassEmitter classEmitter) {
    classEmitter.end();

    final byte[] code = classEmitter.toByteArray();
    final ScriptEnvironment env = context.getEnv();

    DumpBytecode.dumpBytecode(env, log, code, className);

    if (env._verify_code) {
        context.verify(code);
    }

    return code;
}
 
Example #16
Source File: JSONWriter.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns AST as JSON compatible string.
 *
 * @param env  script environment to use
 * @param code code to be parsed
 * @param name name of the code source (used for location)
 * @param includeLoc tells whether to include location information for nodes or not
 * @return JSON string representation of AST of the supplied code
 */
public static String parse(final ScriptEnvironment env, final String code, final String name, final boolean includeLoc) {
    final Parser       parser     = new Parser(env, new Source(name, code), new Context.ThrowErrorManager(), env._strict);
    final JSONWriter   jsonWriter = new JSONWriter(includeLoc);
    try {
        final FunctionNode functionNode = parser.parse(CompilerConstants.RUN_SCRIPT.symbolName());
        functionNode.accept(jsonWriter);
        return jsonWriter.getString();
    } catch (final ParserException e) {
        e.throwAsEcmaException();
        return null;
    }
}
 
Example #17
Source File: ClassEmitter.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor from the compiler
 *
 * @param env           Script environment
 * @param sourceName    Source name
 * @param unitClassName Compile unit class name.
 * @param strictMode    Should we generate this method in strict mode
 */
ClassEmitter(final ScriptEnvironment env, final String sourceName, final String unitClassName, final boolean strictMode) {
    this(env,
         new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS) {
            private static final String OBJECT_CLASS  = "java/lang/Object";

            @Override
            protected String getCommonSuperClass(final String type1, final String type2) {
                try {
                    return super.getCommonSuperClass(type1, type2);
                } catch (final RuntimeException e) {
                    if (isScriptObject(Compiler.SCRIPTS_PACKAGE, type1) && isScriptObject(Compiler.SCRIPTS_PACKAGE, type2)) {
                        return className(ScriptObject.class);
                    }
                    return OBJECT_CLASS;
                }
            }
        });

    this.unitClassName        = unitClassName;
    this.constantMethodNeeded = new HashSet<>();

    cw.visit(V1_7, ACC_PUBLIC | ACC_SUPER, unitClassName, null, pathName(jdk.nashorn.internal.scripts.JS.class.getName()), null);
    cw.visitSource(sourceName, null);

    defineCommonStatics(strictMode);
}
 
Example #18
Source File: Global.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
private static void copyOptions(final ScriptObject options, final ScriptEnvironment scriptEnv) {
    for (final Field f : scriptEnv.getClass().getFields()) {
        try {
            options.set(f.getName(), f.get(scriptEnv), 0);
        } catch (final IllegalArgumentException | IllegalAccessException exp) {
            throw new RuntimeException(exp);
        }
    }
}
 
Example #19
Source File: Compiler.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor
 *
 * @param context                  context
 * @param env                      script environment
 * @param installer                code installer
 * @param source                   source to compile
 * @param errors                   error manager
 * @param isStrict                 is this a strict compilation
 * @param isOnDemand               is this an on demand compilation
 * @param compiledFunction         compiled function, if any
 * @param types                    parameter and return value type information, if any is known
 * @param invalidatedProgramPoints invalidated program points for recompilation
 * @param typeInformationFile      descriptor of the location where type information is persisted
 * @param continuationEntryPoints  continuation entry points for restof method
 * @param runtimeScope             runtime scope for recompilation type lookup in {@code TypeEvaluator}
 */
@SuppressWarnings("unused")
public Compiler(
        final Context context,
        final ScriptEnvironment env,
        final CodeInstaller<ScriptEnvironment> installer,
        final Source source,
        final ErrorManager errors,
        final boolean isStrict,
        final boolean isOnDemand,
        final RecompilableScriptFunctionData compiledFunction,
        final TypeMap types,
        final Map<Integer, Type> invalidatedProgramPoints,
        final Object typeInformationFile,
        final int[] continuationEntryPoints,
        final ScriptObject runtimeScope) {
    this.context                  = context;
    this.env                      = env;
    this.installer                = installer;
    this.constantData             = new ConstantData();
    this.compileUnits             = CompileUnit.createCompileUnitSet();
    this.bytecode                 = new LinkedHashMap<>();
    this.log                      = initLogger(context);
    this.source                   = source;
    this.errors                   = errors;
    this.sourceName               = FunctionNode.getSourceName(source);
    this.onDemand                 = isOnDemand;
    this.compiledFunction         = compiledFunction;
    this.types                    = types;
    this.invalidatedProgramPoints = invalidatedProgramPoints == null ? new HashMap<Integer, Type>() : invalidatedProgramPoints;
    this.typeInformationFile      = typeInformationFile;
    this.continuationEntryPoints  = continuationEntryPoints == null ? null: continuationEntryPoints.clone();
    this.typeEvaluator            = new TypeEvaluator(this, runtimeScope);
    this.firstCompileUnitName     = firstCompileUnitName();
    this.strict                   = isStrict;

    this.optimistic = env._optimistic_types;
}
 
Example #20
Source File: Global.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private void copyInitialMaps(final ScriptEnvironment env) {
    this.accessorPropertyDescriptorMap = AccessorPropertyDescriptor.getInitialMap().duplicate();
    this.dataPropertyDescriptorMap = DataPropertyDescriptor.getInitialMap().duplicate();
    this.genericPropertyDescriptorMap = GenericPropertyDescriptor.getInitialMap().duplicate();
    this.nativeArgumentsMap = NativeArguments.getInitialMap().duplicate();
    this.nativeArrayMap = NativeArray.getInitialMap().duplicate();
    this.nativeBooleanMap = NativeBoolean.getInitialMap().duplicate();
    this.nativeDateMap = NativeDate.getInitialMap().duplicate();
    this.nativeErrorMap = NativeError.getInitialMap().duplicate();
    this.nativeEvalErrorMap = NativeEvalError.getInitialMap().duplicate();
    this.nativeJSAdapterMap = NativeJSAdapter.getInitialMap().duplicate();
    this.nativeNumberMap = NativeNumber.getInitialMap().duplicate();
    this.nativeRangeErrorMap = NativeRangeError.getInitialMap().duplicate();
    this.nativeReferenceErrorMap = NativeReferenceError.getInitialMap().duplicate();
    this.nativeRegExpMap = NativeRegExp.getInitialMap().duplicate();
    this.nativeRegExpExecResultMap = NativeRegExpExecResult.getInitialMap().duplicate();
    this.nativeStrictArgumentsMap = NativeStrictArguments.getInitialMap().duplicate();
    this.nativeStringMap = NativeString.getInitialMap().duplicate();
    this.nativeSyntaxErrorMap = NativeSyntaxError.getInitialMap().duplicate();
    this.nativeTypeErrorMap = NativeTypeError.getInitialMap().duplicate();
    this.nativeURIErrorMap = NativeURIError.getInitialMap().duplicate();
    this.prototypeObjectMap = PrototypeObject.getInitialMap().duplicate();
    this.objectMap = JO.getInitialMap().duplicate();
    this.functionMap = ScriptFunctionImpl.getInitialMap().duplicate();
    this.anonymousFunctionMap = ScriptFunctionImpl.getInitialAnonymousMap().duplicate();
    this.strictFunctionMap = ScriptFunctionImpl.getInitialStrictMap().duplicate();
    this.boundFunctionMap = ScriptFunctionImpl.getInitialBoundMap().duplicate();

    // java
    if (! env._no_java) {
        this.nativeJavaImporterMap = NativeJavaImporter.getInitialMap().duplicate();
    }

    // typed arrays
    if (! env._no_typed_arrays) {
        this.arrayBufferViewMap = ArrayBufferView.getInitialMap().duplicate();
        this.nativeArrayBufferMap = NativeArrayBuffer.getInitialMap().duplicate();
    }
}
 
Example #21
Source File: NativeDate.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
private NativeDate(final double time, final ScriptObject proto, final PropertyMap map) {
    super(proto, map);
    final ScriptEnvironment env = Global.getEnv();

    this.time = time;
    this.timezone = env._timezone;
}
 
Example #22
Source File: ObjectClassGenerator.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Collects the byte codes for a generated JavaScript class.
 *
 * @param classEmitter Open class emitter.
 * @return Byte codes for the class.
 */
private byte[] toByteArray(final String className, final ClassEmitter classEmitter) {
    classEmitter.end();

    final byte[] code = classEmitter.toByteArray();
    final ScriptEnvironment env = context.getEnv();

    DumpBytecode.dumpBytecode(env, log, code, className);

    if (env._verify_code) {
        context.verify(code);
    }

    return code;
}
 
Example #23
Source File: Global.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static void copyOptions(final ScriptObject options, final ScriptEnvironment scriptEnv) {
    for (final Field f : scriptEnv.getClass().getFields()) {
        try {
            options.set(f.getName(), f.get(scriptEnv), 0);
        } catch (final IllegalArgumentException | IllegalAccessException exp) {
            throw new RuntimeException(exp);
        }
    }
}
 
Example #24
Source File: NativeDate.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
private NativeDate(final double time, final ScriptObject proto, final PropertyMap map) {
    super(proto, map);
    final ScriptEnvironment env = Global.getEnv();

    this.time = time;
    this.timezone = env._timezone;
}
 
Example #25
Source File: NativeDate.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private NativeDate(final double time, final ScriptObject proto, final PropertyMap map) {
    super(proto, map);
    final ScriptEnvironment env = Global.getEnv();

    this.time = time;
    this.timezone = env._timezone;
}
 
Example #26
Source File: NativeDate.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private NativeDate(final double time, final ScriptObject proto, final PropertyMap map) {
    super(proto, map);
    final ScriptEnvironment env = Global.getEnv();

    this.time = time;
    this.timezone = env._timezone;
}
 
Example #27
Source File: Compiler.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor
 *
 * @param env          script environment
 * @param installer    code installer
 * @param sequence     {@link Compiler.CompilationSequence} of {@link CompilationPhase}s to apply as this compilation
 * @param strict       should this compilation use strict mode semantics
 */
//TODO support an array of FunctionNodes for batch lazy compilation
Compiler(final ScriptEnvironment env, final CodeInstaller<ScriptEnvironment> installer, final CompilationSequence sequence, final boolean strict) {
    this.env           = env;
    this.sequence      = sequence;
    this.installer     = installer;
    this.constantData  = new ConstantData();
    this.compileUnits  = new TreeSet<>();
    this.bytecode      = new LinkedHashMap<>();
}
 
Example #28
Source File: Global.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
private static void copyOptions(final ScriptObject options, final ScriptEnvironment scriptEnv) {
    for (Field f : scriptEnv.getClass().getFields()) {
        try {
            options.set(f.getName(), f.get(scriptEnv), false);
        } catch (final IllegalArgumentException | IllegalAccessException exp) {
            throw new RuntimeException(exp);
        }
    }
}
 
Example #29
Source File: JSONWriter.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns AST as JSON compatible string.
 *
 * @param env  script environment to use
 * @param code code to be parsed
 * @param name name of the code source (used for location)
 * @param includeLoc tells whether to include location information for nodes or not
 * @return JSON string representation of AST of the supplied code
 */
public static String parse(final ScriptEnvironment env, final String code, final String name, final boolean includeLoc) {
    final Parser       parser     = new Parser(env, new Source(name, code), new Context.ThrowErrorManager(), env._strict);
    final JSONWriter   jsonWriter = new JSONWriter(includeLoc);
    try {
        final FunctionNode functionNode = parser.parse(CompilerConstants.RUN_SCRIPT.symbolName());
        functionNode.accept(jsonWriter);
        return jsonWriter.getString();
    } catch (final ParserException e) {
        e.throwAsEcmaException();
        return null;
    }
}
 
Example #30
Source File: Global.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void copyOptions(final ScriptObject options, final ScriptEnvironment scriptEnv) {
    for (final Field f : scriptEnv.getClass().getFields()) {
        try {
            options.set(f.getName(), f.get(scriptEnv), 0);
        } catch (final IllegalArgumentException | IllegalAccessException exp) {
            throw new RuntimeException(exp);
        }
    }
}