mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-15 23:09:05 +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)
|
GC::Ref<Error> Error::create(Realm& realm, String message)
|
||||||
{
|
{
|
||||||
auto& vm = realm.vm();
|
|
||||||
auto error = Error::create(realm);
|
auto error = Error::create(realm);
|
||||||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
error->set_message(move(message));
|
||||||
error->define_direct_property(vm.names.message, PrimitiveString::create(vm, move(message)), attr);
|
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,6 +75,14 @@ ThrowCompletionOr<void> Error::install_error_cause(Value options)
|
||||||
return {};
|
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()
|
void Error::populate_stack()
|
||||||
{
|
{
|
||||||
auto stack_trace = vm().stack_trace();
|
auto stack_trace = vm().stack_trace();
|
||||||
|
@ -156,30 +162,28 @@ String Error::stack_string(CompactTraceback compact) const
|
||||||
return MUST(stack_string_builder.to_string());
|
return MUST(stack_string_builder.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
|
||||||
GC_DEFINE_ALLOCATOR(ClassName); \
|
GC_DEFINE_ALLOCATOR(ClassName); \
|
||||||
GC::Ref<ClassName> ClassName::create(Realm& realm) \
|
GC::Ref<ClassName> ClassName::create(Realm& realm) \
|
||||||
{ \
|
{ \
|
||||||
return realm.create<ClassName>(realm.intrinsics().snake_name##_prototype()); \
|
return realm.create<ClassName>(realm.intrinsics().snake_name##_prototype()); \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
GC::Ref<ClassName> ClassName::create(Realm& realm, String message) \
|
GC::Ref<ClassName> ClassName::create(Realm& realm, String message) \
|
||||||
{ \
|
{ \
|
||||||
auto& vm = realm.vm(); \
|
auto error = ClassName::create(realm); \
|
||||||
auto error = ClassName::create(realm); \
|
error->set_message(move(message)); \
|
||||||
u8 attr = Attribute::Writable | Attribute::Configurable; \
|
return error; \
|
||||||
error->define_direct_property(vm.names.message, PrimitiveString::create(vm, move(message)), attr); \
|
} \
|
||||||
return error; \
|
\
|
||||||
} \
|
GC::Ref<ClassName> ClassName::create(Realm& realm, StringView message) \
|
||||||
\
|
{ \
|
||||||
GC::Ref<ClassName> ClassName::create(Realm& realm, StringView message) \
|
return create(realm, MUST(String::from_utf8(message))); \
|
||||||
{ \
|
} \
|
||||||
return create(realm, MUST(String::from_utf8(message))); \
|
\
|
||||||
} \
|
ClassName::ClassName(Object& prototype) \
|
||||||
\
|
: Error(prototype) \
|
||||||
ClassName::ClassName(Object& prototype) \
|
{ \
|
||||||
: Error(prototype) \
|
|
||||||
{ \
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JS_ENUMERATE_NATIVE_ERRORS
|
JS_ENUMERATE_NATIVE_ERRORS
|
||||||
|
|
|
@ -41,6 +41,8 @@ public:
|
||||||
|
|
||||||
ThrowCompletionOr<void> install_error_cause(Value options);
|
ThrowCompletionOr<void> install_error_cause(Value options);
|
||||||
|
|
||||||
|
void set_message(String);
|
||||||
|
|
||||||
Vector<TracebackFrame, 32> const& traceback() const { return m_traceback; }
|
Vector<TracebackFrame, 32> const& traceback() const { return m_traceback; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue