@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,972 @@
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
+ OseoResult oseo_internal_async_generator_builtin_dispatch(
31
+ OseoContext *context,
32
+ size_t code_id,
33
+ OseoValue callee,
34
+ OseoValue receiver,
35
+ size_t argument_count,
36
+ const OseoValue *arguments,
37
+ OseoValue new_target
38
+ ) {
39
+ (void)new_target;
40
+ OseoValue argument = argument_count > 0u
41
+ ? arguments[0]
42
+ : oseo_undefined();
43
+ if (code_id == OSEO_ASYNC_GENERATOR_NEXT_CODE_ID) {
44
+ return oseo_async_generator_next(context, receiver, argument);
45
+ }
46
+ if (code_id == OSEO_ASYNC_GENERATOR_RETURN_CODE_ID) {
47
+ return oseo_async_generator_return(context, receiver, argument);
48
+ }
49
+ if (code_id == OSEO_ASYNC_GENERATOR_THROW_CODE_ID) {
50
+ return oseo_async_generator_throw(context, receiver, argument);
51
+ }
52
+ if (code_id != OSEO_ASYNC_GENERATOR_FULFILL_CODE_ID &&
53
+ code_id != OSEO_ASYNC_GENERATOR_REJECT_CODE_ID) {
54
+ return oseo_unknown_function(context, code_id);
55
+ }
56
+ /* One await reaction carries the resumed generator in slot zero. */
57
+ OseoResult result = oseo_function_environment(context, callee);
58
+ OseoValue environment = result.value;
59
+ if (result.status == OSEO_STATUS_NORMAL) {
60
+ result = oseo_environment_get(context, environment, 0u);
61
+ }
62
+ if (result.status == OSEO_STATUS_NORMAL) {
63
+ result = oseo_internal_async_generator_awaited(
64
+ context,
65
+ result.value,
66
+ argument,
67
+ code_id == OSEO_ASYNC_GENERATOR_REJECT_CODE_ID
68
+ );
69
+ }
70
+ return result;
71
+ }
72
+
73
+ /*
74
+ * Asynchronous generator objects: the AsyncGeneratorRequest queue, the
75
+ * driver that runs one request at a time, and the two reactions that
76
+ * resume a body parked on an awaited operand.
77
+ *
78
+ * An asynchronous generator reuses the synchronous generator record for
79
+ * its suspended frame. The difference is who owns a step: a synchronous
80
+ * consumer calls `next` and receives the step it produced, while an
81
+ * asynchronous one receives a promise and the driver reports the step
82
+ * into it whenever the body reaches one. A body that awaits leaves the
83
+ * same frame with OSEO_GENERATOR_SUSPEND_AWAIT, so the driver returns to
84
+ * its caller and a promise reaction resumes the body later.
85
+ */
86
+
87
+ static OseoGenerator *generator_state(OseoValue value) {
88
+ return ordinary_object(value)->generator;
89
+ }
90
+
91
+ /*
92
+ * One of the two reactions an await installs. Unlike the virtualized
93
+ * prototype methods below, a reaction is created fresh per suspension,
94
+ * because each carries the generator it resumes in its environment.
95
+ */
96
+ static OseoResult await_reaction_create(
97
+ OseoContext *context,
98
+ OseoValue environment,
99
+ size_t code_id
100
+ ) {
101
+ return oseo_function_create(
102
+ context,
103
+ code_id,
104
+ environment,
105
+ NULL,
106
+ 0u,
107
+ 1u,
108
+ OSEO_FUNCTION_INTERNAL,
109
+ oseo_undefined(),
110
+ oseo_undefined(),
111
+ OSEO_FUNCTION_NAME_PREFIX_NONE
112
+ );
113
+ }
114
+
115
+ /*
116
+ * The %AsyncGeneratorPrototype% methods occupy realm intrinsic slots.
117
+ */
118
+ OseoResult oseo_internal_async_generator_method(
119
+ OseoContext *context,
120
+ size_t code_id
121
+ ) {
122
+ OseoIntrinsic intrinsic;
123
+ static const uint16_t next_units[] = {'n', 'e', 'x', 't'};
124
+ static const uint16_t return_units[] = {
125
+ 'r', 'e', 't', 'u', 'r', 'n'
126
+ };
127
+ static const uint16_t throw_units[] = {'t', 'h', 'r', 'o', 'w'};
128
+ static const uint16_t symbol_units[] = {
129
+ '[', 'S', 'y', 'm', 'b', 'o', 'l', '.', 'a', 's', 'y', 'n', 'c',
130
+ 'I', 't', 'e', 'r', 'a', 't', 'o', 'r', ']'
131
+ };
132
+ const uint16_t *name;
133
+ size_t name_length;
134
+ size_t parameter_count = 1u;
135
+ if (code_id == OSEO_ASYNC_GENERATOR_NEXT_CODE_ID) {
136
+ intrinsic = OSEO_INTRINSIC_ASYNC_GENERATOR_NEXT;
137
+ name = next_units;
138
+ name_length = sizeof(next_units) / sizeof(*next_units);
139
+ } else if (code_id == OSEO_ASYNC_GENERATOR_RETURN_CODE_ID) {
140
+ intrinsic = OSEO_INTRINSIC_ASYNC_GENERATOR_RETURN;
141
+ name = return_units;
142
+ name_length = sizeof(return_units) / sizeof(*return_units);
143
+ } else if (code_id == OSEO_ASYNC_GENERATOR_THROW_CODE_ID) {
144
+ intrinsic = OSEO_INTRINSIC_ASYNC_GENERATOR_THROW;
145
+ name = throw_units;
146
+ name_length = sizeof(throw_units) / sizeof(*throw_units);
147
+ } else {
148
+ intrinsic = OSEO_INTRINSIC_ASYNC_ITERATOR_SELF;
149
+ name = symbol_units;
150
+ name_length = sizeof(symbol_units) / sizeof(*symbol_units);
151
+ parameter_count = 0u;
152
+ }
153
+ OseoValue *cache = &context->realm->intrinsics[intrinsic];
154
+ if (tag_of(*cache) != OSEO_TAG_UNDEFINED) return normal(*cache);
155
+ OseoRootFrame frame = {NULL, NULL, 0u};
156
+ OseoResult result = oseo_roots_allocate(context, &frame, 1u);
157
+ if (result.status != OSEO_STATUS_NORMAL) return result;
158
+ result = oseo_environment_create(context, 0u);
159
+ frame.slots[0] = result.value;
160
+ if (result.status == OSEO_STATUS_NORMAL) {
161
+ result = oseo_function_create(
162
+ context,
163
+ code_id,
164
+ frame.slots[0],
165
+ name,
166
+ name_length,
167
+ parameter_count,
168
+ OSEO_FUNCTION_INTERNAL,
169
+ oseo_undefined(),
170
+ oseo_undefined(),
171
+ OSEO_FUNCTION_NAME_PREFIX_NONE
172
+ );
173
+ }
174
+ if (result.status == OSEO_STATUS_NORMAL) *cache = result.value;
175
+ oseo_roots_release(context, &frame);
176
+ return result;
177
+ }
178
+
179
+ /* Appends one AsyncGeneratorRequest to the generator's queue. */
180
+ static OseoResult request_enqueue(
181
+ OseoContext *context,
182
+ OseoValue generator,
183
+ OseoValue capability,
184
+ OseoValue value,
185
+ size_t resume_kind
186
+ ) {
187
+ OseoAsyncGeneratorRequest *request =
188
+ oseo_internal_allocate_heap_bytes(context, sizeof(*request));
189
+ if (request == NULL) {
190
+ return failure(
191
+ context,
192
+ "OSEO2001",
193
+ "Asynchronous generator request allocation failed."
194
+ );
195
+ }
196
+ request->next = oseo_undefined();
197
+ request->capability = capability;
198
+ request->value = value;
199
+ request->resume_kind = resume_kind;
200
+ OseoResult published = oseo_internal_publish_heap(
201
+ context,
202
+ &request->header,
203
+ OSEO_HEAP_ASYNC_GENERATOR_REQUEST
204
+ );
205
+ if (published.status != OSEO_STATUS_NORMAL) return published;
206
+ OseoGenerator *state = generator_state(generator);
207
+ if (tag_of(state->request_tail) == OSEO_TAG_UNDEFINED) {
208
+ state->request_head = published.value;
209
+ } else {
210
+ request_object(state->request_tail)->next = published.value;
211
+ }
212
+ state->request_tail = published.value;
213
+ return normal(oseo_undefined());
214
+ }
215
+
216
+ /* Drops the head request once its step has been reported. */
217
+ static void request_dequeue(OseoValue generator) {
218
+ OseoGenerator *state = generator_state(generator);
219
+ OseoValue head = state->request_head;
220
+ if (tag_of(head) == OSEO_TAG_UNDEFINED) return;
221
+ state->request_head = request_object(head)->next;
222
+ if (tag_of(state->request_head) == OSEO_TAG_UNDEFINED) {
223
+ state->request_tail = oseo_undefined();
224
+ }
225
+ }
226
+
227
+ /*
228
+ * AsyncGeneratorCompleteStep: reports one step into the head request's
229
+ * promise and drops the request. A normal completion reports a fresh
230
+ * { value, done } object and a throw completion rejects instead.
231
+ */
232
+ static OseoResult complete_step(
233
+ OseoContext *context,
234
+ OseoValue generator,
235
+ OseoValue value,
236
+ bool done,
237
+ bool rejected
238
+ ) {
239
+ OseoRootFrame frame = {NULL, NULL, 0u};
240
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
241
+ if (result.status != OSEO_STATUS_NORMAL) return result;
242
+ frame.slots[0] = generator;
243
+ frame.slots[1] = value;
244
+ OseoValue head = generator_state(frame.slots[0])->request_head;
245
+ if (tag_of(head) == OSEO_TAG_UNDEFINED) {
246
+ oseo_roots_release(context, &frame);
247
+ return failure(
248
+ context,
249
+ "OSEO2001",
250
+ "An asynchronous generator step has no pending request."
251
+ );
252
+ }
253
+ frame.slots[2] = request_object(head)->capability;
254
+ request_dequeue(frame.slots[0]);
255
+ if (rejected) {
256
+ result = oseo_promise_reject_into(
257
+ context,
258
+ frame.slots[2],
259
+ frame.slots[1]
260
+ );
261
+ oseo_roots_release(context, &frame);
262
+ return result;
263
+ }
264
+ result = oseo_internal_iterator_result(context, frame.slots[1], done);
265
+ frame.slots[1] = result.value;
266
+ if (result.status == OSEO_STATUS_NORMAL) {
267
+ result = oseo_promise_resolve_into(
268
+ context,
269
+ frame.slots[2],
270
+ frame.slots[1]
271
+ );
272
+ }
273
+ oseo_roots_release(context, &frame);
274
+ return result;
275
+ }
276
+
277
+ /*
278
+ * Installs the two reactions one suspension's awaited operand needs. The
279
+ * generator is left parked, so the driver returns to its caller and the
280
+ * settled operand resumes the body from a promise job.
281
+ *
282
+ * The promise `then` derives is never observed: only these two reactions
283
+ * settle it, and a body throw becomes the pending request's rejection
284
+ * rather than this promise's, so it is marked handled the way every
285
+ * other internal await marks its own. This is the Await operation's sole
286
+ * PromiseResolve: iterator start helpers supply the raw value returned by
287
+ * next, return, or throw rather than resolving it before suspension.
288
+ */
289
+ static OseoResult install_await_reactions(
290
+ OseoContext *context,
291
+ OseoValue generator,
292
+ OseoValue operand
293
+ ) {
294
+ OseoRootFrame frame = {NULL, NULL, 0u};
295
+ OseoResult result = oseo_roots_allocate(context, &frame, 5u);
296
+ if (result.status != OSEO_STATUS_NORMAL) return result;
297
+ frame.slots[0] = generator;
298
+ frame.slots[1] = operand;
299
+ result = oseo_environment_create(context, 1u);
300
+ frame.slots[2] = result.value;
301
+ if (result.status == OSEO_STATUS_NORMAL) {
302
+ result = oseo_environment_set(
303
+ context,
304
+ frame.slots[2],
305
+ 0u,
306
+ frame.slots[0]
307
+ );
308
+ }
309
+ if (result.status == OSEO_STATUS_NORMAL) {
310
+ result = await_reaction_create(
311
+ context,
312
+ frame.slots[2],
313
+ OSEO_ASYNC_GENERATOR_FULFILL_CODE_ID
314
+ );
315
+ frame.slots[3] = result.value;
316
+ }
317
+ if (result.status == OSEO_STATUS_NORMAL) {
318
+ result = await_reaction_create(
319
+ context,
320
+ frame.slots[2],
321
+ OSEO_ASYNC_GENERATOR_REJECT_CODE_ID
322
+ );
323
+ frame.slots[4] = result.value;
324
+ }
325
+ if (result.status == OSEO_STATUS_NORMAL) {
326
+ result = oseo_promise_resolve(context, frame.slots[1]);
327
+ frame.slots[1] = result.value;
328
+ }
329
+ if (result.status == OSEO_STATUS_NORMAL) {
330
+ result = oseo_promise_then(
331
+ context,
332
+ frame.slots[1],
333
+ frame.slots[3],
334
+ frame.slots[4]
335
+ );
336
+ }
337
+ if (result.status == OSEO_STATUS_NORMAL && is_promise(result.value)) {
338
+ OseoPromise *derived = promise_object(result.value);
339
+ derived->handled = true;
340
+ derived->pending_report = false;
341
+ }
342
+ oseo_roots_release(context, &frame);
343
+ return result;
344
+ }
345
+
346
+ /*
347
+ * AsyncGeneratorAwaitReturn: `return` reached a generator that never
348
+ * started or already completed, so the requested value is awaited and
349
+ * becomes that request's own final step without entering a body.
350
+ */
351
+ static OseoResult park_awaiting_return(
352
+ OseoContext *context,
353
+ OseoValue generator,
354
+ OseoValue value
355
+ ) {
356
+ OseoRootFrame frame = {NULL, NULL, 0u};
357
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
358
+ if (result.status != OSEO_STATUS_NORMAL) return result;
359
+ frame.slots[0] = generator;
360
+ frame.slots[1] = value;
361
+ OseoGenerator *state = generator_state(frame.slots[0]);
362
+ state->state = OSEO_GENERATOR_AWAITING;
363
+ state->awaiting_return = true;
364
+ result = install_await_reactions(
365
+ context,
366
+ frame.slots[0],
367
+ frame.slots[1]
368
+ );
369
+ frame.slots[2] = result.value;
370
+ if (result.status == OSEO_STATUS_THROW && !context->has_diagnostic) {
371
+ oseo_context_clear_language_error(context);
372
+ state = generator_state(frame.slots[0]);
373
+ state->state = OSEO_GENERATOR_COMPLETED;
374
+ state->awaiting_return = false;
375
+ result = complete_step(
376
+ context,
377
+ frame.slots[0],
378
+ frame.slots[2],
379
+ true,
380
+ true
381
+ );
382
+ } else if (result.status != OSEO_STATUS_NORMAL) {
383
+ state = generator_state(frame.slots[0]);
384
+ state->state = OSEO_GENERATOR_COMPLETED;
385
+ state->awaiting_return = false;
386
+ }
387
+ oseo_roots_release(context, &frame);
388
+ return result;
389
+ }
390
+
391
+ /*
392
+ * AsyncGeneratorDrainQueue and AsyncGeneratorResume together: run the
393
+ * head request against the body until the body parks on an await or the
394
+ * queue empties. Every exit from the body reports one step, so the loop
395
+ * always makes progress or returns with the generator parked.
396
+ */
397
+ static OseoResult drain_queue(
398
+ OseoContext *context,
399
+ OseoValue generator,
400
+ bool resumed,
401
+ OseoValue resumption_value,
402
+ size_t resumption_kind
403
+ ) {
404
+ OseoRootFrame frame = {NULL, NULL, 0u};
405
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
406
+ if (result.status != OSEO_STATUS_NORMAL) return result;
407
+ frame.slots[0] = generator;
408
+ frame.slots[1] = resumption_value;
409
+ while (result.status == OSEO_STATUS_NORMAL) {
410
+ OseoGenerator *state = generator_state(frame.slots[0]);
411
+ if (state->state == OSEO_GENERATOR_EXECUTING ||
412
+ state->state == OSEO_GENERATOR_AWAITING) {
413
+ break;
414
+ }
415
+ OseoValue head = state->request_head;
416
+ if (tag_of(head) == OSEO_TAG_UNDEFINED) break;
417
+ /* An awaited operand resumes the step the head request already
418
+ * owns, so its value and completion replace that request's for
419
+ * this one entry into the body. */
420
+ size_t resume_kind =
421
+ resumed ? resumption_kind : request_object(head)->resume_kind;
422
+ if (!resumed) frame.slots[1] = request_object(head)->value;
423
+ bool unstarted = state->state == OSEO_GENERATOR_SUSPENDED_START;
424
+ bool completed = state->state == OSEO_GENERATOR_COMPLETED;
425
+ /* A return or a throw delivered before the body ever ran
426
+ * completes the generator without entering it, so no `finally`
427
+ * in a body that never started runs. */
428
+ if (!resumed && (completed || (unstarted &&
429
+ resume_kind != OSEO_GENERATOR_RESUME_NEXT))) {
430
+ if (!completed) oseo_internal_generator_complete(frame.slots[0]);
431
+ if (resume_kind == OSEO_GENERATOR_RESUME_RETURN) {
432
+ result = park_awaiting_return(
433
+ context,
434
+ frame.slots[0],
435
+ frame.slots[1]
436
+ );
437
+ if (result.status == OSEO_STATUS_NORMAL &&
438
+ generator_state(frame.slots[0])->state !=
439
+ OSEO_GENERATOR_AWAITING) {
440
+ continue;
441
+ }
442
+ break;
443
+ }
444
+ bool throwing = resume_kind == OSEO_GENERATOR_RESUME_THROW;
445
+ result = complete_step(
446
+ context,
447
+ frame.slots[0],
448
+ throwing ? frame.slots[1] : oseo_undefined(),
449
+ true,
450
+ throwing
451
+ );
452
+ continue;
453
+ }
454
+ if (context->generator_dispatcher == NULL) {
455
+ oseo_internal_generator_complete(frame.slots[0]);
456
+ result = failure(
457
+ context,
458
+ "OSEO2001",
459
+ "No generated generator dispatcher is installed."
460
+ );
461
+ break;
462
+ }
463
+ state->sent = frame.slots[1];
464
+ state->resume_kind = resume_kind;
465
+ state->state = OSEO_GENERATOR_EXECUTING;
466
+ resumed = false;
467
+ result = oseo_call_enter(context);
468
+ if (result.status != OSEO_STATUS_NORMAL) {
469
+ oseo_internal_generator_complete(frame.slots[0]);
470
+ break;
471
+ }
472
+ OseoRealmScope realm_scope;
473
+ oseo_internal_realm_enter(
474
+ context,
475
+ &realm_scope,
476
+ oseo_internal_callee_realm(
477
+ context,
478
+ generator_state(frame.slots[0])->callee
479
+ )
480
+ );
481
+ /* A resumed body is the running function of the calls it makes
482
+ * (ADR 0034); its generator record roots the callee. */
483
+ OseoActivation activation;
484
+ oseo_internal_activation_push(
485
+ context,
486
+ &activation,
487
+ generator_state(frame.slots[0])->callee,
488
+ 0u,
489
+ NULL
490
+ );
491
+ result = context->generator_dispatcher(context, frame.slots[0]);
492
+ oseo_internal_activation_pop(context, &activation);
493
+ oseo_internal_realm_leave(context, &realm_scope);
494
+ oseo_call_leave(context);
495
+ state = generator_state(frame.slots[0]);
496
+ state->sent = oseo_undefined();
497
+ state->resume_kind = OSEO_GENERATOR_RESUME_NEXT;
498
+ /* The completion value only lives in `result` until it is rooted,
499
+ * and a step reports it after allocations of its own. */
500
+ frame.slots[1] = result.value;
501
+ if (result.status != OSEO_STATUS_NORMAL) {
502
+ /* A host diagnostic is terminal, so it reaches the caller
503
+ * instead of becoming one step's rejection. */
504
+ if (context->has_diagnostic) break;
505
+ oseo_context_clear_language_error(context);
506
+ oseo_internal_generator_complete(frame.slots[0]);
507
+ result = complete_step(
508
+ context,
509
+ frame.slots[0],
510
+ frame.slots[1],
511
+ true,
512
+ true
513
+ );
514
+ continue;
515
+ }
516
+ if (state->state == OSEO_GENERATOR_SUSPENDED_YIELD) {
517
+ if (state->suspend_reason == OSEO_GENERATOR_SUSPEND_AWAIT) {
518
+ state->state = OSEO_GENERATOR_AWAITING;
519
+ result = install_await_reactions(
520
+ context,
521
+ frame.slots[0],
522
+ frame.slots[1]
523
+ );
524
+ if (result.status == OSEO_STATUS_THROW &&
525
+ !context->has_diagnostic) {
526
+ frame.slots[1] = result.value;
527
+ oseo_context_clear_language_error(context);
528
+ state = generator_state(frame.slots[0]);
529
+ state->state = OSEO_GENERATOR_SUSPENDED_YIELD;
530
+ state->suspend_reason = OSEO_GENERATOR_SUSPEND_YIELD;
531
+ result = normal(oseo_undefined());
532
+ resumed = true;
533
+ resumption_kind = OSEO_GENERATOR_RESUME_THROW;
534
+ continue;
535
+ }
536
+ break;
537
+ }
538
+ result = complete_step(
539
+ context,
540
+ frame.slots[0],
541
+ frame.slots[1],
542
+ false,
543
+ false
544
+ );
545
+ continue;
546
+ }
547
+ oseo_internal_generator_complete(frame.slots[0]);
548
+ result = complete_step(
549
+ context,
550
+ frame.slots[0],
551
+ frame.slots[1],
552
+ true,
553
+ false
554
+ );
555
+ }
556
+ if (result.status == OSEO_STATUS_NORMAL) result.value = oseo_undefined();
557
+ oseo_roots_release(context, &frame);
558
+ return result;
559
+ }
560
+
561
+ /*
562
+ * The shared body of the three prototype methods. Every path reports
563
+ * through the promise this creates, including the receiver check, so a
564
+ * misapplied method rejects instead of throwing, which is what the
565
+ * specification's IfAbruptRejectPromise does for each of them.
566
+ */
567
+ static OseoResult async_generator_request(
568
+ OseoContext *context,
569
+ OseoValue generator,
570
+ OseoValue value,
571
+ size_t resume_kind
572
+ ) {
573
+ OseoRootFrame frame = {NULL, NULL, 0u};
574
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
575
+ if (result.status != OSEO_STATUS_NORMAL) return result;
576
+ frame.slots[0] = generator;
577
+ frame.slots[1] = value;
578
+ result = oseo_internal_promise_create(context);
579
+ frame.slots[2] = result.value;
580
+ if (result.status != OSEO_STATUS_NORMAL) {
581
+ oseo_roots_release(context, &frame);
582
+ return result;
583
+ }
584
+ if (!is_async_generator(frame.slots[0])) {
585
+ result = oseo_internal_throw_error(
586
+ context,
587
+ OSEO_ERROR_TYPE,
588
+ "An asynchronous generator method requires an asynchronous "
589
+ "generator receiver."
590
+ );
591
+ OseoValue reason = result.value;
592
+ if (context->has_diagnostic) {
593
+ oseo_roots_release(context, &frame);
594
+ return result;
595
+ }
596
+ oseo_context_clear_language_error(context);
597
+ result = oseo_promise_reject_into(context, frame.slots[2], reason);
598
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[2];
599
+ oseo_roots_release(context, &frame);
600
+ return result;
601
+ }
602
+ result = request_enqueue(
603
+ context,
604
+ frame.slots[0],
605
+ frame.slots[2],
606
+ frame.slots[1],
607
+ resume_kind
608
+ );
609
+ if (result.status == OSEO_STATUS_NORMAL) {
610
+ result = drain_queue(
611
+ context,
612
+ frame.slots[0],
613
+ false,
614
+ oseo_undefined(),
615
+ OSEO_GENERATOR_RESUME_NEXT
616
+ );
617
+ }
618
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[2];
619
+ oseo_roots_release(context, &frame);
620
+ return result;
621
+ }
622
+
623
+ OseoResult oseo_async_generator_next(
624
+ OseoContext *context,
625
+ OseoValue generator,
626
+ OseoValue sent
627
+ ) {
628
+ return async_generator_request(
629
+ context,
630
+ generator,
631
+ sent,
632
+ OSEO_GENERATOR_RESUME_NEXT
633
+ );
634
+ }
635
+
636
+ OseoResult oseo_async_generator_return(
637
+ OseoContext *context,
638
+ OseoValue generator,
639
+ OseoValue value
640
+ ) {
641
+ return async_generator_request(
642
+ context,
643
+ generator,
644
+ value,
645
+ OSEO_GENERATOR_RESUME_RETURN
646
+ );
647
+ }
648
+
649
+ OseoResult oseo_async_generator_throw(
650
+ OseoContext *context,
651
+ OseoValue generator,
652
+ OseoValue reason
653
+ ) {
654
+ return async_generator_request(
655
+ context,
656
+ generator,
657
+ reason,
658
+ OSEO_GENERATOR_RESUME_THROW
659
+ );
660
+ }
661
+
662
+ /*
663
+ * Settles the one capability owned by an ordinary asynchronous frame and
664
+ * discards the frame before returning. The capability is rooted separately
665
+ * because completing the frame clears every retained execution value.
666
+ */
667
+ static OseoResult complete_async_function(
668
+ OseoContext *context,
669
+ OseoValue generator,
670
+ OseoValue value,
671
+ bool rejected
672
+ ) {
673
+ OseoRootFrame frame = {NULL, NULL, 0u};
674
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
675
+ if (result.status != OSEO_STATUS_NORMAL) return result;
676
+ frame.slots[0] = generator;
677
+ frame.slots[1] = value;
678
+ OseoGenerator *state = generator_state(frame.slots[0]);
679
+ frame.slots[2] = state->async_function_capability;
680
+ if (!is_promise(frame.slots[2])) {
681
+ oseo_roots_release(context, &frame);
682
+ return failure(
683
+ context,
684
+ "OSEO2001",
685
+ "An asynchronous function frame has no promise capability."
686
+ );
687
+ }
688
+ state->async_function_capability = oseo_undefined();
689
+ oseo_internal_generator_complete(frame.slots[0]);
690
+ result = rejected
691
+ ? oseo_promise_reject_into(context, frame.slots[2], frame.slots[1])
692
+ : oseo_promise_resolve_into(context, frame.slots[2], frame.slots[1]);
693
+ if (result.status == OSEO_STATUS_NORMAL) {
694
+ result.value = oseo_undefined();
695
+ }
696
+ oseo_roots_release(context, &frame);
697
+ return result;
698
+ }
699
+
700
+ /*
701
+ * Runs an ordinary asynchronous body until it completes or parks on an
702
+ * await. The frame uses the asynchronous-generator body dispatcher, but it
703
+ * has no request queue and never exposes an iterator result.
704
+ */
705
+ static OseoResult drive_async_function(
706
+ OseoContext *context,
707
+ OseoValue generator,
708
+ bool resumed,
709
+ OseoValue resumption_value,
710
+ bool rejected
711
+ ) {
712
+ if (!is_generator(generator) ||
713
+ !generator_state(generator)->async_function) {
714
+ return failure(
715
+ context,
716
+ "OSEO2001",
717
+ "An asynchronous function requires its internal frame."
718
+ );
719
+ }
720
+ OseoGenerator *state = generator_state(generator);
721
+ if (resumed && state->state != OSEO_GENERATOR_AWAITING) {
722
+ return failure(
723
+ context,
724
+ "OSEO2001",
725
+ "An await resumed an asynchronous function that was not awaiting."
726
+ );
727
+ }
728
+ if (!resumed && state->state != OSEO_GENERATOR_SUSPENDED_START) {
729
+ return failure(
730
+ context,
731
+ "OSEO2001",
732
+ "An asynchronous function frame was started more than once."
733
+ );
734
+ }
735
+ if (context->generator_dispatcher == NULL) {
736
+ return failure(
737
+ context,
738
+ "OSEO2001",
739
+ "No generated generator dispatcher is installed."
740
+ );
741
+ }
742
+ OseoRootFrame frame = {NULL, NULL, 0u};
743
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
744
+ if (result.status != OSEO_STATUS_NORMAL) return result;
745
+ frame.slots[0] = generator;
746
+ frame.slots[1] = resumption_value;
747
+ resume_body:
748
+ state = generator_state(frame.slots[0]);
749
+ if (resumed) {
750
+ state->state = OSEO_GENERATOR_SUSPENDED_YIELD;
751
+ state->suspend_reason = OSEO_GENERATOR_SUSPEND_YIELD;
752
+ }
753
+ state->sent = frame.slots[1];
754
+ state->resume_kind = rejected
755
+ ? OSEO_GENERATOR_RESUME_THROW
756
+ : OSEO_GENERATOR_RESUME_NEXT;
757
+ state->state = OSEO_GENERATOR_EXECUTING;
758
+ result = oseo_call_enter(context);
759
+ if (result.status == OSEO_STATUS_NORMAL) {
760
+ OseoRealmScope realm_scope;
761
+ oseo_internal_realm_enter(
762
+ context,
763
+ &realm_scope,
764
+ oseo_internal_callee_realm(
765
+ context,
766
+ generator_state(frame.slots[0])->callee
767
+ )
768
+ );
769
+ /* A resumed body is the running function of the calls it makes
770
+ * (ADR 0034); its generator record roots the callee. */
771
+ OseoActivation activation;
772
+ oseo_internal_activation_push(
773
+ context,
774
+ &activation,
775
+ generator_state(frame.slots[0])->callee,
776
+ 0u,
777
+ NULL
778
+ );
779
+ result = context->generator_dispatcher(context, frame.slots[0]);
780
+ oseo_internal_activation_pop(context, &activation);
781
+ oseo_internal_realm_leave(context, &realm_scope);
782
+ oseo_call_leave(context);
783
+ }
784
+ state = generator_state(frame.slots[0]);
785
+ state->sent = oseo_undefined();
786
+ state->resume_kind = OSEO_GENERATOR_RESUME_NEXT;
787
+ frame.slots[1] = result.value;
788
+ if (result.status != OSEO_STATUS_NORMAL) {
789
+ if (context->has_diagnostic) {
790
+ oseo_internal_generator_complete(frame.slots[0]);
791
+ } else {
792
+ oseo_context_clear_language_error(context);
793
+ result = complete_async_function(
794
+ context,
795
+ frame.slots[0],
796
+ frame.slots[1],
797
+ true
798
+ );
799
+ }
800
+ oseo_roots_release(context, &frame);
801
+ return result;
802
+ }
803
+ if (state->state == OSEO_GENERATOR_SUSPENDED_YIELD) {
804
+ if (state->suspend_reason != OSEO_GENERATOR_SUSPEND_AWAIT) {
805
+ oseo_roots_release(context, &frame);
806
+ return failure(
807
+ context,
808
+ "OSEO2001",
809
+ "An ordinary asynchronous function tried to yield."
810
+ );
811
+ }
812
+ state->state = OSEO_GENERATOR_AWAITING;
813
+ result = install_await_reactions(
814
+ context,
815
+ frame.slots[0],
816
+ frame.slots[1]
817
+ );
818
+ if (result.status == OSEO_STATUS_THROW &&
819
+ !context->has_diagnostic) {
820
+ frame.slots[1] = result.value;
821
+ oseo_context_clear_language_error(context);
822
+ resumed = true;
823
+ rejected = true;
824
+ goto resume_body;
825
+ }
826
+ if (result.status == OSEO_STATUS_NORMAL) {
827
+ result.value = oseo_undefined();
828
+ }
829
+ oseo_roots_release(context, &frame);
830
+ return result;
831
+ }
832
+ result = complete_async_function(
833
+ context,
834
+ frame.slots[0],
835
+ frame.slots[1],
836
+ false
837
+ );
838
+ oseo_roots_release(context, &frame);
839
+ return result;
840
+ }
841
+
842
+ OseoResult oseo_async_function_start(
843
+ OseoContext *context,
844
+ OseoValue generator
845
+ ) {
846
+ OseoValue slots[2] = {generator, oseo_undefined()};
847
+ OseoRootFrame frame = {NULL, slots, 2u};
848
+ oseo_roots_push(context, &frame);
849
+ if (!is_generator(slots[0]) ||
850
+ !generator_state(slots[0])->async_function) {
851
+ oseo_roots_pop(context, &frame);
852
+ return failure(
853
+ context,
854
+ "OSEO2001",
855
+ "An asynchronous call did not create its internal frame."
856
+ );
857
+ }
858
+ OseoResult result = oseo_internal_promise_create(context);
859
+ slots[1] = result.value;
860
+ if (result.status == OSEO_STATUS_NORMAL) {
861
+ generator_state(slots[0])->async_function_capability = slots[1];
862
+ result = drive_async_function(
863
+ context,
864
+ slots[0],
865
+ false,
866
+ oseo_undefined(),
867
+ false
868
+ );
869
+ }
870
+ if (result.status == OSEO_STATUS_NORMAL) result.value = slots[1];
871
+ oseo_roots_pop(context, &frame);
872
+ return result;
873
+ }
874
+
875
+ OseoResult oseo_async_function_reject(
876
+ OseoContext *context,
877
+ OseoValue reason
878
+ ) {
879
+ OseoValue slots[2] = {reason, oseo_undefined()};
880
+ OseoRootFrame frame = {NULL, slots, 2u};
881
+ oseo_roots_push(context, &frame);
882
+ OseoResult result = oseo_internal_promise_create(context);
883
+ slots[1] = result.value;
884
+ if (result.status == OSEO_STATUS_NORMAL) {
885
+ result = oseo_promise_reject_into(
886
+ context,
887
+ slots[1],
888
+ slots[0]
889
+ );
890
+ }
891
+ if (result.status == OSEO_STATUS_NORMAL) result.value = slots[1];
892
+ oseo_roots_pop(context, &frame);
893
+ return result;
894
+ }
895
+
896
+ OseoResult oseo_internal_async_generator_awaited(
897
+ OseoContext *context,
898
+ OseoValue generator,
899
+ OseoValue value,
900
+ bool rejected
901
+ ) {
902
+ if (!is_generator(generator) ||
903
+ !generator_state(generator)->asynchronous) {
904
+ return failure(
905
+ context,
906
+ "OSEO2001",
907
+ "An await resumption reached a value that is not an "
908
+ "asynchronous generator."
909
+ );
910
+ }
911
+ OseoGenerator *state = generator_state(generator);
912
+ if (state->state != OSEO_GENERATOR_AWAITING) {
913
+ return failure(
914
+ context,
915
+ "OSEO2001",
916
+ "An await resumption reached a generator that is not awaiting."
917
+ );
918
+ }
919
+ OseoRootFrame frame = {NULL, NULL, 0u};
920
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
921
+ if (result.status != OSEO_STATUS_NORMAL) return result;
922
+ frame.slots[0] = generator;
923
+ frame.slots[1] = value;
924
+ if (state->async_function) {
925
+ result = drive_async_function(
926
+ context,
927
+ frame.slots[0],
928
+ true,
929
+ frame.slots[1],
930
+ rejected
931
+ );
932
+ oseo_roots_release(context, &frame);
933
+ return result;
934
+ }
935
+ if (state->awaiting_return) {
936
+ state->awaiting_return = false;
937
+ state->state = OSEO_GENERATOR_COMPLETED;
938
+ result = complete_step(
939
+ context,
940
+ frame.slots[0],
941
+ frame.slots[1],
942
+ true,
943
+ rejected
944
+ );
945
+ if (result.status == OSEO_STATUS_NORMAL) {
946
+ result = drain_queue(
947
+ context,
948
+ frame.slots[0],
949
+ false,
950
+ oseo_undefined(),
951
+ OSEO_GENERATOR_RESUME_NEXT
952
+ );
953
+ }
954
+ oseo_roots_release(context, &frame);
955
+ return result;
956
+ }
957
+ /* The parked suspension becomes resumable again, and the resumption
958
+ * carries the settled operand: a fulfilled one delivers its value at
959
+ * the await position, and a rejected one delivers a throw completion
960
+ * the body raises there. The head request keeps owning the step. */
961
+ state->state = OSEO_GENERATOR_SUSPENDED_YIELD;
962
+ state->suspend_reason = OSEO_GENERATOR_SUSPEND_YIELD;
963
+ result = drain_queue(
964
+ context,
965
+ frame.slots[0],
966
+ true,
967
+ frame.slots[1],
968
+ rejected ? OSEO_GENERATOR_RESUME_THROW : OSEO_GENERATOR_RESUME_NEXT
969
+ );
970
+ oseo_roots_release(context, &frame);
971
+ return result;
972
+ }