#TypeError in wasm

1 messages · Page 1 of 1 (latest)

craggy lynx
#

this is zig function

export fn twoed(n: i32) i32 {
    return n * 2;
}

export fn rando(seed: u128, min: u128, max: u128) u128 {
    return (((seed * 1103515245) + 12345) & 0x7FFFFFFF) % (max - min) + min;
}

I built with this command
zig build-lib -dynamic -rdynamic -target wasm32-freestanding bum.zig
And called it from JavaScript

"use strict";
const wasmModule = await WebAssembly.instantiateStreaming(fetch('bum.wasm'));
const { twoed, rando } = wasmModule.instance.exports;
console.log(twoed(10));
console.log(rando(Math.floor(Date.now() / 1000), 200, 5302));

and I got this error for the rando function

Uncaught TypeError: can't convert 200 to BigInt

help

shadow tendon
#

you might need to convert it explicitly

#

write 200n instead of 200

#

n makes it a bigint

slate spruce
#

Also, your function signature is entirely wrong on JS side. u128 arguments and return types do not get passed through like that

#

For return types an sparam is used and for arguments it’s passed as 2 64bit integers

shadow tendon
#

oh i didn't know that, interesting

craggy lynx
#

thanks to both. Now this is a js question I guess. I changed all the u128 to u64, and now I get this error

Uncaught TypeError: can't convert 1699262970 to BigInt

How to convert Math.floor(Date.now() / 1000) to BigInt?

shadow tendon
#

BigInt(Math.floor(...))