1
0
mirror of https://github.com/fluencelabs/wasm-bindgen synced 2025-06-19 16:01:23 +00:00

Fix handling of u32 between Rust and JS

All numbers in WebAssembly are signed and then each operation on them
may optionally have an unsigned version. This means that when we pass
large signed numbers to JS they actually show up as large negative
numbers even though JS numbers can faithfully represent the type.

This is fixed by adding `>>>0` in a few locations in the generated
bindings to coerce the JS value into an unsigned value.

Closes 
This commit is contained in:
Alex Crichton
2019-03-26 17:29:53 -07:00
parent e3aabcb27d
commit 0160f6af45
5 changed files with 126 additions and 9 deletions
crates/cli-support/src
tests/wasm

@ -384,7 +384,7 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
return Ok(self);
}
if arg.is_number() {
if arg.number().is_some() {
self.js_arguments.push((name.clone(), "number".to_string()));
if self.cx.config.debug {
@ -681,9 +681,13 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
return Ok(self);
}
if ty.is_number() {
if let Some(num) = ty.number() {
self.ret_ty = "number".to_string();
self.ret_expr = format!("return RET;");
if num.is_u32() {
self.ret_expr = format!("return RET >>> 0;");
} else {
self.ret_expr = format!("return RET;");
}
return Ok(self);
}