use of org.mozilla.javascript.commonjs.module.Require in project hackpad by dropbox.
the class PipeThread method installRequire.
public Require installRequire(Context cx, List<String> modulePath, boolean sandboxed) {
RequireBuilder rb = new RequireBuilder();
rb.setSandboxed(sandboxed);
List<URI> uris = new ArrayList<URI>();
if (modulePath != null) {
for (String path : modulePath) {
try {
URI uri = new URI(path);
if (!uri.isAbsolute()) {
// call resolve("") to canonify the path
uri = new File(path).toURI().resolve("");
}
if (!uri.toString().endsWith("/")) {
// make sure URI always terminates with slash to
// avoid loading from unintended locations
uri = new URI(uri + "/");
}
uris.add(uri);
} catch (URISyntaxException usx) {
throw new RuntimeException(usx);
}
}
}
rb.setModuleScriptProvider(new SoftCachingModuleScriptProvider(new UrlModuleSourceProvider(uris, null)));
Require require = rb.createRequire(cx, this);
require.install(this);
return require;
}
use of org.mozilla.javascript.commonjs.module.Require 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.commonjs.module.Require 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.commonjs.module.Require in project hackpad by dropbox.
the class RequireTest method testRelativeId.
public void testRelativeId() throws Exception {
final Context cx = createContext();
final Scriptable scope = cx.initStandardObjects();
final Require require = getSandboxedRequire(cx, scope, false);
require.install(scope);
cx.evaluateReader(scope, getReader("testRelativeId.js"), "testRelativeId.js", 1, null);
}
use of org.mozilla.javascript.commonjs.module.Require in project hackpad by dropbox.
the class RequireTest method testSetMainForAlreadyLoadedModule.
public void testSetMainForAlreadyLoadedModule() throws Exception {
final Context cx = createContext();
final Scriptable scope = cx.initStandardObjects();
final Require require = getSandboxedRequire(cx, scope, false);
require.install(scope);
cx.evaluateReader(scope, getReader("testSetMainForAlreadyLoadedModule.js"), "testSetMainForAlreadyLoadedModule.js", 1, null);
try {
require.requireMain(cx, "assert");
fail();
} catch (IllegalStateException e) {
assertEquals(e.getMessage(), "Attempt to set main module after it was loaded");
}
}
Aggregations