Java Code Examples for jdk.nashorn.internal.runtime.Context#eval()

The following examples show how to use jdk.nashorn.internal.runtime.Context#eval() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NativeFunction.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.3.2.1 new Function (p1, p2, ... , pn, body)
 *
 * Constructor
 *
 * @param newObj is the new operator used for constructing this function
 * @param self   self reference
 * @param args   arguments
 * @return new NativeFunction
 */
@Constructor(arity = 1)
public static ScriptFunction function(final boolean newObj, final Object self, final Object... args) {
    final StringBuilder sb = new StringBuilder();

    sb.append("(function (");
    final String funcBody;
    if (args.length > 0) {
        final StringBuilder paramListBuf = new StringBuilder();
        for (int i = 0; i < args.length - 1; i++) {
            paramListBuf.append(JSType.toString(args[i]));
            if (i < args.length - 2) {
                paramListBuf.append(",");
            }
        }

        // now convert function body to a string
        funcBody = JSType.toString(args[args.length - 1]);

        final String paramList = paramListBuf.toString();
        if (!paramList.isEmpty()) {
            checkFunctionParameters(paramList);
            sb.append(paramList);
        }
    } else {
        funcBody = null;
    }

    sb.append(") {\n");
    if (args.length > 0) {
        checkFunctionBody(funcBody);
        sb.append(funcBody);
        sb.append('\n');
    }
    sb.append("})");

    final Global global = Global.instance();
    final Context context = global.getContext();
    return (ScriptFunction)context.eval(global, sb.toString(), global, "<function>");
}
 
Example 2
Source File: NativeFunction.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.3.2.1 new Function (p1, p2, ... , pn, body)
 *
 * Constructor
 *
 * @param newObj is the new operator used for constructing this function
 * @param self   self reference
 * @param args   arguments
 * @return new NativeFunction
 */
@Constructor(arity = 1)
public static ScriptFunction function(final boolean newObj, final Object self, final Object... args) {
    final StringBuilder sb = new StringBuilder();

    sb.append("(function (");
    final String funcBody;
    if (args.length > 0) {
        final StringBuilder paramListBuf = new StringBuilder();
        for (int i = 0; i < args.length - 1; i++) {
            paramListBuf.append(JSType.toString(args[i]));
            if (i < args.length - 2) {
                paramListBuf.append(",");
            }
        }

        // now convert function body to a string
        funcBody = JSType.toString(args[args.length - 1]);

        final String paramList = paramListBuf.toString();
        if (!paramList.isEmpty()) {
            checkFunctionParameters(paramList);
            sb.append(paramList);
        }
    } else {
        funcBody = null;
    }

    sb.append(") {\n");
    if (args.length > 0) {
        checkFunctionBody(funcBody);
        sb.append(funcBody);
        sb.append('\n');
    }
    sb.append("})");

    final Global global = Global.instance();
    final Context context = global.getContext();
    return (ScriptFunction)context.eval(global, sb.toString(), global, "<function>");
}
 
Example 3
Source File: NativeFunction.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.3.2.1 new Function (p1, p2, ... , pn, body)
 *
 * Constructor
 *
 * @param newObj is the new operator used for constructing this function
 * @param self   self reference
 * @param args   arguments
 * @return new NativeFunction
 */
@Constructor(arity = 1)
public static ScriptFunction function(final boolean newObj, final Object self, final Object... args) {
    final StringBuilder sb = new StringBuilder();

    sb.append("(function (");
    final String funcBody;
    if (args.length > 0) {
        final StringBuilder paramListBuf = new StringBuilder();
        for (int i = 0; i < args.length - 1; i++) {
            paramListBuf.append(JSType.toString(args[i]));
            if (i < args.length - 2) {
                paramListBuf.append(",");
            }
        }

        // now convert function body to a string
        funcBody = JSType.toString(args[args.length - 1]);

        final String paramList = paramListBuf.toString();
        if (!paramList.isEmpty()) {
            checkFunctionParameters(paramList);
            sb.append(paramList);
        }
    } else {
        funcBody = null;
    }

    sb.append(") {\n");
    if (args.length > 0) {
        checkFunctionBody(funcBody);
        sb.append(funcBody);
        sb.append('\n');
    }
    sb.append("})");

    final Global global = Global.instance();
    final Context context = global.getContext();
    return (ScriptFunction)context.eval(global, sb.toString(), global, "<function>");
}
 
Example 4
Source File: NativeFunction.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.3.2.1 new Function (p1, p2, ... , pn, body)
 *
 * Constructor
 *
 * @param newObj is the new operator used for constructing this function
 * @param self   self reference
 * @param args   arguments
 * @return new NativeFunction
 */
