Skip to content

Commit

Permalink
Merge pull request #1397 from RReverser/option-char-abi
Browse files Browse the repository at this point in the history
Simplify ABI for Option<char>
  • Loading branch information
alexcrichton committed Mar 26, 2019
2 parents 5ae6de5 + 5f742ca commit 72672ff
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 46 deletions.
16 changes: 4 additions & 12 deletions crates/cli-support/src/js/js2rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,8 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
self.cx.expose_is_like_none();
self.js_arguments
.push((name.clone(), "string | undefined".to_string()));
self.rust_arguments.push(format!("!isLikeNone({0})", name));
self.rust_arguments
.push(format!("isLikeNone({0}) ? 0 : {0}.codePointAt(0)", name));
.push(format!("isLikeNone({0}) ? 0xFFFFFF : {0}.codePointAt(0)", name));
return Ok(self);
}
Descriptor::Enum { hole } => {
Expand Down Expand Up @@ -633,17 +632,10 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
}
Descriptor::Char => {
self.ret_ty = "string | undefined".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();
const ret = RET;
return ret === 0xFFFFFF ? undefined : String.fromCodePoint(ret);
".to_string();
return Ok(self);
}
Descriptor::Enum { hole } => {
Expand Down
14 changes: 3 additions & 11 deletions crates/cli-support/src/js/rust2js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,8 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
return Ok(());
}
Descriptor::Char => {
let value = self.shim_argument();
self.js_arguments.push(format!(
"{present} === 0 ? undefined : String.fromCodePoint({value})",
value = value,
present = abi,
));
self.js_arguments
.push(format!("{0} === 0xFFFFFF ? undefined : String.fromCodePoint({0})", abi));
return Ok(());
}
Descriptor::RustStruct(ref class) => {
Expand Down Expand Up @@ -471,13 +467,9 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
return Ok(());
}
Descriptor::Char => {
self.cx.expose_is_like_none();
self.cx.expose_uint32_memory();
self.shim_arguments.insert(0, "ret".to_string());
self.ret_expr = "
const val = JS;
getUint32Memory()[ret / 4] = !isLikeNone(val);
getUint32Memory()[ret / 4 + 1] = isLikeNone(val) ? 0 : val.codePointAt(0);
return isLikeNone(val) ? 0xFFFFFF : val.codePointAt(0);
"
.to_string();
return Ok(());
Expand Down
29 changes: 6 additions & 23 deletions src/convert/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,34 +258,17 @@ impl FromWasmAbi for char {
}
}

impl IntoWasmAbi for Option<char> {
type Abi = WasmOptionalU32;

impl OptionIntoWasmAbi for char {
#[inline]
fn into_abi(self, _extra: &mut Stack) -> WasmOptionalU32 {
match self {
None => WasmOptionalU32 {
present: 0,
value: 0,
},
Some(me) => WasmOptionalU32 {
present: 1,
value: me as u32,
},
}
fn none() -> u32 {
0xFFFFFFu32
}
}

impl FromWasmAbi for Option<char> {
type Abi = WasmOptionalU32;

impl OptionFromWasmAbi for char {
#[inline]
unsafe fn from_abi(js: WasmOptionalU32, _extra: &mut Stack) -> Self {
if js.present == 0 {
None
} else {
Some(char::from_u32_unchecked(js.value))
}
fn is_none(js: &u32) -> bool {
*js == 0xFFFFFFu32
}
}

Expand Down

0 comments on commit 72672ff

Please sign in to comment.