Search in sources :

Example 1 with NativeJavaObject

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();
    }
}
Also used : Context(org.mozilla.javascript.Context) Undefined(org.mozilla.javascript.Undefined) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) RhinoException(org.mozilla.javascript.RhinoException) EvaluationException(org.neo4j.server.rest.domain.EvaluationException) Scriptable(org.mozilla.javascript.Scriptable) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 2 with NativeJavaObject

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));
}
Also used : Context(org.mozilla.javascript.Context) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) Scriptable(org.mozilla.javascript.Scriptable) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with NativeJavaObject

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)));
}
Also used : Context(org.mozilla.javascript.Context) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) Scriptable(org.mozilla.javascript.Scriptable) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) Test(org.junit.Test)

Example 4 with NativeJavaObject

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();
    }
}
Also used : BProgramRunner(il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner) SingleResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) JsEventSet(il.ac.bgu.cs.bp.bpjs.model.eventsets.JsEventSet) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) Test(org.junit.Test)

Example 5 with NativeJavaObject

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);
}
Also used : Wrapper(org.mozilla.javascript.Wrapper) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) Scriptable(org.mozilla.javascript.Scriptable) Date(java.util.Date)

Aggregations

NativeJavaObject (org.mozilla.javascript.NativeJavaObject)11 Context (org.mozilla.javascript.Context)5 Scriptable (org.mozilla.javascript.Scriptable)5 ScriptableObject (org.mozilla.javascript.ScriptableObject)4 Test (org.junit.Test)3 NativeArray (org.mozilla.javascript.NativeArray)2 BProgramRunner (il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner)1 BProgram (il.ac.bgu.cs.bp.bpjs.model.BProgram)1 SingleResourceBProgram (il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram)1 StringBProgram (il.ac.bgu.cs.bp.bpjs.model.StringBProgram)1 JsEventSet (il.ac.bgu.cs.bp.bpjs.model.eventsets.JsEventSet)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ConsString (org.mozilla.javascript.ConsString)1 Function (org.mozilla.javascript.Function)1 NativeFunction (org.mozilla.javascript.NativeFunction)1