final ContextFactory contextFactory,
final Map<String, String> env,
final Function callback,
final boolean debug) {
final NodeJsGlobal global = new NodeJsGlobal();
final Main main = debug ? doDebug(contextFactory, global) : null;
contextFactory.call(new ContextAction() {
@Override
public Object run(Context ctx) {
ctx.setOptimizationLevel(debug ? -1 : 9);
ctx.setLanguageVersion(Context.VERSION_1_8);
global.initStandardObjects(ctx, false);
if ( debug ) {
ScriptableObject.putProperty(global, "strikeThePose", new BaseFunction() {
@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
main.doBreak();
return Undefined.instance;
}
});
}
ExitCallbackExecutor exitCallbackExecutor = new ExitCallbackExecutor();
Scriptable envAsScriptable = mapToScriptable(ctx, global, env);
Require require = global.installNodeJsRequire(ctx, envAsScriptable, nodeModuleProvider,
new NodeRequireBuilder(asyncFunctionQueue, exitCallbackExecutor), false);
//TODO Get a real buffer here...
ScriptableObject.putProperty(global, "Buffer", Context.javaToJS(ctx.newObject(global), global));
Scriptable buffer = (Scriptable) require.call(ctx, global, global, new Object[]{Context.javaToJS("buffer", global)});