@Constructor(arity = 1)
public static ScriptFunction function(final boolean newObj, final Object self, final Object... args) {
    final StringBuilder sb = new StringBuilder();

    sb.append("(function (");
    final String funcBody;
    if (args.length > 0) {
        final StringBuilder paramListBuf = new StringBuilder();
        for (int i = 0; i < args.length - 1; i++) {
            paramListBuf.append(JSType.toString(args[i]));
            if (i < args.length - 2) {
                paramListBuf.append(",");
            }
        }

        // now convert function body to a string
        funcBody = JSType.toString(args[args.length - 1]);

        final String paramList = paramListBuf.toString();
        if (!paramList.isEmpty()) {
            checkFunctionParameters(paramList);
            sb.append(paramList);
        }
    } else {
        funcBody = null;
    }

    sb.append(") {\n");
    if (args.length > 0) {
        checkFunctionBody(funcBody);
        sb.append(funcBody);
        sb.append('\n');
    }
    sb.append("})");

    final Global global = Global.instance();
    final Context context = global.getContext();
    return (ScriptFunction)context.eval(global, sb.toString(), global, "<function>");
}
 
Example 5
Source File: NativeFunction.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.3.2.1 new Function (p1, p2, ... , pn, body)
 *
 * Constructor
 *
 * @param newObj is the new operator used for constructing this function
 * @param self   self reference
 * @param args   arguments
 * @return new NativeFunction
 */
@Constructor(arity = 1)
public static ScriptFunction function(final boolean newObj, final Object self, final Object... args) {
    final StringBuilder sb = new StringBuilder();

    sb.append("(function (");
    final String funcBody;
    if (args.length > 0) {
        final StringBuilder paramListBuf = new StringBuilder();
        for (int i = 0; i < args.length - 1; i++) {
            paramListBuf.append(JSType.toString(args[i]));
            if (i < args.length - 2) {
                paramListBuf.append(",");
            }
        }

        // now convert function body to a string
        funcBody = JSType.toString(args[args.length - 1]);

        final String paramList = paramListBuf.toString();
        if (!paramList.isEmpty()) {
            checkFunctionParameters(paramList);
            sb.append(paramList);
        }
    } else {
        funcBody = null;
    }

    sb.append(") {\n");
    if (args.length > 0) {
        checkFunctionBody(funcBody);
        sb.append(funcBody);
        sb.append('\n');
    }
    sb.append("})");

    final Global global = Global.instance();
    final Context context = global.getContext();
    return (ScriptFunction)context.eval(global, sb.toString(), global, "<function>");
}
 
Example 6
Source File: Main.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void evalImpl(final Context context, final Global global, final String source,
        final PrintWriter err, final boolean doe) {
    try {
        final Object res = context.eval(global, source, global, "<shell>");
        if (res != UNDEFINED) {
            err.println(toString(res, global));
        }
    } catch (final Exception e) {
        err.println(e);
        if (doe) {
            e.printStackTrace(err);
        }
    }
}
 
Example 7
Source File: NativeFunction.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.3.2.1 new Function (p1, p2, ... , pn, body)
 *
 * Constructor
 *
 * @param newObj is the new operator used for constructing this function
 * @param self   self reference
 * @param args   arguments
 * @return new NativeFunction
 */
@Constructor(arity = 1)
public static ScriptFunction function(final boolean newObj, final Object self, final Object... args) {
    final StringBuilder sb = new StringBuilder();

    sb.append("(function (");
    final String funcBody;
    if (args.length > 0) {
        final StringBuilder paramListBuf = new StringBuilder();
        for (int i = 0; i < args.length - 1; i++) {
            paramListBuf.append(JSType.toString(args[i]));
            if (i < args.length - 2) {
                paramListBuf.append(",");
            }
        }

        // now convert function body to a string
        funcBody = JSType.toString(args[args.length - 1]);

        final String paramList = paramListBuf.toString();
        if (!paramList.isEmpty()) {
            checkFunctionParameters(paramList);
            sb.append(paramList);
        }
    } else {
        funcBody = null;
    }

    sb.append(") {\n");
    if (args.length > 0) {
        checkFunctionBody(funcBody);
        sb.append(funcBody);
        sb.append('\n');
    }
    sb.append("})");

    final Global global = Global.instance();
    final Context context = global.getContext();
    return (ScriptFunction)context.eval(global, sb.toString(), global, "<function>");
}
 
Example 8
Source File: NativeFunction.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.3.2.1 new Function (p1, p2, ... , pn, body)
 *
 * Constructor
 *
 * @param newObj is the new operator used for constructing this function
 * @param self   self reference
 * @param args   arguments
 * @return new NativeFunction
 */
