JSR223 Javascript中的回调,Oracle JRE 1.6和OpenJDK 1.6之间的区别(例如安装在Debian上) [英] Callbacks in JSR223 Javascript, difference between Oracle JRE 1.6 and OpenJDK 1.6 (as installed on, say, Debian)

查看:109
本文介绍了JSR223 Javascript中的回调,Oracle JRE 1.6和OpenJDK 1.6之间的区别(例如安装在Debian上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于以下内容,与Oracle JRE 6一起运行会给出输出boo,但是OpenJDK 6会给出异常

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

大概是因为使用OpenJDK(可能附带了rt.jar),该函数获得了java.lang.String,而对于Oracle,则获得了JavaScript String(或可以隐式强制为一个).

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).

那么哪个更正确? Javascript(在这种情况下)是API,那么我们可以编写Java时使两种实现的API相同吗? (如果OpenJDK实现更正确"(并且将来很可能会成为每个人的工作),那么我想适当地更改抛出new String(...)的API(文档,示例,测试)并不是不可能的,但除非我更有信心,否则我不愿意丑化该API.)

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();
        }    
    }
}

public class Foo {
    public void run(FooCB cb) {
        cb.run("foo");
    }
    public static interface FooCB {
        public void run(Object val);
    }
}

推荐答案

Java SE 6规范(

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.

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

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.

我相信Rhino随附Oracle JRE和OpenJDK,但不能保证版本级别,补丁程序等.

I believe both the Oracle JRE and OpenJDK ship with Rhino, but there's no guarantee about version level, patches, etc.

这篇关于JSR223 Javascript中的回调,Oracle JRE 1.6和OpenJDK 1.6之间的区别(例如安装在Debian上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