cincheo / jsweet

A Java to JavaScript transpiler.
http://www.jsweet.org
Other
1.45k stars 160 forks source link

Support for java.time API #592

Closed mueller-jens closed 4 years ago

mueller-jens commented 4 years ago

Are there any plans to support java.time API in the transpiler?

renaudpawlak commented 4 years ago

Not at this point as far as I know. I guess it could be fairly doable by wrapping moment.js. What would be missing so that one can contribute and get started on this project?

lgrignon commented 4 years ago

(Link to the original issue: https://github.com/j4ts/j4ts/issues/57)

mueller-jens commented 4 years ago

Thanks for the answer @renaudpawlak . For starting contribute I miss some basics on how this library works, where the right point to add it (jsweet, jtds, a candy). Also I am unsure if wrapping moment is the right way or using the js-joda library is a better way.

renaudpawlak commented 4 years ago

You are right. I was discussing with @lgrignon about the fact that a quickstart/howto to create a new J4TS lib would be extremely useful. I don't have any opportunity to find the time right now but I will discuss it with Louis. In the meantime, I have pushed my last (unfinished) work as is in the j4ts-time repo. It is 6 years old, so I am not sure it is a significant start (it does not compile) but I guess it is better than nothing.

In order to wrap a JS library in a J4TS project, just do it the very same way as in a regular JSweet project (i.e. add a reference to the candy, or if the candy does not exist, just create a def package and place the JS lib API definitions there).

I have hastily created an empty j4ts-quickstart project where we can write a first example and some documentation to get started. https://github.com/j4ts/j4ts-quickstart

Hope this will help. I should be able to guide, but difficult for me to actively participate for now on.

mueller-jens commented 4 years ago

@renaudpawlak Thanks for the information. I will try to start with it and it is hopefully ok if i come back to you if i have any question.

mueller-jens commented 4 years ago

Sorry but I can't get it work. If i use your example replace the Calendar classes by java.time.LocalDateTime and try to transpile it with jdk 1.8 i get the error message:

org.jsweet:jsweet-maven-plugin:1.2.0-SNAPSHOT:jsweet failed: A required class was missing while executing org.jsweet:jsweet-maven-plugin:1.2.0-SNAPSHOT:jsweet: com/sun/tools/javac/util/Context

if i try to use the actual versions of jsweet and jtds I get a lot of errors starting with

package exists in another module: java.base at D:\tools\git\j4ts-time\src\main\java\java\time\LocalDateTime.java(62)

And i do not know how i came over it.

lgrignon commented 4 years ago

Could you please try with 2.3.7?

mueller-jens commented 4 years ago

@lgrignon Thanks for the suggestion. I tried it and saw that i use a java8 jre not a jdk. Thahs why i got the missing com/sun/tools/javac/util/Context

lgrignon commented 4 years ago

Is tools.jar included in pom.xml?

mueller-jens commented 4 years ago

I switched to a jdk now the bild works and i try to understand what i have to change in the class to map LocalDateTime and the other classes to th js-jods library. But it is very hard for me.

lgrignon commented 4 years ago

Well done @mueller-jens ! What's your newest problem then? Let's try to fix them one by one :)

Did you take a look at other similar projects, such as: https://github.com/j4ts/j4ts-awtgeom

mueller-jens commented 4 years ago

I try to find out how to add js-joda as dependency and how to map the java API to the js API.

lgrignon commented 4 years ago

I may be wrong but you do not need to add js-joda (JS version) as a dependency. You can add it in your webapp as a runtime dependency. However, you need the "type declarations" for JSweet. Simplest way to achieve it is to write only what you need in a def.xxx package. For instance, define in your project a def.jsjoda package, and then declare the classes / methods / functions you need from js-joda. (More info here) Example:

@Interface
public abstract class JsJodaClass {
  public native void jsJodaMethod();
}
mueller-jens commented 4 years ago

Ok Thanks @lgrignon . I will try it. Can you give me an example for a test class for this?

mueller-jens commented 4 years ago

@lgrignon I have pushed my first try to my repository: https://github.com/mueller-jens/j4ts-time But i get some compile errors. I am not do familar with typescript so I can not find out what is wrong. Can you assist me? I think there is something worng with inheritence and the mapping of static functions. Thanks and Best regards Jens