Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webassembly multiple modules?

Tags:

webassembly

Lets assume we have the following:

(module)

Just a simple module with no functions, locals, memory and all the fancy stuff. Now what if I dont want to overpopulate my module? I mean id like to have two modules but I obviously get an error when trying to compile something like:

(module)
(module)

So is there a way to give each module its own memory or id? Or is there any way to do so? Does it even make sense? Im just saying since a module can really become huge.

What im trying to accomplish is to communicate between two modules since I believe it makes sense to prevent a module to become too big. Im new to webassembly so maybe what im saying sounds crazy.

For compilation tests I use https://mbebenita.github.io/WasmExplorer/

like image 1000
Asperger Avatar asked May 17 '26 19:05

Asperger


1 Answers

WebAssembly modules are similar to executables on your disk: they haven't been loaded into memory.

A WebAssembly instance is what makes a module come to life. You can instantiate the same module multiple times, or instantiate multiple modules. Instantiation will load the module, compile / validate it, initialize its globals and memory (if any), populate exports, and then call its start function.

Your examples use the s-expression syntax, but you don't specify how you're executing WebAssembly. The s-expression themselves are just text, an embedder is needed to run the compiled result!

In a JavaScript embedding the API has the WebAssembly.Module and WebAssembly.Instance constructors, as well as the WebAssembly.compile and WebAssembly.instantiate functions (either works, WebAssembly.instantiate is simpler / better). You pass the instance constructor / function an import object. WebAssembly memories are either created by declaring a memory section in the binary (then a memory is automatically created at instantiation time), or by exporting it through the import object.

You can even perform dynamic linking by creating multiple modules, instantiating them with sharing of a memory between them, and having them import / export each other's functions.

In the spec interpreter you can declare multiple modules which get validated, and you have extra helpers to poke at them. See this test which starts with:

(module "\00asm\01\00\00\00")
(module "\00asm" "\01\00\00\00")
(module $M1 "\00asm\01\00\00\00")
(module $M2 "\00asm" "\01\00\00\00")

(assert_malformed (module "") "unexpected end")
(assert_malformed (module "\01") "unexpected end")
like image 72
JF Bastien Avatar answered May 21 '26 18:05

JF Bastien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!