2020-04-01 19:42:07 +01:00
|
|
|
/*
|
2022-02-07 14:38:53 +00:00
|
|
|
* Copyright (c) 2020-2022, 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
|
|
|
*/
|
|
|
|
|
2021-06-20 01:09:39 +01:00
|
|
|
#include <LibJS/Runtime/AbstractOperations.h>
|
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 {
|
|
|
|
|
2022-08-16 00:20:49 +01:00
|
|
|
ErrorConstructor::ErrorConstructor(Realm& realm)
|
|
|
|
: NativeFunction(vm().names.Error.as_string(), *realm.global_object().function_prototype())
|
2020-04-01 19:42:07 +01:00
|
|
|
{
|
2020-06-20 15:40:48 +02:00
|
|
|
}
|
|
|
|
|
2022-08-16 00:20:49 +01:00
|
|
|
void ErrorConstructor::initialize(Realm& realm)
|
2020-06-20 15:40:48 +02:00
|
|
|
{
|
2020-10-13 23:49:19 +02:00
|
|
|
auto& vm = this->vm();
|
2022-08-16 00:20:49 +01:00
|
|
|
NativeFunction::initialize(realm);
|
2021-06-19 00:38:41 +01:00
|
|
|
|
|
|
|
// 20.5.2.1 Error.prototype, https://tc39.es/ecma262/#sec-error.prototype
|
2022-08-16 00:20:49 +01:00
|
|
|
define_direct_property(vm.names.prototype, realm.global_object().error_prototype(), 0);
|
2021-06-19 00:38:41 +01:00
|
|
|
|
2021-07-06 02:15:08 +03:00
|
|
|
define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
|
2020-04-01 19:42:07 +01:00
|
|
|
}
|
|
|
|
|
2022-02-07 14:38:53 +00:00
|
|
|
// 20.5.1.1 Error ( message [ , options ] ), https://tc39.es/ecma262/#sec-error-message
|
2021-10-20 21:16:30 +01:00
|
|
|
ThrowCompletionOr<Value> ErrorConstructor::call()
|
2020-04-01 19:42:07 +01:00
|
|
|
{
|
2022-02-07 14:38:53 +00:00
|
|
|
// 1. If NewTarget is undefined, let newTarget be the active function object; else let newTarget be NewTarget.
|
2021-10-20 21:16:30 +01:00
|
|
|
return TRY(construct(*this));
|
2020-04-01 19:42:07 +01:00
|
|
|
}
|
|
|
|
|
2022-02-07 14:38:53 +00:00
|
|
|
// 20.5.1.1 Error ( message [ , options ] ), https://tc39.es/ecma262/#sec-error-message
|
2021-10-20 21:16:30 +01:00
|
|
|
ThrowCompletionOr<Object*> ErrorConstructor::construct(FunctionObject& new_target)
|
2020-04-01 19:42:07 +01:00
|
|
|
{
|
2020-09-27 18:45:21 +02:00
|
|
|
auto& vm = this->vm();
|
2021-06-20 01:09:39 +01:00
|
|
|
|
2022-02-07 14:38:53 +00:00
|
|
|
auto message = vm.argument(0);
|
|
|
|
auto options = vm.argument(1);
|
|
|
|
|
|
|
|
// 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%Error.prototype%", « [[ErrorData]] »).
|
2022-08-21 19:24:32 +01:00
|
|
|
auto* error = TRY(ordinary_create_from_constructor<Error>(vm, new_target, &GlobalObject::error_prototype));
|
2021-06-11 20:40:08 +01:00
|
|
|
|
2022-02-07 14:38:53 +00:00
|
|
|
// 3. If message is not undefined, then
|
|
|
|
if (!message.is_undefined()) {
|
|
|
|
// a. Let msg be ? ToString(message).
|
2022-08-21 14:00:56 +01:00
|
|
|
auto msg = TRY(message.to_string(vm));
|
2022-02-07 14:38:53 +00:00
|
|
|
|
2022-05-02 20:54:39 +02:00
|
|
|
// b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "message", msg).
|
|
|
|
error->create_non_enumerable_data_property_or_throw(vm.names.message, js_string(vm, move(msg)));
|
2020-05-15 13:39:24 +02:00
|
|
|
}
|
2021-06-11 20:40:08 +01:00
|
|
|
|
2022-02-07 14:38:53 +00:00
|
|
|
// 4. Perform ? InstallErrorCause(O, options).
|
|
|
|
TRY(error->install_error_cause(options));
|
2021-06-11 20:40:08 +01:00
|
|
|
|
2022-02-07 14:38:53 +00:00
|
|
|
// 5. Return O.
|
2021-06-11 20:40:08 +01:00
|
|
|
return error;
|
2020-04-01 19:42:07 +01:00
|
|
|
}
|
|
|
|
|
2022-08-21 19:24:32 +01:00
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
|
|
|
|
ConstructorName::ConstructorName(Realm& realm) \
|
|
|
|
: NativeFunction(vm().names.ClassName.as_string(), *static_cast<Object*>(realm.global_object().error_constructor())) \
|
|
|
|
{ \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
void ConstructorName::initialize(Realm& realm) \
|
|
|
|
{ \
|
|
|
|
auto& vm = this->vm(); \
|
|
|
|
NativeFunction::initialize(realm); \
|
|
|
|
\
|
|
|
|
/* 20.5.6.2.1 NativeError.prototype, https://tc39.es/ecma262/#sec-nativeerror.prototype */ \
|
|
|
|
define_direct_property(vm.names.prototype, realm.global_object().snake_name##_prototype(), 0); \
|
|
|
|
\
|
|
|
|
define_direct_property(vm.names.length, Value(1), Attribute::Configurable); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
ConstructorName::~ConstructorName() = default; \
|
|
|
|
\
|
|
|
|
/* 20.5.6.1.1 NativeError ( message [ , options ] ), https://tc39.es/ecma262/#sec-nativeerror */ \
|
|
|
|
ThrowCompletionOr<Value> ConstructorName::call() \
|
|
|
|
{ \
|
|
|
|
/* 1. If NewTarget is undefined, let newTarget be the active function object; else let newTarget be NewTarget. */ \
|
|
|
|
return TRY(construct(*this)); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
/* 20.5.6.1.1 NativeError ( message [ , options ] ), https://tc39.es/ecma262/#sec-nativeerror */ \
|
|
|
|
ThrowCompletionOr<Object*> ConstructorName::construct(FunctionObject& new_target) \
|
|
|
|
{ \
|
|
|
|
auto& vm = this->vm(); \
|
|
|
|
\
|
|
|
|
auto message = vm.argument(0); \
|
|
|
|
auto options = vm.argument(1); \
|
|
|
|
\
|
|
|
|
/* 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%NativeError.prototype%", « [[ErrorData]] »). */ \
|
|
|
|
auto* error = TRY(ordinary_create_from_constructor<ClassName>(vm, new_target, &GlobalObject::snake_name##_prototype)); \
|
|
|
|
\
|
|
|
|
/* 3. If message is not undefined, then */ \
|
|
|
|
if (!message.is_undefined()) { \
|
|
|
|
/* a. Let msg be ? ToString(message). */ \
|
|
|
|
auto msg = TRY(message.to_string(vm)); \
|
|
|
|
\
|
|
|
|
/* b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "message", msg). */ \
|
|
|
|
error->create_non_enumerable_data_property_or_throw(vm.names.message, js_string(vm, move(msg))); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
/* 4. Perform ? InstallErrorCause(O, options). */ \
|
|
|
|
TRY(error->install_error_cause(options)); \
|
|
|
|
\
|
|
|
|
/* 5. Return O. */ \
|
|
|
|
return error; \
|
2020-04-10 12:42:33 +02:00
|
|
|
}
|
|
|
|
|
2021-06-11 17:54:42 +01:00
|
|
|
JS_ENUMERATE_NATIVE_ERRORS
|
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
|
|
|
}
|