2020-06-03 16:05:49 -07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-03 16:05:49 -07: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 15:40:48 +02:00
|
|
|
RegExpConstructor::RegExpConstructor(GlobalObject& global_object)
|
2020-10-13 23:49:19 +02:00
|
|
|
: NativeFunction(vm().names.RegExp, *global_object.function_prototype())
|
2020-06-03 16:05:49 -07:00
|
|
|
{
|
2020-06-20 15:40:48 +02:00
|
|
|
}
|
|
|
|
|
2020-07-22 17:50:18 +02:00
|
|
|
void RegExpConstructor::initialize(GlobalObject& global_object)
|
2020-06-20 15:40:48 +02:00
|
|
|
{
|
2020-10-13 23:49:19 +02:00
|
|
|
auto& vm = this->vm();
|
2020-07-22 17:50:18 +02:00
|
|
|
NativeFunction::initialize(global_object);
|
2020-10-13 23:49:19 +02:00
|
|
|
define_property(vm.names.prototype, global_object.regexp_prototype(), 0);
|
|
|
|
define_property(vm.names.length, Value(2), Attribute::Configurable);
|
2020-06-03 16:05:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
RegExpConstructor::~RegExpConstructor()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-09-27 17:24:14 +02:00
|
|
|
Value RegExpConstructor::call()
|
2020-06-03 16:05:49 -07:00
|
|
|
{
|
2020-09-27 18:45:21 +02:00
|
|
|
return construct(*this);
|
2020-06-03 16:05:49 -07:00
|
|
|
}
|
|
|
|
|
2020-09-27 18:45:21 +02:00
|
|
|
Value RegExpConstructor::construct(Function&)
|
2020-06-03 16:05:49 -07:00
|
|
|
{
|
2020-09-27 18:36:49 +02:00
|
|
|
auto& vm = this->vm();
|
2020-11-27 22:42:41 +00: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 17:14:50 +03:30
|
|
|
return RegExpObject::create(global_object(), pattern, flags);
|
2020-06-03 16:05:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|