wasm-bindgen icon indicating copy to clipboard operation
wasm-bindgen copied to clipboard

Fix `__wbg_set_wasm` call order on nodejs-module

Open kawaemon opened this issue 1 year ago • 1 comments

When targeting experimental-nodejs-module and using #[wasm_bindgen(start)] attribute, current wasm-bindgen generates wasm.__wbindgen_start(); statement before __wbg_set_wasm call:

import { __wbg_set_wasm } from "./ej_bg.js";

let imports = {};
import * as import0 from './ej_bg.js';
imports['./ej_bg.js'] = import0;

// ... reducted... 

const wasm = wasmInstance.exports;
export const __wasm = wasm;


wasm.__wbindgen_start();

__wbg_set_wasm(wasm);
export * from "./ej_bg.js";

which causes runtime exception like below.

❯ node a/nm/ej.js
file:///tmp/ktmp/ej/a/nm/ej_bg.js:17
        cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
                                                      ^

TypeError: Cannot read properties of undefined (reading 'memory')
    at getUint8ArrayMemory0 (file:///tmp/ktmp/ej/a/nm/ej_bg.js:17:55)
    at getStringFromWasm0 (file:///tmp/ktmp/ej/a/nm/ej_bg.js:24:37)
    at __wbindgen_string_new (file:///tmp/ktmp/ej/a/nm/ej_bg.js:62:17)
    at ej.wasm.wasm_bindgen::JsValue::from_str::h94d293071ebb3387 (wasm://wasm/ej.wasm-00039026:wasm-function[87]:0x7bf7)

This PR fixes this issue.

kawaemon avatar Aug 25 '24 19:08 kawaemon

Cc @Systemcluster, would appreciate your review here.

daxpedda avatar Aug 27 '24 08:08 daxpedda