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