use of org.mozilla.javascript.Scriptable in project hackpad by dropbox.
the class NativeStringTest method assertEvaluates.
private void assertEvaluates(final Object expected, final String source) {
final ContextAction action = new ContextAction() {
public Object run(Context cx) {
final Scriptable scope = cx.initStandardObjects();
final Object rep = cx.evaluateString(scope, source, "test.js", 0, null);
assertEquals(expected, rep);
return null;
}
};
Utils.runWithAllOptimizationLevels(action);
}
use of org.mozilla.javascript.Scriptable in project hackpad by dropbox.
the class ObserveInstructionCountTest method baseCase.
private void baseCase(int optimizationLevel, String source) {
ContextFactory factory = new MyFactory();
Context cx = factory.enterContext();
cx.setOptimizationLevel(optimizationLevel);
assertTrue(cx instanceof MyContext);
try {
Scriptable globalScope = cx.initStandardObjects();
cx.evaluateString(globalScope, source, "test source", 1, null);
fail();
} catch (QuotaExceeded e) {
// expected
} catch (RuntimeException e) {
fail(e.toString());
} finally {
Context.exit();
}
}
use of org.mozilla.javascript.Scriptable in project hackpad by dropbox.
the class PrimitiveTypeScopeResolutionTest method functionObjectPrimitiveToObject.
/**
* Test that FunctionObject use the right top scope to convert a primitive
* to an object
*/
@Test
public void functionObjectPrimitiveToObject() throws Exception {
final String scriptScope2 = "function f() {\n" + "String.prototype.foo = 'from 2'; \n" + "var s2 = 's2';\n" + "var s2Foo = s2.foo;\n" + "var s2FooReadByFunction = myObject.readPropFoo(s2);\n" + "if (s2Foo != s2FooReadByFunction)\n" + "throw 's2 got: ' + s2FooReadByFunction;\n" + "}";
// define object with custom method
final MyObject myObject = new MyObject();
final String[] functionNames = { "readPropFoo" };
myObject.defineFunctionProperties(functionNames, MyObject.class, ScriptableObject.EMPTY);
final String scriptScope1 = "String.prototype.foo = 'from 1'; scope2.f()";
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"));
scope2.put("myObject", scope2, myObject);
cx.evaluateString(scope2, scriptScope2, "source2", 1, null);
scope1.put("scope2", scope1, scope2);
return cx.evaluateString(scope1, scriptScope1, "source1", 1, null);
}
};
Utils.runWithAllOptimizationLevels(action);
}
use of org.mozilla.javascript.Scriptable in project hackpad by dropbox.
the class TypeOfTest method test0.
/**
* 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);
}
use of org.mozilla.javascript.Scriptable in project hackpad by dropbox.
the class TypeOfTest method testCustomizeTypeOf.
private void testCustomizeTypeOf(final String expected, final Scriptable obj) {
final ContextAction action = new ContextAction() {
public Object run(final Context context) {
final Scriptable scope = context.initStandardObjects();
scope.put("myObj", scope, obj);
return context.evaluateString(scope, "typeof myObj", "test script", 1, null);
}
};
doTest(expected, action);
}
Aggregations