@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,1299 @@
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
+ * WeakMap, WeakSet, WeakRef, and FinalizationRegistry. Each source-visible
35
+ * object wraps one collector-only record from runtime_memory.c, so the
36
+ * ephemeron fixed point, weak-target clearing, and deterministic
37
+ * finalization scheduling stay owned by the collector. This component owns
38
+ * the constructors and prototype methods, CanBeHeldWeakly, the job-scoped
39
+ * KeptAlive set, and the cleanup job step the event loop schedules.
40
+ */
41
+
42
+ static void initialize_ordinary(
43
+ OseoContext *context,
44
+ OseoOrdinaryObject *object,
45
+ OseoValue prototype
46
+ ) {
47
+ object->prototype = prototype;
48
+ object->properties = NULL;
49
+ object->property_capacity = 0u;
50
+ object->property_count = 0u;
51
+ oseo_internal_init_dense_index(object);
52
+ object->private_elements = NULL;
53
+ object->private_element_capacity = 0u;
54
+ object->private_element_count = 0u;
55
+ object->shape_id = context->next_shape_id;
56
+ context->next_shape_id += 1u;
57
+ object->array_length = 0u;
58
+ object->dictionary = false;
59
+ object->length_writable = false;
60
+ object->extensible = true;
61
+ object->module_namespace = false;
62
+ object->immutable_prototype = false;
63
+ object->global_object = false;
64
+ object->error_data = false;
65
+ object->number_data = false;
66
+ object->number_value = oseo_undefined();
67
+ object->primitive_data = false;
68
+ object->primitive_value = oseo_undefined();
69
+ object->primitive_wrapper_methods_initialized = false;
70
+ object->virtual_string_iterator = false;
71
+ object->virtual_string_iterator_configurable = false;
72
+ object->virtual_string_iterator_enumerable = false;
73
+ object->virtual_string_iterator_writable = false;
74
+ object->iterator_kind = OSEO_ITERATOR_NONE;
75
+ object->iterator_target = oseo_undefined();
76
+ object->iterator_index = 0u;
77
+ object->regexp_string_iterator = false;
78
+ object->regexp_iterator_regexp = oseo_undefined();
79
+ object->regexp_iterator_subject = oseo_undefined();
80
+ object->regexp_iterator_global = false;
81
+ object->regexp_iterator_unicode = false;
82
+ object->regexp_iterator_complete = false;
83
+ object->async_from_sync = false;
84
+ object->async_sync_iterator = oseo_undefined();
85
+ object->wrap_for_valid_iterator = false;
86
+ object->wrapped_iterator = oseo_undefined();
87
+ object->wrapped_next = oseo_undefined();
88
+ object->arguments_object = false;
89
+ object->mapped_arguments = false;
90
+ object->generator = NULL;
91
+ }
92
+
93
+ /*
94
+ * CanBeHeldWeakly (9.13). A registered Symbol is excluded because its key
95
+ * keeps it observable for the agent's lifetime, so only an unregistered
96
+ * Symbol, whose representative has no registry entry, may be held weakly.
97
+ */
98
+ static bool can_be_held_weakly(OseoValue value) {
99
+ if (is_object(value)) return true;
100
+ return is_symbol(value) && symbol_object(value)->registry_entry == NULL;
101
+ }
102
+
103
+ static OseoValue argument_at(
104
+ size_t argument_count,
105
+ const OseoValue *arguments,
106
+ size_t index
107
+ ) {
108
+ return argument_count > index ? arguments[index] : oseo_undefined();
109
+ }
110
+
111
+ /* OrdinaryCreateFromConstructor's prototype selection for one intrinsic. */
112
+ static OseoResult prototype_from_target(
113
+ OseoContext *context,
114
+ OseoValue new_target,
115
+ OseoIntrinsic fallback,
116
+ OseoValue *prototype
117
+ ) {
118
+ OseoRootFrame frame = {NULL, NULL, 0u};
119
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
120
+ if (result.status != OSEO_STATUS_NORMAL) return result;
121
+ frame.slots[0] = new_target;
122
+ result = oseo_internal_constructor_prototype(context, frame.slots[0]);
123
+ frame.slots[1] = result.value;
124
+ if (result.status == OSEO_STATUS_NORMAL && !is_object(frame.slots[1])) {
125
+ result = oseo_internal_constructor_realm_default(
126
+ context,
127
+ frame.slots[0],
128
+ fallback
129
+ );
130
+ frame.slots[1] = result.value;
131
+ }
132
+ if (result.status == OSEO_STATUS_NORMAL) *prototype = frame.slots[1];
133
+ oseo_roots_release(context, &frame);
134
+ return result;
135
+ }
136
+
137
+ /*
138
+ * Allocates one wrapper of `kind` around the collector record rooted in
139
+ * `slots[1]`, with its prototype rooted in `slots[0]`.
140
+ */
141
+ static OseoResult wrapper_allocate(
142
+ OseoContext *context,
143
+ OseoValue *slots,
144
+ OseoHeapKind kind
145
+ ) {
146
+ OseoOrdinaryObject *ordinary = NULL;
147
+ if (kind == OSEO_HEAP_WEAK_REF) {
148
+ OseoWeakRef *reference =
149
+ oseo_internal_allocate_heap_bytes(context, sizeof(*reference));
150
+ if (reference != NULL) {
151
+ reference->reference = slots[1];
152
+ ordinary = &reference->ordinary;
153
+ }
154
+ } else if (kind == OSEO_HEAP_FINALIZATION_REGISTRY_OBJECT) {
155
+ OseoFinalizationRegistryObject *registry =
156
+ oseo_internal_allocate_heap_bytes(context, sizeof(*registry));
157
+ if (registry != NULL) {
158
+ registry->registry = slots[1];
159
+ ordinary = &registry->ordinary;
160
+ }
161
+ } else {
162
+ OseoWeakCollection *collection =
163
+ oseo_internal_allocate_heap_bytes(context, sizeof(*collection));
164
+ if (collection != NULL) {
165
+ collection->table = slots[1];
166
+ ordinary = &collection->ordinary;
167
+ }
168
+ }
169
+ if (ordinary == NULL) {
170
+ return failure(context, "OSEO2001", "Weak object allocation failed.");
171
+ }
172
+ initialize_ordinary(context, ordinary, slots[0]);
173
+ return oseo_internal_publish_heap(context, &ordinary->header, kind);
174
+ }
175
+
176
+ /* IfAbruptCloseIterator for a catchable completion. */
177
+ static OseoResult close_after_abrupt(
178
+ OseoContext *context,
179
+ OseoValue iterator,
180
+ OseoResult abrupt
181
+ ) {
182
+ if (abrupt.status != OSEO_STATUS_THROW || context->has_diagnostic) {
183
+ return abrupt;
184
+ }
185
+ OseoValue slots[2] = {iterator, abrupt.value};
186
+ OseoRootFrame frame = {NULL, slots, 2u};
187
+ oseo_roots_push(context, &frame);
188
+ oseo_context_clear_language_error(context);
189
+ OseoResult closed = oseo_iterator_close(context, slots[0], true);
190
+ OseoResult result = closed.status == OSEO_STATUS_NORMAL
191
+ ? (OseoResult){OSEO_STATUS_THROW, slots[1]}
192
+ : closed;
193
+ oseo_roots_pop(context, &frame);
194
+ return result;
195
+ }
196
+
197
+ /*
198
+ * The WeakMap and WeakSet constructors. A WeakMap reads its observable
199
+ * `set` adder and adds [key, value] entries through AddEntriesFromIterable;
200
+ * a WeakSet reads `add` and passes each value. Both obtain the adder before
201
+ * acquiring the iterator and close it only after a completed step.
202
+ */
203
+ static OseoResult weak_collection_construct(
204
+ OseoContext *context,
205
+ bool map,
206
+ OseoValue iterable,
207
+ OseoValue new_target
208
+ ) {
209
+ if (tag_of(new_target) == OSEO_TAG_UNDEFINED) {
210
+ return oseo_internal_throw_error(
211
+ context,
212
+ OSEO_ERROR_TYPE,
213
+ map
214
+ ? "WeakMap must be called with new."
215
+ : "WeakSet must be called with new."
216
+ );
217
+ }
218
+ /* 0 prototype then instance, 1 table, 2 iterable, 3 adder, 4 iterator,
219
+ * 5 next method, 6 item, 7 key, 8 value, 9 "0", 10 "1". */
220
+ OseoRootFrame frame = {NULL, NULL, 0u};
221
+ OseoResult result = oseo_roots_allocate(context, &frame, 11u);
222
+ if (result.status != OSEO_STATUS_NORMAL) return result;
223
+ frame.slots[2] = iterable;
224
+ frame.slots[4] = new_target;
225
+ result = prototype_from_target(
226
+ context,
227
+ frame.slots[4],
228
+ map
229
+ ? OSEO_INTRINSIC_WEAK_MAP_PROTOTYPE
230
+ : OSEO_INTRINSIC_WEAK_SET_PROTOTYPE,
231
+ &frame.slots[0]
232
+ );
233
+ frame.slots[4] = oseo_undefined();
234
+ if (result.status == OSEO_STATUS_NORMAL) {
235
+ result = oseo_internal_ephemeron_table_create(context);
236
+ frame.slots[1] = result.value;
237
+ }
238
+ if (result.status == OSEO_STATUS_NORMAL) {
239
+ result = wrapper_allocate(
240
+ context,
241
+ frame.slots,
242
+ map ? OSEO_HEAP_WEAK_MAP : OSEO_HEAP_WEAK_SET
243
+ );
244
+ frame.slots[0] = result.value;
245
+ }
246
+ if (result.status != OSEO_STATUS_NORMAL || is_nullish(frame.slots[2])) {
247
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[0];
248
+ oseo_roots_release(context, &frame);
249
+ return result;
250
+ }
251
+ result = oseo_internal_ascii_string(context, map ? "set" : "add");
252
+ frame.slots[3] = result.value;
253
+ if (result.status == OSEO_STATUS_NORMAL) {
254
+ result = oseo_object_get(context, frame.slots[0], frame.slots[3]);
255
+ frame.slots[3] = result.value;
256
+ }
257
+ if (result.status == OSEO_STATUS_NORMAL &&
258
+ !is_callable(frame.slots[3])) {
259
+ result = oseo_internal_throw_error(
260
+ context,
261
+ OSEO_ERROR_TYPE,
262
+ map
263
+ ? "The WeakMap set property is not callable."
264
+ : "The WeakSet add property is not callable."
265
+ );
266
+ }
267
+ if (result.status == OSEO_STATUS_NORMAL) {
268
+ result = oseo_iterator_get(context, frame.slots[2], &frame.slots[5]);
269
+ frame.slots[4] = result.value;
270
+ }
271
+ static const uint16_t zero_units[] = {'0'};
272
+ static const uint16_t one_units[] = {'1'};
273
+ if (map && result.status == OSEO_STATUS_NORMAL) {
274
+ result = oseo_string_from_units(context, zero_units, 1u);
275
+ frame.slots[9] = result.value;
276
+ }
277
+ if (map && result.status == OSEO_STATUS_NORMAL) {
278
+ result = oseo_string_from_units(context, one_units, 1u);
279
+ frame.slots[10] = result.value;
280
+ }
281
+ while (result.status == OSEO_STATUS_NORMAL) {
282
+ bool done = false;
283
+ /* A throw from IteratorStepValue leaves the record done, so it
284
+ * propagates without IteratorClose. */
285
+ result = oseo_iterator_next(
286
+ context,
287
+ frame.slots[4],
288
+ frame.slots[5],
289
+ &frame.slots[6],
290
+ &done
291
+ );
292
+ if (result.status != OSEO_STATUS_NORMAL || done) break;
293
+ if (!map) {
294
+ result = oseo_call_function(
295
+ context,
296
+ frame.slots[3],
297
+ frame.slots[0],
298
+ 1u,
299
+ &frame.slots[6],
300
+ oseo_undefined()
301
+ );
302
+ } else if (!is_object(frame.slots[6])) {
303
+ result = oseo_internal_throw_error(
304
+ context,
305
+ OSEO_ERROR_TYPE,
306
+ "A WeakMap constructor iterable entry must be an object."
307
+ );
308
+ } else {
309
+ result = oseo_object_get(context, frame.slots[6], frame.slots[9]);
310
+ frame.slots[7] = result.value;
311
+ if (result.status == OSEO_STATUS_NORMAL) {
312
+ result = oseo_object_get(
313
+ context,
314
+ frame.slots[6],
315
+ frame.slots[10]
316
+ );
317
+ frame.slots[8] = result.value;
318
+ }
319
+ if (result.status == OSEO_STATUS_NORMAL) {
320
+ result = oseo_call_function(
321
+ context,
322
+ frame.slots[3],
323
+ frame.slots[0],
324
+ 2u,
325
+ &frame.slots[7],
326
+ oseo_undefined()
327
+ );
328
+ }
329
+ }
330
+ if (result.status != OSEO_STATUS_NORMAL) {
331
+ result = close_after_abrupt(context, frame.slots[4], result);
332
+ break;
333
+ }
334
+ }
335
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[0];
336
+ oseo_roots_release(context, &frame);
337
+ return result;
338
+ }
339
+
340
+ static OseoResult weak_collection_receiver(
341
+ OseoContext *context,
342
+ OseoValue receiver,
343
+ bool map,
344
+ const char *message
345
+ ) {
346
+ if (map ? is_weak_map(receiver) : is_weak_set(receiver)) {
347
+ return normal(receiver);
348
+ }
349
+ return oseo_internal_throw_error(context, OSEO_ERROR_TYPE, message);
350
+ }
351
+
352
+ static OseoResult weak_map_delete(
353
+ OseoContext *context,
354
+ OseoValue receiver,
355
+ OseoValue key
356
+ ) {
357
+ OseoResult checked = weak_collection_receiver(
358
+ context,
359
+ receiver,
360
+ true,
361
+ "WeakMap.prototype.delete receiver is not a WeakMap."
362
+ );
363
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
364
+ if (!can_be_held_weakly(key)) return normal(oseo_boolean(false));
365
+ return normal(oseo_boolean(oseo_internal_ephemeron_delete(
366
+ weak_collection_object(receiver)->table,
367
+ key
368
+ )));
369
+ }
370
+
371
+ static OseoResult weak_map_get(
372
+ OseoContext *context,
373
+ OseoValue receiver,
374
+ OseoValue key
375
+ ) {
376
+ OseoResult checked = weak_collection_receiver(
377
+ context,
378
+ receiver,
379
+ true,
380
+ "WeakMap.prototype.get receiver is not a WeakMap."
381
+ );
382
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
383
+ OseoValue value = oseo_undefined();
384
+ if (can_be_held_weakly(key)) {
385
+ (void)oseo_internal_ephemeron_get(
386
+ weak_collection_object(receiver)->table,
387
+ key,
388
+ &value
389
+ );
390
+ }
391
+ return normal(value);
392
+ }
393
+
394
+ static OseoResult weak_collection_has(
395
+ OseoContext *context,
396
+ OseoValue receiver,
397
+ OseoValue key,
398
+ bool map
399
+ ) {
400
+ OseoResult checked = weak_collection_receiver(
401
+ context,
402
+ receiver,
403
+ map,
404
+ map
405
+ ? "WeakMap.prototype.has receiver is not a WeakMap."
406
+ : "WeakSet.prototype.has receiver is not a WeakSet."
407
+ );
408
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
409
+ OseoValue value = oseo_undefined();
410
+ bool present = can_be_held_weakly(key) &&
411
+ oseo_internal_ephemeron_get(
412
+ weak_collection_object(receiver)->table,
413
+ key,
414
+ &value
415
+ );
416
+ return normal(oseo_boolean(present));
417
+ }
418
+
419
+ /* WeakMap.prototype.set and WeakSet.prototype.add. Both return the
420
+ * receiver; a WeakSet member maps to undefined so it traces nothing. */
421
+ static OseoResult weak_collection_store(
422
+ OseoContext *context,
423
+ OseoValue receiver,
424
+ OseoValue key,
425
+ OseoValue value,
426
+ bool map
427
+ ) {
428
+ OseoResult checked = weak_collection_receiver(
429
+ context,
430
+ receiver,
431
+ map,
432
+ map
433
+ ? "WeakMap.prototype.set receiver is not a WeakMap."
434
+ : "WeakSet.prototype.add receiver is not a WeakSet."
435
+ );
436
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
437
+ if (!can_be_held_weakly(key)) {
438
+ return oseo_internal_throw_error(
439
+ context,
440
+ OSEO_ERROR_TYPE,
441
+ map
442
+ ? "Invalid value used as a WeakMap key."
443
+ : "Invalid value used in a WeakSet."
444
+ );
445
+ }
446
+ OseoValue slots[1] = {receiver};
447
+ OseoRootFrame frame = {NULL, slots, 1u};
448
+ oseo_roots_push(context, &frame);
449
+ OseoResult result = oseo_internal_ephemeron_set(
450
+ context,
451
+ weak_collection_object(slots[0])->table,
452
+ key,
453
+ map ? value : oseo_undefined()
454
+ );
455
+ oseo_roots_pop(context, &frame);
456
+ return result.status == OSEO_STATUS_NORMAL ? normal(slots[0]) : result;
457
+ }
458
+
459
+ /*
460
+ * WeakMap.prototype.getOrInsert and getOrInsertComputed, ECMAScript
461
+ * 2026 24.3.3.3 and 24.3.3.4. Both reject a key that cannot be held
462
+ * weakly, so a registered symbol or a primitive never reaches the
463
+ * ephemeron table. The computed form checks callability next, runs the
464
+ * callback with undefined as its receiver only when the key is absent,
465
+ * and then stores the result even when the callback inserted the key
466
+ * itself, which is what the table's replacing set does.
467
+ */
468
+ static OseoResult weak_map_get_or_insert(
469
+ OseoContext *context,
470
+ OseoValue receiver,
471
+ OseoValue key,
472
+ OseoValue operand,
473
+ bool computed
474
+ ) {
475
+ OseoResult checked = weak_collection_receiver(
476
+ context,
477
+ receiver,
478
+ true,
479
+ computed
480
+ ? "WeakMap.prototype.getOrInsertComputed receiver is not a "
481
+ "WeakMap."
482
+ : "WeakMap.prototype.getOrInsert receiver is not a WeakMap."
483
+ );
484
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
485
+ if (!can_be_held_weakly(key)) {
486
+ return oseo_internal_throw_error(
487
+ context,
488
+ OSEO_ERROR_TYPE,
489
+ "Invalid value used as a WeakMap key."
490
+ );
491
+ }
492
+ if (computed && !is_callable(operand)) {
493
+ return oseo_internal_throw_error(
494
+ context,
495
+ OSEO_ERROR_TYPE,
496
+ "WeakMap.prototype.getOrInsertComputed callback is not "
497
+ "callable."
498
+ );
499
+ }
500
+ OseoValue existing = oseo_undefined();
501
+ if (oseo_internal_ephemeron_get(
502
+ weak_collection_object(receiver)->table,
503
+ key,
504
+ &existing
505
+ )) {
506
+ return normal(existing);
507
+ }
508
+ /* 0 map, 1 callback or value, 2 key (the one call argument), 3 value. */
509
+ OseoValue slots[4] = {receiver, operand, key, operand};
510
+ OseoRootFrame frame = {NULL, slots, 4u};
511
+ oseo_roots_push(context, &frame);
512
+ OseoResult result = normal(oseo_undefined());
513
+ if (computed) {
514
+ result = oseo_call_function(
515
+ context,
516
+ slots[1],
517
+ oseo_undefined(),
518
+ 1u,
519
+ &slots[2],
520
+ oseo_undefined()
521
+ );
522
+ slots[3] = result.value;
523
+ }
524
+ if (result.status == OSEO_STATUS_NORMAL) {
525
+ result = oseo_internal_ephemeron_set(
526
+ context,
527
+ weak_collection_object(slots[0])->table,
528
+ slots[2],
529
+ slots[3]
530
+ );
531
+ }
532
+ if (result.status == OSEO_STATUS_NORMAL) result = normal(slots[3]);
533
+ oseo_roots_pop(context, &frame);
534
+ return result;
535
+ }
536
+
537
+ static OseoResult weak_set_delete(
538
+ OseoContext *context,
539
+ OseoValue receiver,
540
+ OseoValue value
541
+ ) {
542
+ OseoResult checked = weak_collection_receiver(
543
+ context,
544
+ receiver,
545
+ false,
546
+ "WeakSet.prototype.delete receiver is not a WeakSet."
547
+ );
548
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
549
+ if (!can_be_held_weakly(value)) return normal(oseo_boolean(false));
550
+ return normal(oseo_boolean(oseo_internal_ephemeron_delete(
551
+ weak_collection_object(receiver)->table,
552
+ value
553
+ )));
554
+ }
555
+
556
+ /* KeptAlive uses the same address-keyed probing as ephemeron lookup. */
557
+ static size_t kept_slot_start(OseoValue value, size_t capacity) {
558
+ uint64_t hash = ((value & OSEO_PAYLOAD_MASK) >> 3u) *
559
+ UINT64_C(0x9e3779b97f4a7c15);
560
+ return (size_t)(hash >> 20u) & (capacity - 1u);
561
+ }
562
+
563
+ OseoResult oseo_internal_keep_during_job(
564
+ OseoContext *context,
565
+ OseoValue value
566
+ ) {
567
+ if (tag_of(value) != OSEO_TAG_HEAP) return normal(oseo_undefined());
568
+ OseoValue *kept = context->kept_objects;
569
+ size_t capacity = context->kept_object_capacity;
570
+ if (capacity != 0u) {
571
+ size_t slot = kept_slot_start(value, capacity);
572
+ while (kept[slot] != 0u) {
573
+ if (kept[slot] == value) return normal(oseo_undefined());
574
+ slot = (slot + 1u) & (capacity - 1u);
575
+ }
576
+ }
577
+ if ((context->kept_object_count + 1u) > capacity / 4u * 3u) {
578
+ size_t grown = capacity == 0u ? 16u : capacity;
579
+ while (grown / 2u < context->kept_object_count + 1u) {
580
+ if (grown > SIZE_MAX / 2u / sizeof(OseoValue)) {
581
+ return failure(
582
+ context,
583
+ "OSEO2001",
584
+ "The kept-object set is too large."
585
+ );
586
+ }
587
+ grown *= 2u;
588
+ }
589
+ OseoValue *slots =
590
+ oseo_internal_allocate_work_bytes(context, grown * sizeof(*slots));
591
+ if (slots == NULL) {
592
+ return failure(
593
+ context,
594
+ "OSEO2001",
595
+ "Kept-object allocation failed."
596
+ );
597
+ }
598
+ for (size_t index = 0u; index < grown; index += 1u) slots[index] = 0u;
599
+ for (size_t index = 0u; index < capacity; index += 1u) {
600
+ if (kept[index] == 0u) continue;
601
+ size_t slot = kept_slot_start(kept[index], grown);
602
+ while (slots[slot] != 0u) slot = (slot + 1u) & (grown - 1u);
603
+ slots[slot] = kept[index];
604
+ }
605
+ free(kept);
606
+ context->kept_objects = slots;
607
+ context->kept_object_capacity = grown;
608
+ kept = slots;
609
+ capacity = grown;
610
+ }
611
+ size_t slot = kept_slot_start(value, capacity);
612
+ while (kept[slot] != 0u) slot = (slot + 1u) & (capacity - 1u);
613
+ kept[slot] = value;
614
+ context->kept_object_count += 1u;
615
+ return normal(oseo_undefined());
616
+ }
617
+
618
+ void oseo_internal_clear_kept_objects(OseoContext *context) {
619
+ if (context->kept_object_count == 0u) return;
620
+ /* A large set from one job is released rather than rescanned by every
621
+ * later job's clear. */
622
+ if (context->kept_object_capacity > 256u) {
623
+ free(context->kept_objects);
624
+ context->kept_objects = NULL;
625
+ context->kept_object_capacity = 0u;
626
+ } else {
627
+ OseoValue *kept = context->kept_objects;
628
+ for (size_t index = 0u;
629
+ index < context->kept_object_capacity;
630
+ index += 1u) {
631
+ kept[index] = 0u;
632
+ }
633
+ }
634
+ context->kept_object_count = 0u;
635
+ }
636
+
637
+ static OseoResult weak_ref_construct(
638
+ OseoContext *context,
639
+ OseoValue target,
640
+ OseoValue new_target
641
+ ) {
642
+ if (tag_of(new_target) == OSEO_TAG_UNDEFINED) {
643
+ return oseo_internal_throw_error(
644
+ context,
645
+ OSEO_ERROR_TYPE,
646
+ "WeakRef must be called with new."
647
+ );
648
+ }
649
+ if (!can_be_held_weakly(target)) {
650
+ return oseo_internal_throw_error(
651
+ context,
652
+ OSEO_ERROR_TYPE,
653
+ "WeakRef target cannot be held weakly."
654
+ );
655
+ }
656
+ /* 0 prototype then instance, 1 weak reference, 2 target. */
657
+ OseoRootFrame frame = {NULL, NULL, 0u};
658
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
659
+ if (result.status != OSEO_STATUS_NORMAL) return result;
660
+ frame.slots[1] = new_target;
661
+ frame.slots[2] = target;
662
+ result = prototype_from_target(
663
+ context,
664
+ frame.slots[1],
665
+ OSEO_INTRINSIC_WEAK_REF_PROTOTYPE,
666
+ &frame.slots[0]
667
+ );
668
+ if (result.status == OSEO_STATUS_NORMAL) {
669
+ result = oseo_internal_weak_reference_create(context, frame.slots[2]);
670
+ frame.slots[1] = result.value;
671
+ }
672
+ if (result.status == OSEO_STATUS_NORMAL) {
673
+ result = wrapper_allocate(context, frame.slots, OSEO_HEAP_WEAK_REF);
674
+ frame.slots[0] = result.value;
675
+ }
676
+ if (result.status == OSEO_STATUS_NORMAL) {
677
+ result = oseo_internal_keep_during_job(context, frame.slots[2]);
678
+ }
679
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[0];
680
+ oseo_roots_release(context, &frame);
681
+ return result;
682
+ }
683
+
684
+ static OseoResult weak_ref_deref(OseoContext *context, OseoValue receiver) {
685
+ if (!is_weak_ref(receiver)) {
686
+ return oseo_internal_throw_error(
687
+ context,
688
+ OSEO_ERROR_TYPE,
689
+ "WeakRef.prototype.deref receiver is not a WeakRef."
690
+ );
691
+ }
692
+ OseoValue target = oseo_internal_weak_reference_target(
693
+ weak_ref_object(receiver)->reference
694
+ );
695
+ if (tag_of(target) != OSEO_TAG_HEAP) return normal(oseo_undefined());
696
+ OseoResult kept = oseo_internal_keep_during_job(context, target);
697
+ return kept.status == OSEO_STATUS_NORMAL ? normal(target) : kept;
698
+ }
699
+
700
+ static OseoResult finalization_registry_construct(
701
+ OseoContext *context,
702
+ OseoValue callback,
703
+ OseoValue new_target
704
+ ) {
705
+ if (tag_of(new_target) == OSEO_TAG_UNDEFINED) {
706
+ return oseo_internal_throw_error(
707
+ context,
708
+ OSEO_ERROR_TYPE,
709
+ "FinalizationRegistry must be called with new."
710
+ );
711
+ }
712
+ if (!is_callable(callback)) {
713
+ return oseo_internal_throw_error(
714
+ context,
715
+ OSEO_ERROR_TYPE,
716
+ "FinalizationRegistry cleanup callback is not callable."
717
+ );
718
+ }
719
+ /* 0 prototype then instance, 1 registry record, 2 callback. */
720
+ OseoRootFrame frame = {NULL, NULL, 0u};
721
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
722
+ if (result.status != OSEO_STATUS_NORMAL) return result;
723
+ frame.slots[1] = new_target;
724
+ frame.slots[2] = callback;
725
+ result = prototype_from_target(
726
+ context,
727
+ frame.slots[1],
728
+ OSEO_INTRINSIC_FINALIZATION_REGISTRY_PROTOTYPE,
729
+ &frame.slots[0]
730
+ );
731
+ if (result.status == OSEO_STATUS_NORMAL) {
732
+ result = oseo_internal_finalization_registry_create(
733
+ context,
734
+ frame.slots[2]
735
+ );
736
+ frame.slots[1] = result.value;
737
+ }
738
+ if (result.status == OSEO_STATUS_NORMAL) {
739
+ result = wrapper_allocate(
740
+ context,
741
+ frame.slots,
742
+ OSEO_HEAP_FINALIZATION_REGISTRY_OBJECT
743
+ );
744
+ }
745
+ oseo_roots_release(context, &frame);
746
+ return result;
747
+ }
748
+
749
+ static OseoResult finalization_registry_receiver(
750
+ OseoContext *context,
751
+ OseoValue receiver,
752
+ const char *message
753
+ ) {
754
+ if (is_finalization_registry_object(receiver)) return normal(receiver);
755
+ return oseo_internal_throw_error(context, OSEO_ERROR_TYPE, message);
756
+ }
757
+
758
+ static OseoResult finalization_registry_register(
759
+ OseoContext *context,
760
+ OseoValue receiver,
761
+ size_t argument_count,
762
+ const OseoValue *arguments
763
+ ) {
764
+ OseoResult checked = finalization_registry_receiver(
765
+ context,
766
+ receiver,
767
+ "FinalizationRegistry.prototype.register receiver is not a "
768
+ "FinalizationRegistry."
769
+ );
770
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
771
+ OseoValue target = argument_at(argument_count, arguments, 0u);
772
+ OseoValue holdings = argument_at(argument_count, arguments, 1u);
773
+ OseoValue token = argument_at(argument_count, arguments, 2u);
774
+ if (!can_be_held_weakly(target)) {
775
+ return oseo_internal_throw_error(
776
+ context,
777
+ OSEO_ERROR_TYPE,
778
+ "FinalizationRegistry target cannot be held weakly."
779
+ );
780
+ }
781
+ /* SameValue on a value that can be held weakly is identity. */
782
+ if (target == holdings) {
783
+ return oseo_internal_throw_error(
784
+ context,
785
+ OSEO_ERROR_TYPE,
786
+ "FinalizationRegistry target and held value must differ."
787
+ );
788
+ }
789
+ if (!can_be_held_weakly(token)) {
790
+ if (tag_of(token) != OSEO_TAG_UNDEFINED) {
791
+ return oseo_internal_throw_error(
792
+ context,
793
+ OSEO_ERROR_TYPE,
794
+ "FinalizationRegistry unregister token cannot be held weakly."
795
+ );
796
+ }
797
+ token = oseo_undefined();
798
+ }
799
+ OseoResult result = oseo_internal_finalization_register_token(
800
+ context,
801
+ finalization_registry_wrapper_object(receiver)->registry,
802
+ target,
803
+ holdings,
804
+ token
805
+ );
806
+ return result.status == OSEO_STATUS_NORMAL
807
+ ? normal(oseo_undefined())
808
+ : result;
809
+ }
810
+
811
+ static OseoResult finalization_registry_unregister(
812
+ OseoContext *context,
813
+ OseoValue receiver,
814
+ OseoValue token
815
+ ) {
816
+ OseoResult checked = finalization_registry_receiver(
817
+ context,
818
+ receiver,
819
+ "FinalizationRegistry.prototype.unregister receiver is not a "
820
+ "FinalizationRegistry."
821
+ );
822
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
823
+ if (!can_be_held_weakly(token)) {
824
+ return oseo_internal_throw_error(
825
+ context,
826
+ OSEO_ERROR_TYPE,
827
+ "FinalizationRegistry unregister token cannot be held weakly."
828
+ );
829
+ }
830
+ return normal(oseo_boolean(oseo_internal_finalization_unregister(
831
+ context,
832
+ finalization_registry_wrapper_object(receiver)->registry,
833
+ token
834
+ )));
835
+ }
836
+
837
+ OseoResult oseo_internal_finalization_cleanup_job(
838
+ OseoContext *context,
839
+ bool *ran
840
+ ) {
841
+ OseoValue slots[3] = {oseo_undefined(), oseo_undefined(), oseo_undefined()};
842
+ OseoRootFrame frame = {NULL, slots, 3u};
843
+ oseo_roots_push(context, &frame);
844
+ *ran = oseo_internal_finalization_take_cleanup(
845
+ context,
846
+ &slots[0],
847
+ &slots[2]
848
+ );
849
+ OseoResult result = normal(oseo_undefined());
850
+ if (*ran) {
851
+ slots[1] = finalization_registry_object(slots[0])->callback;
852
+ do {
853
+ result = oseo_call_function(
854
+ context,
855
+ slots[1],
856
+ oseo_undefined(),
857
+ 1u,
858
+ &slots[2],
859
+ oseo_undefined()
860
+ );
861
+ slots[2] = oseo_undefined();
862
+ } while (result.status == OSEO_STATUS_NORMAL &&
863
+ oseo_internal_finalization_take_registry_cleanup(
864
+ context,
865
+ slots[0],
866
+ &slots[2]
867
+ ));
868
+ }
869
+ oseo_roots_pop(context, &frame);
870
+ return result;
871
+ }
872
+
873
+ OseoResult oseo_internal_weak_collection_builtin_dispatch(
874
+ OseoContext *context,
875
+ size_t code_id,
876
+ OseoValue callee,
877
+ OseoValue receiver,
878
+ size_t argument_count,
879
+ const OseoValue *arguments,
880
+ OseoValue new_target
881
+ ) {
882
+ (void)callee;
883
+ OseoValue first = argument_at(argument_count, arguments, 0u);
884
+ if (code_id == OSEO_WEAK_MAP_CONSTRUCTOR_CODE_ID) {
885
+ return weak_collection_construct(context, true, first, new_target);
886
+ }
887
+ if (code_id == OSEO_WEAK_MAP_DELETE_CODE_ID) {
888
+ return weak_map_delete(context, receiver, first);
889
+ }
890
+ if (code_id == OSEO_WEAK_MAP_GET_CODE_ID) {
891
+ return weak_map_get(context, receiver, first);
892
+ }
893
+ if (code_id == OSEO_WEAK_MAP_HAS_CODE_ID) {
894
+ return weak_collection_has(context, receiver, first, true);
895
+ }
896
+ if (code_id == OSEO_WEAK_MAP_SET_CODE_ID) {
897
+ return weak_collection_store(
898
+ context,
899
+ receiver,
900
+ first,
901
+ argument_at(argument_count, arguments, 1u),
902
+ true
903
+ );
904
+ }
905
+ if (code_id == OSEO_WEAK_MAP_GET_OR_INSERT_CODE_ID ||
906
+ code_id == OSEO_WEAK_MAP_GET_OR_INSERT_COMPUTED_CODE_ID) {
907
+ return weak_map_get_or_insert(
908
+ context,
909
+ receiver,
910
+ first,
911
+ argument_at(argument_count, arguments, 1u),
912
+ code_id == OSEO_WEAK_MAP_GET_OR_INSERT_COMPUTED_CODE_ID
913
+ );
914
+ }
915
+ if (code_id == OSEO_WEAK_SET_CONSTRUCTOR_CODE_ID) {
916
+ return weak_collection_construct(context, false, first, new_target);
917
+ }
918
+ if (code_id == OSEO_WEAK_SET_ADD_CODE_ID) {
919
+ return weak_collection_store(
920
+ context,
921
+ receiver,
922
+ first,
923
+ oseo_undefined(),
924
+ false
925
+ );
926
+ }
927
+ if (code_id == OSEO_WEAK_SET_DELETE_CODE_ID) {
928
+ return weak_set_delete(context, receiver, first);
929
+ }
930
+ if (code_id == OSEO_WEAK_SET_HAS_CODE_ID) {
931
+ return weak_collection_has(context, receiver, first, false);
932
+ }
933
+ if (code_id == OSEO_WEAK_REF_CONSTRUCTOR_CODE_ID) {
934
+ return weak_ref_construct(context, first, new_target);
935
+ }
936
+ if (code_id == OSEO_WEAK_REF_DEREF_CODE_ID) {
937
+ return weak_ref_deref(context, receiver);
938
+ }
939
+ if (code_id == OSEO_FINALIZATION_REGISTRY_CONSTRUCTOR_CODE_ID) {
940
+ return finalization_registry_construct(context, first, new_target);
941
+ }
942
+ if (code_id == OSEO_FINALIZATION_REGISTRY_REGISTER_CODE_ID) {
943
+ return finalization_registry_register(
944
+ context,
945
+ receiver,
946
+ argument_count,
947
+ arguments
948
+ );
949
+ }
950
+ if (code_id == OSEO_FINALIZATION_REGISTRY_UNREGISTER_CODE_ID) {
951
+ return finalization_registry_unregister(context, receiver, first);
952
+ }
953
+ return oseo_unknown_function(context, code_id);
954
+ }
955
+
956
+ static OseoResult create_weak_builtin(
957
+ OseoContext *context,
958
+ size_t code_id,
959
+ const char *name,
960
+ size_t length,
961
+ OseoFunctionKind kind
962
+ ) {
963
+ size_t name_length = strlen(name);
964
+ if (name_length > 31u) {
965
+ return failure(context, "OSEO2001", "Built-in name is too long.");
966
+ }
967
+ uint16_t units[31];
968
+ for (size_t index = 0u; index < name_length; index += 1u) {
969
+ units[index] = (uint16_t)(unsigned char)name[index];
970
+ }
971
+ OseoValue environment = oseo_undefined();
972
+ OseoRootFrame frame = {NULL, &environment, 1u};
973
+ oseo_roots_push(context, &frame);
974
+ OseoResult result = oseo_environment_create(context, 0u);
975
+ environment = result.value;
976
+ if (result.status == OSEO_STATUS_NORMAL) {
977
+ result = oseo_function_create(
978
+ context,
979
+ code_id,
980
+ environment,
981
+ units,
982
+ name_length,
983
+ length,
984
+ kind,
985
+ oseo_undefined(),
986
+ oseo_undefined(),
987
+ OSEO_FUNCTION_NAME_PREFIX_NONE
988
+ );
989
+ }
990
+ oseo_roots_pop(context, &frame);
991
+ return result;
992
+ }
993
+
994
+ static OseoResult define_ascii_property(
995
+ OseoContext *context,
996
+ OseoValue object,
997
+ const char *name,
998
+ OseoValue value,
999
+ OseoPropertyAttributes attributes
1000
+ ) {
1001
+ OseoValue slots[3] = {object, value, oseo_undefined()};
1002
+ OseoRootFrame frame = {NULL, slots, 3u};
1003
+ oseo_roots_push(context, &frame);
1004
+ OseoResult result = oseo_internal_ascii_string(context, name);
1005
+ slots[2] = result.value;
1006
+ if (result.status == OSEO_STATUS_NORMAL) {
1007
+ result = oseo_object_define(
1008
+ context,
1009
+ slots[0],
1010
+ slots[2],
1011
+ slots[1],
1012
+ attributes
1013
+ );
1014
+ }
1015
+ oseo_roots_pop(context, &frame);
1016
+ return result;
1017
+ }
1018
+
1019
+ /* One source-visible method on a prototype. */
1020
+ typedef struct {
1021
+ OseoIntrinsic intrinsic;
1022
+ size_t code_id;
1023
+ const char *name;
1024
+ size_t length;
1025
+ } OseoWeakMethod;
1026
+
1027
+ /* One constructor, its prototype, and that prototype's methods. */
1028
+ typedef struct {
1029
+ OseoIntrinsic prototype_intrinsic;
1030
+ OseoIntrinsic constructor_intrinsic;
1031
+ size_t constructor_code_id;
1032
+ const char *name;
1033
+ size_t length;
1034
+ const OseoWeakMethod *methods;
1035
+ size_t method_count;
1036
+ } OseoWeakConstructor;
1037
+
1038
+ static const OseoWeakMethod weak_map_methods[] = {
1039
+ {
1040
+ OSEO_INTRINSIC_WEAK_MAP_DELETE,
1041
+ OSEO_WEAK_MAP_DELETE_CODE_ID,
1042
+ "delete",
1043
+ 1u,
1044
+ },
1045
+ {OSEO_INTRINSIC_WEAK_MAP_GET, OSEO_WEAK_MAP_GET_CODE_ID, "get", 1u},
1046
+ {OSEO_INTRINSIC_WEAK_MAP_HAS, OSEO_WEAK_MAP_HAS_CODE_ID, "has", 1u},
1047
+ {OSEO_INTRINSIC_WEAK_MAP_SET, OSEO_WEAK_MAP_SET_CODE_ID, "set", 2u},
1048
+ /* The ECMAScript 2026 upsert methods own no intrinsic slot. */
1049
+ {
1050
+ OSEO_INTRINSIC_COUNT,
1051
+ OSEO_WEAK_MAP_GET_OR_INSERT_CODE_ID,
1052
+ "getOrInsert",
1053
+ 2u,
1054
+ },
1055
+ {
1056
+ OSEO_INTRINSIC_COUNT,
1057
+ OSEO_WEAK_MAP_GET_OR_INSERT_COMPUTED_CODE_ID,
1058
+ "getOrInsertComputed",
1059
+ 2u,
1060
+ },
1061
+ };
1062
+
1063
+ static const OseoWeakMethod weak_set_methods[] = {
1064
+ {OSEO_INTRINSIC_WEAK_SET_ADD, OSEO_WEAK_SET_ADD_CODE_ID, "add", 1u},
1065
+ {
1066
+ OSEO_INTRINSIC_WEAK_SET_DELETE,
1067
+ OSEO_WEAK_SET_DELETE_CODE_ID,
1068
+ "delete",
1069
+ 1u,
1070
+ },
1071
+ {OSEO_INTRINSIC_WEAK_SET_HAS, OSEO_WEAK_SET_HAS_CODE_ID, "has", 1u},
1072
+ };
1073
+
1074
+ static const OseoWeakMethod weak_ref_methods[] = {
1075
+ {OSEO_INTRINSIC_WEAK_REF_DEREF, OSEO_WEAK_REF_DEREF_CODE_ID, "deref", 0u},
1076
+ };
1077
+
1078
+ static const OseoWeakMethod finalization_registry_methods[] = {
1079
+ {
1080
+ OSEO_INTRINSIC_FINALIZATION_REGISTRY_REGISTER,
1081
+ OSEO_FINALIZATION_REGISTRY_REGISTER_CODE_ID,
1082
+ "register",
1083
+ 2u,
1084
+ },
1085
+ {
1086
+ OSEO_INTRINSIC_FINALIZATION_REGISTRY_UNREGISTER,
1087
+ OSEO_FINALIZATION_REGISTRY_UNREGISTER_CODE_ID,
1088
+ "unregister",
1089
+ 1u,
1090
+ },
1091
+ };
1092
+
1093
+ /*
1094
+ * Constructor order is the order globals are installed. The last method of
1095
+ * the last constructor doubles as the cluster's completion marker.
1096
+ */
1097
+ static const OseoWeakConstructor weak_constructors[] = {
1098
+ {
1099
+ OSEO_INTRINSIC_WEAK_MAP_PROTOTYPE,
1100
+ OSEO_INTRINSIC_WEAK_MAP,
1101
+ OSEO_WEAK_MAP_CONSTRUCTOR_CODE_ID,
1102
+ "WeakMap",
1103
+ 0u,
1104
+ weak_map_methods,
1105
+ sizeof(weak_map_methods) / sizeof(weak_map_methods[0]),
1106
+ },
1107
+ {
1108
+ OSEO_INTRINSIC_WEAK_SET_PROTOTYPE,
1109
+ OSEO_INTRINSIC_WEAK_SET,
1110
+ OSEO_WEAK_SET_CONSTRUCTOR_CODE_ID,
1111
+ "WeakSet",
1112
+ 0u,
1113
+ weak_set_methods,
1114
+ sizeof(weak_set_methods) / sizeof(weak_set_methods[0]),
1115
+ },
1116
+ {
1117
+ OSEO_INTRINSIC_WEAK_REF_PROTOTYPE,
1118
+ OSEO_INTRINSIC_WEAK_REF,
1119
+ OSEO_WEAK_REF_CONSTRUCTOR_CODE_ID,
1120
+ "WeakRef",
1121
+ 1u,
1122
+ weak_ref_methods,
1123
+ sizeof(weak_ref_methods) / sizeof(weak_ref_methods[0]),
1124
+ },
1125
+ {
1126
+ OSEO_INTRINSIC_FINALIZATION_REGISTRY_PROTOTYPE,
1127
+ OSEO_INTRINSIC_FINALIZATION_REGISTRY,
1128
+ OSEO_FINALIZATION_REGISTRY_CONSTRUCTOR_CODE_ID,
1129
+ "FinalizationRegistry",
1130
+ 1u,
1131
+ finalization_registry_methods,
1132
+ sizeof(finalization_registry_methods) /
1133
+ sizeof(finalization_registry_methods[0]),
1134
+ },
1135
+ };
1136
+
1137
+ #define OSEO_WEAK_CONSTRUCTOR_COUNT \
1138
+ (sizeof(weak_constructors) / sizeof(weak_constructors[0]))
1139
+
1140
+ /*
1141
+ * Builds one constructor and prototype. `slots` holds 0 Object.prototype,
1142
+ * 1 the prototype, 2 the constructor, 3 a method, 4 a key, and 5 a value.
1143
+ */
1144
+ static OseoResult weak_constructor_build(
1145
+ OseoContext *context,
1146
+ const OseoWeakConstructor *description,
1147
+ OseoValue *slots
1148
+ ) {
1149
+ const OseoPropertyAttributes method = {true, false, true, false};
1150
+ OseoResult result = oseo_object_create(context, slots[0]);
1151
+ slots[1] = result.value;
1152
+ if (result.status == OSEO_STATUS_NORMAL) {
1153
+ context->realm->intrinsics[description->prototype_intrinsic] = slots[1];
1154
+ result = create_weak_builtin(
1155
+ context,
1156
+ description->constructor_code_id,
1157
+ description->name,
1158
+ description->length,
1159
+ OSEO_FUNCTION_ORDINARY
1160
+ );
1161
+ slots[2] = result.value;
1162
+ }
1163
+ if (result.status == OSEO_STATUS_NORMAL) {
1164
+ context->realm->intrinsics[description->constructor_intrinsic] =
1165
+ slots[2];
1166
+ OseoFunction *constructor = function_object(slots[2]);
1167
+ constructor->prototype_object = slots[1];
1168
+ constructor->prototype_writable = false;
1169
+ result = define_ascii_property(
1170
+ context,
1171
+ slots[1],
1172
+ "constructor",
1173
+ slots[2],
1174
+ method
1175
+ );
1176
+ }
1177
+ for (size_t index = 0u;
1178
+ result.status == OSEO_STATUS_NORMAL &&
1179
+ index < description->method_count;
1180
+ index += 1u) {
1181
+ const OseoWeakMethod *entry = &description->methods[index];
1182
+ result = create_weak_builtin(
1183
+ context,
1184
+ entry->code_id,
1185
+ entry->name,
1186
+ entry->length,
1187
+ OSEO_FUNCTION_INTERNAL
1188
+ );
1189
+ slots[3] = result.value;
1190
+ if (result.status != OSEO_STATUS_NORMAL) break;
1191
+ if (entry->intrinsic != OSEO_INTRINSIC_COUNT) {
1192
+ context->realm->intrinsics[entry->intrinsic] = slots[3];
1193
+ }
1194
+ result = define_ascii_property(
1195
+ context,
1196
+ slots[1],
1197
+ entry->name,
1198
+ slots[3],
1199
+ method
1200
+ );
1201
+ }
1202
+ if (result.status == OSEO_STATUS_NORMAL) {
1203
+ result = oseo_internal_well_known_symbol(
1204
+ context,
1205
+ OSEO_WELL_KNOWN_TO_STRING_TAG
1206
+ );
1207
+ slots[4] = result.value;
1208
+ }
1209
+ if (result.status == OSEO_STATUS_NORMAL) {
1210
+ result = oseo_internal_ascii_string(context, description->name);
1211
+ slots[5] = result.value;
1212
+ }
1213
+ if (result.status == OSEO_STATUS_NORMAL) {
1214
+ result = oseo_object_define(
1215
+ context,
1216
+ slots[1],
1217
+ slots[4],
1218
+ slots[5],
1219
+ (OseoPropertyAttributes){true, false, false, false}
1220
+ );
1221
+ }
1222
+ return result;
1223
+ }
1224
+
1225
+ static OseoResult weak_collection_intrinsic_build(OseoContext *context) {
1226
+ OseoValue *intrinsics = context->realm->intrinsics;
1227
+ OseoValue *marker =
1228
+ &intrinsics[OSEO_INTRINSIC_FINALIZATION_REGISTRY_UNREGISTER];
1229
+ if (tag_of(*marker) == OSEO_TAG_UNINITIALIZED) {
1230
+ return failure(
1231
+ context,
1232
+ "OSEO2001",
1233
+ "The weak collection intrinsic cluster is already being built."
1234
+ );
1235
+ }
1236
+ if (tag_of(*marker) != OSEO_TAG_UNDEFINED) return normal(*marker);
1237
+ size_t entry_allocations = context->allocations;
1238
+ OseoRootFrame frame = {NULL, NULL, 0u};
1239
+ OseoResult result = oseo_roots_allocate(context, &frame, 6u);
1240
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1241
+ *marker = oseo_uninitialized();
1242
+ result = oseo_internal_intrinsic(context, OSEO_INTRINSIC_OBJECT_PROTOTYPE);
1243
+ frame.slots[0] = result.value;
1244
+ for (size_t index = 0u;
1245
+ result.status == OSEO_STATUS_NORMAL &&
1246
+ index < OSEO_WEAK_CONSTRUCTOR_COUNT;
1247
+ index += 1u) {
1248
+ result = weak_constructor_build(
1249
+ context,
1250
+ &weak_constructors[index],
1251
+ frame.slots
1252
+ );
1253
+ }
1254
+ if (result.status != OSEO_STATUS_NORMAL) {
1255
+ for (size_t index = OSEO_INTRINSIC_WEAK_MAP_PROTOTYPE;
1256
+ index <= OSEO_INTRINSIC_FINALIZATION_REGISTRY_UNREGISTER;
1257
+ index += 1u) {
1258
+ context->realm->intrinsics[index] = oseo_undefined();
1259
+ }
1260
+ oseo_roots_release(context, &frame);
1261
+ return result;
1262
+ }
1263
+ if (context->observe_specialization) {
1264
+ context->allocations = entry_allocations;
1265
+ }
1266
+ oseo_roots_release(context, &frame);
1267
+ return normal(*marker);
1268
+ }
1269
+
1270
+ OseoResult oseo_internal_weak_collection_intrinsic(OseoContext *context) {
1271
+ return weak_collection_intrinsic_build(context);
1272
+ }
1273
+
1274
+ OseoResult oseo_internal_install_weak_collection_globals(
1275
+ OseoContext *context,
1276
+ OseoValue global
1277
+ ) {
1278
+ OseoValue slots[2] = {global, oseo_undefined()};
1279
+ OseoRootFrame frame = {NULL, slots, 2u};
1280
+ oseo_roots_push(context, &frame);
1281
+ OseoResult result = oseo_internal_weak_collection_intrinsic(context);
1282
+ for (size_t index = 0u;
1283
+ result.status == OSEO_STATUS_NORMAL &&
1284
+ index < OSEO_WEAK_CONSTRUCTOR_COUNT;
1285
+ index += 1u) {
1286
+ OseoIntrinsic constructor =
1287
+ weak_constructors[index].constructor_intrinsic;
1288
+ slots[1] = context->realm->intrinsics[constructor];
1289
+ result = define_ascii_property(
1290
+ context,
1291
+ slots[0],
1292
+ weak_constructors[index].name,
1293
+ slots[1],
1294
+ (OseoPropertyAttributes){true, false, true, false}
1295
+ );
1296
+ }
1297
+ oseo_roots_pop(context, &frame);
1298
+ return result.status == OSEO_STATUS_NORMAL ? normal(slots[0]) : result;
1299
+ }