如何使用rhino和env.rhino.js执行js函数?

发布于 2024-11-18 23:11:10 字数 3571 浏览 2 评论 0原文

Scriptable envGlobals;

        InputStreamReader envReader = new InputStreamReader(getClass()
                .getResourceAsStream("env.rhino.js"));
//      InputStreamReader jqueryReader = new InputStreamReader(getClass()
//              .getResourceAsStream("jquery-1.6.2.js"));
        try {
            Context cx = ContextFactory.getGlobal().enterContext();
            try {
                Global global = new Global();
                global.init(cx);
                cx.setOptimizationLevel(-1);
                cx.setLanguageVersion(Context.VERSION_1_7);

                envGlobals = cx.initStandardObjects(global);
                try {
                    cx.evaluateReader(envGlobals, envReader,
                            "env.rhino.js", 1, null);
//                  cx.evaluateReader(envGlobals, jqueryReader,
//                          "jquery-1.6.2.js", 1, null);
                } catch (IOException e) {

                }
            } finally {
                Context.exit();
            }
        } finally {
            try {
                envReader.close();
            } catch (IOException e) {

            }
        }

        /**
         * the above code nicely evaluates env.rhino.js and provides a scope
         * object (envGlobals). Then for each script I want to evaluate
         * against env.rhino.js's global scope:
         */

        Context scriptContext = ContextFactory.getGlobal().enterContext();
        try {
            // Create a global scope for the dependency we're processing
            // and assign our prototype to the environment globals
            // (env.js defined globals, the console globals etc.). This
            // then allows us to (a) not have to re-establish commonly
            // used globals i.e. we can re-use them in our loop; and (b)
            // any global assignments are guaranteed to have come from
            // the dependency itself (which is what we're trying to
            // determine here).
            Scriptable globalScope = scriptContext.newObject(envGlobals);
            globalScope.setPrototype(envGlobals);
            globalScope.setParentScope(null);

            scriptContext.setOptimizationLevel(-1);
            scriptContext.setLanguageVersion(Context.VERSION_1_7);

            try {
                //scriptContext.evaluateString(globalScope, "window.location='http://www.amazon.com'", "location", 1, null);
                scriptContext.evaluateString(globalScope, tree.toSource(), "script document", 1, null);
                System.out.println(scriptContext.toString());

                // TODO: Do something useful with the globals.


            } finally {
                Context.exit();
            } 
....

 Function f = (Function)fObj;
 Object result = f.call(scriptContext, globalScope, globalScope, params);

throught this format,i always get the Exception info:
     Exception in thread "main" java.lang.NullPointerException
    at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:849)
    at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:164)
    at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:426)
    at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3178)
    at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:162)
    at org.sdc.food.parse.util.JavaScriptParser.getExecutableJS(JavaScriptParser.java:244)
    at org.sdc.food.parse.util.JavaScriptParser.main(JavaScriptParser.java:349)

请有人帮助我!

Scriptable envGlobals;

        InputStreamReader envReader = new InputStreamReader(getClass()
                .getResourceAsStream("env.rhino.js"));
//      InputStreamReader jqueryReader = new InputStreamReader(getClass()
//              .getResourceAsStream("jquery-1.6.2.js"));
        try {
            Context cx = ContextFactory.getGlobal().enterContext();
            try {
                Global global = new Global();
                global.init(cx);
                cx.setOptimizationLevel(-1);
                cx.setLanguageVersion(Context.VERSION_1_7);

                envGlobals = cx.initStandardObjects(global);
                try {
                    cx.evaluateReader(envGlobals, envReader,
                            "env.rhino.js", 1, null);
//                  cx.evaluateReader(envGlobals, jqueryReader,
//                          "jquery-1.6.2.js", 1, null);
                } catch (IOException e) {

                }
            } finally {
                Context.exit();
            }
        } finally {
            try {
                envReader.close();
            } catch (IOException e) {

            }
        }

        /**
         * the above code nicely evaluates env.rhino.js and provides a scope
         * object (envGlobals). Then for each script I want to evaluate
         * against env.rhino.js's global scope:
         */

        Context scriptContext = ContextFactory.getGlobal().enterContext();
        try {
            // Create a global scope for the dependency we're processing
            // and assign our prototype to the environment globals
            // (env.js defined globals, the console globals etc.). This
            // then allows us to (a) not have to re-establish commonly
            // used globals i.e. we can re-use them in our loop; and (b)
            // any global assignments are guaranteed to have come from
            // the dependency itself (which is what we're trying to
            // determine here).
            Scriptable globalScope = scriptContext.newObject(envGlobals);
            globalScope.setPrototype(envGlobals);
            globalScope.setParentScope(null);

            scriptContext.setOptimizationLevel(-1);
            scriptContext.setLanguageVersion(Context.VERSION_1_7);

            try {
                //scriptContext.evaluateString(globalScope, "window.location='http://www.amazon.com'", "location", 1, null);
                scriptContext.evaluateString(globalScope, tree.toSource(), "script document", 1, null);
                System.out.println(scriptContext.toString());

                // TODO: Do something useful with the globals.


            } finally {
                Context.exit();
            } 
....

 Function f = (Function)fObj;
 Object result = f.call(scriptContext, globalScope, globalScope, params);

throught this format,i always get the Exception info:
     Exception in thread "main" java.lang.NullPointerException
    at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:849)
    at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:164)
    at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:426)
    at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3178)
    at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:162)
    at org.sdc.food.parse.util.JavaScriptParser.getExecutableJS(JavaScriptParser.java:244)
    at org.sdc.food.parse.util.JavaScriptParser.main(JavaScriptParser.java:349)

pls,someone help me!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩套路吗 2024-11-25 23:11:10

我已经解决了这个问题。原因是传递给被调用函数的参数错误。

I have solved this. The reason is that the params passed to the called function is wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文