mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-11 18:20:43 +09:00
LibJS: Implement RegExp constructor according to the spec
This allows passing an existing RegExp object (or an object that is sufficiently like a RegExp object) as the "pattern" argument of the RegExp constructor.
This commit is contained in:
parent
2686c5f503
commit
3892b6e6ec
Notes:
sideshowbarker
2024-07-18 09:58:31 +09:00
Author: https://github.com/trflynn89
Commit: 3892b6e6ec
Pull-request: https://github.com/SerenityOS/serenity/pull/8584
Reviewed-by: https://github.com/linusg
2 changed files with 80 additions and 2 deletions
|
@ -36,6 +36,25 @@ RegExpConstructor::~RegExpConstructor()
|
|||
// 22.2.3.1 RegExp ( pattern, flags ), https://tc39.es/ecma262/#sec-regexp-pattern-flags
|
||||
Value RegExpConstructor::call()
|
||||
{
|
||||
auto& vm = this->vm();
|
||||
auto& global_object = this->global_object();
|
||||
|
||||
auto pattern = vm.argument(0);
|
||||
auto flags = vm.argument(1);
|
||||
|
||||
bool pattern_is_regexp = pattern.is_regexp(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
if (pattern_is_regexp && flags.is_undefined()) {
|
||||
auto pattern_constructor = pattern.as_object().get(vm.names.constructor);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
if (same_value(this, pattern_constructor))
|
||||
return pattern;
|
||||
}
|
||||
|
||||
return construct(*this);
|
||||
}
|
||||
|
||||
|
@ -43,8 +62,44 @@ Value RegExpConstructor::call()
|
|||
Value RegExpConstructor::construct(FunctionObject&)
|
||||
{
|
||||
auto& vm = this->vm();
|
||||
// FIXME: This is non-conforming
|
||||
return regexp_create(global_object(), vm.argument(0), vm.argument(1));
|
||||
auto& global_object = this->global_object();
|
||||
|
||||
auto pattern = vm.argument(0);
|
||||
auto flags = vm.argument(1);
|
||||
|
||||
bool pattern_is_regexp = pattern.is_regexp(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
Value pattern_value;
|
||||
Value flags_value;
|
||||
|
||||
if (pattern.is_object() && is<RegExpObject>(pattern.as_object())) {
|
||||
auto& regexp_pattern = static_cast<RegExpObject&>(pattern.as_object());
|
||||
pattern_value = js_string(vm, regexp_pattern.pattern());
|
||||
|
||||
if (flags.is_undefined())
|
||||
flags_value = js_string(vm, regexp_pattern.flags());
|
||||
else
|
||||
flags_value = flags;
|
||||
} else if (pattern_is_regexp) {
|
||||
pattern_value = pattern.as_object().get(vm.names.source);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
if (flags.is_undefined()) {
|
||||
flags_value = pattern.as_object().get(vm.names.flags);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
} else {
|
||||
flags_value = flags;
|
||||
}
|
||||
} else {
|
||||
pattern_value = pattern;
|
||||
flags_value = flags;
|
||||
}
|
||||
|
||||
return regexp_create(global_object, pattern_value, flags_value);
|
||||
}
|
||||
|
||||
// 22.2.4.2 get RegExp [ @@species ], https://tc39.es/ecma262/#sec-get-regexp-@@species
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue