Skip to content

Commit c4776be

Browse files
committed
Touch up descriptions of has_type
1 parent 657b97b commit c4776be

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

crates/backend/src/codegen.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ impl ToTokens for ast::ImportType {
589589
};
590590

591591
let is_type_of = self.is_type_of.as_ref().map(|is_type_of| quote! {
592+
#[inline]
592593
fn is_type_of(val: &JsValue) -> bool {
593594
let is_type_of: fn(&JsValue) -> bool = #is_type_of;
594595
is_type_of(val)

src/cast.rs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,17 @@ pub trait JsCast
1616
where
1717
Self: AsRef<JsValue> + Into<JsValue>,
1818
{
19-
/// Test whether this JS value is an instance of the type `T`.
20-
///
21-
/// This method performs a dynamic check (at runtime) using the JS
22-
/// `instanceof` operator. This method returns `self instanceof T`.
23-
///
24-
/// Note that `instanceof` does not work with primitive values or across
25-
/// different realms (e.g. iframes). Prefer using `has_type` instead.
26-
fn is_instance_of<T>(&self) -> bool
27-
where
28-
T: JsCast,
29-
{
30-
T::instanceof(self.as_ref())
31-
}
32-
3319
/// Test whether this JS value has a type `T`.
3420
///
35-
/// Unlike `is_instance_of`, the type can override this to a specialised
36-
/// check that works reliably with primitives and across realms.
21+
/// This method will dynamically check to see if this JS object can be
22+
/// casted to the JS object of type `T`. Usually this uses the `instanceof`
23+
/// operator. This also works with primitive types like
24+
/// booleans/strings/numbers as well as cross-realm object like `Array`
25+
/// which can originate from other iframes.
26+
///
27+
/// In general this is intended to be a more robust version of
28+
/// `is_instance_of`, but if you want strictly the `instanceof` operator
29+
/// it's recommended to use that instead.
3730
fn has_type<T>(&self) -> bool
3831
where
3932
T: JsCast,
@@ -46,7 +39,7 @@ where
4639
///
4740
/// This method will return `Err(self)` if `self.has_type::<T>()`
4841
/// returns `false`, and otherwise it will return `Ok(T)` manufactured with
49-
/// an unchecked cast (verified correct via the `instanceof` operation).
42+
/// an unchecked cast (verified correct via the `has_type` operation).
5043
fn dyn_into<T>(self) -> Result<T, Self>
5144
where
5245
T: JsCast,
@@ -63,7 +56,7 @@ where
6356
///
6457
/// This method will return `None` if `self.has_type::<T>()`
6558
/// returns `false`, and otherwise it will return `Some(&T)` manufactured
66-
/// with an unchecked cast (verified correct via the `instanceof` operation).
59+
/// with an unchecked cast (verified correct via the `has_type` operation).
6760
fn dyn_ref<T>(&self) -> Option<&T>
6861
where
6962
T: JsCast,
@@ -107,11 +100,28 @@ where
107100
T::unchecked_from_js_ref(self.as_ref())
108101
}
109102

103+
/// Test whether this JS value is an instance of the type `T`.
104+
///
105+
/// This method performs a dynamic check (at runtime) using the JS
106+
/// `instanceof` operator. This method returns `self instanceof T`.
107+
///
108+
/// Note that `instanceof` does not always work with primitive values or
109+
/// across different realms (e.g. iframes). If you're not sure whether you
110+
/// specifically need only `instanceof` it's recommended to use `has_type`
111+
/// instead.
112+
fn is_instance_of<T>(&self) -> bool
113+
where
114+
T: JsCast,
115+
{
116+
T::instanceof(self.as_ref())
117+
}
118+
110119
/// Performs a dynamic `instanceof` check to see whether the `JsValue`
111120
/// provided is an instance of this type.
112121
///
113122
/// This is intended to be an internal implementation detail, you likely
114-
/// won't need to call this.
123+
/// won't need to call this. It's generally called through the
124+
/// `is_instance_of` method instead.
115125
fn instanceof(val: &JsValue) -> bool;
116126

117127
/// Performs a dynamic check to see whether the `JsValue` provided

0 commit comments

Comments
 (0)