Example usage for jdk.nashorn.api.scripting NashornScriptEngine invokeFunction

List of usage examples for jdk.nashorn.api.scripting NashornScriptEngine invokeFunction

Introduction

In this page you can find the example usage for jdk.nashorn.api.scripting NashornScriptEngine invokeFunction.

Prototype

@Override
    public Object invokeFunction(final String name, final Object... args)
            throws ScriptException, NoSuchMethodException 

Source Link

Usage

From source file:com.mckoi.webplatform.nashorn.NashornInstanceGlobalFactory.java

License:Open Source License

/**
 * Initializes the NashornInternal object.
 * /*w ww .  j  a  v a  2s  . co m*/
 * @param cl the ClassLoader to use, or null for default.
 */
public void init(final ClassLoader cl) {

    // Incase we call 'init' more than once,
    if (PRIV_nashorn_ctx != null) {
        return;
    }

    // Hacky way to return properties from the Nashorn engine,
    final Object[] inner_data = new Object[5];

    // Run initialization in a privileged security context,
    AccessController.doPrivileged(new PrivilegedAction<Object>() {
        @Override
        public Object run() {

            // We make a VM static NashornScriptEngine that we use to compile
            // scripts and fork new JavaScript instances.

            NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
            NashornScriptEngine engine;
            if (cl == null) {
                engine = (NashornScriptEngine) factory.getScriptEngine(engine_args);
            } else {
                engine = (NashornScriptEngine) factory.getScriptEngine(engine_args, cl);
            }

            // We add some functions to the global object,
            ScriptObjectMirror global_object = (ScriptObjectMirror) engine
                    .getBindings(ScriptContext.ENGINE_SCOPE);

            // A function that performs startup. We execute this to obtain
            // information about the engine such as the Global and Context object.
            // It seems the only way to get at this information is from code
            // running within the engine.
            global_object.put("engineStartup", new EngineStartupFunction(inner_data));

            // Invoke the 'engineStartup' function to get at the priviledged
            // information.
            try {
                engine.invokeFunction("engineStartup", new Object[0]);
            } catch (ScriptException | NoSuchMethodException ex) {
                // Oops,
                throw new RuntimeException(ex);
            } finally {
                // Don't leave this function around, just incase,
                global_object.delete("engineStartup");
            }

            return null;
        }
    });

    PRIV_nashorn_ctx = (Context) inner_data[0];
    //    base_nashorn_global = (Global) inner_data[1];

}

From source file:IDE.SyntaxTree.JWebParser.java

public DefaultMutableTreeNode createObjectTree(String src) {
    try {/*from w ww  .ja  v  a2 s . c om*/
        ScriptEngineManager engineManager = new ScriptEngineManager();
        NashornScriptEngine engine = (NashornScriptEngine) engineManager.getEngineByName("nashorn");
        engine.eval("load(\"nashorn:parser.js\")");
        ScriptObjectMirror sss = (ScriptObjectMirror) engine.invokeFunction("parse", src);
        ScriptObjectMirror RootBody = (ScriptObjectMirror) ((ScriptObjectMirror) ((ScriptObjectMirror) sss)
                .get("body")).get("0");

        DefaultMutableTreeNode root = new DefaultMutableTreeNode("");
        getTree(RootBody, 0, root);

        return root;
    } catch (ScriptException | NoSuchMethodException ex) {
        Logger.getLogger(JWebParser.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:IDE.SyntaxTree.NewEmptyJUnitTest.java

@Test
public void hello() throws ScriptException {
    //jdk.nashorn.api.
    try {//ww  w.jav a2 s. c  o  m
        String input = readServerFile(new File("c:\\Projects\\JWeb\\www\\Controller\\indexController.jap"));
        ScriptEngineManager engineManager = new ScriptEngineManager();
        NashornScriptEngine engine = (NashornScriptEngine) engineManager.getEngineByName("nashorn");
        engine.eval("load(\"nashorn:parser.js\")");
        // engine.eval("var tree = Java.type(\"com.sun.source.tree.CompilationUnitTree\")");
        //  engine.eval("tree = parse(\"function Test(){return 1;}\")");

        ScriptObjectMirror sss = (ScriptObjectMirror) engine.invokeFunction("parse", input);
        ScriptObjectMirror RootBody = (ScriptObjectMirror) ((ScriptObjectMirror) sss.get("body")).get("0");

        Parse(RootBody, 0);

        engine.put("fff", sss);

        engine.eval("var empty = [];" + "for(var index in fff['body'][0]){    " + "  /*empty.push(index);*/ "
                + "for(var index2 in fff['body'][0][index]){empty.push(index2);}" + "}" + " ");
        String sss2 = (String) engine.eval("JSON.stringify( empty)");

        //  com.sun.source.tree.CompilationUnitTree
        // sss
        // engine.eval("var ast = parse(\"function Test(){ return 1;}\")");
        System.out.println(sss2);
    } catch (IOException | NoSuchMethodException ex) {
        Logger.getLogger(NewEmptyJUnitTest.class.getName()).log(Level.SEVERE, null, ex);
    }
}