@Constructor(arity = 1)
public static ScriptFunction function(final boolean newObj, final Object self, final Object... args) {
    final StringBuilder sb = new StringBuilder();

    sb.append("(function (");
    final String funcBody;
    if (args.length > 0) {
        final StringBuilder paramListBuf = new StringBuilder();
        for (int i = 0; i < args.length - 1; i++) {
            paramListBuf.append(JSType.toString(args[i]));
            if (i < args.length - 2) {
                paramListBuf.append(",");
            }
        }

        // now convert function body to a string
        funcBody = JSType.toString(args[args.length - 1]);

        final String paramList = paramListBuf.toString();
        if (!paramList.isEmpty()) {
            checkFunctionParameters(paramList);
            sb.append(paramList);
        }
    } else {
        funcBody = null;
    }

    sb.append(") {\n");
    if (args.length > 0) {
        checkFunctionBody(funcBody);
        sb.append(funcBody);
        sb.append('\n');
    }
    sb.append("})");

    final Global global = Global.instance();
    final Context context = global.getContext();
    return (ScriptFunction)context.eval(global, sb.toString(), global, "<function>");
}
 
Example 9
Source File: Shell.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * read-eval-print loop for Nashorn shell.
 *
 * @param context the nashorn context
 * @param global  global scope object to use
 * @return return code
 */
private static int readEvalPrint(final Context context, final Global global) {
    final String prompt = bundle.getString("shell.prompt");
    final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    final PrintWriter err = context.getErr();
    final Global oldGlobal = Context.getGlobal();
    final boolean globalChanged = (oldGlobal != global);
    final ScriptEnvironment env = context.getEnv();

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

        global.addShellBuiltins();

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

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

    return SUCCESS;
}
 
Example 10
Source File: Shell.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * read-eval-print loop for Nashorn shell.
 *
 * @param context the nashorn context
 * @param global  global scope object to use
 * @return return code
 */
private static int readEvalPrint(final Context context, final Global global) {
    final String prompt = bundle.getString("shell.prompt");
    final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    final PrintWriter err = context.getErr();
    final Global oldGlobal = Context.getGlobal();
    final boolean globalChanged = (oldGlobal != global);
    final ScriptEnvironment env = context.getEnv();

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

        global.addShellBuiltins();

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

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

    return SUCCESS;
}
 
Example 11
Source File: Shell.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * read-eval-print loop for Nashorn shell.
 *
 * @param context the nashorn context
 * @param global  global scope object to use
 * @return return code
 */
private static int readEvalPrint(final Context context, final Global global) {
    final String prompt = bundle.getString("shell.prompt");
    final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    final PrintWriter err = context.getErr();
    final Global oldGlobal = Context.getGlobal();
    final boolean globalChanged = (oldGlobal != global);
    final ScriptEnvironment env = context.getEnv();

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

        global.addShellBuiltins();

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

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

    return SUCCESS;
}
 
Example 12
Source File: Shell.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * read-eval-print loop for Nashorn shell.
 *
 * @param context the nashorn context
 * @param global  global scope object to use
 * @return return code
 */
private static int readEvalPrint(final Context context, final Global global) {
    final String prompt = bundle.getString("shell.prompt");
    final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    final PrintWriter err = context.getErr();
    final Global oldGlobal = Context.getGlobal();
    final boolean globalChanged = (oldGlobal != global);
    final ScriptEnvironment env = context.getEnv();

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

        global.addShellBuiltins();

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

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

    return SUCCESS;
}
 
Example 13
Source File: Shell.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * read-eval-print loop for Nashorn shell.
 *
 * @param context the nashorn context
 * @param global  global scope object to use
 * @return return code
 */
protected int readEvalPrint(final Context context, final Global global) {
    final String prompt = bundle.getString("shell.prompt");
    final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    final PrintWriter err = context.getErr();
    final Global oldGlobal = Context.getGlobal();
    final boolean globalChanged = (oldGlobal != global);
    final ScriptEnvironment env = context.getEnv();

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

        global.addShellBuiltins();

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

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

    return SUCCESS;
}
 
Example 14
Source File: Shell.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * read-eval-print loop for Nashorn shell.
 *
 * @param context the nashorn context
 * @param global  global scope object to use
 * @return return code
 */
private static int readEvalPrint(final Context context, final Global global) {
    final String prompt = bundle.getString("shell.prompt");
    final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    final PrintWriter err = context.getErr();
    final Global oldGlobal = Context.getGlobal();
    final boolean globalChanged = (oldGlobal != global);
    final ScriptEnvironment env = context.getEnv();

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

        global.addShellBuiltins();

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

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

    return SUCCESS;
}
 
Example 15
Source File: Shell.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
/**
 * read-eval-print loop for Nashorn shell.
 *
 * @param context the nashorn context
 * @param global  global scope object to use
 * @return return code
 */
private static int readEvalPrint(final Context context, final Global global) {
    final String prompt = bundle.getString("shell.prompt");
    final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    final PrintWriter err = context.getErr();
    final Global oldGlobal = Context.getGlobal();
    final boolean globalChanged = (oldGlobal != global);
    final ScriptEnvironment env = context.getEnv();

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

        global.addShellBuiltins();

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

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

    return SUCCESS;
}