JSR223 Javascript 中的回调,Oracle JRE 1.6 和 OpenJDK 1.6 之间的差异(安装在 Debian 上)
鉴于以下情况,使用 Oracle JRE 6 运行会给出输出 boo,但 OpenJDK 6 会给出异常
javax.script.ScriptException: sun.org.mozilla.javascript.EvaluatorException: The choice of Java
constructor replace matching JavaScript argument types (function,string) is ambiguous; candidate
constructors are:
class java.lang.String replace(char,char)
class java.lang.String replace(java.lang.CharSequence,java.lang.CharSequence) (<Unknown source>#1)
in <Unknown source> at line number 1
这可能是因为使用 OpenJDK(可能是随其提供的 rt.jar)该函数获取了 java.lang.String
,但是对于 Oracle 来说,它会获取一个 JavaScript 字符串(或者可以隐式强制转换为一个的字符串)。
那么哪个更正确呢? Javascript(在本例中)是 API,那么我们是否可以编写 Java,使得两种实现的 API 都相同? (如果 OpenJDK 实现“更正确”(因此很可能是将来每个人都会做的事情),那么我猜想更改 API(文档、示例、测试)并添加 new String(...)< /code> 适当的不是不可能的,但我宁愿不丑化 API,除非我更有信心。
import javax.script.*;
class st {
public static void main(String[] args) {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
Bindings bindings = jsEngine.getBindings(ScriptContext.ENGINE_SCOPE);
Foo foo = new Foo();
bindings.put("v", foo);
try {
jsEngine.eval("v.run(function(a) {println(a.replace(/f/,\"b\"));})");
} catch (ScriptException ex) {
ex.printStackTrace();
}
}
}
)
public class Foo {
public void run(FooCB cb) {
cb.run("foo");
}
public static interface FooCB {
public void run(Object val);
}
}
Given the following, running with Oracle JRE 6 gives the output boo, but OpenJDK 6 gives an exception
javax.script.ScriptException: sun.org.mozilla.javascript.EvaluatorException: The choice of Java
constructor replace matching JavaScript argument types (function,string) is ambiguous; candidate
constructors are:
class java.lang.String replace(char,char)
class java.lang.String replace(java.lang.CharSequence,java.lang.CharSequence) (<Unknown source>#1)
in <Unknown source> at line number 1
That's presumably because with OpenJDK (presumably the rt.jar supplied with it) the function's getting a java.lang.String
, but with Oracle's it's getting a JavaScript String (or something that can be implicitly coerced to one).
So which is more correct? The Javascript (in this case) is the API, so can we write the Java such that the API's the same for either implementation? (If the OpenJDK implementation is "more correct" (and so likely to be what everyone does in the future), then I guess changing the API (documentation, examples, tests) throwing in new String(...)
as appropriate wouldn't be impossible, but I'd rather not uglify the API unless I'm more confident.)
import javax.script.*;
class st {
public static void main(String[] args) {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
Bindings bindings = jsEngine.getBindings(ScriptContext.ENGINE_SCOPE);
Foo foo = new Foo();
bindings.put("v", foo);
try {
jsEngine.eval("v.run(function(a) {println(a.replace(/f/,\"b\"));})");
} catch (ScriptException ex) {
ex.printStackTrace();
}
}
}
and
public class Foo {
public void run(FooCB cb) {
cb.run("foo");
}
public static interface FooCB {
public void run(Object val);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Java SE 6 规范 (JSR 270) 只是说:
据我所知,目前还没有关于如何将 Java 类型集成到 JavaScript 中的正式规范。不幸的是,但没有理由期望跨实现 100% 兼容。
我相信 Oracle JRE 和 OpenJDK 都随 Rhino 一起提供,但无法保证版本级别、补丁等。
The Java SE 6 spec (JSR 270) merely says:
To the best of my knowledge, there is no formal spec for how to integrate Java types into JavaScript. It's unfortunate, but there's no reason to expect 100% compatibility across implementations.
I believe both the Oracle JRE and OpenJDK ship with Rhino, but there's no guarantee about version level, patches, etc.