Example usage for jdk.nashorn.internal.objects Global addShellBuiltins

List of usage examples for jdk.nashorn.internal.objects Global addShellBuiltins

Introduction

In this page you can find the example usage for jdk.nashorn.internal.objects Global addShellBuiltins.

Prototype

public void addShellBuiltins() 

Source Link

Document

Adds jjs shell interactive mode builtin functions to global scope.

Usage

From source file:io.stallion.plugins.javascript.JavascriptShell.java

License:Open Source License

private static int readEvalPrint(InputStream input, OutputStream out, Context context, Global global)
        throws IOException, ScriptException {
    JsPluginSettings pluginSettings = new JsPluginSettings();

    //context.eval(global, "");

    //Global global = context.createGlobal();
    //ScriptEnvironment env = context.getEnv();

    String prompt = bundle.getString("shell.prompt");
    //BufferedReader in = new BufferedReader(new InputStreamReader(input));
    ConsoleReader in = new ConsoleReader();
    PrintWriter err = context.getErr();
    Global oldGlobal = Context.getGlobal();
    boolean globalChanged = oldGlobal != global;
    ScriptEnvironment env = context.getEnv();

    if (DB.available()) {
        global.put("DB", DB.instance(), false);
    }//from   ww w  .  j  av a2  s . com

    try {
        if (globalChanged) {
            Context.setGlobal(global);
        }

        global.addShellBuiltins();

        // Load builtins
        global.put("javaToJsHelpers", new JavaToJsHelpers(Sandbox.allPermissions()), true);
        SandboxedContext ctx = new SandboxedContext(Settings.instance().getTargetFolder() + "/js",
                Sandbox.allPermissions(), pluginSettings);
        global.put("myContext", ctx, true);

        String stallionSharedJs = IOUtils
                .toString(JavascriptShell.class.getResource("/jslib/stallion_shared.js"), UTF8);
        context.eval(global,
                "load(" + JSON.stringify(
                        map(val("script", stallionSharedJs), val("name", "stallion_shared.js"))) + ");",
                global, "<shellboot>");
        global.put("stallionClassLoader", new UnrestrictedJsClassLoader(), true);

        while (true) {
            String source;
            do {
                //err.print(prompt);
                //err.flush();
                source = "";

                try {
                    source = in.readLine(prompt);
                } catch (IOException var14) {
                    err.println(var14.toString());
                }

                if (source == null) {
                    return 0;
                }
            } while (source.isEmpty());

            try {
                Object e = context.eval(global, source, global, "<shell>");
                if (e != ScriptRuntime.UNDEFINED) {
                    err.println(JSType.toString(e));
                }
            } catch (Exception var15) {
                err.println(var15);
                if (env._dump_on_error) {
                    var15.printStackTrace(err);
                }
            }
        }
    } finally {
        if (globalChanged) {
            Context.setGlobal(oldGlobal);
        }

    }
}