Merge pull request #1398 from RReverser/is-like-none

Consistently expose `is_like_none`
This commit is contained in:
Alex Crichton 2019-03-26 14:29:46 -05:00 committed by GitHub
commit e3aabcb27d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 21 deletions

View File

@ -248,8 +248,9 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
}
if optional {
self.cx.expose_is_like_none();
if arg.is_wasm_native() {
self.cx.expose_is_like_none();
self.js_arguments
.push((name.clone(), "number | undefined".to_string()));
@ -272,7 +273,6 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
}
if arg.is_abi_as_u32() {
self.cx.expose_is_like_none();
self.js_arguments
.push((name.clone(), "number | undefined".to_string()));
@ -321,7 +321,6 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
match *arg {
Descriptor::Boolean => {
self.cx.expose_is_like_none();
self.js_arguments
.push((name.clone(), "boolean | undefined".to_string()));
if self.cx.config.debug {
@ -337,42 +336,38 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
}
self.rust_arguments
.push(format!("isLikeNone({0}) ? 0xFFFFFF : {0} ? 1 : 0", name));
return Ok(self);
}
Descriptor::Char => {
self.cx.expose_is_like_none();
self.js_arguments
.push((name.clone(), "string | undefined".to_string()));
self.rust_arguments
.push(format!("isLikeNone({0}) ? 0xFFFFFF : {0}.codePointAt(0)", name));
return Ok(self);
}
Descriptor::Enum { hole } => {
self.cx.expose_is_like_none();
self.js_arguments
.push((name.clone(), "number | undefined".to_string()));
self.rust_arguments
.push(format!("isLikeNone({0}) ? {1} : {0}", name, hole));
return Ok(self);
}
Descriptor::RustStruct(ref s) => {
self.js_arguments
.push((name.clone(), format!("{} | undefined", s)));
self.prelude(&format!("let ptr{} = 0;", i));
self.prelude(&format!("if ({0} !== null && {0} !== undefined) {{", name));
self.prelude(&format!("if (!isLikeNone({0})) {{", name));
self.assert_class(&name, s);
self.assert_not_moved(&name);
self.prelude(&format!("ptr{} = {}.ptr;", i, name));
self.prelude(&format!("{}.ptr = 0;", name));
self.prelude("}");
self.rust_arguments.push(format!("ptr{}", i));
return Ok(self);
}
_ => bail!(
"unsupported optional argument type for calling Rust function from JS: {:?}",
arg
),
};
}
return Ok(self);
}
if let Some(s) = arg.rust_struct() {

View File

@ -389,8 +389,9 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
return Ok(());
}
if optional {
self.cx.expose_is_like_none();
if ty.is_wasm_native() {
self.cx.expose_is_like_none();
self.cx.expose_uint32_memory();
match ty {
Descriptor::I32 => self.cx.expose_int32_memory(),
@ -425,7 +426,6 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
}
if ty.is_abi_as_u32() {
self.cx.expose_is_like_none();
self.ret_expr = "
const val = JS;
return isLikeNone(val) ? 0xFFFFFF : val;
@ -435,7 +435,6 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
}
if let Some(signed) = ty.get_64() {
self.cx.expose_is_like_none();
self.cx.expose_uint32_memory();
let f = if signed {
self.cx.expose_int64_memory();
@ -458,13 +457,11 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
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(());
}
Descriptor::Char => {
self.ret_expr = "
@ -472,10 +469,8 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
return isLikeNone(val) ? 0xFFFFFF : val.codePointAt(0);
"
.to_string();
return Ok(());
}
Descriptor::Enum { hole } => {
self.cx.expose_is_like_none();
self.ret_expr = format!(
"
const val = JS;
@ -483,14 +478,13 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
",
hole
);
return Ok(());
}
Descriptor::RustStruct(ref class) => {
// Like below, assert the type
self.ret_expr = format!(
"\
const val = JS;
if (val === undefined || val === null)
if (isLikeNone(val))
return 0;
if (!(val instanceof {0})) {{
throw new Error('expected value of type {0}');
@ -501,13 +495,14 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
",
class
);
return Ok(());
}
_ => bail!(
"unsupported optional return type for calling JS function from Rust: {:?}",
ty
),
};
return Ok(());
}
if ty.is_number() {
self.ret_expr = "return JS;".to_string();