|
1 | 1 | use std::fmt::Debug; |
2 | 2 |
|
3 | 3 | use napi::{ |
4 | | - bindgen_prelude::{FromNapiValue, Function, TypeName, ValidateNapiValue}, |
5 | | - sys, Env, Error, JsObject, JsUnknown, NapiRaw, NapiValue, Result, Status, |
| 4 | + bindgen_prelude::{FromNapiValue, TypeName, ValidateNapiValue}, |
| 5 | + sys, Env, Error, JsFunction, JsObject, NapiRaw, NapiValue, Result, Status, |
6 | 6 | }; |
7 | 7 |
|
8 | 8 | pub struct JsRegExp(JsObject); |
@@ -53,16 +53,25 @@ impl FromNapiValue for JsRegExp { |
53 | 53 |
|
54 | 54 | let env = Env::from(env); |
55 | 55 | let global = env.get_global()?; |
56 | | - let regexp_constructor = global.get_named_property::<Function<JsUnknown, ()>>("RegExp")?; |
| 56 | + let object_prototype_to_string = global |
| 57 | + .get_named_property_unchecked::<JsObject>("Object")? |
| 58 | + .get_named_property::<JsObject>("prototype")? |
| 59 | + .get_named_property::<JsFunction>("toString")?; |
57 | 60 |
|
58 | | - if js_object.instanceof(regexp_constructor)? { |
| 61 | + let js_string = object_prototype_to_string |
| 62 | + .call_without_args(Some(&js_object))? |
| 63 | + .coerce_to_string()? |
| 64 | + .into_utf8()?; |
| 65 | + let js_object_type = js_string.as_str()?; |
| 66 | + |
| 67 | + if js_object_type == "[object RegExp]" { |
59 | 68 | Ok(Self(js_object)) |
60 | 69 | } else { |
61 | 70 | Err(Error::new( |
62 | 71 | Status::ObjectExpected, |
63 | 72 | format!( |
64 | 73 | "Expect value to be '[object RegExp]', but received {}", |
65 | | - js_object.coerce_to_string()?.into_utf8()?.as_str()? |
| 74 | + js_object_type |
66 | 75 | ), |
67 | 76 | )) |
68 | 77 | } |
|
0 commit comments