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