Java Code Examples for jdk.nashorn.internal.runtime.Context#getEnv()
The following examples show how to use
jdk.nashorn.internal.runtime.Context#getEnv() .
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: ClassEmitter.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
/** * Disassemble an array of byte code. * * @param bytecode byte array representing bytecode * * @return disassembly as human readable string */ static String disassemble(final byte[] bytecode) { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (final PrintWriter pw = new PrintWriter(baos)) { final NashornClassReader cr = new NashornClassReader(bytecode); final Context ctx = AccessController.doPrivileged(new PrivilegedAction<Context>() { @Override public Context run() { return Context.getContext(); } }); final TraceClassVisitor tcv = new TraceClassVisitor(null, new NashornTextifier(ctx.getEnv(), cr), pw); cr.accept(tcv, 0); } final String str = new String(baos.toByteArray()); return str; }
Example 2
Source File: Shell.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
/** * 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 ScriptObject 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 3
Source File: Shell.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
/** * 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 4
Source File: ClassEmitter.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
/** * Disassemble an array of byte code. * * @param bytecode byte array representing bytecode * * @return disassembly as human readable string */ static String disassemble(final byte[] bytecode) { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (final PrintWriter pw = new PrintWriter(baos)) { final NashornClassReader cr = new NashornClassReader(bytecode); final Context ctx = AccessController.doPrivileged(new PrivilegedAction<Context>() { @Override public Context run() { return Context.getContext(); } }); final TraceClassVisitor tcv = new TraceClassVisitor(null, new NashornTextifier(ctx.getEnv(), cr), pw); cr.accept(tcv, 0); } final String str = new String(baos.toByteArray()); return str; }
Example 5
Source File: JSONWriter.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
/** * Returns AST as JSON compatible string. * * @param context context * @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 Context context, final String code, final String name, final boolean includeLoc) { final Parser parser = new Parser(context.getEnv(), sourceFor(name, code), new Context.ThrowErrorManager(), context.getEnv()._strict, context.getLogger(Parser.class)); final JSONWriter jsonWriter = new JSONWriter(includeLoc); try { final FunctionNode functionNode = parser.parse(); //symbol name is ":program", default functionNode.accept(jsonWriter); return jsonWriter.getString(); } catch (final ParserException e) { e.throwAsEcmaException(); return null; } }
Example 6
Source File: Global.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
/** * Constructor * * @param context the context */ public Global(final Context context) { super(checkAndGetMap(context)); this.context = context; this.setIsScope(); final int cacheSize = context.getEnv()._class_cache_size; if (cacheSize > 0) { classCache = new ClassCache(cacheSize); } }
Example 7
Source File: Global.java From nashorn with GNU General Public License v2.0 | 5 votes |
/** * Constructor * * @param context the context */ public Global(final Context context) { super(checkAndGetMap(context)); this.context = context; this.setIsScope(); final int cacheSize = context.getEnv()._class_cache_size; if (cacheSize > 0) { classCache = new ClassCache(cacheSize); } }
Example 8
Source File: Shell.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
/** * Runs launches "fx:bootstrap.js" with the given JavaScript files provided * as arguments. * * @param context the nashorn context * @param global the global scope * @param files the list of script files to provide * * @return error code * @throws IOException when any script file read results in I/O error */ private static int runFXScripts(final Context context, final Global global, final List<String> files) throws IOException { final Global oldGlobal = Context.getGlobal(); final boolean globalChanged = (oldGlobal != global); try { if (globalChanged) { Context.setGlobal(global); } global.addOwnProperty("$GLOBAL", Property.NOT_ENUMERABLE, global); global.addOwnProperty("$SCRIPTS", Property.NOT_ENUMERABLE, files); context.load(global, "fx:bootstrap.js"); } catch (final NashornException e) { context.getErrorManager().error(e.toString()); if (context.getEnv()._dump_on_error) { e.printStackTrace(context.getErr()); } return RUNTIME_ERROR; } finally { context.getOut().flush(); context.getErr().flush(); if (globalChanged) { Context.setGlobal(oldGlobal); } } return SUCCESS; }
Example 9
Source File: Shell.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
/** * Runs launches "fx:bootstrap.js" with the given JavaScript files provided * as arguments. * * @param context the nashorn context * @param global the global scope * @param files the list of script files to provide * * @return error code * @throws IOException when any script file read results in I/O error */ private static int runFXScripts(final Context context, final Global global, final List<String> files) throws IOException { final Global oldGlobal = Context.getGlobal(); final boolean globalChanged = (oldGlobal != global); try { if (globalChanged) { Context.setGlobal(global); } global.addOwnProperty("$GLOBAL", Property.NOT_ENUMERABLE, global); global.addOwnProperty("$SCRIPTS", Property.NOT_ENUMERABLE, files); context.load(global, "fx:bootstrap.js"); } catch (final NashornException e) { context.getErrorManager().error(e.toString()); if (context.getEnv()._dump_on_error) { e.printStackTrace(context.getErr()); } return RUNTIME_ERROR; } finally { context.getOut().flush(); context.getErr().flush(); if (globalChanged) { Context.setGlobal(oldGlobal); } } return SUCCESS; }
Example 10
Source File: NashornCompleter.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
NashornCompleter(final Context context, final Global global, final PartialParser partialParser, final PropertiesHelper propsHelper) { this.context = context; this.global = global; this.env = context.getEnv(); this.partialParser = partialParser; this.propsHelper = propsHelper; this.parser = createParser(env); }
Example 11
Source File: PackagesHelper.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Construct a new PackagesHelper. * * @param context the current Nashorn Context */ PackagesHelper(final Context context) throws IOException { this.context = context; final String modulePath = context.getEnv()._module_path; this.modulePathSet = modulePath != null && !modulePath.isEmpty(); if (isJavacAvailable()) { final String classPath = context.getEnv()._classpath; fm = compiler.getStandardFileManager(null, null, null); fileKinds = EnumSet.of(JavaFileObject.Kind.CLASS); if (this.modulePathSet) { fm.setLocation(StandardLocation.MODULE_PATH, getFiles(modulePath)); } if (classPath != null && !classPath.isEmpty()) { fm.setLocation(StandardLocation.CLASS_PATH, getFiles(classPath)); } else { // no classpath set. Make sure that it is empty and not any default like "." fm.setLocation(StandardLocation.CLASS_PATH, Collections.<File>emptyList()); } jrtfs = null; } else { // javac is not available - directly use jrt fs // to support at least platform classes. fm = null; fileKinds = null; jrtfs = FileSystems.getFileSystem(URI.create("jrt:/")); } }
Example 12
Source File: JSONWriter.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Returns AST as JSON compatible string. * * @param context context * @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 Context context, final String code, final String name, final boolean includeLoc) { final Parser parser = new Parser(context.getEnv(), sourceFor(name, code), new Context.ThrowErrorManager(), context.getEnv()._strict, context.getLogger(Parser.class)); final JSONWriter jsonWriter = new JSONWriter(includeLoc); try { final FunctionNode functionNode = parser.parse(); //symbol name is ":program", default functionNode.accept(jsonWriter); return jsonWriter.getString(); } catch (final ParserException e) { e.throwAsEcmaException(); return null; } }
Example 13
Source File: Shell.java From nashorn with GNU General Public License v2.0 | 4 votes |
/** * Runs the given JavaScript files in the command line * * @param context the nashorn context * @param global the global scope * @param files the list of script files to run * * @return error code * @throws IOException when any script file read results in I/O error */ private int runScripts(final Context context, final ScriptObject global, final List<String> files) throws IOException { final ScriptObject oldGlobal = Context.getGlobal(); final boolean globalChanged = (oldGlobal != global); try { if (globalChanged) { Context.setGlobal(global); } final ErrorManager errors = context.getErrorManager(); // For each file on the command line. for (final String fileName : files) { if ("-".equals(fileName)) { final int res = readEvalPrint(context, global); if (res != SUCCESS) { return res; } continue; } final File file = new File(fileName); final ScriptFunction script = context.compileScript(new Source(fileName, file.toURI().toURL()), global); if (script == null || errors.getNumberOfErrors() != 0) { return COMPILATION_ERROR; } try { apply(script, global); } catch (final NashornException e) { errors.error(e.toString()); if (context.getEnv()._dump_on_error) { e.printStackTrace(context.getErr()); } return RUNTIME_ERROR; } } } finally { context.getOut().flush(); context.getErr().flush(); if (globalChanged) { Context.setGlobal(oldGlobal); } } return SUCCESS; }
Example 14
Source File: Global.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
/** * Constructor * * @param context the context */ public Global(final Context context) { super(checkAndGetMap(context)); this.context = context; this.lexicalScope = context.getEnv()._es6 ? new LexicalScope(this) : null; }
Example 15
Source File: Global.java From jdk8u_nashorn with GNU General Public License v2.0 | 4 votes |
/** * Constructor * * @param context the context */ public Global(final Context context) { super(checkAndGetMap(context)); this.context = context; this.lexicalScope = context.getEnv()._es6 ? new LexicalScope(this) : null; }
Example 16
Source File: Shell.java From TencentKona-8 with GNU General Public License v2.0 | 4 votes |
/** * Runs the given JavaScript files in the command line * * @param context the nashorn context * @param global the global scope * @param files the list of script files to run * * @return error code * @throws IOException when any script file read results in I/O error */ private int runScripts(final Context context, final Global global, final List<String> files) throws IOException { final Global oldGlobal = Context.getGlobal(); final boolean globalChanged = (oldGlobal != global); try { if (globalChanged) { Context.setGlobal(global); } final ErrorManager errors = context.getErrorManager(); // For each file on the command line. for (final String fileName : files) { if ("-".equals(fileName)) { final int res = readEvalPrint(context, global); if (res != SUCCESS) { return res; } continue; } final File file = new File(fileName); final ScriptFunction script = context.compileScript(sourceFor(fileName, file), global); if (script == null || errors.getNumberOfErrors() != 0) { return COMPILATION_ERROR; } try { apply(script, global); } catch (final NashornException e) { errors.error(e.toString()); if (context.getEnv()._dump_on_error) { e.printStackTrace(context.getErr()); } return RUNTIME_ERROR; } } } finally { context.getOut().flush(); context.getErr().flush(); if (globalChanged) { Context.setGlobal(oldGlobal); } } return SUCCESS; }
Example 17
Source File: Global.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
/** * Constructor * * @param context the context */ public Global(final Context context) { super(checkAndGetMap(context)); this.context = context; this.lexicalScope = context.getEnv()._es6 ? new LexicalScope(this) : null; }
Example 18
Source File: Shell.java From jdk8u_nashorn with GNU General Public License v2.0 | 4 votes |
/** * Runs the given JavaScript files in the command line * * @param context the nashorn context * @param global the global scope * @param files the list of script files to run * * @return error code * @throws IOException when any script file read results in I/O error */ private int runScripts(final Context context, final Global global, final List<String> files) throws IOException { final Global oldGlobal = Context.getGlobal(); final boolean globalChanged = (oldGlobal != global); try { if (globalChanged) { Context.setGlobal(global); } final ErrorManager errors = context.getErrorManager(); // For each file on the command line. for (final String fileName : files) { if ("-".equals(fileName)) { final int res = readEvalPrint(context, global); if (res != SUCCESS) { return res; } continue; } final File file = new File(fileName); final ScriptFunction script = context.compileScript(sourceFor(fileName, file), global); if (script == null || errors.getNumberOfErrors() != 0) { return COMPILATION_ERROR; } try { apply(script, global); } catch (final NashornException e) { errors.error(e.toString()); if (context.getEnv()._dump_on_error) { e.printStackTrace(context.getErr()); } return RUNTIME_ERROR; } } } finally { context.getOut().flush(); context.getErr().flush(); if (globalChanged) { Context.setGlobal(oldGlobal); } } return SUCCESS; }
Example 19
Source File: ContextTest.java From hottub with GNU General Public License v2.0 | 4 votes |
@Test public void reflectionTest() { final Options options = new Options(""); final ErrorManager errors = new ErrorManager(); final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader()); final boolean strict = cx.getEnv()._strict; final Global oldGlobal = Context.getGlobal(); Context.setGlobal(cx.createGlobal()); try { final String code = "var obj = { x: 344, y: 42 }"; eval(cx, "<reflectionTest>", code); final Object obj = Context.getGlobal().get("obj"); assertTrue(obj instanceof ScriptObject); final ScriptObject sobj = (ScriptObject)obj; int count = 0; for (final Map.Entry<?, ?> ex : sobj.entrySet()) { final Object key = ex.getKey(); if (key.equals("x")) { assertTrue(ex.getValue() instanceof Number); assertTrue(344.0 == ((Number)ex.getValue()).doubleValue()); count++; } else if (key.equals("y")) { assertTrue(ex.getValue() instanceof Number); assertTrue(42.0 == ((Number)ex.getValue()).doubleValue()); count++; } } assertEquals(count, 2); assertEquals(sobj.size(), 2); // add property sobj.put("zee", "hello", strict); assertEquals(sobj.get("zee"), "hello"); assertEquals(sobj.size(), 3); } finally { Context.setGlobal(oldGlobal); } }
Example 20
Source File: ContextTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
@Test public void reflectionTest() { final Options options = new Options(""); final ErrorManager errors = new ErrorManager(); final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader()); final boolean strict = cx.getEnv()._strict; final Global oldGlobal = Context.getGlobal(); Context.setGlobal(cx.createGlobal()); try { final String code = "var obj = { x: 344, y: 42 }"; eval(cx, "<reflectionTest>", code); final Object obj = Context.getGlobal().get("obj"); assertTrue(obj instanceof ScriptObject); final ScriptObject sobj = (ScriptObject)obj; int count = 0; for (final Map.Entry<?, ?> ex : sobj.entrySet()) { final Object key = ex.getKey(); if (key.equals("x")) { assertTrue(ex.getValue() instanceof Number); assertTrue(344.0 == ((Number)ex.getValue()).doubleValue()); count++; } else if (key.equals("y")) { assertTrue(ex.getValue() instanceof Number); assertTrue(42.0 == ((Number)ex.getValue()).doubleValue()); count++; } } assertEquals(count, 2); assertEquals(sobj.size(), 2); // add property sobj.put("zee", "hello", strict); assertEquals(sobj.get("zee"), "hello"); assertEquals(sobj.size(), 3); } finally { Context.setGlobal(oldGlobal); } }