alicorn-systems/v8-adapter

Unknown return type: class java.lang.Long when calling Date#getTime

Closed this issue · 6 comments

newk5 commented

After injecting the Date class and the Long class I get an Unknown return type: class java.lang.Long when calling Date#getTime

V8 v8 = V8.createV8Runtime();
V8JavaAdapter.injectClass(Long.class, v8);
V8JavaAdapter.injectClass(Date.class, v8);
v8.executeVoidScript("var d = new Date(); var time = d.time;");

I also tried to inject long.class instead of Long.class but the result was the same

caer commented

Hi @newk5 !

What happens if you use d.getTime() in your example instead of d.time? I have a suspicion there is a bug in the way we automatically convert Java getters and setters to JavaScript properties.

newk5 commented

The result is the same when using d.getTime() unfortunately

caer commented

😢 Ok! I will look into this some more today; thank you for submitting this issue!

newk5 commented

Thanks! Btw I went looking for the line that is throwing this error in J2V8 (https://github.com/eclipsesource/J2V8/blob/7d1944c76c7a58bc3117dfafa49bc2ec74d3c3fc/src/main/java/com/eclipsesource/v8/V8.java#L884) and I found it interesting that it's checking whether the value is an instance of Integer, Double, Boolean, Float and String, but not Long. Not sure what this means in relation to the V8Adapter but maybe It'll help detecting where the issue is 😛

caer commented

I noticed! A new build of the V8-adapter is now available in Maven Central as version 1.58 which fixes this issue.

J2V8 chooses to represent all large numbers as Doubles because JS internally uses 64-bit floating point numbers for everything...even integers! The latest build fixes this issue by upcasting Long and Float data to Double data. It is not perfect, and may produce some interesting numbers...but it does address the issue in the most JS-native way we have at the moment.

Let me know if you find any more issues!

newk5 commented

Oh I see. Thanks, all working now!