From e61f691e0bdb618bd95a5f902c6b45168b31951d Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 2 Jul 2019 18:44:06 +0200 Subject: [PATCH 1/3] Add is_truthy, is_falsy --- crates/cli-support/src/intrinsic.rs | 6 ++++++ crates/cli-support/src/js/mod.rs | 10 ++++++++++ src/lib.rs | 18 ++++++++++++++++++ tests/wasm/truthy_falsy.rs | 25 +++++++++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 tests/wasm/truthy_falsy.rs diff --git a/crates/cli-support/src/intrinsic.rs b/crates/cli-support/src/intrinsic.rs index 33d74e86..ca17bb40 100644 --- a/crates/cli-support/src/intrinsic.rs +++ b/crates/cli-support/src/intrinsic.rs @@ -85,6 +85,12 @@ intrinsics! { #[symbol = "__wbindgen_is_string"] #[signature = fn(ref_anyref()) -> Boolean] IsString, + #[symbol = "__wbindgen_is_truthy"] + #[signature = fn(ref_anyref()) -> Boolean] + IsTruthy, + #[symbol = "__wbindgen_is_falsy"] + #[signature = fn(ref_anyref()) -> Boolean] + IsFalsy, #[symbol = "__wbindgen_object_clone_ref"] #[signature = fn(ref_anyref()) -> Anyref] ObjectCloneRef, diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 1307ad8a..82cf2261 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -2299,6 +2299,16 @@ impl<'a> Context<'a> { format!("typeof({}) === 'string'", args[0]) } + Intrinsic::IsTruthy => { + assert_eq!(args.len(), 1); + format!("!!{}", args[0]) + } + + Intrinsic::IsFalsy => { + assert_eq!(args.len(), 1); + format!("!{}", args[0]) + } + Intrinsic::ObjectCloneRef => { assert_eq!(args.len(), 1); args[0].clone() diff --git a/src/lib.rs b/src/lib.rs index 487652f9..734a171f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -332,6 +332,22 @@ impl JsValue { unsafe { __wbindgen_is_function(self.idx) == 1 } } + /// Tests whether the value is ["truthy"]. + /// + /// ["truthy"]: https://developer.mozilla.org/en-US/docs/Glossary/Truthy + #[inline] + pub fn is_truthy(&self) -> bool { + unsafe { __wbindgen_is_truthy(self.idx) == 1 } + } + + /// Tests whether the value is ["falsy"]. + /// + /// ["falsy"]: https://developer.mozilla.org/en-US/docs/Glossary/Falsy + #[inline] + pub fn is_falsy(&self) -> bool { + unsafe { __wbindgen_is_falsy(self.idx) == 1 } + } + /// Get a string representation of the JavaScript object for debugging #[cfg(feature = "std")] fn as_debug_string(&self) -> String { @@ -502,6 +518,8 @@ externs! { fn __wbindgen_is_object(idx: u32) -> u32; fn __wbindgen_is_function(idx: u32) -> u32; fn __wbindgen_is_string(idx: u32) -> u32; + fn __wbindgen_is_truthy(idx: u32) -> u32; + fn __wbindgen_is_falsy(idx: u32) -> u32; fn __wbindgen_number_get(idx: u32, invalid: *mut u8) -> f64; fn __wbindgen_boolean_get(idx: u32) -> u32; diff --git a/tests/wasm/truthy_falsy.rs b/tests/wasm/truthy_falsy.rs new file mode 100644 index 00000000..6166be8a --- /dev/null +++ b/tests/wasm/truthy_falsy.rs @@ -0,0 +1,25 @@ +#[wasm_bindgen_test] +fn test_is_truthy() { + assert_eq!(JsValue::from(0).is_truthy(), false); + assert_eq!(JsValue::from("".to_string()).is_truthy(), false); + assert_eq!(JsValue::from(false).is_truthy(), false); + assert_eq!(JsValue::NULL.is_truthy(), false); + assert_eq!(JsValue::UNDEFINED.is_truthy(), false); + + assert_eq!(JsValue::from(10).is_truthy(), true); + assert_eq!(JsValue::from("null".to_string()).is_truthy(), true); + assert_eq!(JsValue::from(true).is_truthy(), true); +} + +#[wasm_bindgen_test] +fn test_is_falsy() { + assert_eq!(JsValue::from(0).is_falsy(), true); + assert_eq!(JsValue::from("".to_string()).is_falsy(), true); + assert_eq!(JsValue::from(false).is_falsy(), true); + assert_eq!(JsValue::NULL.is_falsy(), true); + assert_eq!(JsValue::UNDEFINED.is_falsy(), true); + + assert_eq!(JsValue::from(10).is_falsy(), false); + assert_eq!(JsValue::from("null".to_string()).is_falsy(), false); + assert_eq!(JsValue::from(true).is_falsy(), false); +} From 0ee72366987ee4bcbc92c475c36afb779f93c377 Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 2 Jul 2019 19:06:44 +0200 Subject: [PATCH 2/3] Define is_truthy in terms of is_falsy --- crates/cli-support/src/intrinsic.rs | 3 --- crates/cli-support/src/js/mod.rs | 5 ----- src/lib.rs | 7 +++---- 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/crates/cli-support/src/intrinsic.rs b/crates/cli-support/src/intrinsic.rs index ca17bb40..a82f711f 100644 --- a/crates/cli-support/src/intrinsic.rs +++ b/crates/cli-support/src/intrinsic.rs @@ -85,9 +85,6 @@ intrinsics! { #[symbol = "__wbindgen_is_string"] #[signature = fn(ref_anyref()) -> Boolean] IsString, - #[symbol = "__wbindgen_is_truthy"] - #[signature = fn(ref_anyref()) -> Boolean] - IsTruthy, #[symbol = "__wbindgen_is_falsy"] #[signature = fn(ref_anyref()) -> Boolean] IsFalsy, diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 82cf2261..55e85a9e 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -2299,11 +2299,6 @@ impl<'a> Context<'a> { format!("typeof({}) === 'string'", args[0]) } - Intrinsic::IsTruthy => { - assert_eq!(args.len(), 1); - format!("!!{}", args[0]) - } - Intrinsic::IsFalsy => { assert_eq!(args.len(), 1); format!("!{}", args[0]) diff --git a/src/lib.rs b/src/lib.rs index 734a171f..9b377d56 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -333,15 +333,15 @@ impl JsValue { } /// Tests whether the value is ["truthy"]. - /// + /// /// ["truthy"]: https://developer.mozilla.org/en-US/docs/Glossary/Truthy #[inline] pub fn is_truthy(&self) -> bool { - unsafe { __wbindgen_is_truthy(self.idx) == 1 } + !self.is_falsy() } /// Tests whether the value is ["falsy"]. - /// + /// /// ["falsy"]: https://developer.mozilla.org/en-US/docs/Glossary/Falsy #[inline] pub fn is_falsy(&self) -> bool { @@ -518,7 +518,6 @@ externs! { fn __wbindgen_is_object(idx: u32) -> u32; fn __wbindgen_is_function(idx: u32) -> u32; fn __wbindgen_is_string(idx: u32) -> u32; - fn __wbindgen_is_truthy(idx: u32) -> u32; fn __wbindgen_is_falsy(idx: u32) -> u32; fn __wbindgen_number_get(idx: u32, invalid: *mut u8) -> f64; From 8b99fdc7454cc1d77ebcbf220818e44a12d62489 Mon Sep 17 00:00:00 2001 From: Thomas den Hollander Date: Thu, 8 Aug 2019 09:17:38 +0200 Subject: [PATCH 3/3] Add truthy_falsy tests to main.rs --- tests/wasm/main.rs | 1 + tests/wasm/truthy_falsy.rs | 3 +++ 2 files changed, 4 insertions(+) diff --git a/tests/wasm/main.rs b/tests/wasm/main.rs index 65f47f86..84d5f09a 100644 --- a/tests/wasm/main.rs +++ b/tests/wasm/main.rs @@ -36,6 +36,7 @@ pub mod rethrow; pub mod simple; pub mod slice; pub mod structural; +pub mod truthy_falsy; pub mod u64; pub mod validate_prt; pub mod variadic; diff --git a/tests/wasm/truthy_falsy.rs b/tests/wasm/truthy_falsy.rs index 6166be8a..d04abb05 100644 --- a/tests/wasm/truthy_falsy.rs +++ b/tests/wasm/truthy_falsy.rs @@ -1,3 +1,6 @@ +use wasm_bindgen::prelude::*; +use wasm_bindgen_test::*; + #[wasm_bindgen_test] fn test_is_truthy() { assert_eq!(JsValue::from(0).is_truthy(), false);