Skip to content

Truly independent web browser

LibJS: Keep sticky patterns off the RegExp @@split fast path

Shannon Booth14c789fdb2b3
diff --git a/Libraries/LibJS/Rust/src/runtime/regexp_prototype.rs b/Libraries/LibJS/Rust/src/runtime/regexp_prototype.rs
index 5ba838dfcab..5d48b2e39f3 100644
--- a/Libraries/LibJS/Rust/src/runtime/regexp_prototype.rs
+++ b/Libraries/LibJS/Rust/src/runtime/regexp_prototype.rs
@@ -2091,7 +2091,7 @@ impl RegExpPrototype {
/// The fast path of symbol_split_impl(): split with an unmodified RegExp, which bypasses the spec's
/// SpeciesConstructor/Construct overhead and calls the regex directly with explicit start positions. Returns None
- /// when the regex cannot be compiled, in which case the generic path runs.
+ /// when the RegExp is sticky or the regex cannot be compiled, in which case the generic path runs.
fn symbol_split_fast_path(
vm: &Vm,
realm: Gc<Realm>,
@@ -2099,11 +2099,15 @@ impl RegExpPrototype {
string: Gc<PrimitiveString>,
limit_value: Value,
) -> ThrowCompletionOr<Option<Value>> {
+ // The compiled regex of a sticky pattern only matches at the position it is run from.
+ let flag_bits = typed_regexp.flag_bits();
+ if flag_bits.has(RegExpFlags::STICKY) {
+ return Ok(None);
+ }
let Some(compiled_regex) = get_or_compile_regex(&typed_regexp) else {
return Ok(None);
};
- let flag_bits = typed_regexp.flag_bits();
let is_unicode = flag_bits.has(RegExpFlags::UNICODE);
let is_unicode_sets = flag_bits.has(RegExpFlags::UNICODE_SETS);
let unicode_matching = is_unicode || is_unicode_sets;
diff --git a/Tests/LibJS/Runtime/builtins/RegExp/RegExp.prototype.@@split.js b/Tests/LibJS/Runtime/builtins/RegExp/RegExp.prototype.@@split.js
index 5e479970de7..b1e5493e6f4 100644
--- a/Tests/LibJS/Runtime/builtins/RegExp/RegExp.prototype.@@split.js
+++ b/Tests/LibJS/Runtime/builtins/RegExp/RegExp.prototype.@@split.js
@@ -12,6 +12,14 @@ describe("basic functionality", () => {
expect(JSON.stringify(result)).toBe('["",""]');
});
+ test("sticky patterns split like non-sticky ones", () => {
+ expect("abc".split(/b/y)).toEqual(["a", "c"]);
+ expect("bab".split(/b/y)).toEqual(["", "a", ""]);
+ expect("abab".split(/b/gy)).toEqual(["a", "a", ""]);
+ expect("abc".split(/(b)/y)).toEqual(["a", "b", "c"]);
+ expect("abc".split(/b/y, 1)).toEqual(["a"]);
+ });
+
test("observes an inherited species constructor", () => {
const originalSpecies = Object.getOwnPropertyDescriptor(RegExp, Symbol.species);
try {