JSR223 Javascript 中的回调,Oracle JRE 1.6 和 OpenJDK 1.6 之间的差异(安装在 Debian 上)

发布于 2024-11-05 17:29:43 字数 1605 浏览 0 评论 0原文

鉴于以下情况,使用 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 技术交流群。

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

发布评论

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

评论(1

陌生 2024-11-12 17:29:43

Java SE 6 规范 (JSR 270) 只是说:

不要求任何
特定的脚本语言是
平台支持;
实施者可以选择包括
对脚本语言的支持
他们认为合适的选择。

据我所知,目前还没有关于如何将 Java 类型集成到 JavaScript 中的正式规范。不幸的是,但没有理由期望跨实现 100% 兼容。

我相信 Oracle JRE 和 OpenJDK 都随 Rhino 一起提供,但无法保证版本级别、补丁等。

The Java SE 6 spec (JSR 270) merely says:

There will be no requirement that any
particular scripting language be
supported by the platform;
implementors may choose to include
support for the scripting language(s)
of their choice as they see fit.

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.

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