/** Create a new require function using the shared global. */ @SuppressWarnings("unused") private Require createRequire() { Require require = null; RequireBuilder builder = getRequireBuilder(); Context cx = CommonJSEngine.enterContext(); try { require = builder.createRequire(cx, global); } finally { Context.exit(); } return require; }
new SoftCachingModuleScriptProvider( new UrlModuleSourceProvider(uris, null))); Require require = rb.createRequire(cx, this); require.install(this); return require;
new SoftCachingModuleScriptProvider( new UrlModuleSourceProvider(uris, null))); Require require = rb.createRequire(cx, this); require.install(this); return require;
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); }
Scriptable getRuntimeScope(ScriptContext ctxt) { if (ctxt == null) { throw new NullPointerException("null script context"); } // we create a scope for the given ScriptContext Scriptable newScope = new ExternalScriptable(ctxt, indexedProps); // Set the prototype of newScope to be 'topLevel' so that // JavaScript standard objects are visible from the scope. newScope.setPrototype(topLevel); // define "context" variable in the new scope newScope.put("context", newScope, ctxt); // RRC - save some time and don't define print // LK - these functions are assumed by a lot of code so let's // make them available // define "print" function in the new scope Context cx = enterContext(); try { cx.evaluateString(newScope, printSource, "print", 1, null); requireBuilder.createRequire(cx, newScope).install(newScope); } finally { Context.exit(); } return newScope; }
public static void installNativeRequire(Context cx, Scriptable globalScope, Scriptable scope, final ModuleSourceProvider sourceProvider) { final ModuleScriptProvider scriptProvider = new StrongCachingModuleScriptProvider(sourceProvider); final Script preExec = null; final Script postExec = null; final boolean sandboxed = false; final Require require = new RequireBuilder() .setPreExec(preExec) .setPostExec(postExec) .setModuleScriptProvider(scriptProvider) .setSandboxed(sandboxed) .createRequire(cx, globalScope); require.install(scope); } /*
context.getWrapFactory().setJavaPrimitiveWrap(false); Scriptable topLevelScope = context.initStandardObjects(); Require require = builder.createRequire(context, topLevelScope);