#JS/WASM "TypeError: can't convert BigInt to number"

1 messages · Page 1 of 1 (latest)

autumn prairie
#

I'm trying to implement a mini little FS in zig and export it to JS through WASM but im running into an issue where the JS is erroring out with the error in the title of the post and I can't figure out why. I have a print statement (that i know works) that isnt running which is what is confusing me the most
Relevant zig code:

export fn write(fd: u32, path_ptr: [*:0]u8) u32 {
    logStr("write() called".ptr);
    return filesys.write(fd, .{}, readString(path_ptr)) catch 0;
}
export fn allocString(len: usize) [*]u8 {
    const arr = wasm_alloc.alloc(u8, len + 1) catch panic("allocString() failed", @src());
    if (comptime builtin.target.cpu.arch == .wasm32) logStr("allocString() done".ptr);
    return arr.ptr;
}

Relevant JS code:

    let bytes_written = write(serialNum, stringToPointer("test"));
...
function stringToPointer(str) {
    const buffer = new TextEncoder().encode(str);
    const pointer = allocString(buffer.length); // ask Zig to allocate memory
    const slice = new Uint8Array(
        wasm.instance.exports.memory.buffer, // memory exported from Zig
        pointer,
        buffer.length + 1
    );
    slice.set(buffer);
    slice[buffer.length] = 0; // null byte to null-terminate the string
    return pointer;
}

I can post more code if needed but i believe everything shown is whats relevant

thanks in advance <3

unborn hornet
autumn prairie
#

it points to that first line of the JS code i put, sorry i forgot to mention that

#

specifically let bytes_written = write(serialNum, stringToPointer("test"));

#

where serialNum is known

unborn hornet
#

you can console.log serialNum and stringToPointer("test") to see if either of them are BigInt

autumn prairie
#

so serialNum is a BigInt but i dont see how that is an issue, shouldnt js be able to pass that directly to zig? if not then how would I go about making sure seriaNum is a number

#

nvm on that 2nd question, using a u32 instead of u64 on the return type when getting serialNum fixed it but that's weird behavior to me, i likely just dont know enough abt js/wasm to know why its like that

#

thank you!

unborn hornet
#

all js numbers are f64. an f64 can safely store any 32 bit int, but not any 64 bit int

#

so 64 bit ints in wasm are converted to bigints in js

autumn prairie
#

oh that's,,,,, a choice

unborn hornet
#

it helps for making the interpreter because they can have every value be 8 bytes long (everything is a float, even object references, because they use unused NaN space in the float to store whatever they want)

autumn prairie
#

fair enough

desert shadow
#

JS integers are 54 (53?) bit ints - basically the non-exponent part of an f64.

unborn hornet
desert shadow
#

The range comes up when dealing with JSON interop in other contexts.