Skip to content

Commit ecedcd4

Browse files
committed
LibJS: Add DisposableStack{, Prototype, Constructor}
Since the async parts of the spec are not stage 3 at this point we don't add AsyncDisposableStack.
1 parent 8b34713 commit ecedcd4

22 files changed

+867
-0
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Userland/Libraries/LibJS/Tests/unicode-identifier-escape.js
44
Userland/Libraries/LibJS/Tests/modules/failing.mjs
55

66
# FIXME: Remove once prettier is updated to support using declarations.
7+
Userland/Libraries/LibJS/Tests/builtins/DisposableStack/DisposableStack.prototype.@@dispose.js
78
Userland/Libraries/LibJS/Tests/modules/top-level-dispose.mjs
89
Userland/Libraries/LibJS/Tests/using-declaration.js
910
Userland/Libraries/LibJS/Tests/using-for-loops.js

Userland/Libraries/LibJS/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ set(SOURCES
7575
Runtime/DateConstructor.cpp
7676
Runtime/DatePrototype.cpp
7777
Runtime/DeclarativeEnvironment.cpp
78+
Runtime/DisposableStack.cpp
79+
Runtime/DisposableStackConstructor.cpp
80+
Runtime/DisposableStackPrototype.cpp
7881
Runtime/ECMAScriptFunctionObject.cpp
7982
Runtime/Environment.cpp
8083
Runtime/Error.cpp

Userland/Libraries/LibJS/Forward.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
__JS_ENUMERATE(BooleanObject, boolean, BooleanPrototype, BooleanConstructor, void) \
2828
__JS_ENUMERATE(DataView, data_view, DataViewPrototype, DataViewConstructor, void) \
2929
__JS_ENUMERATE(Date, date, DatePrototype, DateConstructor, void) \
30+
__JS_ENUMERATE(DisposableStack, disposable_stack, DisposableStackPrototype, DisposableStackConstructor, void) \
3031
__JS_ENUMERATE(Error, error, ErrorPrototype, ErrorConstructor, void) \
3132
__JS_ENUMERATE(FinalizationRegistry, finalization_registry, FinalizationRegistryPrototype, FinalizationRegistryConstructor, void) \
3233
__JS_ENUMERATE(FunctionObject, function, FunctionPrototype, FunctionConstructor, void) \

Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ namespace JS {
6565
P(acos) \
6666
P(acosh) \
6767
P(add) \
68+
P(adopt) \
6869
P(all) \
6970
P(allSettled) \
7071
P(anchor) \
@@ -143,6 +144,7 @@ namespace JS {
143144
P(debug) \
144145
P(decodeURI) \
145146
P(decodeURIComponent) \
147+
P(defer) \
146148
P(defineProperties) \
147149
P(defineProperty) \
148150
P(deleteProperty) \
@@ -151,6 +153,7 @@ namespace JS {
151153
P(difference) \
152154
P(direction) \
153155
P(disambiguation) \
156+
P(disposed) \
154157
P(done) \
155158
P(dotAll) \
156159
P(encodeURI) \
@@ -365,6 +368,7 @@ namespace JS {
365368
P(months) \
366369
P(monthsDisplay) \
367370
P(monthsInYear) \
371+
P(move) \
368372
P(multiline) \
369373
P(name) \
370374
P(nanosecond) \
@@ -555,6 +559,7 @@ namespace JS {
555559
P(unshift) \
556560
P(until) \
557561
P(usage) \
562+
P(use) \
558563
P(useGrouping) \
559564
P(value) \
560565
P(valueOf) \
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2022, David Tuin <[email protected]>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#include <LibJS/Runtime/DisposableStack.h>
8+
9+
namespace JS {
10+
11+
DisposableStack::DisposableStack(Vector<DisposableResource> stack, Object& prototype)
12+
: Object(ConstructWithPrototypeTag::Tag, prototype)
13+
, m_disposable_resource_stack(move(stack))
14+
{
15+
}
16+
17+
void DisposableStack::visit_edges(Cell::Visitor& visitor)
18+
{
19+
Base::visit_edges(visitor);
20+
for (auto& resource : m_disposable_resource_stack) {
21+
visitor.visit(resource.resource_value);
22+
visitor.visit(resource.dispose_method);
23+
}
24+
}
25+
26+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2022, David Tuin <[email protected]>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <LibJS/Runtime/AbstractOperations.h>
10+
#include <LibJS/Runtime/Object.h>
11+
12+
namespace JS {
13+
14+
class DisposableStack final : public Object {
15+
JS_OBJECT(DisposableStack, Object);
16+
17+
public:
18+
virtual ~DisposableStack() override = default;
19+
20+
enum class DisposableState {
21+
Pending,
22+
Disposed
23+
};
24+
25+
[[nodiscard]] DisposableState disposable_state() const { return m_state; }
26+
[[nodiscard]] Vector<DisposableResource> const& disposable_resource_stack() const { return m_disposable_resource_stack; }
27+
[[nodiscard]] Vector<DisposableResource>& disposable_resource_stack() { return m_disposable_resource_stack; }
28+
29+
void set_disposed() { m_state = DisposableState::Disposed; }
30+
31+
private:
32+
DisposableStack(Vector<DisposableResource> stack, Object& prototype);
33+
34+
virtual void visit_edges(Visitor& visitor) override;
35+
36+
Vector<DisposableResource> m_disposable_resource_stack;
37+
DisposableState m_state { DisposableState::Pending };
38+
};
39+
40+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2022, David Tuin <[email protected]>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#include <LibJS/Runtime/AbstractOperations.h>
8+
#include <LibJS/Runtime/DisposableStack.h>
9+
#include <LibJS/Runtime/DisposableStackConstructor.h>
10+
11+
namespace JS {
12+
13+
DisposableStackConstructor::DisposableStackConstructor(Realm& realm)
14+
: NativeFunction(realm.vm().names.DisposableStack.as_string(), *realm.intrinsics().function_prototype())
15+
{
16+
}
17+
18+
void DisposableStackConstructor::initialize(Realm& realm)
19+
{
20+
auto& vm = this->vm();
21+
NativeFunction::initialize(realm);
22+
23+
// 26.2.2.1 DisposableStack.prototype, https://tc39.es/ecma262/#sec-finalization-registry.prototype
24+
define_direct_property(vm.names.prototype, realm.intrinsics().disposable_stack_prototype(), 0);
25+
26+
define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
27+
}
28+
29+
// 11.3.1.1 DisposableStack ( ), https://tc39.es/proposal-explicit-resource-management/#sec-disposablestack
30+
ThrowCompletionOr<Value> DisposableStackConstructor::call()
31+
{
32+
// 1. If NewTarget is undefined, throw a TypeError exception.
33+
auto& vm = this->vm();
34+
return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.DisposableStack);
35+
}
36+
37+
// 11.3.1.1 DisposableStack ( ), https://tc39.es/proposal-explicit-resource-management/#sec-disposablestack
38+
ThrowCompletionOr<NonnullGCPtr<Object>> DisposableStackConstructor::construct(FunctionObject& new_target)
39+
{
40+
auto& vm = this->vm();
41+
42+
// 2. Let disposableStack be ? OrdinaryCreateFromConstructor(NewTarget, "%DisposableStack.prototype%", « [[DisposableState]], [[DisposableResourceStack]] »).
43+
// 3. Set disposableStack.[[DisposableState]] to pending.
44+
// 4. Set disposableStack.[[DisposableResourceStack]] to a new empty List.
45+
// 5. Return disposableStack.
46+
return TRY(ordinary_create_from_constructor<DisposableStack>(vm, new_target, &Intrinsics::disposable_stack_prototype, Vector<DisposableResource> {}));
47+
}
48+
49+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2022, David Tuin <[email protected]>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <LibJS/Runtime/NativeFunction.h>
10+
11+
namespace JS {
12+
13+
class DisposableStackConstructor final : public NativeFunction {
14+
JS_OBJECT(DisposableStackConstructor, NativeFunction);
15+
16+
public:
17+
virtual void initialize(Realm&) override;
18+
virtual ~DisposableStackConstructor() override = default;
19+
20+
virtual ThrowCompletionOr<Value> call() override;
21+
virtual ThrowCompletionOr<NonnullGCPtr<Object>> construct(FunctionObject&) override;
22+
23+
private:
24+
explicit DisposableStackConstructor(Realm&);
25+
26+
virtual bool has_constructor() const override { return true; }
27+
};
28+
29+
}

0 commit comments

Comments
 (0)