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