use of org.mozilla.javascript.Context in project neo4j by neo4j.
the class TestWhiteListJavaWrapper method shouldAllowAccessToWhiteListedClassMembers.
@Test
public void shouldAllowAccessToWhiteListedClassMembers() throws Exception {
// XXX: The Rhino security stuff can only be set globally, unfortunately. That means
// that we need to use a class here that is white-listed in the "real" white list, because
// other tests will already have configured global security settings that we cannot override.
// Given
WhiteListJavaWrapper wrapper = new WhiteListJavaWrapper(new WhiteListClassShutter(UserScriptClassWhiteList.getWhiteList()));
Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();
// When
Object wrapped = wrapper.wrap(cx, scope, DynamicRelationshipType.withName("blah"), null);
// Then
assertThat(wrapped, is(instanceOf(NativeJavaObject.class)));
NativeJavaObject obj = (NativeJavaObject) wrapped;
assertThat(obj.get("name", scope), is(instanceOf(NativeJavaMethod.class)));
}
use of org.mozilla.javascript.Context in project hackpad by dropbox.
the class GlobalParseXTest 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.Context 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.Context 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.Context 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);
}
Aggregations