在 Java 和 (Rhino) Javascript 之间传递通用类型

发布于 2024-11-25 04:45:42 字数 887 浏览 1 评论 0原文

我不清楚使用 (Mozilla) Rhino 时如何在 Javascript 和 Java 之间转换类型的规则。

文档中有一些关于 String 的细节:

请务必记住 Java 字符串和 JavaScript 字符串不相同 [...]Rhino 提供了一些帮助来减少 两种类型之间的差异。首先,你可以传递一个JavaScript 字符串到需要 Java 字符串的 Java 方法,Rhino 将 执行转换。我们实际上看到了这个功能在 调用前面示例中的 java.lang.String 构造函数。 Rhino 还使 JavaScript 方法可用于 Java 字符串,如果 java.lang.String 类尚未定义它们

,但是其他类呢?如果我将 javascript Number 传递给需要 intdouble (或 IntegerDouble)的 Java 方法,它会转变? long/Long 怎么样? (它不适合 Double ,因此也不适合 JS 数字?

返回这些值的 Java 方法怎么样?

然后是 Boolean/boolean JS 常量 truefalse 是否与适当的 Java 值相互转换

java.lang.Boolean.TRUE.booleanValue()

? 认为不是。

我 Mozilla Rhino 文档,但请指出我是否遗漏了一些明显的内容。

I'm unclear on the rules for how types are converted between Javascript and Java when using (Mozilla) Rhino.

There's some specifics in the documentation about String:

It's important to keep in mind that Java strings and JavaScript
strings are not the same […]Rhino provides some help in reducing the
differences between the two types. First, you can pass a JavaScript
string to a Java method that requires a Java string and Rhino will
perform the conversion. We actually saw this feature in action on the
call to the java.lang.String constructor in the preceding example.
Rhino also makes the JavaScript methods available to Java strings if
the java.lang.String class doesn't already define them

But what about others? If I pass a javascript Number to a Java method expecting int, double (or Integer or Double) will it get converted? What about long/Long? (which won't fit in a Double and so won't fit in a JS number?

What about Java methods returning these values?

Then there's Boolean/boolean. Are the JS constants true and false converted to and from the appropriate Java value? I've seen code like

java.lang.Boolean.TRUE.booleanValue()

used from JS, so at least some people think it isn't.

I have looked at the Mozilla Rhino documentation but do point out if I've missed something obvious.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

晨与橙与城 2024-12-02 04:45:42

以下是它将 JavaScript 类型转换为 Java 类型的方法: http://www-archive .mozilla.org/js/liveconnect/lc3_method_overloading.html#InitationConversion

尝试一下:

$ java -cp js.jar org.mozilla.javascript.tools.shell.Main

js> new java.lang.Integer(12345)
12345
js> new java.lang.Integer(12345) == 12345
true

js> new java.lang.Double(12345.12345)
12345.12345

js> new java.lang.Long(9223372036854775807)                 
js: Cannot convert 9223372036854776000 to java.lang.Long
js> 9223372036854775807
9223372036854776000
js> new java.lang.Long("9223372036854775807")
9223372036854775807
js> new java.lang.Long("-9223372036854775808")
-9223372036854775808

js> new java.lang.Boolean(true)
true
js> new java.lang.Boolean(true) == true
true
js> new java.lang.Boolean(true) == false
false
js> java.lang.Boolean.TRUE.booleanValue() == true
true
js> java.lang.Boolean.FALSE.booleanValue() == false
true

UPD

不幸的是,我也找不到任何有关 JavaScript-from-Java 类型映射的文档。但是 教程 显示 JavaScript 对象作为 Java < 插入到上下文中并从上下文中检索code>Object 实际上可以是 DoubleBooleanFunction(对于 JavaScript 函数;还实现Scriptable)或Scriptable(对于对象)。

使用此代码片段可以获得JavaScript-Java类型映射参考:

https://gist.github.com/1089320#file_java_script_java_type_mapping.textile

至于 LiveConnect 兼容性。如果您指的是此脚注:

从 JavaScript 调用 Java 的能力最初实现为
称为 LiveConnect 的 Netscape 浏览器技术的一部分。然而,
因为该技术还包含与浏览器的通信
插件,并且由于在 Rhino 中从 Java 调用 JavaScript 的方式是
完全不同,本文中不会使用该术语。

我认为这是关于使用 Java 中的 JavaScript 与 LiveConnect 规范的不同。从 JavaScript 使用 Java 应该是一样的。

Here's how it converts JavaScript types to Java types: http://www-archive.mozilla.org/js/liveconnect/lc3_method_overloading.html#InvocationConversion.

Try it:

$ java -cp js.jar org.mozilla.javascript.tools.shell.Main

js> new java.lang.Integer(12345)
12345
js> new java.lang.Integer(12345) == 12345
true

js> new java.lang.Double(12345.12345)
12345.12345

js> new java.lang.Long(9223372036854775807)                 
js: Cannot convert 9223372036854776000 to java.lang.Long
js> 9223372036854775807
9223372036854776000
js> new java.lang.Long("9223372036854775807")
9223372036854775807
js> new java.lang.Long("-9223372036854775808")
-9223372036854775808

js> new java.lang.Boolean(true)
true
js> new java.lang.Boolean(true) == true
true
js> new java.lang.Boolean(true) == false
false
js> java.lang.Boolean.TRUE.booleanValue() == true
true
js> java.lang.Boolean.FALSE.booleanValue() == false
true

UPD

Unfortunately I can't find any docs about JavaScript-from-Java type mapping either. But the tutorial shows that JavaScript objects are inserted into and retrieved from context as Java Objects that actually can be Doubles, Booleans, Functions (for JavaScript functions; also implements Scriptable) or Scriptables (for objects).

Using this code snippet it's possibly to get JavaScript-Java type mapping reference:

https://gist.github.com/1089320#file_java_script_java_type_mapping.textile

As for LiveConnect compatibility. If you are referring to this footnote:

The ability to call Java from JavaScript was first implemented as
part of a Netscape browser technology called LiveConnect. However,
since that technology also encompassed communication with browser
plugins, and since the way of calling JavaScript from Java in Rhino is
entirely different, that term won't be used in this paper.

I think it's about using JavaScript from Java is different from LiveConnect specification. Using Java from JavaScript should be the same.

你是暖光i 2024-12-02 04:45:42

实际上,即使是“自动”转换,我也遇到了问题,最终我自己进行了转换:

function javaToJavaScript(str)
{
    len = str.length();
    tmp = "";
    for (var i=0; i<len; i++)
        tmp += String.fromCharCode(str.charAt(i));
    return tmp;
}

Actually I had a problem even with the "automatic" conversion, ending up converting myself:

function javaToJavaScript(str)
{
    len = str.length();
    tmp = "";
    for (var i=0; i<len; i++)
        tmp += String.fromCharCode(str.charAt(i));
    return tmp;
}
娇柔作态 2024-12-02 04:45:42

这是通过让包装工厂在 string 没有实现 js string 具有的所有插件的情况下为我完成工作来解决的(例如 string.length !== string.length() )

这可以设置为顶层:

context = Context.enter();

//This tells Rhino to convert String, Number, Boolean and Character into their JavaScript equivalents when returning from a Java function
context.getWrapFactory().setJavaPrimitiveWrap(false);

context.initStandardObjects();

This was solved by having the wrapper factory do the work for me in the situations where string doesn't implement all of the addons that js string has (like how string.length !== string.length() )

This can be set at the top level with:

context = Context.enter();

//This tells Rhino to convert String, Number, Boolean and Character into their JavaScript equivalents when returning from a Java function
context.getWrapFactory().setJavaPrimitiveWrap(false);

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