Example usage for jdk.nashorn.internal.runtime Context eval

List of usage examples for jdk.nashorn.internal.runtime Context eval

Introduction

In this page you can find the example usage for jdk.nashorn.internal.runtime Context eval.

Prototype

public Object eval(final ScriptObject initialScope, final String string, final Object callThis,
        final Object location) 

Source Link

Document

Entry point for eval

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 a  v a2s .c o m*/

    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);
        }

    }
}