| 3 | // contract a bundler must satisfy is small: a default-exported factory that |
| 4 | // owns wasm instantiation and honors the `instantiateWasm` escape hatch. |
| 5 | export default function createModule(moduleArg = {}) { |
| 6 | const Module = moduleArg; |
| 7 | |
| 8 | // The import object the wasm needs. Only the glue knows how to build it, |
| 9 | // which is why webpack cannot instantiate the module itself. |
| 10 | const imports = { |
| 11 | env: { |
| 12 | log(value) { |
| 13 | if (Module.onLog) Module.onLog(value); |
| 14 | } |
| 15 | } |
| 16 | }; |
| 17 | |
| 18 | return new Promise((resolve, reject) => { |
| 19 | const receiveInstance = (instance) => { |
| 20 | Module.run = (n) => instance.exports.run(n); |
| 21 | resolve(Module); |
| 22 | }; |
| 23 | |
| 24 | // Emscripten's official hook: hand instantiation to the embedder. |
| 25 | if (Module.instantiateWasm) { |
| 26 | Module.instantiateWasm(imports, receiveInstance); |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | reject(new Error("This minimal glue requires an instantiateWasm hook")); |
| 31 | }); |
| 32 | } |