2020-03-24 10:37:39 -03:00
|
|
|
/*
|
2025-10-29 04:28:41 -03:00
|
|
|
* Copyright (c) 2020-2025, Andreas Kling <andreas@ladybird.org>
|
2023-04-12 19:47:15 -03:00
|
|
|
* Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
|
2020-03-24 10:37:39 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-24 10:37:39 -03:00
|
|
|
*/
|
|
|
|
|
|
2021-10-02 12:03:45 -03:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2020-03-24 10:37:39 -03:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
2020-04-17 14:31:48 -03:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-03-24 10:37:39 -03:00
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC_DEFINE_ALLOCATOR(Error);
|
2023-11-19 05:45:05 -03:00
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ref<Error> Error::create(Realm& realm)
|
2021-06-11 16:40:08 -03:00
|
|
|
{
|
2024-11-13 13:50:17 -03:00
|
|
|
return realm.create<Error>(realm.intrinsics().error_prototype());
|
2021-06-11 16:40:08 -03:00
|
|
|
}
|
|
|
|
|
|
2025-08-07 20:31:52 -03:00
|
|
|
GC::Ref<Error> Error::create(Realm& realm, Utf16String message)
|
2020-04-17 14:31:48 -03:00
|
|
|
{
|
2022-12-13 17:49:50 -03:00
|
|
|
auto error = Error::create(realm);
|
2025-07-17 10:48:16 -03:00
|
|
|
error->set_message(move(message));
|
2021-04-11 19:08:28 -03:00
|
|
|
return error;
|
2020-04-17 14:31:48 -03:00
|
|
|
}
|
|
|
|
|
|
2026-06-21 14:03:10 -03:00
|
|
|
GC::Ref<Error> Error::create(Realm& realm, Utf16View message)
|
|
|
|
|
{
|
|
|
|
|
auto error = Error::create(realm);
|
|
|
|
|
error->set_message(message);
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 10:39:01 -03:00
|
|
|
Utf16String Error::stack_string(CompactTraceback compact) const
|
|
|
|
|
{
|
|
|
|
|
return ErrorData::stack_string(compact);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 19:08:28 -03:00
|
|
|
Error::Error(Object& prototype)
|
2022-12-14 08:17:58 -03:00
|
|
|
: Object(ConstructWithPrototypeTag::Tag, prototype)
|
2026-04-04 10:39:01 -03:00
|
|
|
, ErrorData(prototype.vm())
|
2020-03-24 10:37:39 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-30 06:21:50 -03:00
|
|
|
void Error::visit_edges(Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
2026-04-04 10:39:01 -03:00
|
|
|
ErrorData::visit_edges(visitor);
|
2025-11-30 06:21:50 -03:00
|
|
|
}
|
|
|
|
|
|
2026-05-05 19:19:35 -03:00
|
|
|
size_t Error::external_memory_size() const
|
|
|
|
|
{
|
|
|
|
|
return Object::external_memory_size() + ErrorData::external_memory_size();
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-21 10:34:06 -03:00
|
|
|
// 20.5.8.1 InstallErrorCause ( O, options ), https://tc39.es/ecma262/#sec-installerrorcause
|
2021-10-02 12:03:45 -03:00
|
|
|
ThrowCompletionOr<void> Error::install_error_cause(Value options)
|
2021-06-26 15:06:55 -03:00
|
|
|
{
|
|
|
|
|
auto& vm = this->vm();
|
2021-10-02 12:03:45 -03:00
|
|
|
|
|
|
|
|
// 1. If Type(options) is Object and ? HasProperty(options, "cause") is true, then
|
2021-10-02 22:00:39 -03:00
|
|
|
if (options.is_object() && TRY(options.as_object().has_property(vm.names.cause))) {
|
2021-10-02 12:03:45 -03:00
|
|
|
// a. Let cause be ? Get(options, "cause").
|
2021-10-02 19:52:27 -03:00
|
|
|
auto cause = TRY(options.as_object().get(vm.names.cause));
|
2021-10-02 12:03:45 -03:00
|
|
|
|
2022-05-02 15:54:39 -03:00
|
|
|
// b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "cause", cause).
|
|
|
|
|
create_non_enumerable_data_property_or_throw(vm.names.cause, cause);
|
2021-10-02 12:03:45 -03:00
|
|
|
}
|
|
|
|
|
|
2022-05-02 15:54:39 -03:00
|
|
|
// 2. Return unused.
|
2021-10-02 12:03:45 -03:00
|
|
|
return {};
|
2021-06-26 15:06:55 -03:00
|
|
|
}
|
|
|
|
|
|
2025-08-07 20:31:52 -03:00
|
|
|
void Error::set_message(Utf16String message)
|
2025-07-17 10:48:16 -03:00
|
|
|
{
|
|
|
|
|
auto& vm = this->vm();
|
|
|
|
|
|
|
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
|
|
|
|
define_direct_property(vm.names.message, PrimitiveString::create(vm, move(message)), attr);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-21 14:03:10 -03:00
|
|
|
void Error::set_message(Utf16View message)
|
|
|
|
|
{
|
|
|
|
|
auto& vm = this->vm();
|
|
|
|
|
|
|
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
|
|
|
|
define_direct_property(vm.names.message, PrimitiveString::create(vm, message), attr);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-17 10:48:16 -03:00
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
|
|
|
|
|
GC_DEFINE_ALLOCATOR(ClassName); \
|
|
|
|
|
GC::Ref<ClassName> ClassName::create(Realm& realm) \
|
|
|
|
|
{ \
|
|
|
|
|
return realm.create<ClassName>(realm.intrinsics().snake_name##_prototype()); \
|
|
|
|
|
} \
|
|
|
|
|
\
|
2025-08-07 20:31:52 -03:00
|
|
|
GC::Ref<ClassName> ClassName::create(Realm& realm, Utf16String message) \
|
2025-07-17 10:48:16 -03:00
|
|
|
{ \
|
|
|
|
|
auto error = ClassName::create(realm); \
|
|
|
|
|
error->set_message(move(message)); \
|
|
|
|
|
return error; \
|
2026-06-21 14:03:10 -03:00
|
|
|
} \
|
|
|
|
|
\
|
|
|
|
|
GC::Ref<ClassName> ClassName::create(Realm& realm, Utf16View message) \
|
|
|
|
|
{ \
|
|
|
|
|
auto error = ClassName::create(realm); \
|
|
|
|
|
error->set_message(message); \
|
|
|
|
|
return error; \
|
2025-07-17 10:48:16 -03:00
|
|
|
} \
|
|
|
|
|
\
|
|
|
|
|
ClassName::ClassName(Object& prototype) \
|
|
|
|
|
: Error(prototype) \
|
|
|
|
|
{ \
|
2021-04-11 19:08:28 -03:00
|
|
|
}
|
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
|
|
|
|
2020-03-24 10:37:39 -03:00
|
|
|
}
|