use of org.mozilla.javascript.Scriptable in project neo4j by neo4j.
the class JavascriptExecutor method execute.
@Override
public Object execute(Map<String, Object> variables) throws EvaluationException {
Context cx = Context.enter();
try {
Scriptable scope = cx.newObject(prototype);
scope.setPrototype(prototype);
if (variables != null) {
for (String k : variables.keySet()) {
scope.put(k, scope, variables.get(k));
}
}
Object out = compiledScript.exec(cx, scope);
if (out instanceof NativeJavaObject) {
return ((NativeJavaObject) out).unwrap();
} else if (out instanceof Undefined) {
return null;
} else {
return out;
}
} catch (RhinoException e) {
throw new EvaluationException("Failed to execute script, see nested exception.", e);
} finally {
Context.exit();
}
}
use of org.mozilla.javascript.Scriptable in project neo4j by neo4j.
the class TestWhiteListJavaWrapper method shouldDownCastSubclassesToAllowedParentClass.
@Test
public void shouldDownCastSubclassesToAllowedParentClass() throws Exception {
// Given
Set<String> whiteList = new HashSet<String>();
whiteList.add(Object.class.getName());
WhiteListJavaWrapper wrapper = new WhiteListJavaWrapper(new WhiteListClassShutter(whiteList));
Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();
// When
Object wrapped = wrapper.wrap(cx, scope, new TestWhiteListJavaWrapper(), null);
// Then
assertThat(wrapped, is(instanceOf(NativeJavaObject.class)));
NativeJavaObject obj = (NativeJavaObject) wrapped;
assertThat(obj.has("aGetter", scope), is(false));
assertThat((UniqueTag) obj.get("aGetter", scope), is(UniqueTag.NOT_FOUND));
}
use of org.mozilla.javascript.Scriptable in project neo4j by neo4j.
the class TestWhiteListJavaWrapper method shouldThrowSecurityExceptionWhenAccessingLockedClasses.
@Test(expected = SecurityException.class)
public void shouldThrowSecurityExceptionWhenAccessingLockedClasses() throws Exception {
// Given
Set<String> whiteList = new HashSet<String>();
whiteList.add(Object.class.getName());
WhiteListJavaWrapper wrapper = new WhiteListJavaWrapper(new WhiteListClassShutter(whiteList));
Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();
// When
Object wrapped = wrapper.wrap(cx, scope, TestWhiteListJavaWrapper.class, null);
}
use of org.mozilla.javascript.Scriptable 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.Scriptable 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);
}
Aggregations