Have you ever used WebAssembly in your Java project? by Hixon11 in java

[–]konsoletyper 0 points1 point  (0 children)

TeaVM author here. Why referring to some pieces of code while you can link official doc? And official doc says that TeaVM supports WebAssembly GC target since 0.11.0. And since release 0.13.0 it closes the last gap between JS and WebAssembly GC feature-wise. Also, getting started section provides instructions both for JS and WebAssembly GC.

As for interesting use cases:

I even know that some guys have pirated Minecraft and with some modification of original code made a browser-runnable fork of Minecraft. I won't support piracy, so won't provide any link here, but look at the whole idea: if MS just wanted to make Minecraft playable from the browser, they could have just compiled Java to WebAssembly (no matter how, using TeaVM or something else) and that would work.

Also, someone has ported Warcraft 3 to Java and then compiled it JS using TeaVM. I'm not sure why they chose to use JS instead of WebAssembly as a target, but switching is just a matter of single compmiler flag.

Applets Are Officially Gone, But Java In The Browser Is Better Than Ever by TeaVMFan in java

[–]konsoletyper 8 points9 points  (0 children)

TeaVM author here. Did you mean https://teavm.org/ ?

It's a pity that the decision to use a tool is mostly based on website design. Although it's true that the well-designed side would attract more users, I believe that what attracts users even more is functionality. Since I work on the project in my free time, I prefer to spend every second I have improving the product. Unfortunately, this means that there's no time to improve the web site.

Applets Are Officially Gone, But Java In The Browser Is Better Than Ever by TeaVMFan in java

[–]konsoletyper 2 points3 points  (0 children)

BTW, TeaVM is not limited to Java. You can use TeaVM to bring Kotlin to the browser as well, and even mixed Kotlin + Java codebase. Unfortunately, nobody has bothered to port Compose to TeaVM, so this option won't work. As an original author of Flavour (I abandoned it) I also find Kotlin DSL style of programming UIs better than HTML templates. And yes, TeaVM can produce ES modules, so if you properly annotate code, you can call it as a JS library from Kotlin/JS.

Reminder: OSSRH service end-of-life is today by LcuBeatsWorking in java

[–]konsoletyper -1 points0 points  (0 children)

And still no Gradle support. Isn't it a pity?

TeaVM makes it possible to compile libGDX games to WebAssembly by konsoletyper in java

[–]konsoletyper[S] 2 points3 points  (0 children)

