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 #1388
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

View File

@ -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);
}

View File

@ -309,8 +309,16 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
return Ok(());
}
if let Some(num) = arg.number() {
if num.is_u32() {
self.js_arguments.push(format!("{} >>> 0", abi));
} else {
self.js_arguments.push(abi);
}
return Ok(());
}
let invoc_arg = match *arg {
ref d if d.is_number() => abi,
Descriptor::Boolean => format!("{} !== 0", abi),
Descriptor::Char => format!("String.fromCodePoint({})", abi),
_ => bail!(
@ -504,7 +512,7 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
return Ok(());
}
if ty.is_number() {
if ty.number().is_some() {
self.ret_expr = "return JS;".to_string();
return Ok(());
}