简体   繁体   English

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)

Given the following, running with Oracle JRE 6 gives the output boo, but OpenJDK 6 gives an exception 鉴于以下情况,使用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

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). 这可能是因为OpenJDK(可能是rt.jar提供它)函数获取了一个java.lang.String ,但是使用Oracle它会得到一个JavaScript字符串(或者可以隐式强制转换为一个)。

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? Javascript(在这种情况下)是API,因此我们可以编写Java,使得API对于任一实现都是相同的吗? (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.) (如果OpenJDK实现“更正确”(并且可能是将来每个人都做的那样),那么我想改变API(文档,示例,测试)投入new String(...) ,如果适当的话这是不可能的,但除非我更自信,否则我宁愿不对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();
        }    
    }
}

and

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

The Java SE 6 spec ( JSR 270 ) merely says: Java SE 6规范( JSR 270 )仅说:

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. 据我所知,没有关于如何将Java类型集成到JavaScript中的正式规范。 It's unfortunate, but there's no reason to expect 100% compatibility across implementations. 这很不幸,但是没有理由期望跨实现的100%兼容性。

I believe both the Oracle JRE and OpenJDK ship with Rhino, but there's no guarantee about version level, patches, etc. 我相信Oracle JRE和OpenJDK都附带Rhino,但不保证版本级别,补丁等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM