MotleyCoderDev / wasm2brs

Bringing the power of WebAssembly to the Roku and supporting languages like C/C++/Rust.
https://motley-coder.com/2020/12/20/webassembly-on-roku/
Other
49 stars 5 forks source link

How Can I Help #3

Open jfspencerAngel opened 2 years ago

jfspencerAngel commented 2 years ago

@TrevorSundberg This work is epic! Are there any plans to integrate with SceneGraph? Is that even possible? I am starting on a Roku project soon and would really enjoy not writing BrightScript. Is there anything that still needs work? I would like to help where I can. I would like to consume this library from Kotlin Source (via the KMP Wasm compiler)

TrevorSundberg commented 2 years ago

Hey there! Right now, interoping is possible by exporting functions with C linkage, however it only works for primitive types (float, double, int32_t, int64_t, etc). Any attempt to pass a pointer shows up as a number in BrightScript, which is an offset from the beginning of a giant array of bytes (virtual memory like WASM). There are some helpers to extract strings from the memory and convert them to BrightScript strings and visa versa, but these are not documented, see helpers.brs I believe.

What that means is that allocating classes and calling some methods in BrightScript (e.g. SceneGraph) isn't possible purely from WASM. This is the same issue browsers have between WASM and JavaScript, where WASM can't allocate JavaScript classes, etc. Typically you end up having to write a binding layer between WASM and BrightScript, so it doesn't entirely eliminate the need to write BrightScript. If you're familiar with Emscripten, they solved this similar issue with JavaScript by writing the embind library which marshals types, and currently I do not have a binding library. It's certainly an open work item though!

To see a more real example of it working: https://www.reddit.com/r/itrunsdoom/comments/m847ne/roku_running_doom_compiled_to_wasm_and_converted/

And here are two articles about how it works if you're interested: https://motley-coder.com/2020/12/23/doom-on-roku/ https://motley-coder.com/2020/12/20/webassembly-on-roku/

Side note, I have no idea what the runtime looks like for Kotlin's KMP Wasm compiler. If they support WASI, then it should just work out of the box (there is some missing WASI implementations, but a good amount works).

Another note, BrightScript is slowwwwwww! Currently the best use case for wasm2brs is to compile small C or Rust libraries into BrightScript and call them from your app.