Search in sources :

Example 11 with Scriptable

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

the class TypeOfTest method doTest.

private void doTest(String expected, final String script) {
    final ContextAction action = new ContextAction() {

        public Object run(final Context context) {
            final Scriptable scope = context.initStandardObjects();
            return context.evaluateString(scope, script, "test script", 1, null);
        }
    };
    doTest(expected, action);
}
Also used : Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) Scriptable(org.mozilla.javascript.Scriptable)

Example 12 with Scriptable

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

the class Utils method executeScript.

/**
	 * Execute the provided script in a fresh context as "myScript.js".
	 * @param script the script code
	 */
static void executeScript(final String script, final int optimizationLevel) {
    final ContextAction action = new ContextAction() {

        public Object run(Context cx) {
            final Scriptable scope = cx.initStandardObjects();
            return cx.evaluateString(scope, script, "myScript.js", 1, null);
        }
    };
    Utils.runWithOptimizationLevel(action, optimizationLevel);
}
Also used : Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) Scriptable(org.mozilla.javascript.Scriptable)

Example 13 with Scriptable

use of org.mozilla.javascript.Scriptable 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 14 with Scriptable

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

the class JsTestsBase method runJsTest.

public void runJsTest(Context cx, Scriptable shared, String name, String source) {
    // create a lightweight top-level scope
    Scriptable scope = cx.newObject(shared);
    scope.setPrototype(shared);
    System.out.print(name + ": ");
    Object result;
    try {
        result = cx.evaluateString(scope, source, "jstest input", 1, null);
    } catch (RuntimeException e) {
        System.out.println("FAILED");
        throw e;
    }
    assertTrue(result != null);
    assertTrue("success".equals(result));
    System.out.println("passed");
}
Also used : Scriptable(org.mozilla.javascript.Scriptable)

Example 15 with Scriptable

use of org.mozilla.javascript.Scriptable 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

Scriptable (org.mozilla.javascript.Scriptable)151 Context (org.mozilla.javascript.Context)67 ScriptableObject (org.mozilla.javascript.ScriptableObject)47 Test (org.junit.Test)17 ContextAction (org.mozilla.javascript.ContextAction)12 Script (org.mozilla.javascript.Script)10 Function (org.mozilla.javascript.Function)9 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)9 ArrayList (java.util.ArrayList)8 NativeObject (org.mozilla.javascript.NativeObject)8 Date (java.util.Date)7 RhinoException (org.mozilla.javascript.RhinoException)7 IOException (java.io.IOException)6 Map (java.util.Map)6 BProgram (il.ac.bgu.cs.bp.bpjs.model.BProgram)5 Notifier (org.apache.cxf.javascript.JavascriptTestUtilities.Notifier)5 KettleException (org.pentaho.di.core.exception.KettleException)5 File (java.io.File)4 InputStreamReader (java.io.InputStreamReader)4 EvaluatorException (org.mozilla.javascript.EvaluatorException)4