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

LibJS: Add DisposableStack{, Prototype, Constructor}

Since the async parts of the spec are not stage 3 at this point we don't
add AsyncDisposableStack.
This commit is contained in:
davidot 2022-12-21 11:48:14 +01:00 committed by Linus Groh
parent bff038411a
commit 6255ca4a42
22 changed files with 868 additions and 0 deletions

View file

@ -65,6 +65,7 @@ namespace JS {
P(acos) \
P(acosh) \
P(add) \
P(adopt) \
P(all) \
P(allSettled) \
P(anchor) \
@ -143,6 +144,7 @@ namespace JS {
P(debug) \
P(decodeURI) \
P(decodeURIComponent) \
P(defer) \
P(defineProperties) \
P(defineProperty) \
P(deleteProperty) \
@ -151,6 +153,7 @@ namespace JS {
P(difference) \
P(direction) \
P(disambiguation) \
P(disposed) \
P(done) \
P(dotAll) \
P(encodeURI) \
@ -365,6 +368,7 @@ namespace JS {
P(months) \
P(monthsDisplay) \
P(monthsInYear) \
P(move) \
P(multiline) \
P(name) \
P(nanosecond) \
@ -555,6 +559,7 @@ namespace JS {
P(unshift) \
P(until) \
P(usage) \
P(use) \
P(useGrouping) \
P(value) \
P(valueOf) \

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/DisposableStack.h>
namespace JS {
DisposableStack::DisposableStack(Vector<DisposableResource> stack, Object& prototype)
: Object(ConstructWithPrototypeTag::Tag, prototype)
, m_disposable_resource_stack(move(stack))
{
}
void DisposableStack::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
for (auto& resource : m_disposable_resource_stack) {
visitor.visit(resource.resource_value);
visitor.visit(resource.dispose_method);
}
}
}

View file

@ -0,0 +1,40 @@
/*
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Object.h>
namespace JS {
class DisposableStack final : public Object {
JS_OBJECT(DisposableStack, Object);
public:
virtual ~DisposableStack() override = default;
enum class DisposableState {
Pending,
Disposed
};
[[nodiscard]] DisposableState disposable_state() const { return m_state; }
[[nodiscard]] Vector<DisposableResource> const& disposable_resource_stack() const { return m_disposable_resource_stack; }
[[nodiscard]] Vector<DisposableResource>& disposable_resource_stack() { return m_disposable_resource_stack; }
void set_disposed() { m_state = DisposableState::Disposed; }
private:
DisposableStack(Vector<DisposableResource> stack, Object& prototype);
virtual void visit_edges(Visitor& visitor) override;
Vector<DisposableResource> m_disposable_resource_stack;
DisposableState m_state { DisposableState::Pending };
};
}

View file

@ -0,0 +1,50 @@
/*
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/DisposableStack.h>
#include <LibJS/Runtime/DisposableStackConstructor.h>
namespace JS {
DisposableStackConstructor::DisposableStackConstructor(Realm& realm)
: NativeFunction(realm.vm().names.DisposableStack.as_string(), *realm.intrinsics().function_prototype())
{
}
void DisposableStackConstructor::initialize(Realm& realm)
{
auto& vm = this->vm();
NativeFunction::initialize(realm);
// 26.2.2.1 DisposableStack.prototype, https://tc39.es/ecma262/#sec-finalization-registry.prototype
define_direct_property(vm.names.prototype, realm.intrinsics().disposable_stack_prototype(), 0);
define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
}
// 11.3.1.1 DisposableStack ( ), https://tc39.es/proposal-explicit-resource-management/#sec-disposablestack
ThrowCompletionOr<Value> DisposableStackConstructor::call()
{
auto& vm = this->vm();
// 1. If NewTarget is undefined, throw a TypeError exception.
return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.DisposableStack);
}
// 11.3.1.1 DisposableStack ( ), https://tc39.es/proposal-explicit-resource-management/#sec-disposablestack
ThrowCompletionOr<NonnullGCPtr<Object>> DisposableStackConstructor::construct(FunctionObject& new_target)
{
auto& vm = this->vm();
// 2. Let disposableStack be ? OrdinaryCreateFromConstructor(NewTarget, "%DisposableStack.prototype%", « [[DisposableState]], [[DisposableResourceStack]] »).
// 3. Set disposableStack.[[DisposableState]] to pending.
// 4. Set disposableStack.[[DisposableResourceStack]] to a new empty List.
// 5. Return disposableStack.
return TRY(ordinary_create_from_constructor<DisposableStack>(vm, new_target, &Intrinsics::disposable_stack_prototype, Vector<DisposableResource> {}));
}
}

View file

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

View file

@ -0,0 +1,206 @@
/*
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/DisposableStack.h>
#include <LibJS/Runtime/DisposableStackConstructor.h>
#include <LibJS/Runtime/DisposableStackPrototype.h>
#include <LibJS/Runtime/NativeFunction.h>
namespace JS {
DisposableStackPrototype::DisposableStackPrototype(Realm& realm)
: PrototypeObject(*realm.intrinsics().object_prototype())
{
}
void DisposableStackPrototype::initialize(Realm& realm)
{
auto& vm = this->vm();
Object::initialize(realm);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_accessor(realm, vm.names.disposed, disposed_getter, {}, attr);
define_native_function(realm, vm.names.dispose, dispose, 0, attr);
define_native_function(realm, vm.names.use, use, 1, attr);
define_native_function(realm, vm.names.adopt, adopt, 2, attr);
define_native_function(realm, vm.names.defer, defer, 1, attr);
define_native_function(realm, vm.names.move, move_, 0, attr);
// 11.3.3.7 DisposableStack.prototype [ @@dispose ] (), https://tc39.es/proposal-explicit-resource-management/#sec-disposablestack.prototype-@@dispose
define_direct_property(*vm.well_known_symbol_dispose(), get_without_side_effects(vm.names.dispose), attr);
// 11.3.3.8 DisposableStack.prototype [ @@toStringTag ], https://tc39.es/proposal-explicit-resource-management/#sec-disposablestack.prototype-@@toStringTag
define_direct_property(*vm.well_known_symbol_to_string_tag(), PrimitiveString::create(vm, vm.names.DisposableStack.as_string()), Attribute::Configurable);
}
// 11.3.3.1 get DisposableStack.prototype.disposed, https://tc39.es/proposal-explicit-resource-management/#sec-get-disposablestack.prototype.disposed
JS_DEFINE_NATIVE_FUNCTION(DisposableStackPrototype::disposed_getter)
{
// 1. Let disposableStack be the this value.
// 2. Perform ? RequireInternalSlot(disposableStack, [[DisposableState]]).
auto* disposable_stack = TRY(typed_this_object(vm));
// 3. If disposableStack.[[DisposableState]] is disposed, return true.
if (disposable_stack->disposable_state() == DisposableStack::DisposableState::Disposed)
return Value(true);
// 4. Otherwise, return false.
return Value(false);
}
// 11.3.3.2 DisposableStack.prototype.dispose (), https://tc39.es/proposal-explicit-resource-management/#sec-disposablestack.prototype.dispose
JS_DEFINE_NATIVE_FUNCTION(DisposableStackPrototype::dispose)
{
// 1. Let disposableStack be the this value.
// 2. Perform ? RequireInternalSlot(disposableStack, [[DisposableState]]).
auto* disposable_stack = TRY(typed_this_object(vm));
// 3. If disposableStack.[[DisposableState]] is disposed, return undefined.
if (disposable_stack->disposable_state() == DisposableStack::DisposableState::Disposed)
return js_undefined();
// 4. Set disposableStack.[[DisposableState]] to disposed.
disposable_stack->set_disposed();
// 5. Return DisposeResources(disposableStack, NormalCompletion(undefined)).
return TRY(dispose_resources(vm, disposable_stack->disposable_resource_stack(), Completion { js_undefined() }));
}
// 11.3.3.3 DisposableStack.prototype.use( value ), https://tc39.es/proposal-explicit-resource-management/#sec-disposablestack.prototype.use
JS_DEFINE_NATIVE_FUNCTION(DisposableStackPrototype::use)
{
auto value = vm.argument(0);
// 1. Let disposableStack be the this value.
// 2. Perform ? RequireInternalSlot(disposableStack, [[DisposableState]]).
auto* disposable_stack = TRY(typed_this_object(vm));
// 3. If disposableStack.[[DisposableState]] is disposed, throw a ReferenceError exception.
if (disposable_stack->disposable_state() == DisposableStack::DisposableState::Disposed)
return vm.throw_completion<ReferenceError>(ErrorType::DisposableStackAlreadyDisposed);
// 4. If value is neither null nor undefined, then
if (!value.is_nullish()) {
// a. If Type(value) is not Object, throw a TypeError exception.
if (!value.is_object())
return vm.throw_completion<TypeError>(ErrorType::NotAnObject, value.to_string_without_side_effects());
// FIXME: This should be TRY in the spec
// b. Let method be GetDisposeMethod(value, sync-dispose).
auto method = TRY(get_dispose_method(vm, value, Environment::InitializeBindingHint::SyncDispose));
// c. If method is undefined, then
if (!method.ptr()) {
// i. Throw a TypeError exception.
return vm.throw_completion<TypeError>(ErrorType::NoDisposeMethod, value.to_string_without_side_effects());
}
// d. Else,
// i. Perform ? AddDisposableResource(disposableStack, value, sync-dispose, method).
add_disposable_resource(vm, disposable_stack->disposable_resource_stack(), value, Environment::InitializeBindingHint::SyncDispose, method);
}
// 5. Return value.
return value;
}
// 11.3.3.4 DisposableStack.prototype.adopt( value, onDispose ), https://tc39.es/proposal-explicit-resource-management/#sec-disposablestack.prototype.adopt
JS_DEFINE_NATIVE_FUNCTION(DisposableStackPrototype::adopt)
{
auto& realm = *vm.current_realm();
auto value = vm.argument(0);
auto on_dispose = vm.argument(1);
// 1. Let disposableStack be the this value.
// 2. Perform ? RequireInternalSlot(disposableStack, [[DisposableState]]).
auto* disposable_stack = TRY(typed_this_object(vm));
// 3. If disposableStack.[[DisposableState]] is disposed, throw a ReferenceError exception.
if (disposable_stack->disposable_state() == DisposableStack::DisposableState::Disposed)
return vm.throw_completion<ReferenceError>(ErrorType::DisposableStackAlreadyDisposed);
// 4. If IsCallable(onDispose) is false, throw a TypeError exception.
if (!on_dispose.is_function())
return vm.throw_completion<TypeError>(ErrorType::NotAFunction, on_dispose.to_string_without_side_effects());
// 5. Let F be a new built-in function object as defined in 11.3.3.4.1.
// 6. Set F.[[Argument]] to value.
// 7. Set F.[[OnDisposeCallback]] to onDispose.
// 11.3.3.4.1 DisposableStack Adopt Callback Functions, https://tc39.es/proposal-explicit-resource-management/#sec-disposablestack-adopt-callback-functions
// A DisposableStack adopt callback function is an anonymous built-in function object that has [[Argument]] and [[OnDisposeCallback]] internal slots.
auto function = NativeFunction::create(
realm, [argument = make_handle(value), callback = make_handle(on_dispose)](VM& vm) {
// When a DisposableStack adopt callback function is called, the following steps are taken:
// 1. Let F be the active function object.
// 2. Assert: IsCallable(F.[[OnDisposeCallback]]) is true.
VERIFY(callback.value().is_function());
// 3. Return Call(F.[[OnDisposeCallback]], undefined, « F.[[Argument]] »).
return call(vm, callback.value(), js_undefined(), argument.value());
},
0, "");
// 8. Perform ? AddDisposableResource(disposableStack, undefined, sync-dispose, F).
TRY(add_disposable_resource(vm, disposable_stack->disposable_resource_stack(), js_undefined(), Environment::InitializeBindingHint::SyncDispose, function));
// 9. Return value.
return value;
}
// 11.3.3.5 DisposableStack.prototype.defer( onDispose ), https://tc39.es/proposal-explicit-resource-management/#sec-disposablestack.prototype.defer
JS_DEFINE_NATIVE_FUNCTION(DisposableStackPrototype::defer)
{
auto on_dispose = vm.argument(0);
// 1. Let disposableStack be the this value.
// 2. Perform ? RequireInternalSlot(disposableStack, [[DisposableState]]).
auto* disposable_stack = TRY(typed_this_object(vm));
// 3. If disposableStack.[[DisposableState]] is disposed, throw a ReferenceError exception.
if (disposable_stack->disposable_state() == DisposableStack::DisposableState::Disposed)
return vm.throw_completion<ReferenceError>(ErrorType::DisposableStackAlreadyDisposed);
// 4. If IsCallable(onDispose) is false, throw a TypeError exception.
if (!on_dispose.is_function())
return vm.throw_completion<TypeError>(ErrorType::NotAFunction, on_dispose.to_string_without_side_effects());
// 5. Perform ? AddDisposableResource(disposableStack, undefined, sync-dispose, onDispose).
TRY(add_disposable_resource(vm, disposable_stack->disposable_resource_stack(), js_undefined(), Environment::InitializeBindingHint::SyncDispose, &on_dispose.as_function()));
// 6. Return undefined.
return js_undefined();
}
// 11.3.3.6 DisposableStack.prototype.move(), https://tc39.es/proposal-explicit-resource-management/#sec-disposablestack.prototype.move
JS_DEFINE_NATIVE_FUNCTION(DisposableStackPrototype::move_)
{
// 1. Let disposableStack be the this value.
// 2. Perform ? RequireInternalSlot(disposableStack, [[DisposableState]]).
auto* disposable_stack = TRY(typed_this_object(vm));
// 3. If disposableStack.[[DisposableState]] is disposed, throw a ReferenceError exception.
if (disposable_stack->disposable_state() == DisposableStack::DisposableState::Disposed)
return vm.throw_completion<ReferenceError>(ErrorType::DisposableStackAlreadyDisposed);
// 4. Let newDisposableStack be ? OrdinaryCreateFromConstructor(%DisposableStack%, "%DisposableStack.prototype%", « [[DisposableState]], [[DisposableResourceStack]] »).
auto new_disposable_stack = TRY(ordinary_create_from_constructor<DisposableStack>(vm, *vm.current_realm()->intrinsics().disposable_stack_constructor(), &Intrinsics::disposable_stack_prototype, disposable_stack->disposable_resource_stack()));
// 5. Set newDisposableStack.[[DisposableState]] to pending.
// 6. Set newDisposableStack.[[DisposableResourceStack]] to disposableStack.[[DisposableResourceStack]].
// NOTE: Already done in the constructor
// 7. Set disposableStack.[[DisposableResourceStack]] to a new empty List.
disposable_stack->disposable_resource_stack().clear();
// 8. Set disposableStack.[[DisposableState]] to disposed.
disposable_stack->set_disposed();
// 9. Return newDisposableStack.
return new_disposable_stack;
}
}

View file

@ -0,0 +1,32 @@
/*
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Runtime/FinalizationRegistry.h>
#include <LibJS/Runtime/PrototypeObject.h>
namespace JS {
class DisposableStackPrototype final : public PrototypeObject<DisposableStackPrototype, DisposableStack> {
JS_PROTOTYPE_OBJECT(DisposableStackPrototype, DisposableStack, DisposableStack);
public:
virtual void initialize(Realm&) override;
virtual ~DisposableStackPrototype() override = default;
private:
explicit DisposableStackPrototype(Realm&);
JS_DECLARE_NATIVE_FUNCTION(disposed_getter);
JS_DECLARE_NATIVE_FUNCTION(dispose);
JS_DECLARE_NATIVE_FUNCTION(use);
JS_DECLARE_NATIVE_FUNCTION(adopt);
JS_DECLARE_NATIVE_FUNCTION(defer);
JS_DECLARE_NATIVE_FUNCTION(move_);
};
}

View file

@ -32,6 +32,7 @@
M(DescWriteNonWritable, "Cannot write to non-writable property '{}'") \
M(DetachedArrayBuffer, "ArrayBuffer is detached") \
M(DetachKeyMismatch, "Provided detach key {} does not match the ArrayBuffer's detach key {}") \
M(DisposableStackAlreadyDisposed, "DisposableStack already disposed values") \
M(DivisionByZero, "Division by zero") \
M(DynamicImportNotAllowed, "Dynamic Imports are not allowed") \
M(FinalizationRegistrySameTargetAndValue, "Target and held value must not be the same") \

View file

@ -28,6 +28,7 @@
#include <LibJS/Runtime/ConsoleObject.h>
#include <LibJS/Runtime/DataViewConstructor.h>
#include <LibJS/Runtime/DateConstructor.h>
#include <LibJS/Runtime/DisposableStackConstructor.h>
#include <LibJS/Runtime/ErrorConstructor.h>
#include <LibJS/Runtime/FinalizationRegistryConstructor.h>
#include <LibJS/Runtime/FinalizationRegistryPrototype.h>
@ -135,6 +136,7 @@ Object& set_default_global_bindings(Realm& realm)
global.define_intrinsic_accessor(vm.names.Boolean, attr, [](auto& realm) -> Value { return realm.intrinsics().boolean_constructor(); });
global.define_intrinsic_accessor(vm.names.DataView, attr, [](auto& realm) -> Value { return realm.intrinsics().data_view_constructor(); });
global.define_intrinsic_accessor(vm.names.Date, attr, [](auto& realm) -> Value { return realm.intrinsics().date_constructor(); });
global.define_intrinsic_accessor(vm.names.DisposableStack, attr, [](auto& realm) -> Value { return realm.intrinsics().disposable_stack_constructor(); });
global.define_intrinsic_accessor(vm.names.Error, attr, [](auto& realm) -> Value { return realm.intrinsics().error_constructor(); });
global.define_intrinsic_accessor(vm.names.EvalError, attr, [](auto& realm) -> Value { return realm.intrinsics().eval_error_constructor(); });
global.define_intrinsic_accessor(vm.names.FinalizationRegistry, attr, [](auto& realm) -> Value { return realm.intrinsics().finalization_registry_constructor(); });

View file

@ -28,6 +28,8 @@
#include <LibJS/Runtime/DataViewPrototype.h>
#include <LibJS/Runtime/DateConstructor.h>
#include <LibJS/Runtime/DatePrototype.h>
#include <LibJS/Runtime/DisposableStackConstructor.h>
#include <LibJS/Runtime/DisposableStackPrototype.h>
#include <LibJS/Runtime/ErrorConstructor.h>
#include <LibJS/Runtime/ErrorPrototype.h>
#include <LibJS/Runtime/FinalizationRegistryConstructor.h>