Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/perry-runtime/src/object/class_registry/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,16 @@ pub unsafe extern "C" fn js_new_function_construct(
if crate::builtins::boxed_primitive_payload(func_value).is_some() {
super::super::object_ops::throw_object_type_error(b"is not a constructor");
}
// `new (new RegExp())` — a RegExp instance has no [[Construct]] internal
// method (Test262 `S15.10.7_A2_T2`). Without this it fell through to the
// empty-object construction fallback and silently produced `{}` instead
// of throwing.
{
let jv = crate::value::JSValue::from_bits(func_value.to_bits());
if jv.is_pointer() && crate::regex::is_registered_regex(jv.as_pointer::<u8>() as usize) {
super::super::object_ops::throw_object_type_error(b"is not a constructor");
}
}
// #3656: `new p()` where `p` is a Proxy dispatches through its `construct`
// trap (or forwards to the target). Reached when the compiler can't prove
// the callee is a proxy statically (e.g. `new record.proxy()`). newTarget
Expand Down
14 changes: 14 additions & 0 deletions crates/perry-runtime/src/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type CompiledRegex = regex::Regex;
#[cfg(not(feature = "regex-engine"))]
type CompiledRegex = ();

#[cfg(feature = "regex-engine")]
mod class_range_validate;
#[cfg(feature = "regex-engine")]
mod compile;
mod escape;
Expand All @@ -41,6 +43,8 @@ mod match_all;
mod replace_expand;
mod replace_fn;
#[cfg(feature = "regex-engine")]
use class_range_validate::has_out_of_order_double_dash_class_range;
#[cfg(feature = "regex-engine")]
pub use compile::js_regexp_compile_value;
pub use escape::js_regexp_escape;
#[cfg(feature = "regex-engine")]
Expand Down Expand Up @@ -587,6 +591,16 @@ pub extern "C" fn js_regexp_new(
pattern_str
));
}
// `--` is the real ClassSetExpression subtraction operator under
// the `v` flag (UTS #51) — `[a--z]` there means "a minus z", not
// a malformed range — so only legacy/`u`-mode patterns are
// subject to the doubled-hyphen range-order check.
if !flags_str.contains('v') && has_out_of_order_double_dash_class_range(pattern_str) {
throw_regexp_syntax_error(&format!(
"Invalid regular expression: /{}/: invalid pattern",
pattern_str
));
}
// Annex B.1.4 legacy escapes (`\1` non-backref octal, `\0DD`, `\8`/`\9`,
// `\c` without a control letter) are accepted in sloppy patterns but are
// a hard SyntaxError under the `/u` (and `/v`) flag — `js_regex_to_rust`
Expand Down
92 changes: 92 additions & 0 deletions crates/perry-runtime/src/regex/class_range_validate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//! JS-spec character-class range validation the underlying `regex` /
//! `fancy-regex` crates don't perform themselves. Split out of `grammar.rs`
//! to keep that file under the 2000-line size gate.

/// Detect a character-class range made out of order by a doubled hyphen
/// (`[a--z]`): JS parses the class contents "a--z" as ClassAtom `a`, a `-`
/// range operator, ClassAtom `-` (a literal hyphen is itself a valid
/// ClassAtom) — i.e. the range `a`..`-`. Since `a` (U+0061) is greater than
/// `-` (U+002D), `CharacterRange` (22.2.1.1) requires the low bound's code
/// point to be no greater than the high bound's, so this is a SyntaxError.
/// The Rust `regex` crate parses the doubled hyphen differently and
/// silently accepts patterns like this, so Perry must catch it before
/// delegating to the crate (test262 `S15.10.4.1_A9_T3`).
pub(super) fn has_out_of_order_double_dash_class_range(pattern: &str) -> bool {
let chars: Vec<char> = pattern.chars().collect();
let mut i = 0;
let mut in_class = false;
// True for the first ClassAtom position of the current class — `^` is
// only a negation marker there (`[^a]`); a `^` anywhere else (`[x^--z]`)
// is an ordinary ClassAtom and must still be range-order-checked.
let mut at_class_start = false;
while i < chars.len() {
match chars[i] {
'\\' => {
i += 2;
at_class_start = false;
}
'[' if !in_class => {
in_class = true;
at_class_start = true;
i += 1;
}
']' if in_class => {
in_class = false;
i += 1;
}
'^' if in_class && at_class_start => {
at_class_start = false;
i += 1;
}
c if in_class
&& chars.get(i + 1) == Some(&'-')
&& chars.get(i + 2) == Some(&'-')
&& !matches!(chars.get(i + 3), None | Some(']')) =>
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
{
if (c as u32) > ('-' as u32) {
return true;
}
at_class_start = false;
i += 1;
}
_ => {
at_class_start = false;
i += 1;
}
}
}
false
}

#[cfg(test)]
mod tests {
use super::has_out_of_order_double_dash_class_range;

#[test]
fn rejects_descending_double_dash_ranges() {
assert!(has_out_of_order_double_dash_class_range("[a--z]"));
assert!(has_out_of_order_double_dash_class_range("[a---z]"));
}

#[test]
fn accepts_ascending_double_dash_ranges() {
assert!(!has_out_of_order_double_dash_class_range("[!--z]"));
assert!(!has_out_of_order_double_dash_class_range("[+--z]"));
}

#[test]
fn ignores_unrelated_class_hyphens() {
assert!(!has_out_of_order_double_dash_class_range("[a-z-]"));
assert!(!has_out_of_order_double_dash_class_range("[a-z]"));
assert!(!has_out_of_order_double_dash_class_range("abc"));
}

#[test]
fn caret_is_only_a_negation_marker_at_class_start() {
// A leading `^` negates the class and is not itself a range atom.
assert!(!has_out_of_order_double_dash_class_range("[^--z]"));
// A non-leading `^` is an ordinary ClassAtom (U+005E > U+002D `-`),
// so `^--z` is the same out-of-order range as `a--z`.
assert!(has_out_of_order_double_dash_class_range("[x^--z]"));
}
}
9 changes: 9 additions & 0 deletions crates/perry-runtime/src/regex/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::sync::Arc;

use regex::Regex;

use super::class_range_validate::has_out_of_order_double_dash_class_range;
use super::grammar::{
has_invalid_repeated_quantifier, has_unicode_forbidden_legacy_escape,
has_unicode_forbidden_pattern, js_regex_to_rust,
Expand Down Expand Up @@ -92,6 +93,14 @@ pub extern "C" fn js_regexp_compile_value(
pattern_str
));
}
// `--` is the real ClassSetExpression subtraction operator under the `v`
// flag (UTS #51) — see the matching comment in `js_regexp_new`.
if !flags_str.contains('v') && has_out_of_order_double_dash_class_range(pattern_str) {
throw_regexp_syntax_error(&format!(
"Invalid regular expression: /{}/: invalid pattern",
pattern_str
));
}
// Annex B.1.4 leniencies are hard `SyntaxError`s under `/u` (mirror of
// `js_regexp_new`): legacy escapes for `u`/`v`, plus the structural
// restrictions for `u` specifically.
Expand Down
Loading