@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,1120 @@
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
+ OseoResult oseo_internal_generator_builtin_dispatch(
34
+ OseoContext *context,
35
+ size_t code_id,
36
+ OseoValue callee,
37
+ OseoValue receiver,
38
+ size_t argument_count,
39
+ const OseoValue *arguments,
40
+ OseoValue new_target
41
+ ) {
42
+ (void)callee;
43
+ (void)new_target;
44
+ OseoValue argument = argument_count > 0u
45
+ ? arguments[0]
46
+ : oseo_undefined();
47
+ if (code_id == OSEO_GENERATOR_NEXT_CODE_ID) {
48
+ return oseo_generator_next(context, receiver, argument);
49
+ }
50
+ if (code_id == OSEO_GENERATOR_RETURN_CODE_ID) {
51
+ return oseo_generator_return(context, receiver, argument);
52
+ }
53
+ if (code_id == OSEO_GENERATOR_THROW_CODE_ID) {
54
+ return oseo_generator_throw(context, receiver, argument);
55
+ }
56
+ if (code_id == OSEO_ASYNC_ITERATOR_SELF_CODE_ID) {
57
+ return normal(receiver);
58
+ }
59
+ /* ADR 0016 keeps every form that compiles source text at run time
60
+ * outside the profile. Each constructor is a materialized intrinsic
61
+ * value, so the chains above it are complete; only reaching its
62
+ * [[Call]] or [[Construct]] reports this boundary. */
63
+ if (code_id == OSEO_ASYNC_GENERATOR_FUNCTION_CODE_ID) {
64
+ return failure(
65
+ context,
66
+ "OSEO1001",
67
+ "AsyncGeneratorFunction compiles source text at run time, "
68
+ "which is outside the admitted profile."
69
+ );
70
+ }
71
+ if (code_id == OSEO_GENERATOR_FUNCTION_CODE_ID) {
72
+ return failure(
73
+ context,
74
+ "OSEO1001",
75
+ "GeneratorFunction compiles source text at run time, "
76
+ "which is outside the admitted profile."
77
+ );
78
+ }
79
+ if (code_id == OSEO_ASYNC_FUNCTION_CODE_ID) {
80
+ return failure(
81
+ context,
82
+ "OSEO1001",
83
+ "AsyncFunction compiles source text at run time, "
84
+ "which is outside the admitted profile."
85
+ );
86
+ }
87
+ return oseo_unknown_function(context, code_id);
88
+ }
89
+
90
+ /*
91
+ * Generator objects: the suspended body frame shared by both generator
92
+ * kinds, its collector-traced root slots and saved completion records,
93
+ * and the %GeneratorPrototype% resumptions that drive a synchronous one.
94
+ * The asynchronous driver lives in *runtime_async_generator.c* and
95
+ * reuses the same record.
96
+ *
97
+ * A generator's roots live in the generator record rather than on the
98
+ * native root stack, so no C frame of a suspended body stays alive. The
99
+ * record is allocated once, is never resized, and the collector never
100
+ * moves it, so generated code can hold `roots` across every safepoint
101
+ * in the resumed body.
102
+ */
103
+
104
+ static OseoGenerator *generator_state(OseoValue value) {
105
+ return ordinary_object(value)->generator;
106
+ }
107
+
108
+ static OseoResult ascii_generator_string(
109
+ OseoContext *context,
110
+ const char *text
111
+ ) {
112
+ size_t length = strlen(text);
113
+ uint16_t *units = NULL;
114
+ if (length > 0u) {
115
+ units = malloc(length * sizeof(uint16_t));
116
+ if (units == NULL) {
117
+ return failure(context, "OSEO2001", "String allocation failed.");
118
+ }
119
+ for (size_t index = 0u; index < length; index += 1u) {
120
+ units[index] = (uint16_t)(unsigned char)text[index];
121
+ }
122
+ }
123
+ OseoResult result = oseo_internal_allocate_string(context, units, length);
124
+ free(units);
125
+ return result;
126
+ }
127
+
128
+ /* Define one ASCII-named data property; object and value stay rooted. */
129
+ static OseoResult define_ascii_property(
130
+ OseoContext *context,
131
+ OseoValue object,
132
+ const char *name,
133
+ OseoValue value,
134
+ OseoPropertyAttributes attributes
135
+ ) {
136
+ OseoValue slots[3] = {object, value, oseo_undefined()};
137
+ OseoRootFrame frame = {NULL, slots, 3u};
138
+ oseo_roots_push(context, &frame);
139
+ OseoResult result = ascii_generator_string(context, name);
140
+ if (result.status == OSEO_STATUS_NORMAL) {
141
+ slots[2] = result.value;
142
+ result = oseo_object_define(
143
+ context,
144
+ slots[0],
145
+ slots[2],
146
+ slots[1],
147
+ attributes
148
+ );
149
+ }
150
+ oseo_roots_pop(context, &frame);
151
+ return result;
152
+ }
153
+
154
+ /* The same for a well-known symbol key; object and value stay rooted. */
155
+ static OseoResult define_symbol_property(
156
+ OseoContext *context,
157
+ OseoValue object,
158
+ size_t well_known,
159
+ OseoValue value,
160
+ OseoPropertyAttributes attributes
161
+ ) {
162
+ OseoValue slots[3] = {object, value, oseo_undefined()};
163
+ OseoRootFrame frame = {NULL, slots, 3u};
164
+ oseo_roots_push(context, &frame);
165
+ OseoResult result =
166
+ oseo_internal_well_known_symbol(context, well_known);
167
+ if (result.status == OSEO_STATUS_NORMAL) {
168
+ slots[2] = result.value;
169
+ result = oseo_object_define(
170
+ context,
171
+ slots[0],
172
+ slots[2],
173
+ slots[1],
174
+ attributes
175
+ );
176
+ }
177
+ oseo_roots_pop(context, &frame);
178
+ return result;
179
+ }
180
+
181
+ /* The same for `Symbol.toStringTag` and an ASCII tag value. */
182
+ static OseoResult define_to_string_tag(
183
+ OseoContext *context,
184
+ OseoValue object,
185
+ const char *tag,
186
+ OseoPropertyAttributes attributes
187
+ ) {
188
+ OseoValue slots[1] = {object};
189
+ OseoRootFrame frame = {NULL, slots, 1u};
190
+ oseo_roots_push(context, &frame);
191
+ OseoResult result = ascii_generator_string(context, tag);
192
+ if (result.status == OSEO_STATUS_NORMAL) {
193
+ result = define_symbol_property(
194
+ context,
195
+ slots[0],
196
+ OSEO_WELL_KNOWN_TO_STRING_TAG,
197
+ result.value,
198
+ attributes
199
+ );
200
+ }
201
+ oseo_roots_pop(context, &frame);
202
+ return result;
203
+ }
204
+
205
+ /*
206
+ * Install one built-in method. Every method is cached on the context, so
207
+ * the value this defines is permanently rooted and two generators created
208
+ * from different functions share one method identity.
209
+ */
210
+ static OseoResult define_method(
211
+ OseoContext *context,
212
+ OseoValue object,
213
+ const char *name,
214
+ size_t code_id
215
+ ) {
216
+ /* A built-in method is writable, non-enumerable, and configurable. */
217
+ const OseoPropertyAttributes method = {true, false, true, false};
218
+ OseoValue slots[1] = {object};
219
+ OseoRootFrame frame = {NULL, slots, 1u};
220
+ oseo_roots_push(context, &frame);
221
+ OseoResult result =
222
+ oseo_internal_async_generator_method(context, code_id);
223
+ if (result.status == OSEO_STATUS_NORMAL) {
224
+ result = define_ascii_property(
225
+ context,
226
+ slots[0],
227
+ name,
228
+ result.value,
229
+ method
230
+ );
231
+ }
232
+ oseo_roots_pop(context, &frame);
233
+ return result;
234
+ }
235
+
236
+ /*
237
+ * The constructor half of one dynamic-source function intrinsic:
238
+ * %GeneratorFunction%, %AsyncGeneratorFunction%, or %AsyncFunction%.
239
+ * Each is an ordinary built-in constructor whose [[Prototype]] is
240
+ * %Function%, so the chain a reflected function exposes reaches the
241
+ * same `call`, `bind`, and `apply` the specification places above it.
242
+ * Only its [[Call]] and [[Construct]] stay outside the profile.
243
+ */
244
+ static OseoResult dynamic_source_constructor(
245
+ OseoContext *context,
246
+ size_t code_id,
247
+ const uint16_t *name_units,
248
+ size_t name_length,
249
+ OseoValue prototype_property
250
+ ) {
251
+ OseoValue slots[3] = {prototype_property, oseo_undefined(),
252
+ oseo_undefined()};
253
+ OseoRootFrame frame = {NULL, slots, 3u};
254
+ oseo_roots_push(context, &frame);
255
+ OseoResult result = oseo_internal_intrinsic(
256
+ context,
257
+ OSEO_INTRINSIC_FUNCTION
258
+ );
259
+ slots[1] = result.value;
260
+ if (result.status == OSEO_STATUS_NORMAL) {
261
+ result = oseo_environment_create(context, 0u);
262
+ slots[2] = result.value;
263
+ }
264
+ if (result.status == OSEO_STATUS_NORMAL) {
265
+ result = oseo_function_create(
266
+ context,
267
+ code_id,
268
+ slots[2],
269
+ name_units,
270
+ name_length,
271
+ 1u,
272
+ OSEO_FUNCTION_ORDINARY,
273
+ oseo_undefined(),
274
+ oseo_undefined(),
275
+ OSEO_FUNCTION_NAME_PREFIX_NONE
276
+ );
277
+ slots[2] = result.value;
278
+ }
279
+ if (result.status == OSEO_STATUS_NORMAL) {
280
+ OseoFunction *constructor = function_object(slots[2]);
281
+ /* The constructor was allocated one statement ago and no read has
282
+ * reached it, so relinking its [[Prototype]] cannot invalidate a
283
+ * shape assumption and keeps it out of dictionary mode, the way a
284
+ * class definition relinks the two objects it allocates. */
285
+ constructor->ordinary.prototype = slots[1];
286
+ constructor->ordinary.shape_id = context->next_shape_id;
287
+ context->next_shape_id += 1u;
288
+ constructor->prototype_object = slots[0];
289
+ constructor->prototype_writable = false;
290
+ result = normal(slots[2]);
291
+ }
292
+ oseo_roots_pop(context, &frame);
293
+ return result;
294
+ }
295
+
296
+ /*
297
+ * The synchronous generator intrinsics, created as one cluster for the
298
+ * reason the asynchronous cluster below is: %GeneratorFunction% names
299
+ * %GeneratorFunction.prototype% as its `prototype`, that object names
300
+ * the constructor back and %GeneratorPrototype% as its own `prototype`,
301
+ * and %GeneratorPrototype% names it as `constructor`. Splitting them
302
+ * would let a program observe a %GeneratorPrototype% whose `constructor`
303
+ * and `Symbol.toStringTag` are still missing.
304
+ *
305
+ * The cluster reaches the context only after every property is defined,
306
+ * so a failed allocation leaves no partially wired intrinsic behind, and
307
+ * its allocations are restored because an intrinsic created on first use
308
+ * is not one the observed program performed.
309
+ *
310
+ * OrdinaryOwnPropertyKeys reports non-index string keys in the order
311
+ * they were created, and every key here is one, so creation order is
312
+ * observable as soon as a program makes a configurable link enumerable.
313
+ * Each object therefore defines its properties in the order the reference
314
+ * hosts expose them:
315
+ * %GeneratorFunction.prototype% takes `prototype` before `constructor`,
316
+ * and %GeneratorPrototype% takes `constructor` before `next`, `return`,
317
+ * and `throw`. The allocation order stays free, because only a
318
+ * definition creates a key.
319
+ */
320
+ OseoResult oseo_internal_generator_prototype(OseoContext *context) {
321
+ OseoValue cached =
322
+ context->realm->intrinsics[OSEO_INTRINSIC_GENERATOR_PROTOTYPE];
323
+ if (tag_of(cached) != OSEO_TAG_UNDEFINED) return normal(cached);
324
+ size_t entry_allocations = context->allocations;
325
+ /* A built-in `constructor`, `prototype`, or `Symbol.toStringTag` is
326
+ * non-writable, non-enumerable, and configurable. */
327
+ const OseoPropertyAttributes hidden = {true, false, false, false};
328
+ OseoRootFrame frame = {NULL, NULL, 0u};
329
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
330
+ if (result.status != OSEO_STATUS_NORMAL) return result;
331
+ result = oseo_internal_array_iterator_prototype(context);
332
+ if (result.status == OSEO_STATUS_NORMAL) {
333
+ result = oseo_object_create(
334
+ context,
335
+ context->realm->intrinsics[OSEO_INTRINSIC_ITERATOR_PROTOTYPE]
336
+ );
337
+ frame.slots[0] = result.value;
338
+ }
339
+ if (result.status == OSEO_STATUS_NORMAL) {
340
+ result = oseo_internal_intrinsic(
341
+ context,
342
+ OSEO_INTRINSIC_FUNCTION_PROTOTYPE
343
+ );
344
+ frame.slots[1] = result.value;
345
+ }
346
+ if (result.status == OSEO_STATUS_NORMAL) {
347
+ result = oseo_object_create(context, frame.slots[1]);
348
+ frame.slots[2] = result.value;
349
+ }
350
+ if (result.status == OSEO_STATUS_NORMAL) {
351
+ static const uint16_t constructor_units[] = {
352
+ 'G', 'e', 'n', 'e', 'r', 'a', 't', 'o', 'r',
353
+ 'F', 'u', 'n', 'c', 't', 'i', 'o', 'n'
354
+ };
355
+ result = dynamic_source_constructor(
356
+ context,
357
+ OSEO_GENERATOR_FUNCTION_CODE_ID,
358
+ constructor_units,
359
+ sizeof(constructor_units) / sizeof(*constructor_units),
360
+ frame.slots[2]
361
+ );
362
+ frame.slots[3] = result.value;
363
+ }
364
+ if (result.status == OSEO_STATUS_NORMAL) {
365
+ result = define_ascii_property(
366
+ context,
367
+ frame.slots[2],
368
+ "prototype",
369
+ frame.slots[0],
370
+ hidden
371
+ );
372
+ }
373
+ if (result.status == OSEO_STATUS_NORMAL) {
374
+ result = define_ascii_property(
375
+ context,
376
+ frame.slots[2],
377
+ "constructor",
378
+ frame.slots[3],
379
+ hidden
380
+ );
381
+ }
382
+ if (result.status == OSEO_STATUS_NORMAL) {
383
+ result = define_to_string_tag(
384
+ context,
385
+ frame.slots[2],
386
+ "GeneratorFunction",
387
+ hidden
388
+ );
389
+ }
390
+ if (result.status == OSEO_STATUS_NORMAL) {
391
+ result = define_ascii_property(
392
+ context,
393
+ frame.slots[0],
394
+ "constructor",
395
+ frame.slots[2],
396
+ hidden
397
+ );
398
+ }
399
+ static const char *const names[] = {"next", "return", "throw"};
400
+ static const size_t codes[] = {
401
+ OSEO_GENERATOR_NEXT_CODE_ID,
402
+ OSEO_GENERATOR_RETURN_CODE_ID,
403
+ OSEO_GENERATOR_THROW_CODE_ID,
404
+ };
405
+ const OseoPropertyAttributes method = {true, false, true, false};
406
+ for (size_t index = 0u;
407
+ result.status == OSEO_STATUS_NORMAL && index < 3u;
408
+ index += 1u) {
409
+ result = oseo_internal_iterator_method(context, codes[index]);
410
+ frame.slots[1] = result.value;
411
+ if (result.status != OSEO_STATUS_NORMAL) break;
412
+ result = define_ascii_property(
413
+ context,
414
+ frame.slots[0],
415
+ names[index],
416
+ frame.slots[1],
417
+ method
418
+ );
419
+ }
420
+ if (result.status == OSEO_STATUS_NORMAL) {
421
+ result = define_to_string_tag(
422
+ context,
423
+ frame.slots[0],
424
+ "Generator",
425
+ hidden
426
+ );
427
+ }
428
+ if (result.status == OSEO_STATUS_NORMAL) {
429
+ context->realm->intrinsics[OSEO_INTRINSIC_GENERATOR_PROTOTYPE] =
430
+ frame.slots[0];
431
+ context->realm->intrinsics[
432
+ OSEO_INTRINSIC_GENERATOR_FUNCTION_PROTOTYPE
433
+ ] = frame.slots[2];
434
+ context->realm->intrinsics[OSEO_INTRINSIC_GENERATOR_FUNCTION] =
435
+ frame.slots[3];
436
+ result.value = frame.slots[0];
437
+ if (context->observe_specialization) {
438
+ context->allocations = entry_allocations;
439
+ }
440
+ }
441
+ oseo_roots_release(context, &frame);
442
+ return result;
443
+ }
444
+
445
+ OseoResult oseo_internal_generator_function_intrinsic(OseoContext *context) {
446
+ OseoResult result = oseo_internal_generator_prototype(context);
447
+ if (result.status != OSEO_STATUS_NORMAL) return result;
448
+ return normal(context->realm->intrinsics[
449
+ OSEO_INTRINSIC_GENERATOR_FUNCTION_PROTOTYPE
450
+ ]);
451
+ }
452
+
453
+ /*
454
+ * The asynchronous function intrinsics. %AsyncFunction.prototype% is an
455
+ * ordinary object rather than a callable one, and, unlike the two
456
+ * generator function prototypes, it carries no `prototype` property:
457
+ * an asynchronous function is not a constructor and creates no
458
+ * instances. `constructor` is therefore its only string key, and the
459
+ * creation order the two generator clusters have to arrange is already
460
+ * the one this object exposes.
461
+ */
462
+ OseoResult oseo_internal_async_function_intrinsic(OseoContext *context) {
463
+ OseoValue cached =
464
+ context->realm->intrinsics[OSEO_INTRINSIC_ASYNC_FUNCTION_PROTOTYPE];
465
+ if (tag_of(cached) != OSEO_TAG_UNDEFINED) return normal(cached);
466
+ size_t entry_allocations = context->allocations;
467
+ const OseoPropertyAttributes hidden = {true, false, false, false};
468
+ OseoRootFrame frame = {NULL, NULL, 0u};
469
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
470
+ if (result.status != OSEO_STATUS_NORMAL) return result;
471
+ result = oseo_internal_intrinsic(
472
+ context,
473
+ OSEO_INTRINSIC_FUNCTION_PROTOTYPE
474
+ );
475
+ frame.slots[0] = result.value;
476
+ if (result.status == OSEO_STATUS_NORMAL) {
477
+ result = oseo_object_create(context, frame.slots[0]);
478
+ frame.slots[1] = result.value;
479
+ }
480
+ if (result.status == OSEO_STATUS_NORMAL) {
481
+ static const uint16_t constructor_units[] = {
482
+ 'A', 's', 'y', 'n', 'c', 'F', 'u', 'n', 'c', 't', 'i', 'o', 'n'
483
+ };
484
+ result = dynamic_source_constructor(
485
+ context,
486
+ OSEO_ASYNC_FUNCTION_CODE_ID,
487
+ constructor_units,
488
+ sizeof(constructor_units) / sizeof(*constructor_units),
489
+ frame.slots[1]
490
+ );
491
+ frame.slots[2] = result.value;
492
+ }
493
+ if (result.status == OSEO_STATUS_NORMAL) {
494
+ result = define_ascii_property(
495
+ context,
496
+ frame.slots[1],
497
+ "constructor",
498
+ frame.slots[2],
499
+ hidden
500
+ );
501
+ }
502
+ if (result.status == OSEO_STATUS_NORMAL) {
503
+ result = define_to_string_tag(
504
+ context,
505
+ frame.slots[1],
506
+ "AsyncFunction",
507
+ hidden
508
+ );
509
+ }
510
+ if (result.status == OSEO_STATUS_NORMAL) {
511
+ context->realm->intrinsics[OSEO_INTRINSIC_ASYNC_FUNCTION_PROTOTYPE] =
512
+ frame.slots[1];
513
+ context->realm->intrinsics[OSEO_INTRINSIC_ASYNC_FUNCTION] =
514
+ frame.slots[2];
515
+ result.value = frame.slots[1];
516
+ if (context->observe_specialization) {
517
+ context->allocations = entry_allocations;
518
+ }
519
+ }
520
+ oseo_roots_release(context, &frame);
521
+ return result;
522
+ }
523
+
524
+ /*
525
+ * The asynchronous generator intrinsics, created as one cluster because
526
+ * their links are circular: %AsyncGeneratorFunction% names
527
+ * %AsyncGeneratorFunction.prototype% as its `prototype`, that object
528
+ * names the constructor back and %AsyncGeneratorPrototype% as its own
529
+ * `prototype`, and %AsyncGeneratorPrototype% names it as `constructor`.
530
+ * Every method is an ordinary own property of the object the
531
+ * specification places it on rather than a brand the property read
532
+ * synthesizes, so a descriptor read, a deletion, and an assignment all
533
+ * observe the same set a read does, and a generator function whose
534
+ * `prototype` is replaced reaches exactly the methods the replacement's
535
+ * chain still retains.
536
+ *
537
+ * The cluster reaches the context only after every property is defined,
538
+ * so a failed allocation leaves no partially wired intrinsic behind and
539
+ * the next reference rebuilds it. Its allocations are restored the way
540
+ * the error intrinsics restore theirs, because an intrinsic created on
541
+ * first use is not one the observed program performed.
542
+ *
543
+ * Every rootward [[Prototype]] link uses the realm-owned object or
544
+ * function prototype rather than a parallel null-terminated chain.
545
+ *
546
+ * Property creation order follows the synchronous cluster's, for the
547
+ * same observability reason: %AsyncGeneratorFunction.prototype% takes
548
+ * `prototype` before `constructor`, and %AsyncGeneratorPrototype% takes
549
+ * `constructor` before `next`, `return`, and `throw`.
550
+ */
551
+ static OseoResult async_generator_intrinsics(OseoContext *context) {
552
+ OseoValue cached =
553
+ context->realm->intrinsics[OSEO_INTRINSIC_ASYNC_GENERATOR_PROTOTYPE];
554
+ if (tag_of(cached) != OSEO_TAG_UNDEFINED) {
555
+ return normal(cached);
556
+ }
557
+ size_t entry_allocations = context->allocations;
558
+ const OseoPropertyAttributes hidden = {true, false, false, false};
559
+ OseoRootFrame frame = {NULL, NULL, 0u};
560
+ OseoResult result = oseo_roots_allocate(context, &frame, 6u);
561
+ if (result.status != OSEO_STATUS_NORMAL) return result;
562
+ result = oseo_internal_intrinsic(
563
+ context,
564
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
565
+ );
566
+ frame.slots[4] = result.value;
567
+ if (result.status == OSEO_STATUS_NORMAL) {
568
+ result = oseo_internal_intrinsic(
569
+ context,
570
+ OSEO_INTRINSIC_FUNCTION_PROTOTYPE
571
+ );
572
+ frame.slots[5] = result.value;
573
+ }
574
+ if (result.status == OSEO_STATUS_NORMAL) {
575
+ result = oseo_object_create(context, frame.slots[4]);
576
+ frame.slots[0] = result.value;
577
+ }
578
+ if (result.status == OSEO_STATUS_NORMAL) {
579
+ result = oseo_internal_async_generator_method(
580
+ context,
581
+ OSEO_ASYNC_ITERATOR_SELF_CODE_ID
582
+ );
583
+ }
584
+ if (result.status == OSEO_STATUS_NORMAL) {
585
+ const OseoPropertyAttributes method = {true, false, true, false};
586
+ result = define_symbol_property(
587
+ context,
588
+ frame.slots[0],
589
+ OSEO_WELL_KNOWN_ASYNC_ITERATOR,
590
+ result.value,
591
+ method
592
+ );
593
+ }
594
+ if (result.status == OSEO_STATUS_NORMAL) {
595
+ result = oseo_internal_iterator_dispose_function(
596
+ context,
597
+ OSEO_ASYNC_ITERATOR_ASYNC_DISPOSE_CODE_ID
598
+ );
599
+ }
600
+ if (result.status == OSEO_STATUS_NORMAL) {
601
+ const OseoPropertyAttributes method = {true, false, true, false};
602
+ result = define_symbol_property(
603
+ context,
604
+ frame.slots[0],
605
+ OSEO_WELL_KNOWN_ASYNC_DISPOSE,
606
+ result.value,
607
+ method
608
+ );
609
+ }
610
+ if (result.status == OSEO_STATUS_NORMAL) {
611
+ result = oseo_object_create(context, frame.slots[0]);
612
+ frame.slots[1] = result.value;
613
+ }
614
+ if (result.status == OSEO_STATUS_NORMAL) {
615
+ result = oseo_object_create(context, frame.slots[5]);
616
+ frame.slots[2] = result.value;
617
+ }
618
+ if (result.status == OSEO_STATUS_NORMAL) {
619
+ static const uint16_t constructor_units[] = {
620
+ 'A', 's', 'y', 'n', 'c', 'G', 'e', 'n', 'e', 'r', 'a', 't',
621
+ 'o', 'r', 'F', 'u', 'n', 'c', 't', 'i', 'o', 'n'
622
+ };
623
+ result = dynamic_source_constructor(
624
+ context,
625
+ OSEO_ASYNC_GENERATOR_FUNCTION_CODE_ID,
626
+ constructor_units,
627
+ sizeof(constructor_units) / sizeof(*constructor_units),
628
+ frame.slots[2]
629
+ );
630
+ frame.slots[3] = result.value;
631
+ }
632
+ if (result.status == OSEO_STATUS_NORMAL) {
633
+ result = define_ascii_property(
634
+ context,
635
+ frame.slots[2],
636
+ "prototype",
637
+ frame.slots[1],
638
+ hidden
639
+ );
640
+ }
641
+ if (result.status == OSEO_STATUS_NORMAL) {
642
+ result = define_ascii_property(
643
+ context,
644
+ frame.slots[2],
645
+ "constructor",
646
+ frame.slots[3],
647
+ hidden
648
+ );
649
+ }
650
+ if (result.status == OSEO_STATUS_NORMAL) {
651
+ result = define_to_string_tag(
652
+ context,
653
+ frame.slots[2],
654
+ "AsyncGeneratorFunction",
655
+ hidden
656
+ );
657
+ }
658
+ if (result.status == OSEO_STATUS_NORMAL) {
659
+ result = define_ascii_property(
660
+ context,
661
+ frame.slots[1],
662
+ "constructor",
663
+ frame.slots[2],
664
+ hidden
665
+ );
666
+ }
667
+ if (result.status == OSEO_STATUS_NORMAL) {
668
+ result = define_method(
669
+ context,
670
+ frame.slots[1],
671
+ "next",
672
+ OSEO_ASYNC_GENERATOR_NEXT_CODE_ID
673
+ );
674
+ }
675
+ if (result.status == OSEO_STATUS_NORMAL) {
676
+ result = define_method(
677
+ context,
678
+ frame.slots[1],
679
+ "return",
680
+ OSEO_ASYNC_GENERATOR_RETURN_CODE_ID
681
+ );
682
+ }
683
+ if (result.status == OSEO_STATUS_NORMAL) {
684
+ result = define_method(
685
+ context,
686
+ frame.slots[1],
687
+ "throw",
688
+ OSEO_ASYNC_GENERATOR_THROW_CODE_ID
689
+ );
690
+ }
691
+ if (result.status == OSEO_STATUS_NORMAL) {
692
+ result = define_to_string_tag(
693
+ context,
694
+ frame.slots[1],
695
+ "AsyncGenerator",
696
+ hidden
697
+ );
698
+ }
699
+ if (result.status == OSEO_STATUS_NORMAL) {
700
+ context->realm->intrinsics[OSEO_INTRINSIC_ASYNC_ITERATOR_PROTOTYPE] =
701
+ frame.slots[0];
702
+ context->realm->intrinsics[OSEO_INTRINSIC_ASYNC_GENERATOR_PROTOTYPE] =
703
+ frame.slots[1];
704
+ context->realm->intrinsics[
705
+ OSEO_INTRINSIC_ASYNC_GENERATOR_FUNCTION_PROTOTYPE
706
+ ] = frame.slots[2];
707
+ context->realm->intrinsics[OSEO_INTRINSIC_ASYNC_GENERATOR_FUNCTION] =
708
+ frame.slots[3];
709
+ result.value = frame.slots[1];
710
+ if (context->observe_specialization) {
711
+ context->allocations = entry_allocations;
712
+ }
713
+ }
714
+ oseo_roots_release(context, &frame);
715
+ return result;
716
+ }
717
+
718
+ OseoResult oseo_internal_async_generator_prototype(OseoContext *context) {
719
+ OseoResult result = async_generator_intrinsics(context);
720
+ if (result.status != OSEO_STATUS_NORMAL) return result;
721
+ return normal(context->realm->intrinsics[
722
+ OSEO_INTRINSIC_ASYNC_GENERATOR_PROTOTYPE
723
+ ]);
724
+ }
725
+
726
+ OseoResult oseo_internal_async_generator_intrinsic(OseoContext *context) {
727
+ OseoResult result = async_generator_intrinsics(context);
728
+ if (result.status != OSEO_STATUS_NORMAL) return result;
729
+ return normal(context->realm->intrinsics[
730
+ OSEO_INTRINSIC_ASYNC_GENERATOR_FUNCTION_PROTOTYPE
731
+ ]);
732
+ }
733
+
734
+ OseoResult oseo_internal_generator_created(
735
+ OseoContext *context,
736
+ OseoValue callee,
737
+ OseoValue generator
738
+ ) {
739
+ /* Only the record this call produced is relinked, so a prologue that
740
+ * returned something else leaves the result untouched. */
741
+ if (!is_generator(generator)) return normal(generator);
742
+ OseoGenerator *state = ordinary_object(generator)->generator;
743
+ if (state->callee != callee) return normal(generator);
744
+ OseoValue slots[2] = {callee, generator};
745
+ OseoRootFrame frame = {NULL, slots, 2u};
746
+ oseo_roots_push(context, &frame);
747
+ OseoResult result = normal(function_object(slots[0])->prototype_object);
748
+ /* GetPrototypeFromConstructor falls back to the intrinsic whenever
749
+ * the function's `prototype` is not an object, exactly as the
750
+ * creation path does. */
751
+ if (!is_object(result.value)) {
752
+ result = function_object(slots[0])->function_kind ==
753
+ OSEO_FUNCTION_ASYNC_GENERATOR
754
+ ? oseo_internal_async_generator_prototype(context)
755
+ : oseo_internal_generator_prototype(context);
756
+ }
757
+ if (result.status == OSEO_STATUS_NORMAL) {
758
+ OseoOrdinaryObject *object = ordinary_object(slots[1]);
759
+ if (object->prototype != result.value) {
760
+ /* The object has not reached the program yet, so no shape
761
+ * assumption can depend on the chain being replaced. */
762
+ object->prototype = result.value;
763
+ object->shape_id = context->next_shape_id;
764
+ context->next_shape_id += 1u;
765
+ }
766
+ result = normal(slots[1]);
767
+ }
768
+ oseo_roots_pop(context, &frame);
769
+ return result;
770
+ }
771
+
772
+ OseoResult oseo_generator_create(
773
+ OseoContext *context,
774
+ OseoValue callee,
775
+ OseoValue receiver,
776
+ size_t slot_count,
777
+ size_t completion_count
778
+ ) {
779
+ if (!is_function(callee)) {
780
+ return oseo_internal_throw_error(
781
+ context,
782
+ OSEO_ERROR_TYPE,
783
+ "A suspension frame requires an asynchronous or generator "
784
+ "function."
785
+ );
786
+ }
787
+ OseoFunctionKind function_kind = function_object(callee)->function_kind;
788
+ if (function_kind != OSEO_FUNCTION_GENERATOR &&
789
+ function_kind != OSEO_FUNCTION_ASYNC_GENERATOR &&
790
+ function_kind != OSEO_FUNCTION_ASYNC &&
791
+ function_kind != OSEO_FUNCTION_ASYNC_ARROW) {
792
+ return oseo_internal_throw_error(
793
+ context,
794
+ OSEO_ERROR_TYPE,
795
+ "A suspension frame requires an asynchronous or generator "
796
+ "function."
797
+ );
798
+ }
799
+ bool async_function = function_kind == OSEO_FUNCTION_ASYNC ||
800
+ function_kind == OSEO_FUNCTION_ASYNC_ARROW;
801
+ bool asynchronous =
802
+ function_kind == OSEO_FUNCTION_ASYNC_GENERATOR || async_function;
803
+ if (slot_count > (SIZE_MAX - sizeof(OseoGenerator)) / sizeof(OseoValue)) {
804
+ return failure(context, "OSEO2001", "Generator frame is too large.");
805
+ }
806
+ size_t slot_bytes = slot_count * sizeof(OseoValue);
807
+ size_t remaining = SIZE_MAX - sizeof(OseoGenerator) - slot_bytes;
808
+ if (completion_count > remaining / sizeof(OseoCompletionRecord)) {
809
+ return failure(context, "OSEO2001", "Generator frame is too large.");
810
+ }
811
+ size_t completion_bytes = completion_count * sizeof(OseoCompletionRecord);
812
+ OseoRootFrame frame = {NULL, NULL, 0u};
813
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
814
+ if (result.status != OSEO_STATUS_NORMAL) return result;
815
+ frame.slots[0] = callee;
816
+ frame.slots[1] = receiver;
817
+ frame.slots[2] = async_function
818
+ ? oseo_null()
819
+ : function_object(callee)->prototype_object;
820
+ /* GetPrototypeFromConstructor falls back to the intrinsic whenever
821
+ * the function's `prototype` is not an object. */
822
+ if (!async_function && !is_object(frame.slots[2])) {
823
+ result = asynchronous
824
+ ? oseo_internal_async_generator_prototype(context)
825
+ : oseo_internal_generator_prototype(context);
826
+ frame.slots[2] = result.value;
827
+ }
828
+ if (result.status == OSEO_STATUS_NORMAL) {
829
+ result = oseo_object_create(context, frame.slots[2]);
830
+ }
831
+ if (result.status != OSEO_STATUS_NORMAL) {
832
+ oseo_roots_release(context, &frame);
833
+ return result;
834
+ }
835
+ frame.slots[2] = result.value;
836
+ OseoValue generator = frame.slots[2];
837
+ /* The record is published only after it is fully initialized, so a
838
+ * collection triggered by this allocation never traces half a
839
+ * frame. */
840
+ OseoGenerator *state = malloc(
841
+ sizeof(OseoGenerator) + slot_bytes + completion_bytes
842
+ );
843
+ if (state == NULL) {
844
+ oseo_roots_release(context, &frame);
845
+ return failure(context, "OSEO2001", "Generator allocation failed.");
846
+ }
847
+ state->callee = frame.slots[0];
848
+ state->receiver = frame.slots[1];
849
+ state->sent = oseo_undefined();
850
+ state->async_function_capability = oseo_undefined();
851
+ state->request_head = oseo_undefined();
852
+ state->request_tail = oseo_undefined();
853
+ state->slots = (OseoValue *)(void *)(state + 1);
854
+ state->completions = (OseoCompletionRecord *)(void *)
855
+ ((unsigned char *)state->slots + slot_bytes);
856
+ state->slot_count = slot_count;
857
+ state->completion_count = completion_count;
858
+ state->resume_point = 0u;
859
+ state->resume_kind = OSEO_GENERATOR_RESUME_NEXT;
860
+ state->suspend_reason = OSEO_GENERATOR_SUSPEND_YIELD;
861
+ state->state = OSEO_GENERATOR_SUSPENDED_START;
862
+ state->asynchronous = asynchronous;
863
+ state->async_function = async_function;
864
+ state->awaiting_return = false;
865
+ state->yielded_result_object = false;
866
+ for (size_t index = 0u; index < slot_count; index += 1u) {
867
+ state->slots[index] = oseo_undefined();
868
+ }
869
+ if (completion_bytes > 0u) memset(state->completions, 0, completion_bytes);
870
+ OseoOrdinaryObject *object = ordinary_object(generator);
871
+ object->generator = state;
872
+ oseo_roots_release(context, &frame);
873
+ return normal(generator);
874
+ }
875
+
876
+ OseoValue *oseo_generator_slots(OseoValue generator) {
877
+ return generator_state(generator)->slots;
878
+ }
879
+
880
+ OseoCompletionRecord *oseo_generator_completions(OseoValue generator) {
881
+ return generator_state(generator)->completions;
882
+ }
883
+
884
+ OseoValue oseo_generator_callee(OseoValue generator) {
885
+ return generator_state(generator)->callee;
886
+ }
887
+
888
+ OseoValue oseo_generator_receiver(OseoValue generator) {
889
+ return generator_state(generator)->receiver;
890
+ }
891
+
892
+ OseoValue oseo_generator_sent(OseoValue generator) {
893
+ return generator_state(generator)->sent;
894
+ }
895
+
896
+ size_t oseo_generator_resume_point(OseoValue generator) {
897
+ return generator_state(generator)->resume_point;
898
+ }
899
+
900
+ size_t oseo_generator_resume_kind(OseoValue generator) {
901
+ return generator_state(generator)->resume_kind;
902
+ }
903
+
904
+ void oseo_generator_suspend(
905
+ OseoContext *context,
906
+ OseoValue generator,
907
+ size_t resume_point,
908
+ bool result_object,
909
+ size_t suspend_reason
910
+ ) {
911
+ (void)context;
912
+ /* Both suspension reasons leave the body resumable at `resume_point`;
913
+ * the asynchronous driver is the only caller that distinguishes them,
914
+ * and it parks an awaiting body itself once it has the operand. */
915
+ OseoGenerator *state = generator_state(generator);
916
+ state->resume_point = resume_point;
917
+ state->state = OSEO_GENERATOR_SUSPENDED_YIELD;
918
+ state->suspend_reason = suspend_reason;
919
+ state->yielded_result_object = result_object;
920
+ }
921
+
922
+ /*
923
+ * Discard [[GeneratorContext]] once a generator completes. A completed
924
+ * generator can never be resumed, so retaining its frame would keep the
925
+ * whole suspended object graph reachable for as long as the generator
926
+ * itself is.
927
+ */
928
+ void oseo_internal_generator_complete(OseoValue generator) {
929
+ OseoGenerator *state = generator_state(generator);
930
+ state->state = OSEO_GENERATOR_COMPLETED;
931
+ state->callee = oseo_undefined();
932
+ state->receiver = oseo_undefined();
933
+ state->sent = oseo_undefined();
934
+ /* A completed asynchronous generator still reports the requests it
935
+ * already accepted, so only the frame is discarded here; the driver
936
+ * drains the queue itself. */
937
+ for (size_t index = 0u; index < state->slot_count; index += 1u) {
938
+ state->slots[index] = oseo_undefined();
939
+ }
940
+ if (state->completion_count > 0u) {
941
+ memset(
942
+ state->completions,
943
+ 0,
944
+ state->completion_count * sizeof(OseoCompletionRecord)
945
+ );
946
+ }
947
+ }
948
+
949
+ /*
950
+ * GeneratorResume and GeneratorResumeAbrupt over a return completion.
951
+ * Both deliver `sent` to the pending suspension and differ only in the
952
+ * resume kind the body observes and in what an unstarted or completed
953
+ * generator reports without entering the body.
954
+ */
955
+ static OseoResult generator_resume(
956
+ OseoContext *context,
957
+ OseoValue generator,
958
+ OseoValue sent,
959
+ size_t resume_kind
960
+ ) {
961
+ bool returning = resume_kind == OSEO_GENERATOR_RESUME_RETURN;
962
+ bool throwing = resume_kind == OSEO_GENERATOR_RESUME_THROW;
963
+ /* An asynchronous generator reports its steps through promises, so
964
+ * the synchronous resumption never drives one even when a caller
965
+ * reaches this method with one as the receiver. */
966
+ if (!is_generator(generator) || is_async_generator(generator)) {
967
+ return oseo_internal_throw_error(
968
+ context,
969
+ OSEO_ERROR_TYPE,
970
+ returning
971
+ ? "Generator return requires a generator receiver."
972
+ : throwing
973
+ ? "Generator throw requires a generator receiver."
974
+ : "Generator next requires a generator receiver."
975
+ );
976
+ }
977
+ if (generator_state(generator)->state == OSEO_GENERATOR_EXECUTING) {
978
+ return oseo_internal_throw_error(
979
+ context,
980
+ OSEO_ERROR_TYPE,
981
+ "Cannot resume an already running generator."
982
+ );
983
+ }
984
+ OseoRootFrame frame = {NULL, NULL, 0u};
985
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
986
+ if (result.status != OSEO_STATUS_NORMAL) return result;
987
+ frame.slots[0] = generator;
988
+ frame.slots[1] = sent;
989
+ OseoGeneratorState current = generator_state(frame.slots[0])->state;
990
+ /* A return completion delivered before the body ever ran completes the
991
+ * generator without entering it, so no `finally` in a body that never
992
+ * started runs, and it reports the requested value. A completed
993
+ * generator reports it too, while `next` reports undefined. A throw
994
+ * completion re-throws the value. */
995
+ bool unstarted =
996
+ (returning || throwing) && current == OSEO_GENERATOR_SUSPENDED_START;
997
+ if (current == OSEO_GENERATOR_COMPLETED || unstarted) {
998
+ if (unstarted) oseo_internal_generator_complete(frame.slots[0]);
999
+ if (throwing) {
1000
+ result = (OseoResult){OSEO_STATUS_THROW, frame.slots[1]};
1001
+ oseo_roots_release(context, &frame);
1002
+ return result;
1003
+ }
1004
+ result = oseo_internal_iterator_result(
1005
+ context,
1006
+ returning ? frame.slots[1] : oseo_undefined(),
1007
+ true
1008
+ );
1009
+ oseo_roots_release(context, &frame);
1010
+ return result;
1011
+ }
1012
+ if (context->generator_dispatcher == NULL) {
1013
+ oseo_roots_release(context, &frame);
1014
+ return failure(
1015
+ context,
1016
+ "OSEO2001",
1017
+ "No generated generator dispatcher is installed."
1018
+ );
1019
+ }
1020
+ OseoGenerator *state = generator_state(frame.slots[0]);
1021
+ state->sent = frame.slots[1];
1022
+ state->resume_kind = resume_kind;
1023
+ state->state = OSEO_GENERATOR_EXECUTING;
1024
+ result = oseo_call_enter(context);
1025
+ if (result.status != OSEO_STATUS_NORMAL) {
1026
+ oseo_internal_generator_complete(frame.slots[0]);
1027
+ oseo_roots_release(context, &frame);
1028
+ return result;
1029
+ }
1030
+ OseoRealmScope realm_scope;
1031
+ oseo_internal_realm_enter(
1032
+ context,
1033
+ &realm_scope,
1034
+ oseo_internal_callee_realm(
1035
+ context,
1036
+ generator_state(frame.slots[0])->callee
1037
+ )
1038
+ );
1039
+ /* A resumed body is the running function of the calls it makes
1040
+ * (ADR 0034); its generator record roots the callee. */
1041
+ OseoActivation activation;
1042
+ oseo_internal_activation_push(
1043
+ context,
1044
+ &activation,
1045
+ generator_state(frame.slots[0])->callee,
1046
+ 0u,
1047
+ NULL
1048
+ );
1049
+ result = context->generator_dispatcher(context, frame.slots[0]);
1050
+ oseo_internal_activation_pop(context, &activation);
1051
+ oseo_internal_realm_leave(context, &realm_scope);
1052
+ oseo_call_leave(context);
1053
+ state = generator_state(frame.slots[0]);
1054
+ state->sent = oseo_undefined();
1055
+ /* The kind describes one resumption only; the next suspension is
1056
+ * resumed normally unless that resumption says otherwise. */
1057
+ state->resume_kind = OSEO_GENERATOR_RESUME_NEXT;
1058
+ /* The completion value only lives in `result` until it is rooted,
1059
+ * and discarding the frame drops the slot it came from. */
1060
+ frame.slots[1] = result.value;
1061
+ if (result.status != OSEO_STATUS_NORMAL) {
1062
+ oseo_internal_generator_complete(frame.slots[0]);
1063
+ result.value = frame.slots[1];
1064
+ oseo_roots_release(context, &frame);
1065
+ return result;
1066
+ }
1067
+ bool done = state->state != OSEO_GENERATOR_SUSPENDED_YIELD;
1068
+ if (done) {
1069
+ oseo_internal_generator_complete(frame.slots[0]);
1070
+ } else if (state->yielded_result_object) {
1071
+ /* GeneratorYield receives an already-built iterator result
1072
+ * object from `yield*`, so it reaches the resuming consumer
1073
+ * unchanged, including a result that omits `done`. */
1074
+ OseoValue yielded = frame.slots[1];
1075
+ oseo_roots_release(context, &frame);
1076
+ return normal(yielded);
1077
+ }
1078
+ result = oseo_internal_iterator_result(context, frame.slots[1], done);
1079
+ oseo_roots_release(context, &frame);
1080
+ return result;
1081
+ }
1082
+
1083
+ OseoResult oseo_generator_next(
1084
+ OseoContext *context,
1085
+ OseoValue generator,
1086
+ OseoValue sent
1087
+ ) {
1088
+ return generator_resume(
1089
+ context,
1090
+ generator,
1091
+ sent,
1092
+ OSEO_GENERATOR_RESUME_NEXT
1093
+ );
1094
+ }
1095
+
1096
+ OseoResult oseo_generator_return(
1097
+ OseoContext *context,
1098
+ OseoValue generator,
1099
+ OseoValue value
1100
+ ) {
1101
+ return generator_resume(
1102
+ context,
1103
+ generator,
1104
+ value,
1105
+ OSEO_GENERATOR_RESUME_RETURN
1106
+ );
1107
+ }
1108
+
1109
+ OseoResult oseo_generator_throw(
1110
+ OseoContext *context,
1111
+ OseoValue generator,
1112
+ OseoValue value
1113
+ ) {
1114
+ return generator_resume(
1115
+ context,
1116
+ generator,
1117
+ value,
1118
+ OSEO_GENERATOR_RESUME_THROW
1119
+ );
1120
+ }