mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-28 11:49:44 +00:00
LibJS: Allow assigning an error message after Error object creation
This will be used by structured deserialization in LibWeb.
This commit is contained in:
parent
fd6d868ae2
commit
3fb4e769d8
Notes:
github-actions[bot]
2025-07-18 14:11:17 +00:00
Author: https://github.com/trflynn89
Commit: 3fb4e769d8
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5492
Reviewed-by: https://github.com/ADKaster
Reviewed-by: https://github.com/shannonbooth
2 changed files with 33 additions and 27 deletions
|
@ -41,10 +41,8 @@ GC::Ref<Error> Error::create(Realm& realm)
|
|||
|
||||
GC::Ref<Error> Error::create(Realm& realm, String message)
|
||||
{
|
||||
auto& vm = realm.vm();
|
||||
auto error = Error::create(realm);
|
||||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||
error->define_direct_property(vm.names.message, PrimitiveString::create(vm, move(message)), attr);
|
||||
error->set_message(move(message));
|
||||
return error;
|
||||
}
|
||||
|
||||
|
@ -77,6 +75,14 @@ ThrowCompletionOr<void> Error::install_error_cause(Value options)
|
|||
return {};
|
||||
}
|
||||
|
||||
void Error::set_message(String message)
|
||||
{
|
||||
auto& vm = this->vm();
|
||||
|
||||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||
define_direct_property(vm.names.message, PrimitiveString::create(vm, move(message)), attr);
|
||||
}
|
||||
|
||||
void Error::populate_stack()
|
||||
{
|
||||
auto stack_trace = vm().stack_trace();
|
||||
|
@ -156,30 +162,28 @@ String Error::stack_string(CompactTraceback compact) const
|
|||
return MUST(stack_string_builder.to_string());
|
||||
}
|
||||
|
||||
#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()); \
|
||||
} \
|
||||
\
|
||||
GC::Ref<ClassName> ClassName::create(Realm& realm, String message) \
|
||||
{ \
|
||||
auto& vm = realm.vm(); \
|
||||
auto error = ClassName::create(realm); \
|
||||
u8 attr = Attribute::Writable | Attribute::Configurable; \
|
||||
error->define_direct_property(vm.names.message, PrimitiveString::create(vm, move(message)), attr); \
|
||||
return error; \
|
||||
} \
|
||||
\
|
||||
GC::Ref<ClassName> ClassName::create(Realm& realm, StringView message) \
|
||||
{ \
|
||||
return create(realm, MUST(String::from_utf8(message))); \
|
||||
} \
|
||||
\
|
||||
ClassName::ClassName(Object& prototype) \
|
||||
: Error(prototype) \
|
||||
{ \
|
||||
#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()); \
|
||||
} \
|
||||
\
|
||||
GC::Ref<ClassName> ClassName::create(Realm& realm, String message) \
|
||||
{ \
|
||||
auto error = ClassName::create(realm); \
|
||||
error->set_message(move(message)); \
|
||||
return error; \
|
||||
} \
|
||||
\
|
||||
GC::Ref<ClassName> ClassName::create(Realm& realm, StringView message) \
|
||||
{ \
|
||||
return create(realm, MUST(String::from_utf8(message))); \
|
||||
} \
|
||||
\
|
||||
ClassName::ClassName(Object& prototype) \
|
||||
: Error(prototype) \
|
||||
{ \
|
||||
}
|
||||
|
||||
JS_ENUMERATE_NATIVE_ERRORS
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue