mirkosertic / Bytecoder

Framework to interpret and transpile JVM bytecode to JavaScript, OpenCL or WebAssembly.
https://www.mirkosertic.de/blog/2017/06/compiling-bytecode-to-javascript/
Apache License 2.0
891 stars 59 forks source link

How to convert int,double,String and boolean to OpaqueReferenceType [Question] #452

Closed bertung closed 2 years ago

bertung commented 4 years ago

How to convert int,double,String and boolean to OpaqueReferenceType?

OpaqueReferenceArray array=OpaqueArrays.createObjectArray(); //how to use array.push(5); array.push("hey"); array.push(true); array.push(5.6);

mirkosertic commented 4 years ago

This question is related to #433.

The JVM does not allow creating arrays with mixted types. So it is not possible to create an array containing booleans and ints. However, it is possible to create an array of type Object[] and store an integer primitive in it. The compiler will apply autoboxing, and convert the int primitive to a java.lang.Integer instance, which can be safely stored in an Object[] arrays.

The OpaqueReferenceType and the OpaqueReferenceArray support a different use case. OpaqueReferenceType is the base type for everything not implemented but used by JVM classes. This might be something like the browser console, a promise or even a HTML DOM element. So all if this is some kind of reference used by JVM code.

The OpaqueReferenceArray is not designed to store primitive datatypes. Can you please elaborate your usecase?

bertung commented 4 years ago

Please see org.teavm.jso.core.JSArray.java for a possible use case to convert and pass a Java object array to Javascript.

mirkosertic commented 4 years ago

You can always pass a Object[] array to an impored JavaScript function, but you will get no auto-unboxing etc. All type conversions have to be done on JS side.

So can you please elaborate your usecase why to want to use an OpaqueReferenceType? Please help me so I can help you!

bertung commented 4 years ago

I need to pass a large raw mixed Javascript array from Bytecoder instead of passing a large JSON string for performance. Mixed array would only include primitive types, strings or other mixed arrays (they may include nested arrays as well) in its structure. Although I mentioned it in this question, I am not particularly after OpaqueReferenceType to use. Any other feature in the API that may help me accomplish this will do.

mirkosertic commented 2 years ago

Closed due to no further interaction.