Passing common types between Java and (Rhino) Javascript

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.


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