Search in sources :

Example 16 with Context

use of org.mozilla.javascript.Context in project hackpad by dropbox.

the class WriteReadOnlyPropertyTest method testWriteReadOnly.

void testWriteReadOnly(final boolean acceptWriteReadOnly) throws Exception {
    final Method readMethod = Foo.class.getMethod("getMyProp", (Class[]) null);
    final Foo foo = new Foo("hello");
    foo.defineProperty("myProp", null, readMethod, null, ScriptableObject.EMPTY);
    final String script = "foo.myProp = 123; foo.myProp";
    final ContextAction action = new ContextAction() {

        public Object run(final Context cx) {
            final ScriptableObject top = cx.initStandardObjects();
            ScriptableObject.putProperty(top, "foo", foo);
            cx.evaluateString(top, script, "script", 0, null);
            return null;
        }
    };
    final ContextFactory contextFactory = new ContextFactory() {

        @Override
        protected boolean hasFeature(final Context cx, final int featureIndex) {
            if (Context.FEATURE_STRICT_MODE == featureIndex) {
                return !acceptWriteReadOnly;
            }
            return super.hasFeature(cx, featureIndex);
        }
    };
    contextFactory.call(action);
}
Also used : Context(org.mozilla.javascript.Context) ContextFactory(org.mozilla.javascript.ContextFactory) ContextAction(org.mozilla.javascript.ContextAction) ScriptableObject(org.mozilla.javascript.ScriptableObject) Method(java.lang.reflect.Method)

Example 17 with Context

use of org.mozilla.javascript.Context in project hackpad by dropbox.

the class RequireTest method testSandboxed.

public void testSandboxed() throws Exception {
    final Context cx = createContext();
    final Require require = getSandboxedRequire(cx);
    require.requireMain(cx, "testSandboxed");
    // Also, test idempotent double-require of same main:
    require.requireMain(cx, "testSandboxed");
    // Also, test failed require of different main:
    try {
        require.requireMain(cx, "blah");
        fail();
    } catch (IllegalStateException e) {
    // Expected, success
    }
}
Also used : Context(org.mozilla.javascript.Context) Require(org.mozilla.javascript.commonjs.module.Require)

Example 18 with Context

use of org.mozilla.javascript.Context in project hackpad by dropbox.

the class RequireTest method testNonSandboxed.

public void testNonSandboxed() throws Exception {
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    final String jsFile = getClass().getResource("testNonSandboxed.js").toExternalForm();
    ScriptableObject.putProperty(scope, "moduleUri", jsFile);
    require.requireMain(cx, "testNonSandboxed");
}
Also used : Context(org.mozilla.javascript.Context) Require(org.mozilla.javascript.commonjs.module.Require) Scriptable(org.mozilla.javascript.Scriptable)

Example 19 with Context

use of org.mozilla.javascript.Context in project hackpad by dropbox.

the class RequireTest method createContext.

private Context createContext() {
    final Context cx = Context.enter();
    cx.setOptimizationLevel(-1);
    return cx;
}
Also used : Context(org.mozilla.javascript.Context)

Example 20 with Context

use of org.mozilla.javascript.Context in project hackpad by dropbox.

the class JsTestsBase method runJsTests.

public void runJsTests(File[] tests) throws IOException {
    ContextFactory factory = ContextFactory.getGlobal();
    Context cx = factory.enterContext();
    try {
        cx.setOptimizationLevel(this.optimizationLevel);
        Scriptable shared = cx.initStandardObjects();
        for (File f : tests) {
            // don't worry about very long
            int length = (int) f.length();
            // files
            char[] buf = new char[length];
            new FileReader(f).read(buf, 0, length);
            String session = new String(buf);
            runJsTest(cx, shared, f.getName(), session);
        }
    } finally {
        Context.exit();
    }
}
Also used : ContextFactory(org.mozilla.javascript.ContextFactory) Context(org.mozilla.javascript.Context) FileReader(java.io.FileReader) Scriptable(org.mozilla.javascript.Scriptable) File(java.io.File)

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