2020-04-01 15:42:07 -03:00
|
|
|
/*
|
2023-04-12 19:47:15 -03:00
|
|
|
* Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
|
2020-04-01 15:42:07 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-01 15:42:07 -03:00
|
|
|
*/
|
|
|
|
|
|
2021-06-19 21:09:39 -03:00
|
|
|
#include <LibJS/Runtime/AbstractOperations.h>
|
2020-04-01 15:42:07 -03:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
|
|
|
|
#include <LibJS/Runtime/ErrorConstructor.h>
|
2020-04-18 08:18:06 -03:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-04-01 15:42:07 -03:00
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC_DEFINE_ALLOCATOR(ErrorConstructor);
|
2023-11-19 05:45:05 -03:00
|
|
|
|
2022-08-15 20:20:49 -03:00
|
|
|
ErrorConstructor::ErrorConstructor(Realm& realm)
|
2023-04-12 19:47:15 -03:00
|
|
|
: NativeFunction(realm.vm().names.Error.as_string(), realm.intrinsics().function_prototype())
|
2020-04-01 15:42:07 -03:00
|
|
|
{
|
2020-06-20 10:40:48 -03:00
|
|
|
}
|
|
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
void ErrorConstructor::initialize(Realm& realm)
|
2020-06-20 10:40:48 -03:00
|
|
|
{
|
2020-10-13 18:49:19 -03:00
|
|
|
auto& vm = this->vm();
|
2023-08-07 03:41:28 -03:00
|
|
|
Base::initialize(realm);
|
2021-06-18 20:38:41 -03:00
|
|
|
|
|
|
|
|
// 20.5.2.1 Error.prototype, https://tc39.es/ecma262/#sec-error.prototype
|
2022-08-26 20:54:55 -03:00
|
|
|
define_direct_property(vm.names.prototype, realm.intrinsics().error_prototype(), 0);
|
2021-06-18 20:38:41 -03:00
|
|
|
|
2021-07-05 20:15:08 -03:00
|
|
|
define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
|
2024-11-09 13:59:08 -03:00
|
|
|
|
|
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
|
|
|
|
define_native_function(realm, vm.names.isError, is_error, 1, attr);
|
2020-04-01 15:42:07 -03:00
|
|
|
}
|
|
|
|
|
|
2022-02-07 11:38:53 -03:00
|
|
|
// 20.5.1.1 Error ( message [ , options ] ), https://tc39.es/ecma262/#sec-error-message
|
2021-10-20 17:16:30 -03:00
|
|
|
ThrowCompletionOr<Value> ErrorConstructor::call()
|
2020-04-01 15:42:07 -03:00
|
|
|
{
|
2022-02-07 11:38:53 -03:00
|
|
|
// 1. If NewTarget is undefined, let newTarget be the active function object; else let newTarget be NewTarget.
|
2021-10-20 17:16:30 -03:00
|
|
|
return TRY(construct(*this));
|
2020-04-01 15:42:07 -03:00
|
|
|
}
|
|
|
|
|
|
2022-02-07 11:38:53 -03:00
|
|
|
// 20.5.1.1 Error ( message [ , options ] ), https://tc39.es/ecma262/#sec-error-message
|
2024-11-14 12:01:23 -03:00
|
|
|
ThrowCompletionOr<GC::Ref<Object>> ErrorConstructor::construct(FunctionObject& new_target)
|
2020-04-01 15:42:07 -03:00
|
|
|
{
|
2020-09-27 13:45:21 -03:00
|
|
|
auto& vm = this->vm();
|
2021-06-19 21:09:39 -03:00
|
|
|
|
2022-02-07 11:38:53 -03:00
|
|
|
auto message = vm.argument(0);
|
|
|
|
|
auto options = vm.argument(1);
|
|
|
|
|
|
|
|
|
|
// 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%Error.prototype%", « [[ErrorData]] »).
|
2022-12-14 15:34:32 -03:00
|
|
|
auto error = TRY(ordinary_create_from_constructor<Error>(vm, new_target, &Intrinsics::error_prototype));
|
2021-06-11 16:40:08 -03:00
|
|
|
|
2022-02-07 11:38:53 -03:00
|
|
|
// 3. If message is not undefined, then
|
|
|
|
|
if (!message.is_undefined()) {
|
|
|
|
|
// a. Let msg be ? ToString(message).
|
2025-08-07 20:31:52 -03:00
|
|
|
auto msg = TRY(message.to_utf16_string(vm));
|
2022-02-07 11:38:53 -03:00
|
|
|
|
2022-05-02 15:54:39 -03:00
|
|
|
// b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "message", msg).
|
2022-12-06 19:17:27 -03:00
|
|
|
error->create_non_enumerable_data_property_or_throw(vm.names.message, PrimitiveString::create(vm, move(msg)));
|
2020-05-15 08:39:24 -03:00
|
|
|
}
|
2021-06-11 16:40:08 -03:00
|
|
|
|
2022-02-07 11:38:53 -03:00
|
|
|
// 4. Perform ? InstallErrorCause(O, options).
|
|
|
|
|
TRY(error->install_error_cause(options));
|
2021-06-11 16:40:08 -03:00
|
|
|
|
2022-02-07 11:38:53 -03:00
|
|
|
// 5. Return O.
|
2022-12-14 16:18:10 -03:00
|
|
|
return error;
|
2020-04-01 15:42:07 -03:00
|
|
|
}
|
|
|
|
|
|
2023-04-12 19:47:15 -03:00
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
|
2024-11-14 12:01:23 -03:00
|
|
|
GC_DEFINE_ALLOCATOR(ConstructorName); \
|
2023-04-12 19:47:15 -03:00
|
|
|
ConstructorName::ConstructorName(Realm& realm) \
|
|
|
|
|
: NativeFunction(realm.vm().names.ClassName.as_string(), realm.intrinsics().error_constructor()) \
|
|
|
|
|
{ \
|
|
|
|
|
} \
|
|
|
|
|
\
|
2023-08-07 03:41:28 -03:00
|
|
|
void ConstructorName::initialize(Realm& realm) \
|
2023-04-12 19:47:15 -03:00
|
|
|
{ \
|
|
|
|
|
auto& vm = this->vm(); \
|
2023-08-07 03:41:28 -03:00
|
|
|
Base::initialize(realm); \
|
2023-04-12 19:47:15 -03:00
|
|
|
\
|
|
|
|
|
/* 20.5.6.2.1 NativeError.prototype, https://tc39.es/ecma262/#sec-nativeerror.prototype */ \
|
|
|
|
|
define_direct_property(vm.names.prototype, realm.intrinsics().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 */ \
|
2024-11-14 12:01:23 -03:00
|
|
|
ThrowCompletionOr<GC::Ref<Object>> ConstructorName::construct(FunctionObject& new_target) \
|
2023-04-12 19:47:15 -03:00
|
|
|
{ \
|
|
|
|
|
auto& vm = this->vm(); \
|
|
|
|
|
\
|
|
|
|
|
auto message = vm.argument(0); \
|
|
|
|
|
auto options = vm.argument(1); \
|
|
|
|
|
\
|
2024-12-27 20:47:33 -03:00
|
|
|
/* 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%NativeError.prototype%", « [[ErrorData]] »). */ \
|
2023-04-12 19:47:15 -03:00
|
|
|
auto error = TRY(ordinary_create_from_constructor<ClassName>(vm, new_target, &Intrinsics::snake_name##_prototype)); \
|
|
|
|
|
\
|
|
|
|
|
/* 3. If message is not undefined, then */ \
|
|
|
|
|
if (!message.is_undefined()) { \
|
|
|
|
|
/* a. Let msg be ? ToString(message). */ \
|
2025-08-07 20:31:52 -03:00
|
|
|
auto msg = TRY(message.to_utf16_string(vm)); \
|
2023-04-12 19:47:15 -03:00
|
|
|
\
|
|
|
|
|
/* b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "message", msg). */ \
|
|
|
|
|
error->create_non_enumerable_data_property_or_throw(vm.names.message, PrimitiveString::create(vm, move(msg))); \
|
|
|
|
|
} \
|
|
|
|
|
\
|
|
|
|
|
/* 4. Perform ? InstallErrorCause(O, options). */ \
|
|
|
|
|
TRY(error->install_error_cause(options)); \
|
|
|
|
|
\
|
|
|
|
|
/* 5. Return O. */ \
|
|
|
|
|
return error; \
|
2020-04-10 07:42:33 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-11 13:54:42 -03:00
|
|
|
JS_ENUMERATE_NATIVE_ERRORS
|
2020-04-10 09:06:52 -03:00
|
|
|
#undef __JS_ENUMERATE
|
2020-04-10 07:42:33 -03:00
|
|
|
|
2025-10-01 12:58:16 -03:00
|
|
|
// 20.5.2.1 Error.isError ( arg ), https://tc39.es/ecma262/#sec-error.iserror
|
2024-11-09 13:59:08 -03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ErrorConstructor::is_error)
|
|
|
|
|
{
|
2025-10-01 12:58:16 -03:00
|
|
|
// 1. If arg is not an Object, return false.
|
2026-04-04 10:39:01 -03:00
|
|
|
auto object = vm.argument(0).as_if<Object>();
|
|
|
|
|
if (!object)
|
|
|
|
|
return false;
|
|
|
|
|
|
2025-10-01 12:58:16 -03:00
|
|
|
// 2. If arg does not have an [[ErrorData]] internal slot, return false.
|
2026-04-04 10:39:01 -03:00
|
|
|
if (!object->has_error_data())
|
|
|
|
|
return false;
|
|
|
|
|
|
2025-10-01 12:58:16 -03:00
|
|
|
// 3. Return true.
|
2026-04-04 10:39:01 -03:00
|
|
|
return true;
|
2024-11-09 13:59:08 -03:00
|
|
|
}
|
|
|
|
|
|
2020-04-01 15:42:07 -03:00
|
|
|
}
|