Simplify ABI for Option<char>

Flatten None to a special u32 value instead of using an intermediate pointer.
This commit is contained in:
Ingvar Stepanyan 2019-03-26 18:12:24 +00:00
parent 11bb8f03c2
commit 5f742ca4c4
3 changed files with 13 additions and 46 deletions

View File

@ -343,9 +343,8 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
self.cx.expose_is_like_none(); self.cx.expose_is_like_none();
self.js_arguments self.js_arguments
.push((name.clone(), "string | undefined".to_string())); .push((name.clone(), "string | undefined".to_string()));
self.rust_arguments.push(format!("!isLikeNone({0})", name));
self.rust_arguments self.rust_arguments
.push(format!("isLikeNone({0}) ? 0 : {0}.codePointAt(0)", name)); .push(format!("isLikeNone({0}) ? 0xFFFFFF : {0}.codePointAt(0)", name));
return Ok(self); return Ok(self);
} }
Descriptor::Enum { hole } => { Descriptor::Enum { hole } => {
@ -632,17 +631,10 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
} }
Descriptor::Char => { Descriptor::Char => {
self.ret_ty = "string | undefined".to_string(); 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 = " self.ret_expr = "
RET; const ret = RET;
const present = getUint32Memory()[retptr / 4]; return ret === 0xFFFFFF ? undefined : String.fromCodePoint(ret);
const value = getUint32Memory()[retptr / 4 + 1]; ".to_string();
return present === 0 ? undefined : String.fromCodePoint(value);
"
.to_string();
return Ok(self); return Ok(self);
} }
Descriptor::Enum { hole } => { Descriptor::Enum { hole } => {

View File

@ -215,12 +215,8 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
return Ok(()); return Ok(());
} }
Descriptor::Char => { Descriptor::Char => {
let value = self.shim_argument(); self.js_arguments
self.js_arguments.push(format!( .push(format!("{0} === 0xFFFFFF ? undefined : String.fromCodePoint({0})", abi));
"{present} === 0 ? undefined : String.fromCodePoint({value})",
value = value,
present = abi,
));
return Ok(()); return Ok(());
} }
Descriptor::RustStruct(ref class) => { Descriptor::RustStruct(ref class) => {
@ -468,13 +464,9 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
return Ok(()); return Ok(());
} }
Descriptor::Char => { Descriptor::Char => {
self.cx.expose_is_like_none();
self.cx.expose_uint32_memory();
self.shim_arguments.insert(0, "ret".to_string());
self.ret_expr = " self.ret_expr = "
const val = JS; const val = JS;
getUint32Memory()[ret / 4] = !isLikeNone(val); return isLikeNone(val) ? 0xFFFFFF : val.codePointAt(0);
getUint32Memory()[ret / 4 + 1] = isLikeNone(val) ? 0 : val.codePointAt(0);
" "
.to_string(); .to_string();
return Ok(()); return Ok(());

View File

@ -258,34 +258,17 @@ impl FromWasmAbi for char {
} }
} }
impl IntoWasmAbi for Option<char> { impl OptionIntoWasmAbi for char {
type Abi = WasmOptionalU32;
#[inline] #[inline]
fn into_abi(self, _extra: &mut Stack) -> WasmOptionalU32 { fn none() -> u32 {
match self { 0xFFFFFFu32
None => WasmOptionalU32 {
present: 0,
value: 0,
},
Some(me) => WasmOptionalU32 {
present: 1,
value: me as u32,
},
}
} }
} }
impl FromWasmAbi for Option<char> { impl OptionFromWasmAbi for char {
type Abi = WasmOptionalU32;
#[inline] #[inline]
unsafe fn from_abi(js: WasmOptionalU32, _extra: &mut Stack) -> Self { fn is_none(js: &u32) -> bool {
if js.present == 0 { *js == 0xFFFFFFu32
None
} else {
Some(char::from_u32_unchecked(js.value))
}
} }
} }