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实例中的先决条件lib文件,包括我的函数等等。
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.
然而,尽管所有的evals工作没有问题,我得到一个错误,对我来说建议加载函数的问题。
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot find function blah. (<Unknown source>#x) in <Unknown source> at line number x
javax.script.ScriptException:sun.org.mozilla.javascript.internal.EcmaError:TypeError:找不到函数blah。 ( <未知来源> #x)在行号x的 <未知来源> 中
1 个解决方案
#1
0
Two separate eval
calls on the same ScriptEngine
works for me. Are you setting a new ScriptContext
or otherwise eliminating your definition of blah
?
同一个ScriptEngine上的两个单独的eval调用对我有用。您是在设置新的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);
}
}
#1
0
Two separate eval
calls on the same ScriptEngine
works for me. Are you setting a new ScriptContext
or otherwise eliminating your definition of blah
?
同一个ScriptEngine上的两个单独的eval调用对我有用。您是在设置新的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);
}
}