Search in sources :

Example 1 with Scriptable

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();
    }
}
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 Scriptable

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));
}
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 Scriptable

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

Example 4 with Scriptable

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)));
}
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 5 with Scriptable

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

Aggregations

Scriptable (org.mozilla.javascript.Scriptable)164 Context (org.mozilla.javascript.Context)70 ScriptableObject (org.mozilla.javascript.ScriptableObject)49 ArrayList (java.util.ArrayList)17 Test (org.junit.Test)17 ContextAction (org.mozilla.javascript.ContextAction)12 Map (java.util.Map)10 Function (org.mozilla.javascript.Function)10 Script (org.mozilla.javascript.Script)10 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)9 Serializable (java.io.Serializable)8 NativeObject (org.mozilla.javascript.NativeObject)8 Date (java.util.Date)7 RhinoException (org.mozilla.javascript.RhinoException)7 IOException (java.io.IOException)6 ValueConverter (org.alfresco.repo.jscript.ValueConverter)6 BProgram (il.ac.bgu.cs.bp.bpjs.model.BProgram)5 HashMap (java.util.HashMap)5 Notifier (org.apache.cxf.javascript.JavascriptTestUtilities.Notifier)5 Wrapper (org.mozilla.javascript.Wrapper)5