Browse Source

Fix `RegExp` constructor return value when pattern is a regexp (#2880)

When `RegExp` constructor is called with a regexp it should return the same regexp, if `NewTarget`'s constructor is the same.
pull/2882/head
Haled Odat 2 years ago
parent
commit
c738b50727
  1. 9
      boa_engine/src/builtins/regexp/mod.rs

9
boa_engine/src/builtins/regexp/mod.rs

@ -171,13 +171,20 @@ impl BuiltInConstructor for RegExp {
// 3. Else, let newTarget be NewTarget. // 3. Else, let newTarget be NewTarget.
if new_target.is_undefined() { if new_target.is_undefined() {
// a. Let newTarget be the active function object. // a. Let newTarget be the active function object.
let new_target = context
.vm
.active_function
.clone()
.map_or(JsValue::undefined(), JsValue::new);
// b. If patternIsRegExp is true and flags is undefined, then // b. If patternIsRegExp is true and flags is undefined, then
if let Some(pattern) = pattern_is_regexp { if let Some(pattern) = pattern_is_regexp {
if flags.is_undefined() { if flags.is_undefined() {
// i. Let patternConstructor be ? Get(pattern, "constructor"). // i. Let patternConstructor be ? Get(pattern, "constructor").
let pattern_constructor = pattern.get(CONSTRUCTOR, context)?; let pattern_constructor = pattern.get(CONSTRUCTOR, context)?;
// ii. If SameValue(newTarget, patternConstructor) is true, return pattern. // ii. If SameValue(newTarget, patternConstructor) is true, return pattern.
if JsValue::same_value(new_target, &pattern_constructor) { if JsValue::same_value(&new_target, &pattern_constructor) {
return Ok(pattern.clone().into()); return Ok(pattern.clone().into());
} }
} }

Loading…
Cancel
Save