mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-11-30 22:25:35 +03:00
Add support for optional bools
This commit is contained in:
parent
0ef528165f
commit
4a0c69ffed
@ -259,7 +259,26 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
||||
return Ok(self);
|
||||
}
|
||||
|
||||
bail!("unsupported optional argument type for calling Rust function from JS: {:?}", arg);
|
||||
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);
|
||||
},
|
||||
_ => bail!("unsupported optional argument type for calling Rust function from JS: {:?}", arg),
|
||||
};
|
||||
}
|
||||
|
||||
if let Some(s) = arg.rust_struct() {
|
||||
@ -486,7 +505,17 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
||||
return Ok(self);
|
||||
}
|
||||
|
||||
bail!("unsupported optional return type for calling Rust function from JS: {:?}", ty);
|
||||
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);
|
||||
},
|
||||
_ => bail!("unsupported optional return type for calling Rust function from JS: {:?}", ty),
|
||||
};
|
||||
}
|
||||
|
||||
if ty.is_ref_anyref() {
|
||||
|
@ -173,7 +173,13 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
bail!("unsupported optional argument type for calling JS function from Rust: {:?}", arg);
|
||||
match *arg {
|
||||
Descriptor::Boolean => {
|
||||
self.js_arguments.push(format!("{0} === 0xFFFFFF ? undefined : {0} !== 0", abi));
|
||||
return Ok(())
|
||||
},
|
||||
_ => bail!("unsupported optional argument type for calling JS function from Rust: {:?}", arg),
|
||||
};
|
||||
}
|
||||
|
||||
if let Some(signed) = arg.get_64() {
|
||||
@ -424,7 +430,17 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
bail!("unsupported optional return type for calling JS function from Rust: {:?}", ty);
|
||||
match *ty {
|
||||
Descriptor::Boolean => {
|
||||
self.cx.expose_is_like_none();
|
||||
self.ret_expr = "
|
||||
const val = JS;
|
||||
return isLikeNone(val) ? 0xFFFFFF : val ? 1 : 0;
|
||||
".to_string();
|
||||
return Ok(());
|
||||
},
|
||||
_ => bail!("unsupported optional return type for calling JS function from Rust: {:?}", ty),
|
||||
};
|
||||
}
|
||||
if ty.is_number() {
|
||||
self.ret_expr = "return JS;".to_string();
|
||||
|
@ -220,6 +220,16 @@ impl FromWasmAbi for bool {
|
||||
}
|
||||
}
|
||||
|
||||
impl OptionIntoWasmAbi for bool {
|
||||
#[inline]
|
||||
fn none() -> u32 { 0xFFFFFFu32 }
|
||||
}
|
||||
|
||||
impl OptionFromWasmAbi for bool {
|
||||
#[inline]
|
||||
fn is_none(js: &u32) -> bool { *js == 0xFFFFFFu32 }
|
||||
}
|
||||
|
||||
impl IntoWasmAbi for char {
|
||||
type Abi = u32;
|
||||
|
||||
|
@ -76,6 +76,10 @@ exports.test_works = function() {
|
||||
assert.strictEqual(wasm.u64_identity(wasm.u64_one()), BigInt('1'));
|
||||
assert.strictEqual(wasm.u64_identity(wasm.u64_max()), BigInt('18446744073709551615'));
|
||||
assert.strictEqual(wasm.u64_identity(wasm.u64_min()), BigInt('0'));
|
||||
|
||||
assert.strictEqual(wasm.bool_identity(wasm.bool_none()), undefined);
|
||||
assert.strictEqual(wasm.bool_identity(wasm.bool_false()), false);
|
||||
assert.strictEqual(wasm.bool_identity(wasm.bool_true()), true);
|
||||
};
|
||||
|
||||
exports.i32_js_identity = function(a) { return a; };
|
||||
@ -90,3 +94,4 @@ exports.i16_js_identity = function(a) { return a; };
|
||||
exports.u16_js_identity = function(a) { return a; };
|
||||
exports.i64_js_identity = function(a) { return a; };
|
||||
exports.u64_js_identity = function(a) { return a; };
|
||||
exports.bool_js_identity = function(a) { return a; };
|
||||
|
@ -15,6 +15,7 @@ extern {
|
||||
fn u16_js_identity(a: Option<u16>) -> Option<u16>;
|
||||
fn i64_js_identity(a: Option<i64>) -> Option<i64>;
|
||||
fn u64_js_identity(a: Option<u64>) -> Option<u64>;
|
||||
fn bool_js_identity(a: Option<bool>) -> Option<bool>;
|
||||
|
||||
fn test_works();
|
||||
}
|
||||
@ -181,6 +182,15 @@ pub fn u64_min() -> Option<u64> { Some(u64::min_value()) }
|
||||
#[wasm_bindgen]
|
||||
pub fn u64_identity(a: Option<u64>) -> Option<u64> { u64_js_identity(a) }
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn bool_none() -> Option<bool> { None }
|
||||
#[wasm_bindgen]
|
||||
pub fn bool_false() -> Option<bool> { Some(false) }
|
||||
#[wasm_bindgen]
|
||||
pub fn bool_true() -> Option<bool> { Some(true) }
|
||||
#[wasm_bindgen]
|
||||
pub fn bool_identity(a: Option<bool>) -> Option<bool> { bool_js_identity(a) }
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn works() {
|
||||
test_works();
|
||||
|
Loading…
Reference in New Issue
Block a user