Skip to content

Commit 564ce74

Browse files
authored
add JsValue::is_array() + tests (#3087)
* add JsValue::is_array() + tests * use Array.isArray() instead of instanceof
1 parent 2d1651f commit 564ce74

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

crates/cli-support/src/intrinsic.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ intrinsics! {
9595
#[symbol = "__wbindgen_is_function"]
9696
#[signature = fn(ref_externref()) -> Boolean]
9797
IsFunction,
98+
#[symbol = "__wbindgen_is_array"]
99+
#[signature = fn(ref_externref()) -> Boolean]
100+
IsArray,
98101
#[symbol = "__wbindgen_is_undefined"]
99102
#[signature = fn(ref_externref()) -> Boolean]
100103
IsUndefined,

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3141,6 +3141,11 @@ impl<'a> Context<'a> {
31413141
format!("typeof({}) === 'function'", args[0])
31423142
}
31433143

3144+
Intrinsic::IsArray => {
3145+
assert_eq!(args.len(), 1);
3146+
format!("Array.isArray({})", args[0])
3147+
}
3148+
31443149
Intrinsic::IsUndefined => {
31453150
assert_eq!(args.len(), 1);
31463151
format!("{} === undefined", args[0])

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,12 @@ impl JsValue {
348348
unsafe { __wbindgen_is_object(self.idx) == 1 }
349349
}
350350

351+
/// Tests whether this JS value is an instance of Array.
352+
#[inline]
353+
pub fn is_array(&self) -> bool {
354+
unsafe { __wbindgen_is_array(self.idx) == 1 }
355+
}
356+
351357
/// Tests whether the type of this JS value is `function`.
352358
#[inline]
353359
pub fn is_function(&self) -> bool {
@@ -1003,6 +1009,7 @@ externs! {
10031009
fn __wbindgen_is_undefined(idx: u32) -> u32;
10041010
fn __wbindgen_is_symbol(idx: u32) -> u32;
10051011
fn __wbindgen_is_object(idx: u32) -> u32;
1012+
fn __wbindgen_is_array(idx: u32) -> u32;
10061013
fn __wbindgen_is_function(idx: u32) -> u32;
10071014
fn __wbindgen_is_string(idx: u32) -> u32;
10081015
fn __wbindgen_is_bigint(idx: u32) -> u32;

tests/wasm/intrinsics.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::convert::TryFrom;
44
use std::fmt::Debug;
55

6-
use js_sys::{Object, RangeError, Reflect};
6+
use js_sys::{Array, Object, RangeError, Reflect};
77
use wasm_bindgen::{JsCast, JsValue};
88
use wasm_bindgen_test::wasm_bindgen_test;
99

@@ -78,6 +78,7 @@ fn types() {
7878
assert!(JsValue::UNDEFINED.is_undefined());
7979
assert!(JsValue::NULL.is_null());
8080
assert!(Object::new().is_object());
81+
assert!(Array::new().is_array());
8182
assert!(JsValue::symbol(None).is_symbol());
8283
assert!(JsValue::from_str("hi").is_string());
8384
assert!(JsValue::bigint_from_str("5").is_bigint());

0 commit comments

Comments
 (0)