You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 as i32 in JavaScript.
Although this function is meant to return a u32, it is represented as i32 in the WASM type system.
When I import and call this function in Deno, the result appears incorrect:
add(2147483647,1);// Produces -2147483648
Some informations I found
WebAssembly supports only a limited set of number types: i32, i64, f32, and f64. The distinction between signed and unsigned integers is made through specific instructions like i32.gt (signed) and i32.gt_u (unsigned). Consequently, a function returning i32 in WASM may correspond to either i32 or u32 in the original Rust source.
Tools like wasm-bindgen handle unsigned integer transformations during post-processing. They wrap the result using number >>> 0 in JavaScript to correctly interpret u32 values. For example, a function returning u32 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 or u32, how can deno ensure that the result is interpreted correctly as u32?
The text was updated successfully, but these errors were encountered:
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
?The text was updated successfully, but these errors were encountered: