2020-03-24 13:37:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2021-04-22 20:51:19 +00:00
|
|
|
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
|
2020-03-24 13:37:39 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-24 13:37:39 +00:00
|
|
|
*/
|
|
|
|
|
2021-10-02 15:03:45 +00:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2020-03-24 13:37:39 +00:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
2020-04-17 17:31:48 +00:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-03-24 13:37:39 +00:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2021-06-11 19:40:08 +00:00
|
|
|
Error* Error::create(GlobalObject& global_object)
|
|
|
|
{
|
|
|
|
return global_object.heap().allocate<Error>(global_object, *global_object.error_prototype());
|
|
|
|
}
|
|
|
|
|
|
|
|
Error* Error::create(GlobalObject& global_object, String const& message)
|
2020-04-17 17:31:48 +00:00
|
|
|
{
|
2021-04-11 22:08:28 +00:00
|
|
|
auto& vm = global_object.vm();
|
2021-06-11 19:40:08 +00:00
|
|
|
auto* error = Error::create(global_object);
|
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
2021-07-05 23:15:08 +00:00
|
|
|
error->define_direct_property(vm.names.message, js_string(vm, message), attr);
|
2021-04-11 22:08:28 +00:00
|
|
|
return error;
|
2020-04-17 17:31:48 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 22:08:28 +00:00
|
|
|
Error::Error(Object& prototype)
|
2020-06-23 15:21:53 +00:00
|
|
|
: Object(prototype)
|
2020-03-24 13:37:39 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-26 18:06:55 +00:00
|
|
|
// 20.5.8.1 InstallErrorCause ( O, options ), https://tc39.es/proposal-error-cause/#sec-errorobjects-install-error-cause
|
2021-10-02 15:03:45 +00:00
|
|
|
ThrowCompletionOr<void> Error::install_error_cause(Value options)
|
2021-06-26 18:06:55 +00:00
|
|
|
{
|
|
|
|
auto& vm = this->vm();
|
2021-10-02 15:03:45 +00:00
|
|
|
|
|
|
|
// 1. If Type(options) is Object and ? HasProperty(options, "cause") is true, then
|
2021-06-26 18:06:55 +00:00
|
|
|
if (!options.is_object())
|
2021-10-02 15:03:45 +00:00
|
|
|
return {};
|
|
|
|
auto has_property = options.as_object().has_property(vm.names.cause);
|
|
|
|
if (auto* exception = vm.exception())
|
|
|
|
return throw_completion(exception->value());
|
|
|
|
if (has_property) {
|
|
|
|
// a. Let cause be ? Get(options, "cause").
|
2021-10-02 22:52:27 +00:00
|
|
|
auto cause = TRY(options.as_object().get(vm.names.cause));
|
2021-10-02 15:03:45 +00:00
|
|
|
|
|
|
|
// b. Perform ! CreateNonEnumerableDataPropertyOrThrow(O, "cause", cause).
|
2021-10-03 00:22:32 +00:00
|
|
|
MUST(create_non_enumerable_data_property_or_throw(vm.names.cause, cause));
|
2021-10-02 15:03:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return NormalCompletion(undefined).
|
|
|
|
return {};
|
2021-06-26 18:06:55 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 19:40:08 +00:00
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
|
|
|
|
ClassName* ClassName::create(GlobalObject& global_object) \
|
|
|
|
{ \
|
|
|
|
return global_object.heap().allocate<ClassName>(global_object, *global_object.snake_name##_prototype()); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
ClassName* ClassName::create(GlobalObject& global_object, String const& message) \
|
|
|
|
{ \
|
|
|
|
auto& vm = global_object.vm(); \
|
|
|
|
auto* error = ClassName::create(global_object); \
|
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable; \
|
2021-07-05 23:15:08 +00:00
|
|
|
error->define_direct_property(vm.names.message, js_string(vm, message), attr); \
|
2021-06-11 19:40:08 +00:00
|
|
|
return error; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
ClassName::ClassName(Object& prototype) \
|
|
|
|
: Error(prototype) \
|
|
|
|
{ \
|
2021-04-11 22:08:28 +00:00
|
|
|
}
|
2020-04-10 10:42:33 +00:00
|
|
|
|
2021-06-11 16:54:42 +00:00
|
|
|
JS_ENUMERATE_NATIVE_ERRORS
|
2020-04-10 12:06:52 +00:00
|
|
|
#undef __JS_ENUMERATE
|
2020-04-10 10:42:33 +00:00
|
|
|
|
2020-03-24 13:37:39 +00:00
|
|
|
}
|