Search in sources :

Example 1 with Require

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;
}
Also used : RequireBuilder(org.mozilla.javascript.commonjs.module.RequireBuilder) Require(org.mozilla.javascript.commonjs.module.Require) ArrayList(java.util.ArrayList) UrlModuleSourceProvider(org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider) SoftCachingModuleScriptProvider(org.mozilla.javascript.commonjs.module.provider.SoftCachingModuleScriptProvider)

Example 2 with 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
    }
}
Also used : Context(org.mozilla.javascript.Context) Require(org.mozilla.javascript.commonjs.module.Require)

Example 3 with Require

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");
}
Also used : Context(org.mozilla.javascript.Context) Require(org.mozilla.javascript.commonjs.module.Require) Scriptable(org.mozilla.javascript.Scriptable)

Example 4 with Require

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);
}
Also used : Context(org.mozilla.javascript.Context) Require(org.mozilla.javascript.commonjs.module.Require) Scriptable(org.mozilla.javascript.Scriptable)

Example 5 with Require

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");
    }
}
Also used : Context(org.mozilla.javascript.Context) Require(org.mozilla.javascript.commonjs.module.Require) Scriptable(org.mozilla.javascript.Scriptable)

Aggregations

Require (org.mozilla.javascript.commonjs.module.Require)5 Context (org.mozilla.javascript.Context)4 Scriptable (org.mozilla.javascript.Scriptable)3 ArrayList (java.util.ArrayList)1 RequireBuilder (org.mozilla.javascript.commonjs.module.RequireBuilder)1 SoftCachingModuleScriptProvider (org.mozilla.javascript.commonjs.module.provider.SoftCachingModuleScriptProvider)1 UrlModuleSourceProvider (org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider)1