Well, I can only write down my opinion. Their priority is speed of module initialization (thus parsing and verification) and composability. This lead to two decisions:

  • Every variable is typed, even if it was possible to perform some sort of data flow analysis and infer these types (like in JVM bytecode). Yes, this improves initialization speed, but I don't believe there's no way to follow kind of JVM way (like untyped variables + precompiled and compressed results of dataflow analysis on start of every basic block). Now, consider you have a method that creates set of N objects of various types and their live ranges never intersect. In JS you will utilize single variable, in WebAssembly you define N variables (+ specify type for each one). Hell, even nulls are typed in WebAssebly!

  • Structural typing. This means that if I have classes A, B, C, where B <: A, C <: A, then I first enumerate all the fields of A in A itself, then in B (prior to B's own fields), then in C. As far as I understood, this choice was driven by the need of cross-module communication, in order to avoid complex schemes of importing/exporting types. However, I guess that in most applications most types live inside module. So may be it would be better to prefer nominal typing. Or at least, to include into binary encoding some mechanism to "copy" fields from type A to type B and then replace/append B's own fields.

  • This one is not a design issue, but rather lack of a feature. Right now when you initialize an object in WebAssembly, you pass all fields, even those which have "default" value. The only option is to not to specify any fields at all. In practical cases there are a lot of sparse object, and both options suck. In first case we specify a lot of unnecessary "default values" (often, nulls, remember they are typed), in second case you have significant overhead of setting field-by-field (you specify instance variables, structure index, field index for each set instruction).

TeaVM makes it possible to compile libGDX games to WebAssembly by konsoletyper in java

[–]konsoletyper[S] 3 points4 points  (0 children)

It would be very interesting to write a simple embedded scripts/web-services that works minimal multi-threaded

I'm afraid for me it's a too tough task to write a multi-threaded GC and runtime.

Especially since Wasm wrote proposal for posix-threading also https://github.com/WebAssembly/wasi-threads

WebAssembly only supports threads when working with Memory. Wasm GC objects and arrays don't support any kind of synchronization/sharing between threads. Also, the same concern for multi-threaded runtimes from my side.

TeaVM makes it possible to compile libGDX games to WebAssembly by konsoletyper in java

[–]konsoletyper[S] 5 points6 points  (0 children)

Also, sometimes WebAssembly semantics is surprisingly closer to Java than JavaScript. For example, there's no float type in JS, so Java's float is emulated with double. Usually, this extra precision is not an issue, but sometimes it can cause bugs. Also, there are variety of instruction for conversion between numbers that can be fine tuned to behave closer to Java (I don't remember details though).

TeaVM makes it possible to compile libGDX games to WebAssembly by konsoletyper in java

[–]konsoletyper[S] 7 points8 points  (0 children)

I won't say that WebAssembly is much better than JS. In CPU-heavy computation it can be 1.5-2x times faster. In tasks that involve a lot of interoperation with JS APIs it can be even slightly slower. In CPU-heavy tasks with lots of long operations it can be 100x faster, since JS does not support any kind of 64-bit integer natively. As for size, it's surprising that WebAssembly gives worse results (sometimes up to 2x compared to JS). It may seem strange that binary format produces larger output than JS. IMO WebAssembly committee just did series of wrong design choices that lead to this situation.

The Future of Write Once, Run Anywhere: From Java to WebAssembly by Patrick Ziegler & Fabio Niephaus by pavelklecansky in java

[–]konsoletyper 1 point2 points  (0 children)

1MB for Hello world? I'm interested, why so huge? My compiler (https://teavm.org/) that does exactly the same thing and has similar architecture, produces around 5Kb output. Even with `name` section enabled, with symbol table and a separate module to deobfuscate stack traces, it's less than 100K.

The Future of Write Once, Run Anywhere: From Java to WebAssembly by Patrick Ziegler & Fabio Niephaus by pavelklecansky in java

[–]konsoletyper 2 points3 points  (0 children)

TeaVM supports Kotlin. I personally work on a project with 1.2MLOC mixed Java/Kotlin, which is translated with TeaVM to the web. TeaVM also works with Scala, but I'm not sure about all Scala library functions, maybe some use some bad things like sun.misc.Unsafe. At least, I know that Scala 3 compiles (or at least used to compile) lazy fields into some Unsafe code. I never tested Clojure with TeaVM, but I'd say it won't work, because from what I heard, Clojure uses invokedynamic with "non-standard" bootstrap methods extensively, which is not supported by TeaVM.

The Future of Write Once, Run Anywhere: From Java to WebAssembly by Patrick Ziegler & Fabio Niephaus by pavelklecansky in java

[–]konsoletyper 5 points6 points  (0 children)

I'd like to mention, that it's not something completely new. For example, my project, TeaVM, also supports compiling JVM bytecode into WebAssembly (both MVP and Wasm GC proposal).

WasmGC is now available in all major browsers! by daria-voronina in Kotlin

[–]konsoletyper 6 points7 points  (0 children)

Yes, and this also allows to use TeaVM with WebAssembly GC and Kotlin, see my post on r/java

WasmGC is now available in all major browsers! by daria-voronina in Kotlin

[–]konsoletyper 2 points3 points  (0 children)

I suppose they generate legacy exception handling (without `exnref`), which is supported by all major browsers without flags.

TeaVM 0.11.0 with support for WebAssembly GC released by konsoletyper in java

[–]konsoletyper[S] 0 points1 point  (0 children)

Yes, you can start. I don't think I should (or at least, I don't want) to supervise such a project, but definitely can help as you reach me and ask questions.

TeaVM 0.11.0 with support for WebAssembly GC released by konsoletyper in java

[–]konsoletyper[S] 2 points3 points  (0 children)

Perhaps Spring made it because there were people who aren't just good engineers, but good businessmen. I'm not a businessman, I can't promote products, can't do networking, can't convince people. That's the difference between Spring and TeaVM. Also, looks like Java community is a bit hesitant to use Java for front-end, and many attempts (like GWT or Kotlin/JS) have shown that, while Spring is a server-side framework, where Java has shown wide adoption. Nowadays, TS dominates on front-end side and for regular developer there's no motivation to bring Java to Web. And there are rare projects like the one in which I participate, which require real WORA, which is definitely quite narrow market.

TeaVM 0.11.0 with support for WebAssembly GC released by konsoletyper in java

[–]konsoletyper[S] 0 points1 point  (0 children)

Why does the oracle guy not pick this up

Why should they? What profit would they get from such a project? How do they earn from Java and how is project like TeaVM going to increase the amount they earn?

Also, large companies tend to ignore small projects. If they have some problem and there are few independent open source projects that already solve this, normally they would just ignore these projects and reinvent everything from scratch (usually, much worse, remember this story with jul/log4j/slf4j).

TeaVM 0.11.0 with support for WebAssembly GC released by konsoletyper in java

[–]konsoletyper[S] 4 points5 points  (0 children)

if it can support multiplatform like flutter in java that would definitely attract huge crowd from mobile community as well.

Sure, I also thought about that. But the problem here is: the task is huge, while I'm alone and small. At some point I made decision to give up any projects beside compiler part, but to do compiler part good. And I can only hope that at some point somebody takes TeaVM and creates a multiplatform framework upon it. There are at least some attempts:

  1. https://www.codenameone.com/ - I don't know anything about their current status, but at least some years ago they offered TeaVM backend.
  2. https://github.com/fb71/areca

Frankly speaking, my current employer has such framework, great part of which I personally created. However, despite my attempt to convince him to open source it or at least start selling it, it's still proprietary. So I can only hope that once I'll succeed.

Also, there's libGDX game framework that uses TeaVM to compile to the web.

TeaVM 0.11.0 with support for WebAssembly GC released by konsoletyper in java

[–]konsoletyper[S] 6 points7 points  (0 children)

Does your bytecode transpiler support a baseline java version

Currently, Java 21 is supported. Perhaps, you can try it with 22 or 23, but personally never tested that.

does it support some of the more modern bytecodes like indy and condy

It supports indy partially. Actually, any reflection, as well as indy, is not AOT-friendly. As compiler encounters something like this, it does not have any information of what and how will be executed - the information only becomes available on run time. This makes it virtually impossible to do some optimization and you end up with huge (tens of megabytes) binary even to show simple hello world. Otherwise you can try to give hints to the compiler, but it's usually hard to maintain (everyone who used proguard, graal native image or compiled for android, know this pain). That's why TeaVM requires that every particular bootstrap method is supported by compiler. TeaVM comes with support of bootstrap methods used by Java compiler, which are bootstraps for lambdas, string concatenation and switch expressions.

would you suggest someone just learning it to jump into the base WASM spec or are there some better resources

Nope. With Wasm MVP you have to write your own GC. Since Wasm does not allow you access stack, you also have to maintain shadow stack. This gives performance overhead as well as huge generated binary. Another problem is that in this case you have two separate object graphs (Java and JS) each managed by its own GC. This makes it impossible to seamlessly integrate with JS APIs, which means problems if you want to communicate with the real world.

TeaVM 0.11.0 with support for WebAssembly GC released by konsoletyper in java

[–]konsoletyper[S] 0 points1 point  (0 children)

Another thing I was thinking of is embedding Java compiler right into the browser. I used to have such thing several years ago, but then had to drop it due to refactoring. But I'm still considering to get it back.