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 {
|
|
|
|
|
|
2020-06-20 10:40:48 -03:00
|
|
|
RegExpConstructor::RegExpConstructor(GlobalObject& global_object)
|
2020-10-13 18:49:19 -03:00
|
|
|
: NativeFunction(vm().names.RegExp, *global_object.function_prototype())
|
2020-06-03 20:05:49 -03:00
|
|
|
{
|
2020-06-20 10:40:48 -03:00
|
|
|
}
|
|
|
|
|
|
2020-07-22 12:50:18 -03:00
|
|
|
void RegExpConstructor::initialize(GlobalObject& global_object)
|
2020-06-20 10:40:48 -03:00
|
|
|
{
|
2020-10-13 18:49:19 -03:00
|
|
|
auto& vm = this->vm();
|
2020-07-22 12:50:18 -03:00
|
|
|
NativeFunction::initialize(global_object);
|
2020-10-13 18:49:19 -03:00
|
|
|
define_property(vm.names.prototype, global_object.regexp_prototype(), 0);
|
|
|
|
|
define_property(vm.names.length, Value(2), Attribute::Configurable);
|
2021-06-07 13:31:32 -03:00
|
|
|
|
|
|
|
|
define_native_property(vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
|
2020-06-03 20:05:49 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RegExpConstructor::~RegExpConstructor()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-27 12:24:14 -03:00
|
|
|
Value RegExpConstructor::call()
|
2020-06-03 20:05:49 -03:00
|
|
|
{
|
2020-09-27 13:45:21 -03:00
|
|
|
return construct(*this);
|
2020-06-03 20:05:49 -03:00
|
|
|
}
|
|
|
|
|
|
2020-09-27 13:45:21 -03:00
|
|
|
Value RegExpConstructor::construct(Function&)
|
2020-06-03 20:05:49 -03:00
|
|
|
{
|
2020-09-27 13:36:49 -03:00
|
|
|
auto& vm = this->vm();
|
2020-11-27 19:42:41 -03:00
|
|
|
String pattern = "";
|
|
|
|
|
String flags = "";
|
|
|
|
|
if (!vm.argument(0).is_undefined()) {
|
|
|
|
|
pattern = vm.argument(0).to_string(global_object());
|
|
|
|
|
if (vm.exception())
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
if (!vm.argument(1).is_undefined()) {
|
|
|
|
|
flags = vm.argument(1).to_string(global_object());
|
|
|
|
|
if (vm.exception())
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2020-11-27 10:44:50 -03:00
|
|
|
return RegExpObject::create(global_object(), pattern, flags);
|
2020-06-03 20:05:49 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-07 13:31:32 -03:00
|
|
|
JS_DEFINE_NATIVE_GETTER(RegExpConstructor::symbol_species_getter)
|
|
|
|
|
{
|
|
|
|
|
return vm.this_value(global_object);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-03 20:05:49 -03:00
|
|
|
}
|