Skip to content

Add support for optional numbers #630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion crates/cli-support/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,27 @@ impl Descriptor {
}
}

pub fn get_64bit(&self) -> Option<bool> {
pub fn is_wasm_native(&self) -> bool {
match *self {
Descriptor::I32
| Descriptor::U32
| Descriptor::F32
| Descriptor::F64 => true,
_ => return false,
}
}

pub fn is_abi_as_u32(&self) -> bool {
match *self {
Descriptor::I8
| Descriptor::U8
| Descriptor::I16
| Descriptor::U16 => true,
_ => return false,
}
}

pub fn get_64(&self) -> Option<bool> {
match *self {
Descriptor::I64 => Some(true),
Descriptor::U64 => Some(false),
Expand Down
212 changes: 200 additions & 12 deletions crates/cli-support/src/js/js2rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,100 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
}

if optional {
bail!("unsupported optional argument to rust function {:?}", arg);
if arg.is_wasm_native() {
self.cx.expose_is_like_none();
self.js_arguments.push((name.clone(), "number".to_string()));

if self.cx.config.debug {
self.cx.expose_assert_num();
self.prelude(&format!(
"
if (!isLikeNone({0})) {{
_assertNum({0});
}}
",
name
));
}

self.rust_arguments.push(format!("!isLikeNone({0})", name));
self.rust_arguments.push(format!("isLikeNone({0}) ? 0 : {0}", name));
return Ok(self);
}

if arg.is_abi_as_u32() {
self.cx.expose_is_like_none();
self.js_arguments.push((name.clone(), "number".to_string()));

if self.cx.config.debug {
self.cx.expose_assert_num();
self.prelude(&format!(
"
if (!isLikeNone({0})) {{
_assertNum({0});
}}
",
name
));
}

self.rust_arguments.push(format!("isLikeNone({0}) ? 0xFFFFFF : {0}", name));
return Ok(self);
}

if let Some(signed) = arg.get_64() {
let f = if signed {
self.cx.expose_int64_cvt_shim()
} else {
self.cx.expose_uint64_cvt_shim()
};
self.cx.expose_uint32_memory();
self.cx.expose_global_argument_ptr()?;
self.js_arguments.push((name.clone(), "BigInt".to_string()));
self.prelude(&format!(
"
{f}[0] = isLikeNone({name}) ? BigInt(0) : {name};
const low{i} = isLikeNone({name}) ? 0 : u32CvtShim[0];
const high{i} = isLikeNone({name}) ? 0 : u32CvtShim[1];
",
i = i,
f = f,
name = name,
));
self.rust_arguments.push(format!("!isLikeNone({})", name));
self.rust_arguments.push(format!("0"));
self.rust_arguments.push(format!("low{}", i));
self.rust_arguments.push(format!("high{}", i));
return Ok(self);
}

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);
},
Descriptor::Char => {
self.cx.expose_is_like_none();
self.js_arguments.push((name.clone(), "string".to_string()));
self.rust_arguments.push(format!("!isLikeNone({0})", name));
self.rust_arguments.push(format!("isLikeNone({0}) ? 0 : {0}.codePointAt(0)", name));
return Ok(self);
},
_ => bail!("unsupported optional argument type for calling Rust function from JS: {:?}", arg),
};
}

if let Some(s) = arg.rust_struct() {
Expand Down Expand Up @@ -240,7 +333,7 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
return Ok(self);
}

if let Some(signed) = arg.get_64bit() {
if let Some(signed) = arg.get_64() {
let f = if signed {
self.cx.expose_int64_cvt_shim()
} else {
Expand All @@ -250,17 +343,17 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
self.cx.expose_global_argument_ptr()?;
self.js_arguments.push((name.clone(), "BigInt".to_string()));
self.prelude(&format!(
"\
{f}[0] = {name};\n\
const lo{i} = u32CvtShim[0];\n\
const hi{i} = u32CvtShim[1];\n\
"
{f}[0] = {name};
const low{i} = u32CvtShim[0];
const high{i} = u32CvtShim[1];
",
i = i,
f = f,
name = name,
));
self.rust_arguments.push(format!("lo{}", i));
self.rust_arguments.push(format!("hi{}", i));
self.rust_arguments.push(format!("low{}", i));
self.rust_arguments.push(format!("high{}", i));
return Ok(self);
}

Expand Down Expand Up @@ -292,7 +385,7 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
self.js_arguments.push((name.clone(), "string".to_string()));
self.rust_arguments.push(format!("{}.codePointAt(0)", name))
}
_ => bail!("unsupported argument to rust function {:?}", arg),
_ => bail!("unsupported argument type for calling Rust function from JS: {:?}", arg),
}
Ok(self)
}
Expand Down Expand Up @@ -348,7 +441,102 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
}

