Java Code Examples for org.mozilla.javascript.commonjs.module.Require#install()
The following examples show how to use
org.mozilla.javascript.commonjs.module.Require#install() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequireJarTest.java From rhino-android with Apache License 2.0 | 6 votes |
@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"); } }
Example 2
Source File: Global.java From JsDroidCmd with Mozilla Public License 2.0 | 5 votes |
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; }
Example 3
Source File: RequireTest.java From rhino-android with Apache License 2.0 | 5 votes |
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); }
Example 4
Source File: RequireTest.java From rhino-android with Apache License 2.0 | 5 votes |
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"); } }
Example 5
Source File: RequireJarTest.java From rhino-android with Apache License 2.0 | 5 votes |
@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); }
Example 6
Source File: Global.java From astor with GNU General Public License v2.0 | 5 votes |
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; }
Example 7
Source File: RequireTest.java From astor with GNU General Public License v2.0 | 5 votes |
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); }
Example 8
Source File: RequireTest.java From astor with GNU General Public License v2.0 | 5 votes |
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"); } }
Example 9
Source File: RequireJarTest.java From astor with GNU General Public License v2.0 | 5 votes |
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); }
Example 10
Source File: RequireJarTest.java From astor with GNU General Public License v2.0 | 5 votes |
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"); } }