Bytecoder icon indicating copy to clipboard operation
Bytecoder copied to clipboard

`java.time.*` support ?

Open sureshg opened this issue 1 year ago • 2 comments

Wasm compilation fails when using LocalDateTime.now()

Env

Java: OpenJDK 20
bytecoder = "2023-04-05"

Error

Caused by: java.lang.IllegalStateException: No such method : Parsing method body of LMain;.main
 Parsing method body of Ljava/time/LocalDateTime;.now
  Parsing method body of Ljava/time/Clock;.systemDefaultZone
   Parsing method body of Ljava/time/ZoneId;.systemDefault
    Parsing method body of Ljava/util/TimeZone;.getDefault
     Parsing method body of Ljava/util/TimeZone;.getDefaultRef
      Parsing method body of Ljava/util/TimeZone;.setDefaultZone
       Parsing method body of Ljdk/internal/util/StaticProperty;.<clinit>
         Visiting #2 INVOKESTATIC Stack size is 0 Source line 63
         Visiting #3 ASTORE Stack size is 1 Source line 63
         Stack 0 of type Ljava/util/Properties;
        ....
         Local 0 of type Ljava/util/Properties;
         Stack 0 of type Ljava/lang/String;
         Stack 1 of type Ljava/nio/charset/Charset;
de.mirkosertic.bytecoder.core.ir.AnalysisException: java.lang.IllegalStateException: No such method : java/nio/charset/Charset.forName(Ljava/lang/String;Ljava/nio/charset/Charset;)Ljava/nio/charset/Charset;
        at de.mirkosertic.bytecoder.core.ir.ResolvedMethod.parseBody(ResolvedMethod.java:50)
        at de.mirkosertic.bytecoder.core.ir.ResolvedClass.resolveMethodInternal(ResolvedClass.java:133)
        at de.mirkosertic.bytecoder.core.ir.ResolvedClass.resolveMethod(ResolvedClass.java:109)
        at de.mirkosertic.bytecoder.core.ir.ResolvedClass.requestInitialization(ResolvedClass.java:97)
        at de.mirkosertic.bytecoder.core.parser.CompileUnit.resolveClass(CompileUnit.java:137)
        at de.mirkosertic.bytecoder.core.parser.GraphParser.parse_INVOKESTATIC(GraphParser.java:828)
        at de.mirkosertic.bytecoder.core.parser.GraphParser.parseMethodInsNode(GraphParser.java:860)
        at de.mirkosertic.bytecoder.core.parser.GraphParser.parse(GraphParser.java:2570)
        at de.mirkosertic.bytecoder.core.parser.GraphParser.parse(GraphParser.java:405)
        at de.mirkosertic.bytecoder.core.parser.GraphParser.<init>(GraphParser.java:150)
        ...
Caused by: java.lang.IllegalStateException: No such method : java/nio/charset/Charset.forName(Ljava/lang/String;Ljava/nio/charset/Charset;)Ljava/nio/charset/Charset;
        at de.mirkosertic.bytecoder.core.ir.ResolvedClass.resolveMethod(ResolvedClass.java:111)
        at de.mirkosertic.bytecoder.core.parser.GraphParser.parse_INVOKESTATIC(GraphParser.java:829)
        at de.mirkosertic.bytecoder.core.parser.GraphParser.parseMethodInsNode(GraphParser.java:860)
        at de.mirkosertic.bytecoder.core.parser.GraphParser.parse(GraphParser.java:2570)

sureshg avatar Apr 06 '23 22:04 sureshg

Regardless of the lack of Charset.forName implementation, I guess this will require platform (JavaScript now, potentially WASI later) integration right? I am wondering if we could take shortcut for such popular use case like directly leveraging Date.now() instead of the regular implementation that requires all the timezone management.

Same should be possible with WASI.

sdeleuze avatar Apr 07 '23 11:04 sdeleuze

Thank you for using Bytecoder and submitting a detailed problem description.

Indeed, current Bytecoder version does not support Class.forName() and other Java reflection APIs. They still need to be implemented to a certain point, but were not a top level priority during the latest major refactoring.

However, as @sdeleuze mentioned, Bytecoder does not include Charset or Timezone data. Access to this data must be provided as a JavaScript implementation for both JS and Wasm backends. My initial guess is that this implementation is straight forward, and maybe I'll find time in the next days to do so.,

mirkosertic avatar Apr 09 '23 18:04 mirkosertic