/** * Creates a new require() function. You are still responsible for invoking * either {@link Require#install(Scriptable)} or * {@link Require#requireMain(Context, String)} to effectively make it * available to its JavaScript program. * @param cx the current context * @param globalScope the global scope containing the JS standard natives. * @return a new Require instance. */ public Require createRequire(Context cx, Scriptable globalScope) { return new Require(cx, globalScope, moduleScriptProvider, preExec, postExec, sandboxed); } }
this.preExec = preExec; this.postExec = postExec; setPrototype(ScriptableObject.getFunctionPrototype(nativeScope)); if(!sandboxed) { paths = cx.newArray(nativeScope, 0); defineReadOnlyProperty(this, "paths", paths);
private Scriptable executeModuleScript(Context cx, String id, Scriptable exports, ModuleScript moduleScript, boolean isMain) { final ScriptableObject moduleObject = (ScriptableObject)cx.newObject( nativeScope); URI uri = moduleScript.getUri(); URI base = moduleScript.getBase(); defineReadOnlyProperty(moduleObject, "id", id); if(!sandboxed) { defineReadOnlyProperty(moduleObject, "uri", uri.toString()); } final Scriptable executionScope = new ModuleScope(nativeScope, uri, base); // Set this so it can access the global JS environment objects. // This means we're currently using the "MGN" approach (ModuleScript // with Global Natives) as specified here: // <http://wiki.commonjs.org/wiki/Modules/ProposalForNativeExtension> executionScope.put("exports", executionScope, exports); executionScope.put("module", executionScope, moduleObject); moduleObject.put("exports", moduleObject, exports); install(executionScope); if(isMain) { defineReadOnlyProperty(this, "main", moduleObject); } executeOptionalScript(preExec, cx, executionScope); moduleScript.getScript().exec(cx, executionScope); executeOptionalScript(postExec, cx, executionScope); return ScriptRuntime.toObject(nativeScope, ScriptableObject.getProperty(moduleObject, "exports")); }
new UrlModuleSourceProvider(uris, null))); Require require = rb.createRequire(cx, this); require.install(this); return require;
private void init(final Class<?> clazz) { final Context context = Context.enter(); context.setOptimizationLevel(this.optimizationLevel); context.setLanguageVersion(Context.VERSION_1_7); final ScriptableObject scope = context.initStandardObjects(); final Require require = new Require(Context.getCurrentContext(), scope, getModuleScriptProvider(clazz), null, null, false); require.install(scope); try { this.moduleScope = new ModuleScope(scope, new URI("./" + this.name), null); } catch (final URISyntaxException e) { throw new SmallerException("Failed to create moduleScope", e); } addGlobalFunction("print", LOGGER, "info"); }
final ModuleScript moduleScript = getModule(cx, id, uri, base); if (sandboxed && !moduleScript.isSandboxed()) { throw ScriptRuntime.throwError(cx, nativeScope, "Module \"" Scriptable newExports = executeModuleScript(cx, id, exports, moduleScript, isMain); if (exports != newExports) {
mainExports = getExportedModuleInterface(cx, mainModuleId, null, null, true); } else if (!sandboxed) { mainExports = getExportedModuleInterface(cx, mainUri.toString(), mainUri, null, true);
protected Scriptable setupModule(Context context, ScriptableObject scope, final Resource moduleResource, String moduleId) { RequireBuilder requireBuilder = new RequireBuilder(); requireBuilder.setSandboxed(false); requireBuilder.setModuleScriptProvider(new ModuleScriptProvider() { @Override public ModuleScript getModuleScript(Context cx, String moduleId, URI moduleUri, URI baseUri, Scriptable paths) throws Exception { Script script = cx.compileString(moduleResource.getOutput().getContent(), moduleResource.getName(), 1, null); return new ModuleScript(script, URI.create(moduleResource.getName()), URI.create(moduleResource.getName())); } }); Require require = requireBuilder.createRequire(context, scope); return require.requireMain(context, moduleId); }
private Scriptable executeModuleScript(Context cx, String id, Scriptable exports, ModuleScript moduleScript, boolean isMain) { final ScriptableObject moduleObject = (ScriptableObject)cx.newObject( nativeScope); URI uri = moduleScript.getUri(); URI base = moduleScript.getBase(); defineReadOnlyProperty(moduleObject, "id", id); if(!sandboxed) { defineReadOnlyProperty(moduleObject, "uri", uri.toString()); } final Scriptable executionScope = new ModuleScope(nativeScope, uri, base); // Set this so it can access the global JS environment objects. // This means we're currently using the "MGN" approach (ModuleScript // with Global Natives) as specified here: // <http://wiki.commonjs.org/wiki/Modules/ProposalForNativeExtension> executionScope.put("exports", executionScope, exports); executionScope.put("module", executionScope, moduleObject); moduleObject.put("exports", moduleObject, exports); install(executionScope); if(isMain) { defineReadOnlyProperty(this, "main", moduleObject); } executeOptionalScript(preExec, cx, executionScope); moduleScript.getScript().exec(cx, executionScope); executeOptionalScript(postExec, cx, executionScope); return ScriptRuntime.toObject(cx, nativeScope, ScriptableObject.getProperty(moduleObject, "exports")); }
new UrlModuleSourceProvider(uris, null))); Require require = rb.createRequire(cx, this); require.install(this); return require;
final ModuleScript moduleScript = getModule(cx, id, uri, base); if (sandboxed && !moduleScript.isSandboxed()) { throw ScriptRuntime.throwError(cx, nativeScope, "Module \"" Scriptable newExports = executeModuleScript(cx, id, exports, moduleScript, isMain); if (exports != newExports) {
mainExports = getExportedModuleInterface(cx, mainModuleId, null, null, true); } else if (!sandboxed) { mainExports = getExportedModuleInterface(cx, mainUri.toString(), mainUri, null, true);
public CoffeeScriptCompiler(String version, boolean bare) { this.bare = bare; this.version = version; try { Context context = createContext(); globalScope = context.initStandardObjects(); final Require require = getSandboxedRequire(context, globalScope, true); coffeeScript = require.requireMain(context, "coffee-script"); } catch (Exception e1) { throw new CoffeeScriptException("Unable to load the coffeeScript compiler into Rhino", e1); } finally { Context.exit(); } }
private Scriptable executeModuleScript(Context cx, String id, Scriptable exports, ModuleScript moduleScript, boolean isMain) { final ScriptableObject moduleObject = (ScriptableObject)cx.newObject( nativeScope); URI uri = moduleScript.getUri(); URI base = moduleScript.getBase(); defineReadOnlyProperty(moduleObject, "id", id); if(!sandboxed) { defineReadOnlyProperty(moduleObject, "uri", uri.toString()); } final Scriptable executionScope = new ModuleScope(nativeScope, uri, base); // Set this so it can access the global JS environment objects. // This means we're currently using the "MGN" approach (ModuleScript // with Global Natives) as specified here: // <http://wiki.commonjs.org/wiki/Modules/ProposalForNativeExtension> executionScope.put("exports", executionScope, exports); executionScope.put("module", executionScope, moduleObject); moduleObject.put("exports", moduleObject, exports); install(executionScope); if(isMain) { defineReadOnlyProperty(this, "main", moduleObject); } executeOptionalScript(preExec, cx, executionScope); moduleScript.getScript().exec(cx, executionScope); executeOptionalScript(postExec, cx, executionScope); return ScriptRuntime.toObject(nativeScope, ScriptableObject.getProperty(moduleObject, "exports")); }
require.install(this); return require;
this.preExec = preExec; this.postExec = postExec; setPrototype(ScriptableObject.getFunctionPrototype(nativeScope)); if(!sandboxed) { paths = cx.newArray(nativeScope, 0); defineReadOnlyProperty(this, "paths", paths);
final ModuleScript moduleScript = getModule(cx, id, uri, base); if (sandboxed && !moduleScript.isSandboxed()) { throw ScriptRuntime.throwError(cx, nativeScope, "Module \"" Scriptable newExports = executeModuleScript(cx, id, exports, moduleScript, isMain); if (exports != newExports) {
/** * Creates a new require() function. You are still responsible for invoking * either {@link Require#install(Scriptable)} or * {@link Require#requireMain(Context, String)} to effectively make it * available to its JavaScript program. * @param cx the current context * @param globalScope the global scope containing the JS standard natives. * @return a new Require instance. */ public Require createRequire(Context cx, Scriptable globalScope) { return new Require(cx, globalScope, moduleScriptProvider, preExec, postExec, sandboxed); } }
mainExports = getExportedModuleInterface(cx, mainModuleId, null, null, true); } else if (!sandboxed) { mainExports = getExportedModuleInterface(cx, mainUri.toString(), mainUri, null, true);
console.flush(); } else if (useRequire && filename.equals(mainModule)) { require.requireMain(cx, filename); } else { processFile(cx, getScope(filename), filename);