如何使用rhino和env.rhino.js执行js函数?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经解决了这个问题。原因是传递给被调用函数的参数错误。
I have solved this. The reason is that the params passed to the called function is wrong.