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