emscripten-core / emscripten-core/emscripten
How to share memory between two modules?
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
In my application I have two separate modules, built with `-sMODULARIZE=1` and `USE_PTHREADS=1`. Now I'm trying to make them share the same block of memory by initializing them this way:
```js
var wasmMemory = new WebAssembly.Memory({
"initial": 65536e3 / 65536,
"maximum": 2147483648 / 65536,
"shared": true
});
const moduleMain = await ModuleMain({
noInitialRun: true,
wasmMemory: wasmMemory,
canvas: (function () {
return document.getElementById('canvas');
})()
})
const moduleInterpreter = await ModuleInterpreter({
noInitialRun: true,
wasmMemory: wasmMemory,
})
console.log("moduleInterpreter loaded")
```
`noItinitialRun` is set to `true` so I can do a step-by-step experimentation.
The problem I'm facing is that everything blows up during the runtime initialization of `ModuleInterpreter`:

Looks like the memory is being shared, but the second module is tripping up on memory of the first one, during the call of static constructors.
So, is there an "official" way of doing this? I suppose I should telll `ModuleInterpreter` to start using the `wasmMemory` at a certain offset, past the memory used by the first module.
I guess I'll run into other problems with the dynamic memory allocator after solving this, but they should be easier to deal with since I already use a custom one
Contributor guide
Assessment
This issue has not been assessed yet.