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

@ -99,6 +99,10 @@ pub enum VectorKind {
Anyref,
}
pub struct Number {
u32: bool,
}
impl Descriptor {
pub fn decode(mut data: &[u32]) -> Descriptor {
let descriptor = Descriptor::_decode(&mut data);
@ -149,18 +153,20 @@ impl Descriptor {
}
}
pub fn is_number(&self) -> bool {
/// Returns `Some` if this type is a number, and the returned `Number` type
/// can be accessed to learn more about what kind of number this is.
pub fn number(&self) -> Option<Number> {
match *self {
Descriptor::I8
| Descriptor::U8
| Descriptor::I16
| Descriptor::U16
| Descriptor::I32
| Descriptor::U32
| Descriptor::F32
| Descriptor::F64
| Descriptor::Enum { .. } => true,
_ => return false,
| Descriptor::Enum { .. } => Some(Number { u32: false }),
Descriptor::U32 => Some(Number { u32: true }),
_ => None,
}
}
@ -360,3 +366,9 @@ impl VectorKind {
}
}
}
impl Number {
pub fn is_u32(&self) -> bool {
self.u32
}
}

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