ladybird/Userland/Libraries/LibJS/Runtime/ErrorConstructor.cpp
Linus Groh 0e38c9b2f7 LibJS: Set NativeError constructors' prototype to Error constructor
The FunctionPrototype is correct for ErrorConstructor itself:

    20.5.2 Properties of the Error Constructor

    The Error constructor:
    - has a [[Prototype]] internal slot whose value is
      %Function.prototype%.

However, not for all the other "NativeError" constructors:

    20.5.6.2 Properties of the NativeError Constructors

    Each NativeError constructor:
    - has a [[Prototype]] internal slot whose value is %Error%.
2021-06-11 00:23:13 +01:00

79 lines
3.9 KiB
C++

/*
* Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/ErrorConstructor.h>
#include <LibJS/Runtime/GlobalObject.h>
namespace JS {
ErrorConstructor::ErrorConstructor(GlobalObject& global_object)
: NativeFunction(vm().names.Error, *global_object.function_prototype())
{
}
void ErrorConstructor::initialize(GlobalObject& global_object)
{
auto& vm = this->vm();
NativeFunction::initialize(global_object);
define_property(vm.names.prototype, global_object.error_prototype(), 0);
define_property(vm.names.length, Value(1), Attribute::Configurable);
}
Value ErrorConstructor::call()
{
return construct(*this);
}
Value ErrorConstructor::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 Error::create(global_object(), message);
}
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
ConstructorName::ConstructorName(GlobalObject& global_object) \
: NativeFunction(*static_cast<Object*>(global_object.error_constructor())) \
{ \
} \
\
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); \
}
JS_ENUMERATE_ERROR_SUBCLASSES
#undef __JS_ENUMERATE
}