Example usage for jdk.nashorn.internal.runtime ScriptRuntime UNDEFINED

List of usage examples for jdk.nashorn.internal.runtime ScriptRuntime UNDEFINED

Introduction

In this page you can find the example usage for jdk.nashorn.internal.runtime ScriptRuntime UNDEFINED.

Prototype

Undefined UNDEFINED

To view the source code for jdk.nashorn.internal.runtime ScriptRuntime UNDEFINED.

Click Source Link

Document

Unique instance of undefined.

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  w  ww .j  a va 2  s  .  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);
        }

    }
}

From source file:org.kihara.util.JavascriptEngine.java

License:Open Source License

/**
 * read-eval-print loop for Nashorn shell.
 *
 * @param context the nashorn context/*from   ww  w  . j a v a  2s. c  om*/
 * @param global  global scope object to use
 * @return return code
 */
@SuppressWarnings("resource")
private static int readEvalPrint(final Context context, final ScriptObject global) {
    final String prompt = "> ";//bundle.getString("shell.prompt");
    final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    final PrintWriter err = context.getErr();
    final ScriptObject oldGlobal = Context.getGlobal();
    final boolean globalChanged = (oldGlobal != global);
    final ScriptEnvironment env = context.getEnv();

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

        // initialize with "shell.js" script
        try {
            final Source source = Source.sourceFor("<shell.js>", JavascriptEngine.SHELL_INIT_JS);
            context.eval(global, source.getString(), global, "<shell.js>", false, false);

            // custom scripts
            context.eval(global, script.get(), global, "<shell.js>", false, false);
        } catch (final Exception e) {
            err.println(e);
            if (env._dump_on_error) {
                e.printStackTrace(err);
            }

            return INTERNAL_ERROR;
        }

        while (true) {
            err.print(prompt);
            err.flush();

            String source = "";
            try {
                source = in.readLine();
            } catch (final IOException ioe) {
                err.println(ioe.toString());
            }

            if (source == null) {
                break;
            }

            if (source.isEmpty()) {
                continue;
            }

            Object res;
            try {
                res = context.eval(global, source, global, "<shell>", env._strict, false);
            } catch (final Exception e) {
                err.println(e);
                if (env._dump_on_error) {
                    e.printStackTrace(err);
                }
                continue;
            }

            if (res != ScriptRuntime.UNDEFINED) {
                err.println(JSType.toString(res));
            }
        }
    } finally {
        if (globalChanged) {
            Context.setGlobal(global);
        }
    }

    return SUCCESS;
}

From source file:z.zee.Z.java

License:Open Source License

/**
 * read-eval-print loop for Nashorn shell.
 *
 * @param context the nashorn context/*  w ww.j  av  a2 s .c om*/
 * @param global  global scope object to use
 * @return return code
 */
@SuppressWarnings("resource")
private static int readEvalPrint(final Context context, final ScriptObject global) {
    final String prompt = bundle.getString("shell.prompt");
    final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    final PrintWriter err = context.getErr();
    final ScriptObject oldGlobal = Context.getGlobal();
    final boolean globalChanged = (oldGlobal != global);
    final ScriptEnvironment env = context.getEnv();

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

        // initialize with "z.js" script
        try {
            final Source source = new Source("<z.js>", Z.class.getResource("resources/z.js"));
            context.eval(global, source.getString(), global, "<z.js>", false);
        } catch (final Exception e) {
            err.println(e);
            if (env._dump_on_error) {
                e.printStackTrace(err);
            }

            return INTERNAL_ERROR;
        }

        while (true) {
            err.print(prompt);
            err.flush();

            String source = "";
            try {
                source = in.readLine();
            } catch (final IOException ioe) {
                err.println(ioe.toString());
            }

            if (source == null) {
                break;
            }

            Object res;
            try {
                res = context.eval(global, source, global, "<shell>", env._strict);
            } catch (final Exception e) {
                err.println(e);
                if (env._dump_on_error) {
                    e.printStackTrace(err);
                }
                continue;
            }

            if (res != null && res != ScriptRuntime.UNDEFINED) {
                err.println(ScriptRuntime.safeToString(res));
            }
        }
    } finally {
        if (globalChanged) {
            Context.setGlobal(global);
        }
    }

    return SUCCESS;
}