Skip to content

Commit 9cb7d7f

Browse files
authored
perf: regex from js and js resolver api (#10537)
1 parent abd1552 commit 9cb7d7f

File tree

2 files changed

+17
-26
lines changed
  • crates
    • node_binding/src/raw_options/raw_module
    • rspack_regex/src

2 files changed

+17
-26
lines changed

crates/node_binding/src/raw_options/raw_module/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,7 @@ impl TryFrom<RawRuleSetCondition> for rspack_core::RuleSetCondition {
101101
fn try_from(x: RawRuleSetCondition) -> rspack_error::Result<Self> {
102102
let result = match x {
103103
RawRuleSetCondition::string(s) => Self::String(s),
104-
RawRuleSetCondition::regexp(r) => {
105-
let reg = RspackRegex::with_flags(&r.source, &r.flags)?;
106-
Self::Regexp(reg)
107-
}
104+
RawRuleSetCondition::regexp(r) => Self::Regexp(r),
108105
RawRuleSetCondition::logical(mut l) => {
109106
let l = l.get_mut(0).ok_or_else(|| {
110107
error!("TODO: use Box after https://github.com/napi-rs/napi-rs/issues/1500 landed")

crates/rspack_regex/src/napi.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use napi::{
22
bindgen_prelude::{
3-
FromNapiValue, Function, JsObjectValue, Object, ToNapiValue, TypeName, Undefined,
4-
ValidateNapiValue,
3+
FromNapiValue, Function, JsObjectValue, ToNapiValue, TypeName, ValidateNapiValue,
54
},
6-
Env, JsValue, Unknown,
5+
Env, JsValue, Unknown, ValueType,
76
};
87

98
use crate::RspackRegex;
@@ -25,32 +24,27 @@ impl FromNapiValue for RspackRegex {
2524
raw_env: napi::sys::napi_env,
2625
napi_val: napi::sys::napi_value,
2726
) -> napi::Result<Self> {
28-
let js_object = Object::from_raw(raw_env, napi_val);
27+
let unknown = Unknown::from_raw_unchecked(raw_env, napi_val);
2928

30-
let env = Env::from(raw_env);
31-
let global = env.get_global()?;
32-
let object_prototype_to_string = global
33-
.get_named_property_unchecked::<Object>("Object")?
34-
.get_named_property_unchecked::<Object>("prototype")?
35-
.get_named_property_unchecked::<Function>("toString")?;
36-
37-
let raw_undefined = Undefined::to_napi_value(raw_env, ())?;
38-
let undefined = Unknown::from_napi_value(raw_env, raw_undefined)?;
39-
let js_value = object_prototype_to_string.apply(js_object, undefined)?;
40-
let js_string = js_value.coerce_to_string()?;
41-
let js_utf8_string = js_string.into_utf8()?;
42-
let object_type = js_utf8_string.as_str()?;
43-
44-
if object_type == "[object RegExp]" {
45-
let source = js_object.get_named_property::<String>("source")?;
46-
let flags = js_object.get_named_property::<String>("flags")?;
29+
if unknown.get_type()? == ValueType::Object {
30+
let object = unknown.coerce_to_object()?;
31+
let source = object.get_named_property::<String>("source").map_err(|_| {
32+
napi::Error::from_reason(
33+
"Failed to extract the 'source' property. Ensure the value is a valid RegExp object.",
34+
)
35+
})?;
36+
let flags = object.get_named_property::<String>("flags").map_err(|_| {
37+
napi::Error::from_reason(
38+
"Failed to extract the 'flags'. Ensure the value is a valid RegExp object.",
39+
)
40+
})?;
4741

4842
Self::with_flags(&source, &flags)
4943
.map_err(|err| napi::Error::new(napi::Status::InvalidArg, err.to_string()))
5044
} else {
5145
Err(napi::Error::new(
5246
napi::Status::ObjectExpected,
53-
format!("Expect value to be '[object RegExp]', but received {object_type}"),
47+
"Expected a RegExp object as input value.",
5448
))
5549
}
5650
}

0 commit comments

Comments
 (0)