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