mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-18 07:21:24 +00:00
Add support for optional chars
This commit is contained in:
committed by
Alex Crichton
parent
4a0c69ffed
commit
afaf94a428
@ -277,6 +277,13 @@ 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".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),
|
||||
};
|
||||
}
|
||||
@ -514,6 +521,20 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
||||
".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),
|
||||
};
|
||||
}
|
||||
|
@ -178,6 +178,15 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
self.js_arguments.push(format!("{0} === 0xFFFFFF ? undefined : {0} !== 0", abi));
|
||||
return Ok(())
|
||||
},
|
||||
Descriptor::Char => {
|
||||
let value = self.shim_argument();
|
||||
self.js_arguments.push(format!(
|
||||
"{present} === 0 ? undefined : String.fromCodePoint({value})",
|
||||
value = value,
|
||||
present = abi,
|
||||
));
|
||||
return Ok(())
|
||||
},
|
||||
_ => bail!("unsupported optional argument type for calling JS function from Rust: {:?}", arg),
|
||||
};
|
||||
}
|
||||
@ -439,6 +448,17 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
".to_string();
|
||||
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);
|
||||
".to_string();
|
||||
return Ok(());
|
||||
},
|
||||
_ => bail!("unsupported optional return type for calling JS function from Rust: {:?}", ty),
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user