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

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