@oseo/runtime-c 0.0.0-stage → 0.1.0-canary.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/MIT-LICENSE.txt +21 -0
- package/README.md +1677 -2
- package/UNICODE-LICENSE.txt +39 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +269 -0
- package/dist/index.js.map +1 -0
- package/native/oseo_runtime.h +2594 -0
- package/native/runtime_agent.c +1693 -0
- package/native/runtime_arguments.c +355 -0
- package/native/runtime_array.c +5742 -0
- package/native/runtime_array_buffer.c +1724 -0
- package/native/runtime_async_generator.c +948 -0
- package/native/runtime_atomics.c +1259 -0
- package/native/runtime_bigint.c +1363 -0
- package/native/runtime_bigint_object.c +602 -0
- package/native/runtime_binding.c +1005 -0
- package/native/runtime_boolean.c +296 -0
- package/native/runtime_clock.c +157 -0
- package/native/runtime_clock_posix.c +575 -0
- package/native/runtime_core.c +464 -0
- package/native/runtime_data_view.c +1111 -0
- package/native/runtime_date.c +1973 -0
- package/native/runtime_descriptor.c +1080 -0
- package/native/runtime_disposal.c +1643 -0
- package/native/runtime_enumeration.c +612 -0
- package/native/runtime_error.c +973 -0
- package/native/runtime_event_loop.c +629 -0
- package/native/runtime_function.c +3643 -0
- package/native/runtime_generator.c +1096 -0
- package/native/runtime_internal.h +5554 -0
- package/native/runtime_iterator.c +5355 -0
- package/native/runtime_json.c +1968 -0
- package/native/runtime_map.c +1539 -0
- package/native/runtime_math.c +894 -0
- package/native/runtime_memory.c +1341 -0
- package/native/runtime_module.c +860 -0
- package/native/runtime_number.c +2053 -0
- package/native/runtime_object.c +399 -0
- package/native/runtime_object_builtin.c +3877 -0
- package/native/runtime_primitive.c +2244 -0
- package/native/runtime_process.c +720 -0
- package/native/runtime_promise.c +3354 -0
- package/native/runtime_property.c +791 -0
- package/native/runtime_proxy.c +1533 -0
- package/native/runtime_realm.c +411 -0
- package/native/runtime_reflect.c +624 -0
- package/native/runtime_regexp.c +3495 -0
- package/native/runtime_regexp_matcher.c +3535 -0
- package/native/runtime_regexp_symbol.c +1471 -0
- package/native/runtime_set.c +1751 -0
- package/native/runtime_string.c +2862 -0
- package/native/runtime_string_match.c +1161 -0
- package/native/runtime_symbol.c +986 -0
- package/native/runtime_typed_array.c +5113 -0
- package/native/runtime_unicode_tables.h +14647 -0
- package/native/runtime_uri.c +676 -0
- package/native/runtime_weak_collection.c +1275 -0
- package/package.json +38 -3
|
@@ -0,0 +1,1643 @@
|
|
|
1
|
+
/* SPDX-License-Identifier: MIT */
|
|
2
|
+
/* Copyright (c) 2026 Hong Minhee; see ../MIT-LICENSE.txt. */
|
|
3
|
+
|
|
4
|
+
#include "runtime_internal.h"
|
|
5
|
+
|
|
6
|
+
#include <stdlib.h>
|
|
7
|
+
#include <string.h>
|
|
8
|
+
|
|
9
|
+
/*
|
|
10
|
+
* Explicit resource management: DisposableStack, AsyncDisposableStack,
|
|
11
|
+
* %Iterator.prototype%[@@dispose], and %AsyncIteratorPrototype%
|
|
12
|
+
* [@@asyncDispose].
|
|
13
|
+
*
|
|
14
|
+
* Both stack kinds share one heap record, OseoDisposableStack, whose
|
|
15
|
+
* traced resource buffer is the [[DisposeCapability]] of ECMAScript 2026
|
|
16
|
+
* 27.3. DisposeResources runs here in both forms. The synchronous form
|
|
17
|
+
* calls every dispose method in reverse order on the native stack. The
|
|
18
|
+
* asynchronous form keeps its loop position, its pending completion, and
|
|
19
|
+
* the needsAwait and hasAwaited flags in the record and returns at every
|
|
20
|
+
* Await; two internal reactions resume it, so no native frame outlives a
|
|
21
|
+
* suspension.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
static void initialize_ordinary(
|
|
25
|
+
OseoContext *context,
|
|
26
|
+
OseoOrdinaryObject *object,
|
|
27
|
+
OseoValue prototype
|
|
28
|
+
) {
|
|
29
|
+
object->prototype = prototype;
|
|
30
|
+
object->properties = NULL;
|
|
31
|
+
object->property_capacity = 0u;
|
|
32
|
+
object->property_count = 0u;
|
|
33
|
+
oseo_internal_init_dense_index(object);
|
|
34
|
+
object->private_elements = NULL;
|
|
35
|
+
object->private_element_capacity = 0u;
|
|
36
|
+
object->private_element_count = 0u;
|
|
37
|
+
object->shape_id = context->next_shape_id;
|
|
38
|
+
context->next_shape_id += 1u;
|
|
39
|
+
object->array_length = 0u;
|
|
40
|
+
object->dictionary = false;
|
|
41
|
+
object->length_writable = false;
|
|
42
|
+
object->extensible = true;
|
|
43
|
+
object->module_namespace = false;
|
|
44
|
+
object->immutable_prototype = false;
|
|
45
|
+
object->global_object = false;
|
|
46
|
+
object->error_data = false;
|
|
47
|
+
object->number_data = false;
|
|
48
|
+
object->number_value = oseo_undefined();
|
|
49
|
+
object->primitive_data = false;
|
|
50
|
+
object->primitive_value = oseo_undefined();
|
|
51
|
+
object->primitive_wrapper_methods_initialized = false;
|
|
52
|
+
object->virtual_string_iterator = false;
|
|
53
|
+
object->virtual_string_iterator_configurable = false;
|
|
54
|
+
object->virtual_string_iterator_enumerable = false;
|
|
55
|
+
object->virtual_string_iterator_writable = false;
|
|
56
|
+
object->iterator_kind = OSEO_ITERATOR_NONE;
|
|
57
|
+
object->iterator_target = oseo_undefined();
|
|
58
|
+
object->iterator_index = 0u;
|
|
59
|
+
object->regexp_string_iterator = false;
|
|
60
|
+
object->regexp_iterator_regexp = oseo_undefined();
|
|
61
|
+
object->regexp_iterator_subject = oseo_undefined();
|
|
62
|
+
object->regexp_iterator_global = false;
|
|
63
|
+
object->regexp_iterator_unicode = false;
|
|
64
|
+
object->regexp_iterator_complete = false;
|
|
65
|
+
object->async_from_sync = false;
|
|
66
|
+
object->async_sync_iterator = oseo_undefined();
|
|
67
|
+
object->wrap_for_valid_iterator = false;
|
|
68
|
+
object->wrapped_iterator = oseo_undefined();
|
|
69
|
+
object->wrapped_next = oseo_undefined();
|
|
70
|
+
object->arguments_object = false;
|
|
71
|
+
object->mapped_arguments = false;
|
|
72
|
+
object->generator = NULL;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static OseoValue argument_at(
|
|
76
|
+
size_t argument_count,
|
|
77
|
+
const OseoValue *arguments,
|
|
78
|
+
size_t index
|
|
79
|
+
) {
|
|
80
|
+
return argument_count > index && arguments != NULL
|
|
81
|
+
? arguments[index]
|
|
82
|
+
: oseo_undefined();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
static OseoResult type_error(OseoContext *context, const char *message) {
|
|
86
|
+
return oseo_internal_throw_error(context, OSEO_ERROR_TYPE, message);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/* A catchable throw, as opposed to a runtime diagnostic. */
|
|
90
|
+
static bool language_throw(OseoContext *context, OseoResult result) {
|
|
91
|
+
return result.status == OSEO_STATUS_THROW && !context->has_diagnostic;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/* Allocates one empty stack whose prototype is rooted in `*prototype`. */
|
|
95
|
+
static OseoResult stack_allocate(
|
|
96
|
+
OseoContext *context,
|
|
97
|
+
const OseoValue *prototype,
|
|
98
|
+
bool async
|
|
99
|
+
) {
|
|
100
|
+
OseoDisposableStack *stack =
|
|
101
|
+
oseo_internal_allocate_heap_bytes(context, sizeof(*stack));
|
|
102
|
+
if (stack == NULL) {
|
|
103
|
+
return failure(
|
|
104
|
+
context,
|
|
105
|
+
"OSEO2001",
|
|
106
|
+
"DisposableStack allocation failed."
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
initialize_ordinary(context, &stack->ordinary, *prototype);
|
|
110
|
+
stack->resources = NULL;
|
|
111
|
+
stack->resource_count = 0u;
|
|
112
|
+
stack->resource_capacity = 0u;
|
|
113
|
+
stack->async = async;
|
|
114
|
+
stack->disposed = false;
|
|
115
|
+
stack->capability_only = false;
|
|
116
|
+
stack->capability = oseo_undefined();
|
|
117
|
+
stack->promise = oseo_undefined();
|
|
118
|
+
stack->error = oseo_undefined();
|
|
119
|
+
stack->on_fulfilled = oseo_undefined();
|
|
120
|
+
stack->on_rejected = oseo_undefined();
|
|
121
|
+
stack->cursor = 0u;
|
|
122
|
+
stack->has_error = false;
|
|
123
|
+
stack->error_changed = false;
|
|
124
|
+
stack->awaiting = false;
|
|
125
|
+
stack->await_value = oseo_undefined();
|
|
126
|
+
stack->needs_await = false;
|
|
127
|
+
stack->has_awaited = false;
|
|
128
|
+
stack->phase = OSEO_DISPOSE_PHASE_RESULT;
|
|
129
|
+
return oseo_internal_publish_heap(
|
|
130
|
+
context,
|
|
131
|
+
&stack->ordinary.header,
|
|
132
|
+
OSEO_HEAP_DISPOSABLE_STACK
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
OseoResult oseo_internal_dispose_capability_create(
|
|
137
|
+
OseoContext *context,
|
|
138
|
+
bool async
|
|
139
|
+
) {
|
|
140
|
+
OseoValue prototype = oseo_null();
|
|
141
|
+
OseoRootFrame frame = {NULL, &prototype, 1u};
|
|
142
|
+
oseo_roots_push(context, &frame);
|
|
143
|
+
OseoResult result = stack_allocate(context, &prototype, async);
|
|
144
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
145
|
+
disposable_stack_object(result.value)->capability_only = true;
|
|
146
|
+
}
|
|
147
|
+
oseo_roots_pop(context, &frame);
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/*
|
|
152
|
+
* Allocates one stack whose prototype is rooted in `*prototype`, with a
|
|
153
|
+
* fresh, empty capability record of its own.
|
|
154
|
+
*/
|
|
155
|
+
static OseoResult stack_create(
|
|
156
|
+
OseoContext *context,
|
|
157
|
+
const OseoValue *prototype,
|
|
158
|
+
bool async
|
|
159
|
+
) {
|
|
160
|
+
OseoValue capability = oseo_undefined();
|
|
161
|
+
OseoRootFrame frame = {NULL, &capability, 1u};
|
|
162
|
+
oseo_roots_push(context, &frame);
|
|
163
|
+
OseoResult result = oseo_internal_dispose_capability_create(context, async);
|
|
164
|
+
capability = result.value;
|
|
165
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
166
|
+
result = stack_allocate(context, prototype, async);
|
|
167
|
+
}
|
|
168
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
169
|
+
disposable_stack_object(result.value)->capability = capability;
|
|
170
|
+
}
|
|
171
|
+
oseo_roots_pop(context, &frame);
|
|
172
|
+
return result;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/*
|
|
176
|
+
* The DisposableStack and AsyncDisposableStack constructors. Both take
|
|
177
|
+
* the prototype from the new target, falling back to the new target's
|
|
178
|
+
* realm, and start pending with an empty capability.
|
|
179
|
+
*/
|
|
180
|
+
static OseoResult stack_construct(
|
|
181
|
+
OseoContext *context,
|
|
182
|
+
OseoValue new_target,
|
|
183
|
+
bool async
|
|
184
|
+
) {
|
|
185
|
+
if (tag_of(new_target) == OSEO_TAG_UNDEFINED) {
|
|
186
|
+
return type_error(
|
|
187
|
+
context,
|
|
188
|
+
async
|
|
189
|
+
? "AsyncDisposableStack must be called with new."
|
|
190
|
+
: "DisposableStack must be called with new."
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
194
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 2u);
|
|
195
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
196
|
+
frame.slots[0] = new_target;
|
|
197
|
+
result = oseo_internal_constructor_prototype(context, frame.slots[0]);
|
|
198
|
+
frame.slots[1] = result.value;
|
|
199
|
+
if (result.status == OSEO_STATUS_NORMAL && !is_object(frame.slots[1])) {
|
|
200
|
+
result = oseo_internal_constructor_realm_default(
|
|
201
|
+
context,
|
|
202
|
+
frame.slots[0],
|
|
203
|
+
async
|
|
204
|
+
? OSEO_INTRINSIC_ASYNC_DISPOSABLE_STACK_PROTOTYPE
|
|
205
|
+
: OSEO_INTRINSIC_DISPOSABLE_STACK_PROTOTYPE
|
|
206
|
+
);
|
|
207
|
+
frame.slots[1] = result.value;
|
|
208
|
+
}
|
|
209
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
210
|
+
result = stack_create(context, &frame.slots[1], async);
|
|
211
|
+
}
|
|
212
|
+
oseo_roots_release(context, &frame);
|
|
213
|
+
return result;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/* The capability record a constructed stack currently owns. */
|
|
217
|
+
static OseoValue stack_capability(OseoValue stack) {
|
|
218
|
+
return disposable_stack_object(stack)->capability;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/* RequireInternalSlot for [[DisposableState]] or [[AsyncDisposableState]]. */
|
|
222
|
+
static bool stack_receiver(OseoValue receiver, bool async) {
|
|
223
|
+
if (!is_disposable_stack(receiver)) return false;
|
|
224
|
+
OseoDisposableStack *stack = disposable_stack_object(receiver);
|
|
225
|
+
/* A `using` scope's capability is never exposed, so only a
|
|
226
|
+
* constructed stack of the requested kind qualifies. */
|
|
227
|
+
return stack->async == async && !stack->capability_only;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
static OseoResult receiver_error(OseoContext *context, bool async) {
|
|
231
|
+
return type_error(
|
|
232
|
+
context,
|
|
233
|
+
async
|
|
234
|
+
? "The receiver is not an AsyncDisposableStack."
|
|
235
|
+
: "The receiver is not a DisposableStack."
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
static OseoResult disposed_error(OseoContext *context, bool async) {
|
|
240
|
+
return oseo_internal_throw_error(
|
|
241
|
+
context,
|
|
242
|
+
OSEO_ERROR_REFERENCE,
|
|
243
|
+
async
|
|
244
|
+
? "The AsyncDisposableStack is already disposed."
|
|
245
|
+
: "The DisposableStack is already disposed."
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/*
|
|
250
|
+
* Appends one DisposableResource. The values are read from the caller's
|
|
251
|
+
* roots and only the host buffer grows, so no collection runs between
|
|
252
|
+
* the reads and the stores.
|
|
253
|
+
*/
|
|
254
|
+
static OseoResult stack_push(
|
|
255
|
+
OseoContext *context,
|
|
256
|
+
OseoValue stack_value,
|
|
257
|
+
OseoValue value,
|
|
258
|
+
OseoValue method,
|
|
259
|
+
int flags
|
|
260
|
+
) {
|
|
261
|
+
OseoDisposableStack *stack = disposable_stack_object(stack_value);
|
|
262
|
+
if (stack->resource_count + 3u > stack->resource_capacity) {
|
|
263
|
+
size_t capacity = stack->resource_capacity == 0u
|
|
264
|
+
? 12u
|
|
265
|
+
: stack->resource_capacity * 2u;
|
|
266
|
+
if (capacity < stack->resource_capacity ||
|
|
267
|
+
capacity > SIZE_MAX / sizeof(OseoValue)) {
|
|
268
|
+
return failure(
|
|
269
|
+
context,
|
|
270
|
+
"OSEO2001",
|
|
271
|
+
"DisposableStack resource list is too large."
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
OseoValue *resources =
|
|
275
|
+
realloc(stack->resources, capacity * sizeof(OseoValue));
|
|
276
|
+
if (resources == NULL) {
|
|
277
|
+
return failure(
|
|
278
|
+
context,
|
|
279
|
+
"OSEO2001",
|
|
280
|
+
"DisposableStack resource allocation failed."
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
stack->resources = resources;
|
|
284
|
+
stack->resource_capacity = capacity;
|
|
285
|
+
}
|
|
286
|
+
stack->resources[stack->resource_count] = value;
|
|
287
|
+
stack->resources[stack->resource_count + 1u] = method;
|
|
288
|
+
stack->resources[stack->resource_count + 2u] = oseo_number((double)flags);
|
|
289
|
+
stack->resource_count += 3u;
|
|
290
|
+
return normal(oseo_undefined());
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/* GetMethod(V, P) for a well-known symbol key, which runs user getters. */
|
|
294
|
+
static OseoResult symbol_method(
|
|
295
|
+
OseoContext *context,
|
|
296
|
+
OseoValue value,
|
|
297
|
+
size_t well_known
|
|
298
|
+
) {
|
|
299
|
+
OseoValue slots[2] = {value, oseo_undefined()};
|
|
300
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
301
|
+
oseo_roots_push(context, &frame);
|
|
302
|
+
OseoResult result = oseo_internal_well_known_symbol(context, well_known);
|
|
303
|
+
slots[1] = result.value;
|
|
304
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
305
|
+
result = oseo_object_get(context, slots[0], slots[1]);
|
|
306
|
+
}
|
|
307
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
308
|
+
if (is_nullish(result.value)) {
|
|
309
|
+
result.value = oseo_undefined();
|
|
310
|
+
} else if (!is_callable(result.value)) {
|
|
311
|
+
result = type_error(
|
|
312
|
+
context,
|
|
313
|
+
well_known == OSEO_WELL_KNOWN_DISPOSE
|
|
314
|
+
? "The Symbol.dispose property is not callable."
|
|
315
|
+
: "The Symbol.asyncDispose property is not callable."
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
oseo_roots_pop(context, &frame);
|
|
320
|
+
return result;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
OseoResult oseo_internal_dispose_capability_add(
|
|
324
|
+
OseoContext *context,
|
|
325
|
+
OseoValue capability,
|
|
326
|
+
OseoValue value,
|
|
327
|
+
bool async
|
|
328
|
+
) {
|
|
329
|
+
/* AddDisposableResource without a method: a synchronous hint skips a
|
|
330
|
+
* nullish value, and an asynchronous hint records it so disposal
|
|
331
|
+
* still awaits once. */
|
|
332
|
+
if (is_nullish(value)) {
|
|
333
|
+
if (!async) return normal(oseo_undefined());
|
|
334
|
+
return stack_push(
|
|
335
|
+
context,
|
|
336
|
+
capability,
|
|
337
|
+
oseo_undefined(),
|
|
338
|
+
oseo_undefined(),
|
|
339
|
+
OSEO_DISPOSE_ASYNC_HINT
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
if (!is_object(value)) {
|
|
343
|
+
return type_error(
|
|
344
|
+
context,
|
|
345
|
+
"A disposable resource must be an object."
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
OseoValue slots[3] = {capability, value, oseo_undefined()};
|
|
349
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
350
|
+
oseo_roots_push(context, &frame);
|
|
351
|
+
int flags = async ? OSEO_DISPOSE_ASYNC_HINT : 0;
|
|
352
|
+
OseoResult result = symbol_method(
|
|
353
|
+
context,
|
|
354
|
+
slots[1],
|
|
355
|
+
async ? OSEO_WELL_KNOWN_ASYNC_DISPOSE : OSEO_WELL_KNOWN_DISPOSE
|
|
356
|
+
);
|
|
357
|
+
slots[2] = result.value;
|
|
358
|
+
if (result.status == OSEO_STATUS_NORMAL && async &&
|
|
359
|
+
tag_of(slots[2]) == OSEO_TAG_UNDEFINED) {
|
|
360
|
+
result = symbol_method(context, slots[1], OSEO_WELL_KNOWN_DISPOSE);
|
|
361
|
+
slots[2] = result.value;
|
|
362
|
+
flags |= OSEO_DISPOSE_SYNC_FALLBACK;
|
|
363
|
+
}
|
|
364
|
+
if (result.status == OSEO_STATUS_NORMAL &&
|
|
365
|
+
tag_of(slots[2]) == OSEO_TAG_UNDEFINED) {
|
|
366
|
+
result = type_error(
|
|
367
|
+
context,
|
|
368
|
+
async
|
|
369
|
+
? "The resource has no Symbol.asyncDispose or "
|
|
370
|
+
"Symbol.dispose method."
|
|
371
|
+
: "The resource has no Symbol.dispose method."
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
375
|
+
result = stack_push(context, slots[0], slots[1], slots[2], flags);
|
|
376
|
+
}
|
|
377
|
+
oseo_roots_pop(context, &frame);
|
|
378
|
+
if (result.status == OSEO_STATUS_NORMAL) result.value = value;
|
|
379
|
+
return result;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/* Calls the dispose method of resource `index`; the stack stays rooted. */
|
|
383
|
+
static OseoResult call_resource(
|
|
384
|
+
OseoContext *context,
|
|
385
|
+
OseoValue stack_value,
|
|
386
|
+
size_t index
|
|
387
|
+
) {
|
|
388
|
+
OseoDisposableStack *stack = disposable_stack_object(stack_value);
|
|
389
|
+
OseoValue slots[2] = {
|
|
390
|
+
stack->resources[index * 3u],
|
|
391
|
+
stack->resources[index * 3u + 1u],
|
|
392
|
+
};
|
|
393
|
+
int flags = (int)number_value(stack->resources[index * 3u + 2u]);
|
|
394
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
395
|
+
oseo_roots_push(context, &frame);
|
|
396
|
+
OseoResult result = (flags & OSEO_DISPOSE_ADOPTED) != 0
|
|
397
|
+
? oseo_call_function(
|
|
398
|
+
context,
|
|
399
|
+
slots[1],
|
|
400
|
+
oseo_undefined(),
|
|
401
|
+
1u,
|
|
402
|
+
&slots[0],
|
|
403
|
+
oseo_undefined()
|
|
404
|
+
)
|
|
405
|
+
: oseo_call_function(
|
|
406
|
+
context,
|
|
407
|
+
slots[1],
|
|
408
|
+
slots[0],
|
|
409
|
+
0u,
|
|
410
|
+
NULL,
|
|
411
|
+
oseo_undefined()
|
|
412
|
+
);
|
|
413
|
+
oseo_roots_pop(context, &frame);
|
|
414
|
+
return result;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/*
|
|
418
|
+
* Folds one throw completion of a dispose method into the stored one. A
|
|
419
|
+
* later error replaces the completion with a SuppressedError whose
|
|
420
|
+
* `error` is the new value and whose `suppressed` is the earlier one.
|
|
421
|
+
*/
|
|
422
|
+
static OseoResult record_error(
|
|
423
|
+
OseoContext *context,
|
|
424
|
+
OseoValue stack_value,
|
|
425
|
+
OseoValue error
|
|
426
|
+
) {
|
|
427
|
+
oseo_context_clear_language_error(context);
|
|
428
|
+
OseoDisposableStack *stack = disposable_stack_object(stack_value);
|
|
429
|
+
stack->error_changed = true;
|
|
430
|
+
if (!stack->has_error) {
|
|
431
|
+
stack->has_error = true;
|
|
432
|
+
stack->error = error;
|
|
433
|
+
return normal(oseo_undefined());
|
|
434
|
+
}
|
|
435
|
+
OseoValue slots[2] = {stack_value, error};
|
|
436
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
437
|
+
oseo_roots_push(context, &frame);
|
|
438
|
+
OseoResult result = oseo_internal_suppressed_error_create(
|
|
439
|
+
context,
|
|
440
|
+
slots[1],
|
|
441
|
+
disposable_stack_object(slots[0])->error
|
|
442
|
+
);
|
|
443
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
444
|
+
disposable_stack_object(slots[0])->error = result.value;
|
|
445
|
+
}
|
|
446
|
+
oseo_roots_pop(context, &frame);
|
|
447
|
+
return result;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
static void release_resources(OseoDisposableStack *stack) {
|
|
451
|
+
free(stack->resources);
|
|
452
|
+
stack->resources = NULL;
|
|
453
|
+
stack->resource_count = 0u;
|
|
454
|
+
stack->resource_capacity = 0u;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/* The final completion of one DisposeResources, read from the record. */
|
|
458
|
+
static OseoResult stored_completion(
|
|
459
|
+
OseoContext *context,
|
|
460
|
+
OseoValue stack_value
|
|
461
|
+
) {
|
|
462
|
+
OseoDisposableStack *stack = disposable_stack_object(stack_value);
|
|
463
|
+
if (!stack->has_error) return normal(oseo_undefined());
|
|
464
|
+
OseoValue error = stack->error;
|
|
465
|
+
stack->has_error = false;
|
|
466
|
+
stack->error = oseo_undefined();
|
|
467
|
+
oseo_context_clear_language_error(context);
|
|
468
|
+
return (OseoResult){OSEO_STATUS_THROW, error};
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
OseoResult oseo_internal_dispose_capability_dispose(
|
|
472
|
+
OseoContext *context,
|
|
473
|
+
OseoValue capability,
|
|
474
|
+
bool has_completion,
|
|
475
|
+
OseoValue completion
|
|
476
|
+
) {
|
|
477
|
+
OseoValue slots[1] = {capability};
|
|
478
|
+
OseoRootFrame frame = {NULL, slots, 1u};
|
|
479
|
+
oseo_roots_push(context, &frame);
|
|
480
|
+
OseoDisposableStack *stack = disposable_stack_object(slots[0]);
|
|
481
|
+
stack->has_error = has_completion;
|
|
482
|
+
stack->error = has_completion ? completion : oseo_undefined();
|
|
483
|
+
OseoResult result = normal(oseo_undefined());
|
|
484
|
+
for (size_t index = stack->resource_count / 3u; index > 0u; index -= 1u) {
|
|
485
|
+
result = call_resource(context, slots[0], index - 1u);
|
|
486
|
+
if (language_throw(context, result)) {
|
|
487
|
+
result = record_error(context, slots[0], result.value);
|
|
488
|
+
}
|
|
489
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
490
|
+
}
|
|
491
|
+
stack = disposable_stack_object(slots[0]);
|
|
492
|
+
release_resources(stack);
|
|
493
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
494
|
+
result = stored_completion(context, slots[0]);
|
|
495
|
+
}
|
|
496
|
+
oseo_roots_pop(context, &frame);
|
|
497
|
+
return result;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/* A fresh %Promise% already settled with `value`. */
|
|
501
|
+
static OseoResult settled_promise(
|
|
502
|
+
OseoContext *context,
|
|
503
|
+
OseoValue value,
|
|
504
|
+
bool fulfilled
|
|
505
|
+
) {
|
|
506
|
+
OseoValue slots[2] = {value, oseo_undefined()};
|
|
507
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
508
|
+
oseo_roots_push(context, &frame);
|
|
509
|
+
OseoResult result = oseo_internal_promise_create(context);
|
|
510
|
+
slots[1] = result.value;
|
|
511
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
512
|
+
result = fulfilled
|
|
513
|
+
? oseo_promise_resolve_into(context, slots[1], slots[0])
|
|
514
|
+
: oseo_promise_reject_into(context, slots[1], slots[0]);
|
|
515
|
+
}
|
|
516
|
+
if (result.status == OSEO_STATUS_NORMAL) result.value = slots[1];
|
|
517
|
+
oseo_roots_pop(context, &frame);
|
|
518
|
+
return result;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/* A promise rejected with the catchable throw `abrupt` carries. */
|
|
522
|
+
static OseoResult rejected_with(OseoContext *context, OseoResult abrupt) {
|
|
523
|
+
if (!language_throw(context, abrupt)) return abrupt;
|
|
524
|
+
oseo_context_clear_language_error(context);
|
|
525
|
+
return settled_promise(context, abrupt.value, false);
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/*
|
|
529
|
+
* Await(value) inside an asynchronous DisposeResources. PromiseResolve
|
|
530
|
+
* may throw synchronously, for example from a `constructor` getter; that
|
|
531
|
+
* throw is returned as is, and the caller treats it as the dispose
|
|
532
|
+
* result. Otherwise the record's two reactions are attached and the run
|
|
533
|
+
* returns until one of them resumes it.
|
|
534
|
+
*/
|
|
535
|
+
static OseoResult dispose_await(
|
|
536
|
+
OseoContext *context,
|
|
537
|
+
OseoValue stack_value,
|
|
538
|
+
OseoValue value
|
|
539
|
+
) {
|
|
540
|
+
OseoDisposableStack *scope = disposable_stack_object(stack_value);
|
|
541
|
+
if (!is_promise(scope->promise)) {
|
|
542
|
+
/* A `using` scope's run has no promise of its own: generated
|
|
543
|
+
* code performs this Await in its own frame. */
|
|
544
|
+
scope->await_value = value;
|
|
545
|
+
scope->awaiting = true;
|
|
546
|
+
return normal(oseo_undefined());
|
|
547
|
+
}
|
|
548
|
+
OseoValue slots[2] = {stack_value, value};
|
|
549
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
550
|
+
oseo_roots_push(context, &frame);
|
|
551
|
+
OseoResult result = oseo_promise_resolve(context, slots[1]);
|
|
552
|
+
slots[1] = result.value;
|
|
553
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
554
|
+
OseoDisposableStack *stack = disposable_stack_object(slots[0]);
|
|
555
|
+
result = oseo_promise_then(
|
|
556
|
+
context,
|
|
557
|
+
slots[1],
|
|
558
|
+
stack->on_fulfilled,
|
|
559
|
+
stack->on_rejected
|
|
560
|
+
);
|
|
561
|
+
}
|
|
562
|
+
oseo_roots_pop(context, &frame);
|
|
563
|
+
return result;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/*
|
|
567
|
+
* Settles the run's promise with its final completion. A run started by
|
|
568
|
+
* generated code for `await using` has no promise of its own; its final
|
|
569
|
+
* completion stays in the record for the generated frame to read.
|
|
570
|
+
*/
|
|
571
|
+
static OseoResult async_dispose_finish(
|
|
572
|
+
OseoContext *context,
|
|
573
|
+
OseoValue stack_value
|
|
574
|
+
) {
|
|
575
|
+
OseoValue slots[2] = {stack_value, oseo_undefined()};
|
|
576
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
577
|
+
oseo_roots_push(context, &frame);
|
|
578
|
+
OseoDisposableStack *stack = disposable_stack_object(slots[0]);
|
|
579
|
+
release_resources(stack);
|
|
580
|
+
stack->phase = OSEO_DISPOSE_PHASE_DONE;
|
|
581
|
+
slots[1] = stack->promise;
|
|
582
|
+
OseoResult result = normal(oseo_undefined());
|
|
583
|
+
if (is_promise(slots[1])) {
|
|
584
|
+
stack->promise = oseo_undefined();
|
|
585
|
+
bool has_error = stack->has_error;
|
|
586
|
+
OseoValue error = stack->error;
|
|
587
|
+
stack->has_error = false;
|
|
588
|
+
stack->error = oseo_undefined();
|
|
589
|
+
result = has_error
|
|
590
|
+
? oseo_promise_reject_into(context, slots[1], error)
|
|
591
|
+
: oseo_promise_resolve_into(context, slots[1], oseo_undefined());
|
|
592
|
+
}
|
|
593
|
+
oseo_roots_pop(context, &frame);
|
|
594
|
+
return result;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/*
|
|
598
|
+
* Runs an asynchronous DisposeResources from the record's cursor until
|
|
599
|
+
* it awaits or finishes. The record is rooted by the caller.
|
|
600
|
+
*/
|
|
601
|
+
static OseoResult async_dispose_run(
|
|
602
|
+
OseoContext *context,
|
|
603
|
+
OseoValue stack_value
|
|
604
|
+
) {
|
|
605
|
+
OseoValue slots[2] = {stack_value, oseo_undefined()};
|
|
606
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
607
|
+
oseo_roots_push(context, &frame);
|
|
608
|
+
OseoResult result = normal(oseo_undefined());
|
|
609
|
+
for (;;) {
|
|
610
|
+
OseoDisposableStack *stack = disposable_stack_object(slots[0]);
|
|
611
|
+
if (stack->cursor == 0u) break;
|
|
612
|
+
size_t index = stack->cursor - 1u;
|
|
613
|
+
int flags = (int)number_value(stack->resources[index * 3u + 2u]);
|
|
614
|
+
bool async_hint = (flags & OSEO_DISPOSE_ASYNC_HINT) != 0;
|
|
615
|
+
if (!async_hint && stack->needs_await && !stack->has_awaited) {
|
|
616
|
+
/* The resource stays unconsumed until the tick resumes. */
|
|
617
|
+
stack->needs_await = false;
|
|
618
|
+
stack->phase = OSEO_DISPOSE_PHASE_TICK;
|
|
619
|
+
result = dispose_await(context, slots[0], oseo_undefined());
|
|
620
|
+
oseo_roots_pop(context, &frame);
|
|
621
|
+
return result;
|
|
622
|
+
}
|
|
623
|
+
stack->cursor = index;
|
|
624
|
+
if (tag_of(stack->resources[index * 3u + 1u]) == OSEO_TAG_UNDEFINED) {
|
|
625
|
+
stack->needs_await = true;
|
|
626
|
+
continue;
|
|
627
|
+
}
|
|
628
|
+
result = call_resource(context, slots[0], index);
|
|
629
|
+
if (result.status == OSEO_STATUS_NORMAL && async_hint) {
|
|
630
|
+
if ((flags & OSEO_DISPOSE_SYNC_FALLBACK) != 0) {
|
|
631
|
+
result = settled_promise(context, oseo_undefined(), true);
|
|
632
|
+
}
|
|
633
|
+
} else if (language_throw(context, result) && async_hint &&
|
|
634
|
+
(flags & OSEO_DISPOSE_SYNC_FALLBACK) != 0) {
|
|
635
|
+
/* The fallback closure turns the throw into its rejected
|
|
636
|
+
* promise, which is still awaited. */
|
|
637
|
+
oseo_context_clear_language_error(context);
|
|
638
|
+
result = settled_promise(context, result.value, false);
|
|
639
|
+
} else if (language_throw(context, result)) {
|
|
640
|
+
result = record_error(context, slots[0], result.value);
|
|
641
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
642
|
+
continue;
|
|
643
|
+
}
|
|
644
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
645
|
+
if (!async_hint) continue;
|
|
646
|
+
slots[1] = result.value;
|
|
647
|
+
stack = disposable_stack_object(slots[0]);
|
|
648
|
+
stack->has_awaited = true;
|
|
649
|
+
stack->phase = OSEO_DISPOSE_PHASE_RESULT;
|
|
650
|
+
result = dispose_await(context, slots[0], slots[1]);
|
|
651
|
+
if (language_throw(context, result)) {
|
|
652
|
+
result = record_error(context, slots[0], result.value);
|
|
653
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
654
|
+
continue;
|
|
655
|
+
}
|
|
656
|
+
oseo_roots_pop(context, &frame);
|
|
657
|
+
return result;
|
|
658
|
+
}
|
|
659
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
660
|
+
OseoDisposableStack *stack = disposable_stack_object(slots[0]);
|
|
661
|
+
if (stack->needs_await && !stack->has_awaited) {
|
|
662
|
+
stack->needs_await = false;
|
|
663
|
+
stack->phase = OSEO_DISPOSE_PHASE_FINAL;
|
|
664
|
+
result = dispose_await(context, slots[0], oseo_undefined());
|
|
665
|
+
} else {
|
|
666
|
+
result = async_dispose_finish(context, slots[0]);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
oseo_roots_pop(context, &frame);
|
|
670
|
+
return result;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
/*
|
|
674
|
+
* Continues an asynchronous DisposeResources once its pending Await has
|
|
675
|
+
* settled. Only a dispose method's result can reject; the two Await
|
|
676
|
+
* (undefined) steps always fulfill.
|
|
677
|
+
*/
|
|
678
|
+
static OseoResult async_dispose_settled(
|
|
679
|
+
OseoContext *context,
|
|
680
|
+
OseoValue stack_value,
|
|
681
|
+
bool fulfilled,
|
|
682
|
+
OseoValue value
|
|
683
|
+
) {
|
|
684
|
+
OseoValue slots[1] = {stack_value};
|
|
685
|
+
OseoRootFrame frame = {NULL, slots, 1u};
|
|
686
|
+
oseo_roots_push(context, &frame);
|
|
687
|
+
OseoDisposableStack *stack = disposable_stack_object(slots[0]);
|
|
688
|
+
int phase = stack->phase;
|
|
689
|
+
OseoResult result = normal(oseo_undefined());
|
|
690
|
+
if (!fulfilled && phase == OSEO_DISPOSE_PHASE_RESULT) {
|
|
691
|
+
result = record_error(context, slots[0], value);
|
|
692
|
+
}
|
|
693
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
694
|
+
result = phase == OSEO_DISPOSE_PHASE_FINAL
|
|
695
|
+
? async_dispose_finish(context, slots[0])
|
|
696
|
+
: async_dispose_run(context, slots[0]);
|
|
697
|
+
}
|
|
698
|
+
oseo_roots_pop(context, &frame);
|
|
699
|
+
return result;
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
/* One of the two reactions that resume an asynchronous disposal. */
|
|
703
|
+
static OseoResult async_dispose_resume(
|
|
704
|
+
OseoContext *context,
|
|
705
|
+
OseoValue callee,
|
|
706
|
+
OseoValue argument,
|
|
707
|
+
bool fulfilled
|
|
708
|
+
) {
|
|
709
|
+
OseoValue slots[2] = {callee, argument};
|
|
710
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
711
|
+
oseo_roots_push(context, &frame);
|
|
712
|
+
OseoResult result = oseo_function_environment(context, slots[0]);
|
|
713
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
714
|
+
result = oseo_environment_get(context, result.value, 0u);
|
|
715
|
+
slots[0] = result.value;
|
|
716
|
+
}
|
|
717
|
+
if (result.status == OSEO_STATUS_NORMAL && !is_disposable_stack(slots[0])) {
|
|
718
|
+
result = failure(
|
|
719
|
+
context,
|
|
720
|
+
"OSEO2001",
|
|
721
|
+
"Invalid asynchronous disposal reaction."
|
|
722
|
+
);
|
|
723
|
+
}
|
|
724
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
725
|
+
result = async_dispose_settled(context, slots[0], fulfilled, slots[1]);
|
|
726
|
+
}
|
|
727
|
+
oseo_roots_pop(context, &frame);
|
|
728
|
+
return result.status == OSEO_STATUS_NORMAL
|
|
729
|
+
? normal(oseo_undefined())
|
|
730
|
+
: result;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
/* Creates the record's two resuming reactions over one environment. */
|
|
734
|
+
static OseoResult async_dispose_reactions(
|
|
735
|
+
OseoContext *context,
|
|
736
|
+
OseoValue stack_value
|
|
737
|
+
) {
|
|
738
|
+
OseoValue slots[3] = {stack_value, oseo_undefined(), oseo_undefined()};
|
|
739
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
740
|
+
oseo_roots_push(context, &frame);
|
|
741
|
+
OseoResult result = oseo_environment_create(context, 1u);
|
|
742
|
+
slots[1] = result.value;
|
|
743
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
744
|
+
result = oseo_environment_set(context, slots[1], 0u, slots[0]);
|
|
745
|
+
}
|
|
746
|
+
static const size_t codes[2] = {
|
|
747
|
+
OSEO_ASYNC_DISPOSE_FULFILLED_CODE_ID,
|
|
748
|
+
OSEO_ASYNC_DISPOSE_REJECTED_CODE_ID,
|
|
749
|
+
};
|
|
750
|
+
for (size_t index = 0u;
|
|
751
|
+
result.status == OSEO_STATUS_NORMAL && index < 2u;
|
|
752
|
+
index += 1u) {
|
|
753
|
+
result = oseo_function_create(
|
|
754
|
+
context,
|
|
755
|
+
codes[index],
|
|
756
|
+
slots[1],
|
|
757
|
+
NULL,
|
|
758
|
+
0u,
|
|
759
|
+
1u,
|
|
760
|
+
OSEO_FUNCTION_INTERNAL,
|
|
761
|
+
oseo_undefined(),
|
|
762
|
+
oseo_undefined(),
|
|
763
|
+
OSEO_FUNCTION_NAME_PREFIX_NONE
|
|
764
|
+
);
|
|
765
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
766
|
+
slots[2] = result.value;
|
|
767
|
+
OseoDisposableStack *stack = disposable_stack_object(slots[0]);
|
|
768
|
+
if (index == 0u) {
|
|
769
|
+
stack->on_fulfilled = slots[2];
|
|
770
|
+
} else {
|
|
771
|
+
stack->on_rejected = slots[2];
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
oseo_roots_pop(context, &frame);
|
|
775
|
+
return result;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
OseoResult oseo_internal_dispose_capability_dispose_async(
|
|
779
|
+
OseoContext *context,
|
|
780
|
+
OseoValue capability,
|
|
781
|
+
OseoValue promise,
|
|
782
|
+
bool has_completion,
|
|
783
|
+
OseoValue completion
|
|
784
|
+
) {
|
|
785
|
+
OseoValue slots[3] = {capability, promise, completion};
|
|
786
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
787
|
+
oseo_roots_push(context, &frame);
|
|
788
|
+
OseoResult result = async_dispose_reactions(context, slots[0]);
|
|
789
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
790
|
+
OseoDisposableStack *stack = disposable_stack_object(slots[0]);
|
|
791
|
+
stack->promise = slots[1];
|
|
792
|
+
stack->has_error = has_completion;
|
|
793
|
+
stack->error = has_completion ? slots[2] : oseo_undefined();
|
|
794
|
+
stack->cursor = stack->resource_count / 3u;
|
|
795
|
+
stack->needs_await = false;
|
|
796
|
+
stack->has_awaited = false;
|
|
797
|
+
stack->phase = OSEO_DISPOSE_PHASE_RESULT;
|
|
798
|
+
result = async_dispose_run(context, slots[0]);
|
|
799
|
+
}
|
|
800
|
+
oseo_roots_pop(context, &frame);
|
|
801
|
+
return result;
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
bool oseo_internal_dispose_capability_done(OseoValue capability) {
|
|
805
|
+
return disposable_stack_object(capability)->phase ==
|
|
806
|
+
OSEO_DISPOSE_PHASE_DONE;
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
OseoResult oseo_internal_dispose_capability_completion(
|
|
810
|
+
OseoContext *context,
|
|
811
|
+
OseoValue capability
|
|
812
|
+
) {
|
|
813
|
+
return stored_completion(context, capability);
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
/* DisposableStack.prototype.dispose and @@dispose. */
|
|
817
|
+
static OseoResult stack_dispose(OseoContext *context, OseoValue receiver) {
|
|
818
|
+
if (!stack_receiver(receiver, false)) {
|
|
819
|
+
return receiver_error(context, false);
|
|
820
|
+
}
|
|
821
|
+
OseoDisposableStack *stack = disposable_stack_object(receiver);
|
|
822
|
+
if (stack->disposed) return normal(oseo_undefined());
|
|
823
|
+
stack->disposed = true;
|
|
824
|
+
return oseo_internal_dispose_capability_dispose(
|
|
825
|
+
context,
|
|
826
|
+
stack->capability,
|
|
827
|
+
false,
|
|
828
|
+
oseo_undefined()
|
|
829
|
+
);
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
/* AsyncDisposableStack.prototype.disposeAsync and @@asyncDispose. */
|
|
833
|
+
static OseoResult stack_dispose_async(
|
|
834
|
+
OseoContext *context,
|
|
835
|
+
OseoValue receiver
|
|
836
|
+
) {
|
|
837
|
+
if (!stack_receiver(receiver, true)) {
|
|
838
|
+
return rejected_with(context, receiver_error(context, true));
|
|
839
|
+
}
|
|
840
|
+
OseoDisposableStack *stack = disposable_stack_object(receiver);
|
|
841
|
+
if (stack->disposed) {
|
|
842
|
+
return settled_promise(context, oseo_undefined(), true);
|
|
843
|
+
}
|
|
844
|
+
OseoValue slots[2] = {receiver, oseo_undefined()};
|
|
845
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
846
|
+
oseo_roots_push(context, &frame);
|
|
847
|
+
OseoResult result = oseo_internal_promise_create(context);
|
|
848
|
+
slots[1] = result.value;
|
|
849
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
850
|
+
disposable_stack_object(slots[0])->disposed = true;
|
|
851
|
+
result = oseo_internal_dispose_capability_dispose_async(
|
|
852
|
+
context,
|
|
853
|
+
stack_capability(slots[0]),
|
|
854
|
+
slots[1],
|
|
855
|
+
false,
|
|
856
|
+
oseo_undefined()
|
|
857
|
+
);
|
|
858
|
+
}
|
|
859
|
+
if (result.status == OSEO_STATUS_NORMAL) result.value = slots[1];
|
|
860
|
+
oseo_roots_pop(context, &frame);
|
|
861
|
+
return result;
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
/* The `disposed` getter of either prototype. */
|
|
865
|
+
static OseoResult stack_disposed(
|
|
866
|
+
OseoContext *context,
|
|
867
|
+
OseoValue receiver,
|
|
868
|
+
bool async
|
|
869
|
+
) {
|
|
870
|
+
if (!stack_receiver(receiver, async)) {
|
|
871
|
+
return receiver_error(context, async);
|
|
872
|
+
}
|
|
873
|
+
return normal(oseo_boolean(disposable_stack_object(receiver)->disposed));
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
/* The checks every adding method and `move` start with. */
|
|
877
|
+
static OseoResult pending_receiver(
|
|
878
|
+
OseoContext *context,
|
|
879
|
+
OseoValue receiver,
|
|
880
|
+
bool async
|
|
881
|
+
) {
|
|
882
|
+
if (!stack_receiver(receiver, async)) {
|
|
883
|
+
return receiver_error(context, async);
|
|
884
|
+
}
|
|
885
|
+
if (disposable_stack_object(receiver)->disposed) {
|
|
886
|
+
return disposed_error(context, async);
|
|
887
|
+
}
|
|
888
|
+
return normal(receiver);
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
static OseoResult stack_use(
|
|
892
|
+
OseoContext *context,
|
|
893
|
+
OseoValue receiver,
|
|
894
|
+
OseoValue value,
|
|
895
|
+
bool async
|
|
896
|
+
) {
|
|
897
|
+
OseoResult result = pending_receiver(context, receiver, async);
|
|
898
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
899
|
+
/* The capability is captured before the dispose method's getter
|
|
900
|
+
* runs, so a `move` inside that getter still sees the resource. */
|
|
901
|
+
result = oseo_internal_dispose_capability_add(
|
|
902
|
+
context,
|
|
903
|
+
stack_capability(receiver),
|
|
904
|
+
value,
|
|
905
|
+
async
|
|
906
|
+
);
|
|
907
|
+
if (result.status == OSEO_STATUS_NORMAL) result.value = value;
|
|
908
|
+
return result;
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
static OseoResult stack_adopt(
|
|
912
|
+
OseoContext *context,
|
|
913
|
+
OseoValue receiver,
|
|
914
|
+
OseoValue value,
|
|
915
|
+
OseoValue on_dispose,
|
|
916
|
+
bool async
|
|
917
|
+
) {
|
|
918
|
+
OseoResult result = pending_receiver(context, receiver, async);
|
|
919
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
920
|
+
if (!is_callable(on_dispose)) {
|
|
921
|
+
return type_error(context, "The onDispose callback is not callable.");
|
|
922
|
+
}
|
|
923
|
+
int flags = OSEO_DISPOSE_ADOPTED | (async ? OSEO_DISPOSE_ASYNC_HINT : 0);
|
|
924
|
+
result = stack_push(
|
|
925
|
+
context,
|
|
926
|
+
stack_capability(receiver),
|
|
927
|
+
value,
|
|
928
|
+
on_dispose,
|
|
929
|
+
flags
|
|
930
|
+
);
|
|
931
|
+
if (result.status == OSEO_STATUS_NORMAL) result.value = value;
|
|
932
|
+
return result;
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
static OseoResult stack_defer(
|
|
936
|
+
OseoContext *context,
|
|
937
|
+
OseoValue receiver,
|
|
938
|
+
OseoValue on_dispose,
|
|
939
|
+
bool async
|
|
940
|
+
) {
|
|
941
|
+
OseoResult result = pending_receiver(context, receiver, async);
|
|
942
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
943
|
+
if (!is_callable(on_dispose)) {
|
|
944
|
+
return type_error(context, "The onDispose callback is not callable.");
|
|
945
|
+
}
|
|
946
|
+
return stack_push(
|
|
947
|
+
context,
|
|
948
|
+
stack_capability(receiver),
|
|
949
|
+
oseo_undefined(),
|
|
950
|
+
on_dispose,
|
|
951
|
+
async ? OSEO_DISPOSE_ASYNC_HINT : 0
|
|
952
|
+
);
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
/*
|
|
956
|
+
* `move` creates the new stack from the intrinsic constructor of the
|
|
957
|
+
* running realm, hands it the receiver's capability record, and leaves
|
|
958
|
+
* the receiver disposed with a fresh, empty one.
|
|
959
|
+
*/
|
|
960
|
+
static OseoResult stack_move(
|
|
961
|
+
OseoContext *context,
|
|
962
|
+
OseoValue receiver,
|
|
963
|
+
bool async
|
|
964
|
+
) {
|
|
965
|
+
OseoResult result = pending_receiver(context, receiver, async);
|
|
966
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
967
|
+
OseoValue slots[2] = {receiver, oseo_undefined()};
|
|
968
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
969
|
+
oseo_roots_push(context, &frame);
|
|
970
|
+
result = oseo_internal_intrinsic(
|
|
971
|
+
context,
|
|
972
|
+
async
|
|
973
|
+
? OSEO_INTRINSIC_ASYNC_DISPOSABLE_STACK_PROTOTYPE
|
|
974
|
+
: OSEO_INTRINSIC_DISPOSABLE_STACK_PROTOTYPE
|
|
975
|
+
);
|
|
976
|
+
slots[1] = result.value;
|
|
977
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
978
|
+
result = stack_create(context, &slots[1], async);
|
|
979
|
+
}
|
|
980
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
981
|
+
OseoDisposableStack *source = disposable_stack_object(slots[0]);
|
|
982
|
+
OseoDisposableStack *target = disposable_stack_object(result.value);
|
|
983
|
+
OseoValue fresh = target->capability;
|
|
984
|
+
target->capability = source->capability;
|
|
985
|
+
source->capability = fresh;
|
|
986
|
+
source->disposed = true;
|
|
987
|
+
}
|
|
988
|
+
oseo_roots_pop(context, &frame);
|
|
989
|
+
return result;
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
/* %Iterator.prototype%[@@dispose]: GetMethod(O, "return") and a call. */
|
|
993
|
+
static OseoResult iterator_dispose(OseoContext *context, OseoValue receiver) {
|
|
994
|
+
OseoValue slots[2] = {receiver, oseo_undefined()};
|
|
995
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
996
|
+
oseo_roots_push(context, &frame);
|
|
997
|
+
OseoResult result = oseo_internal_ascii_string(context, "return");
|
|
998
|
+
slots[1] = result.value;
|
|
999
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1000
|
+
result = oseo_object_get(context, slots[0], slots[1]);
|
|
1001
|
+
slots[1] = result.value;
|
|
1002
|
+
}
|
|
1003
|
+
if (result.status == OSEO_STATUS_NORMAL && !is_nullish(slots[1])) {
|
|
1004
|
+
result = is_callable(slots[1])
|
|
1005
|
+
? oseo_call_function(
|
|
1006
|
+
context,
|
|
1007
|
+
slots[1],
|
|
1008
|
+
slots[0],
|
|
1009
|
+
0u,
|
|
1010
|
+
NULL,
|
|
1011
|
+
oseo_undefined()
|
|
1012
|
+
)
|
|
1013
|
+
: type_error(
|
|
1014
|
+
context,
|
|
1015
|
+
"The iterator return method is not callable."
|
|
1016
|
+
);
|
|
1017
|
+
}
|
|
1018
|
+
oseo_roots_pop(context, &frame);
|
|
1019
|
+
return result.status == OSEO_STATUS_NORMAL
|
|
1020
|
+
? normal(oseo_undefined())
|
|
1021
|
+
: result;
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
/*
|
|
1025
|
+
* %AsyncIteratorPrototype%[@@asyncDispose]. Each abrupt step rejects the
|
|
1026
|
+
* returned promise. A `return` result is passed through PromiseResolve,
|
|
1027
|
+
* and the promise returned is the one PerformPromiseThen derives from it
|
|
1028
|
+
* with a reaction that discards the fulfillment value.
|
|
1029
|
+
*/
|
|
1030
|
+
static OseoResult async_iterator_dispose(
|
|
1031
|
+
OseoContext *context,
|
|
1032
|
+
OseoValue receiver
|
|
1033
|
+
) {
|
|
1034
|
+
OseoValue slots[3] = {receiver, oseo_undefined(), oseo_undefined()};
|
|
1035
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
1036
|
+
oseo_roots_push(context, &frame);
|
|
1037
|
+
OseoResult result = oseo_internal_ascii_string(context, "return");
|
|
1038
|
+
slots[1] = result.value;
|
|
1039
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1040
|
+
result = oseo_object_get(context, slots[0], slots[1]);
|
|
1041
|
+
slots[1] = result.value;
|
|
1042
|
+
}
|
|
1043
|
+
if (result.status == OSEO_STATUS_NORMAL && is_nullish(slots[1])) {
|
|
1044
|
+
result = settled_promise(context, oseo_undefined(), true);
|
|
1045
|
+
oseo_roots_pop(context, &frame);
|
|
1046
|
+
return result;
|
|
1047
|
+
}
|
|
1048
|
+
if (result.status == OSEO_STATUS_NORMAL && !is_callable(slots[1])) {
|
|
1049
|
+
result = type_error(
|
|
1050
|
+
context,
|
|
1051
|
+
"The iterator return method is not callable."
|
|
1052
|
+
);
|
|
1053
|
+
}
|
|
1054
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1055
|
+
result = oseo_call_function(
|
|
1056
|
+
context,
|
|
1057
|
+
slots[1],
|
|
1058
|
+
slots[0],
|
|
1059
|
+
0u,
|
|
1060
|
+
NULL,
|
|
1061
|
+
oseo_undefined()
|
|
1062
|
+
);
|
|
1063
|
+
}
|
|
1064
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1065
|
+
result = oseo_promise_resolve(context, result.value);
|
|
1066
|
+
slots[1] = result.value;
|
|
1067
|
+
}
|
|
1068
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1069
|
+
result = oseo_environment_create(context, 0u);
|
|
1070
|
+
slots[2] = result.value;
|
|
1071
|
+
}
|
|
1072
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1073
|
+
result = oseo_function_create(
|
|
1074
|
+
context,
|
|
1075
|
+
OSEO_ASYNC_ITERATOR_DISPOSE_UNWRAP_CODE_ID,
|
|
1076
|
+
slots[2],
|
|
1077
|
+
NULL,
|
|
1078
|
+
0u,
|
|
1079
|
+
1u,
|
|
1080
|
+
OSEO_FUNCTION_INTERNAL,
|
|
1081
|
+
oseo_undefined(),
|
|
1082
|
+
oseo_undefined(),
|
|
1083
|
+
OSEO_FUNCTION_NAME_PREFIX_NONE
|
|
1084
|
+
);
|
|
1085
|
+
slots[2] = result.value;
|
|
1086
|
+
}
|
|
1087
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1088
|
+
result = oseo_promise_then(
|
|
1089
|
+
context,
|
|
1090
|
+
slots[1],
|
|
1091
|
+
slots[2],
|
|
1092
|
+
oseo_undefined()
|
|
1093
|
+
);
|
|
1094
|
+
}
|
|
1095
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1096
|
+
result = rejected_with(context, result);
|
|
1097
|
+
}
|
|
1098
|
+
oseo_roots_pop(context, &frame);
|
|
1099
|
+
return result;
|
|
1100
|
+
}
|
|
1101
|
+
|
|
1102
|
+
OseoResult oseo_internal_disposal_builtin_dispatch(
|
|
1103
|
+
OseoContext *context,
|
|
1104
|
+
size_t code_id,
|
|
1105
|
+
OseoValue callee,
|
|
1106
|
+
OseoValue receiver,
|
|
1107
|
+
size_t argument_count,
|
|
1108
|
+
const OseoValue *arguments,
|
|
1109
|
+
OseoValue new_target
|
|
1110
|
+
) {
|
|
1111
|
+
OseoValue first = argument_at(argument_count, arguments, 0u);
|
|
1112
|
+
OseoValue second = argument_at(argument_count, arguments, 1u);
|
|
1113
|
+
switch (code_id) {
|
|
1114
|
+
case OSEO_DISPOSABLE_STACK_CONSTRUCTOR_CODE_ID:
|
|
1115
|
+
return stack_construct(context, new_target, false);
|
|
1116
|
+
case OSEO_ASYNC_DISPOSABLE_STACK_CONSTRUCTOR_CODE_ID:
|
|
1117
|
+
return stack_construct(context, new_target, true);
|
|
1118
|
+
case OSEO_DISPOSABLE_STACK_ADOPT_CODE_ID:
|
|
1119
|
+
return stack_adopt(context, receiver, first, second, false);
|
|
1120
|
+
case OSEO_ASYNC_DISPOSABLE_STACK_ADOPT_CODE_ID:
|
|
1121
|
+
return stack_adopt(context, receiver, first, second, true);
|
|
1122
|
+
case OSEO_DISPOSABLE_STACK_DEFER_CODE_ID:
|
|
1123
|
+
return stack_defer(context, receiver, first, false);
|
|
1124
|
+
case OSEO_ASYNC_DISPOSABLE_STACK_DEFER_CODE_ID:
|
|
1125
|
+
return stack_defer(context, receiver, first, true);
|
|
1126
|
+
case OSEO_DISPOSABLE_STACK_DISPOSE_CODE_ID:
|
|
1127
|
+
return stack_dispose(context, receiver);
|
|
1128
|
+
case OSEO_ASYNC_DISPOSABLE_STACK_DISPOSE_ASYNC_CODE_ID:
|
|
1129
|
+
return stack_dispose_async(context, receiver);
|
|
1130
|
+
case OSEO_DISPOSABLE_STACK_DISPOSED_CODE_ID:
|
|
1131
|
+
return stack_disposed(context, receiver, false);
|
|
1132
|
+
case OSEO_ASYNC_DISPOSABLE_STACK_DISPOSED_CODE_ID:
|
|
1133
|
+
return stack_disposed(context, receiver, true);
|
|
1134
|
+
case OSEO_DISPOSABLE_STACK_MOVE_CODE_ID:
|
|
1135
|
+
return stack_move(context, receiver, false);
|
|
1136
|
+
case OSEO_ASYNC_DISPOSABLE_STACK_MOVE_CODE_ID:
|
|
1137
|
+
return stack_move(context, receiver, true);
|
|
1138
|
+
case OSEO_DISPOSABLE_STACK_USE_CODE_ID:
|
|
1139
|
+
return stack_use(context, receiver, first, false);
|
|
1140
|
+
case OSEO_ASYNC_DISPOSABLE_STACK_USE_CODE_ID:
|
|
1141
|
+
return stack_use(context, receiver, first, true);
|
|
1142
|
+
case OSEO_ITERATOR_DISPOSE_CODE_ID:
|
|
1143
|
+
return iterator_dispose(context, receiver);
|
|
1144
|
+
case OSEO_ASYNC_ITERATOR_ASYNC_DISPOSE_CODE_ID:
|
|
1145
|
+
return async_iterator_dispose(context, receiver);
|
|
1146
|
+
case OSEO_ASYNC_ITERATOR_DISPOSE_UNWRAP_CODE_ID:
|
|
1147
|
+
return normal(oseo_undefined());
|
|
1148
|
+
case OSEO_ASYNC_DISPOSE_FULFILLED_CODE_ID:
|
|
1149
|
+
return async_dispose_resume(context, callee, first, true);
|
|
1150
|
+
case OSEO_ASYNC_DISPOSE_REJECTED_CODE_ID:
|
|
1151
|
+
return async_dispose_resume(context, callee, first, false);
|
|
1152
|
+
default:
|
|
1153
|
+
return oseo_unknown_function(context, code_id);
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
static OseoResult create_disposal_builtin(
|
|
1158
|
+
OseoContext *context,
|
|
1159
|
+
size_t code_id,
|
|
1160
|
+
const char *name,
|
|
1161
|
+
size_t length,
|
|
1162
|
+
OseoFunctionKind kind,
|
|
1163
|
+
OseoFunctionNamePrefix prefix
|
|
1164
|
+
) {
|
|
1165
|
+
size_t name_length = strlen(name);
|
|
1166
|
+
if (name_length > 31u) {
|
|
1167
|
+
return failure(context, "OSEO2001", "Built-in name is too long.");
|
|
1168
|
+
}
|
|
1169
|
+
uint16_t units[31];
|
|
1170
|
+
for (size_t index = 0u; index < name_length; index += 1u) {
|
|
1171
|
+
units[index] = (uint16_t)(unsigned char)name[index];
|
|
1172
|
+
}
|
|
1173
|
+
OseoValue environment = oseo_undefined();
|
|
1174
|
+
OseoRootFrame frame = {NULL, &environment, 1u};
|
|
1175
|
+
oseo_roots_push(context, &frame);
|
|
1176
|
+
OseoResult result = oseo_environment_create(context, 0u);
|
|
1177
|
+
environment = result.value;
|
|
1178
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1179
|
+
result = oseo_function_create(
|
|
1180
|
+
context,
|
|
1181
|
+
code_id,
|
|
1182
|
+
environment,
|
|
1183
|
+
units,
|
|
1184
|
+
name_length,
|
|
1185
|
+
length,
|
|
1186
|
+
kind,
|
|
1187
|
+
oseo_undefined(),
|
|
1188
|
+
oseo_undefined(),
|
|
1189
|
+
prefix
|
|
1190
|
+
);
|
|
1191
|
+
}
|
|
1192
|
+
oseo_roots_pop(context, &frame);
|
|
1193
|
+
return result;
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
OseoResult oseo_internal_iterator_dispose_function(
|
|
1197
|
+
OseoContext *context,
|
|
1198
|
+
size_t code_id
|
|
1199
|
+
) {
|
|
1200
|
+
return create_disposal_builtin(
|
|
1201
|
+
context,
|
|
1202
|
+
code_id,
|
|
1203
|
+
code_id == OSEO_ITERATOR_DISPOSE_CODE_ID
|
|
1204
|
+
? "[Symbol.dispose]"
|
|
1205
|
+
: "[Symbol.asyncDispose]",
|
|
1206
|
+
0u,
|
|
1207
|
+
OSEO_FUNCTION_INTERNAL,
|
|
1208
|
+
OSEO_FUNCTION_NAME_PREFIX_NONE
|
|
1209
|
+
);
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
static OseoResult define_key(
|
|
1213
|
+
OseoContext *context,
|
|
1214
|
+
OseoValue object,
|
|
1215
|
+
OseoValue key,
|
|
1216
|
+
OseoValue value,
|
|
1217
|
+
OseoPropertyAttributes attributes
|
|
1218
|
+
) {
|
|
1219
|
+
return oseo_object_define(context, object, key, value, attributes);
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
static OseoResult define_ascii_property(
|
|
1223
|
+
OseoContext *context,
|
|
1224
|
+
OseoValue object,
|
|
1225
|
+
const char *name,
|
|
1226
|
+
OseoValue value,
|
|
1227
|
+
OseoPropertyAttributes attributes
|
|
1228
|
+
) {
|
|
1229
|
+
OseoValue slots[3] = {object, value, oseo_undefined()};
|
|
1230
|
+
OseoRootFrame frame = {NULL, slots, 3u};
|
|
1231
|
+
oseo_roots_push(context, &frame);
|
|
1232
|
+
OseoResult result = oseo_internal_ascii_string(context, name);
|
|
1233
|
+
slots[2] = result.value;
|
|
1234
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1235
|
+
result = define_key(context, slots[0], slots[2], slots[1], attributes);
|
|
1236
|
+
}
|
|
1237
|
+
oseo_roots_pop(context, &frame);
|
|
1238
|
+
return result;
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
/* One prototype method of a stack constructor. */
|
|
1242
|
+
typedef struct {
|
|
1243
|
+
size_t code_id;
|
|
1244
|
+
const char *name;
|
|
1245
|
+
size_t length;
|
|
1246
|
+
} OseoDisposalMethod;
|
|
1247
|
+
|
|
1248
|
+
/* One stack constructor, its prototype, and the prototype's members. */
|
|
1249
|
+
typedef struct {
|
|
1250
|
+
OseoIntrinsic prototype_intrinsic;
|
|
1251
|
+
OseoIntrinsic constructor_intrinsic;
|
|
1252
|
+
size_t constructor_code_id;
|
|
1253
|
+
const char *name;
|
|
1254
|
+
const OseoDisposalMethod *methods;
|
|
1255
|
+
size_t method_count;
|
|
1256
|
+
/* The method also installed under `dispose_symbol`. */
|
|
1257
|
+
size_t dispose_method;
|
|
1258
|
+
size_t dispose_symbol;
|
|
1259
|
+
size_t disposed_code_id;
|
|
1260
|
+
} OseoDisposalConstructor;
|
|
1261
|
+
|
|
1262
|
+
static const OseoDisposalMethod disposable_stack_methods[] = {
|
|
1263
|
+
{OSEO_DISPOSABLE_STACK_ADOPT_CODE_ID, "adopt", 2u},
|
|
1264
|
+
{OSEO_DISPOSABLE_STACK_DEFER_CODE_ID, "defer", 1u},
|
|
1265
|
+
{OSEO_DISPOSABLE_STACK_DISPOSE_CODE_ID, "dispose", 0u},
|
|
1266
|
+
{OSEO_DISPOSABLE_STACK_MOVE_CODE_ID, "move", 0u},
|
|
1267
|
+
{OSEO_DISPOSABLE_STACK_USE_CODE_ID, "use", 1u},
|
|
1268
|
+
};
|
|
1269
|
+
|
|
1270
|
+
static const OseoDisposalMethod async_disposable_stack_methods[] = {
|
|
1271
|
+
{OSEO_ASYNC_DISPOSABLE_STACK_ADOPT_CODE_ID, "adopt", 2u},
|
|
1272
|
+
{OSEO_ASYNC_DISPOSABLE_STACK_DEFER_CODE_ID, "defer", 1u},
|
|
1273
|
+
{OSEO_ASYNC_DISPOSABLE_STACK_DISPOSE_ASYNC_CODE_ID, "disposeAsync", 0u},
|
|
1274
|
+
{OSEO_ASYNC_DISPOSABLE_STACK_MOVE_CODE_ID, "move", 0u},
|
|
1275
|
+
{OSEO_ASYNC_DISPOSABLE_STACK_USE_CODE_ID, "use", 1u},
|
|
1276
|
+
};
|
|
1277
|
+
|
|
1278
|
+
/*
|
|
1279
|
+
* Constructor order is the order the globals are installed. The last
|
|
1280
|
+
* constructor's slot doubles as the cluster's completion marker.
|
|
1281
|
+
*/
|
|
1282
|
+
static const OseoDisposalConstructor disposal_constructors[] = {
|
|
1283
|
+
{
|
|
1284
|
+
OSEO_INTRINSIC_DISPOSABLE_STACK_PROTOTYPE,
|
|
1285
|
+
OSEO_INTRINSIC_DISPOSABLE_STACK,
|
|
1286
|
+
OSEO_DISPOSABLE_STACK_CONSTRUCTOR_CODE_ID,
|
|
1287
|
+
"DisposableStack",
|
|
1288
|
+
disposable_stack_methods,
|
|
1289
|
+
sizeof(disposable_stack_methods) / sizeof(disposable_stack_methods[0]),
|
|
1290
|
+
2u,
|
|
1291
|
+
OSEO_WELL_KNOWN_DISPOSE,
|
|
1292
|
+
OSEO_DISPOSABLE_STACK_DISPOSED_CODE_ID,
|
|
1293
|
+
},
|
|
1294
|
+
{
|
|
1295
|
+
OSEO_INTRINSIC_ASYNC_DISPOSABLE_STACK_PROTOTYPE,
|
|
1296
|
+
OSEO_INTRINSIC_ASYNC_DISPOSABLE_STACK,
|
|
1297
|
+
OSEO_ASYNC_DISPOSABLE_STACK_CONSTRUCTOR_CODE_ID,
|
|
1298
|
+
"AsyncDisposableStack",
|
|
1299
|
+
async_disposable_stack_methods,
|
|
1300
|
+
sizeof(async_disposable_stack_methods) /
|
|
1301
|
+
sizeof(async_disposable_stack_methods[0]),
|
|
1302
|
+
2u,
|
|
1303
|
+
OSEO_WELL_KNOWN_ASYNC_DISPOSE,
|
|
1304
|
+
OSEO_ASYNC_DISPOSABLE_STACK_DISPOSED_CODE_ID,
|
|
1305
|
+
},
|
|
1306
|
+
};
|
|
1307
|
+
|
|
1308
|
+
#define OSEO_DISPOSAL_CONSTRUCTOR_COUNT \
|
|
1309
|
+
(sizeof(disposal_constructors) / sizeof(disposal_constructors[0]))
|
|
1310
|
+
|
|
1311
|
+
/*
|
|
1312
|
+
* Builds one constructor and prototype in the specification's property
|
|
1313
|
+
* order. `slots` holds 0 Object.prototype, 1 the prototype, 2 the
|
|
1314
|
+
* constructor, 3 a method, 4 a key, 5 a value, and 6 the dispose method.
|
|
1315
|
+
*/
|
|
1316
|
+
static OseoResult disposal_constructor_build(
|
|
1317
|
+
OseoContext *context,
|
|
1318
|
+
const OseoDisposalConstructor *description,
|
|
1319
|
+
OseoValue *slots
|
|
1320
|
+
) {
|
|
1321
|
+
const OseoPropertyAttributes method = {true, false, true, false};
|
|
1322
|
+
OseoResult result = oseo_object_create(context, slots[0]);
|
|
1323
|
+
slots[1] = result.value;
|
|
1324
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1325
|
+
result = create_disposal_builtin(
|
|
1326
|
+
context,
|
|
1327
|
+
description->constructor_code_id,
|
|
1328
|
+
description->name,
|
|
1329
|
+
0u,
|
|
1330
|
+
OSEO_FUNCTION_ORDINARY,
|
|
1331
|
+
OSEO_FUNCTION_NAME_PREFIX_NONE
|
|
1332
|
+
);
|
|
1333
|
+
slots[2] = result.value;
|
|
1334
|
+
}
|
|
1335
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1336
|
+
OseoFunction *constructor = function_object(slots[2]);
|
|
1337
|
+
constructor->prototype_object = slots[1];
|
|
1338
|
+
constructor->prototype_writable = false;
|
|
1339
|
+
result = define_ascii_property(
|
|
1340
|
+
context,
|
|
1341
|
+
slots[1],
|
|
1342
|
+
"constructor",
|
|
1343
|
+
slots[2],
|
|
1344
|
+
method
|
|
1345
|
+
);
|
|
1346
|
+
}
|
|
1347
|
+
for (size_t index = 0u;
|
|
1348
|
+
result.status == OSEO_STATUS_NORMAL &&
|
|
1349
|
+
index < description->method_count;
|
|
1350
|
+
index += 1u) {
|
|
1351
|
+
const OseoDisposalMethod *entry = &description->methods[index];
|
|
1352
|
+
result = create_disposal_builtin(
|
|
1353
|
+
context,
|
|
1354
|
+
entry->code_id,
|
|
1355
|
+
entry->name,
|
|
1356
|
+
entry->length,
|
|
1357
|
+
OSEO_FUNCTION_INTERNAL,
|
|
1358
|
+
OSEO_FUNCTION_NAME_PREFIX_NONE
|
|
1359
|
+
);
|
|
1360
|
+
slots[3] = result.value;
|
|
1361
|
+
if (result.status != OSEO_STATUS_NORMAL) break;
|
|
1362
|
+
if (index == description->dispose_method) slots[6] = slots[3];
|
|
1363
|
+
result = define_ascii_property(
|
|
1364
|
+
context,
|
|
1365
|
+
slots[1],
|
|
1366
|
+
entry->name,
|
|
1367
|
+
slots[3],
|
|
1368
|
+
method
|
|
1369
|
+
);
|
|
1370
|
+
/* The `disposed` accessor follows the dispose method. */
|
|
1371
|
+
if (result.status != OSEO_STATUS_NORMAL ||
|
|
1372
|
+
index != description->dispose_method) {
|
|
1373
|
+
continue;
|
|
1374
|
+
}
|
|
1375
|
+
result = create_disposal_builtin(
|
|
1376
|
+
context,
|
|
1377
|
+
description->disposed_code_id,
|
|
1378
|
+
"disposed",
|
|
1379
|
+
0u,
|
|
1380
|
+
OSEO_FUNCTION_INTERNAL,
|
|
1381
|
+
OSEO_FUNCTION_NAME_PREFIX_GET
|
|
1382
|
+
);
|
|
1383
|
+
slots[3] = result.value;
|
|
1384
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1385
|
+
result = oseo_internal_ascii_string(context, "disposed");
|
|
1386
|
+
slots[4] = result.value;
|
|
1387
|
+
}
|
|
1388
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1389
|
+
result = oseo_object_define_accessor(
|
|
1390
|
+
context,
|
|
1391
|
+
slots[1],
|
|
1392
|
+
slots[4],
|
|
1393
|
+
slots[3],
|
|
1394
|
+
oseo_undefined(),
|
|
1395
|
+
true,
|
|
1396
|
+
false,
|
|
1397
|
+
(OseoPropertyAttributes){true, false, false, true}
|
|
1398
|
+
);
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1402
|
+
result = oseo_internal_well_known_symbol(
|
|
1403
|
+
context,
|
|
1404
|
+
description->dispose_symbol
|
|
1405
|
+
);
|
|
1406
|
+
slots[4] = result.value;
|
|
1407
|
+
}
|
|
1408
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1409
|
+
result = define_key(context, slots[1], slots[4], slots[6], method);
|
|
1410
|
+
}
|
|
1411
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1412
|
+
result = oseo_internal_well_known_symbol(
|
|
1413
|
+
context,
|
|
1414
|
+
OSEO_WELL_KNOWN_TO_STRING_TAG
|
|
1415
|
+
);
|
|
1416
|
+
slots[4] = result.value;
|
|
1417
|
+
}
|
|
1418
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1419
|
+
result = oseo_internal_ascii_string(context, description->name);
|
|
1420
|
+
slots[5] = result.value;
|
|
1421
|
+
}
|
|
1422
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1423
|
+
result = define_key(
|
|
1424
|
+
context,
|
|
1425
|
+
slots[1],
|
|
1426
|
+
slots[4],
|
|
1427
|
+
slots[5],
|
|
1428
|
+
(OseoPropertyAttributes){true, false, false, false}
|
|
1429
|
+
);
|
|
1430
|
+
}
|
|
1431
|
+
if (result.status == OSEO_STATUS_NORMAL) {
|
|
1432
|
+
context->realm->intrinsics[description->prototype_intrinsic] =
|
|
1433
|
+
slots[1];
|
|
1434
|
+
context->realm->intrinsics[description->constructor_intrinsic] =
|
|
1435
|
+
slots[2];
|
|
1436
|
+
}
|
|
1437
|
+
return result;
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
OseoResult oseo_internal_disposal_intrinsic(OseoContext *context) {
|
|
1441
|
+
OseoValue *intrinsics = context->realm->intrinsics;
|
|
1442
|
+
OseoValue *marker = &intrinsics[OSEO_INTRINSIC_ASYNC_DISPOSABLE_STACK];
|
|
1443
|
+
if (tag_of(*marker) != OSEO_TAG_UNDEFINED) return normal(*marker);
|
|
1444
|
+
size_t entry_allocations = context->allocations;
|
|
1445
|
+
OseoRootFrame frame = {NULL, NULL, 0u};
|
|
1446
|
+
OseoResult result = oseo_roots_allocate(context, &frame, 7u);
|
|
1447
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1448
|
+
result = oseo_internal_intrinsic(context, OSEO_INTRINSIC_OBJECT_PROTOTYPE);
|
|
1449
|
+
frame.slots[0] = result.value;
|
|
1450
|
+
for (size_t index = 0u;
|
|
1451
|
+
result.status == OSEO_STATUS_NORMAL &&
|
|
1452
|
+
index < OSEO_DISPOSAL_CONSTRUCTOR_COUNT;
|
|
1453
|
+
index += 1u) {
|
|
1454
|
+
result = disposal_constructor_build(
|
|
1455
|
+
context,
|
|
1456
|
+
&disposal_constructors[index],
|
|
1457
|
+
frame.slots
|
|
1458
|
+
);
|
|
1459
|
+
}
|
|
1460
|
+
if (result.status != OSEO_STATUS_NORMAL) {
|
|
1461
|
+
for (size_t index = OSEO_INTRINSIC_DISPOSABLE_STACK_PROTOTYPE;
|
|
1462
|
+
index <= OSEO_INTRINSIC_ASYNC_DISPOSABLE_STACK;
|
|
1463
|
+
index += 1u) {
|
|
1464
|
+
context->realm->intrinsics[index] = oseo_undefined();
|
|
1465
|
+
}
|
|
1466
|
+
oseo_roots_release(context, &frame);
|
|
1467
|
+
return result;
|
|
1468
|
+
}
|
|
1469
|
+
if (context->observe_specialization) {
|
|
1470
|
+
context->allocations = entry_allocations;
|
|
1471
|
+
}
|
|
1472
|
+
oseo_roots_release(context, &frame);
|
|
1473
|
+
return normal(*marker);
|
|
1474
|
+
}
|
|
1475
|
+
|
|
1476
|
+
OseoResult oseo_internal_install_disposal_globals(
|
|
1477
|
+
OseoContext *context,
|
|
1478
|
+
OseoValue global
|
|
1479
|
+
) {
|
|
1480
|
+
OseoValue slots[2] = {global, oseo_undefined()};
|
|
1481
|
+
OseoRootFrame frame = {NULL, slots, 2u};
|
|
1482
|
+
oseo_roots_push(context, &frame);
|
|
1483
|
+
OseoResult result = oseo_internal_disposal_intrinsic(context);
|
|
1484
|
+
for (size_t index = 0u;
|
|
1485
|
+
result.status == OSEO_STATUS_NORMAL &&
|
|
1486
|
+
index < OSEO_DISPOSAL_CONSTRUCTOR_COUNT;
|
|
1487
|
+
index += 1u) {
|
|
1488
|
+
slots[1] = context->realm->intrinsics[
|
|
1489
|
+
disposal_constructors[index].constructor_intrinsic
|
|
1490
|
+
];
|
|
1491
|
+
result = define_ascii_property(
|
|
1492
|
+
context,
|
|
1493
|
+
slots[0],
|
|
1494
|
+
disposal_constructors[index].name,
|
|
1495
|
+
slots[1],
|
|
1496
|
+
(OseoPropertyAttributes){true, false, true, false}
|
|
1497
|
+
);
|
|
1498
|
+
}
|
|
1499
|
+
oseo_roots_pop(context, &frame);
|
|
1500
|
+
return result.status == OSEO_STATUS_NORMAL ? normal(slots[0]) : result;
|
|
1501
|
+
}
|
|
1502
|
+
|
|
1503
|
+
/*
|
|
1504
|
+
* The generated-code operations of a `using` scope. Generated code
|
|
1505
|
+
* creates one capability per scope, adds each declared value, and runs
|
|
1506
|
+
* DisposeResources when the scope's cleanup region is entered, passing
|
|
1507
|
+
* whether the pending completion is a throw and its value. A dispose
|
|
1508
|
+
* result is a throw only when a dispose method failed; otherwise the
|
|
1509
|
+
* cleanup resumes its pending completion, so an incoming throw keeps its
|
|
1510
|
+
* diagnostic location.
|
|
1511
|
+
*/
|
|
1512
|
+
static OseoResult require_scope(OseoContext *context, OseoValue scope) {
|
|
1513
|
+
if (is_disposable_stack(scope) &&
|
|
1514
|
+
disposable_stack_object(scope)->capability_only) {
|
|
1515
|
+
return normal(scope);
|
|
1516
|
+
}
|
|
1517
|
+
return failure(context, "OSEO2001", "Invalid using scope.");
|
|
1518
|
+
}
|
|
1519
|
+
|
|
1520
|
+
/* The final completion of a scope's run: a throw only if it changed. */
|
|
1521
|
+
static OseoResult scope_completion(OseoContext *context, OseoValue scope) {
|
|
1522
|
+
OseoDisposableStack *stack = disposable_stack_object(scope);
|
|
1523
|
+
bool changed = stack->error_changed;
|
|
1524
|
+
stack->error_changed = false;
|
|
1525
|
+
OseoResult result = stored_completion(context, scope);
|
|
1526
|
+
if (result.status == OSEO_STATUS_THROW && !changed) {
|
|
1527
|
+
return normal(oseo_undefined());
|
|
1528
|
+
}
|
|
1529
|
+
return result;
|
|
1530
|
+
}
|
|
1531
|
+
|
|
1532
|
+
OseoResult oseo_dispose_scope_create(OseoContext *context, bool async) {
|
|
1533
|
+
return oseo_internal_dispose_capability_create(context, async);
|
|
1534
|
+
}
|
|
1535
|
+
|
|
1536
|
+
OseoResult oseo_dispose_scope_add(
|
|
1537
|
+
OseoContext *context,
|
|
1538
|
+
OseoValue scope,
|
|
1539
|
+
OseoValue value,
|
|
1540
|
+
bool async
|
|
1541
|
+
) {
|
|
1542
|
+
OseoResult result = require_scope(context, scope);
|
|
1543
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1544
|
+
result = oseo_internal_dispose_capability_add(context, scope, value, async);
|
|
1545
|
+
if (result.status == OSEO_STATUS_NORMAL) result.value = value;
|
|
1546
|
+
return result;
|
|
1547
|
+
}
|
|
1548
|
+
|
|
1549
|
+
OseoResult oseo_dispose_scope_dispose(
|
|
1550
|
+
OseoContext *context,
|
|
1551
|
+
OseoValue scope,
|
|
1552
|
+
bool throwing,
|
|
1553
|
+
OseoValue completion
|
|
1554
|
+
) {
|
|
1555
|
+
OseoResult result = require_scope(context, scope);
|
|
1556
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1557
|
+
OseoValue slots[1] = {scope};
|
|
1558
|
+
OseoRootFrame frame = {NULL, slots, 1u};
|
|
1559
|
+
oseo_roots_push(context, &frame);
|
|
1560
|
+
disposable_stack_object(slots[0])->error_changed = false;
|
|
1561
|
+
result = oseo_internal_dispose_capability_dispose(
|
|
1562
|
+
context,
|
|
1563
|
+
slots[0],
|
|
1564
|
+
throwing,
|
|
1565
|
+
completion
|
|
1566
|
+
);
|
|
1567
|
+
if (language_throw(context, result) &&
|
|
1568
|
+
!disposable_stack_object(slots[0])->error_changed) {
|
|
1569
|
+
oseo_context_clear_language_error(context);
|
|
1570
|
+
result = normal(oseo_undefined());
|
|
1571
|
+
}
|
|
1572
|
+
disposable_stack_object(slots[0])->error_changed = false;
|
|
1573
|
+
oseo_roots_pop(context, &frame);
|
|
1574
|
+
return result;
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1577
|
+
OseoResult oseo_dispose_scope_begin(
|
|
1578
|
+
OseoContext *context,
|
|
1579
|
+
OseoValue scope,
|
|
1580
|
+
bool throwing,
|
|
1581
|
+
OseoValue completion
|
|
1582
|
+
) {
|
|
1583
|
+
OseoResult result = require_scope(context, scope);
|
|
1584
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1585
|
+
OseoDisposableStack *stack = disposable_stack_object(scope);
|
|
1586
|
+
stack->error_changed = false;
|
|
1587
|
+
stack->awaiting = false;
|
|
1588
|
+
stack->await_value = oseo_undefined();
|
|
1589
|
+
return oseo_internal_dispose_capability_dispose_async(
|
|
1590
|
+
context,
|
|
1591
|
+
scope,
|
|
1592
|
+
oseo_undefined(),
|
|
1593
|
+
throwing,
|
|
1594
|
+
completion
|
|
1595
|
+
);
|
|
1596
|
+
}
|
|
1597
|
+
|
|
1598
|
+
OseoResult oseo_dispose_scope_awaiting(OseoContext *context, OseoValue scope) {
|
|
1599
|
+
OseoResult result = require_scope(context, scope);
|
|
1600
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1601
|
+
return normal(oseo_boolean(disposable_stack_object(scope)->awaiting));
|
|
1602
|
+
}
|
|
1603
|
+
|
|
1604
|
+
OseoResult oseo_dispose_scope_await_value(
|
|
1605
|
+
OseoContext *context,
|
|
1606
|
+
OseoValue scope
|
|
1607
|
+
) {
|
|
1608
|
+
OseoResult result = require_scope(context, scope);
|
|
1609
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1610
|
+
OseoDisposableStack *stack = disposable_stack_object(scope);
|
|
1611
|
+
OseoValue value = stack->await_value;
|
|
1612
|
+
stack->await_value = oseo_undefined();
|
|
1613
|
+
stack->awaiting = false;
|
|
1614
|
+
return normal(value);
|
|
1615
|
+
}
|
|
1616
|
+
|
|
1617
|
+
OseoResult oseo_dispose_scope_continue(
|
|
1618
|
+
OseoContext *context,
|
|
1619
|
+
OseoValue scope,
|
|
1620
|
+
bool fulfilled,
|
|
1621
|
+
OseoValue value
|
|
1622
|
+
) {
|
|
1623
|
+
OseoResult result = require_scope(context, scope);
|
|
1624
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1625
|
+
if (!fulfilled) oseo_context_clear_language_error(context);
|
|
1626
|
+
result = async_dispose_settled(context, scope, fulfilled, value);
|
|
1627
|
+
return result.status == OSEO_STATUS_NORMAL
|
|
1628
|
+
? normal(oseo_undefined())
|
|
1629
|
+
: result;
|
|
1630
|
+
}
|
|
1631
|
+
|
|
1632
|
+
OseoResult oseo_dispose_scope_result(OseoContext *context, OseoValue scope) {
|
|
1633
|
+
OseoResult result = require_scope(context, scope);
|
|
1634
|
+
if (result.status != OSEO_STATUS_NORMAL) return result;
|
|
1635
|
+
if (disposable_stack_object(scope)->phase != OSEO_DISPOSE_PHASE_DONE) {
|
|
1636
|
+
return failure(
|
|
1637
|
+
context,
|
|
1638
|
+
"OSEO2001",
|
|
1639
|
+
"The using scope's disposal has not finished."
|
|
1640
|
+
);
|
|
1641
|
+
}
|
|
1642
|
+
return scope_completion(context, scope);
|
|
1643
|
+
}
|