/** * Helper to extract a map of properties from a scriptable object (generally an associative array) * * @param scriptable The scriptable object to extract name/value pairs from. * @param map The map to add the converted name/value pairs to. */ private void extractScriptablePropertiesToMap(ScriptableObject scriptable, Map<QName, Serializable> map) { // get all the keys to the provided properties // and convert them to a Map of QName to Serializable objects Object[] propIds = scriptable.getIds(); for (int i = 0; i < propIds.length; i++) { // work on each key in turn Object propId = propIds[i]; // we are only interested in keys that are formed of Strings i.e. QName.toString() if (propId instanceof String) { // get the value out for the specified key - it must be Serializable String key = (String)propId; Object value = scriptable.get(key, scriptable); if (value instanceof Serializable) { value = getValueConverter().convertValueForRepo((Serializable)value); map.put(createQName(key), (Serializable)value); } } } }
/** * Extract a map of properties from a scriptable object (generally an associative array) * * @param scriptable The scriptable object to extract name/value pairs from. * @param map The map to add the converted name/value pairs to. */ private void extractScriptableProperties(ScriptableObject scriptable, Map<QName, Serializable> map) { // we need to get all the keys to the properties provided // and convert them to a Map of QName to Serializable objects Object[] propIds = scriptable.getIds(); for (int i = 0; i < propIds.length; i++) { // work on each key in turn Object propId = propIds[i]; // we are only interested in keys that are formed of Strings i.e. QName.toString() if (propId instanceof String) { // get the value out for the specified key - it must be Serializable String key = (String)propId; Object value = scriptable.get(key, scriptable); if (value instanceof Serializable) { value = getValueConverter().convertValueForRepo((Serializable)value); map.put(createQName(key), (Serializable)value); } } } }
private ScriptableObject getScope() { // Create a scope for the value conversion. This scope will be an empty scope exposing basic Object and Function, sufficient for value-conversion. // In case no context is active for the current thread, we can safely enter end exit one to get hold of a scope ScriptableObject scope; Context ctx = Context.getCurrentContext(); boolean closeContext = false; if (ctx == null) { ctx = Context.enter(); closeContext = true; } scope = ctx.initStandardObjects(); scope.setParentScope(null); if (closeContext) { Context.exit(); } return scope; }
public static void init(Context cx, Scriptable scope, boolean sealed) { NativeRegExp proto = new NativeRegExp(); proto.re = compileRE(cx, "", null, false); proto.activatePrototypeMap(MAX_PROTOTYPE_ID); proto.setParentScope(scope); proto.setPrototype(getObjectPrototype(scope)); NativeRegExpCtor ctor = new NativeRegExpCtor(); // Bug #324006: ECMA-262 15.10.6.1 says "The initial value of // RegExp.prototype.constructor is the builtin RegExp constructor." proto.defineProperty("constructor", ctor, ScriptableObject.DONTENUM); ScriptRuntime.setFunctionProtoAndParent(ctor, scope); ctor.setImmunePrototypeProperty(proto); if (sealed) { proto.sealObject(); ctor.sealObject(); } defineProperty(scope, "RegExp", ctor, ScriptableObject.DONTENUM); }
/** * 执行JS * * @param js js代码 * @param functionName js方法名称 * @param functionParams js方法参数 * @return */ public static String runScript(Context context, String js, String functionName, Object[] functionParams) { org.mozilla.javascript.Context rhino = org.mozilla.javascript.Context.enter(); rhino.setOptimizationLevel(-1); try { Scriptable scope = rhino.initStandardObjects(); ScriptableObject.putProperty(scope, "javaContext", org.mozilla.javascript.Context.javaToJS(context, scope)); ScriptableObject.putProperty(scope, "javaLoader", org.mozilla.javascript.Context.javaToJS(context.getClass().getClassLoader(), scope)); rhino.evaluateString(scope, js, context.getClass().getSimpleName(), 1, null); Function function = (Function) scope.get(functionName, scope); Object result = function.call(rhino, scope, scope, functionParams); if (result instanceof String) { return (String) result; } else if (result instanceof NativeJavaObject) { return (String) ((NativeJavaObject) result).getDefaultValue(String.class); } else if (result instanceof NativeObject) { return (String) ((NativeObject) result).getDefaultValue(String.class); } return result.toString();//(String) function.call(rhino, scope, scope, functionParams); } finally { org.mozilla.javascript.Context.exit(); } }
private String decryptSignature(String encryptedSig, String decryptionCode) throws DecryptException { Context context = Context.enter(); context.setOptimizationLevel(-1); Object result; try { ScriptableObject scope = context.initStandardObjects(); context.evaluateString(scope, decryptionCode, "decryptionCode", 1, null); Function decryptionFunc = (Function) scope.get("decrypt", scope); result = decryptionFunc.call(context, scope, scope, new Object[]{encryptedSig}); } catch (Exception e) { throw new DecryptException("could not get decrypt signature", e); } finally { Context.exit(); } return result == null ? "" : result.toString(); }
public static void init(Context cx, Scriptable scope, boolean sealed) { NativeRegExp proto = new NativeRegExp(); proto.re = (RECompiled)compileRE(cx, "", null, false); proto.activatePrototypeMap(MAX_PROTOTYPE_ID); proto.setParentScope(scope); proto.setPrototype(getObjectPrototype(scope)); NativeRegExpCtor ctor = new NativeRegExpCtor(); // Bug #324006: ECMA-262 15.10.6.1 says "The initial value of // RegExp.prototype.constructor is the builtin RegExp constructor." proto.defineProperty("constructor", ctor, ScriptableObject.DONTENUM); ScriptRuntime.setFunctionProtoAndParent(ctor, scope); ctor.setImmunePrototypeProperty(proto); if (sealed) { proto.sealObject(); ctor.sealObject(); } defineProperty(scope, "RegExp", ctor, ScriptableObject.DONTENUM); }
public void testJsApi() throws Exception { Context cx = Context.enter(); cx.setOptimizationLevel(-1); Script script = cx.compileReader(new InputStreamReader( Bug482203.class.getResourceAsStream("conttest.js")), "", 1, null); Scriptable scope = cx.initStandardObjects(); script.exec(cx, scope); for(;;) { Object cont = ScriptableObject.getProperty(scope, "c"); if(cont == null) { break; } ((Callable)cont).call(cx, scope, scope, new Object[] { null }); } }
public void testJavaApi() throws Exception { Context cx = Context.enter(); try { cx.setOptimizationLevel(-1); Script script = cx.compileReader(new InputStreamReader( Bug482203.class.getResourceAsStream("conttest.js")), "", 1, null); Scriptable scope = cx.initStandardObjects(); cx.executeScriptWithContinuations(script, scope); for(;;) { Object cont = ScriptableObject.getProperty(scope, "c"); if(cont == null) { break; } cx.resumeContinuation(cont, scope, null); } } finally { Context.exit(); } }
/** * 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); }
@SuppressWarnings("unchecked") @Override protected void setUp() { // set up a reference map reference = new ArrayList<Object>(); reference.add("a"); reference.add(Boolean.TRUE); reference.add(new HashMap<Object, Object>()); reference.add(new Integer(42)); reference.add("a"); // get a js object as map Context context = Context.enter(); ScriptableObject scope = context.initStandardObjects(); list = (List<Object>) context.evaluateString(scope, "(['a', true, new java.util.HashMap(), 42, 'a']);", "testsrc", 1, null); Context.exit(); }
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); }
private static Test createTest(final File testDir, final String name) { return new TestCase(name) { @Override public int countTestCases() { return 1; } @Override public void runBare() throws Throwable { final Context cx = Context.enter(); try { cx.setOptimizationLevel(-1); final Scriptable scope = cx.initStandardObjects(); ScriptableObject.putProperty(scope, "print", new Print(scope)); createRequire(testDir, cx, scope).requireMain(cx, "program"); } finally { Context.exit(); } } }; }
public void testSetNullForScriptableSetter() throws Exception { final String scriptCode = "foo.myProp = new Foo2();\n" + "foo.myProp = null;"; final ContextFactory factory = new ContextFactory(); final Context cx = factory.enterContext(); try { final ScriptableObject topScope = cx.initStandardObjects(); final Foo foo = new Foo(); // define custom setter method final Method setMyPropMethod = Foo.class.getMethod("setMyProp", Foo2.class); foo.defineProperty("myProp", null, null, setMyPropMethod, ScriptableObject.EMPTY); topScope.put("foo", topScope, foo); ScriptableObject.defineClass(topScope, Foo2.class); cx.evaluateString(topScope, scriptCode, "myScript", 1, null); } finally { Context.exit(); } }
@SuppressWarnings("unchecked") @Override protected void setUp() { // set up a reference map reference = new LinkedHashMap<Object, Object>(); reference.put("a", "a"); reference.put("b", Boolean.TRUE); reference.put("c", new HashMap<Object, Object>()); reference.put(new Integer(1), new Integer(42)); // get a js object as map Context context = Context.enter(); ScriptableObject scope = context.initStandardObjects(); map = (Map<Object, Object>) context.evaluateString(scope, "({ a: 'a', b: true, c: new java.util.HashMap(), 1: 42});", "testsrc", 1, null); Context.exit(); }
/** * After finding the decrypted code in the js html5 player code * run the code passing the encryptedSig parameter * * @param encryptedSig * @param html5player * @return * @throws Exception */ private static String decipherKey(String encryptedSig, String html5player) throws Exception { String decipherFunc = loadFunction(html5player); Context context = Context.enter(); // Rhino interpreted mode context.setOptimizationLevel(-1); Object result = null; try { ScriptableObject scope = context.initStandardObjects(); context.evaluateString(scope, decipherFunc, "decipherFunc", 1, null); Function decryptionFunc = (Function) scope.get("decrypt", scope); result = decryptionFunc.call(context, scope, scope, new Object[]{encryptedSig}); } catch (Exception e) { e.printStackTrace(); } finally { Context.exit(); } if (result == null) { return ""; } else { return result.toString(); } }
@JSStaticFunction public static Object getContext() { Context jsContext = KakaoTalkListener.getJsEngines()[0].getContext(); ScriptableObject scope = KakaoTalkListener.getJsEngines()[0].getScope(); return jsContext.javaToJS(MainActivity.getContext(), scope); }
@Benchmark @SuppressWarnings("unused") void accessFields(int count) { Function create = (Function)ScriptableObject.getProperty(scope, "createObject"); Object o = create.call(cx, scope, null, new Object[]{1, strings, ints}); Function access = (Function)ScriptableObject.getProperty(scope, "accessObject"); access.call(cx, scope, null, new Object[] {count, o, strings, ints}); }
@Benchmark @SuppressWarnings("unused") void iterateFields(long count) { Function create = (Function)ScriptableObject.getProperty(scope, "createObject"); Object o = create.call(cx, scope, null, new Object[]{1, strings, ints}); Function iterate = (Function)ScriptableObject.getProperty(scope, "iterateObject"); iterate.call(cx, scope, null, new Object[] {count, o}); }
@Benchmark @SuppressWarnings("unused") void ownKeysFields(long count) { Function create = (Function)ScriptableObject.getProperty(scope, "createObject"); Object o = create.call(cx, scope, null, new Object[]{1, strings, ints}); Function iterate = (Function)ScriptableObject.getProperty(scope, "iterateOwnKeysObject"); iterate.call(cx, scope, null, new Object[] {count, o}); }
private void test(int variables) { double expected = (variables * (variables - 1)) / 2d; StringBuilder sb = new StringBuilder(); sb.append("function F (){\n"); for (int i = 0; i < variables; ++i) { sb.append("const x_").append(i).append("=").append(i).append(";"); } sb.append("return 0"); for (int i = 0; i < variables; ++i) { sb.append("+").append("x_").append(i); } sb.append("}; F()"); ScriptableObject scope = cx.initStandardObjects(); Object ret = cx.evaluateString(scope, sb.toString(), "<eval>", 1, null); assertTrue(ret instanceof Number); assertEquals(expected, ((Number) ret).doubleValue(), 0); }
@Test public void test() throws IllegalAccessException, InstantiationException, InvocationTargetException { Context cx = Context.enter(); try { ScriptableObject scope = cx.initStandardObjects(); // define two classes that share a parent prototype ScriptableObject.defineClass(scope, Fruit.class, false, true); ScriptableObject.defineClass(scope, Vegetable.class, false, true); assertEquals(Boolean.TRUE, evaluate(cx, scope, "(new Fruit instanceof Food)")); assertEquals(Boolean.TRUE, evaluate(cx, scope, "(new Vegetable instanceof Food)")); } finally { Context.exit(); } }
@Test public void test_getElem() { String fn = "function test(){ return ''['foo'] }"; runWithAllOptimizationLevels(action(fn, new Action() { public void run(Context cx, ScriptableObject scope1, ScriptableObject scope2) { eval(cx, scope1, "String.prototype.foo = 'scope1'"); eval(cx, scope2, "String.prototype.foo = 'scope2'"); assertEquals("scope2", eval(cx, scope2, "test()")); assertEquals("scope2", eval(cx, scope1, "scope2.test()")); assertEquals("scope2", eval(cx, scope1, "scope2.test.call(null)")); assertEquals("scope2", eval(cx, scope1, "var t=scope2.test; t()")); assertEquals("scope2", eval(cx, scope1, "var t=scope2.test; t.call(null)")); } })); }
@Test public void test_getProp() { String fn = "function test(){ return ''.foo }"; runWithAllOptimizationLevels(action(fn, new Action() { public void run(Context cx, ScriptableObject scope1, ScriptableObject scope2) { eval(cx, scope1, "String.prototype.foo = 'scope1'"); eval(cx, scope2, "String.prototype.foo = 'scope2'"); assertEquals("scope2", eval(cx, scope2, "test()")); assertEquals("scope2", eval(cx, scope1, "scope2.test()")); assertEquals("scope2", eval(cx, scope1, "scope2.test.call(null)")); assertEquals("scope2", eval(cx, scope1, "var t=scope2.test; t()")); assertEquals("scope2", eval(cx, scope1, "var t=scope2.test; t.call(null)")); } })); }
@Test public void test_setElem() { String fn = "function test(){ ''['foo'] = '_' }"; runWithAllOptimizationLevels(action(fn, new Action() { public void run(Context cx, ScriptableObject scope1, ScriptableObject scope2) { String code = ""; code += "String.prototype.c = 0;"; code += "Object.defineProperty(String.prototype, 'foo', {" + " set: function(v){ this.__proto__.c++ }})"; eval(cx, scope1, code); eval(cx, scope2, code); eval(cx, scope2, "test()"); eval(cx, scope1, "scope2.test()"); eval(cx, scope1, "scope2.test.call(null)"); eval(cx, scope1, "var t=scope2.test; t()"); eval(cx, scope1, "var t=scope2.test; t.call(null)"); assertTRUE(eval(cx, scope1, "0 == String.prototype.c")); assertTRUE(eval(cx, scope2, "5 == String.prototype.c")); } })); }
@Test public void test_setProp() { String fn = "function test(){ ''.foo = '_' }"; runWithAllOptimizationLevels(action(fn, new Action() { public void run(Context cx, ScriptableObject scope1, ScriptableObject scope2) { String code = ""; code += "String.prototype.c = 0;"; code += "Object.defineProperty(String.prototype, 'foo', {" + " set: function(v){ this.__proto__.c++ }})"; eval(cx, scope1, code); eval(cx, scope2, code); eval(cx, scope2, "test()"); eval(cx, scope1, "scope2.test()"); eval(cx, scope1, "scope2.test.call(null)"); eval(cx, scope1, "var t=scope2.test; t()"); eval(cx, scope1, "var t=scope2.test; t.call(null)"); assertTRUE(eval(cx, scope1, "0 == String.prototype.c")); assertTRUE(eval(cx, scope2, "5 == String.prototype.c")); } })); }
@Test public void test_setElemIncDec() { String fn = "function test(){ ''['foo']++ }"; runWithAllOptimizationLevels(action(fn, new Action() { public void run(Context cx, ScriptableObject scope1, ScriptableObject scope2) { String code = ""; code += "String.prototype.c = 0;"; code += "Object.defineProperty(String.prototype, 'foo', {" + " set: function(v){ this.__proto__.c++ }})"; eval(cx, scope1, code); eval(cx, scope2, code); eval(cx, scope2, "test()"); eval(cx, scope1, "scope2.test()"); eval(cx, scope1, "scope2.test.call(null)"); eval(cx, scope1, "var t=scope2.test; t()"); eval(cx, scope1, "var t=scope2.test; t.call(null)"); assertTRUE(eval(cx, scope1, "0 == String.prototype.c")); assertTRUE(eval(cx, scope2, "5 == String.prototype.c")); } })); }
@Test public void test_setPropIncDec() { String fn = "function test(){ ''.foo++ }"; runWithAllOptimizationLevels(action(fn, new Action() { public void run(Context cx, ScriptableObject scope1, ScriptableObject scope2) { String code = ""; code += "String.prototype.c = 0;"; code += "Object.defineProperty(String.prototype, 'foo', {" + " set: function(v){ this.__proto__.c++ }})"; eval(cx, scope1, code); eval(cx, scope2, code); eval(cx, scope2, "test()"); eval(cx, scope1, "scope2.test()"); eval(cx, scope1, "scope2.test.call(null)"); eval(cx, scope1, "var t=scope2.test; t()"); eval(cx, scope1, "var t=scope2.test; t.call(null)"); assertTRUE(eval(cx, scope1, "0 == String.prototype.c")); assertTRUE(eval(cx, scope2, "5 == String.prototype.c")); } })); }
@Test public void test_setElemOp1() { String fn = "function test(){ return ''['foo'] += '_' }"; runWithAllOptimizationLevels(action(fn, new Action() { public void run(Context cx, ScriptableObject scope1, ScriptableObject scope2) { String code = ""; code += "String.prototype.c = 0;"; code += "Object.defineProperty(String.prototype, 'foo', {" + " set: function(v){ this.__proto__.c++ }})"; eval(cx, scope1, code); eval(cx, scope2, code); eval(cx, scope2, "test()"); eval(cx, scope1, "scope2.test()"); eval(cx, scope1, "scope2.test.call(null)"); eval(cx, scope1, "var t=scope2.test; t()"); eval(cx, scope1, "var t=scope2.test; t.call(null)"); assertTRUE(eval(cx, scope1, "0 == String.prototype.c")); assertTRUE(eval(cx, scope2, "5 == String.prototype.c")); } })); }
@Test public void test_setPropOp2() { String fn = "function test(){ return ''.foo += '_' }"; runWithAllOptimizationLevels(action(fn, new Action() { public void run(Context cx, ScriptableObject scope1, ScriptableObject scope2) { eval(cx, scope1, "String.prototype.foo = 'scope1'"); eval(cx, scope2, "String.prototype.foo = 'scope2'"); assertEquals("scope2_", eval(cx, scope2, "test()")); assertEquals("scope2_", eval(cx, scope1, "scope2.test()")); assertEquals("scope2_", eval(cx, scope1, "scope2.test.call(null)")); assertEquals("scope2_", eval(cx, scope1, "var t=scope2.test; t()")); assertEquals("scope2_", eval(cx, scope1, "var t=scope2.test; t.call(null)")); } })); }
@Test public void test_getElemCall() { String fn = "function test(){ return ''['foo']() }"; runWithAllOptimizationLevels(action(fn, new Action() { public void run(Context cx, ScriptableObject scope1, ScriptableObject scope2) { eval(cx, scope1, "String.prototype.foo = function(){ return 'scope1' }"); eval(cx, scope2, "String.prototype.foo = function(){ return 'scope2' }"); assertEquals("scope2", eval(cx, scope2, "test()")); assertEquals("scope2", eval(cx, scope1, "scope2.test()")); assertEquals("scope2", eval(cx, scope1, "scope2.test.call(null)")); assertEquals("scope2", eval(cx, scope1, "var t=scope2.test; t()")); assertEquals("scope2", eval(cx, scope1, "var t=scope2.test; t.call(null)")); } })); }
@Test public void test_getPropCall() { String fn = "function test(){ return ''.foo() }"; runWithAllOptimizationLevels(action(fn, new Action() { public void run(Context cx, ScriptableObject scope1, ScriptableObject scope2) { eval(cx, scope1, "String.prototype.foo = function(){ return 'scope1' }"); eval(cx, scope2, "String.prototype.foo = function(){ return 'scope2' }"); assertEquals("scope2", eval(cx, scope2, "test()")); assertEquals("scope2", eval(cx, scope1, "scope2.test()")); assertEquals("scope2", eval(cx, scope1, "scope2.test.call(null)")); assertEquals("scope2", eval(cx, scope1, "var t=scope2.test; t()")); assertEquals("scope2", eval(cx, scope1, "var t=scope2.test; t.call(null)")); } })); }
@Test public void test_ReturnThisNestedCall() { String fn = "function test(o){ return (function(){ return this }).call(o) }"; runWithAllOptimizationLevels(action(fn, new Action() { public void run(Context cx, ScriptableObject scope1, ScriptableObject scope2) { assertSame(scope2, eval(cx, scope2, "test()")); assertSame(scope2, eval(cx, scope2, "test(null)")); assertSame(scope2, eval(cx, scope2, "test.call(null)")); assertSame(scope2, eval(cx, scope2, "test.call(null, null)")); assertSame(scope1, eval(cx, scope1, "scope2.test()")); assertSame(scope1, eval(cx, scope1, "scope2.test(null)")); assertSame(scope1, eval(cx, scope1, "scope2.test.call(null)")); assertSame(scope1, eval(cx, scope1, "scope2.test.call(null, null)")); assertSame(scope1, eval(cx, scope1, "var t=scope2.test; t()")); assertSame(scope1, eval(cx, scope1, "var t=scope2.test; t(null)")); assertSame(scope1, eval(cx, scope1, "var t=scope2.test; t.call(null)")); assertSame(scope1, eval(cx, scope1, "var t=scope2.test; t.call(null, null)")); } })); }
public void testJsApi() throws Exception { Context cx = RhinoAndroidHelper.prepareContext(); try { cx.setOptimizationLevel(-1); Script script = cx.compileString(TestUtils.readAsset("Bug482203.js"), "", 1, null); Scriptable scope = cx.initStandardObjects(); script.exec(cx, scope); int counter = 0; for(;;) { Object cont = ScriptableObject.getProperty(scope, "c"); if(cont == null) { break; } counter++; ((Callable)cont).call(cx, scope, scope, new Object[] { null }); } assertEquals(counter, 5); assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result")); } finally { Context.exit(); } }
public void testJavaApi() throws Exception { Context cx = RhinoAndroidHelper.prepareContext(); try { cx.setOptimizationLevel(-1); Script script = cx.compileString(TestUtils.readAsset("Bug482203.js"), "", 1, null); Scriptable scope = cx.initStandardObjects(); cx.executeScriptWithContinuations(script, scope); int counter = 0; for(;;) { Object cont = ScriptableObject.getProperty(scope, "c"); if(cont == null) { break; } counter++; cx.resumeContinuation(cont, scope, null); } assertEquals(counter, 5); assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result")); } finally { Context.exit(); } }