private void testWithTwoScopes(final String scriptScope1, final String scriptScope2) { final ContextAction action = new ContextAction() { public Object run(final Context cx) { final Scriptable scope1 = cx.initStandardObjects( new MySimpleScriptableObject("scope1")); final Scriptable scope2 = cx.initStandardObjects( new MySimpleScriptableObject("scope2")); cx.evaluateString(scope2, scriptScope2, "source2", 1, null); scope1.put("scope2", scope1, scope2); return cx.evaluateString(scope1, scriptScope1, "source1", 1, null); } }; Utils.runWithAllOptimizationLevels(action); }
/** * delete should not delete anything in the prototype chain. */ @Test public void testDeletePropInPrototype() throws Exception { final String script = "Array.prototype.foo = function() {};\n" + "Array.prototype[1] = function() {};\n" + "var t = [];\n" + "[].foo();\n" + "for (var i in t) delete t[i];\n" + "[].foo();\n" + "[][1]();\n"; final ContextAction action = new ContextAction() { public Object run(final Context _cx) { final ScriptableObject scope = _cx.initStandardObjects(); final Object result = _cx.evaluateString(scope, script, "test script", 0, null); return null; } }; Utils.runWithAllOptimizationLevels(action); }
public void testIt() { final String script = "var fn = function() { return this; }\n" + "fn.apply(1)"; final ContextAction action = new ContextAction() { public Object run(final Context _cx) { final ScriptableObject scope = _cx.initStandardObjects(); final Object result = _cx.evaluateString(scope, script, "test script", 0, null); assertEquals("object", ScriptRuntime.typeof(result)); assertEquals("1", Context.toString(result)); return null; } }; Utils.runWithAllOptimizationLevels(action); }
public void testArrayConcat() { final String script = "var a = ['a0', 'a1'];\n" + "a[3] = 'a3';\n" + "var b = ['b1', 'b2'];\n" + "b.concat(a)"; final ContextAction action = new ContextAction() { public Object run(final Context _cx) { final ScriptableObject scope = _cx.initStandardObjects(); final Object result = _cx.evaluateString(scope, script, "test script", 0, null); assertEquals("b1,b2,a0,a1,,a3", Context.toString(result)); return null; } }; Utils.runWithAllOptimizationLevels(action); }
/** * ECMA 11.4.3 says that typeof on host object is Implementation-dependent */ public void test0() throws Exception { final Function f = new BaseFunction() { @Override public Object call(Context _cx, Scriptable _scope, Scriptable _thisObj, Object[] _args) { return _args[0].getClass().getName(); } }; final ContextAction action = new ContextAction() { public Object run(final Context context) { final Scriptable scope = context.initStandardObjects(); scope.put("myObj", scope, f); return context.evaluateString(scope, "typeof myObj", "test script", 1, null); } }; doTest("function", action); }
@Test public void callOverloadedFunction() { new ContextFactory().call(new ContextAction() { public Object run(Context cx) { cx.evaluateString( cx.initStandardObjects(), "new org.mozilla.javascript.tests.Bug496585().method('one', 'two', 'three')", "<test>", 1, null); cx.evaluateString( cx.initStandardObjects(), "new org.mozilla.javascript.tests.Bug496585().method('one', function() {})", "<test>", 1, null); return null; } }); }
private void runWithExpectedStackTrace(final String _source, final String _expectedStackTrace) { final ContextAction action = new ContextAction() { public Object run(Context cx) { final Scriptable scope = cx.initStandardObjects(); try { cx.evaluateString(scope, _source, "test.js", 0, null); } catch (final JavaScriptException e) { assertEquals(_expectedStackTrace, e.getScriptStackTrace()); return null; } throw new RuntimeException("Exception expected!"); } }; Utils.runWithOptimizationLevel(action, -1); }
@Test public void callOverloadedFunction() { new AndroidContextFactory(new File(System.getProperty("java.io.tmpdir", "."), "classes")).call(new ContextAction() { public Object run(Context cx) { cx.getWrapFactory().setJavaPrimitiveWrap(false); Assert.assertEquals("string[]", cx.evaluateString( cx.initStandardObjects(), "new org.mozilla.javascript.tests.Bug496585Test().method('one', 'two', 'three')", "<test>", 1, null)); Assert.assertEquals("string+function", cx.evaluateString( cx.initStandardObjects(), "new org.mozilla.javascript.tests.Bug496585Test().method('one', function() {})", "<test>", 1, null)); return null; } }); }
private void initJavaScript() { LogUtils.logMessage("Initialising JavaScript..."); contextFactory.call(new ContextAction() { @Override public Object run(Context cx) { ImporterTopLevel importer = new ImporterTopLevel(); importer.initStandardObjects(cx, false); systemScope = importer; Object frameworkScriptable = Context.javaToJS(Framework.this, systemScope); ScriptableObject.putProperty(systemScope, FRAMEWORK_VARIABLE, frameworkScriptable); systemScope.setAttributes(FRAMEWORK_VARIABLE, ScriptableObject.READONLY); globalScope = (ScriptableObject) cx.newObject(systemScope); globalScope.setPrototype(systemScope); globalScope.setParentScope(null); return null; } }); }
public void setJavaScriptProperty(final String name, final Object object, final ScriptableObject scope, final boolean readOnly) { deleteJavaScriptProperty(name, scope); contextFactory.call(new ContextAction() { @Override public Object run(Context arg0) { Object scriptable = Context.javaToJS(object, scope); ScriptableObject.putProperty(scope, name, scriptable); if (readOnly) { scope.setAttributes(name, ScriptableObject.READONLY); } return scriptable; } }); }
public void shutdownGUI() throws OperationCancelledException { if (isInGuiMode()) { mainWindow.shutdown(); mainWindow.dispose(); mainWindow = null; inGuiMode = false; contextFactory.call(new ContextAction() { @Override public Object run(Context cx) { ScriptableObject.deleteProperty(systemScope, "mainWindow"); return null; } }); } }
/** * This method registers a particular Java <code>Object</code> in * the environment of the interpreter. * @param name the name of the script object to create * @param object the Java object */ public void bindObject(final String name, final Object object) { contextFactory.call(new ContextAction() { public Object run(Context cx) { Object o = object; if (name.equals(BIND_NAME_WINDOW) && object instanceof Window) { ((WindowWrapper) globalObject).window = (Window) object; window = (Window) object; o = globalObject; } Scriptable jsObject; jsObject = Context.toObject(o, globalObject); globalObject.put(name, globalObject, jsObject); return null; } }); }
@Test public void callOverloadedFunction() { new ContextFactory().call(new ContextAction() { public Object run(Context cx) { cx.getWrapFactory().setJavaPrimitiveWrap(false); Assert.assertEquals("string[]", cx.evaluateString( cx.initStandardObjects(), "new org.mozilla.javascript.tests.Bug496585Test().method('one', 'two', 'three')", "<test>", 1, null)); Assert.assertEquals("string+function", cx.evaluateString( cx.initStandardObjects(), "new org.mozilla.javascript.tests.Bug496585Test().method('one', function() {})", "<test>", 1, null)); return null; } }); }
@Override public ScriptImpl compile(final Script script, final WorkflowParser parser) { ScriptImpl scriptImpl = new ScriptImpl(); scriptImpl.scriptService = this; scriptImpl.mappings = script.getMappings(); scriptImpl.compiledScript = contextFactory.call(new ContextAction() { public Object run(Context context) { try { return context.compileString(script.getScript(), "script", 1, null); } catch (Exception e) { parser.addWarning("Script doesn't compile: %s", e.getMessage()); return null; } } }); return scriptImpl; }
private Scriptable optionsAsJavaScriptObject(final Map<String, Object> options) { return (Scriptable) contextFactory.call(new ContextAction() { public Object run(Context cx) { Scriptable opts = cx.newObject(scope); for (Map.Entry<String, Object> entry : options.entrySet()) { String key = entry.getKey(); Object value = javaToJS(entry.getValue(), opts); opts.put(key, opts, value); } return opts; } }); }