@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,1341 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /* Copyright (c) 2026 Hong Minhee; see ../MIT-LICENSE.txt. */
3
+
4
+ #include "runtime_internal.h"
5
+
6
+ #include <math.h>
7
+ #include <stdio.h>
8
+ #include <stdlib.h>
9
+ #include <string.h>
10
+
11
+ /*
12
+ * Heap allocation, publication, tracing, collection, and
13
+ * destruction for every runtime heap kind.
14
+ */
15
+
16
+ static bool mark_value(
17
+ OseoValue value,
18
+ OseoHeapObject **worklist
19
+ ) {
20
+ if (tag_of(value) != OSEO_TAG_HEAP) return false;
21
+ OseoHeapObject *object = heap_object(value);
22
+ if (object->marked) return false;
23
+ object->marked = true;
24
+ object->trace_next = *worklist;
25
+ *worklist = object;
26
+ return true;
27
+ }
28
+
29
+ /*
30
+ * Marks everything one realm owns: its intrinsic graph, its global
31
+ * object, and the literal-cache entries that keep a site's object alive.
32
+ */
33
+ static void mark_realm(const OseoRealm *realm, OseoHeapObject **worklist) {
34
+ for (size_t index = 0u; index < OSEO_INTRINSIC_COUNT; index += 1u) {
35
+ mark_value(realm->intrinsics[index], worklist);
36
+ }
37
+ mark_value(realm->global_this, worklist);
38
+ mark_value(realm->global_lexicals, worklist);
39
+ const OseoTemplateCacheEntry *template_cache = realm->template_cache;
40
+ for (size_t index = 0u; index < realm->template_cache_count; index += 1u) {
41
+ mark_value(template_cache[index].object, worklist);
42
+ }
43
+ const OseoRegExpLiteralCacheEntry *regexp_cache =
44
+ realm->regexp_literal_cache;
45
+ for (size_t index = 0u;
46
+ index < realm->regexp_literal_cache_capacity;
47
+ index += 1u) {
48
+ if (regexp_cache[index].literal == NULL) continue;
49
+ mark_value(regexp_cache[index].matcher, worklist);
50
+ }
51
+ }
52
+
53
+ /* Return the least set bit's index; callers pass a nonzero word. */
54
+ static size_t heap_bitmap_lowest_bit(uint64_t bits) {
55
+ size_t index = 0u;
56
+ if ((bits & UINT64_C(0xffffffff)) == 0u) {
57
+ index += 32u;
58
+ bits >>= 32u;
59
+ }
60
+ if ((bits & UINT64_C(0xffff)) == 0u) {
61
+ index += 16u;
62
+ bits >>= 16u;
63
+ }
64
+ if ((bits & UINT64_C(0xff)) == 0u) {
65
+ index += 8u;
66
+ bits >>= 8u;
67
+ }
68
+ if ((bits & UINT64_C(0xf)) == 0u) {
69
+ index += 4u;
70
+ bits >>= 4u;
71
+ }
72
+ if ((bits & UINT64_C(0x3)) == 0u) {
73
+ index += 2u;
74
+ bits >>= 2u;
75
+ }
76
+ if ((bits & UINT64_C(0x1)) == 0u) index += 1u;
77
+ return index;
78
+ }
79
+
80
+ static void trace_object(
81
+ OseoHeapObject *object,
82
+ OseoHeapObject **worklist
83
+ ) {
84
+ if (object->kind == OSEO_HEAP_BIGINT ||
85
+ object->kind == OSEO_HEAP_STRING ||
86
+ object->kind == OSEO_HEAP_PRIVATE_NAME) {
87
+ return;
88
+ }
89
+ if (object->kind == OSEO_HEAP_REALM) {
90
+ mark_realm(&((OseoRealmRecord *)object)->realm, worklist);
91
+ return;
92
+ }
93
+ if (object->kind == OSEO_HEAP_REGEXP_MATCHER) {
94
+ OseoRegExpMatcher *matcher = (OseoRegExpMatcher *)object;
95
+ mark_value(matcher->source, worklist);
96
+ mark_value(matcher->flags, worklist);
97
+ return;
98
+ }
99
+ if (object->kind == OSEO_HEAP_ENVIRONMENT) {
100
+ OseoEnvironment *environment = (OseoEnvironment *)object;
101
+ const uint64_t *bitmap = environment_heap_bitmap(environment);
102
+ for (size_t word = 0u;
103
+ word < environment->heap_word_count;
104
+ word += 1u) {
105
+ uint64_t bits = bitmap[word];
106
+ while (bits != 0u) {
107
+ size_t index = word * 64u + heap_bitmap_lowest_bit(bits);
108
+ mark_value(environment->slots[index], worklist);
109
+ bits &= bits - UINT64_C(1);
110
+ }
111
+ }
112
+ } else if (object->kind == OSEO_HEAP_CELL) {
113
+ OseoCell *cell = (OseoCell *)object;
114
+ mark_value(cell->value, worklist);
115
+ if (cell->object_environment) {
116
+ mark_value(cell->object, worklist);
117
+ mark_value(cell->key, worklist);
118
+ }
119
+ } else if (object->kind == OSEO_HEAP_SYMBOL) {
120
+ mark_value(((OseoSymbol *)object)->description, worklist);
121
+ } else if (object->kind == OSEO_HEAP_ARGUMENT_LIST) {
122
+ OseoArgumentList *list = (OseoArgumentList *)object;
123
+ for (size_t index = 0u; index < list->length; index += 1u) {
124
+ mark_value(list->values[index], worklist);
125
+ }
126
+ } else if (object->kind == OSEO_HEAP_EPHEMERON_TABLE) {
127
+ OseoEphemeronTable *table = (OseoEphemeronTable *)object;
128
+ mark_value(table->head, worklist);
129
+ mark_value(table->tail, worklist);
130
+ } else if (object->kind == OSEO_HEAP_EPHEMERON_ENTRY) {
131
+ /* `key` and `value` are processed by the fixed-point phase. */
132
+ mark_value(((OseoEphemeronEntry *)object)->next, worklist);
133
+ } else if (object->kind == OSEO_HEAP_WEAK_REFERENCE) {
134
+ /* `target` is cleared after the ephemeron fixed point. */
135
+ return;
136
+ } else if (object->kind == OSEO_HEAP_FINALIZATION_REGISTRY) {
137
+ OseoFinalizationRegistry *registry =
138
+ (OseoFinalizationRegistry *)object;
139
+ mark_value(registry->callback, worklist);
140
+ mark_value(registry->cell_head, worklist);
141
+ mark_value(registry->cell_tail, worklist);
142
+ } else if (object->kind == OSEO_HEAP_FINALIZATION_CELL) {
143
+ OseoFinalizationCell *cell = (OseoFinalizationCell *)object;
144
+ mark_value(cell->next, worklist);
145
+ mark_value(cell->queue_next, worklist);
146
+ mark_value(cell->registry, worklist);
147
+ if (!cell->processed) mark_value(cell->holdings, worklist);
148
+ /* `target` and `unregister_token` are weak and never enter
149
+ * ordinary tracing. */
150
+ } else if (object->kind == OSEO_HEAP_ENUMERATION) {
151
+ /* The receiver, candidates, and visited keys are reachable only
152
+ * through the record a for-in head roots, so a suspended body keeps
153
+ * all three alive. */
154
+ OseoEnumeration *enumeration = (OseoEnumeration *)object;
155
+ mark_value(enumeration->receiver, worklist);
156
+ mark_value(enumeration->candidates, worklist);
157
+ mark_value(enumeration->visited, worklist);
158
+ } else if (object->kind == OSEO_HEAP_OBJECT ||
159
+ object->kind == OSEO_HEAP_ARRAY ||
160
+ object->kind == OSEO_HEAP_FUNCTION ||
161
+ object->kind == OSEO_HEAP_PROMISE ||
162
+ object->kind == OSEO_HEAP_ARRAY_BUFFER ||
163
+ object->kind == OSEO_HEAP_MAP ||
164
+ object->kind == OSEO_HEAP_MAP_ITERATOR ||
165
+ object->kind == OSEO_HEAP_DATA_VIEW ||
166
+ object->kind == OSEO_HEAP_DATE ||
167
+ object->kind == OSEO_HEAP_REGEXP ||
168
+ object->kind == OSEO_HEAP_ITERATOR_HELPER ||
169
+ object->kind == OSEO_HEAP_PROXY ||
170
+ object->kind == OSEO_HEAP_SET ||
171
+ object->kind == OSEO_HEAP_SET_ITERATOR ||
172
+ object->kind == OSEO_HEAP_TYPED_ARRAY ||
173
+ object->kind == OSEO_HEAP_WEAK_MAP ||
174
+ object->kind == OSEO_HEAP_WEAK_SET ||
175
+ object->kind == OSEO_HEAP_WEAK_REF ||
176
+ object->kind == OSEO_HEAP_FINALIZATION_REGISTRY_OBJECT ||
177
+ object->kind == OSEO_HEAP_DISPOSABLE_STACK) {
178
+ OseoOrdinaryObject *ordinary = (OseoOrdinaryObject *)object;
179
+ mark_value(ordinary->prototype, worklist);
180
+ if (ordinary->primitive_data) {
181
+ mark_value(ordinary->primitive_value, worklist);
182
+ }
183
+ if (ordinary->iterator_kind != OSEO_ITERATOR_NONE) {
184
+ mark_value(ordinary->iterator_target, worklist);
185
+ }
186
+ if (ordinary->regexp_string_iterator) {
187
+ mark_value(ordinary->regexp_iterator_subject, worklist);
188
+ mark_value(ordinary->regexp_iterator_regexp, worklist);
189
+ }
190
+ if (ordinary->async_from_sync) {
191
+ mark_value(ordinary->async_sync_iterator, worklist);
192
+ }
193
+ if (ordinary->wrap_for_valid_iterator) {
194
+ mark_value(ordinary->wrapped_iterator, worklist);
195
+ mark_value(ordinary->wrapped_next, worklist);
196
+ }
197
+ if (ordinary->generator != NULL) {
198
+ OseoGenerator *generator = ordinary->generator;
199
+ mark_value(generator->callee, worklist);
200
+ mark_value(generator->receiver, worklist);
201
+ mark_value(generator->sent, worklist);
202
+ mark_value(generator->async_function_capability, worklist);
203
+ /* The pending AsyncGeneratorRequest queue is reachable only
204
+ * through the generator that accepted it. */
205
+ mark_value(generator->request_head, worklist);
206
+ mark_value(generator->request_tail, worklist);
207
+ /* The suspended body's roots, including its saved
208
+ * completion values, live only here. */
209
+ for (size_t index = 0u;
210
+ index < generator->slot_count;
211
+ index += 1u) {
212
+ mark_value(generator->slots[index], worklist);
213
+ }
214
+ }
215
+ for (size_t index = 0u; index < ordinary->property_count; index += 1u) {
216
+ mark_value(ordinary->properties[index].key, worklist);
217
+ mark_value(ordinary->properties[index].value, worklist);
218
+ mark_value(ordinary->properties[index].getter, worklist);
219
+ mark_value(ordinary->properties[index].setter, worklist);
220
+ }
221
+ /* [[PrivateElements]] is reachable only through the object that
222
+ * carries it, so nothing else keeps a private name or a private
223
+ * method alive once the instance dies. */
224
+ for (size_t index = 0u;
225
+ index < ordinary->private_element_count;
226
+ index += 1u) {
227
+ mark_value(ordinary->private_elements[index].key, worklist);
228
+ mark_value(ordinary->private_elements[index].value, worklist);
229
+ mark_value(ordinary->private_elements[index].getter, worklist);
230
+ mark_value(ordinary->private_elements[index].setter, worklist);
231
+ }
232
+ if (object->kind == OSEO_HEAP_FUNCTION) {
233
+ OseoFunction *function = (OseoFunction *)object;
234
+ mark_value(function->environment, worklist);
235
+ mark_value(function->lexical_this, worklist);
236
+ mark_value(function->lexical_new_target, worklist);
237
+ mark_value(function->lexical_super, worklist);
238
+ mark_value(function->prototype_object, worklist);
239
+ mark_value(function->home_object, worklist);
240
+ mark_value(function->initial_name, worklist);
241
+ mark_value(function->source_text, worklist);
242
+ mark_value(function->realm, worklist);
243
+ mark_value(function->bound_target, worklist);
244
+ mark_value(function->bound_this, worklist);
245
+ mark_value(function->bound_arguments, worklist);
246
+ for (size_t index = 0u;
247
+ index < function->element_count;
248
+ index += 1u) {
249
+ mark_value(function->elements[index].key, worklist);
250
+ mark_value(function->elements[index].value, worklist);
251
+ mark_value(function->elements[index].getter, worklist);
252
+ mark_value(function->elements[index].setter, worklist);
253
+ }
254
+ } else if (object->kind == OSEO_HEAP_PROMISE) {
255
+ OseoPromise *promise = (OseoPromise *)object;
256
+ mark_value(promise->result, worklist);
257
+ mark_value(promise->reaction_head, worklist);
258
+ mark_value(promise->reaction_tail, worklist);
259
+ mark_value(promise->unhandled_next, worklist);
260
+ mark_value(promise->realm, worklist);
261
+ } else if (object->kind == OSEO_HEAP_MAP) {
262
+ OseoMap *map = (OseoMap *)object;
263
+ for (size_t index = 0u; index < map->entry_count; index += 1u) {
264
+ if (!map->entries[index].live) continue;
265
+ mark_value(map->entries[index].key, worklist);
266
+ mark_value(map->entries[index].value, worklist);
267
+ }
268
+ } else if (object->kind == OSEO_HEAP_MAP_ITERATOR) {
269
+ mark_value(((OseoMapIterator *)object)->target, worklist);
270
+ } else if (object->kind == OSEO_HEAP_DATA_VIEW) {
271
+ /* A view holds no Data Block of its own; tracing its buffer
272
+ * is what keeps that buffer's block alive. */
273
+ mark_value(((OseoDataView *)object)->buffer, worklist);
274
+ } else if (object->kind == OSEO_HEAP_TYPED_ARRAY) {
275
+ mark_value(
276
+ ((OseoTypedArray *)object)->viewed_buffer,
277
+ worklist
278
+ );
279
+ } else if (object->kind == OSEO_HEAP_REGEXP) {
280
+ mark_value(((OseoRegExp *)object)->matcher, worklist);
281
+ } else if (object->kind == OSEO_HEAP_ITERATOR_HELPER) {
282
+ /* A helper's underlying iterator record, callback, and
283
+ * in-flight inner iterator are reachable only through the
284
+ * helper, so a collection at any safepoint inside one
285
+ * resumption keeps every one of them alive. */
286
+ OseoIteratorHelper *helper = (OseoIteratorHelper *)object;
287
+ mark_value(helper->underlying_iterator, worklist);
288
+ mark_value(helper->underlying_next, worklist);
289
+ mark_value(helper->callback, worklist);
290
+ mark_value(helper->inner_iterator, worklist);
291
+ mark_value(helper->inner_next, worklist);
292
+ } else if (object->kind == OSEO_HEAP_PROXY) {
293
+ OseoProxy *proxy = (OseoProxy *)object;
294
+ mark_value(proxy->target, worklist);
295
+ mark_value(proxy->handler, worklist);
296
+ } else if (object->kind == OSEO_HEAP_SET) {
297
+ OseoSet *set = (OseoSet *)object;
298
+ for (size_t index = 0u;
299
+ index < set->element_count;
300
+ index += 1u) {
301
+ if (set->elements[index].present) {
302
+ mark_value(set->elements[index].value, worklist);
303
+ }
304
+ }
305
+ } else if (object->kind == OSEO_HEAP_SET_ITERATOR) {
306
+ mark_value(((OseoSetIterator *)object)->set, worklist);
307
+ } else if (object->kind == OSEO_HEAP_WEAK_MAP ||
308
+ object->kind == OSEO_HEAP_WEAK_SET) {
309
+ /* The table is strong; its entries stay ephemerons. */
310
+ mark_value(((OseoWeakCollection *)object)->table, worklist);
311
+ } else if (object->kind == OSEO_HEAP_WEAK_REF) {
312
+ mark_value(((OseoWeakRef *)object)->reference, worklist);
313
+ } else if (object->kind == OSEO_HEAP_FINALIZATION_REGISTRY_OBJECT) {
314
+ mark_value(
315
+ ((OseoFinalizationRegistryObject *)object)->registry,
316
+ worklist
317
+ );
318
+ } else if (object->kind == OSEO_HEAP_DISPOSABLE_STACK) {
319
+ OseoDisposableStack *stack = (OseoDisposableStack *)object;
320
+ mark_value(stack->capability, worklist);
321
+ mark_value(stack->error, worklist);
322
+ mark_value(stack->await_value, worklist);
323
+ mark_value(stack->promise, worklist);
324
+ mark_value(stack->on_fulfilled, worklist);
325
+ mark_value(stack->on_rejected, worklist);
326
+ for (size_t index = 0u;
327
+ index < stack->resource_count;
328
+ index += 1u) {
329
+ mark_value(stack->resources[index], worklist);
330
+ }
331
+ }
332
+ } else if (object->kind == OSEO_HEAP_PROMISE_REACTION) {
333
+ OseoPromiseReaction *reaction = (OseoPromiseReaction *)object;
334
+ mark_value(reaction->next, worklist);
335
+ mark_value(reaction->on_fulfilled, worklist);
336
+ mark_value(reaction->on_rejected, worklist);
337
+ mark_value(reaction->capability, worklist);
338
+ mark_value(reaction->aggregate, worklist);
339
+ } else if (object->kind == OSEO_HEAP_JOB) {
340
+ OseoJob *job = (OseoJob *)object;
341
+ mark_value(job->next, worklist);
342
+ mark_value(job->primary, worklist);
343
+ mark_value(job->secondary, worklist);
344
+ mark_value(job->argument, worklist);
345
+ mark_value(job->realm, worklist);
346
+ } else if (object->kind == OSEO_HEAP_PROMISE_AGGREGATE) {
347
+ OseoPromiseAggregate *aggregate = (OseoPromiseAggregate *)object;
348
+ mark_value(aggregate->capability, worklist);
349
+ mark_value(aggregate->values, worklist);
350
+ } else if (object->kind == OSEO_HEAP_ASYNC_GENERATOR_REQUEST) {
351
+ OseoAsyncGeneratorRequest *request =
352
+ (OseoAsyncGeneratorRequest *)object;
353
+ mark_value(request->next, worklist);
354
+ mark_value(request->capability, worklist);
355
+ mark_value(request->value, worklist);
356
+ } else if (object->kind == OSEO_HEAP_TIMER) {
357
+ OseoTimer *timer = (OseoTimer *)object;
358
+ mark_value(timer->next, worklist);
359
+ mark_value(timer->callback, worklist);
360
+ mark_value(timer->arguments, worklist);
361
+ mark_value(timer->waiter, worklist);
362
+ } else if (object->kind == OSEO_HEAP_ATOMICS_WAITER) {
363
+ OseoAtomicsWaiter *waiter = (OseoAtomicsWaiter *)object;
364
+ mark_value(waiter->next, worklist);
365
+ mark_value(waiter->buffer, worklist);
366
+ mark_value(waiter->promise, worklist);
367
+ mark_value(waiter->timer, worklist);
368
+ }
369
+ }
370
+
371
+ static bool value_is_marked(OseoValue value) {
372
+ return tag_of(value) == OSEO_TAG_HEAP && heap_object(value)->marked;
373
+ }
374
+
375
+ static OseoValue heap_value(OseoHeapObject *object) {
376
+ return tagged(OSEO_TAG_HEAP, (uint64_t)(uintptr_t)object);
377
+ }
378
+
379
+ /*
380
+ * The collector never moves an object, so a key's heap address is a stable
381
+ * identity. Multiplicative hashing spreads the aligned low bits.
382
+ */
383
+ static size_t ephemeron_index_start(OseoValue key, size_t capacity) {
384
+ uint64_t hash = ((key & OSEO_PAYLOAD_MASK) >> 3u) *
385
+ UINT64_C(0x9e3779b97f4a7c15);
386
+ return (size_t)(hash >> 20u) & (capacity - 1u);
387
+ }
388
+
389
+ /* Returns the index slot holding `key`'s entry, or NULL. */
390
+ static OseoValue *ephemeron_index_find(
391
+ OseoEphemeronTable *table,
392
+ OseoValue key
393
+ ) {
394
+ if (table->index_capacity == 0u) return NULL;
395
+ size_t mask = table->index_capacity - 1u;
396
+ size_t slot = ephemeron_index_start(key, table->index_capacity);
397
+ for (size_t probe = 0u; probe < table->index_capacity; probe += 1u) {
398
+ OseoValue *candidate = &table->index[slot];
399
+ if (*candidate == 0u) return NULL;
400
+ if (*candidate != OSEO_EPHEMERON_INDEX_TOMBSTONE &&
401
+ ephemeron_entry_object(*candidate)->key == key) {
402
+ return candidate;
403
+ }
404
+ slot = (slot + 1u) & mask;
405
+ }
406
+ return NULL;
407
+ }
408
+
409
+ /*
410
+ * Inserts into a reserved index; the key must not be present. Returns
411
+ * whether the insertion consumed an empty slot rather than reusing a
412
+ * tombstone, which is the only case that grows the used-slot count.
413
+ */
414
+ static bool ephemeron_index_insert(
415
+ OseoValue *index,
416
+ size_t capacity,
417
+ OseoValue key,
418
+ OseoValue entry
419
+ ) {
420
+ size_t slot = ephemeron_index_start(key, capacity);
421
+ while (index[slot] != 0u &&
422
+ index[slot] != OSEO_EPHEMERON_INDEX_TOMBSTONE) {
423
+ slot = (slot + 1u) & (capacity - 1u);
424
+ }
425
+ bool empty = index[slot] == 0u;
426
+ index[slot] = entry;
427
+ return empty;
428
+ }
429
+
430
+ /* Activate entries that waited for this key to finish marking. */
431
+ static void activate_ephemeron_values(
432
+ OseoHeapObject *key,
433
+ OseoHeapObject **worklist
434
+ ) {
435
+ OseoHeapObject *pending = key->ephemeron_pending;
436
+ key->ephemeron_pending = NULL;
437
+ while (pending != NULL) {
438
+ OseoEphemeronEntry *entry = (OseoEphemeronEntry *)pending;
439
+ pending = entry->header.trace_next;
440
+ entry->header.trace_next = NULL;
441
+ mark_value(entry->value, worklist);
442
+ }
443
+ }
444
+
445
+ /*
446
+ * Drain strong edges and activate ephemeron values as their keys become
447
+ * reachable. An entry discovered before its key parks on the key object's
448
+ * collector-only pending list. Processing that key activates every waiting
449
+ * value, so the worklist computes the fixed point without repeated scans.
450
+ */
451
+ static void trace_ephemeron_fixed_point(OseoHeapObject **worklist) {
452
+ while (*worklist != NULL) {
453
+ OseoHeapObject *object = *worklist;
454
+ *worklist = object->trace_next;
455
+ object->trace_next = NULL;
456
+ trace_object(object, worklist);
457
+ if (object->kind == OSEO_HEAP_EPHEMERON_ENTRY) {
458
+ OseoEphemeronEntry *entry = (OseoEphemeronEntry *)object;
459
+ OseoHeapObject *key = heap_object(entry->key);
460
+ if (key->marked) {
461
+ mark_value(entry->value, worklist);
462
+ } else {
463
+ entry->header.trace_next = key->ephemeron_pending;
464
+ key->ephemeron_pending = &entry->header;
465
+ }
466
+ }
467
+ activate_ephemeron_values(object, worklist);
468
+ }
469
+ }
470
+
471
+ /*
472
+ * An entry is reachable only through its table's chain, so walking every
473
+ * marked table visits every marked entry exactly once. A dead-key entry is
474
+ * unlinked and unmarked here, which lets the same sweep reclaim it: weak
475
+ * tables must not retain per-entry records after their keys die.
476
+ */
477
+ static void clear_dead_weak_edges(OseoContext *context) {
478
+ for (OseoHeapObject *object = context->objects;
479
+ object != NULL;
480
+ object = object->next) {
481
+ if (!object->marked) continue;
482
+ if (object->kind == OSEO_HEAP_WEAK_REFERENCE) {
483
+ OseoWeakReference *reference = (OseoWeakReference *)object;
484
+ if (tag_of(reference->target) == OSEO_TAG_HEAP &&
485
+ !value_is_marked(reference->target)) {
486
+ reference->target = oseo_undefined();
487
+ }
488
+ } else if (object->kind == OSEO_HEAP_EPHEMERON_TABLE) {
489
+ OseoEphemeronTable *table = (OseoEphemeronTable *)object;
490
+ OseoValue *link = &table->head;
491
+ OseoHeapObject *last = NULL;
492
+ while (tag_of(*link) == OSEO_TAG_HEAP) {
493
+ OseoEphemeronEntry *entry = ephemeron_entry_object(*link);
494
+ if (value_is_marked(entry->key)) {
495
+ last = &entry->header;
496
+ link = &entry->next;
497
+ continue;
498
+ }
499
+ OseoValue *slot = ephemeron_index_find(table, entry->key);
500
+ if (slot != NULL) *slot = OSEO_EPHEMERON_INDEX_TOMBSTONE;
501
+ *link = entry->next;
502
+ if (tag_of(entry->next) == OSEO_TAG_HEAP) {
503
+ ephemeron_entry_object(entry->next)->previous =
504
+ last == NULL ? oseo_undefined() : heap_value(last);
505
+ }
506
+ entry->next = oseo_undefined();
507
+ entry->previous = oseo_undefined();
508
+ entry->key = oseo_undefined();
509
+ entry->value = oseo_undefined();
510
+ entry->header.marked = false;
511
+ if (table->live_count > 0u) table->live_count -= 1u;
512
+ }
513
+ table->tail =
514
+ last == NULL ? oseo_undefined() : heap_value(last);
515
+ } else if (object->kind == OSEO_HEAP_FINALIZATION_CELL) {
516
+ OseoFinalizationCell *cell = (OseoFinalizationCell *)object;
517
+ if (tag_of(cell->unregister_token) == OSEO_TAG_HEAP &&
518
+ !value_is_marked(cell->unregister_token)) {
519
+ cell->unregister_token = oseo_undefined();
520
+ }
521
+ }
522
+ }
523
+ }
524
+
525
+ /*
526
+ * A consumed cleanup cell has no further observable state, so unlinking
527
+ * it from its registry's chain and dropping its mark lets the sweep free
528
+ * it. Without this, a long-lived registry would retain one dead record
529
+ * per registration forever.
530
+ */
531
+ static void compact_finalization_registries(OseoContext *context) {
532
+ for (OseoHeapObject *object = context->objects;
533
+ object != NULL;
534
+ object = object->next) {
535
+ if (object->kind != OSEO_HEAP_FINALIZATION_REGISTRY ||
536
+ !object->marked) {
537
+ continue;
538
+ }
539
+ OseoFinalizationRegistry *registry =
540
+ (OseoFinalizationRegistry *)object;
541
+ OseoValue *link = &registry->cell_head;
542
+ OseoHeapObject *last = NULL;
543
+ while (tag_of(*link) == OSEO_TAG_HEAP) {
544
+ OseoFinalizationCell *cell = finalization_cell_object(*link);
545
+ if (cell->processed) {
546
+ *link = cell->next;
547
+ cell->next = oseo_undefined();
548
+ /* An unregistered record still on the cleanup FIFO stays
549
+ * alive until the dequeue skips it. */
550
+ if (!cell->queued) cell->header.marked = false;
551
+ } else {
552
+ last = &cell->header;
553
+ link = &cell->next;
554
+ }
555
+ }
556
+ registry->cell_tail =
557
+ last == NULL ? oseo_undefined() : heap_value(last);
558
+ }
559
+ }
560
+
561
+ /*
562
+ * Scheduling is allocation-free and deterministic: each collection sorts
563
+ * its newly eligible cells by registration order and appends that batch to
564
+ * the FIFO, so the order is by registration within a batch, not globally.
565
+ * The collector only publishes cleanup records; a later checkpoint decides
566
+ * when to invoke the JavaScript callback that will consume them. One pass
567
+ * gathers the newly eligible cells onto an ordinal-sorted list threaded
568
+ * through their free `trace_next` links, so the heap is scanned once
569
+ * rather than once per queued cell.
570
+ */
571
+ static void schedule_finalization(OseoContext *context) {
572
+ OseoHeapObject *candidates = NULL;
573
+ for (OseoHeapObject *object = context->objects;
574
+ object != NULL;
575
+ object = object->next) {
576
+ if (object->kind != OSEO_HEAP_FINALIZATION_CELL ||
577
+ !object->marked) {
578
+ continue;
579
+ }
580
+ OseoFinalizationCell *cell = (OseoFinalizationCell *)object;
581
+ if (cell->processed || cell->queued ||
582
+ tag_of(cell->target) != OSEO_TAG_HEAP ||
583
+ value_is_marked(cell->target)) {
584
+ continue;
585
+ }
586
+ OseoHeapObject **link = &candidates;
587
+ while (*link != NULL &&
588
+ ((OseoFinalizationCell *)*link)->registration_order <
589
+ cell->registration_order) {
590
+ link = &(*link)->trace_next;
591
+ }
592
+ cell->header.trace_next = *link;
593
+ *link = &cell->header;
594
+ }
595
+ while (candidates != NULL) {
596
+ OseoFinalizationCell *cell = (OseoFinalizationCell *)candidates;
597
+ candidates = cell->header.trace_next;
598
+ cell->header.trace_next = NULL;
599
+ OseoValue cell_value = heap_value(&cell->header);
600
+ cell->target = oseo_undefined();
601
+ cell->queue_next = oseo_undefined();
602
+ cell->queued = true;
603
+ if (tag_of(context->finalization_tail) == OSEO_TAG_HEAP) {
604
+ finalization_cell_object(context->finalization_tail)->queue_next =
605
+ cell_value;
606
+ } else {
607
+ context->finalization_head = cell_value;
608
+ }
609
+ context->finalization_tail = cell_value;
610
+ context->finalization_pending_count += 1u;
611
+ }
612
+ }
613
+
614
+ static void destroy_heap_object(OseoHeapObject *object) {
615
+ if (object->kind == OSEO_HEAP_OBJECT ||
616
+ object->kind == OSEO_HEAP_ARRAY ||
617
+ object->kind == OSEO_HEAP_FUNCTION ||
618
+ object->kind == OSEO_HEAP_PROMISE ||
619
+ object->kind == OSEO_HEAP_ARRAY_BUFFER ||
620
+ object->kind == OSEO_HEAP_MAP ||
621
+ object->kind == OSEO_HEAP_MAP_ITERATOR ||
622
+ object->kind == OSEO_HEAP_DATA_VIEW ||
623
+ object->kind == OSEO_HEAP_DATE ||
624
+ object->kind == OSEO_HEAP_REGEXP ||
625
+ object->kind == OSEO_HEAP_ITERATOR_HELPER ||
626
+ object->kind == OSEO_HEAP_PROXY ||
627
+ object->kind == OSEO_HEAP_SET ||
628
+ object->kind == OSEO_HEAP_SET_ITERATOR ||
629
+ object->kind == OSEO_HEAP_TYPED_ARRAY ||
630
+ object->kind == OSEO_HEAP_WEAK_MAP ||
631
+ object->kind == OSEO_HEAP_WEAK_SET ||
632
+ object->kind == OSEO_HEAP_WEAK_REF ||
633
+ object->kind == OSEO_HEAP_FINALIZATION_REGISTRY_OBJECT ||
634
+ object->kind == OSEO_HEAP_DISPOSABLE_STACK) {
635
+ OseoOrdinaryObject *ordinary = (OseoOrdinaryObject *)object;
636
+ free(ordinary->properties);
637
+ free(ordinary->private_elements);
638
+ free(ordinary->generator);
639
+ if (object->kind == OSEO_HEAP_FUNCTION) {
640
+ free(((OseoFunction *)object)->elements);
641
+ } else if (object->kind == OSEO_HEAP_ARRAY_BUFFER) {
642
+ /* The Data Block is owned by this buffer alone, and the
643
+ * release leaves the record detached, so a block already
644
+ * given up by a transfer is never freed a second time. */
645
+ oseo_internal_array_buffer_release(object);
646
+ } else if (object->kind == OSEO_HEAP_MAP) {
647
+ free(((OseoMap *)object)->entries);
648
+ } else if (object->kind == OSEO_HEAP_SET) {
649
+ free(((OseoSet *)object)->elements);
650
+ } else if (object->kind == OSEO_HEAP_DISPOSABLE_STACK) {
651
+ free(((OseoDisposableStack *)object)->resources);
652
+ }
653
+ } else if (object->kind == OSEO_HEAP_REALM) {
654
+ oseo_internal_realm_release(&((OseoRealmRecord *)object)->realm);
655
+ } else if (object->kind == OSEO_HEAP_ARGUMENT_LIST) {
656
+ free(((OseoArgumentList *)object)->values);
657
+ } else if (object->kind == OSEO_HEAP_EPHEMERON_TABLE) {
658
+ free(((OseoEphemeronTable *)object)->index);
659
+ } else if (object->kind == OSEO_HEAP_REGEXP_MATCHER) {
660
+ /* A dynamic pattern's compiled program is unmanaged memory this
661
+ * artifact alone owns, so its lifetime ends with the artifact.
662
+ * An ahead-of-time literal's program is static generated data
663
+ * this artifact only borrows. */
664
+ OseoRegExpMatcher *matcher = (OseoRegExpMatcher *)object;
665
+ if (matcher->owns_program) {
666
+ oseo_internal_regexp_program_release(matcher->program);
667
+ }
668
+ }
669
+ free(object);
670
+ }
671
+
672
+ /*
673
+ * These record kinds never change after publication. Count only objects
674
+ * linked to this context, not foreign objects the marking worklist visits.
675
+ * Publication and sweep are their only heap-membership transitions;
676
+ * unregistering or consuming a cleanup record does not remove the object.
677
+ */
678
+ static size_t *weak_record_count(OseoContext *context, OseoHeapKind kind) {
679
+ switch (kind) {
680
+ case OSEO_HEAP_EPHEMERON_TABLE:
681
+ return &context->ephemeron_table_count;
682
+ case OSEO_HEAP_WEAK_REFERENCE:
683
+ return &context->weak_reference_count;
684
+ case OSEO_HEAP_FINALIZATION_REGISTRY:
685
+ return &context->finalization_registry_count;
686
+ case OSEO_HEAP_FINALIZATION_CELL:
687
+ return &context->finalization_cell_count;
688
+ default:
689
+ return NULL;
690
+ }
691
+ }
692
+
693
+ /*
694
+ * Drops every mark this heap carries. A mark belongs to one collection of
695
+ * one context, but a context that holds a value another context owns in
696
+ * its roots marks that value while collecting, and only the owning
697
+ * context's sweep clears a mark on its own heap. Such a mark therefore
698
+ * survives until this heap sweeps again, which the final sweep of a
699
+ * destroyed context never does: the object would stay linked with nothing
700
+ * left to free it. Clearing the marks first makes that sweep unconditional.
701
+ */
702
+ void oseo_internal_clear_heap_marks(OseoContext *context) {
703
+ for (OseoHeapObject *object = context->objects;
704
+ object != NULL;
705
+ object = object->next) {
706
+ object->marked = false;
707
+ }
708
+ }
709
+
710
+ void oseo_collect(OseoContext *context) {
711
+ if (context->observe_specialization) context->collections += 1u;
712
+ OseoHeapObject *worklist = NULL;
713
+ for (OseoRootFrame *frame = context->roots;
714
+ frame != NULL;
715
+ frame = frame->previous) {
716
+ for (size_t index = 0u; index < frame->slot_count; index += 1u) {
717
+ mark_value(frame->slots[index], &worklist);
718
+ }
719
+ }
720
+ mark_value(context->microtask_head, &worklist);
721
+ mark_value(context->async_call_capability, &worklist);
722
+ mark_value(context->microtask_tail, &worklist);
723
+ mark_value(context->pending_rejections, &worklist);
724
+ mark_value(context->pending_rejection_tail, &worklist);
725
+ mark_realm(&context->initial_realm, &worklist);
726
+ mark_value(context->realm_record, &worklist);
727
+ /* Each record's values are also rooted by the frame that made its
728
+ * call; tracing them keeps that an optimization, not a requirement. */
729
+ for (const OseoActivation *activation = context->activation;
730
+ activation != NULL;
731
+ activation = activation->previous) {
732
+ mark_value(activation->function, &worklist);
733
+ mark_value(activation->caller, &worklist);
734
+ for (size_t index = 0u;
735
+ index < activation->argument_count;
736
+ index += 1u) {
737
+ mark_value(activation->arguments[index], &worklist);
738
+ }
739
+ }
740
+ mark_value(context->legacy_function_keys[0], &worklist);
741
+ mark_value(context->legacy_function_keys[1], &worklist);
742
+ for (size_t index = 0u;
743
+ index < OSEO_WELL_KNOWN_SYMBOL_COUNT;
744
+ index += 1u) {
745
+ mark_value(context->well_known_symbols[index], &worklist);
746
+ }
747
+ for (size_t index = 0u;
748
+ index < context->registered_symbol_capacity;
749
+ index += 1u) {
750
+ mark_value(context->registered_symbols[index], &worklist);
751
+ }
752
+ mark_value(context->timer_head, &worklist);
753
+ mark_value(context->atomics_waiter_head, &worklist);
754
+ mark_value(context->atomics_waiter_tail, &worklist);
755
+ mark_value(context->agent_broadcast_callback, &worklist);
756
+ mark_value(context->finalization_head, &worklist);
757
+ mark_value(context->finalization_tail, &worklist);
758
+ OseoValue *kept_objects = context->kept_objects;
759
+ for (size_t index = 0u;
760
+ index < context->kept_object_capacity;
761
+ index += 1u) {
762
+ mark_value(kept_objects[index], &worklist);
763
+ }
764
+ const OseoModuleTable *modules = context->module_table;
765
+ if (modules != NULL) {
766
+ for (size_t index = 0u; index < modules->count; index += 1u) {
767
+ const OseoModuleRecord *record = &modules->records[index];
768
+ mark_value(record->evaluator, &worklist);
769
+ mark_value(record->namespace_object, &worklist);
770
+ mark_value(record->top_level_capability, &worklist);
771
+ mark_value(record->evaluation_error, &worklist);
772
+ }
773
+ }
774
+ const OseoTailCall *tail_call = context->tail_call;
775
+ if (tail_call != NULL && tail_call->pending) {
776
+ mark_value(tail_call->callee, &worklist);
777
+ mark_value(tail_call->receiver, &worklist);
778
+ mark_value(tail_call->realm_record, &worklist);
779
+ mark_value(tail_call->this_cell, &worklist);
780
+ mark_value(tail_call->constructor_realm, &worklist);
781
+ mark_value(tail_call->caller, &worklist);
782
+ for (size_t index = 0u;
783
+ index < tail_call->argument_count;
784
+ index += 1u) {
785
+ mark_value(tail_call->arguments[index], &worklist);
786
+ }
787
+ }
788
+ trace_ephemeron_fixed_point(&worklist);
789
+ if (context->ephemeron_table_count != 0u ||
790
+ context->weak_reference_count != 0u ||
791
+ context->finalization_cell_count != 0u) {
792
+ clear_dead_weak_edges(context);
793
+ }
794
+ if (context->finalization_cell_count != 0u) {
795
+ schedule_finalization(context);
796
+ }
797
+ if (context->finalization_registry_count != 0u) {
798
+ compact_finalization_registries(context);
799
+ }
800
+ /* Marking is final here, so the table forgets exactly the strings the
801
+ * sweep below frees. */
802
+ oseo_internal_string_table_sweep(context);
803
+ OseoHeapObject **link = &context->objects;
804
+ while (*link != NULL) {
805
+ OseoHeapObject *object = *link;
806
+ if (object->marked) {
807
+ object->marked = false;
808
+ link = &object->next;
809
+ } else {
810
+ *link = object->next;
811
+ size_t *count = weak_record_count(context, object->kind);
812
+ if (count != NULL) *count -= 1u;
813
+ destroy_heap_object(object);
814
+ }
815
+ }
816
+ }
817
+
818
+ void *oseo_internal_allocate_heap_bytes(OseoContext *context, size_t size) {
819
+ if (context->collect_every_safepoint) oseo_collect(context);
820
+ context->allocation_attempts += 1u;
821
+ if (context->fail_allocation_at != 0u &&
822
+ context->allocation_attempts == context->fail_allocation_at) {
823
+ return NULL;
824
+ }
825
+ return malloc(size);
826
+ }
827
+
828
+ void *oseo_internal_allocate_work_bytes(OseoContext *context, size_t size) {
829
+ context->allocation_attempts += 1u;
830
+ if (context->fail_allocation_at != 0u &&
831
+ context->allocation_attempts == context->fail_allocation_at) {
832
+ return NULL;
833
+ }
834
+ return malloc(size);
835
+ }
836
+
837
+ void *oseo_internal_reallocate_work_bytes(
838
+ OseoContext *context,
839
+ void *block,
840
+ size_t size
841
+ ) {
842
+ context->allocation_attempts += 1u;
843
+ if (context->fail_allocation_at != 0u &&
844
+ context->allocation_attempts == context->fail_allocation_at) {
845
+ return NULL;
846
+ }
847
+ return realloc(block, size);
848
+ }
849
+
850
+ OseoResult oseo_internal_publish_heap(
851
+ OseoContext *context,
852
+ OseoHeapObject *object,
853
+ OseoHeapKind kind
854
+ ) {
855
+ uintptr_t address = (uintptr_t)object;
856
+ if (address == 0u || address > OSEO_PAYLOAD_MASK) {
857
+ free(object);
858
+ return failure(
859
+ context,
860
+ "OSEO3001",
861
+ "The host cannot represent a heap address in 48 bits."
862
+ );
863
+ }
864
+ size_t *count = weak_record_count(context, kind);
865
+ if (count != NULL && *count == SIZE_MAX) {
866
+ free(object);
867
+ return failure(context, "OSEO2001", "Too many weak heap records.");
868
+ }
869
+ object->next = context->objects;
870
+ object->trace_next = NULL;
871
+ object->ephemeron_pending = NULL;
872
+ object->kind = kind;
873
+ object->marked = false;
874
+ object->retained = false;
875
+ object->raw_json = false;
876
+ context->objects = object;
877
+ if (count != NULL) *count += 1u;
878
+ if (context->observe_specialization &&
879
+ kind != OSEO_HEAP_ENVIRONMENT && kind != OSEO_HEAP_CELL &&
880
+ kind != OSEO_HEAP_FUNCTION) {
881
+ context->allocations += 1u;
882
+ }
883
+ return normal(tagged(OSEO_TAG_HEAP, (uint64_t)address));
884
+ }
885
+
886
+ static bool has_heap_kind(OseoValue value, OseoHeapKind kind) {
887
+ return tag_of(value) == OSEO_TAG_HEAP && heap_object(value)->kind == kind;
888
+ }
889
+
890
+ OseoResult oseo_internal_ephemeron_table_create(OseoContext *context) {
891
+ OseoEphemeronTable *table =
892
+ oseo_internal_allocate_heap_bytes(context, sizeof(*table));
893
+ if (table == NULL) {
894
+ return failure(
895
+ context,
896
+ "OSEO2001",
897
+ "Ephemeron table allocation failed."
898
+ );
899
+ }
900
+ table->head = oseo_undefined();
901
+ table->tail = oseo_undefined();
902
+ table->live_count = 0u;
903
+ table->index = NULL;
904
+ table->index_capacity = 0u;
905
+ table->index_used = 0u;
906
+ return oseo_internal_publish_heap(
907
+ context,
908
+ &table->header,
909
+ OSEO_HEAP_EPHEMERON_TABLE
910
+ );
911
+ }
912
+
913
+ /*
914
+ * Keeps one free index slot for an insertion at a load factor, counting
915
+ * tombstones, of at most three quarters. The unmanaged allocation never
916
+ * collects, and a later collection only turns slots into tombstones, so
917
+ * the reservation survives the entry allocation that follows it.
918
+ */
919
+ static OseoResult ephemeron_index_reserve(
920
+ OseoContext *context,
921
+ OseoValue table_value
922
+ ) {
923
+ OseoEphemeronTable *table = ephemeron_table_object(table_value);
924
+ if (table->index_capacity != 0u &&
925
+ (table->index_used + 1u) <= table->index_capacity / 4u * 3u) {
926
+ return normal(table_value);
927
+ }
928
+ /* A rebuilt index is at most half full, so at least a quarter of its
929
+ * slots accept insertions before the next rebuild. */
930
+ size_t capacity = 8u;
931
+ while (capacity / 2u < table->live_count + 1u) {
932
+ if (capacity > SIZE_MAX / 2u / sizeof(OseoValue)) {
933
+ return failure(
934
+ context,
935
+ "OSEO2001",
936
+ "Ephemeron index is too large."
937
+ );
938
+ }
939
+ capacity *= 2u;
940
+ }
941
+ OseoValue *index = oseo_internal_allocate_work_bytes(
942
+ context,
943
+ capacity * sizeof(*index)
944
+ );
945
+ if (index == NULL) {
946
+ return failure(
947
+ context,
948
+ "OSEO2001",
949
+ "Ephemeron index allocation failed."
950
+ );
951
+ }
952
+ for (size_t slot = 0u; slot < capacity; slot += 1u) index[slot] = 0u;
953
+ size_t used = 0u;
954
+ for (OseoValue cursor = table->head;
955
+ tag_of(cursor) == OSEO_TAG_HEAP;
956
+ cursor = ephemeron_entry_object(cursor)->next) {
957
+ (void)ephemeron_index_insert(
958
+ index,
959
+ capacity,
960
+ ephemeron_entry_object(cursor)->key,
961
+ cursor
962
+ );
963
+ used += 1u;
964
+ }
965
+ free(table->index);
966
+ table->index = index;
967
+ table->index_capacity = capacity;
968
+ table->index_used = used;
969
+ return normal(table_value);
970
+ }
971
+
972
+ OseoResult oseo_internal_ephemeron_set(
973
+ OseoContext *context,
974
+ OseoValue table_value,
975
+ OseoValue key,
976
+ OseoValue value
977
+ ) {
978
+ if (!has_heap_kind(table_value, OSEO_HEAP_EPHEMERON_TABLE)) {
979
+ return failure(context, "OSEO2001", "Value is not an ephemeron table.");
980
+ }
981
+ if (tag_of(key) != OSEO_TAG_HEAP) {
982
+ return failure(
983
+ context,
984
+ "OSEO2001",
985
+ "Ephemeron key is not a heap value."
986
+ );
987
+ }
988
+ OseoValue *existing =
989
+ ephemeron_index_find(ephemeron_table_object(table_value), key);
990
+ if (existing != NULL) {
991
+ ephemeron_entry_object(*existing)->value = value;
992
+ return normal(table_value);
993
+ }
994
+
995
+ OseoValue roots[] = {table_value, key, value};
996
+ OseoRootFrame frame = {NULL, roots, 3u};
997
+ oseo_roots_push(context, &frame);
998
+ OseoResult reserved = ephemeron_index_reserve(context, roots[0]);
999
+ if (reserved.status != OSEO_STATUS_NORMAL) {
1000
+ oseo_roots_pop(context, &frame);
1001
+ return reserved;
1002
+ }
1003
+ OseoEphemeronEntry *entry =
1004
+ oseo_internal_allocate_heap_bytes(context, sizeof(*entry));
1005
+ if (entry == NULL) {
1006
+ oseo_roots_pop(context, &frame);
1007
+ return failure(
1008
+ context,
1009
+ "OSEO2001",
1010
+ "Ephemeron entry allocation failed."
1011
+ );
1012
+ }
1013
+ entry->next = oseo_undefined();
1014
+ entry->previous = oseo_undefined();
1015
+ entry->key = roots[1];
1016
+ entry->value = roots[2];
1017
+ OseoResult published = oseo_internal_publish_heap(
1018
+ context,
1019
+ &entry->header,
1020
+ OSEO_HEAP_EPHEMERON_ENTRY
1021
+ );
1022
+ if (published.status == OSEO_STATUS_NORMAL) {
1023
+ OseoEphemeronTable *table = ephemeron_table_object(roots[0]);
1024
+ if (has_heap_kind(table->tail, OSEO_HEAP_EPHEMERON_ENTRY)) {
1025
+ ephemeron_entry_object(table->tail)->next = published.value;
1026
+ entry->previous = table->tail;
1027
+ } else {
1028
+ table->head = published.value;
1029
+ }
1030
+ table->tail = published.value;
1031
+ table->live_count += 1u;
1032
+ if (ephemeron_index_insert(
1033
+ table->index,
1034
+ table->index_capacity,
1035
+ roots[1],
1036
+ published.value
1037
+ )) {
1038
+ table->index_used += 1u;
1039
+ }
1040
+ }
1041
+ oseo_roots_pop(context, &frame);
1042
+ return published.status == OSEO_STATUS_NORMAL
1043
+ ? normal(roots[0])
1044
+ : published;
1045
+ }
1046
+
1047
+ bool oseo_internal_ephemeron_get(
1048
+ OseoValue table_value,
1049
+ OseoValue key,
1050
+ OseoValue *value
1051
+ ) {
1052
+ if (!has_heap_kind(table_value, OSEO_HEAP_EPHEMERON_TABLE)) return false;
1053
+ OseoValue *slot =
1054
+ ephemeron_index_find(ephemeron_table_object(table_value), key);
1055
+ if (slot == NULL) return false;
1056
+ *value = ephemeron_entry_object(*slot)->value;
1057
+ return true;
1058
+ }
1059
+
1060
+ bool oseo_internal_ephemeron_delete(OseoValue table_value, OseoValue key) {
1061
+ if (!has_heap_kind(table_value, OSEO_HEAP_EPHEMERON_TABLE)) return false;
1062
+ OseoEphemeronTable *table = ephemeron_table_object(table_value);
1063
+ OseoValue *slot = ephemeron_index_find(table, key);
1064
+ if (slot == NULL) return false;
1065
+ OseoEphemeronEntry *entry = ephemeron_entry_object(*slot);
1066
+ *slot = OSEO_EPHEMERON_INDEX_TOMBSTONE;
1067
+ if (tag_of(entry->previous) == OSEO_TAG_HEAP) {
1068
+ ephemeron_entry_object(entry->previous)->next = entry->next;
1069
+ } else {
1070
+ table->head = entry->next;
1071
+ }
1072
+ if (tag_of(entry->next) == OSEO_TAG_HEAP) {
1073
+ ephemeron_entry_object(entry->next)->previous = entry->previous;
1074
+ } else {
1075
+ table->tail = entry->previous;
1076
+ }
1077
+ entry->next = oseo_undefined();
1078
+ entry->previous = oseo_undefined();
1079
+ entry->key = oseo_undefined();
1080
+ entry->value = oseo_undefined();
1081
+ if (table->live_count > 0u) table->live_count -= 1u;
1082
+ return true;
1083
+ }
1084
+
1085
+ size_t oseo_internal_ephemeron_live_count(OseoValue table_value) {
1086
+ if (!has_heap_kind(table_value, OSEO_HEAP_EPHEMERON_TABLE)) return 0u;
1087
+ return ephemeron_table_object(table_value)->live_count;
1088
+ }
1089
+
1090
+ OseoResult oseo_internal_weak_reference_create(
1091
+ OseoContext *context,
1092
+ OseoValue target
1093
+ ) {
1094
+ if (tag_of(target) != OSEO_TAG_HEAP) {
1095
+ return failure(context, "OSEO2001", "Weak target is not a heap value.");
1096
+ }
1097
+ OseoRootFrame frame = {NULL, &target, 1u};
1098
+ oseo_roots_push(context, &frame);
1099
+ OseoWeakReference *reference =
1100
+ oseo_internal_allocate_heap_bytes(context, sizeof(*reference));
1101
+ if (reference == NULL) {
1102
+ oseo_roots_pop(context, &frame);
1103
+ return failure(
1104
+ context,
1105
+ "OSEO2001",
1106
+ "Weak reference allocation failed."
1107
+ );
1108
+ }
1109
+ reference->target = target;
1110
+ OseoResult published = oseo_internal_publish_heap(
1111
+ context,
1112
+ &reference->header,
1113
+ OSEO_HEAP_WEAK_REFERENCE
1114
+ );
1115
+ oseo_roots_pop(context, &frame);
1116
+ return published;
1117
+ }
1118
+
1119
+ OseoValue oseo_internal_weak_reference_target(OseoValue reference) {
1120
+ if (!has_heap_kind(reference, OSEO_HEAP_WEAK_REFERENCE)) {
1121
+ return oseo_undefined();
1122
+ }
1123
+ return weak_reference_object(reference)->target;
1124
+ }
1125
+
1126
+ OseoResult oseo_internal_finalization_registry_create(
1127
+ OseoContext *context,
1128
+ OseoValue callback
1129
+ ) {
1130
+ OseoRootFrame frame = {NULL, &callback, 1u};
1131
+ oseo_roots_push(context, &frame);
1132
+ OseoFinalizationRegistry *registry =
1133
+ oseo_internal_allocate_heap_bytes(context, sizeof(*registry));
1134
+ if (registry == NULL) {
1135
+ oseo_roots_pop(context, &frame);
1136
+ return failure(
1137
+ context,
1138
+ "OSEO2001",
1139
+ "Finalization registry allocation failed."
1140
+ );
1141
+ }
1142
+ registry->callback = callback;
1143
+ registry->cell_head = oseo_undefined();
1144
+ registry->cell_tail = oseo_undefined();
1145
+ OseoResult published = oseo_internal_publish_heap(
1146
+ context,
1147
+ &registry->header,
1148
+ OSEO_HEAP_FINALIZATION_REGISTRY
1149
+ );
1150
+ oseo_roots_pop(context, &frame);
1151
+ return published;
1152
+ }
1153
+
1154
+ OseoResult oseo_internal_finalization_register(
1155
+ OseoContext *context,
1156
+ OseoValue registry_value,
1157
+ OseoValue target,
1158
+ OseoValue holdings
1159
+ ) {
1160
+ return oseo_internal_finalization_register_token(
1161
+ context,
1162
+ registry_value,
1163
+ target,
1164
+ holdings,
1165
+ oseo_undefined()
1166
+ );
1167
+ }
1168
+
1169
+ OseoResult oseo_internal_finalization_register_token(
1170
+ OseoContext *context,
1171
+ OseoValue registry_value,
1172
+ OseoValue target,
1173
+ OseoValue holdings,
1174
+ OseoValue unregister_token
1175
+ ) {
1176
+ if (!has_heap_kind(registry_value, OSEO_HEAP_FINALIZATION_REGISTRY)) {
1177
+ return failure(
1178
+ context,
1179
+ "OSEO2001",
1180
+ "Value is not a finalization registry."
1181
+ );
1182
+ }
1183
+ if (tag_of(target) != OSEO_TAG_HEAP || target == holdings) {
1184
+ return failure(context, "OSEO2001", "Finalization target is invalid.");
1185
+ }
1186
+ if (context->next_finalization_order == UINT64_MAX) {
1187
+ return failure(
1188
+ context,
1189
+ "OSEO2001",
1190
+ "Finalization registration limit exceeded."
1191
+ );
1192
+ }
1193
+ OseoValue roots[] = {registry_value, target, holdings, unregister_token};
1194
+ OseoRootFrame frame = {NULL, roots, 4u};
1195
+ oseo_roots_push(context, &frame);
1196
+ OseoFinalizationCell *cell =
1197
+ oseo_internal_allocate_heap_bytes(context, sizeof(*cell));
1198
+ if (cell == NULL) {
1199
+ oseo_roots_pop(context, &frame);
1200
+ return failure(
1201
+ context,
1202
+ "OSEO2001",
1203
+ "Finalization cell allocation failed."
1204
+ );
1205
+ }
1206
+ cell->next = oseo_undefined();
1207
+ cell->queue_next = oseo_undefined();
1208
+ cell->registry = roots[0];
1209
+ cell->target = roots[1];
1210
+ cell->holdings = roots[2];
1211
+ cell->unregister_token = roots[3];
1212
+ cell->registration_order = context->next_finalization_order;
1213
+ cell->queued = false;
1214
+ cell->processed = false;
1215
+ OseoResult published = oseo_internal_publish_heap(
1216
+ context,
1217
+ &cell->header,
1218
+ OSEO_HEAP_FINALIZATION_CELL
1219
+ );
1220
+ if (published.status == OSEO_STATUS_NORMAL) {
1221
+ OseoFinalizationRegistry *registry =
1222
+ finalization_registry_object(roots[0]);
1223
+ if (has_heap_kind(
1224
+ registry->cell_tail,
1225
+ OSEO_HEAP_FINALIZATION_CELL
1226
+ )) {
1227
+ finalization_cell_object(registry->cell_tail)->next =
1228
+ published.value;
1229
+ } else {
1230
+ registry->cell_head = published.value;
1231
+ }
1232
+ registry->cell_tail = published.value;
1233
+ context->next_finalization_order += 1u;
1234
+ }
1235
+ oseo_roots_pop(context, &frame);
1236
+ return published.status == OSEO_STATUS_NORMAL
1237
+ ? normal(roots[0])
1238
+ : published;
1239
+ }
1240
+
1241
+ bool oseo_internal_finalization_unregister(
1242
+ OseoContext *context,
1243
+ OseoValue registry_value,
1244
+ OseoValue unregister_token
1245
+ ) {
1246
+ if (!has_heap_kind(registry_value, OSEO_HEAP_FINALIZATION_REGISTRY) ||
1247
+ tag_of(unregister_token) != OSEO_TAG_HEAP) {
1248
+ return false;
1249
+ }
1250
+ bool removed = false;
1251
+ OseoValue cursor = finalization_registry_object(registry_value)->cell_head;
1252
+ while (has_heap_kind(cursor, OSEO_HEAP_FINALIZATION_CELL)) {
1253
+ OseoFinalizationCell *cell = finalization_cell_object(cursor);
1254
+ cursor = cell->next;
1255
+ if (cell->processed || cell->unregister_token != unregister_token) {
1256
+ continue;
1257
+ }
1258
+ cell->processed = true;
1259
+ cell->target = oseo_undefined();
1260
+ cell->holdings = oseo_undefined();
1261
+ cell->unregister_token = oseo_undefined();
1262
+ if (cell->queued && context->finalization_pending_count > 0u) {
1263
+ context->finalization_pending_count -= 1u;
1264
+ }
1265
+ removed = true;
1266
+ }
1267
+ return removed;
1268
+ }
1269
+
1270
+ /*
1271
+ * Unlinks and consumes the oldest queued record, restricted to `registry`
1272
+ * unless it is undefined. A record an unregister already consumed has
1273
+ * given back its pending count, so the scan unlinks it without a callback.
1274
+ */
1275
+ static bool take_queued_cleanup(
1276
+ OseoContext *context,
1277
+ OseoValue registry,
1278
+ OseoValue *registry_out,
1279
+ OseoValue *holdings
1280
+ ) {
1281
+ OseoValue previous = oseo_undefined();
1282
+ OseoValue cursor = context->finalization_head;
1283
+ while (has_heap_kind(cursor, OSEO_HEAP_FINALIZATION_CELL)) {
1284
+ OseoFinalizationCell *cell = finalization_cell_object(cursor);
1285
+ OseoValue next = cell->queue_next;
1286
+ bool matches = !cell->processed &&
1287
+ (tag_of(registry) == OSEO_TAG_UNDEFINED ||
1288
+ cell->registry == registry);
1289
+ if (!cell->processed && !matches) {
1290
+ previous = cursor;
1291
+ cursor = next;
1292
+ continue;
1293
+ }
1294
+ if (has_heap_kind(previous, OSEO_HEAP_FINALIZATION_CELL)) {
1295
+ finalization_cell_object(previous)->queue_next = next;
1296
+ } else {
1297
+ context->finalization_head = next;
1298
+ }
1299
+ if (context->finalization_tail == cursor) {
1300
+ context->finalization_tail = previous;
1301
+ }
1302
+ cell->queue_next = oseo_undefined();
1303
+ cell->queued = false;
1304
+ if (!matches) {
1305
+ cursor = next;
1306
+ continue;
1307
+ }
1308
+ cell->processed = true;
1309
+ *registry_out = cell->registry;
1310
+ *holdings = cell->holdings;
1311
+ cell->holdings = oseo_undefined();
1312
+ if (context->finalization_pending_count > 0u) {
1313
+ context->finalization_pending_count -= 1u;
1314
+ }
1315
+ return true;
1316
+ }
1317
+ return false;
1318
+ }
1319
+
1320
+ bool oseo_internal_finalization_take_cleanup(
1321
+ OseoContext *context,
1322
+ OseoValue *registry,
1323
+ OseoValue *holdings
1324
+ ) {
1325
+ return take_queued_cleanup(
1326
+ context,
1327
+ oseo_undefined(),
1328
+ registry,
1329
+ holdings
1330
+ );
1331
+ }
1332
+
1333
+ bool oseo_internal_finalization_take_registry_cleanup(
1334
+ OseoContext *context,
1335
+ OseoValue registry,
1336
+ OseoValue *holdings
1337
+ ) {
1338
+ OseoValue taken = oseo_undefined();
1339
+ return has_heap_kind(registry, OSEO_HEAP_FINALIZATION_REGISTRY) &&
1340
+ take_queued_cleanup(context, registry, &taken, holdings);
1341
+ }