konsoletyper / teavm

Compiles Java bytecode to JavaScript, WebAssembly and C
https://teavm.org
Apache License 2.0
2.54k stars 260 forks source link

Support for java.time #451

Closed hohwille closed 3 years ago

hohwille commented 4 years ago

TeaVM does not seem to support java.time (JSR310). I get such errors:

[ERROR] Class java.time.Instant was not found
[ERROR] Class java.time.LocalDate was not found
[ERROR] Class java.time.LocalDateTime was not found
[ERROR] Class java.time.LocalTime was not found
[ERROR] Class java.time.OffsetDateTime was not found
[ERROR] Class java.time.OffsetTime was not found
[ERROR] Class java.time.ZonedDateTime was not found
[ERROR] Class java.time.LocalDateTime was not found
[ERROR] Class java.time.OffsetDateTime was not found
[ERROR] Class java.time.LocalDate was not found
[ERROR] Class java.time.OffsetTime was not found
[ERROR] Class java.time.LocalTime was not found
[ERROR] Class java.time.Instant was not found
[ERROR] Class java.time.ZonedDateTime was not found
hohwille commented 4 years ago

That would also be some area where I might assist. Looking at TDate this does not look like an easy task and java.time is huge and complex.

hohwille commented 4 years ago

I started working on this. Here are some general thoughts:

hohwille commented 4 years ago

When I write T* types for classlib I can use the regular java type as well as the T* type. From what I have seen in classlib I assume that using the T* types wherever possible is preferred. In cases like toString() that can not return TString the plain Java type is used. I hope my assumption is correct and I am going into the right direction. I am sometimes unsure whether I should use CharSequence or TCharSequence. Also I have issues with TList as I can not use it in for-each loops since it does not implement Iterable. Can I also just use List in T* code of classlib?

konsoletyper commented 4 years ago

ZoneId should be supported to some degree. At least UTC has to work so it makes any sense. However, also ZoneOffset would be nice to have. But region IDs would most probably be overkill again. They could be supported as containing any kind of string but not allowing to calculate with it as the offsets for all regions require a larger map of data.

Timezones are already supported in java.util.* package, with all larger map of data, so you can just use it.

Code from java.time is using java.lang.Math with methods addExact, multiplyExact, floorMod, and floorDiv that is not (yet) supported by TMath

So you are porting some code to TeaVM? Where did you get it?

Do not yet even know how TeaVM emulates long as JS Number has less precision.

TeaVM emulates long with object that contains two 32-bit integer fields

From what I have seen in classlib I assume that using the T* types wherever possible is preferred.

No, it was very old approach when I only had few emulated class. This approach let me ensure that all newly implemented classes have all the dependencies. I think that times I didn't even have support for tests, so it was the only option. Now you only need to name your classes with T prefix, but use other classes from java.* package directly.