if optional {
bail!("unsupported optional argument to rust function {:?}", ty);
if ty.is_wasm_native() {
self.ret_ty = "number".to_string();
self.cx.expose_global_argument_ptr()?;
self.cx.expose_uint32_memory();
match ty {
Descriptor::I32 => self.cx.expose_int32_memory(),
Descriptor::U32 => (),
Descriptor::F32 => self.cx.expose_f32_memory(),
Descriptor::F64 => self.cx.expose_f64_memory(),
_ => (),
};
self.prelude("const retptr = globalArgumentPtr();");
self.rust_arguments.insert(0, "retptr".to_string());
self.ret_expr = format!(
"
RET;
const present = getUint32Memory()[retptr / 4];
const value = {mem}[retptr / {size} + 1];
return present === 0 ? undefined : value;
",
size = match ty {
Descriptor::I32 => 4,
Descriptor::U32 => 4,
Descriptor::F32 => 4,
Descriptor::F64 => 8,
_ => unreachable!(),
},
mem = match ty {
Descriptor::I32 => "getInt32Memory()",
Descriptor::U32 => "getUint32Memory()",
Descriptor::F32 => "getFloat32Memory()",
Descriptor::F64 => "getFloat64Memory()",
_ => unreachable!(),
}
);
return Ok(self);
}

if ty.is_abi_as_u32() {
self.ret_ty = "number".to_string();
self.ret_expr = "
const ret = RET;
return ret === 0xFFFFFF ? undefined : ret;
".to_string();
return Ok(self);
}

if let Some(signed) = ty.get_64() {
self.ret_ty = "BigInt".to_string();
self.cx.expose_global_argument_ptr()?;
let f = if signed {
self.cx.expose_int64_memory();
"getInt64Memory"
} else {
self.cx.expose_uint64_memory();
"getUint64Memory"
};
self.prelude("const retptr = globalArgumentPtr();");
self.rust_arguments.insert(0, "retptr".to_string());
self.ret_expr = format!(
"
RET;
const present = getUint32Memory()[retptr / 4];
const value = {}()[retptr / 8 + 1];
return present === 0 ? undefined : value;
",
f
);
return Ok(self);
}

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);
},
Descriptor::Char => {
self.ret_ty = "string".to_string();
self.cx.expose_global_argument_ptr()?;
self.cx.expose_uint32_memory();
self.prelude("const retptr = globalArgumentPtr();");
self.rust_arguments.insert(0, "retptr".to_string());
self.ret_expr = "
RET;
const present = getUint32Memory()[retptr / 4];
const value = getUint32Memory()[retptr / 4 + 1];
return present === 0 ? undefined : String.fromCodePoint(value);
".to_string();
return Ok(self);
},
_ => bail!("unsupported optional return type for calling Rust function from JS: {:?}", ty),
};
}

if ty.is_ref_anyref() {
Expand All @@ -374,7 +562,7 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
return Ok(self);
}

if let Some(signed) = ty.get_64bit() {
if let Some(signed) = ty.get_64() {
self.ret_ty = "BigInt".to_string();
self.cx.expose_global_argument_ptr()?;
let f = if signed {
Expand Down Expand Up @@ -405,7 +593,7 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
self.ret_ty = "string".to_string();
self.ret_expr = format!("return String.fromCodePoint(RET);")
}
_ => bail!("unsupported return from Rust to JS {:?}", ty),
_ => bail!("unsupported return type for calling Rust function from JS: {:?}", ty),
}
Ok(self)
}
Expand Down
Loading