@oseo/runtime-c 0.0.0-stage → 0.1.0-canary.370

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