Skip to content

Commit 72672ff

Browse files
authored
Merge pull request #1397 from RReverser/option-char-abi
Simplify ABI for Option<char>
2 parents 5ae6de5 + 5f742ca commit 72672ff

File tree

3 files changed

+13
-46
lines changed

3 files changed

+13
-46
lines changed

crates/cli-support/src/js/js2rust.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,8 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
343343
self.cx.expose_is_like_none();
344344
self.js_arguments
345345
.push((name.clone(), "string | undefined".to_string()));
346-
self.rust_arguments.push(format!("!isLikeNone({0})", name));
347346
self.rust_arguments
348-
.push(format!("isLikeNone({0}) ? 0 : {0}.codePointAt(0)", name));
347+
.push(format!("isLikeNone({0}) ? 0xFFFFFF : {0}.codePointAt(0)", name));
349348
return Ok(self);
350349
}
351350
Descriptor::Enum { hole } => {
@@ -633,17 +632,10 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
633632
}
634633
Descriptor::Char => {
635634
self.ret_ty = "string | undefined".to_string();
636-
self.cx.expose_global_argument_ptr()?;
637-
self.cx.expose_uint32_memory();
638-
self.prelude("const retptr = globalArgumentPtr();");
639-
self.rust_arguments.insert(0, "retptr".to_string());
640635
self.ret_expr = "
641-
RET;
642-
const present = getUint32Memory()[retptr / 4];
643-
const value = getUint32Memory()[retptr / 4 + 1];
644-
return present === 0 ? undefined : String.fromCodePoint(value);
645-
"
646-
.to_string();
636+
const ret = RET;
637+
return ret === 0xFFFFFF ? undefined : String.fromCodePoint(ret);
638+
".to_string();
647639
return Ok(self);
648640
}
649641
Descriptor::Enum { hole } => {

crates/cli-support/src/js/rust2js.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,8 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
215215
return Ok(());
216216
}
217217
Descriptor::Char => {
218-
let value = self.shim_argument();
219-
self.js_arguments.push(format!(
220-
"{present} === 0 ? undefined : String.fromCodePoint({value})",
221-
value = value,
222-
present = abi,
223-
));
218+
self.js_arguments
219+
.push(format!("{0} === 0xFFFFFF ? undefined : String.fromCodePoint({0})", abi));
224220
return Ok(());
225221
}
226222
Descriptor::RustStruct(ref class) => {
@@ -471,13 +467,9 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
471467
return Ok(());
472468
}
473469
Descriptor::Char => {
474-
self.cx.expose_is_like_none();
475-
self.cx.expose_uint32_memory();
476-
self.shim_arguments.insert(0, "ret".to_string());
477470
self.ret_expr = "
478471
const val = JS;
479-
getUint32Memory()[ret / 4] = !isLikeNone(val);
480-
getUint32Memory()[ret / 4 + 1] = isLikeNone(val) ? 0 : val.codePointAt(0);
472+
return isLikeNone(val) ? 0xFFFFFF : val.codePointAt(0);
481473
"
482474
.to_string();
483475
return Ok(());

src/convert/impls.rs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -258,34 +258,17 @@ impl FromWasmAbi for char {
258258
}
259259
}
260260

261-
impl IntoWasmAbi for Option<char> {
262-
type Abi = WasmOptionalU32;
263-
261+
impl OptionIntoWasmAbi for char {
264262
#[inline]
265-
fn into_abi(self, _extra: &mut Stack) -> WasmOptionalU32 {
266-
match self {
267-
None => WasmOptionalU32 {
268-
present: 0,
269-
value: 0,
270-
},
271-
Some(me) => WasmOptionalU32 {
272-
present: 1,
273-
value: me as u32,
274-
},
275-
}
263+
fn none() -> u32 {
264+
0xFFFFFFu32
276265
}
277266
}
278267

279-
impl FromWasmAbi for Option<char> {
280-
type Abi = WasmOptionalU32;
281-
268+
impl OptionFromWasmAbi for char {
282269
#[inline]
283-
unsafe fn from_abi(js: WasmOptionalU32, _extra: &mut Stack) -> Self {
284-
if js.present == 0 {
285-
None
286-
} else {
287-
Some(char::from_u32_unchecked(js.value))
288-
}
270+
fn is_none(js: &u32) -> bool {
271+
*js == 0xFFFFFFu32
289272
}
290273
}
291274

0 commit comments

Comments
 (0)