2020-04-01 19:42:07 +01:00
|
|
|
/*
|
2021-04-22 22:51:19 +02:00
|
|
|
* Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
|
2020-04-01 19:42:07 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-01 19:42:07 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibJS/Runtime/Error.h>
|
|
|
|
#include <LibJS/Runtime/ErrorConstructor.h>
|
2020-04-18 13:18:06 +02:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-04-01 19:42:07 +01:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2020-06-20 15:40:48 +02:00
|
|
|
ErrorConstructor::ErrorConstructor(GlobalObject& global_object)
|
2020-10-13 23:49:19 +02:00
|
|
|
: NativeFunction(vm().names.Error, *global_object.function_prototype())
|
2020-04-01 19:42:07 +01:00
|
|
|
{
|
2020-06-20 15:40:48 +02:00
|
|
|
}
|
|
|
|
|
2020-07-22 17:50:18 +02:00
|
|
|
void ErrorConstructor::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.error_prototype(), 0);
|
|
|
|
define_property(vm.names.length, Value(1), Attribute::Configurable);
|
2020-04-01 19:42:07 +01:00
|
|
|
}
|
|
|
|
|
2020-09-27 17:24:14 +02:00
|
|
|
Value ErrorConstructor::call()
|
2020-04-01 19:42:07 +01:00
|
|
|
{
|
2020-09-27 18:45:21 +02:00
|
|
|
return construct(*this);
|
2020-04-01 19:42:07 +01:00
|
|
|
}
|
|
|
|
|
2020-09-27 18:45:21 +02:00
|
|
|
Value ErrorConstructor::construct(Function&)
|
2020-04-01 19:42:07 +01:00
|
|
|
{
|
2020-09-27 18:45:21 +02:00
|
|
|
auto& vm = this->vm();
|
2021-04-12 00:08:28 +02:00
|
|
|
String message;
|
|
|
|
if (!vm.argument(0).is_undefined()) {
|
|
|
|
message = vm.argument(0).to_string(global_object());
|
2020-09-27 18:45:21 +02:00
|
|
|
if (vm.exception())
|
2020-05-15 13:39:24 +02:00
|
|
|
return {};
|
|
|
|
}
|
2021-04-12 00:08:28 +02:00
|
|
|
return Error::create(global_object(), message);
|
2020-04-01 19:42:07 +01:00
|
|
|
}
|
|
|
|
|
2021-04-12 00:08:28 +02:00
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
|
|
|
|
ConstructorName::ConstructorName(GlobalObject& global_object) \
|
2021-06-11 00:20:27 +01:00
|
|
|
: NativeFunction(*static_cast<Object*>(global_object.error_constructor())) \
|
2021-04-12 00:08:28 +02:00
|
|
|
{ \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
void ConstructorName::initialize(GlobalObject& global_object) \
|
|
|
|
{ \
|
|
|
|
auto& vm = this->vm(); \
|
|
|
|
NativeFunction::initialize(global_object); \
|
|
|
|
define_property(vm.names.prototype, global_object.snake_name##_prototype(), 0); \
|
|
|
|
define_property(vm.names.length, Value(1), Attribute::Configurable); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
ConstructorName::~ConstructorName() { } \
|
|
|
|
\
|
|
|
|
Value ConstructorName::call() \
|
|
|
|
{ \
|
|
|
|
return construct(*this); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
Value ConstructorName::construct(Function&) \
|
|
|
|
{ \
|
|
|
|
auto& vm = this->vm(); \
|
|
|
|
String message = ""; \
|
|
|
|
if (!vm.argument(0).is_undefined()) { \
|
|
|
|
message = vm.argument(0).to_string(global_object()); \
|
|
|
|
if (vm.exception()) \
|
|
|
|
return {}; \
|
|
|
|
} \
|
|
|
|
return ClassName::create(global_object(), message); \
|
2020-04-10 12:42:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
JS_ENUMERATE_ERROR_SUBCLASSES
|
2020-04-10 14:06:52 +02:00
|
|
|
#undef __JS_ENUMERATE
|
2020-04-10 12:42:33 +02:00
|
|
|
|
2020-04-01 19:42:07 +01:00
|
|
|
}
|