@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,612 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /* Copyright (c) 2026 Hong Minhee; see ../MIT-LICENSE.txt. */
3
+
4
+ #include "runtime_internal.h"
5
+
6
+ #include <stdio.h>
7
+
8
+ /*
9
+ * EnumerateObjectProperties (14.7.5.9).
10
+ *
11
+ * The specification leaves the mechanics and order unspecified and
12
+ * states rules the iterator must obey instead, so a conforming
13
+ * implementation must choose when it obtains each level's own keys. This
14
+ * implementation makes the choice both reference hosts make, because the
15
+ * choice is observable and no rule prefers the other one: the whole
16
+ * prototype chain is collected once, when the enumeration is acquired.
17
+ *
18
+ * Collection walks the chain outward. Each level's own string keys are
19
+ * obtained in OrdinaryOwnPropertyKeys order, symbol keys are dropped,
20
+ * and the chain and keys are snapshotted before descriptors are read,
21
+ * matching both reference hosts' observable Proxy operation order.
22
+ *
23
+ * Each step reads the next candidate's descriptor at the time the
24
+ * candidate is processed. A key deleted before that point is ignored, as
25
+ * the rules require, while a property added during the enumeration stays
26
+ * invisible to it. A descriptor found at a nearer level records the key,
27
+ * whether or not the property was enumerable, which applies the specified
28
+ * shadow rule without reporting any name twice.
29
+ *
30
+ * Proxy own-key, descriptor, and prototype operations can run user code,
31
+ * reenter enumeration, and complete abruptly. Each observable operation
32
+ * therefore returns an OseoResult and the caller propagates its status.
33
+ * A candidate invokes [[GetOwnProperty]] only once in its processing step.
34
+ */
35
+
36
+ /*
37
+ * One level's own string keys in [[OwnPropertyKeys]] order: the
38
+ * OrdinaryOwnPropertyKeys order, or a module namespace's [[Exports]]
39
+ * order with integer-like names in place.
40
+ */
41
+ static OseoResult enumeration_keys(OseoContext *context, OseoValue level) {
42
+ OseoRootFrame frame = {NULL, NULL, 0u};
43
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
44
+ if (result.status != OSEO_STATUS_NORMAL) return result;
45
+ frame.slots[0] = level;
46
+ result = oseo_argument_list_create(context);
47
+ frame.slots[1] = result.value;
48
+ if (result.status == OSEO_STATUS_NORMAL && is_proxy(frame.slots[0])) {
49
+ result = oseo_internal_proxy_own_keys(
50
+ context, frame.slots[0], OSEO_OWN_KEY_STRINGS);
51
+ frame.slots[2] = result.value;
52
+ if (result.status == OSEO_STATUS_NORMAL) {
53
+ result = oseo_internal_array_like_list(
54
+ context, frame.slots[2], &frame.slots[1]);
55
+ }
56
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[1];
57
+ oseo_roots_release(context, &frame);
58
+ return result;
59
+ }
60
+ if (result.status == OSEO_STATUS_NORMAL && is_string(frame.slots[0])) {
61
+ /* A String exotic object owns one enumerable property per code
62
+ * unit index, then a non-enumerable `length`. */
63
+ size_t length = string_object(frame.slots[0])->length;
64
+ for (size_t index = 0u;
65
+ result.status == OSEO_STATUS_NORMAL && index < length;
66
+ index += 1u) {
67
+ char key_text[24];
68
+ (void)snprintf(key_text, sizeof(key_text), "%zu", index);
69
+ result = oseo_internal_ascii_string(context, key_text);
70
+ frame.slots[2] = result.value;
71
+ if (result.status != OSEO_STATUS_NORMAL) break;
72
+ result = oseo_argument_list_append(
73
+ context,
74
+ frame.slots[1],
75
+ frame.slots[2]
76
+ );
77
+ }
78
+ if (result.status == OSEO_STATUS_NORMAL) {
79
+ result = oseo_internal_ascii_string(context, "length");
80
+ frame.slots[2] = result.value;
81
+ }
82
+ if (result.status == OSEO_STATUS_NORMAL) {
83
+ result = oseo_argument_list_append(
84
+ context,
85
+ frame.slots[1],
86
+ frame.slots[2]
87
+ );
88
+ }
89
+ } else if (result.status == OSEO_STATUS_NORMAL &&
90
+ is_object(frame.slots[0])) {
91
+ /* A TypedArray's valid integer indices lead its own keys, and its
92
+ * property vector never stores a canonical numeric key. */
93
+ size_t index_count =
94
+ oseo_internal_typed_array_index_key_count(frame.slots[0]);
95
+ for (size_t index = 0u;
96
+ result.status == OSEO_STATUS_NORMAL && index < index_count;
97
+ index += 1u) {
98
+ result = oseo_internal_typed_array_index_key(context, index);
99
+ frame.slots[2] = result.value;
100
+ if (result.status == OSEO_STATUS_NORMAL) {
101
+ result = oseo_argument_list_append(
102
+ context,
103
+ frame.slots[1],
104
+ frame.slots[2]
105
+ );
106
+ }
107
+ }
108
+ bool index_keys_lead =
109
+ !ordinary_object(frame.slots[0])->module_namespace;
110
+ uint64_t previous = UINT64_MAX;
111
+ while (index_keys_lead && result.status == OSEO_STATUS_NORMAL) {
112
+ OseoOrdinaryObject *object = ordinary_object(frame.slots[0]);
113
+ size_t selected = SIZE_MAX;
114
+ uint32_t selected_number = 0u;
115
+ for (size_t index = 0u;
116
+ index < object->property_count;
117
+ index += 1u) {
118
+ uint32_t number = 0u;
119
+ if (!oseo_internal_array_index(
120
+ object->properties[index].key, &number) ||
121
+ (previous != UINT64_MAX && number <= previous)) continue;
122
+ if (selected == SIZE_MAX || number < selected_number) {
123
+ selected = index;
124
+ selected_number = number;
125
+ }
126
+ }
127
+ if (selected == SIZE_MAX) break;
128
+ frame.slots[2] = object->properties[selected].key;
129
+ result = oseo_argument_list_append(
130
+ context,
131
+ frame.slots[1],
132
+ frame.slots[2]
133
+ );
134
+ previous = selected_number;
135
+ }
136
+ /* An Array's `length` and a function's `prototype` are own
137
+ * non-enumerable properties this runtime keeps outside the
138
+ * property vector, and both are created before any string key
139
+ * source code can add. */
140
+ const char *reserved = is_array(frame.slots[0])
141
+ ? "length"
142
+ : (function_has_prototype_property(frame.slots[0])
143
+ ? "prototype"
144
+ : NULL);
145
+ if (result.status == OSEO_STATUS_NORMAL && reserved != NULL) {
146
+ result = oseo_internal_ascii_string(context, reserved);
147
+ frame.slots[2] = result.value;
148
+ if (result.status == OSEO_STATUS_NORMAL) {
149
+ result = oseo_argument_list_append(
150
+ context,
151
+ frame.slots[1],
152
+ frame.slots[2]
153
+ );
154
+ }
155
+ }
156
+ /* The legacy `caller` and `arguments` of a non-strict function
157
+ * stay outside the property vector until a definition moves
158
+ * them into it (ADR 0034). */
159
+ uint8_t legacy_live = is_function(frame.slots[0])
160
+ ? function_object(frame.slots[0])->legacy_live
161
+ : 0u;
162
+ if (result.status == OSEO_STATUS_NORMAL && legacy_live != 0u) {
163
+ static const uint8_t bits[2] = {
164
+ OSEO_LEGACY_ARGUMENTS_LIVE,
165
+ OSEO_LEGACY_CALLER_LIVE,
166
+ };
167
+ result = oseo_internal_legacy_function_keys(context);
168
+ for (size_t index = 0u;
169
+ result.status == OSEO_STATUS_NORMAL && index < 2u;
170
+ index += 1u) {
171
+ if ((legacy_live & bits[index]) == 0u) continue;
172
+ result = oseo_argument_list_append(
173
+ context,
174
+ frame.slots[1],
175
+ context->legacy_function_keys[index]
176
+ );
177
+ }
178
+ }
179
+ for (size_t index = 0u; result.status == OSEO_STATUS_NORMAL; ) {
180
+ OseoOrdinaryObject *object = ordinary_object(frame.slots[0]);
181
+ if (index >= object->property_count) break;
182
+ OseoValue key = object->properties[index].key;
183
+ index += 1u;
184
+ uint32_t ignored = 0u;
185
+ if (is_symbol(key) ||
186
+ (index_keys_lead &&
187
+ oseo_internal_array_index(key, &ignored))) continue;
188
+ if (reserved != NULL &&
189
+ oseo_internal_string_is_ascii(key, reserved)) continue;
190
+ frame.slots[2] = key;
191
+ result = oseo_argument_list_append(
192
+ context,
193
+ frame.slots[1],
194
+ frame.slots[2]
195
+ );
196
+ }
197
+ }
198
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[1];
199
+ oseo_roots_release(context, &frame);
200
+ return result;
201
+ }
202
+
203
+ static bool enumeration_recorded(
204
+ OseoContext *context,
205
+ OseoValue list,
206
+ OseoValue key
207
+ ) {
208
+ size_t count = 0u;
209
+ const OseoValue *values = NULL;
210
+ OseoResult viewed = oseo_argument_list_view(
211
+ context,
212
+ list,
213
+ &count,
214
+ &values
215
+ );
216
+ if (viewed.status != OSEO_STATUS_NORMAL) return false;
217
+ for (size_t index = 0u; index < count; index += 1u) {
218
+ if (oseo_internal_property_key_equal(values[index], key)) return true;
219
+ }
220
+ return false;
221
+ }
222
+
223
+ static OseoResult enumeration_cache_proxy_descriptor(
224
+ OseoContext *context, OseoValue cache, OseoValue proxy,
225
+ OseoValue key, bool found
226
+ ) {
227
+ OseoResult result = oseo_argument_list_append(context, cache, proxy);
228
+ if (result.status == OSEO_STATUS_NORMAL) {
229
+ result = oseo_argument_list_append(context, cache, key);
230
+ }
231
+ if (result.status == OSEO_STATUS_NORMAL) {
232
+ result = oseo_argument_list_append(context, cache, oseo_boolean(found));
233
+ }
234
+ return result;
235
+ }
236
+
237
+ static OseoResult enumeration_cached_proxy_descriptor(
238
+ OseoContext *context, OseoValue cache, OseoValue proxy,
239
+ OseoValue key, bool *cached, bool *found
240
+ ) {
241
+ *cached = false;
242
+ *found = false;
243
+ size_t count = 0u;
244
+ const OseoValue *values = NULL;
245
+ OseoResult result = oseo_argument_list_view(
246
+ context, cache, &count, &values);
247
+ if (result.status != OSEO_STATUS_NORMAL) return result;
248
+ for (size_t index = 0u; index + 2u < count; index += 3u) {
249
+ if (values[index] == proxy &&
250
+ oseo_internal_property_key_equal(values[index + 1u], key)) {
251
+ *cached = true;
252
+ *found = oseo_to_boolean(values[index + 2u]);
253
+ break;
254
+ }
255
+ }
256
+ return normal(oseo_undefined());
257
+ }
258
+
259
+ /*
260
+ * [[GetOwnProperty]] of one collected level, reduced to what collection
261
+ * observes: whether the key is an own property, and whether it is
262
+ * enumerable.
263
+ */
264
+ static OseoResult enumeration_own_key(
265
+ OseoContext *context,
266
+ OseoValue level,
267
+ OseoValue key,
268
+ bool *found,
269
+ bool *enumerable
270
+ ) {
271
+ *found = false;
272
+ if (is_string(level)) {
273
+ uint32_t index = 0u;
274
+ if (oseo_internal_string_is_ascii(key, "length")) {
275
+ *enumerable = false;
276
+ *found = true;
277
+ return normal(oseo_undefined());
278
+ }
279
+ if (oseo_internal_array_index(key, &index) &&
280
+ index < string_object(level)->length) {
281
+ *enumerable = true;
282
+ *found = true;
283
+ return normal(oseo_undefined());
284
+ }
285
+ return normal(oseo_undefined());
286
+ }
287
+ if (!is_object(level)) return normal(oseo_undefined());
288
+ OseoValue value = oseo_undefined();
289
+ OseoValue getter = oseo_undefined();
290
+ OseoValue setter = oseo_undefined();
291
+ OseoPropertyAttributes attributes = {false, false, false, false};
292
+ if (is_typed_array(level)) {
293
+ bool numeric = false;
294
+ size_t index = SIZE_MAX;
295
+ OseoResult result = oseo_internal_typed_array_numeric_key(
296
+ context, key, &numeric, &index);
297
+ if (result.status != OSEO_STATUS_NORMAL) return result;
298
+ if (numeric) {
299
+ *found = oseo_internal_typed_array_has_index(level, index);
300
+ *enumerable = true;
301
+ return normal(oseo_undefined());
302
+ }
303
+ }
304
+ if (is_proxy(level)) {
305
+ OseoResult result = oseo_internal_proxy_get_own_property(
306
+ context, level, key, found, &value, &attributes,
307
+ &getter, &setter);
308
+ if (result.status != OSEO_STATUS_NORMAL || !*found) return result;
309
+ } else if (!oseo_internal_own_descriptor(
310
+ level,
311
+ key,
312
+ &value,
313
+ &attributes,
314
+ &getter,
315
+ &setter
316
+ )) return normal(oseo_undefined());
317
+ else {
318
+ OseoResult ready = oseo_internal_namespace_export_ready(
319
+ context, level, value);
320
+ if (ready.status != OSEO_STATUS_NORMAL) return ready;
321
+ *found = true;
322
+ }
323
+ *enumerable = attributes.enumerable;
324
+ return normal(oseo_undefined());
325
+ }
326
+
327
+ static OseoResult enumeration_reachable(
328
+ OseoContext *context, OseoValue receiver, OseoValue key,
329
+ OseoValue proxy_descriptors
330
+ ) {
331
+ bool found = false;
332
+ bool enumerable = false;
333
+ if (is_string(receiver)) {
334
+ OseoResult result = enumeration_own_key(
335
+ context, receiver, key, &found, &enumerable);
336
+ if (result.status == OSEO_STATUS_NORMAL) {
337
+ result.value = oseo_boolean(found);
338
+ }
339
+ return result;
340
+ }
341
+ OseoValue current = receiver;
342
+ while (is_object(current)) {
343
+ OseoResult result = normal(oseo_undefined());
344
+ bool cached = false;
345
+ if (is_proxy(current)) {
346
+ result = enumeration_cached_proxy_descriptor(
347
+ context, proxy_descriptors, current, key, &cached, &found);
348
+ }
349
+ if (result.status == OSEO_STATUS_NORMAL && !cached) {
350
+ result = enumeration_own_key(
351
+ context, current, key, &found, &enumerable);
352
+ }
353
+ if (result.status != OSEO_STATUS_NORMAL || found) {
354
+ if (result.status == OSEO_STATUS_NORMAL) {
355
+ result.value = oseo_boolean(true);
356
+ }
357
+ return result;
358
+ }
359
+ result = oseo_internal_get_prototype(context, current);
360
+ if (result.status != OSEO_STATUS_NORMAL) return result;
361
+ current = result.value;
362
+ }
363
+ return normal(oseo_boolean(false));
364
+ }
365
+
366
+ /* The next level of the chain, or a non-object when the walk ends. */
367
+ static OseoResult enumeration_parent(
368
+ OseoContext *context,
369
+ OseoValue level
370
+ ) {
371
+ /* Acquisition materializes primitives before this outward walk, so
372
+ * ordinary inputs reach their actual intrinsic prototype chains. */
373
+ return is_object(level)
374
+ ? oseo_internal_get_prototype(context, level)
375
+ : normal(oseo_null());
376
+ }
377
+
378
+ /*
379
+ * Collect the receiver's own and inherited enumerable string keys, with
380
+ * every nearer own key suppressing the same name behind it.
381
+ */
382
+ static OseoResult enumeration_collect(
383
+ OseoContext *context,
384
+ OseoValue receiver
385
+ ) {
386
+ OseoRootFrame frame = {NULL, NULL, 0u};
387
+ OseoResult result = oseo_roots_allocate(context, &frame, 5u);
388
+ if (result.status != OSEO_STATUS_NORMAL) return result;
389
+ frame.slots[0] = receiver;
390
+ result = oseo_argument_list_create(context);
391
+ frame.slots[1] = result.value;
392
+ if (result.status == OSEO_STATUS_NORMAL) {
393
+ result = oseo_argument_list_create(context);
394
+ frame.slots[4] = result.value;
395
+ }
396
+ while (result.status == OSEO_STATUS_NORMAL &&
397
+ (is_object(frame.slots[0]) || is_string(frame.slots[0]))) {
398
+ result = enumeration_keys(context, frame.slots[0]);
399
+ frame.slots[2] = result.value;
400
+ for (size_t index = 0u; result.status == OSEO_STATUS_NORMAL; ) {
401
+ size_t count = 0u;
402
+ const OseoValue *values = NULL;
403
+ result = oseo_argument_list_view(
404
+ context,
405
+ frame.slots[2],
406
+ &count,
407
+ &values
408
+ );
409
+ if (result.status != OSEO_STATUS_NORMAL || index >= count) break;
410
+ frame.slots[3] = values[index];
411
+ index += 1u;
412
+ result = oseo_argument_list_append(
413
+ context,
414
+ frame.slots[4],
415
+ frame.slots[0]
416
+ );
417
+ if (result.status == OSEO_STATUS_NORMAL) {
418
+ result = oseo_argument_list_append(
419
+ context,
420
+ frame.slots[4],
421
+ frame.slots[3]
422
+ );
423
+ }
424
+ }
425
+ if (result.status != OSEO_STATUS_NORMAL) break;
426
+ result = enumeration_parent(context, frame.slots[0]);
427
+ frame.slots[0] = result.value;
428
+ }
429
+ for (size_t index = 0u; result.status == OSEO_STATUS_NORMAL; ) {
430
+ size_t count = 0u;
431
+ const OseoValue *values = NULL;
432
+ result = oseo_argument_list_view(
433
+ context, frame.slots[4], &count, &values);
434
+ if (result.status != OSEO_STATUS_NORMAL || index >= count) break;
435
+ frame.slots[0] = values[index];
436
+ frame.slots[3] = values[index + 1u];
437
+ index += 2u;
438
+ bool found = false;
439
+ bool enumerable = false;
440
+ if (!is_proxy(frame.slots[0])) {
441
+ result = enumeration_own_key(
442
+ context, frame.slots[0], frame.slots[3],
443
+ &found, &enumerable);
444
+ }
445
+ if (result.status == OSEO_STATUS_NORMAL) {
446
+ result = oseo_argument_list_append(
447
+ context, frame.slots[1], frame.slots[0]);
448
+ }
449
+ if (result.status == OSEO_STATUS_NORMAL) {
450
+ result = oseo_argument_list_append(
451
+ context, frame.slots[1], frame.slots[3]);
452
+ }
453
+ if (result.status == OSEO_STATUS_NORMAL) {
454
+ result = oseo_argument_list_append(
455
+ context, frame.slots[1], is_proxy(frame.slots[0])
456
+ ? oseo_undefined() : oseo_boolean(found));
457
+ }
458
+ if (result.status == OSEO_STATUS_NORMAL) {
459
+ result = oseo_argument_list_append(
460
+ context, frame.slots[1], oseo_boolean(enumerable));
461
+ }
462
+ }
463
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[1];
464
+ oseo_roots_release(context, &frame);
465
+ return result;
466
+ }
467
+
468
+ OseoResult oseo_enumerate_get(
469
+ OseoContext *context,
470
+ OseoValue subject,
471
+ OseoValue *record,
472
+ bool *done
473
+ ) {
474
+ *record = oseo_undefined();
475
+ *done = true;
476
+ /* ForIn/OfHeadEvaluation returns a break completion for a nullish
477
+ * subject, so the whole statement is skipped without an error and
478
+ * without a ToObject conversion. */
479
+ if (is_nullish(subject)) return normal(oseo_undefined());
480
+ OseoRootFrame frame = {NULL, NULL, 0u};
481
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
482
+ if (result.status != OSEO_STATUS_NORMAL) return result;
483
+ /* Root the original primitive while ToObject materializes its full
484
+ * wrapper and prototype chain. String indices remain own keys, and
485
+ * every primitive kind can inherit enumerable metadata or user keys. */
486
+ frame.slots[0] = subject;
487
+ result = oseo_internal_to_object(context, frame.slots[0]);
488
+ frame.slots[0] = result.value;
489
+ if (result.status == OSEO_STATUS_NORMAL) {
490
+ result = enumeration_collect(context, frame.slots[0]);
491
+ }
492
+ frame.slots[1] = result.value;
493
+ if (result.status == OSEO_STATUS_NORMAL) {
494
+ result = oseo_argument_list_create(context);
495
+ frame.slots[2] = result.value;
496
+ }
497
+ if (result.status == OSEO_STATUS_NORMAL) {
498
+ OseoEnumeration *enumeration = oseo_internal_allocate_heap_bytes(
499
+ context,
500
+ sizeof(*enumeration)
501
+ );
502
+ if (enumeration == NULL) {
503
+ result = failure(
504
+ context,
505
+ "OSEO2001",
506
+ "Enumeration allocation failed."
507
+ );
508
+ } else {
509
+ enumeration->receiver = frame.slots[0];
510
+ enumeration->candidates = frame.slots[1];
511
+ enumeration->visited = frame.slots[2];
512
+ enumeration->index = 0u;
513
+ result = oseo_internal_publish_heap(
514
+ context,
515
+ &enumeration->header,
516
+ OSEO_HEAP_ENUMERATION
517
+ );
518
+ }
519
+ }
520
+ if (result.status == OSEO_STATUS_NORMAL) {
521
+ *record = result.value;
522
+ *done = false;
523
+ }
524
+ oseo_roots_release(context, &frame);
525
+ return result;
526
+ }
527
+
528
+ OseoResult oseo_enumerate_next(
529
+ OseoContext *context,
530
+ OseoValue record,
531
+ OseoValue *key,
532
+ bool *done
533
+ ) {
534
+ *key = oseo_undefined();
535
+ *done = true;
536
+ if (!is_enumeration(record)) {
537
+ return failure(
538
+ context,
539
+ "OSEO2001",
540
+ "Enumeration step requires an enumeration record."
541
+ );
542
+ }
543
+ OseoRootFrame frame = {NULL, NULL, 0u};
544
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
545
+ if (result.status != OSEO_STATUS_NORMAL) return result;
546
+ frame.slots[0] = record;
547
+ result = oseo_argument_list_create(context);
548
+ frame.slots[3] = result.value;
549
+ OseoEnumeration *enumeration = enumeration_object(frame.slots[0]);
550
+ size_t count = 0u;
551
+ const OseoValue *values = NULL;
552
+ if (result.status == OSEO_STATUS_NORMAL) result = oseo_argument_list_view(
553
+ context,
554
+ enumeration->candidates,
555
+ &count,
556
+ &values
557
+ );
558
+ if (result.status != OSEO_STATUS_NORMAL) {
559
+ oseo_roots_release(context, &frame);
560
+ return result;
561
+ }
562
+ while (enumeration->index < count) {
563
+ frame.slots[1] = values[enumeration->index];
564
+ frame.slots[2] = values[enumeration->index + 1u];
565
+ OseoValue found_state = values[enumeration->index + 2u];
566
+ bool enumerable = oseo_to_boolean(
567
+ values[enumeration->index + 3u]);
568
+ enumeration->index += 4u;
569
+ if (enumeration_recorded(
570
+ context, enumeration->visited, frame.slots[2])) continue;
571
+ bool pending = tag_of(found_state) == OSEO_TAG_UNDEFINED;
572
+ bool found = pending ? false : oseo_to_boolean(found_state);
573
+ if (pending) {
574
+ result = enumeration_own_key(
575
+ context, frame.slots[1], frame.slots[2],
576
+ &found, &enumerable);
577
+ if (result.status == OSEO_STATUS_NORMAL) {
578
+ result = enumeration_cache_proxy_descriptor(
579
+ context, frame.slots[3], frame.slots[1],
580
+ frame.slots[2], found);
581
+ }
582
+ }
583
+ if (result.status != OSEO_STATUS_NORMAL) break;
584
+ if (!found) {
585
+ /* The reference hosts advance a missing Proxy candidate with a
586
+ * live [[GetPrototypeOf]] observation. The snapshotted candidate
587
+ * list still decides which previously collected keys can run. */
588
+ if (pending) {
589
+ result = enumeration_parent(context, frame.slots[1]);
590
+ if (result.status != OSEO_STATUS_NORMAL) break;
591
+ }
592
+ continue;
593
+ }
594
+ result = oseo_argument_list_append(
595
+ context,
596
+ enumeration->visited,
597
+ frame.slots[2]
598
+ );
599
+ if (result.status != OSEO_STATUS_NORMAL) break;
600
+ if (!enumerable) continue;
601
+ result = enumeration_reachable(
602
+ context, enumeration->receiver, frame.slots[2], frame.slots[3]);
603
+ if (result.status != OSEO_STATUS_NORMAL) break;
604
+ if (!oseo_to_boolean(result.value)) continue;
605
+ *key = frame.slots[2];
606
+ *done = false;
607
+ break;
608
+ }
609
+ if (result.status == OSEO_STATUS_NORMAL) result = normal(oseo_undefined());
610
+ oseo_roots_release(context, &frame);
611
+ return result;
612
+ }