LibJS: Allow assigning an error message after Error object creation

This will be used by structured deserialization in LibWeb.
This commit is contained in:
Timothy Flynn 2025-07-17 09:48:16 -04:00 committed by Tim Flynn
commit 3fb4e769d8
Notes: github-actions[bot] 2025-07-18 14:11:17 +00:00
2 changed files with 33 additions and 27 deletions

View file

@ -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();
@ -165,10 +171,8 @@ String Error::stack_string(CompactTraceback compact) const
\
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); \
error->set_message(move(message)); \
return error; \
} \
\

View file

@ -41,6 +41,8 @@ public:
ThrowCompletionOr<void> install_error_cause(Value options);
void set_message(String);
Vector<TracebackFrame, 32> const& traceback() const { return m_traceback; }
protected: