@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,1539 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /* Copyright (c) 2026 Hong Minhee; see ../MIT-LICENSE.txt. */
3
+
4
+ #include "runtime_internal.h"
5
+
6
+ #include <stdlib.h>
7
+ #include <string.h>
8
+
9
+ /*
10
+ * The %Map% intrinsic: keyed collection storage with SameValueZero
11
+ * identity and insertion order, its prototype methods, and
12
+ * %MapIteratorPrototype%.
13
+ */
14
+
15
+ static OseoResult map_intrinsic_build(OseoContext *context);
16
+
17
+ /*
18
+ * [[MapData]] storage, growth, and SameValueZero lookup.
19
+ */
20
+
21
+ /* Searches the live records by SameValueZero, skipping tombstones left
22
+ * by a prior delete or clear. */
23
+ static size_t map_entry_find(const OseoMap *map, OseoValue key) {
24
+ for (size_t index = 0u; index < map->entry_count; index += 1u) {
25
+ if (map->entries[index].live &&
26
+ oseo_internal_same_value_zero(map->entries[index].key, key)) {
27
+ return index;
28
+ }
29
+ }
30
+ return SIZE_MAX;
31
+ }
32
+
33
+ /* If SameValueZero(key, -0) is true, set key to +0, so a newly created
34
+ * record always stores the canonical positive zero. */
35
+ static OseoValue map_normalize_key(OseoValue key) {
36
+ if (is_number(key) && number_value(key) == 0.0) return oseo_number(0.0);
37
+ return key;
38
+ }
39
+
40
+ static OseoResult map_grow_entries(OseoContext *context, OseoValue map_value) {
41
+ OseoMap *map = map_object(map_value);
42
+ if (map->entry_count < map->entry_capacity) return normal(map_value);
43
+ size_t capacity = map->entry_capacity == 0u
44
+ ? 4u
45
+ : map->entry_capacity * 2u;
46
+ if (capacity < map->entry_capacity ||
47
+ capacity > SIZE_MAX / sizeof(OseoMapEntry)) {
48
+ return failure(context, "OSEO2001", "Map storage is too large.");
49
+ }
50
+ if (context->collect_every_safepoint) oseo_collect(context);
51
+ map = map_object(map_value);
52
+ context->allocation_attempts += 1u;
53
+ if (context->fail_allocation_at != 0u &&
54
+ context->allocation_attempts == context->fail_allocation_at) {
55
+ return failure(context, "OSEO2001", "Map storage allocation failed.");
56
+ }
57
+ OseoMapEntry *entries = malloc(capacity * sizeof(*entries));
58
+ if (entries == NULL) {
59
+ return failure(context, "OSEO2001", "Map storage allocation failed.");
60
+ }
61
+ if (map->entry_count > 0u) {
62
+ memcpy(entries, map->entries, map->entry_count * sizeof(*entries));
63
+ }
64
+ free(map->entries);
65
+ map->entries = entries;
66
+ map->entry_capacity = capacity;
67
+ return normal(map_value);
68
+ }
69
+
70
+ /* Appends {key, value} as a fresh live [[MapData]] record. `key` must
71
+ * already be normalized. A record is always appended rather than
72
+ * reusing a tombstone slot, which is what lets a key deleted and later
73
+ * re-added reappear at the end of insertion order instead of its
74
+ * original position. */
75
+ static OseoResult map_append_entry(
76
+ OseoContext *context,
77
+ OseoValue map_value,
78
+ OseoValue key,
79
+ OseoValue value
80
+ ) {
81
+ OseoValue slots[3] = {map_value, key, value};
82
+ OseoRootFrame frame = {NULL, slots, 3u};
83
+ oseo_roots_push(context, &frame);
84
+ /* A record stores this realm's own representative of a registered
85
+ * symbol, so [[MapData]] never holds a value another realm owns. */
86
+ OseoResult result = oseo_internal_local_symbol(context, slots[1]);
87
+ if (result.status == OSEO_STATUS_NORMAL) {
88
+ slots[1] = result.value;
89
+ result = map_grow_entries(context, slots[0]);
90
+ }
91
+ if (result.status == OSEO_STATUS_NORMAL) {
92
+ OseoMap *map = map_object(slots[0]);
93
+ map->entries[map->entry_count].key = slots[1];
94
+ map->entries[map->entry_count].value = slots[2];
95
+ map->entries[map->entry_count].live = true;
96
+ map->entry_count += 1u;
97
+ map->live_count += 1u;
98
+ result = normal(slots[0]);
99
+ }
100
+ oseo_roots_pop(context, &frame);
101
+ return result;
102
+ }
103
+
104
+ /*
105
+ * Allocation.
106
+ */
107
+
108
+ static OseoResult map_allocate(
109
+ OseoContext *context,
110
+ OseoValue prototype_value
111
+ ) {
112
+ OseoValue rooted = prototype_value;
113
+ OseoRootFrame frame = {NULL, &rooted, 1u};
114
+ oseo_roots_push(context, &frame);
115
+ OseoMap *map = oseo_internal_allocate_heap_bytes(context, sizeof(*map));
116
+ if (map == NULL) {
117
+ oseo_roots_pop(context, &frame);
118
+ return failure(context, "OSEO2001", "Map allocation failed.");
119
+ }
120
+ map->ordinary.prototype = rooted;
121
+ map->ordinary.properties = NULL;
122
+ map->ordinary.property_capacity = 0u;
123
+ map->ordinary.property_count = 0u;
124
+ oseo_internal_init_dense_index(&map->ordinary);
125
+ map->ordinary.private_elements = NULL;
126
+ map->ordinary.private_element_capacity = 0u;
127
+ map->ordinary.private_element_count = 0u;
128
+ map->ordinary.shape_id = context->next_shape_id;
129
+ context->next_shape_id += 1u;
130
+ map->ordinary.array_length = 0u;
131
+ map->ordinary.dictionary = false;
132
+ map->ordinary.length_writable = false;
133
+ map->ordinary.extensible = true;
134
+ map->ordinary.module_namespace = false;
135
+ map->ordinary.immutable_prototype = false;
136
+ map->ordinary.global_object = false;
137
+ map->ordinary.error_data = false;
138
+ map->ordinary.number_data = false;
139
+ map->ordinary.number_value = oseo_undefined();
140
+ map->ordinary.primitive_data = false;
141
+ map->ordinary.primitive_value = oseo_undefined();
142
+ map->ordinary.primitive_wrapper_methods_initialized = false;
143
+ map->ordinary.virtual_string_iterator = false;
144
+ map->ordinary.virtual_string_iterator_configurable = false;
145
+ map->ordinary.virtual_string_iterator_enumerable = false;
146
+ map->ordinary.virtual_string_iterator_writable = false;
147
+ map->ordinary.iterator_kind = OSEO_ITERATOR_NONE;
148
+ map->ordinary.iterator_target = oseo_undefined();
149
+ map->ordinary.iterator_index = 0u;
150
+ map->ordinary.regexp_string_iterator = false;
151
+ map->ordinary.regexp_iterator_regexp = oseo_undefined();
152
+ map->ordinary.regexp_iterator_subject = oseo_undefined();
153
+ map->ordinary.regexp_iterator_global = false;
154
+ map->ordinary.regexp_iterator_unicode = false;
155
+ map->ordinary.regexp_iterator_complete = false;
156
+ map->ordinary.async_from_sync = false;
157
+ map->ordinary.async_sync_iterator = oseo_undefined();
158
+ map->ordinary.wrap_for_valid_iterator = false;
159
+ map->ordinary.wrapped_iterator = oseo_undefined();
160
+ map->ordinary.wrapped_next = oseo_undefined();
161
+ map->ordinary.generator = NULL;
162
+ map->ordinary.arguments_object = false;
163
+ map->ordinary.mapped_arguments = false;
164
+ map->entries = NULL;
165
+ map->entry_count = 0u;
166
+ map->entry_capacity = 0u;
167
+ map->live_count = 0u;
168
+ OseoResult published = oseo_internal_publish_heap(
169
+ context,
170
+ &map->ordinary.header,
171
+ OSEO_HEAP_MAP
172
+ );
173
+ oseo_roots_pop(context, &frame);
174
+ return published;
175
+ }
176
+
177
+ static OseoResult map_prototype_intrinsic(OseoContext *context) {
178
+ OseoValue *cache =
179
+ &context->realm->intrinsics[OSEO_INTRINSIC_MAP_PROTOTYPE];
180
+ if (tag_of(*cache) != OSEO_TAG_UNDEFINED) return normal(*cache);
181
+ OseoResult built = map_intrinsic_build(context);
182
+ if (built.status != OSEO_STATUS_NORMAL) return built;
183
+ return normal(*cache);
184
+ }
185
+
186
+ /* ! Construct(%Map%): a fresh empty map taking the realm prototype
187
+ * directly, used by Map.groupBy, which never observes a subclass. */
188
+ static OseoResult map_create(OseoContext *context) {
189
+ OseoResult prototype = map_prototype_intrinsic(context);
190
+ if (prototype.status != OSEO_STATUS_NORMAL) return prototype;
191
+ return map_allocate(context, prototype.value);
192
+ }
193
+
194
+ /*
195
+ * OrdinaryCreateFromConstructor(newTarget, "%Map.prototype%"). A
196
+ * subclass constructor carries its own `prototype` object, so
197
+ * `new Subclass(iterable)` gives its instance that object while a plain
198
+ * `new Map(iterable)` keeps the realm prototype. 24.1.1.1 performs this
199
+ * before it reads the `set` adder, and the `prototype` read is a real
200
+ * Get: `Reflect.construct` can name a bound function, which owns no
201
+ * synthetic slot and can carry an observable accessor instead.
202
+ */
203
+ static OseoResult map_create_from_constructor(
204
+ OseoContext *context,
205
+ OseoValue new_target
206
+ ) {
207
+ OseoValue slots[2] = {new_target, oseo_undefined()};
208
+ OseoRootFrame frame = {NULL, slots, 2u};
209
+ oseo_roots_push(context, &frame);
210
+ OseoResult result = oseo_internal_constructor_prototype(
211
+ context,
212
+ slots[0]
213
+ );
214
+ slots[1] = result.value;
215
+ if (result.status == OSEO_STATUS_NORMAL && !is_object(slots[1])) {
216
+ result = oseo_internal_constructor_realm_default(
217
+ context,
218
+ slots[0],
219
+ OSEO_INTRINSIC_MAP_PROTOTYPE
220
+ );
221
+ slots[1] = result.value;
222
+ }
223
+ if (result.status == OSEO_STATUS_NORMAL) {
224
+ result = map_allocate(context, slots[1]);
225
+ }
226
+ oseo_roots_pop(context, &frame);
227
+ return result;
228
+ }
229
+
230
+ /*
231
+ * Constructor: iteration, IteratorClose, and Map.groupBy.
232
+ */
233
+
234
+ /* IfAbruptCloseIterator: closes the iterator record and returns the
235
+ * original abrupt completion, unless closing itself throws or the
236
+ * runtime already carries an internal diagnostic rather than a
237
+ * catchable thrown value. `abrupt` is returned unexamined when it is
238
+ * not a catchable throw, which is what keeps a non-catchable internal
239
+ * diagnostic, such as an allocation failure while constructing the
240
+ * thrown error itself, from being cleared and replaced with a
241
+ * catchable completion. */
242
+ static OseoResult map_iterator_close_after_abrupt(
243
+ OseoContext *context,
244
+ OseoValue iterator,
245
+ OseoResult abrupt
246
+ ) {
247
+ if (abrupt.status != OSEO_STATUS_THROW || context->has_diagnostic) {
248
+ return abrupt;
249
+ }
250
+ OseoValue slots[2] = {iterator, abrupt.value};
251
+ OseoRootFrame frame = {NULL, slots, 2u};
252
+ oseo_roots_push(context, &frame);
253
+ oseo_context_clear_language_error(context);
254
+ OseoResult closed = oseo_iterator_close(context, slots[0], true);
255
+ OseoResult result = closed.status == OSEO_STATUS_NORMAL
256
+ ? (OseoResult){OSEO_STATUS_THROW, slots[1]}
257
+ : closed;
258
+ oseo_roots_pop(context, &frame);
259
+ return result;
260
+ }
261
+
262
+ /*
263
+ * AddEntriesFromIterable(target, iterable, adder), specialized to the
264
+ * Map constructor's own already-captured adder.
265
+ */
266
+ static OseoResult map_add_entries_from_iterable(
267
+ OseoContext *context,
268
+ OseoValue map_value,
269
+ OseoValue iterable,
270
+ OseoValue adder
271
+ ) {
272
+ /* 0 map, 1 adder, 2 iterator, 3 next method, 4 next item,
273
+ * 5 "0" key, 6 "1" key, 7 k, 8 v. */
274
+ OseoValue slots[9] = {
275
+ map_value, adder, iterable, oseo_undefined(), oseo_undefined(),
276
+ oseo_undefined(), oseo_undefined(), oseo_undefined(), oseo_undefined()
277
+ };
278
+ OseoRootFrame frame = {NULL, slots, 9u};
279
+ oseo_roots_push(context, &frame);
280
+ OseoValue next_method = oseo_undefined();
281
+ OseoResult result = oseo_iterator_get(context, slots[2], &next_method);
282
+ slots[2] = result.value;
283
+ if (result.status == OSEO_STATUS_NORMAL) slots[3] = next_method;
284
+ static const uint16_t zero_units[] = {'0'};
285
+ static const uint16_t one_units[] = {'1'};
286
+ if (result.status == OSEO_STATUS_NORMAL) {
287
+ result = oseo_string_from_units(context, zero_units, 1u);
288
+ slots[5] = result.value;
289
+ }
290
+ if (result.status == OSEO_STATUS_NORMAL) {
291
+ result = oseo_string_from_units(context, one_units, 1u);
292
+ slots[6] = result.value;
293
+ }
294
+ while (result.status == OSEO_STATUS_NORMAL) {
295
+ bool done = false;
296
+ result = oseo_iterator_next(
297
+ context,
298
+ slots[2],
299
+ slots[3],
300
+ &slots[4],
301
+ &done
302
+ );
303
+ /* A throw from IteratorStep leaves the iterator record done, so
304
+ * the specification rejects without calling IteratorClose. */
305
+ if (result.status != OSEO_STATUS_NORMAL || done) break;
306
+ if (!is_object(slots[4])) {
307
+ result = oseo_internal_throw_error(
308
+ context,
309
+ OSEO_ERROR_TYPE,
310
+ "A Map constructor iterable entry must be an object."
311
+ );
312
+ result = map_iterator_close_after_abrupt(
313
+ context,
314
+ slots[2],
315
+ result
316
+ );
317
+ break;
318
+ }
319
+ result = oseo_object_get(context, slots[4], slots[5]);
320
+ slots[7] = result.value;
321
+ if (result.status != OSEO_STATUS_NORMAL) {
322
+ result = map_iterator_close_after_abrupt(
323
+ context,
324
+ slots[2],
325
+ result
326
+ );
327
+ break;
328
+ }
329
+ result = oseo_object_get(context, slots[4], slots[6]);
330
+ slots[8] = result.value;
331
+ if (result.status != OSEO_STATUS_NORMAL) {
332
+ result = map_iterator_close_after_abrupt(
333
+ context,
334
+ slots[2],
335
+ result
336
+ );
337
+ break;
338
+ }
339
+ result = oseo_call_function(
340
+ context,
341
+ slots[1],
342
+ slots[0],
343
+ 2u,
344
+ &slots[7],
345
+ oseo_undefined()
346
+ );
347
+ if (result.status != OSEO_STATUS_NORMAL) {
348
+ result = map_iterator_close_after_abrupt(
349
+ context,
350
+ slots[2],
351
+ result
352
+ );
353
+ break;
354
+ }
355
+ }
356
+ OseoValue map_result = slots[0];
357
+ oseo_roots_pop(context, &frame);
358
+ return result.status == OSEO_STATUS_NORMAL ? normal(map_result) : result;
359
+ }
360
+
361
+ /*
362
+ * Slots: 0 the new target, 1 the iterable, 2 the created map, 3 the
363
+ * adder. OrdinaryCreateFromConstructor runs the new target's
364
+ * `prototype` accessor and then allocates, so both the argument and the
365
+ * new target are rooted across it rather than held in C locals.
366
+ */
367
+ static OseoResult map_construct_with_target(
368
+ OseoContext *context,
369
+ OseoValue new_target,
370
+ OseoValue iterable
371
+ ) {
372
+ OseoValue slots[4] = {
373
+ new_target,
374
+ iterable,
375
+ oseo_undefined(),
376
+ oseo_undefined(),
377
+ };
378
+ OseoRootFrame frame = {NULL, slots, 4u};
379
+ oseo_roots_push(context, &frame);
380
+ OseoResult result = map_create_from_constructor(context, slots[0]);
381
+ slots[2] = result.value;
382
+ bool adds = result.status == OSEO_STATUS_NORMAL && !is_nullish(slots[1]);
383
+ if (adds) {
384
+ static const uint16_t set_units[] = {'s', 'e', 't'};
385
+ result = oseo_string_from_units(context, set_units, 3u);
386
+ if (result.status == OSEO_STATUS_NORMAL) {
387
+ result = oseo_object_get(context, slots[2], result.value);
388
+ slots[3] = result.value;
389
+ }
390
+ }
391
+ if (adds && result.status == OSEO_STATUS_NORMAL &&
392
+ !is_callable(slots[3])) {
393
+ result = oseo_internal_throw_error(
394
+ context,
395
+ OSEO_ERROR_TYPE,
396
+ "The Map adder property is not callable."
397
+ );
398
+ }
399
+ if (adds && result.status == OSEO_STATUS_NORMAL) {
400
+ result = map_add_entries_from_iterable(
401
+ context,
402
+ slots[2],
403
+ slots[1],
404
+ slots[3]
405
+ );
406
+ }
407
+ OseoValue map_value = slots[2];
408
+ oseo_roots_pop(context, &frame);
409
+ return result.status == OSEO_STATUS_NORMAL ? normal(map_value) : result;
410
+ }
411
+
412
+ /*
413
+ * Map.groupBy(items, callbackfn): GroupBy with the zero (SameValueZero)
414
+ * key comparator, built directly into a fresh map's [[MapData]] instead
415
+ * of through a Get(map, "set") lookup, since the specified algorithm
416
+ * never observes an overridden adder.
417
+ */
418
+ static OseoResult map_group_by(
419
+ OseoContext *context,
420
+ OseoValue items,
421
+ OseoValue callback
422
+ ) {
423
+ if (!is_callable(callback)) {
424
+ return oseo_internal_throw_error(
425
+ context,
426
+ OSEO_ERROR_TYPE,
427
+ "Map.groupBy callback is not callable."
428
+ );
429
+ }
430
+ OseoResult created = map_create(context);
431
+ if (created.status != OSEO_STATUS_NORMAL) return created;
432
+ /* 0 map, 1 callback, 2 iterator, 3 next method, 4 value,
433
+ * 5 index number, 6 key, 7 group array. */
434
+ OseoValue slots[8] = {
435
+ created.value, callback, items, oseo_undefined(), oseo_undefined(),
436
+ oseo_undefined(), oseo_undefined(), oseo_undefined()
437
+ };
438
+ OseoRootFrame frame = {NULL, slots, 8u};
439
+ oseo_roots_push(context, &frame);
440
+ OseoValue next_method = oseo_undefined();
441
+ OseoResult result = oseo_iterator_get(context, slots[2], &next_method);
442
+ slots[2] = result.value;
443
+ if (result.status == OSEO_STATUS_NORMAL) slots[3] = next_method;
444
+ double index = 0.0;
445
+ while (result.status == OSEO_STATUS_NORMAL) {
446
+ /* GroupBy checks k >= 2^53 - 1 before every IteratorStepValue,
447
+ * even one that would report the iterator done, rather than
448
+ * after. */
449
+ if (index >= 9007199254740991.0) {
450
+ result = oseo_internal_throw_error(
451
+ context,
452
+ OSEO_ERROR_TYPE,
453
+ "Map.groupBy has too many values."
454
+ );
455
+ result = map_iterator_close_after_abrupt(
456
+ context,
457
+ slots[2],
458
+ result
459
+ );
460
+ break;
461
+ }
462
+ bool done = false;
463
+ result = oseo_iterator_next(
464
+ context,
465
+ slots[2],
466
+ slots[3],
467
+ &slots[4],
468
+ &done
469
+ );
470
+ if (result.status != OSEO_STATUS_NORMAL || done) break;
471
+ slots[5] = oseo_number(index);
472
+ index += 1.0;
473
+ result = oseo_call_function(
474
+ context,
475
+ slots[1],
476
+ oseo_undefined(),
477
+ 2u,
478
+ &slots[4],
479
+ oseo_undefined()
480
+ );
481
+ if (result.status != OSEO_STATUS_NORMAL) {
482
+ result = map_iterator_close_after_abrupt(
483
+ context,
484
+ slots[2],
485
+ result
486
+ );
487
+ break;
488
+ }
489
+ slots[6] = map_normalize_key(result.value);
490
+ size_t found = map_entry_find(map_object(slots[0]), slots[6]);
491
+ if (found != SIZE_MAX) {
492
+ slots[7] = map_object(slots[0])->entries[found].value;
493
+ result = oseo_array_append(context, slots[7], slots[4]);
494
+ } else {
495
+ result = oseo_array_create(context, 0u);
496
+ slots[7] = result.value;
497
+ if (result.status == OSEO_STATUS_NORMAL) {
498
+ result = oseo_array_append(context, slots[7], slots[4]);
499
+ }
500
+ if (result.status == OSEO_STATUS_NORMAL) {
501
+ result = map_append_entry(
502
+ context,
503
+ slots[0],
504
+ slots[6],
505
+ slots[7]
506
+ );
507
+ }
508
+ }
509
+ if (result.status != OSEO_STATUS_NORMAL) break;
510
+ }
511
+ OseoValue map_result = slots[0];
512
+ oseo_roots_pop(context, &frame);
513
+ return result.status == OSEO_STATUS_NORMAL ? normal(map_result) : result;
514
+ }
515
+
516
+ /*
517
+ * Map.prototype methods. Each brands its receiver before touching
518
+ * [[MapData]].
519
+ */
520
+
521
+ static OseoResult map_prototype_get(
522
+ OseoContext *context,
523
+ OseoValue receiver,
524
+ OseoValue key
525
+ ) {
526
+ if (!is_map(receiver)) {
527
+ return oseo_internal_throw_error(
528
+ context,
529
+ OSEO_ERROR_TYPE,
530
+ "Method Map.prototype.get called on incompatible receiver."
531
+ );
532
+ }
533
+ size_t index = map_entry_find(map_object(receiver), key);
534
+ if (index == SIZE_MAX) return normal(oseo_undefined());
535
+ return normal(map_object(receiver)->entries[index].value);
536
+ }
537
+
538
+ static OseoResult map_prototype_has(
539
+ OseoContext *context,
540
+ OseoValue receiver,
541
+ OseoValue key
542
+ ) {
543
+ if (!is_map(receiver)) {
544
+ return oseo_internal_throw_error(
545
+ context,
546
+ OSEO_ERROR_TYPE,
547
+ "Method Map.prototype.has called on incompatible receiver."
548
+ );
549
+ }
550
+ bool present = map_entry_find(map_object(receiver), key) != SIZE_MAX;
551
+ return normal(oseo_boolean(present));
552
+ }
553
+
554
+ static OseoResult map_prototype_set(
555
+ OseoContext *context,
556
+ OseoValue receiver,
557
+ OseoValue key,
558
+ OseoValue value
559
+ ) {
560
+ if (!is_map(receiver)) {
561
+ return oseo_internal_throw_error(
562
+ context,
563
+ OSEO_ERROR_TYPE,
564
+ "Method Map.prototype.set called on incompatible receiver."
565
+ );
566
+ }
567
+ OseoValue normalized_key = map_normalize_key(key);
568
+ size_t index = map_entry_find(map_object(receiver), normalized_key);
569
+ if (index != SIZE_MAX) {
570
+ map_object(receiver)->entries[index].value = value;
571
+ return normal(receiver);
572
+ }
573
+ OseoResult result =
574
+ map_append_entry(context, receiver, normalized_key, value);
575
+ if (result.status != OSEO_STATUS_NORMAL) return result;
576
+ return normal(receiver);
577
+ }
578
+
579
+ /*
580
+ * Map.prototype.getOrInsert, ECMAScript 2026 24.1.3.7. An existing
581
+ * record keeps its value; otherwise the canonical key and the given
582
+ * value are appended, and either way the stored value is returned.
583
+ */
584
+ static OseoResult map_prototype_get_or_insert(
585
+ OseoContext *context,
586
+ OseoValue receiver,
587
+ OseoValue key,
588
+ OseoValue value
589
+ ) {
590
+ if (!is_map(receiver)) {
591
+ return oseo_internal_throw_error(
592
+ context,
593
+ OSEO_ERROR_TYPE,
594
+ "Method Map.prototype.getOrInsert called on incompatible "
595
+ "receiver."
596
+ );
597
+ }
598
+ OseoValue normalized_key = map_normalize_key(key);
599
+ size_t index = map_entry_find(map_object(receiver), normalized_key);
600
+ if (index != SIZE_MAX) {
601
+ return normal(map_object(receiver)->entries[index].value);
602
+ }
603
+ OseoResult result =
604
+ map_append_entry(context, receiver, normalized_key, value);
605
+ if (result.status != OSEO_STATUS_NORMAL) return result;
606
+ return normal(value);
607
+ }
608
+
609
+ /*
610
+ * Map.prototype.getOrInsertComputed, ECMAScript 2026 24.1.3.8. The
611
+ * callback runs once, with undefined as its receiver and the canonical
612
+ * key as its argument, only when the key is absent. Because the
613
+ * callback may itself insert, delete, or clear, the key is looked up
614
+ * again afterwards: a record it inserted takes the computed value, and
615
+ * otherwise a fresh record is appended.
616
+ */
617
+ static OseoResult map_prototype_get_or_insert_computed(
618
+ OseoContext *context,
619
+ OseoValue receiver,
620
+ OseoValue key,
621
+ OseoValue callback
622
+ ) {
623
+ if (!is_map(receiver)) {
624
+ return oseo_internal_throw_error(
625
+ context,
626
+ OSEO_ERROR_TYPE,
627
+ "Method Map.prototype.getOrInsertComputed called on "
628
+ "incompatible receiver."
629
+ );
630
+ }
631
+ if (!is_callable(callback)) {
632
+ return oseo_internal_throw_error(
633
+ context,
634
+ OSEO_ERROR_TYPE,
635
+ "Map.prototype.getOrInsertComputed callback is not callable."
636
+ );
637
+ }
638
+ OseoValue normalized_key = map_normalize_key(key);
639
+ size_t index = map_entry_find(map_object(receiver), normalized_key);
640
+ if (index != SIZE_MAX) {
641
+ return normal(map_object(receiver)->entries[index].value);
642
+ }
643
+ /* 0 map, 1 callback, 2 key (the one call argument), 3 value. */
644
+ OseoValue slots[4] = {receiver, callback, normalized_key, oseo_undefined()};
645
+ OseoRootFrame frame = {NULL, slots, 4u};
646
+ oseo_roots_push(context, &frame);
647
+ OseoResult result = oseo_call_function(
648
+ context,
649
+ slots[1],
650
+ oseo_undefined(),
651
+ 1u,
652
+ &slots[2],
653
+ oseo_undefined()
654
+ );
655
+ if (result.status == OSEO_STATUS_NORMAL) {
656
+ slots[3] = result.value;
657
+ OseoMap *map = map_object(slots[0]);
658
+ index = map_entry_find(map, slots[2]);
659
+ if (index != SIZE_MAX) {
660
+ map->entries[index].value = slots[3];
661
+ } else {
662
+ result = map_append_entry(context, slots[0], slots[2], slots[3]);
663
+ }
664
+ }
665
+ if (result.status == OSEO_STATUS_NORMAL) result = normal(slots[3]);
666
+ oseo_roots_pop(context, &frame);
667
+ return result;
668
+ }
669
+
670
+ static OseoResult map_prototype_delete(
671
+ OseoContext *context,
672
+ OseoValue receiver,
673
+ OseoValue key
674
+ ) {
675
+ if (!is_map(receiver)) {
676
+ return oseo_internal_throw_error(
677
+ context,
678
+ OSEO_ERROR_TYPE,
679
+ "Method Map.prototype.delete called on incompatible receiver."
680
+ );
681
+ }
682
+ OseoMap *map = map_object(receiver);
683
+ size_t index = map_entry_find(map, key);
684
+ if (index == SIZE_MAX) return normal(oseo_boolean(false));
685
+ map->entries[index].live = false;
686
+ map->entries[index].key = oseo_undefined();
687
+ map->entries[index].value = oseo_undefined();
688
+ map->live_count -= 1u;
689
+ return normal(oseo_boolean(true));
690
+ }
691
+
692
+ static OseoResult map_prototype_clear(
693
+ OseoContext *context,
694
+ OseoValue receiver
695
+ ) {
696
+ if (!is_map(receiver)) {
697
+ return oseo_internal_throw_error(
698
+ context,
699
+ OSEO_ERROR_TYPE,
700
+ "Method Map.prototype.clear called on incompatible receiver."
701
+ );
702
+ }
703
+ OseoMap *map = map_object(receiver);
704
+ for (size_t index = 0u; index < map->entry_count; index += 1u) {
705
+ map->entries[index].live = false;
706
+ map->entries[index].key = oseo_undefined();
707
+ map->entries[index].value = oseo_undefined();
708
+ }
709
+ map->live_count = 0u;
710
+ return normal(oseo_undefined());
711
+ }
712
+
713
+ static OseoResult map_prototype_size_getter(
714
+ OseoContext *context,
715
+ OseoValue receiver
716
+ ) {
717
+ if (!is_map(receiver)) {
718
+ return oseo_internal_throw_error(
719
+ context,
720
+ OSEO_ERROR_TYPE,
721
+ "Method get Map.prototype.size called on incompatible receiver."
722
+ );
723
+ }
724
+ return normal(oseo_number((double)map_object(receiver)->live_count));
725
+ }
726
+
727
+ /*
728
+ * Map.prototype.forEach walks [[MapData]] by index rather than a
729
+ * snapshot, re-reading the entry array pointer on every step: the
730
+ * callback may itself call `set` on this map, which can reallocate the
731
+ * backing storage and grow entry_count, and a value it adds during the
732
+ * walk is still visited once the walk reaches it, matching the
733
+ * specified behavior.
734
+ */
735
+ static OseoResult map_prototype_for_each(
736
+ OseoContext *context,
737
+ OseoValue receiver,
738
+ OseoValue callback,
739
+ OseoValue this_argument
740
+ ) {
741
+ if (!is_map(receiver)) {
742
+ return oseo_internal_throw_error(
743
+ context,
744
+ OSEO_ERROR_TYPE,
745
+ "Method Map.prototype.forEach called on incompatible receiver."
746
+ );
747
+ }
748
+ if (!is_callable(callback)) {
749
+ return oseo_internal_throw_error(
750
+ context,
751
+ OSEO_ERROR_TYPE,
752
+ "Map.prototype.forEach callback is not callable."
753
+ );
754
+ }
755
+ /* 0 map, 1 callback, 2 thisArg, 3 value, 4 key, 5 map (again, so the
756
+ * three call arguments sit in one contiguous rooted range). */
757
+ OseoValue slots[6] = {
758
+ receiver, callback, this_argument,
759
+ oseo_undefined(), oseo_undefined(), receiver
760
+ };
761
+ OseoRootFrame frame = {NULL, slots, 6u};
762
+ oseo_roots_push(context, &frame);
763
+ OseoResult result = normal(oseo_undefined());
764
+ size_t index = 0u;
765
+ while (true) {
766
+ OseoMap *map = map_object(slots[0]);
767
+ if (index >= map->entry_count) break;
768
+ if (!map->entries[index].live) {
769
+ index += 1u;
770
+ continue;
771
+ }
772
+ slots[3] = map->entries[index].value;
773
+ slots[4] = map->entries[index].key;
774
+ index += 1u;
775
+ OseoResult called = oseo_call_function(
776
+ context,
777
+ slots[1],
778
+ slots[2],
779
+ 3u,
780
+ &slots[3],
781
+ oseo_undefined()
782
+ );
783
+ /* forEach always returns undefined; only an abrupt callback
784
+ * completion overrides that, never a callback's return value. */
785
+ if (called.status != OSEO_STATUS_NORMAL) {
786
+ result = called;
787
+ break;
788
+ }
789
+ }
790
+ oseo_roots_pop(context, &frame);
791
+ return result;
792
+ }
793
+
794
+ /*
795
+ * %MapIteratorPrototype% and CreateMapIterator.
796
+ */
797
+
798
+ static OseoResult map_iterator_prototype_intrinsic(OseoContext *context) {
799
+ OseoValue *cache =
800
+ &context->realm->intrinsics[OSEO_INTRINSIC_MAP_ITERATOR_PROTOTYPE];
801
+ if (tag_of(*cache) != OSEO_TAG_UNDEFINED) return normal(*cache);
802
+ OseoResult built = map_intrinsic_build(context);
803
+ if (built.status != OSEO_STATUS_NORMAL) return built;
804
+ return normal(*cache);
805
+ }
806
+
807
+ static OseoResult map_create_iterator(
808
+ OseoContext *context,
809
+ OseoValue receiver,
810
+ OseoMapIterationKind kind,
811
+ const char *message
812
+ ) {
813
+ if (!is_map(receiver)) {
814
+ return oseo_internal_throw_error(context, OSEO_ERROR_TYPE, message);
815
+ }
816
+ OseoResult prototype = map_iterator_prototype_intrinsic(context);
817
+ if (prototype.status != OSEO_STATUS_NORMAL) return prototype;
818
+ OseoValue slots[2] = {receiver, prototype.value};
819
+ OseoRootFrame frame = {NULL, slots, 2u};
820
+ oseo_roots_push(context, &frame);
821
+ OseoMapIterator *iterator =
822
+ oseo_internal_allocate_heap_bytes(context, sizeof(*iterator));
823
+ if (iterator == NULL) {
824
+ oseo_roots_pop(context, &frame);
825
+ return failure(
826
+ context,
827
+ "OSEO2001",
828
+ "Map iterator allocation failed."
829
+ );
830
+ }
831
+ iterator->ordinary.prototype = slots[1];
832
+ iterator->ordinary.properties = NULL;
833
+ iterator->ordinary.property_capacity = 0u;
834
+ iterator->ordinary.property_count = 0u;
835
+ oseo_internal_init_dense_index(&iterator->ordinary);
836
+ iterator->ordinary.private_elements = NULL;
837
+ iterator->ordinary.private_element_capacity = 0u;
838
+ iterator->ordinary.private_element_count = 0u;
839
+ iterator->ordinary.shape_id = context->next_shape_id;
840
+ context->next_shape_id += 1u;
841
+ iterator->ordinary.array_length = 0u;
842
+ iterator->ordinary.dictionary = false;
843
+ iterator->ordinary.length_writable = false;
844
+ iterator->ordinary.extensible = true;
845
+ iterator->ordinary.module_namespace = false;
846
+ iterator->ordinary.immutable_prototype = false;
847
+ iterator->ordinary.global_object = false;
848
+ iterator->ordinary.error_data = false;
849
+ iterator->ordinary.number_data = false;
850
+ iterator->ordinary.number_value = oseo_undefined();
851
+ iterator->ordinary.primitive_data = false;
852
+ iterator->ordinary.primitive_value = oseo_undefined();
853
+ iterator->ordinary.primitive_wrapper_methods_initialized = false;
854
+ iterator->ordinary.virtual_string_iterator = false;
855
+ iterator->ordinary.virtual_string_iterator_configurable = false;
856
+ iterator->ordinary.virtual_string_iterator_enumerable = false;
857
+ iterator->ordinary.virtual_string_iterator_writable = false;
858
+ iterator->ordinary.iterator_kind = OSEO_ITERATOR_NONE;
859
+ iterator->ordinary.iterator_target = oseo_undefined();
860
+ iterator->ordinary.iterator_index = 0u;
861
+ iterator->ordinary.regexp_string_iterator = false;
862
+ iterator->ordinary.regexp_iterator_regexp = oseo_undefined();
863
+ iterator->ordinary.regexp_iterator_subject = oseo_undefined();
864
+ iterator->ordinary.regexp_iterator_global = false;
865
+ iterator->ordinary.regexp_iterator_unicode = false;
866
+ iterator->ordinary.regexp_iterator_complete = false;
867
+ iterator->ordinary.async_from_sync = false;
868
+ iterator->ordinary.async_sync_iterator = oseo_undefined();
869
+ iterator->ordinary.wrap_for_valid_iterator = false;
870
+ iterator->ordinary.wrapped_iterator = oseo_undefined();
871
+ iterator->ordinary.wrapped_next = oseo_undefined();
872
+ iterator->ordinary.generator = NULL;
873
+ iterator->ordinary.arguments_object = false;
874
+ iterator->ordinary.mapped_arguments = false;
875
+ iterator->target = slots[0];
876
+ iterator->index = 0u;
877
+ iterator->kind = kind;
878
+ OseoResult published = oseo_internal_publish_heap(
879
+ context,
880
+ &iterator->ordinary.header,
881
+ OSEO_HEAP_MAP_ITERATOR
882
+ );
883
+ oseo_roots_pop(context, &frame);
884
+ return published;
885
+ }
886
+
887
+ static OseoResult map_iterator_next(OseoContext *context, OseoValue receiver) {
888
+ if (!is_map_iterator(receiver)) {
889
+ return oseo_internal_throw_error(
890
+ context,
891
+ OSEO_ERROR_TYPE,
892
+ "Method %MapIteratorPrototype%.next called on incompatible "
893
+ "receiver."
894
+ );
895
+ }
896
+ OseoMapIterator *iterator = map_iterator_object(receiver);
897
+ if (tag_of(iterator->target) == OSEO_TAG_UNDEFINED) {
898
+ return oseo_internal_iterator_result(context, oseo_undefined(), true);
899
+ }
900
+ OseoValue slots[3] = {receiver, iterator->target, oseo_undefined()};
901
+ OseoRootFrame frame = {NULL, slots, 3u};
902
+ oseo_roots_push(context, &frame);
903
+ OseoResult result = normal(oseo_undefined());
904
+ bool found = false;
905
+ while (true) {
906
+ OseoMap *map = map_object(slots[1]);
907
+ OseoMapIterator *live_iterator = map_iterator_object(slots[0]);
908
+ if (live_iterator->index >= map->entry_count) break;
909
+ size_t index = live_iterator->index;
910
+ live_iterator->index += 1u;
911
+ if (!map->entries[index].live) continue;
912
+ found = true;
913
+ if (live_iterator->kind == OSEO_MAP_ITERATION_KEY) {
914
+ slots[2] = map->entries[index].key;
915
+ } else if (live_iterator->kind == OSEO_MAP_ITERATION_VALUE) {
916
+ slots[2] = map->entries[index].value;
917
+ } else {
918
+ OseoValue key = map->entries[index].key;
919
+ OseoValue value = map->entries[index].value;
920
+ result = oseo_array_create(context, 0u);
921
+ slots[2] = result.value;
922
+ if (result.status == OSEO_STATUS_NORMAL) {
923
+ result = oseo_array_append(context, slots[2], key);
924
+ }
925
+ if (result.status == OSEO_STATUS_NORMAL) {
926
+ result = oseo_array_append(context, slots[2], value);
927
+ }
928
+ }
929
+ break;
930
+ }
931
+ if (result.status == OSEO_STATUS_NORMAL && found) {
932
+ result = oseo_internal_iterator_result(context, slots[2], false);
933
+ } else if (result.status == OSEO_STATUS_NORMAL) {
934
+ map_iterator_object(slots[0])->target = oseo_undefined();
935
+ result = oseo_internal_iterator_result(
936
+ context,
937
+ oseo_undefined(),
938
+ true
939
+ );
940
+ }
941
+ oseo_roots_pop(context, &frame);
942
+ return result;
943
+ }
944
+
945
+ /*
946
+ * Intrinsic cluster construction and installation.
947
+ */
948
+
949
+ static OseoResult create_map_builtin(
950
+ OseoContext *context,
951
+ size_t code_id,
952
+ const char *name,
953
+ size_t length,
954
+ OseoFunctionKind kind,
955
+ OseoFunctionNamePrefix prefix
956
+ ) {
957
+ size_t name_length = strlen(name);
958
+ if (name_length > 31u) {
959
+ return failure(context, "OSEO2001", "Built-in name is too long.");
960
+ }
961
+ uint16_t units[31];
962
+ for (size_t index = 0u; index < name_length; index += 1u) {
963
+ units[index] = (uint16_t)(unsigned char)name[index];
964
+ }
965
+ OseoValue environment = oseo_undefined();
966
+ OseoRootFrame frame = {NULL, &environment, 1u};
967
+ oseo_roots_push(context, &frame);
968
+ OseoResult result = oseo_environment_create(context, 0u);
969
+ environment = result.value;
970
+ if (result.status == OSEO_STATUS_NORMAL) {
971
+ result = oseo_function_create(
972
+ context,
973
+ code_id,
974
+ environment,
975
+ units,
976
+ name_length,
977
+ length,
978
+ kind,
979
+ oseo_undefined(),
980
+ oseo_undefined(),
981
+ prefix
982
+ );
983
+ }
984
+ oseo_roots_pop(context, &frame);
985
+ return result;
986
+ }
987
+
988
+ static OseoResult define_map_property(
989
+ OseoContext *context,
990
+ OseoValue object,
991
+ const char *name,
992
+ OseoValue value,
993
+ OseoPropertyAttributes attributes
994
+ ) {
995
+ OseoValue slots[3] = {object, value, oseo_undefined()};
996
+ OseoRootFrame frame = {NULL, slots, 3u};
997
+ oseo_roots_push(context, &frame);
998
+ OseoResult result = oseo_internal_ascii_string(context, name);
999
+ slots[2] = result.value;
1000
+ if (result.status == OSEO_STATUS_NORMAL) {
1001
+ result = oseo_object_define(
1002
+ context,
1003
+ slots[0],
1004
+ slots[2],
1005
+ slots[1],
1006
+ attributes
1007
+ );
1008
+ }
1009
+ oseo_roots_pop(context, &frame);
1010
+ return result;
1011
+ }
1012
+
1013
+ /*
1014
+ * Materializes %MapIteratorPrototype%, %Map.prototype%, and %Map%
1015
+ * together, so a map or map iterator reached without naming `Map`
1016
+ * still finds every specified own property on its prototype chain.
1017
+ *
1018
+ * `OSEO_INTRINSIC_MAP_SPECIES` is filled last, so it doubles as the
1019
+ * completion marker the way the Promise cluster's species slot does:
1020
+ * a partially built cluster leaves it undefined and the failure path
1021
+ * clears every slot the attempt filled, and the uninitialized sentinel
1022
+ * reports a reentrant build attempt instead of splitting identities
1023
+ * across two concurrent attempts.
1024
+ */
1025
+ static OseoResult map_intrinsic_build(OseoContext *context) {
1026
+ OseoValue *marker = &context->realm->intrinsics[OSEO_INTRINSIC_MAP_SPECIES];
1027
+ if (tag_of(*marker) == OSEO_TAG_UNINITIALIZED) {
1028
+ return failure(
1029
+ context,
1030
+ "OSEO2001",
1031
+ "The Map intrinsic cluster is already being built."
1032
+ );
1033
+ }
1034
+ if (tag_of(*marker) != OSEO_TAG_UNDEFINED) return normal(*marker);
1035
+ size_t entry_allocations = context->allocations;
1036
+ OseoRootFrame frame = {NULL, NULL, 0u};
1037
+ OseoResult result = oseo_roots_allocate(context, &frame, 6u);
1038
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1039
+ *marker = oseo_uninitialized();
1040
+
1041
+ /* %MapIteratorPrototype%, independent of %Map%/%Map.prototype%. */
1042
+ result = oseo_internal_intrinsic(
1043
+ context,
1044
+ OSEO_INTRINSIC_ITERATOR_PROTOTYPE
1045
+ );
1046
+ frame.slots[0] = result.value;
1047
+ if (result.status == OSEO_STATUS_NORMAL) {
1048
+ result = oseo_object_create(context, frame.slots[0]);
1049
+ frame.slots[1] = result.value;
1050
+ }
1051
+ if (result.status == OSEO_STATUS_NORMAL) {
1052
+ context->realm->intrinsics[OSEO_INTRINSIC_MAP_ITERATOR_PROTOTYPE] =
1053
+ frame.slots[1];
1054
+ result = create_map_builtin(
1055
+ context,
1056
+ OSEO_MAP_ITERATOR_NEXT_CODE_ID,
1057
+ "next",
1058
+ 0u,
1059
+ OSEO_FUNCTION_INTERNAL,
1060
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1061
+ );
1062
+ frame.slots[2] = result.value;
1063
+ }
1064
+ if (result.status == OSEO_STATUS_NORMAL) {
1065
+ context->realm->intrinsics[OSEO_INTRINSIC_MAP_ITERATOR_NEXT] =
1066
+ frame.slots[2];
1067
+ result = define_map_property(
1068
+ context,
1069
+ frame.slots[1],
1070
+ "next",
1071
+ frame.slots[2],
1072
+ (OseoPropertyAttributes){true, false, true, false}
1073
+ );
1074
+ }
1075
+ if (result.status == OSEO_STATUS_NORMAL) {
1076
+ result = oseo_internal_well_known_symbol(
1077
+ context,
1078
+ OSEO_WELL_KNOWN_TO_STRING_TAG
1079
+ );
1080
+ frame.slots[3] = result.value;
1081
+ }
1082
+ if (result.status == OSEO_STATUS_NORMAL) {
1083
+ result = oseo_internal_ascii_string(context, "Map Iterator");
1084
+ frame.slots[4] = result.value;
1085
+ }
1086
+ if (result.status == OSEO_STATUS_NORMAL) {
1087
+ result = oseo_object_define(
1088
+ context,
1089
+ frame.slots[1],
1090
+ frame.slots[3],
1091
+ frame.slots[4],
1092
+ (OseoPropertyAttributes){true, false, false, false}
1093
+ );
1094
+ }
1095
+
1096
+ /* %Map.prototype%. */
1097
+ if (result.status == OSEO_STATUS_NORMAL) {
1098
+ result = oseo_internal_intrinsic(
1099
+ context,
1100
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
1101
+ );
1102
+ frame.slots[0] = result.value;
1103
+ }
1104
+ if (result.status == OSEO_STATUS_NORMAL) {
1105
+ result = oseo_object_create(context, frame.slots[0]);
1106
+ frame.slots[1] = result.value;
1107
+ }
1108
+ if (result.status == OSEO_STATUS_NORMAL) {
1109
+ context->realm->intrinsics[OSEO_INTRINSIC_MAP_PROTOTYPE] =
1110
+ frame.slots[1];
1111
+ }
1112
+ static const OseoIntrinsic method_intrinsics[] = {
1113
+ OSEO_INTRINSIC_MAP_GET,
1114
+ OSEO_INTRINSIC_MAP_SET,
1115
+ OSEO_INTRINSIC_MAP_HAS,
1116
+ OSEO_INTRINSIC_MAP_DELETE,
1117
+ OSEO_INTRINSIC_MAP_CLEAR,
1118
+ OSEO_INTRINSIC_MAP_FOR_EACH,
1119
+ OSEO_INTRINSIC_MAP_KEYS,
1120
+ OSEO_INTRINSIC_MAP_VALUES,
1121
+ };
1122
+ static const size_t method_codes[] = {
1123
+ OSEO_MAP_GET_CODE_ID,
1124
+ OSEO_MAP_SET_CODE_ID,
1125
+ OSEO_MAP_HAS_CODE_ID,
1126
+ OSEO_MAP_DELETE_CODE_ID,
1127
+ OSEO_MAP_CLEAR_CODE_ID,
1128
+ OSEO_MAP_FOR_EACH_CODE_ID,
1129
+ OSEO_MAP_KEYS_CODE_ID,
1130
+ OSEO_MAP_VALUES_CODE_ID,
1131
+ };
1132
+ static const char *const method_names[] = {
1133
+ "get", "set", "has", "delete", "clear", "forEach", "keys", "values",
1134
+ };
1135
+ static const size_t method_lengths[] = {1u, 2u, 1u, 1u, 0u, 1u, 0u, 0u};
1136
+ const OseoPropertyAttributes method = {true, false, true, false};
1137
+ for (size_t index = 0u;
1138
+ result.status == OSEO_STATUS_NORMAL && index < 8u;
1139
+ index += 1u) {
1140
+ result = create_map_builtin(
1141
+ context,
1142
+ method_codes[index],
1143
+ method_names[index],
1144
+ method_lengths[index],
1145
+ OSEO_FUNCTION_INTERNAL,
1146
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1147
+ );
1148
+ frame.slots[2] = result.value;
1149
+ if (result.status != OSEO_STATUS_NORMAL) break;
1150
+ context->realm->intrinsics[method_intrinsics[index]] = frame.slots[2];
1151
+ result = define_map_property(
1152
+ context,
1153
+ frame.slots[1],
1154
+ method_names[index],
1155
+ frame.slots[2],
1156
+ method
1157
+ );
1158
+ }
1159
+ /* The ECMAScript 2026 upsert methods own no intrinsic slot. */
1160
+ static const size_t upsert_codes[] = {
1161
+ OSEO_MAP_GET_OR_INSERT_CODE_ID,
1162
+ OSEO_MAP_GET_OR_INSERT_COMPUTED_CODE_ID,
1163
+ };
1164
+ static const char *const upsert_names[] = {
1165
+ "getOrInsert", "getOrInsertComputed",
1166
+ };
1167
+ for (size_t index = 0u;
1168
+ result.status == OSEO_STATUS_NORMAL && index < 2u;
1169
+ index += 1u) {
1170
+ result = create_map_builtin(
1171
+ context,
1172
+ upsert_codes[index],
1173
+ upsert_names[index],
1174
+ 2u,
1175
+ OSEO_FUNCTION_INTERNAL,
1176
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1177
+ );
1178
+ frame.slots[2] = result.value;
1179
+ if (result.status != OSEO_STATUS_NORMAL) break;
1180
+ result = define_map_property(
1181
+ context,
1182
+ frame.slots[1],
1183
+ upsert_names[index],
1184
+ frame.slots[2],
1185
+ method
1186
+ );
1187
+ }
1188
+ /* entries and [Symbol.iterator] are the same function object. */
1189
+ if (result.status == OSEO_STATUS_NORMAL) {
1190
+ result = create_map_builtin(
1191
+ context,
1192
+ OSEO_MAP_ENTRIES_CODE_ID,
1193
+ "entries",
1194
+ 0u,
1195
+ OSEO_FUNCTION_INTERNAL,
1196
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1197
+ );
1198
+ frame.slots[2] = result.value;
1199
+ }
1200
+ if (result.status == OSEO_STATUS_NORMAL) {
1201
+ context->realm->intrinsics[OSEO_INTRINSIC_MAP_ENTRIES] = frame.slots[2];
1202
+ result = define_map_property(
1203
+ context,
1204
+ frame.slots[1],
1205
+ "entries",
1206
+ frame.slots[2],
1207
+ method
1208
+ );
1209
+ }
1210
+ if (result.status == OSEO_STATUS_NORMAL) {
1211
+ result = oseo_internal_well_known_symbol(
1212
+ context,
1213
+ OSEO_WELL_KNOWN_ITERATOR
1214
+ );
1215
+ frame.slots[3] = result.value;
1216
+ }
1217
+ if (result.status == OSEO_STATUS_NORMAL) {
1218
+ result = oseo_object_define(
1219
+ context,
1220
+ frame.slots[1],
1221
+ frame.slots[3],
1222
+ frame.slots[2],
1223
+ method
1224
+ );
1225
+ }
1226
+ /* size, a getter-only accessor. */
1227
+ if (result.status == OSEO_STATUS_NORMAL) {
1228
+ result = create_map_builtin(
1229
+ context,
1230
+ OSEO_MAP_SIZE_GETTER_CODE_ID,
1231
+ "size",
1232
+ 0u,
1233
+ OSEO_FUNCTION_INTERNAL,
1234
+ OSEO_FUNCTION_NAME_PREFIX_GET
1235
+ );
1236
+ frame.slots[2] = result.value;
1237
+ }
1238
+ if (result.status == OSEO_STATUS_NORMAL) {
1239
+ result = oseo_internal_ascii_string(context, "size");
1240
+ frame.slots[3] = result.value;
1241
+ }
1242
+ if (result.status == OSEO_STATUS_NORMAL) {
1243
+ context->realm->intrinsics[OSEO_INTRINSIC_MAP_SIZE_GETTER] =
1244
+ frame.slots[2];
1245
+ result = oseo_object_define_accessor(
1246
+ context,
1247
+ frame.slots[1],
1248
+ frame.slots[3],
1249
+ frame.slots[2],
1250
+ oseo_undefined(),
1251
+ true,
1252
+ false,
1253
+ (OseoPropertyAttributes){true, false, false, true}
1254
+ );
1255
+ }
1256
+ if (result.status == OSEO_STATUS_NORMAL) {
1257
+ result = oseo_internal_well_known_symbol(
1258
+ context,
1259
+ OSEO_WELL_KNOWN_TO_STRING_TAG
1260
+ );
1261
+ frame.slots[3] = result.value;
1262
+ }
1263
+ if (result.status == OSEO_STATUS_NORMAL) {
1264
+ result = oseo_internal_ascii_string(context, "Map");
1265
+ frame.slots[4] = result.value;
1266
+ }
1267
+ if (result.status == OSEO_STATUS_NORMAL) {
1268
+ result = oseo_object_define(
1269
+ context,
1270
+ frame.slots[1],
1271
+ frame.slots[3],
1272
+ frame.slots[4],
1273
+ (OseoPropertyAttributes){true, false, false, false}
1274
+ );
1275
+ }
1276
+
1277
+ /* %Map%. */
1278
+ if (result.status == OSEO_STATUS_NORMAL) {
1279
+ result = create_map_builtin(
1280
+ context,
1281
+ OSEO_MAP_CONSTRUCTOR_CODE_ID,
1282
+ "Map",
1283
+ 0u,
1284
+ OSEO_FUNCTION_ORDINARY,
1285
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1286
+ );
1287
+ frame.slots[2] = result.value;
1288
+ }
1289
+ if (result.status == OSEO_STATUS_NORMAL) {
1290
+ context->realm->intrinsics[OSEO_INTRINSIC_MAP] = frame.slots[2];
1291
+ OseoFunction *constructor = function_object(frame.slots[2]);
1292
+ constructor->prototype_object = frame.slots[1];
1293
+ constructor->prototype_writable = false;
1294
+ result = define_map_property(
1295
+ context,
1296
+ frame.slots[1],
1297
+ "constructor",
1298
+ frame.slots[2],
1299
+ method
1300
+ );
1301
+ }
1302
+ if (result.status == OSEO_STATUS_NORMAL) {
1303
+ result = create_map_builtin(
1304
+ context,
1305
+ OSEO_MAP_GROUP_BY_CODE_ID,
1306
+ "groupBy",
1307
+ 2u,
1308
+ OSEO_FUNCTION_INTERNAL,
1309
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1310
+ );
1311
+ frame.slots[5] = result.value;
1312
+ }
1313
+ if (result.status == OSEO_STATUS_NORMAL) {
1314
+ context->realm->intrinsics[OSEO_INTRINSIC_MAP_GROUP_BY] =
1315
+ frame.slots[5];
1316
+ result = define_map_property(
1317
+ context,
1318
+ frame.slots[2],
1319
+ "groupBy",
1320
+ frame.slots[5],
1321
+ method
1322
+ );
1323
+ }
1324
+ if (result.status == OSEO_STATUS_NORMAL) {
1325
+ result = create_map_builtin(
1326
+ context,
1327
+ OSEO_MAP_SPECIES_CODE_ID,
1328
+ "[Symbol.species]",
1329
+ 0u,
1330
+ OSEO_FUNCTION_INTERNAL,
1331
+ OSEO_FUNCTION_NAME_PREFIX_GET
1332
+ );
1333
+ frame.slots[5] = result.value;
1334
+ }
1335
+ if (result.status == OSEO_STATUS_NORMAL) {
1336
+ result = oseo_internal_well_known_symbol(
1337
+ context,
1338
+ OSEO_WELL_KNOWN_SPECIES
1339
+ );
1340
+ frame.slots[3] = result.value;
1341
+ }
1342
+ if (result.status == OSEO_STATUS_NORMAL) {
1343
+ result = oseo_object_define_accessor(
1344
+ context,
1345
+ frame.slots[2],
1346
+ frame.slots[3],
1347
+ frame.slots[5],
1348
+ oseo_undefined(),
1349
+ true,
1350
+ false,
1351
+ (OseoPropertyAttributes){true, false, false, true}
1352
+ );
1353
+ }
1354
+ if (result.status != OSEO_STATUS_NORMAL) {
1355
+ for (size_t index = OSEO_INTRINSIC_MAP_ITERATOR_PROTOTYPE;
1356
+ index <= OSEO_INTRINSIC_MAP_SPECIES;
1357
+ index += 1u) {
1358
+ context->realm->intrinsics[index] = oseo_undefined();
1359
+ }
1360
+ oseo_roots_release(context, &frame);
1361
+ return result;
1362
+ }
1363
+ OseoValue species = frame.slots[5];
1364
+ context->realm->intrinsics[OSEO_INTRINSIC_MAP_SPECIES] = species;
1365
+ if (context->observe_specialization) {
1366
+ context->allocations = entry_allocations;
1367
+ }
1368
+ oseo_roots_release(context, &frame);
1369
+ return normal(species);
1370
+ }
1371
+
1372
+ OseoResult oseo_internal_map_prototype(OseoContext *context) {
1373
+ return map_prototype_intrinsic(context);
1374
+ }
1375
+
1376
+ OseoResult oseo_internal_map_intrinsic(OseoContext *context) {
1377
+ OseoResult built = map_intrinsic_build(context);
1378
+ if (built.status != OSEO_STATUS_NORMAL) return built;
1379
+ return normal(context->realm->intrinsics[OSEO_INTRINSIC_MAP]);
1380
+ }
1381
+
1382
+ OseoResult oseo_internal_install_map_global(
1383
+ OseoContext *context,
1384
+ OseoValue global
1385
+ ) {
1386
+ OseoValue slots[2] = {global, oseo_undefined()};
1387
+ OseoRootFrame frame = {NULL, slots, 2u};
1388
+ oseo_roots_push(context, &frame);
1389
+ OseoResult result = oseo_internal_map_intrinsic(context);
1390
+ slots[1] = result.value;
1391
+ if (result.status == OSEO_STATUS_NORMAL) {
1392
+ result = define_map_property(
1393
+ context,
1394
+ slots[0],
1395
+ "Map",
1396
+ slots[1],
1397
+ (OseoPropertyAttributes){true, false, true, false}
1398
+ );
1399
+ }
1400
+ oseo_roots_pop(context, &frame);
1401
+ return result.status == OSEO_STATUS_NORMAL ? normal(slots[0]) : result;
1402
+ }
1403
+
1404
+ /*
1405
+ * Dispatch.
1406
+ */
1407
+
1408
+ OseoResult oseo_internal_map_builtin_dispatch(
1409
+ OseoContext *context,
1410
+ size_t code_id,
1411
+ OseoValue callee,
1412
+ OseoValue receiver,
1413
+ size_t argument_count,
1414
+ const OseoValue *arguments,
1415
+ OseoValue new_target
1416
+ ) {
1417
+ (void)callee;
1418
+ if (code_id == OSEO_MAP_CONSTRUCTOR_CODE_ID) {
1419
+ /* 24.1.1.1 step 1: Map is not callable without new. */
1420
+ if (tag_of(new_target) == OSEO_TAG_UNDEFINED) {
1421
+ return oseo_internal_throw_error(
1422
+ context,
1423
+ OSEO_ERROR_TYPE,
1424
+ "The Map constructor requires new."
1425
+ );
1426
+ }
1427
+ return map_construct_with_target(
1428
+ context,
1429
+ new_target,
1430
+ argument_count > 0u ? arguments[0] : oseo_undefined()
1431
+ );
1432
+ }
1433
+ if (tag_of(new_target) != OSEO_TAG_UNDEFINED) {
1434
+ /* Every remaining built-in here is a method, accessor, or
1435
+ * static, and ECMA-262 gives none of them a [[Construct]]. */
1436
+ return oseo_internal_throw_error(
1437
+ context,
1438
+ OSEO_ERROR_TYPE,
1439
+ "The Map built-in function is not a constructor."
1440
+ );
1441
+ }
1442
+ if (code_id == OSEO_MAP_SPECIES_CODE_ID) {
1443
+ return normal(receiver);
1444
+ }
1445
+ if (code_id == OSEO_MAP_GET_CODE_ID) {
1446
+ return map_prototype_get(
1447
+ context,
1448
+ receiver,
1449
+ argument_count > 0u ? arguments[0] : oseo_undefined()
1450
+ );
1451
+ }
1452
+ if (code_id == OSEO_MAP_HAS_CODE_ID) {
1453
+ return map_prototype_has(
1454
+ context,
1455
+ receiver,
1456
+ argument_count > 0u ? arguments[0] : oseo_undefined()
1457
+ );
1458
+ }
1459
+ if (code_id == OSEO_MAP_SET_CODE_ID) {
1460
+ return map_prototype_set(
1461
+ context,
1462
+ receiver,
1463
+ argument_count > 0u ? arguments[0] : oseo_undefined(),
1464
+ argument_count > 1u ? arguments[1] : oseo_undefined()
1465
+ );
1466
+ }
1467
+ if (code_id == OSEO_MAP_GET_OR_INSERT_CODE_ID) {
1468
+ return map_prototype_get_or_insert(
1469
+ context,
1470
+ receiver,
1471
+ argument_count > 0u ? arguments[0] : oseo_undefined(),
1472
+ argument_count > 1u ? arguments[1] : oseo_undefined()
1473
+ );
1474
+ }
1475
+ if (code_id == OSEO_MAP_GET_OR_INSERT_COMPUTED_CODE_ID) {
1476
+ return map_prototype_get_or_insert_computed(
1477
+ context,
1478
+ receiver,
1479
+ argument_count > 0u ? arguments[0] : oseo_undefined(),
1480
+ argument_count > 1u ? arguments[1] : oseo_undefined()
1481
+ );
1482
+ }
1483
+ if (code_id == OSEO_MAP_DELETE_CODE_ID) {
1484
+ return map_prototype_delete(
1485
+ context,
1486
+ receiver,
1487
+ argument_count > 0u ? arguments[0] : oseo_undefined()
1488
+ );
1489
+ }
1490
+ if (code_id == OSEO_MAP_CLEAR_CODE_ID) {
1491
+ return map_prototype_clear(context, receiver);
1492
+ }
1493
+ if (code_id == OSEO_MAP_SIZE_GETTER_CODE_ID) {
1494
+ return map_prototype_size_getter(context, receiver);
1495
+ }
1496
+ if (code_id == OSEO_MAP_FOR_EACH_CODE_ID) {
1497
+ return map_prototype_for_each(
1498
+ context,
1499
+ receiver,
1500
+ argument_count > 0u ? arguments[0] : oseo_undefined(),
1501
+ argument_count > 1u ? arguments[1] : oseo_undefined()
1502
+ );
1503
+ }
1504
+ if (code_id == OSEO_MAP_KEYS_CODE_ID) {
1505
+ return map_create_iterator(
1506
+ context,
1507
+ receiver,
1508
+ OSEO_MAP_ITERATION_KEY,
1509
+ "Method Map.prototype.keys called on incompatible receiver."
1510
+ );
1511
+ }
1512
+ if (code_id == OSEO_MAP_VALUES_CODE_ID) {
1513
+ return map_create_iterator(
1514
+ context,
1515
+ receiver,
1516
+ OSEO_MAP_ITERATION_VALUE,
1517
+ "Method Map.prototype.values called on incompatible receiver."
1518
+ );
1519
+ }
1520
+ if (code_id == OSEO_MAP_ENTRIES_CODE_ID) {
1521
+ return map_create_iterator(
1522
+ context,
1523
+ receiver,
1524
+ OSEO_MAP_ITERATION_KEY_VALUE,
1525
+ "Method Map.prototype.entries called on incompatible receiver."
1526
+ );
1527
+ }
1528
+ if (code_id == OSEO_MAP_GROUP_BY_CODE_ID) {
1529
+ return map_group_by(
1530
+ context,
1531
+ argument_count > 0u ? arguments[0] : oseo_undefined(),
1532
+ argument_count > 1u ? arguments[1] : oseo_undefined()
1533
+ );
1534
+ }
1535
+ if (code_id == OSEO_MAP_ITERATOR_NEXT_CODE_ID) {
1536
+ return map_iterator_next(context, receiver);
1537
+ }
1538
+ return oseo_unknown_function(context, code_id);
1539
+ }