2020-06-03 23:05:49 +00:00
|
|
|
/*
|
2021-04-22 23:53:07 +00:00
|
|
|
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
|
2020-06-03 23:05:49 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-03 23:05:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibJS/Runtime/Error.h>
|
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
|
|
#include <LibJS/Runtime/RegExpConstructor.h>
|
|
|
|
#include <LibJS/Runtime/RegExpObject.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2022-08-15 23:20:49 +00:00
|
|
|
RegExpConstructor::RegExpConstructor(Realm& realm)
|
|
|
|
: NativeFunction(vm().names.RegExp.as_string(), *realm.global_object().function_prototype())
|
2020-06-03 23:05:49 +00:00
|
|
|
{
|
2020-06-20 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 23:20:49 +00:00
|
|
|
void RegExpConstructor::initialize(Realm& realm)
|
2020-06-20 13:40:48 +00:00
|
|
|
{
|
2020-10-13 21:49:19 +00:00
|
|
|
auto& vm = this->vm();
|
2022-08-15 23:20:49 +00:00
|
|
|
NativeFunction::initialize(realm);
|
2021-06-18 23:38:41 +00:00
|
|
|
|
|
|
|
// 22.2.4.1 RegExp.prototype, https://tc39.es/ecma262/#sec-regexp.prototype
|
2022-08-15 23:20:49 +00:00
|
|
|
define_direct_property(vm.names.prototype, realm.global_object().regexp_prototype(), 0);
|
2021-06-18 23:38:41 +00:00
|
|
|
|
2022-08-22 20:47:35 +00:00
|
|
|
define_native_accessor(realm, *vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
|
2021-07-07 23:49:53 +00:00
|
|
|
|
|
|
|
define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
|
2020-06-03 23:05:49 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 23:38:41 +00:00
|
|
|
// 22.2.3.1 RegExp ( pattern, flags ), https://tc39.es/ecma262/#sec-regexp-pattern-flags
|
2021-10-20 20:16:30 +00:00
|
|
|
ThrowCompletionOr<Value> RegExpConstructor::call()
|
2020-06-03 23:05:49 +00:00
|
|
|
{
|
2021-07-08 17:36:09 +00:00
|
|
|
auto& vm = this->vm();
|
|
|
|
|
|
|
|
auto pattern = vm.argument(0);
|
|
|
|
auto flags = vm.argument(1);
|
|
|
|
|
2022-08-21 13:00:56 +00:00
|
|
|
bool pattern_is_regexp = TRY(pattern.is_regexp(vm));
|
2021-07-08 17:36:09 +00:00
|
|
|
|
|
|
|
if (pattern_is_regexp && flags.is_undefined()) {
|
2021-10-20 20:16:30 +00:00
|
|
|
auto pattern_constructor = TRY(pattern.as_object().get(vm.names.constructor));
|
2021-07-08 17:36:09 +00:00
|
|
|
if (same_value(this, pattern_constructor))
|
|
|
|
return pattern;
|
|
|
|
}
|
|
|
|
|
2021-10-20 20:16:30 +00:00
|
|
|
return TRY(construct(*this));
|
2020-06-03 23:05:49 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 23:38:41 +00:00
|
|
|
// 22.2.3.1 RegExp ( pattern, flags ), https://tc39.es/ecma262/#sec-regexp-pattern-flags
|
2021-10-20 20:16:30 +00:00
|
|
|
ThrowCompletionOr<Object*> RegExpConstructor::construct(FunctionObject&)
|
2020-06-03 23:05:49 +00:00
|
|
|
{
|
2020-09-27 16:36:49 +00:00
|
|
|
auto& vm = this->vm();
|
2021-07-08 17:36:09 +00:00
|
|
|
|
|
|
|
auto pattern = vm.argument(0);
|
|
|
|
auto flags = vm.argument(1);
|
|
|
|
|
2022-08-21 13:00:56 +00:00
|
|
|
bool pattern_is_regexp = TRY(pattern.is_regexp(vm));
|
2021-07-08 17:36:09 +00:00
|
|
|
|
|
|
|
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) {
|
2021-10-20 20:16:30 +00:00
|
|
|
pattern_value = TRY(pattern.as_object().get(vm.names.source));
|
2021-07-08 17:36:09 +00:00
|
|
|
|
|
|
|
if (flags.is_undefined()) {
|
2021-10-20 20:16:30 +00:00
|
|
|
flags_value = TRY(pattern.as_object().get(vm.names.flags));
|
2021-07-08 17:36:09 +00:00
|
|
|
} else {
|
|
|
|
flags_value = flags;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pattern_value = pattern;
|
|
|
|
flags_value = flags;
|
|
|
|
}
|
|
|
|
|
2022-08-21 15:14:51 +00:00
|
|
|
return TRY(regexp_create(vm, pattern_value, flags_value));
|
2020-06-03 23:05:49 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 23:38:41 +00:00
|
|
|
// 22.2.4.2 get RegExp [ @@species ], https://tc39.es/ecma262/#sec-get-regexp-@@species
|
2021-10-23 00:52:55 +00:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(RegExpConstructor::symbol_species_getter)
|
2021-06-07 16:31:32 +00:00
|
|
|
{
|
2022-08-20 08:48:43 +00:00
|
|
|
return vm.this_value();
|
2021-06-07 16:31:32 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 23:05:49 +00:00
|
|
|
}
|