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