Search in sources :

Example 6 with Context

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

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

Example 8 with Context

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

Example 9 with Context

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

Example 10 with Context

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

Aggregations

Context (org.mozilla.javascript.Context)153 Scriptable (org.mozilla.javascript.Scriptable)69 ScriptableObject (org.mozilla.javascript.ScriptableObject)63 ContextAction (org.mozilla.javascript.ContextAction)23 ContextFactory (org.mozilla.javascript.ContextFactory)22 Script (org.mozilla.javascript.Script)17 IOException (java.io.IOException)14 Function (org.mozilla.javascript.Function)13 ScriptContext (javax.script.ScriptContext)10 Test (org.junit.Test)8 RhinoException (org.mozilla.javascript.RhinoException)8 ArrayList (java.util.ArrayList)7 ContinuationPending (org.mozilla.javascript.ContinuationPending)7 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)7 InputStreamReader (java.io.InputStreamReader)6 Date (java.util.Date)6 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)6 JavaScriptException (org.mozilla.javascript.JavaScriptException)6 HashSet (java.util.HashSet)5 List (java.util.List)5