use of org.mozilla.javascript.NativeJavaObject 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.NativeJavaObject 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.NativeJavaObject 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.NativeJavaObject in project BPjs by bThink-BGU.
the class JsEventSetTest method testJsSetData.
@Test
public void testJsSetData() throws InterruptedException, URISyntaxException {
try {
Context.enter();
BProgram bpr = new StringBProgram("Eventset", "var es=bp.EventSet('a',function(e){return e.name=='a';});\n");
new BProgramRunner(bpr).run();
NativeJavaObject sut = (NativeJavaObject) bpr.getGlobalScope().get("es", bpr.getGlobalScope());
JsEventSet jsSut = (JsEventSet) Context.jsToJava(sut, JsEventSet.class);
assertEquals("a", jsSut.getName());
assertTrue(jsSut.toString().contains("a"));
assertTrue(jsSut.toString().contains("JsEventSet"));
assertTrue(jsSut.contains(BEvent.named("a")));
assertFalse(jsSut.contains(BEvent.named("b")));
} finally {
Context.exit();
}
}
use of org.mozilla.javascript.NativeJavaObject in project scriptographer by scriptographer.
the class TopLevel method toJava.
/**
* Convert an object into a wrapper that exposes the java methods of the
* object to JavaScript. This is useful for treating native numbers,
* strings, etc as their java counterpart such as java.lang.Double,
* java.lang.String etc.
*
* @param thisObj a java object that is wrapped in a special way Rhino
* @return the object wrapped as NativeJavaObject, exposing the public
* methods of the underlying class.
*/
public static Object toJava(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
if (thisObj == null || thisObj instanceof NativeJavaObject || thisObj == Undefined.instance) {
return thisObj;
}
Scriptable topLevel = ScriptRuntime.getTopCallScope(cx);
Object obj = thisObj;
if (thisObj instanceof Wrapper) {
obj = ((Wrapper) thisObj).unwrap();
} else {
if ("Date".equals(thisObj.getClassName())) {
return new NativeJavaObject(topLevel, new Date((long) ScriptRuntime.toNumber(thisObj)), null);
}
}
return new NativeJavaObject(topLevel, obj, null);
}
Aggregations