-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LibJS: Add DisposableStack{, Prototype, Constructor}
Since the async parts of the spec are not stage 3 at this point we don't add AsyncDisposableStack.
- Loading branch information
Showing
22 changed files
with
866 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
Vector<DisposableResource> const& disposable_resource_stack() const { return m_disposable_resource_stack; } | ||
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 }; | ||
}; | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
Userland/Libraries/LibJS/Runtime/DisposableStackConstructor.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
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(); | ||
|
||
// NOTE: Step 1 is implemented in DisposableStackConstructor::call() | ||
|
||
// 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> {})); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
Userland/Libraries/LibJS/Runtime/DisposableStackConstructor.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
}; | ||
|
||
} |
Oops, something went wrong.