@oseo/runtime-c 0.0.0-stage → 0.1.0-canary.0

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