Java 类org.mozilla.javascript.commonjs.module.Require 实例源码

项目:whackpad    文件:RequireTest.java   
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
    }
}
项目:rhino-android    文件:RequireJarTest.java   
@Override
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
    }
}
项目:rhino-android    文件:RequireJarTest.java   
@Override
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");
    }
}
项目:rec-core    文件:Scripting.java   
public static void runfile(File file,
                           String filename,
                           boolean enableRetry,
                           String retryFile) throws Exception {
    Context ctx = Context.enter();
    ctx.putThreadLocal("SCRIPT_PATH", file.getAbsoluteFile().getParent());

    ScriptableObject scope = ctx.initStandardObjects();

    NativeExecutionContext executionContext = initializeNativeExecutionContext(
            file,
            enableRetry,
            retryFile,
            ctx);

    scope.putConst("context", scope, javaToJS(executionContext, scope));

    ctx.setLanguageVersion(VERSION_1_8);
    Require require = new RequireBuilder()
            .setModuleScriptProvider(new SoftCachingModuleScriptProvider(new NativeModuleSourceProvider()))
            .createRequire(ctx, scope);
    require.install(scope);
    require.requireMain(ctx, filename);
}
项目:JsSandbox    文件:JsSandboxEvaluators.java   
private static void loadModulesFromIncludeDirs(Context cx, Scriptable scope, List<File> requireDirList)
{
    List<URI> uris = new ArrayList<URI>();
    for (File dir : requireDirList) {
        URI uri = dir.toURI();
        uris.add(uri);
        continue;
    }
    if (uris.isEmpty()) {
        return;
    }
    RequireBuilder rb = new RequireBuilder();
    rb.setModuleScriptProvider(new SoftCachingModuleScriptProvider(
            new UrlModuleSourceProvider(uris, null)));
    Require require = rb.createRequire(cx, scope);
    require.install(scope);
    return;
}
项目:code404    文件:RequireTest.java   
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
    }
}
项目:code404    文件:RequireJarTest.java   
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
    }
}
项目:rhino-jscover    文件:RequireTest.java   
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
    }
}
项目:rhino-jscover    文件:RequireJarTest.java   
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
    }
}
项目:astor    文件:RequireTest.java   
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
    }
}
项目:astor    文件:RequireJarTest.java   
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
    }
}
项目:Rhino-Prov-Mod    文件:RequireTest.java   
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
    }
}
项目:Rhino-Prov-Mod    文件:RequireJarTest.java   
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
    }
}
项目:Ignite    文件:JSXTransformer.java   
public void init() throws URISyntaxException {
    ctx = Context.enter();
    try {
        RequireBuilder builder = new RequireBuilder();
        builder.setModuleScriptProvider(new SoftCachingModuleScriptProvider(
                new UrlModuleSourceProvider(buildModulePaths(), null)));

        topLevelScope = ctx.initStandardObjects();
        builder.setSandboxed(false); // allows this to load scripts
        Require require = builder.createRequire(ctx, topLevelScope);

        exports = require.requireMain(ctx, jsxTransformerJS);
        transform = (Function) exports.get("transform", topLevelScope);
    } finally {
        Context.exit();
    }
}
项目:whackpad    文件:Global.java   
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;
}
项目:whackpad    文件:RequireTest.java   
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");
}
项目:whackpad    文件:RequireTest.java   
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);
}
项目:whackpad    文件:RequireTest.java   
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");
    }
}
项目:whackpad    文件:RequireTest.java   
private Require getSandboxedRequire(Context cx, Scriptable scope, boolean sandboxed)
        throws URISyntaxException
{
    return new Require(cx, cx.initStandardObjects(), 
            new StrongCachingModuleScriptProvider(
                    new UrlModuleSourceProvider(Collections.singleton(
                            getDirectory()), null)), null, null, true);
}
项目:whackpad    文件:ComplianceTest.java   
private static Require createRequire(File dir, Context cx, Scriptable scope) 
throws URISyntaxException 
{
    return new Require(cx, scope, new StrongCachingModuleScriptProvider(
            new UrlModuleSourceProvider(Collections.singleton(new URI(
                    "file:" + dir.getAbsolutePath().replace(File.separatorChar,'/') + "/")), 
                    Collections.singleton(new URI(ComplianceTest.class.getResource(".").toExternalForm() + "/")))),
                    null, null, false);
}
项目:rhino-android    文件:RequireTest.java   
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
    }
}
项目:rhino-android    文件:RequireTest.java   
public void testNonSandboxed() throws Exception {
    final Context cx = createContext();
    final Scriptable scope = cx.initStandardObjects();
    final Require require = getSandboxedRequire(cx, scope, false);
    final String jsFile = TestUtils.readAsset(dir + File.separator + "testNonSandboxed.js");
    ScriptableObject.putProperty(scope, "moduleUri", jsFile);
    require.requireMain(cx, "testNonSandboxed");
}
项目:rhino-android    文件:RequireTest.java   
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.evaluateString(scope, TestUtils.readAsset(dir + File.separator + "testRelativeId.js"),
            "testRelativeId.js", 1, null);
}
项目:rhino-android    文件:RequireTest.java   
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.evaluateString(scope, TestUtils.readAsset(dir + File.separator + "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");
    }
}
项目:rhino-android    文件:RequireJarTest.java   
@Override
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");
}
项目:rhino-android    文件:RequireJarTest.java   
@Override
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);
}
项目:rhino-android    文件:RequireJarTest.java   
private Require getSandboxedRequire(Context cx, Scriptable scope, boolean sandboxed)
        throws URISyntaxException
{
    return new Require(cx, cx.initStandardObjects(), 
            new StrongCachingModuleScriptProvider(
                    new UrlModuleSourceProvider(Collections.singleton(
                            getDirectory()), null)), null, null, true);
}
项目:code404    文件:Global.java   
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;
}
项目:code404    文件:RequireTest.java   
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");
}
项目:code404    文件:RequireTest.java   
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);
}
项目:code404    文件:RequireTest.java   
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");
    }
}
项目:code404    文件:RequireTest.java   
private Require getSandboxedRequire(Context cx, Scriptable scope, boolean sandboxed)
        throws URISyntaxException
{
    return new Require(cx, cx.initStandardObjects(),
            new StrongCachingModuleScriptProvider(
                    new UrlModuleSourceProvider(Collections.singleton(
                            getDirectory()), null)), null, null, true);
}
项目:code404    文件:RequireJarTest.java   
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");
}
项目:code404    文件:RequireJarTest.java   
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);
}
项目:code404    文件:RequireJarTest.java   
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");
    }
}
项目:code404    文件:RequireJarTest.java   
private Require getSandboxedRequire(Context cx, Scriptable scope, boolean sandboxed)
        throws URISyntaxException
{
    return new Require(cx, cx.initStandardObjects(), 
            new StrongCachingModuleScriptProvider(
                    new UrlModuleSourceProvider(Collections.singleton(
                            getDirectory()), null)), null, null, true);
}
项目:code404    文件:ComplianceTest.java   
private static Require createRequire(File dir, Context cx, Scriptable scope)
throws URISyntaxException
{
    return new Require(cx, scope, new StrongCachingModuleScriptProvider(
            new UrlModuleSourceProvider(
                    Collections.singleton(dir.getAbsoluteFile().toURI()),
                    Collections.singleton(new URI(ComplianceTest.class.getResource(".").toExternalForm() + "/")))),
                    null, null, false);
}
项目:S3F    文件:Global.java   
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;
}
项目:rhino-jscover    文件:Global.java   
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;
}
项目:rhino-jscover    文件:RequireTest.java   
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");
}