Skip to content

Truly independent web browser

LibJS: Report the last capture group in RegExp.lastParen

Shannon Booth9637896cd08a
diff --git a/Libraries/LibJS/Rust/src/runtime/regexp_legacy_static_properties.rs b/Libraries/LibJS/Rust/src/runtime/regexp_legacy_static_properties.rs
index 1349c98d78f..f83ba953a46 100644
--- a/Libraries/LibJS/Rust/src/runtime/regexp_legacy_static_properties.rs
+++ b/Libraries/LibJS/Rust/src/runtime/regexp_legacy_static_properties.rs
@@ -20,6 +20,34 @@ use crate::runtime::value::same_value;
const PAREN_COUNT: usize = 9;
+/// The captured ranges of a match that the legacy static properties record: those of $1 through $9, and that of the
+/// last capture group, which can be past $9.
+pub struct LegacyCaptureSlots {
+ starts: [i32; PAREN_COUNT],
+ ends: [i32; PAREN_COUNT],
+ last_paren: (i32, i32),
+}
+
+impl LegacyCaptureSlots {
+ /// `slot(2 * group)` and `slot(2 * group + 1)` are the start and end of each of the `group_count` capture groups, or
+ /// -1 for a group that did not participate.
+ pub fn new(group_count: usize, slot: impl Fn(usize) -> i32) -> Self {
+ let mut captures = Self {
+ starts: [-1; PAREN_COUNT],
+ ends: [-1; PAREN_COUNT],
+ last_paren: (-1, -1),
+ };
+ for index in 0..group_count.min(PAREN_COUNT) {
+ captures.starts[index] = slot((index + 1) * 2);
+ captures.ends[index] = slot((index + 1) * 2 + 1);
+ }
+ if group_count > 0 {
+ captures.last_paren = (slot(group_count * 2), slot(group_count * 2 + 1));
+ }
+ captures
+ }
+}
+
// https://github.com/tc39/proposal-regexp-legacy-features#regexp
// The %RegExp% intrinsic object, which is the builtin RegExp constructor, has the following additional internal slots:
// [[RegExpInput]]
@@ -256,16 +284,10 @@ impl RegExpLegacyStaticProperties {
// Lazy update: store capture indices without materializing strings.
// Strings are created on demand when $1-$9 are accessed.
- pub fn set_captures_lazy(&self, vm: &Vm, capture_starts: &[i32], capture_ends: &[i32]) {
- let num_captures = capture_starts.len();
+ pub fn set_captures_lazy(&self, vm: &Vm, captures: &LegacyCaptureSlots) {
for index in 0..PAREN_COUNT {
- if index < num_captures {
- self.paren_starts[index].set(capture_starts[index]);
- self.paren_ends[index].set(capture_ends[index]);
- } else {
- self.paren_starts[index].set(-1);
- self.paren_ends[index].set(-1);
- }
+ self.paren_starts[index].set(captures.starts[index]);
+ self.paren_ends[index].set(captures.ends[index]);
}
// Clear any previously materialized strings.
for paren in &self.parens {
@@ -274,10 +296,11 @@ impl RegExpLegacyStaticProperties {
self.parens_materialized.set(false);
// Set last_paren to the last captured value.
- if num_captures > 0 && capture_starts[num_captures - 1] >= 0 && capture_ends[num_captures - 1] >= 0 {
+ let (last_paren_start, last_paren_end) = captures.last_paren;
+ if last_paren_start >= 0 && last_paren_end >= 0 {
self.last_paren.set(None);
- self.last_paren_start.set(capture_starts[num_captures - 1]);
- self.last_paren_end.set(capture_ends[num_captures - 1]);
+ self.last_paren_start.set(last_paren_start);
+ self.last_paren_end.set(last_paren_end);
} else {
self.last_paren.set(Some(self.empty_string(vm)));
self.last_paren_start.set(-1);
@@ -377,8 +400,7 @@ pub fn update_legacy_regexp_static_properties_lazy(
string: Gc<PrimitiveString>,
start_index: usize,
end_index: usize,
- capture_starts: &[i32],
- capture_ends: &[i32],
+ captures: &LegacyCaptureSlots,
) {
let legacy_static_properties = constructor.legacy_static_properties();
@@ -395,7 +417,7 @@ pub fn update_legacy_regexp_static_properties_lazy(
legacy_static_properties.set_right_context(end_index, length - end_index);
- legacy_static_properties.set_captures_lazy(vm, capture_starts, capture_ends);
+ legacy_static_properties.set_captures_lazy(vm, captures);
}
// InvalidateLegacyRegExpStaticProperties ( C ), https://github.com/tc39/proposal-regexp-legacy-features#invalidatelegacyregexpstaticproperties--c
diff --git a/Libraries/LibJS/Rust/src/runtime/regexp_prototype.rs b/Libraries/LibJS/Rust/src/runtime/regexp_prototype.rs
index 5d48b2e39f3..952bff2bb6a 100644
--- a/Libraries/LibJS/Rust/src/runtime/regexp_prototype.rs
+++ b/Libraries/LibJS/Rust/src/runtime/regexp_prototype.rs
@@ -42,7 +42,7 @@ use crate::runtime::realm::Realm;
use crate::runtime::realm_watchpoints;
use crate::runtime::regexp_constructor::is_raw_native_function_running;
use crate::runtime::regexp_legacy_static_properties::{
- invalidate_legacy_regexp_static_properties, update_legacy_regexp_static_properties_lazy,
+ LegacyCaptureSlots, invalidate_legacy_regexp_static_properties, update_legacy_regexp_static_properties_lazy,
};
#[cfg(feature = "jit")]
use crate::runtime::regexp_object::NEW_REGEXP_OBJECT_LAST_INDEX_OFFSET;
@@ -52,11 +52,9 @@ use crate::runtime::regexp_object::{
use crate::runtime::regexp_string_iterator::RegExpStringIterator;
use crate::runtime::string_prototype::code_point_at;
use crate::runtime::value::same_value;
+use crate::u32_from_usize;
use crate::utf16::{Utf16StringBuilder, Utf16View, concatenate};
-/// The most legacy static properties ($1-$9) a match updates.
-const MAX_LEGACY_CAPTURES: u32 = 9;
-
fn contains_code_unit(string: &Utf16String, code_unit: u8) -> bool {
Utf16View::of_string(string)
.code_units()
@@ -343,41 +341,16 @@ fn exec_with_unicode_last_index_retry(
}
}
-/// The start and end of each of the first nine captures of the last exec of `compiled_regex`, which the legacy
-/// static properties are updated with.
-/// The capture slots that the legacy static properties record for $1 through $9, kept inline.
-struct LegacyCaptureSlots {
- starts: [i32; MAX_LEGACY_CAPTURES as usize],
- ends: [i32; MAX_LEGACY_CAPTURES as usize],
- count: usize,
-}
-
-impl LegacyCaptureSlots {
- fn starts(&self) -> &[i32] {
- &self.starts[..self.count]
- }
-
- fn ends(&self) -> &[i32] {
- &self.ends[..self.count]
- }
-}
-
fn legacy_capture_slots(compiled_regex: &EcmaScriptRegex, n_capture_groups: u32) -> LegacyCaptureSlots {
let total_groups = compiled_regex.total_groups();
- let capture_count = MAX_LEGACY_CAPTURES.min(n_capture_groups);
- let mut slots = LegacyCaptureSlots {
- starts: [-1; MAX_LEGACY_CAPTURES as usize],
- ends: [-1; MAX_LEGACY_CAPTURES as usize],
- count: capture_count as usize,
- };
- for group in 0..capture_count {
- let group_index = group + 1;
- if group_index < total_groups {
- slots.starts[group as usize] = compiled_regex.capture_slot(group_index * 2);
- slots.ends[group as usize] = compiled_regex.capture_slot(group_index * 2 + 1);
+ LegacyCaptureSlots::new(n_capture_groups as usize, |slot| {
+ let slot = u32_from_usize(slot);
+ if slot / 2 < total_groups {
+ compiled_regex.capture_slot(slot)
+ } else {
+ -1
}
- }
- slots
+ })
}
fn throw_backtrack_limit_exceeded<T>(vm: &Vm) -> ThrowCompletionOr<T> {
@@ -628,8 +601,7 @@ fn regexp_builtin_exec(
string,
match_index,
end_index,
- legacy_captures.starts(),
- legacy_captures.ends(),
+ &legacy_captures,
);
} else if realm == regexp_object.realm() {
invalidate_legacy_regexp_static_properties(realm.intrinsics().regexp_constructor(vm));
@@ -1493,8 +1465,7 @@ impl RegExpPrototype {
string,
last_match_start,
last_match_end,
- legacy_captures.starts(),
- legacy_captures.ends(),
+ &legacy_captures,
);
} else if had_match && realm == typed_regexp.realm() {
invalidate_legacy_regexp_static_properties(realm.intrinsics().regexp_constructor(vm));
@@ -1596,17 +1567,14 @@ impl RegExpPrototype {
if let Some(last_match) = slots.rchunks(slots_per_match).next() {
if typed_regexp.legacy_features_enabled() && realm == typed_regexp.realm() {
- let capture_count = (slots_per_match / 2 - 1).min(MAX_LEGACY_CAPTURES as usize);
- let starts: Vec<i32> = (1..=capture_count).map(|group| last_match[group * 2]).collect();
- let ends: Vec<i32> = (1..=capture_count).map(|group| last_match[group * 2 + 1]).collect();
+ let legacy_captures = LegacyCaptureSlots::new(slots_per_match / 2 - 1, |slot| last_match[slot]);
update_legacy_regexp_static_properties_lazy(
vm,
realm.intrinsics().regexp_constructor(vm),
string,
last_match[0] as usize,
last_match[1] as usize,
- &starts,
- &ends,
+ &legacy_captures,
);
} else if realm == typed_regexp.realm() {
invalidate_legacy_regexp_static_properties(realm.intrinsics().regexp_constructor(vm));
@@ -2181,8 +2149,7 @@ impl RegExpPrototype {
string,
match_start,
match_end,
- legacy_captures.starts(),
- legacy_captures.ends(),
+ &legacy_captures,
);
}
@@ -2519,8 +2486,7 @@ impl RegExpPrototype {
string,
match_start,
match_end,
- legacy_captures.starts(),
- legacy_captures.ends(),
+ &legacy_captures,
);
}
return Ok(Value::from_bool(matched));
diff --git a/Tests/LibJS/Runtime/builtins/RegExp/RegExp.legacy.js b/Tests/LibJS/Runtime/builtins/RegExp/RegExp.legacy.js
index 4a45073ee05..158b0670658 100644
--- a/Tests/LibJS/Runtime/builtins/RegExp/RegExp.legacy.js
+++ b/Tests/LibJS/Runtime/builtins/RegExp/RegExp.legacy.js
@@ -202,6 +202,21 @@ test("the last parenthesis didn't match.", () => {
expect(RegExp.lastParen).toBe("");
});
+test("the last parenthesis can be past $9", () => {
+ /(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)/.exec("abcdefghij");
+ expect(RegExp.lastParen).toBe("j");
+ expect(RegExp.$9).toBe("i");
+
+ /(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)?/.exec("abcdefghij");
+ expect(RegExp.lastParen).toBe("");
+
+ "abcdefghij".replace(/(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)/g, "");
+ expect(RegExp.lastParen).toBe("j");
+
+ "abcdefghij".split(/(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)/);
+ expect(RegExp.lastParen).toBe("j");
+});
+
test("$1 to $9", () => {
for (var i = 1; i <= 9; i++) {
var haystack = "foo";