v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
js-disposable-stack-inl.h
Go to the documentation of this file.
1// Copyright 2024 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_OBJECTS_JS_DISPOSABLE_STACK_INL_H_
6#define V8_OBJECTS_JS_DISPOSABLE_STACK_INL_H_
7
9// Include the non-inl header before the rest of the headers.
10
12#include "src/handles/handles.h"
14#include "src/heap/factory.h"
18#include "src/objects/objects.h"
19
20// Has to be the last include (doesn't have include guards):
22
23namespace v8 {
24namespace internal {
25
26#include "torque-generated/src/objects/js-disposable-stack-tq-inl.inc"
27
28TQ_OBJECT_CONSTRUCTORS_IMPL(JSDisposableStackBase)
29TQ_OBJECT_CONSTRUCTORS_IMPL(JSSyncDisposableStack)
30TQ_OBJECT_CONSTRUCTORS_IMPL(JSAsyncDisposableStack)
31
33 JSDisposableStackBase::StateBit)
35 JSDisposableStackBase::NeedsAwaitBit)
37 JSDisposableStackBase::HasAwaitedBit)
39 JSDisposableStackBase::SuppressedErrorCreatedBit)
41 JSDisposableStackBase::LengthBits)
42
43inline void JSDisposableStackBase::Add(
44 Isolate* isolate, DirectHandle<JSDisposableStackBase> disposable_stack,
47 DCHECK(!IsUndefined(disposable_stack->stack()));
48 int length = disposable_stack->length();
49 int stack_type =
51 DirectHandle<Smi> stack_type_handle(Smi::FromInt(stack_type), isolate);
52
53 Handle<FixedArray> array(disposable_stack->stack(), isolate);
54 array = FixedArray::SetAndGrow(isolate, array, length++, value);
55 array = FixedArray::SetAndGrow(isolate, array, length++, method);
56 array = FixedArray::SetAndGrow(isolate, array, length++, stack_type_handle);
57
58 disposable_stack->set_length(length);
59 disposable_stack->set_stack(*array);
60}
61
62// part of
63// https://arai-a.github.io/ecma262-compare/?pr=3000&id=sec-createdisposableresource
64inline MaybeDirectHandle<Object>
67 DisposeMethodHint hint) {
70 // 1. If method is not present, then
71 // a. If V is either null or undefined, then
72 // i. Set V to undefined.
73 // ii. Set method to undefined.
74 // We has already returned from the caller if V is null or undefined, when
75 // hint is `kSyncDispose`.
76 DCHECK(!IsNullOrUndefined(*value));
77
78 // b. Else,
79 // i. If V is not an Object, throw a TypeError exception.
80 if (!IsJSReceiver(*value)) {
81 THROW_NEW_ERROR(isolate,
82 NewTypeError(MessageTemplate::kExpectAnObjectWithUsing));
83 }
84
85 // ii. Set method to ? GetDisposeMethod(V, hint).
87 isolate, method,
88 Object::GetProperty(isolate, value,
89 isolate->factory()->dispose_symbol()));
90 // (GetMethod)3. If IsCallable(func) is false, throw a TypeError
91 // exception.
92 if (!IsJSFunction(*method)) {
93 THROW_NEW_ERROR(isolate,
94 NewTypeError(MessageTemplate::kNotCallable,
95 isolate->factory()->dispose_symbol()));
96 }
97
98 // iii. If method is undefined, throw a TypeError exception.
99 // It is already checked in step ii.
100
101 } else if (hint == DisposeMethodHint::kAsyncDispose) {
102 // 1. If method is not present, then
103 // a. If V is either null or undefined, then
104 // i. Set V to undefined.
105 // ii. Set method to undefined.
106 if (IsNullOrUndefined(*value)) {
107 return isolate->factory()->undefined_value();
108 }
109
110 // b. Else,
111 // i. If V is not an Object, throw a TypeError exception.
112 if (!IsJSReceiver(*value)) {
113 THROW_NEW_ERROR(isolate,
114 NewTypeError(MessageTemplate::kExpectAnObjectWithUsing));
115 }
116 // https://tc39.es/proposal-explicit-resource-management/#sec-getdisposemethod
117 // 1. If hint is async-dispose, then
118 // a. Let method be ? GetMethod(V, @@asyncDispose).
120 isolate, method,
121 Object::GetProperty(isolate, value,
122 isolate->factory()->async_dispose_symbol()));
123 // b. If method is undefined, then
125 // i. Set method to ? GetMethod(V, @@dispose).
127 isolate, method,
128 Object::GetProperty(isolate, value,
129 isolate->factory()->dispose_symbol()));
130 // (GetMethod)3. If IsCallable(func) is false, throw a TypeError
131 // exception.
132 if (!IsJSFunction(*method)) {
133 THROW_NEW_ERROR(isolate,
134 NewTypeError(MessageTemplate::kNotCallable,
135 isolate->factory()->dispose_symbol()));
136 }
137 // ii. If method is not undefined, then
138 if (!IsUndefined(*method)) {
139 // 1. Let closure be a new Abstract Closure with no parameters that
140 // captures method and performs the following steps when called:
141 // a. Let O be the this value.
142 // b. Let promiseCapability be ! NewPromiseCapability(%Promise%).
143 // c. Let result be Completion(Call(method, O)).
144 // d. IfAbruptRejectPromise(result, promiseCapability).
145 // e. Perform ? Call(promiseCapability.[[Resolve]], undefined, «
146 // undefined »).
147 // f. Return promiseCapability.[[Promise]].
148 // 2. NOTE: This function is not observable to user code. It is
149 // used to ensure that a Promise returned from a synchronous
150 // @@dispose method will not be awaited and that any exception
151 // thrown will not be thrown synchronously.
152 // 3. Return CreateBuiltinFunction(closure, 0, "", « »).
153
154 // (TODO:rezvan): Add `kAsyncFromSyncDispose` to the `DisposeMethodHint`
155 // enum and remove the following allocation of adapter closure.
156 DirectHandle<Context> async_dispose_from_sync_dispose_context =
157 isolate->factory()->NewBuiltinContext(
158 isolate->native_context(),
159 static_cast<int>(
161 async_dispose_from_sync_dispose_context->set(
163 *method);
164
165 method =
167 isolate,
168 isolate->factory()
169 ->async_dispose_from_sync_dispose_shared_fun(),
170 async_dispose_from_sync_dispose_context}
171 .Build();
172 }
173 }
174 // (GetMethod)3. If IsCallable(func) is false, throw a TypeError
175 // exception.
176 if (!IsJSFunction(*method)) {
177 THROW_NEW_ERROR(isolate,
178 NewTypeError(MessageTemplate::kNotCallable,
179 isolate->factory()->async_dispose_symbol()));
180 }
181 }
182 return method;
183}
184
186 Isolate* isolate, DirectHandle<JSDisposableStackBase> disposable_stack,
187 DirectHandle<Object> current_error,
188 DirectHandle<Object> current_error_message) {
189 DCHECK(isolate->is_catchable_by_javascript(*current_error));
190
191 DirectHandle<Object> maybe_error(disposable_stack->error(), isolate);
192
193 // i. If completion is a throw completion, then
194 if (!IsUninitialized(*maybe_error)) {
195 // 1. Set result to result.[[Value]].
196 // 2. Let suppressed be completion.[[Value]].
197 // 3. Let error be a newly created SuppressedError object.
198 // 4. Perform CreateNonEnumerableDataPropertyOrThrow(error, "error",
199 // result).
200 // 5. Perform CreateNonEnumerableDataPropertyOrThrow(error,
201 // "suppressed", suppressed).
202 // 6. Set completion to ThrowCompletion(error).
203 maybe_error = isolate->factory()->NewSuppressedErrorAtDisposal(
204 isolate, current_error, maybe_error);
205 disposable_stack->set_suppressed_error_created(true);
206
207 } else {
208 // ii. Else,
209 // 1. Set completion to result.
210 maybe_error = current_error;
211 }
212
213 disposable_stack->set_error(*maybe_error);
214 disposable_stack->set_error_message(*current_error_message);
215}
216
217} // namespace internal
218} // namespace v8
219
221
222#endif // V8_OBJECTS_JS_DISPOSABLE_STACK_INL_H_
static constexpr U encode(T value)
Definition bit-field.h:55
V8_WARN_UNUSED_RESULT Handle< JSFunction > Build()
Definition factory.cc:4732
static V8_EXPORT_PRIVATE HandleType< FixedArray > SetAndGrow(Isolate *isolate, HandleType< FixedArray > array, int index, DirectHandle< Object > value)
static void HandleErrorInDisposal(Isolate *isolate, DirectHandle< JSDisposableStackBase > disposable_stack, DirectHandle< Object > current_error, DirectHandle< Object > current_error_message)
static MaybeDirectHandle< Object > CheckValueAndGetDisposeMethod(Isolate *isolate, DirectHandle< JSAny > value, DisposeMethodHint hint)
V8_EXPORT_PRIVATE static V8_WARN_UNUSED_RESULT MaybeHandle< Object > GetProperty(LookupIterator *it, bool is_global_reference=false)
Definition objects.cc:1248
static constexpr Tagged< Smi > FromInt(int value)
Definition smi.h:38
#define ASSIGN_RETURN_ON_EXCEPTION(isolate, dst, call)
Definition isolate.h:291
#define THROW_NEW_ERROR(isolate, call)
Definition isolate.h:307
Isolate * isolate
bool IsNullOrUndefined(Tagged< Object > obj, Isolate *isolate)
JSDisposableStackBase::NeedsAwaitBit suppressed_error_created
#define TQ_OBJECT_CONSTRUCTORS_IMPL(Type)
#define BIT_FIELD_ACCESSORS(holder, field, name, BitField)
#define DCHECK(condition)
Definition logging.h:482
Symbol method