Closed swwind closed 3 days ago
u32
is not supported in Wasm and Deno cannot handle it. Only build tools like wasm-bindgen
know the type from the source and can coerce it to an unsigned value.
There are two ways for a user:
>>> 0
).u32
.add(2147483647, 1); // Produces -2147483648
Due to the reasons mentioned this is correct; it is interpreted as i32
.
Thanks for the reminder. I was hoping Deno's direct import of WASM would make things as straightforward as importing another JS file. However, it seems we still can’t avoid relying on wasm-bindgen and other tools to make the types correct for JS.
Version: Deno 2.1.1
I'm exploring Deno's capability to import WebAssembly (WASM) directly into JavaScript. However, I encountered an issue where a function returning a
u32
in Rust is always interpreted asi32
in JavaScript.For example, consider the following Rust code:
This compiles to the following WebAssembly (shown in WAT):
Although this function is meant to return a
u32
, it is represented asi32
in the WASM type system.When I import and call this function in Deno, the result appears incorrect:
Some informations I found
WebAssembly supports only a limited set of number types:
i32
,i64
,f32
, andf64
. The distinction between signed and unsigned integers is made through specific instructions likei32.gt
(signed) andi32.gt_u
(unsigned). Consequently, a function returningi32
in WASM may correspond to eitheri32
oru32
in the original Rust source.Tools like
wasm-bindgen
handle unsigned integer transformations during post-processing. They wrap the result usingnumber >>> 0
in JavaScript to correctly interpretu32
values. For example, a function returningu32
in Rust would be transformed into a JS-compatible form like this:So, how can Deno handle this situation effectively? Since there’s nothing in the compiled WASM output explicitly indicating whether a return type is
i32
oru32
, how can deno ensure that the result is interpreted correctly asu32
?