Stream: javy

Topic: Loading external WASM files


view this post on Zulip Tom Wieland (Jun 29 2023 at 12:22):

Hello.

I've now gotten my Javy built WASM program to run and do a hello world so my next question is how I would integrate WASM code written in different languages (for example AssemblyScript or Rust) and run them from within the JavaScript.

I think it would be very beneficial for this to be documented and/or to become an example on the Javy Github.

view this post on Zulip Jeff Charles (Jun 29 2023 at 13:51):

The short answer is that isn't possible using the Javy CLI.

What you can do is use the javy and javy-apis crate along with your own Rust code to build your own Wasm module that includes whatever Wasm imports you need (in Rust) and then you can use the wrap_callback method on JSContextRef (accessible via the context method on Runtime) to expose functions to the JavaScript runtime that will enable the JS code to invoke those imported Wasm functions. You'll also need to set up a linker in your browser or Wasmtime runtime to link the exports from your other Wasm modules to the Wasm module you create. Does that make sense?

view this post on Zulip Jeff Charles (Jun 29 2023 at 14:04):

Another option to consider is looking into jco which should provide better tooling if you want to use Wasm components and JavaScript.

JavaScript tooling for working with WebAssembly Components - GitHub - bytecodealliance/jco: JavaScript tooling for working with WebAssembly Components

view this post on Zulip Tom Wieland (Jun 29 2023 at 15:16):

Jeff Charles said:

The short answer is that isn't possible using the Javy CLI.

What you can do is use the javy and javy-apis crate along with your own Rust code to build your own Wasm module that includes whatever Wasm imports you need (in Rust) and then you can use the wrap_callback method on JSContextRef (accessible via the context method on Runtime) to expose functions to the JavaScript runtime that will enable the JS code to invoke those imported Wasm functions. You'll also need to set up a linker in your browser or Wasmtime runtime to link the exports from your other Wasm modules to the Wasm module you create. Does that make sense?

Right that makes sense. Rust is totally new to me but the fun of being able to use different languages and libraries from them in the same ecosystem is why I started the project. So I'll have to learn a bit of Rust to get going :)

My aim is to use PureScript for the application code, Javy for providing it as WASM and https://github.com/lunatic-solutions/lunatic for the server side runtime.

I think using the approach you described would allow me to package the WASM file with the rust crates, javy & quickjs & application code part and run it as a lunatic application :)

This is pretty challenging but that's the fun \ :D /

Lunatic is an Erlang-inspired runtime for WebAssembly - GitHub - lunatic-solutions/lunatic: Lunatic is an Erlang-inspired runtime for WebAssembly

view this post on Zulip Jeff Charles (Jun 29 2023 at 19:36):

Sorry, I'm not quite following what you mean by:

package the WASM file with the rust crates, javy & quickjs & application code part

What I was suggesting was you can create a Rust app or library which will compile to a Wasm module. And that Rust app or library that you're compiling to Wasm can use the javy and javy-apis crates to run JS (or PureScript).

If you want to call exports from other Wasm modules or make exports from your Wasm module available to other Wasm modules, you may want to look into the Wasm component model and jco instead. You can make it work with your own Rust code but jco and the component model provide helpful abstractions for sending complex data (that is, values that aren't numbers) between Wasm modules.


Last updated: Oct 23 2024 at 20:03 UTC