[英]TypeError: Cannot find function blah in
I'm using the JavaScript support in javax.script to do some basic unit testing of JS scripts and I've run into a problem.我在 javax.script 中使用 JavaScript 支持对 JS 脚本进行一些基本的单元测试,但遇到了问题。
I have some code that eval(...)
s the pre-requisite lib files I have in an instance of ScriptEngine, including the one that has my function blah in it.我有一些代码
eval(...)
是我在 ScriptEngine 实例中拥有的先决条件库文件,包括其中包含我的 function blah 的那个。
I then eval a further file in the same instance of ScriptEngine, in which I have some functions that serve as tests, and then I invoke the tests.然后我在 ScriptEngine 的同一个实例中评估另一个文件,其中我有一些用作测试的函数,然后我调用测试。
However, despite all the evals working without issue, I get an error that to me suggests a problem loading the function.然而,尽管所有的评估工作没有问题,我收到一个错误,对我来说表明加载 function 时出现问题。
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot find function blah. (<Unknown source>#x) in <Unknown source> at line number x
Two separate eval
calls on the same ScriptEngine
works for me.同一个
ScriptEngine
上的两个单独的eval
调用对我有用。 Are you setting a new ScriptContext
or otherwise eliminating your definition of blah
?您是在设置新的
ScriptContext
还是以其他方式消除对blah
的定义?
public class TryScripting {
public static void main(String[] args) throws ScriptException {
ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
String makeFun = "function hello() {\n" +
"return \"hello world\";\n" +
"}\n" +
"{\n" +
"}";
engine.eval(makeFun);
engine.eval("myVar = hello()");
ret = engine.get("myVar");
System.out.println(ret);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.