jtransc icon indicating copy to clipboard operation
jtransc copied to clipboard

WebAssembly support

Open sdeleuze opened this issue 9 years ago • 9 comments

First, congrats and thanks for creating such ambitious project!

It seems to me that there is no current effort for supporting JVM -> WebAssembly, which is likely to be the future for gaming/high performance applications on the browser. Do you have any plan to support generating WASM bytecode?

sdeleuze avatar May 29 '16 22:05 sdeleuze

Thanks :)

Actually, i think that lime supports generating emscripten (which is used un both: gdx-backend-jtransc, and jtransc-media). And since it also can generate c++ already, it can for sure generate WASM too using emscripten. But didn't tried yet.

soywiz avatar May 30 '16 00:05 soywiz

Interesting, thanks ! Based on this tweet, I guess we can also expect WebAssembly support in Haxe when it will support GC (sadly not planned for the MVP).

I know that's not necessarily the curent focus, but do you think it could make sense from a technical POV to generate directly WASM bytecode (without using Haxe) using Jtransc AST if somebody want to contribute it, or is it better even for the long term to use Haxe for that?

sdeleuze avatar May 30 '16 14:05 sdeleuze

Yeah I think it will happen sometime.

As far as i know it could be pretty complicated/expensive implement a GC on top of that, since you don't have direct access to the call stack. I didn't think too much about this, so maybe there are workarounds. But there is no much documentation about GC on emscripten, asm.js or wasm.

It would be easier to do ARC just as j2objc does: http://j2objc.org/docs/Memory-Management.html

Just marking fields as Weak. That memory management should be compatible with (W)ASM.JS, but it would require extra effort marking fields as Weak, not just the application ones but marking fields in external libraries. Also some libraries could lead to leaks without small modifications.

I think it would be even more useful to just generate LLVM assembly instead of directly WASM, so you can reuse it in other places. Also LLVM allows you to define memory management strategies. And it would work directly with emscripten and allow to benefit from all the LLVM optimizers.

soywiz avatar May 30 '16 17:05 soywiz

Indeed, on WASM side there is only this documentation about GC.

I also thought about generating LLVM assembly. I saw some limitations mentioned in this LLVM mailing list thread, do you think Jtransc could help to mitigate some of them?

sdeleuze avatar May 30 '16 20:05 sdeleuze

Compiling haxe to c++ and finally to emscripten/asm.js seems to work fine. Since emscripten can also emit WASM it should be possible to compile haxe to WASM.

intrigus avatar May 30 '16 20:05 intrigus

I think is could be done already. As far as I know Unity's il2cpp uses boehm GC too and seems to work with emscripten. So with C++ target, we could do this. I think we can add support for the emscripten toolchain and do some example calling JS like webgl and so.

soywiz avatar May 31 '17 20:05 soywiz

I'm currently working on integrating bdwgc, but bdwgc is currently broken for emscripten https://github.com/ivmai/bdwgc/issues/163 Running without gc already works but I'm not sure how the interface should look. E.g. One mainTarget cpp and the subTarget emscripten? Special emscripten target?

intrigus avatar May 31 '17 20:05 intrigus

Yes, the idea is to use subtargets. We can also add defines/macros to subtargets. Actually I would like to change the "subtarget" name to something else. Maybe toolchain? So you have target C++ and toolchain emscripten? ./gradlew distcpp:emscripten or something like that.

I forgot the references: https://blogs.unity3d.com/es/2015/07/09/il2cpp-internals-garbage-collector-integration/

soywiz avatar May 31 '17 20:05 soywiz

Also soon will be able to use plain threads here: https://v8project.blogspot.com.es/2017/06/v8-release-60.html http://kangax.github.io/compat-table/es2016plus/#test-shared_memory_and_atomics

soywiz avatar Jun 10 '17 12:06 soywiz