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.
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 Object
s that actually can be Double
s, Boolean
s, Function
s (for JavaScript functions; also implements Scriptable
) or Scriptable
s (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.