1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:27:35 +00:00

LibJS: Add SuppressedError{, Prototype, Constructor}

This commit is contained in:
davidot 2022-12-06 02:10:01 +01:00 committed by Linus Groh
parent 0d8bab82f0
commit 3353cf68f1
14 changed files with 300 additions and 0 deletions

View file

@ -489,6 +489,7 @@ namespace JS {
P(substring) \
P(subtract) \
P(sup) \
P(suppressed) \
P(supportedLocalesOf) \
P(supportedValuesOf) \
P(symmetricDifference) \

View file

@ -63,6 +63,7 @@
#include <LibJS/Runtime/Shape.h>
#include <LibJS/Runtime/StringConstructor.h>
#include <LibJS/Runtime/StringPrototype.h>
#include <LibJS/Runtime/SuppressedErrorConstructor.h>
#include <LibJS/Runtime/SymbolConstructor.h>
#include <LibJS/Runtime/Temporal/CalendarConstructor.h>
#include <LibJS/Runtime/Temporal/DurationConstructor.h>
@ -154,6 +155,7 @@ Object& set_default_global_bindings(Realm& realm)
global.define_intrinsic_accessor(vm.names.Set, attr, [](auto& realm) -> Value { return realm.intrinsics().set_constructor(); });
global.define_intrinsic_accessor(vm.names.ShadowRealm, attr, [](auto& realm) -> Value { return realm.intrinsics().shadow_realm_constructor(); });
global.define_intrinsic_accessor(vm.names.String, attr, [](auto& realm) -> Value { return realm.intrinsics().string_constructor(); });
global.define_intrinsic_accessor(vm.names.SuppressedError, attr, [](auto& realm) -> Value { return realm.intrinsics().suppressed_error_constructor(); });
global.define_intrinsic_accessor(vm.names.Symbol, attr, [](auto& realm) -> Value { return realm.intrinsics().symbol_constructor(); });
global.define_intrinsic_accessor(vm.names.SyntaxError, attr, [](auto& realm) -> Value { return realm.intrinsics().syntax_error_constructor(); });
global.define_intrinsic_accessor(vm.names.TypeError, attr, [](auto& realm) -> Value { return realm.intrinsics().type_error_constructor(); });

View file

@ -89,6 +89,8 @@
#include <LibJS/Runtime/StringConstructor.h>
#include <LibJS/Runtime/StringIteratorPrototype.h>
#include <LibJS/Runtime/StringPrototype.h>
#include <LibJS/Runtime/SuppressedErrorConstructor.h>
#include <LibJS/Runtime/SuppressedErrorPrototype.h>
#include <LibJS/Runtime/SymbolConstructor.h>
#include <LibJS/Runtime/SymbolPrototype.h>
#include <LibJS/Runtime/Temporal/CalendarConstructor.h>

View file

@ -0,0 +1,23 @@
/*
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/SuppressedError.h>
namespace JS {
NonnullGCPtr<SuppressedError> SuppressedError::create(Realm& realm)
{
return *realm.heap().allocate<SuppressedError>(realm, *realm.intrinsics().suppressed_error_prototype());
}
SuppressedError::SuppressedError(Object& prototype)
: Error(prototype)
{
}
}

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/Error.h>
namespace JS {
class SuppressedError : public Error {
JS_OBJECT(SuppressedError, Error);
public:
static NonnullGCPtr<SuppressedError> create(Realm&);
virtual ~SuppressedError() override = default;
private:
explicit SuppressedError(Object& prototype);
};
}

View file

@ -0,0 +1,74 @@
/*
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/ErrorConstructor.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/SuppressedError.h>
#include <LibJS/Runtime/SuppressedErrorConstructor.h>
namespace JS {
SuppressedErrorConstructor::SuppressedErrorConstructor(Realm& realm)
: NativeFunction(static_cast<Object&>(*realm.intrinsics().error_constructor()))
{
}
void SuppressedErrorConstructor::initialize(Realm& realm)
{
auto& vm = this->vm();
NativeFunction::initialize(realm);
// 10.1.4.2.1 SuppressedError.prototype, https://tc39.es/proposal-explicit-resource-management/#sec-suppressederror.prototype
define_direct_property(vm.names.prototype, realm.intrinsics().suppressed_error_prototype(), 0);
define_direct_property(vm.names.length, Value(3), Attribute::Configurable);
}
// 10.1.4.1.1 SuppressedError ( error, suppressed, message [ , options ] ), https://tc39.es/proposal-explicit-resource-management/#sec-suppressederror
ThrowCompletionOr<Value> SuppressedErrorConstructor::call()
{
// 1. If NewTarget is undefined, let newTarget be the active function object; else let newTarget be NewTarget.
return TRY(construct(*this));
}
// 10.1.4.1.1 SuppressedError ( error, suppressed, message [ , options ] ), https://tc39.es/proposal-explicit-resource-management/#sec-suppressederror
ThrowCompletionOr<NonnullGCPtr<Object>> SuppressedErrorConstructor::construct(FunctionObject& new_target)
{
auto& vm = this->vm();
auto error = vm.argument(0);
auto suppressed = vm.argument(1);
auto message = vm.argument(2);
auto options = vm.argument(3);
// 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%SuppressedError.prototype%", « [[ErrorData]] »).
auto suppressed_error = TRY(ordinary_create_from_constructor<SuppressedError>(vm, new_target, &Intrinsics::suppressed_error_prototype));
// 3. If message is not undefined, then
if (!message.is_undefined()) {
// a. Let msg be ? ToString(message).
auto msg = TRY(message.to_string(vm));
// b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "message", msg).
suppressed_error->create_non_enumerable_data_property_or_throw(vm.names.message, PrimitiveString::create(vm, move(msg)));
}
// 4. Perform ? InstallErrorCause(O, options).
TRY(suppressed_error->install_error_cause(options));
// 5. Perform ! DefinePropertyOrThrow(O, "error", PropertyDescriptor { [[Configurable]]: true, [[Enumerable]]: false, [[Writable]]: true, [[Value]]: error }).
MUST(suppressed_error->define_property_or_throw(vm.names.error, { .value = error, .writable = true, .enumerable = false, .configurable = true }));
// 6. Perform ! DefinePropertyOrThrow(O, "suppressed", PropertyDescriptor { [[Configurable]]: true, [[Enumerable]]: false, [[Writable]]: true, [[Value]]: suppressed }).
MUST(suppressed_error->define_property_or_throw(vm.names.suppressed, { .value = suppressed, .writable = true, .enumerable = false, .configurable = true }));
// 7. Return O.
return suppressed_error;
}
}

View file

@ -0,0 +1,28 @@
/*
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/NativeFunction.h>
namespace JS {
class SuppressedErrorConstructor final : public NativeFunction {
JS_OBJECT(SuppressedErrorConstructor, NativeFunction);
public:
virtual void initialize(Realm&) override;
virtual ~SuppressedErrorConstructor() override = default;
virtual ThrowCompletionOr<Value> call() override;
virtual ThrowCompletionOr<NonnullGCPtr<Object>> construct(FunctionObject& new_target) override;
private:
explicit SuppressedErrorConstructor(Realm&);
virtual bool has_constructor() const override { return true; }
};
}

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/PrimitiveString.h>
#include <LibJS/Runtime/SuppressedErrorPrototype.h>
namespace JS {
SuppressedErrorPrototype::SuppressedErrorPrototype(Realm& realm)
: Object(ConstructWithPrototypeTag::Tag, *realm.intrinsics().error_prototype())
{
}
void SuppressedErrorPrototype::initialize(Realm& realm)
{
auto& vm = this->vm();
Object::initialize(realm);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_direct_property(vm.names.name, PrimitiveString::create(vm, "SuppressedError"), attr);
define_direct_property(vm.names.message, PrimitiveString::create(vm, ""), attr);
}
}

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/Object.h>
namespace JS {
class SuppressedErrorPrototype final : public Object {
JS_OBJECT(SuppressedErrorPrototype, Object);
public:
virtual void initialize(Realm&) override;
virtual ~SuppressedErrorPrototype() override = default;
private:
explicit SuppressedErrorPrototype(Realm&);
};
}