Add support for optional bools

This commit is contained in:
Anton Danilkin
2018-08-03 19:07:12 +03:00
committed by Alex Crichton
parent 0ef528165f
commit 4a0c69ffed
5 changed files with 74 additions and 4 deletions

View File

@ -259,7 +259,26 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
return Ok(self);
}
bail!("unsupported optional argument type for calling Rust function from JS: {:?}", arg);
match *arg {
Descriptor::Boolean => {
self.cx.expose_is_like_none();
self.js_arguments.push((name.clone(), "boolean".to_string()));
if self.cx.config.debug {
self.cx.expose_assert_bool();
self.prelude(&format!(
"
if (!isLikeNone({0})) {{
_assertBoolean({0});
}}
",
name,
));
}
self.rust_arguments.push(format!("isLikeNone({0}) ? 0xFFFFFF : {0} ? 1 : 0", name));
return Ok(self);
},
_ => bail!("unsupported optional argument type for calling Rust function from JS: {:?}", arg),
};
}
if let Some(s) = arg.rust_struct() {
@ -486,7 +505,17 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
return Ok(self);
}
bail!("unsupported optional return type for calling Rust function from JS: {:?}", ty);
match *ty {
Descriptor::Boolean => {
self.ret_ty = "boolean".to_string();
self.ret_expr = "
const ret = RET;
return ret === 0xFFFFFF ? undefined : ret !== 0;
".to_string();
return Ok(self);
},
_ => bail!("unsupported optional return type for calling Rust function from JS: {:?}", ty),
};
}
if ty.is_ref_anyref() {

View File

@ -173,7 +173,13 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
return Ok(());
}
bail!("unsupported optional argument type for calling JS function from Rust: {:?}", arg);
match *arg {
Descriptor::Boolean => {
self.js_arguments.push(format!("{0} === 0xFFFFFF ? undefined : {0} !== 0", abi));
return Ok(())
},
_ => bail!("unsupported optional argument type for calling JS function from Rust: {:?}", arg),
};
}
if let Some(signed) = arg.get_64() {
@ -424,7 +430,17 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
return Ok(());
}
bail!("unsupported optional return type for calling JS function from Rust: {:?}", ty);
match *ty {
Descriptor::Boolean => {
self.cx.expose_is_like_none();
self.ret_expr = "
const val = JS;
return isLikeNone(val) ? 0xFFFFFF : val ? 1 : 0;
".to_string();
return Ok(());
},
_ => bail!("unsupported optional return type for calling JS function from Rust: {:?}", ty),
};
}
if ty.is_number() {
self.ret_expr = "return JS;".to_string();