@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,986 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /* Copyright (c) 2026 Hong Minhee; see ../MIT-LICENSE.txt. */
3
+
4
+ #include "runtime_internal.h"
5
+
6
+ #include <stdatomic.h>
7
+ #include <stdlib.h>
8
+ #include <string.h>
9
+
10
+ /*
11
+ * The GlobalSymbolRegistry is shared by every realm in one process. Each
12
+ * realm owns its Symbol heap values: `Symbol.for` resolves the key to one
13
+ * immortal entry here and then to the realm's single rooted representative
14
+ * of that entry. The entry is the identity, so `same_registered_symbol`
15
+ * joins two realms' representatives of one entry wherever a symbol identity
16
+ * rule applies, while `oseo_internal_local_symbol` replaces a registered
17
+ * symbol with the storing realm's own representative before a property key,
18
+ * a Map key, or a Set element keeps it, so every identity position a lookup
19
+ * probes is a value the probing realm owns. A value position keeps whatever
20
+ * representative reached it, and a representative outlives the realm that
21
+ * created it: `oseo_internal_retire_registered_symbols` moves it out of a
22
+ * dying heap rather than freeing it. A foreign representative is traced only
23
+ * while this realm's roots or stores hold it, and
24
+ * `oseo_internal_clear_heap_marks` keeps such a mark from outliving the
25
+ * realm that owns the value. Entries are never removed because a registered
26
+ * key stays observable for the agent's lifetime.
27
+ */
28
+ typedef struct OseoSymbolRegistryEntry {
29
+ size_t hash;
30
+ size_t length;
31
+ uint16_t units[];
32
+ } OseoSymbolRegistryEntry;
33
+
34
+ /*
35
+ * An open-addressed table of entry pointers, grown before it is half full,
36
+ * so a lookup takes an expected constant number of probes. The hash is
37
+ * deterministic, like every other runtime table, so keys chosen to collide
38
+ * degrade toward a linear scan rather than failing. The table and its
39
+ * entries change only under the lock.
40
+ */
41
+ static atomic_flag symbol_registry_lock = ATOMIC_FLAG_INIT;
42
+ static OseoSymbolRegistryEntry **symbol_registry;
43
+ static size_t symbol_registry_count;
44
+ static size_t symbol_registry_capacity;
45
+
46
+ static void lock_symbol_registry(void) {
47
+ while (atomic_flag_test_and_set_explicit(
48
+ &symbol_registry_lock,
49
+ memory_order_acquire
50
+ )) {}
51
+ }
52
+
53
+ static void unlock_symbol_registry(void) {
54
+ atomic_flag_clear_explicit(&symbol_registry_lock, memory_order_release);
55
+ }
56
+
57
+ static size_t symbol_key_hash(const uint16_t *units, size_t length) {
58
+ uint64_t hash = UINT64_C(14695981039346656037);
59
+ for (size_t index = 0u; index < length; index += 1u) {
60
+ hash ^= (uint64_t)units[index];
61
+ hash *= UINT64_C(1099511628211);
62
+ }
63
+ return (size_t)(hash ^ (hash >> 32u));
64
+ }
65
+
66
+ /* Doubles the process table; the caller holds the lock. */
67
+ static bool symbol_registry_grow(void) {
68
+ size_t capacity = symbol_registry_capacity == 0u
69
+ ? 64u
70
+ : symbol_registry_capacity;
71
+ if (symbol_registry_capacity != 0u) {
72
+ if (capacity > SIZE_MAX / 2u / sizeof(*symbol_registry)) return false;
73
+ capacity *= 2u;
74
+ }
75
+ OseoSymbolRegistryEntry **table = calloc(capacity, sizeof(*table));
76
+ if (table == NULL) return false;
77
+ for (size_t index = 0u; index < symbol_registry_capacity; index += 1u) {
78
+ OseoSymbolRegistryEntry *entry = symbol_registry[index];
79
+ if (entry == NULL) continue;
80
+ size_t slot = entry->hash & (capacity - 1u);
81
+ while (table[slot] != NULL) slot = (slot + 1u) & (capacity - 1u);
82
+ table[slot] = entry;
83
+ }
84
+ free(symbol_registry);
85
+ symbol_registry = table;
86
+ symbol_registry_capacity = capacity;
87
+ return true;
88
+ }
89
+
90
+ static OseoResult symbol_registry_find_or_create(
91
+ OseoContext *context,
92
+ OseoValue key,
93
+ const OseoSymbolRegistryEntry **entry
94
+ ) {
95
+ OseoString *string = string_object(key);
96
+ if (string->length >
97
+ (SIZE_MAX - sizeof(OseoSymbolRegistryEntry)) / sizeof(uint16_t)) {
98
+ return failure(context, "OSEO2001", "Symbol registry key is long.");
99
+ }
100
+ size_t hash = symbol_key_hash(string->units, string->length);
101
+ lock_symbol_registry();
102
+ if ((symbol_registry_count + 1u) * 2u > symbol_registry_capacity &&
103
+ !symbol_registry_grow()) {
104
+ unlock_symbol_registry();
105
+ return failure(
106
+ context,
107
+ "OSEO2001",
108
+ "Symbol registry allocation failed."
109
+ );
110
+ }
111
+ size_t mask = symbol_registry_capacity - 1u;
112
+ size_t slot = hash & mask;
113
+ for (OseoSymbolRegistryEntry *candidate = symbol_registry[slot];
114
+ candidate != NULL;
115
+ candidate = symbol_registry[slot]) {
116
+ if (candidate->hash == hash &&
117
+ candidate->length == string->length &&
118
+ (string->length == 0u || memcmp(
119
+ candidate->units,
120
+ string->units,
121
+ string->length * sizeof(uint16_t)
122
+ ) == 0)) {
123
+ *entry = candidate;
124
+ unlock_symbol_registry();
125
+ return normal(key);
126
+ }
127
+ slot = (slot + 1u) & mask;
128
+ }
129
+ size_t size = sizeof(OseoSymbolRegistryEntry) +
130
+ string->length * sizeof(uint16_t);
131
+ OseoSymbolRegistryEntry *created = malloc(size);
132
+ if (created == NULL) {
133
+ unlock_symbol_registry();
134
+ return failure(
135
+ context,
136
+ "OSEO2001",
137
+ "Symbol registry allocation failed."
138
+ );
139
+ }
140
+ created->hash = hash;
141
+ created->length = string->length;
142
+ if (string->length > 0u) {
143
+ memcpy(
144
+ created->units,
145
+ string->units,
146
+ string->length * sizeof(uint16_t)
147
+ );
148
+ }
149
+ symbol_registry[slot] = created;
150
+ symbol_registry_count += 1u;
151
+ *entry = created;
152
+ unlock_symbol_registry();
153
+ return normal(key);
154
+ }
155
+
156
+ /*
157
+ * Rehashes the realm's open-addressed representative table into twice its
158
+ * capacity. Empty slots hold undefined so the collector can mark every slot.
159
+ */
160
+ static bool registered_symbols_grow(OseoContext *context) {
161
+ size_t old_capacity = context->registered_symbol_capacity;
162
+ size_t capacity = old_capacity == 0u ? 16u : old_capacity;
163
+ if (old_capacity != 0u) {
164
+ if (capacity > SIZE_MAX / 2u / sizeof(OseoValue)) return false;
165
+ capacity *= 2u;
166
+ }
167
+ OseoValue *table = malloc(capacity * sizeof(OseoValue));
168
+ if (table == NULL) return false;
169
+ for (size_t index = 0u; index < capacity; index += 1u) {
170
+ table[index] = oseo_undefined();
171
+ }
172
+ for (size_t index = 0u; index < old_capacity; index += 1u) {
173
+ OseoValue symbol = context->registered_symbols[index];
174
+ if (tag_of(symbol) != OSEO_TAG_HEAP) continue;
175
+ const OseoSymbolRegistryEntry *entry =
176
+ symbol_object(symbol)->registry_entry;
177
+ size_t slot = entry->hash & (capacity - 1u);
178
+ while (tag_of(table[slot]) == OSEO_TAG_HEAP) {
179
+ slot = (slot + 1u) & (capacity - 1u);
180
+ }
181
+ table[slot] = symbol;
182
+ }
183
+ free(context->registered_symbols);
184
+ context->registered_symbols = table;
185
+ context->registered_symbol_capacity = capacity;
186
+ return true;
187
+ }
188
+
189
+ static OseoResult symbol_create(
190
+ OseoContext *context,
191
+ OseoValue description,
192
+ const OseoSymbolRegistryEntry *registry_entry
193
+ );
194
+
195
+ static OseoResult symbol_this_value(
196
+ OseoContext *context,
197
+ OseoValue receiver
198
+ ) {
199
+ if (is_symbol(receiver)) return normal(receiver);
200
+ if (is_object(receiver) && !is_proxy(receiver)) {
201
+ OseoOrdinaryObject *object = ordinary_object(receiver);
202
+ if (object->primitive_data && is_symbol(object->primitive_value)) {
203
+ return normal(object->primitive_value);
204
+ }
205
+ }
206
+ return oseo_internal_throw_error(
207
+ context,
208
+ OSEO_ERROR_TYPE,
209
+ "Symbol method receiver has no SymbolData."
210
+ );
211
+ }
212
+
213
+ /*
214
+ * This context's own representative of one registry entry, created here
215
+ * from the shared key when the context has not seen the entry before.
216
+ * The result is reachable from `registered_symbols`, which the collector
217
+ * marks as a root, so a caller may hold it across a later safepoint
218
+ * without rooting it.
219
+ */
220
+ static OseoResult context_representative(
221
+ OseoContext *context,
222
+ const OseoSymbolRegistryEntry *entry
223
+ ) {
224
+ if ((context->registered_symbol_count + 1u) * 2u >
225
+ context->registered_symbol_capacity &&
226
+ !registered_symbols_grow(context)) {
227
+ return failure(
228
+ context,
229
+ "OSEO2001",
230
+ "Symbol registry allocation failed."
231
+ );
232
+ }
233
+ size_t mask = context->registered_symbol_capacity - 1u;
234
+ size_t slot = entry->hash & mask;
235
+ while (tag_of(context->registered_symbols[slot]) == OSEO_TAG_HEAP) {
236
+ OseoValue candidate = context->registered_symbols[slot];
237
+ if (symbol_object(candidate)->registry_entry == entry) {
238
+ return normal(candidate);
239
+ }
240
+ slot = (slot + 1u) & mask;
241
+ }
242
+ OseoResult description = oseo_internal_allocate_string(
243
+ context,
244
+ entry->units,
245
+ entry->length
246
+ );
247
+ if (description.status != OSEO_STATUS_NORMAL) return description;
248
+ OseoValue slots[1] = {description.value};
249
+ OseoRootFrame frame = {NULL, slots, 1u};
250
+ oseo_roots_push(context, &frame);
251
+ OseoResult created = symbol_create(context, slots[0], entry);
252
+ oseo_roots_pop(context, &frame);
253
+ if (created.status != OSEO_STATUS_NORMAL) return created;
254
+ /*
255
+ * Another context may keep this representative in a value position
256
+ * that no localization reaches, so it and the description it holds
257
+ * outlive this context rather than being freed with it.
258
+ */
259
+ heap_object(created.value)->retained = true;
260
+ heap_object(slots[0])->retained = true;
261
+ /*
262
+ * Allocation may collect but never resizes this table, so the empty
263
+ * slot found before it is still the insertion point.
264
+ */
265
+ context->registered_symbols[slot] = created.value;
266
+ context->registered_symbol_count += 1u;
267
+ return created;
268
+ }
269
+
270
+ OseoResult oseo_internal_local_symbol(
271
+ OseoContext *context,
272
+ OseoValue value
273
+ ) {
274
+ if (!is_symbol(value)) return normal(value);
275
+ const OseoSymbolRegistryEntry *entry =
276
+ symbol_object(value)->registry_entry;
277
+ if (entry == NULL) return normal(value);
278
+ return context_representative(context, entry);
279
+ }
280
+
281
+ /*
282
+ * Representatives whose context is gone. A registered representative is the
283
+ * one heap value a context hands to another context, and the receiving
284
+ * context may keep it in any persistent store: an ordinary property value, a
285
+ * Map value, an array element, a closure slot, a promise result, a saved
286
+ * generator slot. The collector traces more than forty such value fields
287
+ * across twenty heap kinds, and every one of them accepts an arbitrary
288
+ * value, so localizing at each store could not be complete and would have to
289
+ * be repeated by every later component. Localizing covers the identity
290
+ * positions, a property key, a Map key, and a Set element, where a
291
+ * representative also decides a lookup; a value position keeps whatever
292
+ * representative it was given. Such a value stays valid because the
293
+ * representative outlives the context that created it: a destroyed context
294
+ * hands its representatives and their descriptions to this list instead of
295
+ * freeing them, and a symbol and its description are the whole closure,
296
+ * since a description is an immutable string with its units inline. The
297
+ * retained memory is one symbol and one string for each key a destroyed
298
+ * context registered, so it grows with the number of destroyed contexts
299
+ * that used the registry rather than with the number of keys: a program
300
+ * with one context retains one pair per key it registered, while an
301
+ * embedder that creates and destroys contexts in a loop retains one pair
302
+ * per context and key. That cost buys a per-context representative, which
303
+ * is what makes a context's own lookup tables hold only values it owns.
304
+ * The alternative, one process-owned representative per entry, would bound
305
+ * the retention by the entry count but would replace the per-context
306
+ * representative that decides identity here, so this component keeps the
307
+ * cost and states it. The list head keeps every retired object reachable
308
+ * rather than leaked. The `retained` header flag names exactly these
309
+ * objects, so retiring never depends on what a collection left behind, and
310
+ * a retired object leaves with its mark set, so no later collection traces
311
+ * it and no sweep list holds it.
312
+ */
313
+ static OseoHeapObject *retired_symbols;
314
+
315
+ void oseo_internal_retire_registered_symbols(OseoContext *context) {
316
+ OseoHeapObject *head = NULL;
317
+ OseoHeapObject *tail = NULL;
318
+ OseoHeapObject **link = &context->objects;
319
+ while (*link != NULL) {
320
+ OseoHeapObject *object = *link;
321
+ if (!object->retained) {
322
+ link = &object->next;
323
+ continue;
324
+ }
325
+ *link = object->next;
326
+ object->next = NULL;
327
+ object->trace_next = NULL;
328
+ object->ephemeron_pending = NULL;
329
+ object->marked = true;
330
+ if (tail == NULL) head = object; else tail->next = object;
331
+ tail = object;
332
+ }
333
+ if (tail == NULL) return;
334
+ lock_symbol_registry();
335
+ tail->next = retired_symbols;
336
+ retired_symbols = head;
337
+ unlock_symbol_registry();
338
+ }
339
+
340
+ static OseoResult symbol_for(
341
+ OseoContext *context,
342
+ size_t argument_count,
343
+ const OseoValue *arguments
344
+ ) {
345
+ OseoValue key = argument_count > 0u
346
+ ? arguments[0]
347
+ : oseo_undefined();
348
+ OseoRootFrame frame = {NULL, &key, 1u};
349
+ oseo_roots_push(context, &frame);
350
+ OseoResult result = oseo_internal_value_string(context, key);
351
+ key = result.value;
352
+ const OseoSymbolRegistryEntry *entry = NULL;
353
+ if (result.status == OSEO_STATUS_NORMAL) {
354
+ result = symbol_registry_find_or_create(context, key, &entry);
355
+ }
356
+ if (result.status == OSEO_STATUS_NORMAL) {
357
+ result = context_representative(context, entry);
358
+ }
359
+ oseo_roots_pop(context, &frame);
360
+ return result;
361
+ }
362
+
363
+ static OseoResult symbol_key_for(
364
+ OseoContext *context,
365
+ size_t argument_count,
366
+ const OseoValue *arguments
367
+ ) {
368
+ OseoValue symbol = argument_count > 0u
369
+ ? arguments[0]
370
+ : oseo_undefined();
371
+ if (!is_symbol(symbol)) {
372
+ return oseo_internal_throw_error(
373
+ context,
374
+ OSEO_ERROR_TYPE,
375
+ "Symbol.keyFor requires a Symbol."
376
+ );
377
+ }
378
+ const OseoSymbolRegistryEntry *entry =
379
+ symbol_object(symbol)->registry_entry;
380
+ if (entry == NULL) return normal(oseo_undefined());
381
+ return oseo_internal_allocate_string(context, entry->units, entry->length);
382
+ }
383
+
384
+ OseoResult oseo_internal_symbol_builtin_dispatch(
385
+ OseoContext *context,
386
+ size_t code_id,
387
+ OseoValue callee,
388
+ OseoValue receiver,
389
+ size_t argument_count,
390
+ const OseoValue *arguments,
391
+ OseoValue new_target
392
+ ) {
393
+ (void)callee;
394
+ bool constructing = tag_of(new_target) != OSEO_TAG_UNDEFINED;
395
+ if (code_id == OSEO_SYMBOL_CONSTRUCT_CODE_ID) {
396
+ if (constructing) {
397
+ return oseo_internal_throw_error(
398
+ context,
399
+ OSEO_ERROR_TYPE,
400
+ "Symbol is not a constructor."
401
+ );
402
+ }
403
+ OseoValue description_input = argument_count > 0u
404
+ ? arguments[0]
405
+ : oseo_undefined();
406
+ if (tag_of(description_input) == OSEO_TAG_UNDEFINED) {
407
+ return oseo_internal_symbol_create(context, oseo_undefined());
408
+ }
409
+ OseoResult result =
410
+ oseo_internal_value_string(context, description_input);
411
+ if (result.status == OSEO_STATUS_NORMAL) {
412
+ result = oseo_internal_symbol_create(context, result.value);
413
+ }
414
+ return result;
415
+ }
416
+ if (constructing) {
417
+ return oseo_internal_throw_error(
418
+ context,
419
+ OSEO_ERROR_TYPE,
420
+ "Symbol built-in method is not a constructor."
421
+ );
422
+ }
423
+ if (code_id == OSEO_SYMBOL_FOR_CODE_ID) {
424
+ return symbol_for(context, argument_count, arguments);
425
+ }
426
+ if (code_id == OSEO_SYMBOL_KEY_FOR_CODE_ID) {
427
+ return symbol_key_for(context, argument_count, arguments);
428
+ }
429
+ if (code_id == OSEO_SYMBOL_TO_STRING_CODE_ID) {
430
+ OseoResult value = symbol_this_value(context, receiver);
431
+ return value.status == OSEO_STATUS_NORMAL
432
+ ? oseo_internal_symbol_text(context, value.value)
433
+ : value;
434
+ }
435
+ if (code_id == OSEO_SYMBOL_VALUE_OF_CODE_ID ||
436
+ code_id == OSEO_SYMBOL_TO_PRIMITIVE_CODE_ID) {
437
+ return symbol_this_value(context, receiver);
438
+ }
439
+ if (code_id == OSEO_SYMBOL_DESCRIPTION_GETTER_CODE_ID) {
440
+ OseoResult value = symbol_this_value(context, receiver);
441
+ return value.status == OSEO_STATUS_NORMAL
442
+ ? normal(symbol_object(value.value)->description)
443
+ : value;
444
+ }
445
+ return oseo_unknown_function(context, code_id);
446
+ }
447
+
448
+ /*
449
+ * Symbol values: unique heap primitives with an optional description,
450
+ * the lazily created Symbol intrinsic, and the well-known symbols
451
+ * stored on it.
452
+ */
453
+
454
+ static OseoResult symbol_create(
455
+ OseoContext *context,
456
+ OseoValue description,
457
+ const OseoSymbolRegistryEntry *registry_entry
458
+ ) {
459
+ OseoValue slots[1] = {description};
460
+ OseoRootFrame frame = {NULL, slots, 1u};
461
+ oseo_roots_push(context, &frame);
462
+ OseoSymbol *symbol =
463
+ oseo_internal_allocate_heap_bytes(context, sizeof(*symbol));
464
+ oseo_roots_pop(context, &frame);
465
+ if (symbol == NULL) {
466
+ return failure(context, "OSEO2001", "Symbol allocation failed.");
467
+ }
468
+ symbol->description = slots[0];
469
+ symbol->registry_entry = registry_entry;
470
+ return oseo_internal_publish_heap(
471
+ context,
472
+ &symbol->header,
473
+ OSEO_HEAP_SYMBOL
474
+ );
475
+ }
476
+
477
+ OseoResult oseo_internal_symbol_create(
478
+ OseoContext *context,
479
+ OseoValue description
480
+ ) {
481
+ return symbol_create(context, description, NULL);
482
+ }
483
+
484
+ /* Render "Symbol(description)" for console output and diagnostics. */
485
+ OseoResult oseo_internal_symbol_text(
486
+ OseoContext *context,
487
+ OseoValue symbol
488
+ ) {
489
+ if (!is_symbol(symbol)) {
490
+ return failure(context, "OSEO2001", "Value is not a symbol.");
491
+ }
492
+ OseoValue description = symbol_object(symbol)->description;
493
+ size_t description_length = is_string(description)
494
+ ? string_object(description)->length
495
+ : 0u;
496
+ if (description_length > SIZE_MAX / sizeof(uint16_t) - 8u) {
497
+ return failure(context, "OSEO2001", "String allocation is too large.");
498
+ }
499
+ size_t length = description_length + 8u;
500
+ OseoResult valid = oseo_internal_validate_string_length(context, length);
501
+ if (valid.status != OSEO_STATUS_NORMAL) return valid;
502
+ uint16_t *units = malloc(length * sizeof(uint16_t));
503
+ if (units == NULL) {
504
+ return failure(context, "OSEO2001", "String allocation failed.");
505
+ }
506
+ static const char prefix[] = "Symbol(";
507
+ for (size_t index = 0u; index < 7u; index += 1u) {
508
+ units[index] = (uint16_t)(unsigned char)prefix[index];
509
+ }
510
+ if (description_length > 0u) {
511
+ memcpy(
512
+ units + 7u,
513
+ string_object(description)->units,
514
+ description_length * sizeof(uint16_t)
515
+ );
516
+ }
517
+ units[length - 1u] = ')';
518
+ OseoValue slots[1] = {symbol};
519
+ OseoRootFrame frame = {NULL, slots, 1u};
520
+ oseo_roots_push(context, &frame);
521
+ OseoResult result = oseo_internal_allocate_string(context, units, length);
522
+ oseo_roots_pop(context, &frame);
523
+ free(units);
524
+ return result;
525
+ }
526
+
527
+ /*
528
+ * SetFunctionName for a symbol key: "[description]" for a described
529
+ * symbol, or the empty string for a descriptionless symbol.
530
+ */
531
+ OseoResult oseo_internal_symbol_name(
532
+ OseoContext *context,
533
+ OseoValue symbol
534
+ ) {
535
+ if (!is_symbol(symbol)) {
536
+ return failure(context, "OSEO2001", "Value is not a symbol.");
537
+ }
538
+ OseoValue description = symbol_object(symbol)->description;
539
+ /* SetFunctionName leaves a descriptionless symbol key nameless. */
540
+ if (!is_string(description)) {
541
+ return oseo_internal_allocate_string(context, NULL, 0u);
542
+ }
543
+ size_t description_length = string_object(description)->length;
544
+ if (description_length > SIZE_MAX / sizeof(uint16_t) - 2u) {
545
+ return failure(context, "OSEO2001", "String allocation is too large.");
546
+ }
547
+ size_t length = description_length + 2u;
548
+ OseoResult valid = oseo_internal_validate_string_length(context, length);
549
+ if (valid.status != OSEO_STATUS_NORMAL) return valid;
550
+ uint16_t *units = malloc(length * sizeof(uint16_t));
551
+ if (units == NULL) {
552
+ return failure(context, "OSEO2001", "String allocation failed.");
553
+ }
554
+ units[0] = '[';
555
+ if (description_length > 0u) {
556
+ memcpy(
557
+ units + 1u,
558
+ string_object(description)->units,
559
+ description_length * sizeof(uint16_t)
560
+ );
561
+ }
562
+ units[length - 1u] = ']';
563
+ OseoValue slots[1] = {symbol};
564
+ OseoRootFrame frame = {NULL, slots, 1u};
565
+ oseo_roots_push(context, &frame);
566
+ OseoResult result = oseo_internal_allocate_string(context, units, length);
567
+ oseo_roots_pop(context, &frame);
568
+ free(units);
569
+ return result;
570
+ }
571
+
572
+ static OseoResult define_symbol_property(
573
+ OseoContext *context,
574
+ OseoValue target,
575
+ const char *name,
576
+ OseoValue value,
577
+ OseoPropertyAttributes attributes
578
+ ) {
579
+ size_t name_length = strlen(name);
580
+ uint16_t units[20];
581
+ if (name_length > sizeof(units) / sizeof(*units)) {
582
+ return failure(context, "OSEO2001", "Symbol property name is long.");
583
+ }
584
+ for (size_t index = 0u; index < name_length; index += 1u) {
585
+ units[index] = (uint16_t)(unsigned char)name[index];
586
+ }
587
+ OseoValue slots[3] = {target, value, oseo_undefined()};
588
+ OseoRootFrame frame = {NULL, slots, 3u};
589
+ oseo_roots_push(context, &frame);
590
+ OseoResult result = oseo_string_from_units(context, units, name_length);
591
+ if (result.status == OSEO_STATUS_NORMAL) {
592
+ slots[2] = result.value;
593
+ result = oseo_object_define(
594
+ context,
595
+ slots[0],
596
+ slots[2],
597
+ slots[1],
598
+ attributes
599
+ );
600
+ }
601
+ oseo_roots_pop(context, &frame);
602
+ return result;
603
+ }
604
+
605
+ static OseoResult create_symbol_builtin(
606
+ OseoContext *context,
607
+ size_t code_id,
608
+ const char *name,
609
+ size_t length,
610
+ OseoFunctionNamePrefix prefix
611
+ ) {
612
+ size_t name_length = strlen(name);
613
+ if (name_length > 31u) {
614
+ return failure(context, "OSEO2001", "Built-in name is too long.");
615
+ }
616
+ uint16_t units[31];
617
+ for (size_t index = 0u; index < name_length; index += 1u) {
618
+ units[index] = (uint16_t)(unsigned char)name[index];
619
+ }
620
+ OseoValue environment = oseo_undefined();
621
+ OseoRootFrame frame = {NULL, &environment, 1u};
622
+ oseo_roots_push(context, &frame);
623
+ OseoResult result = oseo_environment_create(context, 0u);
624
+ environment = result.value;
625
+ if (result.status == OSEO_STATUS_NORMAL) {
626
+ /*
627
+ * Only the constructor has [[Construct]]: IsConstructor observes it,
628
+ * while dispatch still rejects every construction before conversion.
629
+ */
630
+ OseoFunctionKind kind = code_id == OSEO_SYMBOL_CONSTRUCT_CODE_ID
631
+ ? OSEO_FUNCTION_ORDINARY
632
+ : OSEO_FUNCTION_INTERNAL;
633
+ result = oseo_function_create(
634
+ context,
635
+ code_id,
636
+ environment,
637
+ units,
638
+ name_length,
639
+ length,
640
+ kind,
641
+ oseo_undefined(),
642
+ oseo_undefined(),
643
+ prefix
644
+ );
645
+ }
646
+ oseo_roots_pop(context, &frame);
647
+ return result;
648
+ }
649
+
650
+ static OseoResult define_symbol_accessor(
651
+ OseoContext *context,
652
+ OseoValue object,
653
+ const char *name,
654
+ OseoValue getter
655
+ ) {
656
+ OseoValue slots[3] = {object, getter, oseo_undefined()};
657
+ OseoRootFrame frame = {NULL, slots, 3u};
658
+ oseo_roots_push(context, &frame);
659
+ OseoResult result = oseo_internal_ascii_string(context, name);
660
+ slots[2] = result.value;
661
+ if (result.status == OSEO_STATUS_NORMAL) {
662
+ result = oseo_object_define_accessor(
663
+ context,
664
+ slots[0],
665
+ slots[2],
666
+ slots[1],
667
+ oseo_undefined(),
668
+ true,
669
+ false,
670
+ (OseoPropertyAttributes){true, false, false, true}
671
+ );
672
+ }
673
+ oseo_roots_pop(context, &frame);
674
+ return result;
675
+ }
676
+
677
+ static OseoResult symbol_intrinsic_create(OseoContext *context) {
678
+ static const char *const well_known_names[OSEO_WELL_KNOWN_SYMBOL_COUNT] = {
679
+ "asyncIterator",
680
+ "hasInstance",
681
+ "isConcatSpreadable",
682
+ "iterator",
683
+ "match",
684
+ "matchAll",
685
+ "replace",
686
+ "search",
687
+ "species",
688
+ "split",
689
+ "toPrimitive",
690
+ "toStringTag",
691
+ "unscopables",
692
+ "asyncDispose",
693
+ "dispose",
694
+ };
695
+ static const char *const well_known_descriptions[
696
+ OSEO_WELL_KNOWN_SYMBOL_COUNT
697
+ ] = {
698
+ "Symbol.asyncIterator",
699
+ "Symbol.hasInstance",
700
+ "Symbol.isConcatSpreadable",
701
+ "Symbol.iterator",
702
+ "Symbol.match",
703
+ "Symbol.matchAll",
704
+ "Symbol.replace",
705
+ "Symbol.search",
706
+ "Symbol.species",
707
+ "Symbol.split",
708
+ "Symbol.toPrimitive",
709
+ "Symbol.toStringTag",
710
+ "Symbol.unscopables",
711
+ "Symbol.asyncDispose",
712
+ "Symbol.dispose",
713
+ };
714
+ OseoValue *marker = &context->realm->intrinsics[
715
+ OSEO_INTRINSIC_SYMBOL_DESCRIPTION_GETTER
716
+ ];
717
+ if (tag_of(*marker) == OSEO_TAG_UNINITIALIZED) {
718
+ return failure(
719
+ context,
720
+ "OSEO2001",
721
+ "The Symbol intrinsic cluster is already being built."
722
+ );
723
+ }
724
+ if (tag_of(*marker) != OSEO_TAG_UNDEFINED) {
725
+ return normal(context->realm->intrinsics[OSEO_INTRINSIC_SYMBOL]);
726
+ }
727
+ *marker = oseo_uninitialized();
728
+ size_t entry_allocations = context->allocations;
729
+ OseoRootFrame frame = {NULL, NULL, 0u};
730
+ OseoResult result = oseo_roots_allocate(context, &frame, 5u);
731
+ if (result.status != OSEO_STATUS_NORMAL) {
732
+ *marker = oseo_undefined();
733
+ return result;
734
+ }
735
+ for (size_t index = 0u;
736
+ result.status == OSEO_STATUS_NORMAL &&
737
+ index < OSEO_WELL_KNOWN_SYMBOL_COUNT;
738
+ index += 1u) {
739
+ /*
740
+ * A failed build leaves the symbols it created in place, and an
741
+ * intrinsic materialized under it may already key a method by one
742
+ * of them, so a retry reuses an identity that is already
743
+ * observable rather than replacing it.
744
+ */
745
+ if (tag_of(context->well_known_symbols[index]) !=
746
+ OSEO_TAG_UNDEFINED) {
747
+ continue;
748
+ }
749
+ const char *description = well_known_descriptions[index];
750
+ size_t description_length = strlen(description);
751
+ uint16_t units[32];
752
+ if (description_length > sizeof(units) / sizeof(*units)) {
753
+ result = failure(
754
+ context,
755
+ "OSEO2001",
756
+ "Well-known symbol description is long."
757
+ );
758
+ break;
759
+ }
760
+ for (size_t unit = 0u; unit < description_length; unit += 1u) {
761
+ units[unit] = (uint16_t)(unsigned char)description[unit];
762
+ }
763
+ result = oseo_string_from_units(context, units, description_length);
764
+ frame.slots[2] = result.value;
765
+ if (result.status != OSEO_STATUS_NORMAL) break;
766
+ result = oseo_internal_symbol_create(context, frame.slots[2]);
767
+ frame.slots[2] = result.value;
768
+ if (result.status != OSEO_STATUS_NORMAL) break;
769
+ context->well_known_symbols[index] = frame.slots[2];
770
+ }
771
+ if (result.status == OSEO_STATUS_NORMAL) {
772
+ result = create_symbol_builtin(
773
+ context,
774
+ OSEO_SYMBOL_CONSTRUCT_CODE_ID,
775
+ "Symbol",
776
+ 0u,
777
+ OSEO_FUNCTION_NAME_PREFIX_NONE
778
+ );
779
+ frame.slots[0] = result.value;
780
+ }
781
+ if (result.status == OSEO_STATUS_NORMAL) {
782
+ /*
783
+ * The ordinary kind synthesizes the fixed `prototype` property, so
784
+ * only its writability needs to match the specified descriptor.
785
+ */
786
+ OseoFunction *constructor = function_object(frame.slots[0]);
787
+ constructor->prototype_writable = false;
788
+ frame.slots[1] = constructor->prototype_object;
789
+ context->realm->intrinsics[OSEO_INTRINSIC_SYMBOL] = frame.slots[0];
790
+ context->realm->intrinsics[OSEO_INTRINSIC_SYMBOL_PROTOTYPE] =
791
+ frame.slots[1];
792
+ }
793
+ for (size_t index = 0u;
794
+ result.status == OSEO_STATUS_NORMAL &&
795
+ index < OSEO_WELL_KNOWN_SYMBOL_COUNT;
796
+ index += 1u) {
797
+ frame.slots[2] = context->well_known_symbols[index];
798
+ result = define_symbol_property(
799
+ context,
800
+ frame.slots[0],
801
+ well_known_names[index],
802
+ frame.slots[2],
803
+ (OseoPropertyAttributes){false, false, false, false}
804
+ );
805
+ }
806
+ static const size_t static_codes[] = {
807
+ OSEO_SYMBOL_FOR_CODE_ID,
808
+ OSEO_SYMBOL_KEY_FOR_CODE_ID,
809
+ };
810
+ static const OseoIntrinsic static_intrinsics[] = {
811
+ OSEO_INTRINSIC_SYMBOL_FOR,
812
+ OSEO_INTRINSIC_SYMBOL_KEY_FOR,
813
+ };
814
+ static const char *const static_names[] = {"for", "keyFor"};
815
+ for (size_t index = 0u;
816
+ result.status == OSEO_STATUS_NORMAL && index < 2u;
817
+ index += 1u) {
818
+ result = create_symbol_builtin(
819
+ context,
820
+ static_codes[index],
821
+ static_names[index],
822
+ 1u,
823
+ OSEO_FUNCTION_NAME_PREFIX_NONE
824
+ );
825
+ frame.slots[2] = result.value;
826
+ if (result.status == OSEO_STATUS_NORMAL) {
827
+ context->realm->intrinsics[static_intrinsics[index]] =
828
+ frame.slots[2];
829
+ result = define_symbol_property(
830
+ context,
831
+ frame.slots[0],
832
+ static_names[index],
833
+ frame.slots[2],
834
+ (OseoPropertyAttributes){true, false, true, false}
835
+ );
836
+ }
837
+ }
838
+ if (result.status == OSEO_STATUS_NORMAL) {
839
+ result = define_symbol_property(
840
+ context,
841
+ frame.slots[1],
842
+ "constructor",
843
+ frame.slots[0],
844
+ (OseoPropertyAttributes){true, false, true, false}
845
+ );
846
+ }
847
+ static const size_t method_codes[] = {
848
+ OSEO_SYMBOL_TO_STRING_CODE_ID,
849
+ OSEO_SYMBOL_VALUE_OF_CODE_ID,
850
+ };
851
+ static const OseoIntrinsic method_intrinsics[] = {
852
+ OSEO_INTRINSIC_SYMBOL_TO_STRING,
853
+ OSEO_INTRINSIC_SYMBOL_VALUE_OF,
854
+ };
855
+ static const char *const method_names[] = {"toString", "valueOf"};
856
+ for (size_t index = 0u;
857
+ result.status == OSEO_STATUS_NORMAL && index < 2u;
858
+ index += 1u) {
859
+ result = create_symbol_builtin(
860
+ context,
861
+ method_codes[index],
862
+ method_names[index],
863
+ 0u,
864
+ OSEO_FUNCTION_NAME_PREFIX_NONE
865
+ );
866
+ frame.slots[2] = result.value;
867
+ if (result.status == OSEO_STATUS_NORMAL) {
868
+ context->realm->intrinsics[method_intrinsics[index]] =
869
+ frame.slots[2];
870
+ result = define_symbol_property(
871
+ context,
872
+ frame.slots[1],
873
+ method_names[index],
874
+ frame.slots[2],
875
+ (OseoPropertyAttributes){true, false, true, false}
876
+ );
877
+ }
878
+ }
879
+ if (result.status == OSEO_STATUS_NORMAL) {
880
+ result = create_symbol_builtin(
881
+ context,
882
+ OSEO_SYMBOL_DESCRIPTION_GETTER_CODE_ID,
883
+ "description",
884
+ 0u,
885
+ OSEO_FUNCTION_NAME_PREFIX_GET
886
+ );
887
+ frame.slots[2] = result.value;
888
+ }
889
+ if (result.status == OSEO_STATUS_NORMAL) {
890
+ result = define_symbol_accessor(
891
+ context,
892
+ frame.slots[1],
893
+ "description",
894
+ frame.slots[2]
895
+ );
896
+ }
897
+ if (result.status == OSEO_STATUS_NORMAL) {
898
+ frame.slots[3] =
899
+ context->well_known_symbols[OSEO_WELL_KNOWN_TO_STRING_TAG];
900
+ result = oseo_internal_ascii_string(context, "Symbol");
901
+ frame.slots[4] = result.value;
902
+ }
903
+ if (result.status == OSEO_STATUS_NORMAL) {
904
+ result = oseo_object_define(
905
+ context,
906
+ frame.slots[1],
907
+ frame.slots[3],
908
+ frame.slots[4],
909
+ (OseoPropertyAttributes){true, false, false, false}
910
+ );
911
+ }
912
+ if (result.status == OSEO_STATUS_NORMAL) {
913
+ result = create_symbol_builtin(
914
+ context,
915
+ OSEO_SYMBOL_TO_PRIMITIVE_CODE_ID,
916
+ "[Symbol.toPrimitive]",
917
+ 1u,
918
+ OSEO_FUNCTION_NAME_PREFIX_NONE
919
+ );
920
+ frame.slots[4] = result.value;
921
+ }
922
+ if (result.status == OSEO_STATUS_NORMAL) {
923
+ context->realm->intrinsics[OSEO_INTRINSIC_SYMBOL_TO_PRIMITIVE] =
924
+ frame.slots[4];
925
+ frame.slots[3] =
926
+ context->well_known_symbols[OSEO_WELL_KNOWN_TO_PRIMITIVE];
927
+ result = oseo_object_define(
928
+ context,
929
+ frame.slots[1],
930
+ frame.slots[3],
931
+ frame.slots[4],
932
+ (OseoPropertyAttributes){true, false, false, false}
933
+ );
934
+ }
935
+ if (result.status == OSEO_STATUS_NORMAL) {
936
+ context->realm->intrinsics[OSEO_INTRINSIC_SYMBOL_DESCRIPTION_GETTER] =
937
+ frame.slots[2];
938
+ result.value = frame.slots[0];
939
+ if (context->observe_specialization) {
940
+ context->allocations = entry_allocations;
941
+ }
942
+ } else {
943
+ context->realm->intrinsics[OSEO_INTRINSIC_SYMBOL_PROTOTYPE] =
944
+ oseo_undefined();
945
+ context->realm->intrinsics[OSEO_INTRINSIC_SYMBOL] = oseo_undefined();
946
+ for (size_t intrinsic = OSEO_INTRINSIC_SYMBOL_FOR;
947
+ intrinsic <= OSEO_INTRINSIC_SYMBOL_DESCRIPTION_GETTER;
948
+ intrinsic += 1u) {
949
+ context->realm->intrinsics[intrinsic] = oseo_undefined();
950
+ }
951
+ /*
952
+ * The well-known symbols stay. Creating the constructor
953
+ * materializes %Function.prototype%, which keys its
954
+ * `[Symbol.hasInstance]` method by the symbol this build created,
955
+ * and that key cannot be rewritten, so replacing the symbol would
956
+ * leave the method unreachable through `Symbol.hasInstance`. They
957
+ * are edition-fixed values that no part of this cluster owns,
958
+ * and `oseo_internal_well_known_symbol` answers them without a
959
+ * constructor, so keeping them costs the retry nothing.
960
+ */
961
+ }
962
+ oseo_roots_release(context, &frame);
963
+ return result;
964
+ }
965
+
966
+ OseoResult oseo_symbol_intrinsic(OseoContext *context) {
967
+ OseoValue value = context->realm->intrinsics[OSEO_INTRINSIC_SYMBOL];
968
+ if (tag_of(value) != OSEO_TAG_UNDEFINED) {
969
+ return normal(value);
970
+ }
971
+ return symbol_intrinsic_create(context);
972
+ }
973
+
974
+ OseoResult oseo_internal_well_known_symbol(
975
+ OseoContext *context,
976
+ size_t index
977
+ ) {
978
+ if (index >= OSEO_WELL_KNOWN_SYMBOL_COUNT) {
979
+ return failure(context, "OSEO2001", "Unknown well-known symbol.");
980
+ }
981
+ OseoValue value = context->well_known_symbols[index];
982
+ if (tag_of(value) != OSEO_TAG_UNDEFINED) return normal(value);
983
+ OseoResult result = oseo_symbol_intrinsic(context);
984
+ if (result.status != OSEO_STATUS_NORMAL) return result;
985
+ return normal(context->well_known_symbols[index]);
986
+ }