mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-11 11:15:33 +03:00
Fix TryFrom<JsValue> for f64
and JsValue::checked_div
(#3021)
* Fix `TryFrom<JsValue> for f64` and `JsValue::checked_div` Also added some tests for those and other obscure intrinsics. * fmt
This commit is contained in:
parent
434454da7a
commit
8e19dcfe53
@ -3200,7 +3200,14 @@ impl<'a> Context<'a> {
|
||||
|
||||
Intrinsic::TryIntoNumber => {
|
||||
assert_eq!(args.len(), 1);
|
||||
format!("try {{ +{} }} catch(e) {{ e }}", args[0])
|
||||
prelude.push_str("let result;\n");
|
||||
writeln!(
|
||||
prelude,
|
||||
"try {{ result = +{} }} catch (e) {{ result = e }}",
|
||||
args[0]
|
||||
)
|
||||
.unwrap();
|
||||
"result".to_owned()
|
||||
}
|
||||
|
||||
Intrinsic::Neg => {
|
||||
@ -3260,7 +3267,22 @@ impl<'a> Context<'a> {
|
||||
|
||||
Intrinsic::CheckedDiv => {
|
||||
assert_eq!(args.len(), 2);
|
||||
format!("try {{ {} / {} }} catch (e) {{ if (e instanceof RangeError) {{ e }} else {{ throw e }} }}", args[0], args[1])
|
||||
prelude.push_str("let result;\n");
|
||||
writeln!(
|
||||
prelude,
|
||||
"try {{
|
||||
result = {} / {};
|
||||
}} catch (e) {{
|
||||
if (e instanceof RangeError) {{
|
||||
result = e;
|
||||
}} else {{
|
||||
throw e;
|
||||
}}
|
||||
}}",
|
||||
args[0], args[1]
|
||||
)
|
||||
.unwrap();
|
||||
"result".to_owned()
|
||||
}
|
||||
|
||||
Intrinsic::Mul => {
|
||||
|
113
tests/wasm/intrinsics.rs
Normal file
113
tests/wasm/intrinsics.rs
Normal file
@ -0,0 +1,113 @@
|
||||
//! Tests that some of our more obscure intrinsics work properly.
|
||||
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt::Debug;
|
||||
|
||||
use js_sys::{Object, RangeError, Reflect};
|
||||
use wasm_bindgen::{JsCast, JsValue};
|
||||
use wasm_bindgen_test::wasm_bindgen_test;
|
||||
|
||||
// Shorter `JsValue::from(i32)`.
|
||||
fn int(x: i32) -> JsValue {
|
||||
JsValue::from(x)
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn bitwise() {
|
||||
assert_eq!(int(0b1110) & int(0b0111), 0b0110);
|
||||
assert_eq!(int(0b1000) | int(0b0010), 0b1010);
|
||||
assert_eq!(int(0b1110) ^ int(0b0100), 0b1010);
|
||||
assert_eq!(int(0x00ffffff).bit_not(), 0xff000000u32 as i32);
|
||||
|
||||
assert_eq!(int(0b0001) << int(2), 0b0100);
|
||||
assert_eq!(int(0b1000) >> int(2), 0b0010);
|
||||
assert_eq!(int(-0b1000) >> int(2), -0b0010);
|
||||
// Note that bit-wise, this is the same as the above `-0b1000`.
|
||||
assert_eq!(
|
||||
JsValue::from(0xfffffff8u32).unsigned_shr(&int(2)),
|
||||
0x3ffffffe
|
||||
);
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn arithmetic() {
|
||||
assert_eq!(-int(12), -12);
|
||||
assert_eq!(int(1) + int(2), 3);
|
||||
assert_eq!(int(1) - int(2), -1);
|
||||
assert_eq!(int(10) / int(5), 2);
|
||||
assert_eq!(int(42).checked_div(&int(6)), 7);
|
||||
// Note that this doesn't throw for regular numbers, since they just give
|
||||
// `NaN` for invalid results.
|
||||
assert!(JsValue::bigint_from_str("0")
|
||||
.checked_div(&JsValue::bigint_from_str("0"))
|
||||
.dyn_into::<RangeError>()
|
||||
.is_ok());
|
||||
assert_eq!(int(12) * int(34), 408);
|
||||
assert_eq!(int(17) % int(10), 7);
|
||||
assert_eq!(int(2).pow(&int(8)), 256)
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn cmp() {
|
||||
assert!(int(2).lt(&int(3)));
|
||||
assert!(!int(2).lt(&int(2)));
|
||||
|
||||
assert!(int(2).le(&int(3)));
|
||||
assert!(int(2).le(&int(2)));
|
||||
|
||||
assert!(int(3).ge(&int(2)));
|
||||
assert!(int(3).ge(&int(3)));
|
||||
|
||||
assert!(int(3).gt(&int(2)));
|
||||
assert!(!int(3).gt(&int(3)));
|
||||
|
||||
assert!(int(2) == int(2));
|
||||
assert!(int(2) != int(3));
|
||||
|
||||
assert!(int(2) != JsValue::from_str("2"));
|
||||
assert!(int(2).loose_eq(&JsValue::from_str("2")));
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn types() {
|
||||
assert!(
|
||||
Reflect::get(&js_sys::global(), &JsValue::from_str("Number"))
|
||||
.unwrap()
|
||||
.is_function()
|
||||
);
|
||||
assert!(JsValue::UNDEFINED.is_undefined());
|
||||
assert!(JsValue::NULL.is_null());
|
||||
assert!(Object::new().is_object());
|
||||
assert!(JsValue::symbol(None).is_symbol());
|
||||
assert!(JsValue::from_str("hi").is_string());
|
||||
assert!(JsValue::bigint_from_str("5").is_bigint());
|
||||
assert_eq!(int(5).js_typeof(), "number");
|
||||
assert_eq!(JsValue::bigint_from_str("5").js_typeof(), "bigint");
|
||||
assert_eq!(JsValue::NULL.js_typeof(), "object");
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn misc() {
|
||||
assert!(JsValue::from_str("Number").js_in(&js_sys::global()));
|
||||
assert!(!JsValue::from_str("frob").js_in(&js_sys::global()));
|
||||
|
||||
assert_eq!(int(5).unchecked_into_f64(), 5.0);
|
||||
assert_eq!(JsValue::from_str("5").unchecked_into_f64(), 5.0);
|
||||
|
||||
assert_eq!(f64::try_from(int(5)), Ok(5.0));
|
||||
assert_eq!(f64::try_from(JsValue::from_str("5")), Ok(5.0));
|
||||
assert!(f64::try_from(JsValue::from_str("hi")).unwrap().is_nan());
|
||||
assert!(f64::try_from(JsValue::symbol(None)).is_err());
|
||||
}
|
||||
|
||||
fn debug(x: impl Debug) -> String {
|
||||
format!("{x:?}")
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn debug_string() {
|
||||
assert_eq!(debug(int(5)), "JsValue(5)");
|
||||
assert_eq!(debug(JsValue::TRUE), "JsValue(true)");
|
||||
assert_eq!(debug(JsValue::symbol(None)), "JsValue(Symbol)");
|
||||
assert_eq!(debug(JsValue::from_str("hi")), "JsValue(\"hi\")");
|
||||
}
|
@ -27,6 +27,7 @@ pub mod futures;
|
||||
pub mod getters_and_setters;
|
||||
pub mod import_class;
|
||||
pub mod imports;
|
||||
pub mod intrinsics;
|
||||
pub mod js_keywords;
|
||||
pub mod js_objects;
|
||||
pub mod jscast;
|
||||
|
Loading…
Reference in New Issue
Block a user