@Test public void runDoctest() throws Exception { ContextFactory factory = ContextFactory.getGlobal(); Context cx = factory.enterContext(); try { cx.setOptimizationLevel(optimizationLevel); Global global = new Global(cx); // global.runDoctest throws an exception on any failure int testsPassed = global.runDoctest(cx, global, source, name, 1); System.out.println(name + "(" + optimizationLevel + "): " + testsPassed + " passed."); assertTrue(testsPassed > 0); } catch (Exception ex) { System.out.println(name + "(" + optimizationLevel + "): FAILED due to "+ex); throw ex; } finally { Context.exit(); } }
@Test public void runDoctest() throws Exception { Context cx = RhinoAndroidHelper.prepareContext(); try { cx.setOptimizationLevel(optimizationLevel); Global global = new Global(cx); TestUtils.addAssetLoading(global); // global.runDoctest throws an exception on any failure int testsPassed = global.runDoctest(cx, global, source, name, 1); System.out.println(name + "(" + optimizationLevel + "): " + testsPassed + " passed."); assertTrue(testsPassed > 0); } catch (Exception ex) { System.out.println(name + "(" + optimizationLevel + "): FAILED due to "+ex); throw ex; } finally { Context.exit(); } }
protected void init() throws Exception { context = Context.enter(); global = new Global(); global.init(context); ScriptableObject.defineClass(global, ScriptableMessage.class); ScriptableObject.defineClass(global, ScriptableIMC.class); Object o = Context.javaToJS(new ScriptableConsole(), global); ScriptableObject.putProperty(global, "console", o); o = Context.javaToJS(new ScriptableGui(), global); ScriptableObject.putProperty(global, "gui", o); Scriptable imc = context.newObject(global, "IMC", null); global.put("imc", global, imc); }
/** * Entry point for embedded applications. This method attaches * to the global {@link ContextFactory} with a scope of a newly * created {@link Global} object. No I/O redirection is performed * as with {@link #main(String[])}. */ public static Main mainEmbedded(String title) { ContextFactory factory = ContextFactory.getGlobal(); Global global = new Global(); global.init(factory); return mainEmbedded(factory, global, title); }
/** * Helper method for {@link #mainEmbedded(String)}, etc. */ private static Main mainEmbeddedImpl(ContextFactory factory, Object scopeProvider, String title) { if (title == null) { title = "Rhino JavaScript Debugger (embedded usage)"; } Main main = new Main(title); main.doBreak(); main.setExitAction(new IProxy(IProxy.EXIT_ACTION)); main.attachTo(factory); if (scopeProvider instanceof ScopeProvider) { main.setScopeProvider((ScopeProvider)scopeProvider); } else { Scriptable scope = (Scriptable)scopeProvider; if (scope instanceof Global) { Global global = (Global)scope; global.setIn(main.getIn()); global.setOut(main.getOut()); global.setErr(main.getErr()); } main.setScope(scope); } main.pack(); main.setSize(600, 460); main.setVisible(true); return main; }
public static void loadAsset(Context cx, Scriptable thisObj, Object[] args, Function funObj) { for (Object arg : args) { String file = Context.toString(arg); Global.load(cx, thisObj, new Object[]{copyFromAssets(file)}, funObj); } }
@BeforeExperiment @SuppressWarnings("unused") void create() throws IOException { cx = Context.enter(); cx.setOptimizationLevel(optLevel); cx.setLanguageVersion(Context.VERSION_ES6); scope = new Global(cx); for (String bn : BENCHMARKS) { compileScript(cx, bn); } }
@BeforeExperiment @SuppressWarnings("unused") void create() throws IOException { cx = Context.enter(); cx.setOptimizationLevel(optLevel); cx.setLanguageVersion(Context.VERSION_ES6); scope = new Global(cx); runCode(cx, scope, "testsrc/benchmarks/caliper/fieldTests.js"); Object[] sarray = new Object[stringKeys]; for (int i = 0; i < stringKeys; i++) { int len = rand.nextInt(49) + 1; char[] c = new char[len]; for (int cc = 0; cc < len; cc++) { c[cc] = (char) ('a' + rand.nextInt(25)); } sarray[i] = new String(c); } strings = cx.newArray(scope, sarray); Object[] iarray = new Object[intKeys]; for (int i = 0; i < intKeys; i++) { iarray[i] = rand.nextInt(10000); } ints = cx.newArray(scope, iarray); }
private void runTest(int optLevel) throws IOException { Context cx = RhinoAndroidHelper.prepareContext(); cx.setLanguageVersion(Context.VERSION_1_8); cx.setOptimizationLevel(optLevel); Global root = new Global(cx); TestUtils.addAssetLoading(root); root.put("RUN_NAME", root, "V8-Benchmark-" + optLevel); Object result = cx.evaluateString(root, TestUtils.readAsset(TEST_SRC), TEST_SRC, 1, null); results.put(optLevel, result.toString()); }
private void runTest(int optLevel) throws IOException { Context cx = RhinoAndroidHelper.prepareContext(); cx.setLanguageVersion(Context.VERSION_1_8); cx.setOptimizationLevel(optLevel); Global root = new Global(cx); TestUtils.addAssetLoading(root); root.put("RUN_NAME", root, "SunSpider-" + optLevel); Object result = cx.evaluateString(root, TestUtils.readAsset(TEST_SRC), TEST_SRC, 1, null); results.put(optLevel, result.toString()); }
@Before public void init() { cx = RhinoAndroidHelper.prepareContext(); cx.setLanguageVersion(Context.VERSION_1_8); cx.setGeneratingDebug(true); Global global = new Global(cx); TestUtils.addAssetLoading(global); root = cx.newObject(global); }
@JsScriptFuntion public static Object readFile(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws IOException { List<File> dirList = readOnDirs; for (int i = 0; i < args.length; i++) { String filename = Context.toString(args[i]); if (!isPathInsideOneOfListedDirs(dirList, filename)) { throw new RuntimeException("File " + filename + " must be inside on one of those dirs: " + dirList); } } return Global.readFile(cx, thisObj, args, funObj); }
public Object run(Context context) { try { Global global = new Global(context); // 参考注意事项2 InputStream ins = new FileInputStream(file); Reader reader = new InputStreamReader(ins); Object result = context.evaluateReader(global, reader, file.getName(), 1, null); return Context.toString(result); } catch (Exception e) { LogUtil.exception(e); } return "!!!ERROR!!!"; }
public <T> RhinoEngine(final RhinoProperties rhinoProperties) { this.executorService = Executors.newSingleThreadExecutor(new ThreadFactory() { @Override public Thread newThread(Runnable r) { Preconditions.checkState(executionThread == null, "Only one thread is supported"); executionThread = FACTORY.newThread(r); return executionThread; } }); // this ensures a single thread for this engine runWithContext(new RhinoCallable<Void, RuntimeException>() { @Override protected Void doCall(Context cx, Scriptable s) { try { Global global = new Global(cx); RequireProperties require = rhinoProperties.getRequire(); if (require != null) { List<String> modulePathURIs = getModulePathURIs(require); LOGGER.debug("Module paths: {}", modulePathURIs); global.installRequire(cx, modulePathURIs, require.isSandboxed()); } ClassLoader classloader = Thread.currentThread().getContextClassLoader(); // we need to load compat/timeout.js because rhino does not have setTimeout, setInterval, etc. try (Reader in = new InputStreamReader(classloader.getResourceAsStream("compat/timeout.js"))) { cx.evaluateReader(global, in, "compat/timeout.js", 1, null); } scope = global; prototype = new NativeObject(); scope.put("__prototype", scope, prototype); return null; } catch (IOException e) { throw Throwables.propagate(e); } } }); }
Object evaluateTestConfig(Reader reader, String desc) throws IOException { Global scope = jsConfigParser.createGlobalScope(); Object wrappedContext = Context.javaToJS(this, scope); Map jsProperties = Maps.newHashMap(); jsProperties.put("testConfigurationParser", wrappedContext); jsConfigParser.putPropertiesInScope(jsProperties, scope); InputStream scripts = getClass().getResourceAsStream("celos-ci-scripts.js"); jsConfigParser.evaluateReader(scope, new InputStreamReader(scripts), "celos-ci-scripts.js"); return jsConfigParser.evaluateReader(scope, reader, desc); }
public void importDefaultsIntoScope(String label, Global scope) throws IOException { File defaultsFile = new File(defaultsDir, label + "." + WORKFLOW_FILE_EXTENSION); LOGGER.info("Loading defaults: " + defaultsFile); FileReader fileReader = new FileReader(defaultsFile); String fileName = defaultsFile.toString(); jsConfigParser.evaluateReader(scope, fileReader, fileName); }
public Global createGlobalScope() { Global scope = new Global(); scope.initStandardObjects(context, true); Object wrappedMapper = Context.javaToJS(Util.JSON_READER, scope); ScriptableObject.putProperty(scope, "celosReader", wrappedMapper); Object wrappedScope = Context.javaToJS(scope, scope); ScriptableObject.putProperty(scope, "celosScope", wrappedScope); return scope; }
public void putPropertiesInScope(Map<String, Object> jsParameters, Global scope) { if (jsParameters != null) { for (String key : jsParameters.keySet()) { ScriptableObject.putProperty(scope, key, jsParameters.get(key)); } } }
public static void main(String[] args) throws Exception { // initialization Context cx = Context.enter(); Global s = new Global(); s.init(cx); cx.evaluateString(s, "importClass(java.lang.Thread);", "x", 0, null); ScriptableObject.defineClass(s, ScriptableMessage.class); // end of initialization cx.evaluateString(s, "m = new Message('EstimatedState');\n" + "spawn(function() {print('Hello!'); Thread.sleep(5000); print('Goodbye!');});\n" + "m.ref = 'NED_LLD';\n" + "m.x = -100.567;\n" + "m.p = (Math.PI / 180) * 10;\n" + "m.dump();", "inline", 1, null); Context.exit(); }
public static void main(String[] args) throws Exception { // initialization Context cx = Context.enter(); Global s = new Global(); s.init(cx); cx.evaluateString(s, "importClass(java.lang.Thread);", "x", 0, null); ScriptableObject.defineClass(s, KrakenScript.class); // end of initialization cx.evaluateString(s, "m = new Message('EstimatedState');\n" + "spawn(function() {print('Hello!'); Thread.sleep(5000); print('Goodbye!');});\n" + "m.ref = 'NED_LLD';\n" + "m.x = -100.567;\n" + "m.p = (Math.PI / 180) * 10;\n" + "m.dump();", "inline", 1, null); Context.exit(); }
public static Global getGlobal() { return global; }