Skip to content

Commit

Permalink
feat: eval array and regexp in binary expr (#5051)
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanjoi authored Dec 19, 2023
1 parent d017ca7 commit 0995f39
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ fn handle_strict_equality_comparison<'a>(
res.set_bool(eql == left.compare_compile_time_value(&right));
res.set_side_effects(left.could_have_side_effects() || right.could_have_side_effects());
Some(res)
} else if left.is_array() && right.is_array() {
res.set_bool(!eql);
res.set_side_effects(left.could_have_side_effects() || right.could_have_side_effects());
Some(res)
} else {
None
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ pub fn eval_lit_expr(expr: &Lit) -> Option<BasicEvaluatedExpression> {
res.set_string(str.value.to_string());
Some(res)
}
Lit::Regex(regexp) => {
let mut res = BasicEvaluatedExpression::new();
res.set_range(regexp.span().real_lo(), regexp.span_hi().0);
res.set_regexp(regexp.exp.to_string(), regexp.flags.to_string());
Some(res)
}
// TODO:
_ => None,
}
Expand Down
23 changes: 14 additions & 9 deletions crates/rspack_plugin_javascript/src/utils/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ enum Ty {
String,
Number,
Boolean,
// RegExp,
RegExp,
Conditional,
TypeArray,
ConstArray,
Expand All @@ -37,9 +37,11 @@ type Boolean = bool;
type Number = f64;
type Bigint = num_bigint::BigInt;
// type Array<'a> = &'a ast::ArrayLit;
// type Regexp<'a> = &'a ast::Regex;
type String = std::string::String;
type Regexp = (String, String);

// I really don't want there has many alloc, maybe this can be optimized after
// parse finished.
#[derive(Debug)]
pub struct BasicEvaluatedExpression {
ty: Ty,
Expand All @@ -52,7 +54,7 @@ pub struct BasicEvaluatedExpression {
number: Option<Number>,
string: Option<String>,
bigint: Option<Bigint>,
// regexp: Option<Regexp<'a>>,
regexp: Option<Regexp>,
items: Option<Vec<BasicEvaluatedExpression>>,
quasis: Option<Vec<BasicEvaluatedExpression>>,
parts: Option<Vec<BasicEvaluatedExpression>>,
Expand Down Expand Up @@ -86,7 +88,7 @@ impl BasicEvaluatedExpression {
options: None,
string: None,
items: None,
// regexp: None,
regexp: None,
}
}

Expand Down Expand Up @@ -124,7 +126,7 @@ impl BasicEvaluatedExpression {
| Ty::String
| Ty::Number
| Ty::Boolean
// | Ty::RegExp
| Ty::RegExp
| Ty::ConstArray
| Ty::BigInt
)
Expand Down Expand Up @@ -169,10 +171,7 @@ impl BasicEvaluatedExpression {
b.boolean.as_ref().expect("should not empty")
== self.boolean.as_ref().expect("should not empty")
}
// Ty::RegExp => std::ptr::eq(
// b.regexp.as_ref().expect("should not empty"),
// self.regexp.as_ref().expect("should not empty"),
// ),
Ty::RegExp => false, // FIXME: maybe we can use std::ptr::eq
// Ty::ConstArray => {
// },
Ty::BigInt => {
Expand Down Expand Up @@ -247,6 +246,12 @@ impl BasicEvaluatedExpression {
self.side_effects = false;
}

pub fn set_regexp(&mut self, regexp: String, flags: String) {
self.ty = Ty::RegExp;
self.regexp = Some((regexp, flags));
self.side_effects = false;
}

pub fn string(&self) -> &String {
self.string.as_ref().expect("make sure string exists")
}
Expand Down
4 changes: 3 additions & 1 deletion packages/rspack/tests/cases/parsing/issue-4816/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ it("should build success for logic op", () => {
).toBe(false);
expect([] != [] || require("fail")).toBe(true);
expect(null === 1 && require("fail")).toBe(false);
expect([] === [] && require("fail")).toBe(false);
expect(/a/ === /a/ && require("fail")).toBe(false);
// NEXT:
// expect([] === [] && require("fail")).toBe(false);
// expect(`hello${Math.random()}` === `world${Math.random()}` && require("fail")).toBe(false);
});

0 comments on commit 0995f39

Please sign in to comment.