private File getBasePathForModule(ModuleScope thisObj) { ModuleScope moduleScope = thisObj; if (moduleScope == null) { return null; } URI base = moduleScope.getUri(); File cwdFile = new File(base.getPath()); if ( !cwdFile.isDirectory() ) { cwdFile = cwdFile.getParentFile(); } return cwdFile; } }
public ModuleScope(Scriptable prototype, URI uri, URI base) { this.uri = uri; this.base = base; setPrototype(prototype); cacheBuiltins(); }
public Object callSuperWrapped(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { try { ScriptableObject.putProperty(thisObj, "_compile", compile); return super.call(cx, scope, thisObj, args); } catch (JavaScriptException e) { if ( thisObj instanceof ModuleScope) { String id = (String)Context.jsToJava(args[0], String.class); ModuleScope moduleScope = (ModuleScope) thisObj; URI base = moduleScope.getBase(); URI current = moduleScope.getUri(); URI uri = current.resolve(id + ".js/"); if (!id.startsWith("./") && !id.startsWith("../") && base != null && new File(uri.getPath()).exists()) { // try to convert to a relative URI rooted on base return super.call(cx, scope, thisObj, new Object[]{uri.getPath()}); } } throw e; } }
static Scriptable getScope(String path) { if (useRequire) { // If CommonJS modules are enabled use a module scope that resolves // relative ids relative to the current URL, file or working directory. URI uri; if (path == null) { // use current directory for shell and -e switch uri = new File(System.getProperty("user.dir")).toURI(); } else { // find out whether this is a file path or a URL if (SourceReader.toUrl(path) != null) { try { uri = new URI(path); } catch (URISyntaxException x) { // fall back to file uri uri = new File(path).toURI(); } } else { uri = new File(path).toURI(); } } return new ModuleScope(global, uri, null); } else { return global; } }
static Scriptable getScope(String path) { if (useRequire) { // If CommonJS modules are enabled use a module scope that resolves // relative ids relative to the current URL, file or working directory. URI uri; if (path == null) { // use current directory for shell and -e switch uri = new File(System.getProperty("user.dir")).toURI(); } else { // find out whether this is a file path or a URL if (SourceReader.toUrl(path) != null) { try { uri = new URI(path); } catch (URISyntaxException x) { // fall back to file uri uri = new File(path).toURI(); } } else { uri = new File(path).toURI(); } } return new ModuleScope(global, uri, null); } else { return global; } }
@Override public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { final ScriptableObject moduleObject = (ScriptableObject)cx.newObject( thisObj); defineReadOnlyProperty(moduleObject, "id", args[0]); String path = Context.toString(args[1]); File file = new File(path); URI uri = file.toURI(); URI base; if (file.exists() && file.isFile()) { base = file.getParentFile().toURI(); } else { base = file.toURI(); } final Scriptable executionScope = new ModuleScope(thisObj, uri, base); defineReadOnlyProperty(moduleObject, "uri", uri.toString()); Scriptable exports = cx.newObject(scope); executionScope.put("exports", executionScope, exports); executionScope.put("module", executionScope, moduleObject); moduleObject.put("exports", moduleObject, exports); install(executionScope); executeOptionalScript(preExec, cx, executionScope); cx.compileString(Context.toString(args[0]), path, 0, null).exec(cx, executionScope); executeOptionalScript(postExec, cx, executionScope); return ScriptRuntime.toObject(scope, ScriptableObject.getProperty(moduleObject, "exports")); } };
public ModuleScope(Scriptable prototype, URI uri, URI base) { this.uri = uri; this.base = base; setPrototype(prototype); cacheBuiltins(); }
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"); }
public ModuleScope(Scriptable prototype, URI uri, URI base) { this.uri = uri; this.base = base; setPrototype(prototype); cacheBuiltins(); }
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")); }
public ModuleScope(Scriptable prototype, URI uri, URI base) { this.uri = uri; this.base = base; setPrototype(prototype); cacheBuiltins(); }
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")); }
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")); }
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")); }