Search in sources :

Example 1 with Script

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

the class Main method loadCompiledScript.

private static Script loadCompiledScript(Context cx, String path, byte[] data, Object securityDomain) {
    if (data == null) {
        exitCode = EXITCODE_FILE_NOT_FOUND;
        return null;
    }
    // XXX: For now extract class name of compiled Script from path
    // instead of parsing class bytes
    int nameStart = path.lastIndexOf('/');
    if (nameStart < 0) {
        nameStart = 0;
    } else {
        ++nameStart;
    }
    int nameEnd = path.lastIndexOf('.');
    if (nameEnd < nameStart) {
        // '.' does not exist in path (nameEnd < 0)
        // or it comes before nameStart
        nameEnd = path.length();
    }
    String name = path.substring(nameStart, nameEnd);
    try {
        GeneratedClassLoader loader = SecurityController.createLoader(cx.getApplicationClassLoader(), securityDomain);
        Class<?> clazz = loader.defineClass(name, data);
        loader.linkClass(clazz);
        if (!Script.class.isAssignableFrom(clazz)) {
            throw Context.reportRuntimeError("msg.must.implement.Script");
        }
        return (Script) clazz.newInstance();
    } catch (RhinoException rex) {
        ToolErrorReporter.reportException(cx.getErrorReporter(), rex);
        exitCode = EXITCODE_RUNTIME_ERROR;
    } catch (IllegalAccessException iaex) {
        exitCode = EXITCODE_RUNTIME_ERROR;
        Context.reportError(iaex.toString());
    } catch (InstantiationException inex) {
        exitCode = EXITCODE_RUNTIME_ERROR;
        Context.reportError(inex.toString());
    }
    return null;
}
Also used : Script(org.mozilla.javascript.Script) GeneratedClassLoader(org.mozilla.javascript.GeneratedClassLoader) RhinoException(org.mozilla.javascript.RhinoException)

Example 2 with Script

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

the class Bug482203 method testJsApi.

public void testJsApi() throws Exception {
    Context cx = Context.enter();
    cx.setOptimizationLevel(-1);
    Script script = cx.compileReader(new InputStreamReader(Bug482203.class.getResourceAsStream("conttest.js")), "", 1, null);
    Scriptable scope = cx.initStandardObjects();
    script.exec(cx, scope);
    for (; ; ) {
        Object cont = ScriptableObject.getProperty(scope, "c");
        if (cont == null) {
            break;
        }
        ((Callable) cont).call(cx, scope, scope, new Object[] { null });
    }
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) InputStreamReader(java.io.InputStreamReader) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable) Callable(org.mozilla.javascript.Callable)

Example 3 with Script

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

the class ContinuationsApiTest method testScriptWithMultipleContinuations.

public void testScriptWithMultipleContinuations() {
    Context cx = Context.enter();
    try {
        // must use interpreter mode
        cx.setOptimizationLevel(-1);
        Script script = cx.compileString("myObject.f(3) + myObject.g(3) + 2;", "test source", 1, null);
        cx.executeScriptWithContinuations(script, globalScope);
        fail("Should throw ContinuationPending");
    } catch (ContinuationPending pending) {
        try {
            Object applicationState = pending.getApplicationState();
            assertEquals(new Integer(3), applicationState);
            int saved = (Integer) applicationState;
            cx.resumeContinuation(pending.getContinuation(), globalScope, saved + 1);
            fail("Should throw another ContinuationPending");
        } catch (ContinuationPending pending2) {
            Object applicationState2 = pending2.getApplicationState();
            assertEquals(new Integer(6), applicationState2);
            int saved2 = (Integer) applicationState2;
            Object result2 = cx.resumeContinuation(pending2.getContinuation(), globalScope, saved2 + 1);
            assertEquals(13, ((Number) result2).intValue());
        }
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) ContinuationPending(org.mozilla.javascript.ContinuationPending)

Example 4 with Script

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

the class ContinuationsApiTest method testScriptWithContinuations.

public void testScriptWithContinuations() {
    Context cx = Context.enter();
    try {
        // must use interpreter mode
        cx.setOptimizationLevel(-1);
        Script script = cx.compileString("myObject.f(3) + 1;", "test source", 1, null);
        cx.executeScriptWithContinuations(script, globalScope);
        fail("Should throw ContinuationPending");
    } catch (ContinuationPending pending) {
        Object applicationState = pending.getApplicationState();
        assertEquals(new Integer(3), applicationState);
        int saved = (Integer) applicationState;
        Object result = cx.resumeContinuation(pending.getContinuation(), globalScope, saved + 1);
        assertEquals(5, ((Number) result).intValue());
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) ContinuationPending(org.mozilla.javascript.ContinuationPending)

Example 5 with Script

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

the class DecompileTest method newObject0Arg.

/**
	 * As of head of trunk on 30.09.09, decompile of "new Date()" returns "new Date" without parentheses.
	 * @see <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=519692">Bug 519692</a> 
	 */
@Test
public void newObject0Arg() {
    final String source = "var x = new Date().getTime();";
    final ContextAction action = new ContextAction() {

        public Object run(final Context cx) {
            final Script script = cx.compileString(source, "my script", 0, null);
            Assert.assertEquals(source, cx.decompileScript(script, 4).trim());
            return null;
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) ContextAction(org.mozilla.javascript.ContextAction) Test(org.junit.Test)

Aggregations

Script (org.mozilla.javascript.Script)17 Context (org.mozilla.javascript.Context)12 ScriptableObject (org.mozilla.javascript.ScriptableObject)5 IOException (java.io.IOException)3 InputStreamReader (java.io.InputStreamReader)3 ContinuationPending (org.mozilla.javascript.ContinuationPending)3 RhinoException (org.mozilla.javascript.RhinoException)3 Scriptable (org.mozilla.javascript.Scriptable)3 CompiledScript (javax.script.CompiledScript)2 ScriptContext (javax.script.ScriptContext)2 ScriptException (javax.script.ScriptException)2 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)2 ContextAction (org.mozilla.javascript.ContextAction)2 GeneratedClassLoader (org.mozilla.javascript.GeneratedClassLoader)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 BufferedReader (java.io.BufferedReader)1 PrintStream (java.io.PrintStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 MalformedURLException (java.net.MalformedURLException)1