moonbitlang / moonbit-docs

The docs of MoonBit programming language
https://docs.moonbitlang.com
Other
1.07k stars 51 forks source link

WebAssembly.instantiate(): invalid value type #208

Closed 1226085293 closed 4 months ago

1226085293 commented 4 months ago

main.mbt

pub fn now() -> Int = "Date" "now"

fn main {
  let startTime = now()
  let mut value = 0
  for index = 0; index < 10000000; index = index + 1 {
    value = value + 1;
  }
  println(now() - startTime)
  // println(@lib.hello())
}

Usage:

// 读取 WebAssembly 文件
const wasmBuffer = fs.readFileSync("./wasm/main.wasm");

// 使用 WebAssembly.instantiate 直接实例化
//@ts-ignore
const wasmModule = await WebAssembly.instantiate(wasmBuffer, {
    Date: {
        now: () => Date.now(),
    },
    now: () => Date.now(),
});

Error:

Error instantiating WebAssembly module: CompileError: WebAssembly.instantiate(): invalid value type 0x77 @+12 {stack: 'CompileError: WebAssembly.instantiate(): invalid value type 0x77 @+12', message: 'WebAssembly.instantiate(): invalid value type 0x77 @+12'}

Note: There is no error if the now function is not used.

peter-jerry-ye commented 4 months ago

Can not reproduce. Are you using Node 22 that supports wasm gc?

1226085293 commented 4 months ago

MoonBit Project: MoonBit.zip

Test NodeJs Project: ts_test.zip

image

My Node version supports GC, because I had this problem and upgraded my Node version, then it was the problem now

System information: Windows 11 Professional.23H2

peter-jerry-ye commented 4 months ago

Node 20 does not support GC. It has to be enabled with an experimental flag, and the support was unstable. The support was added in Node 22.

1226085293 commented 4 months ago

Node 20 does not support GC. It has to be enabled with an experimental flag, and the support was unstable. The support was added in Node 22.

I will try using Node22 later