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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE +674 -0
  2. package/MIT-LICENSE.txt +21 -0
  3. package/README.md +28 -2
  4. package/UNICODE-LICENSE.txt +39 -0
  5. package/dist/index.d.ts +7 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +269 -0
  8. package/dist/index.js.map +1 -0
  9. package/native/oseo_runtime.h +2618 -0
  10. package/native/runtime_agent.c +1717 -0
  11. package/native/runtime_arguments.c +379 -0
  12. package/native/runtime_array.c +5766 -0
  13. package/native/runtime_array_buffer.c +1748 -0
  14. package/native/runtime_async_generator.c +972 -0
  15. package/native/runtime_atomics.c +1283 -0
  16. package/native/runtime_bigint.c +1387 -0
  17. package/native/runtime_bigint_object.c +626 -0
  18. package/native/runtime_binding.c +1029 -0
  19. package/native/runtime_boolean.c +320 -0
  20. package/native/runtime_clock.c +181 -0
  21. package/native/runtime_clock_posix.c +599 -0
  22. package/native/runtime_core.c +488 -0
  23. package/native/runtime_data_view.c +1135 -0
  24. package/native/runtime_date.c +1997 -0
  25. package/native/runtime_descriptor.c +1104 -0
  26. package/native/runtime_disposal.c +1667 -0
  27. package/native/runtime_enumeration.c +636 -0
  28. package/native/runtime_error.c +997 -0
  29. package/native/runtime_event_loop.c +653 -0
  30. package/native/runtime_function.c +3667 -0
  31. package/native/runtime_generator.c +1120 -0
  32. package/native/runtime_internal.h +5578 -0
  33. package/native/runtime_iterator.c +5379 -0
  34. package/native/runtime_json.c +1992 -0
  35. package/native/runtime_map.c +1563 -0
  36. package/native/runtime_math.c +918 -0
  37. package/native/runtime_memory.c +1365 -0
  38. package/native/runtime_module.c +884 -0
  39. package/native/runtime_number.c +2077 -0
  40. package/native/runtime_object.c +423 -0
  41. package/native/runtime_object_builtin.c +3901 -0
  42. package/native/runtime_primitive.c +2268 -0
  43. package/native/runtime_process.c +744 -0
  44. package/native/runtime_promise.c +3378 -0
  45. package/native/runtime_property.c +815 -0
  46. package/native/runtime_proxy.c +1557 -0
  47. package/native/runtime_realm.c +435 -0
  48. package/native/runtime_reflect.c +648 -0
  49. package/native/runtime_regexp.c +3519 -0
  50. package/native/runtime_regexp_matcher.c +3559 -0
  51. package/native/runtime_regexp_symbol.c +1495 -0
  52. package/native/runtime_set.c +1775 -0
  53. package/native/runtime_string.c +2886 -0
  54. package/native/runtime_string_match.c +1185 -0
  55. package/native/runtime_symbol.c +1010 -0
  56. package/native/runtime_typed_array.c +5137 -0
  57. package/native/runtime_unicode_tables.h +14670 -0
  58. package/native/runtime_uri.c +700 -0
  59. package/native/runtime_weak_collection.c +1299 -0
  60. package/package.json +38 -3
@@ -0,0 +1,1775 @@
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 <math.h>
31
+ #include <stdlib.h>
32
+ #include <string.h>
33
+
34
+ /*
35
+ * Set construction, insertion-ordered element storage, core prototype
36
+ * methods, and %SetIteratorPrototype%.
37
+ */
38
+
39
+ static void initialize_ordinary(
40
+ OseoContext *context,
41
+ OseoOrdinaryObject *object,
42
+ OseoValue prototype
43
+ ) {
44
+ object->prototype = prototype;
45
+ object->properties = NULL;
46
+ object->property_capacity = 0u;
47
+ object->property_count = 0u;
48
+ oseo_internal_init_dense_index(object);
49
+ object->private_elements = NULL;
50
+ object->private_element_capacity = 0u;
51
+ object->private_element_count = 0u;
52
+ object->shape_id = context->next_shape_id;
53
+ context->next_shape_id += 1u;
54
+ object->array_length = 0u;
55
+ object->dictionary = false;
56
+ object->length_writable = false;
57
+ object->extensible = true;
58
+ object->module_namespace = false;
59
+ object->immutable_prototype = false;
60
+ object->global_object = false;
61
+ object->error_data = false;
62
+ object->number_data = false;
63
+ object->number_value = oseo_undefined();
64
+ object->primitive_data = false;
65
+ object->primitive_value = oseo_undefined();
66
+ object->primitive_wrapper_methods_initialized = false;
67
+ object->virtual_string_iterator = false;
68
+ object->virtual_string_iterator_configurable = false;
69
+ object->virtual_string_iterator_enumerable = false;
70
+ object->virtual_string_iterator_writable = false;
71
+ object->iterator_kind = OSEO_ITERATOR_NONE;
72
+ object->iterator_target = oseo_undefined();
73
+ object->iterator_index = 0u;
74
+ object->regexp_string_iterator = false;
75
+ object->regexp_iterator_regexp = oseo_undefined();
76
+ object->regexp_iterator_subject = oseo_undefined();
77
+ object->regexp_iterator_global = false;
78
+ object->regexp_iterator_unicode = false;
79
+ object->regexp_iterator_complete = false;
80
+ object->async_from_sync = false;
81
+ object->async_sync_iterator = oseo_undefined();
82
+ object->wrap_for_valid_iterator = false;
83
+ object->wrapped_iterator = oseo_undefined();
84
+ object->wrapped_next = oseo_undefined();
85
+ object->arguments_object = false;
86
+ object->mapped_arguments = false;
87
+ object->generator = NULL;
88
+ }
89
+
90
+ static OseoResult set_receiver(
91
+ OseoContext *context,
92
+ OseoValue receiver
93
+ ) {
94
+ if (is_set(receiver)) return normal(receiver);
95
+ return oseo_internal_throw_error(
96
+ context,
97
+ OSEO_ERROR_TYPE,
98
+ "Set method receiver is not a Set."
99
+ );
100
+ }
101
+
102
+ /* Set normalizes either zero sign to the realm's canonical positive zero. */
103
+ static OseoValue normalize_set_value(OseoValue value) {
104
+ return is_number(value) && number_value(value) == 0.0
105
+ ? oseo_number(0.0)
106
+ : value;
107
+ }
108
+
109
+ /* Searches the live slots by the shared SameValueZero, skipping the
110
+ * tombstones a prior delete or clear left behind. */
111
+ static size_t set_find(const OseoSet *set, OseoValue value) {
112
+ for (size_t index = 0u; index < set->element_count; index += 1u) {
113
+ if (set->elements[index].present &&
114
+ oseo_internal_same_value_zero(set->elements[index].value, value)) {
115
+ return index;
116
+ }
117
+ }
118
+ return SIZE_MAX;
119
+ }
120
+
121
+ /* Reserving element storage is a safepoint. The Set value stays rooted by
122
+ * every caller, and the record is reacquired after collection. */
123
+ static OseoResult grow_set(OseoContext *context, OseoValue set_value) {
124
+ OseoSet *set = set_object(set_value);
125
+ if (set->element_count < set->element_capacity) {
126
+ return normal(set_value);
127
+ }
128
+ size_t capacity = set->element_capacity == 0u
129
+ ? 4u
130
+ : set->element_capacity * 2u;
131
+ if (capacity < set->element_capacity ||
132
+ capacity > SIZE_MAX / sizeof(OseoSetElement)) {
133
+ return failure(context, "OSEO2001", "Set storage is too large.");
134
+ }
135
+ if (context->collect_every_safepoint) oseo_collect(context);
136
+ set = set_object(set_value);
137
+ context->allocation_attempts += 1u;
138
+ if (context->fail_allocation_at != 0u &&
139
+ context->allocation_attempts == context->fail_allocation_at) {
140
+ return failure(context, "OSEO2001", "Set storage allocation failed.");
141
+ }
142
+ OseoSetElement *elements = malloc(capacity * sizeof(*elements));
143
+ if (elements == NULL) {
144
+ return failure(context, "OSEO2001", "Set storage allocation failed.");
145
+ }
146
+ if (set->element_count > 0u) {
147
+ memcpy(
148
+ elements,
149
+ set->elements,
150
+ set->element_count * sizeof(*elements)
151
+ );
152
+ }
153
+ free(set->elements);
154
+ set->elements = elements;
155
+ set->element_capacity = capacity;
156
+ return normal(set_value);
157
+ }
158
+
159
+ static OseoResult set_allocate(
160
+ OseoContext *context,
161
+ OseoValue prototype
162
+ ) {
163
+ OseoValue slot = prototype;
164
+ OseoRootFrame frame = {NULL, &slot, 1u};
165
+ oseo_roots_push(context, &frame);
166
+ OseoSet *set = oseo_internal_allocate_heap_bytes(context, sizeof(*set));
167
+ if (set == NULL) {
168
+ oseo_roots_pop(context, &frame);
169
+ return failure(context, "OSEO2001", "Set allocation failed.");
170
+ }
171
+ initialize_ordinary(context, &set->ordinary, slot);
172
+ set->elements = NULL;
173
+ set->element_capacity = 0u;
174
+ set->element_count = 0u;
175
+ set->size = 0u;
176
+ OseoResult result = oseo_internal_publish_heap(
177
+ context,
178
+ &set->ordinary.header,
179
+ OSEO_HEAP_SET
180
+ );
181
+ oseo_roots_pop(context, &frame);
182
+ return result;
183
+ }
184
+
185
+ /* OrdinaryCreateFromConstructor(newTarget, "%Set.prototype%"). */
186
+ static OseoResult set_prototype_from_target(
187
+ OseoContext *context,
188
+ OseoValue new_target,
189
+ OseoValue *prototype
190
+ ) {
191
+ OseoRootFrame frame = {NULL, NULL, 0u};
192
+ OseoResult result = oseo_roots_allocate(context, &frame, 2u);
193
+ if (result.status != OSEO_STATUS_NORMAL) return result;
194
+ frame.slots[0] = new_target;
195
+ result = oseo_internal_constructor_prototype(context, frame.slots[0]);
196
+ frame.slots[1] = result.value;
197
+ if (result.status == OSEO_STATUS_NORMAL && !is_object(frame.slots[1])) {
198
+ result = oseo_internal_constructor_realm_default(
199
+ context,
200
+ frame.slots[0],
201
+ OSEO_INTRINSIC_SET_PROTOTYPE
202
+ );
203
+ frame.slots[1] = result.value;
204
+ }
205
+ if (result.status == OSEO_STATUS_NORMAL) *prototype = frame.slots[1];
206
+ oseo_roots_release(context, &frame);
207
+ return result;
208
+ }
209
+
210
+ /* Appends a value the caller has already found absent. Growing the vector
211
+ * is a safepoint, so both values are rooted across it. */
212
+ static OseoResult set_append(
213
+ OseoContext *context,
214
+ OseoValue set_value,
215
+ OseoValue value
216
+ ) {
217
+ OseoValue slots[2] = {set_value, value};
218
+ OseoRootFrame frame = {NULL, slots, 2u};
219
+ oseo_roots_push(context, &frame);
220
+ /* An element stores this realm's own representative of a registered
221
+ * symbol, so [[SetData]] never holds a value another realm owns. */
222
+ OseoResult result = oseo_internal_local_symbol(context, slots[1]);
223
+ if (result.status == OSEO_STATUS_NORMAL) {
224
+ slots[1] = result.value;
225
+ result = grow_set(context, slots[0]);
226
+ }
227
+ if (result.status == OSEO_STATUS_NORMAL) {
228
+ OseoSet *set = set_object(slots[0]);
229
+ OseoSetElement *element = &set->elements[set->element_count];
230
+ element->value = slots[1];
231
+ element->present = true;
232
+ set->element_count += 1u;
233
+ set->size += 1u;
234
+ result = normal(slots[0]);
235
+ }
236
+ oseo_roots_pop(context, &frame);
237
+ return result;
238
+ }
239
+
240
+ /* Replaces one live slot with a tombstone. */
241
+ static void set_remove_at(OseoSet *set, size_t index) {
242
+ set->elements[index].present = false;
243
+ set->elements[index].value = oseo_undefined();
244
+ set->size -= 1u;
245
+ }
246
+
247
+ static OseoResult set_add(
248
+ OseoContext *context,
249
+ OseoValue receiver,
250
+ OseoValue value
251
+ ) {
252
+ OseoResult checked = set_receiver(context, receiver);
253
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
254
+ value = normalize_set_value(value);
255
+ if (set_find(set_object(receiver), value) != SIZE_MAX) {
256
+ return normal(receiver);
257
+ }
258
+ return set_append(context, receiver, value);
259
+ }
260
+
261
+ static OseoResult set_clear(OseoContext *context, OseoValue receiver) {
262
+ OseoResult checked = set_receiver(context, receiver);
263
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
264
+ OseoSet *set = set_object(receiver);
265
+ for (size_t index = 0u; index < set->element_count; index += 1u) {
266
+ set->elements[index].present = false;
267
+ set->elements[index].value = oseo_undefined();
268
+ }
269
+ set->size = 0u;
270
+ return normal(oseo_undefined());
271
+ }
272
+
273
+ static OseoResult set_delete(
274
+ OseoContext *context,
275
+ OseoValue receiver,
276
+ OseoValue value
277
+ ) {
278
+ OseoResult checked = set_receiver(context, receiver);
279
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
280
+ OseoSet *set = set_object(receiver);
281
+ size_t index = set_find(set, value);
282
+ if (index == SIZE_MAX) return normal(oseo_boolean(false));
283
+ set_remove_at(set, index);
284
+ return normal(oseo_boolean(true));
285
+ }
286
+
287
+ static OseoResult set_has(
288
+ OseoContext *context,
289
+ OseoValue receiver,
290
+ OseoValue value
291
+ ) {
292
+ OseoResult checked = set_receiver(context, receiver);
293
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
294
+ bool present = set_find(set_object(receiver), value) != SIZE_MAX;
295
+ return normal(oseo_boolean(present));
296
+ }
297
+
298
+ static OseoResult set_size(OseoContext *context, OseoValue receiver) {
299
+ OseoResult checked = set_receiver(context, receiver);
300
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
301
+ return normal(oseo_number((double)set_object(receiver)->size));
302
+ }
303
+
304
+ static OseoResult set_iterator_create(
305
+ OseoContext *context,
306
+ OseoValue set,
307
+ OseoSetIteratorKind kind
308
+ ) {
309
+ OseoValue slots[2] = {set, oseo_undefined()};
310
+ OseoRootFrame frame = {NULL, slots, 2u};
311
+ oseo_roots_push(context, &frame);
312
+ OseoResult result = oseo_internal_intrinsic(
313
+ context,
314
+ OSEO_INTRINSIC_SET_ITERATOR_PROTOTYPE
315
+ );
316
+ slots[1] = result.value;
317
+ if (result.status == OSEO_STATUS_NORMAL) {
318
+ OseoSetIterator *iterator =
319
+ oseo_internal_allocate_heap_bytes(context, sizeof(*iterator));
320
+ if (iterator == NULL) {
321
+ result = failure(
322
+ context,
323
+ "OSEO2001",
324
+ "Set iterator allocation failed."
325
+ );
326
+ } else {
327
+ initialize_ordinary(context, &iterator->ordinary, slots[1]);
328
+ iterator->set = slots[0];
329
+ iterator->index = 0u;
330
+ iterator->kind = kind;
331
+ iterator->done = false;
332
+ result = oseo_internal_publish_heap(
333
+ context,
334
+ &iterator->ordinary.header,
335
+ OSEO_HEAP_SET_ITERATOR
336
+ );
337
+ }
338
+ }
339
+ oseo_roots_pop(context, &frame);
340
+ return result;
341
+ }
342
+
343
+ static OseoResult set_entries(OseoContext *context, OseoValue receiver) {
344
+ OseoResult checked = set_receiver(context, receiver);
345
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
346
+ return set_iterator_create(context, receiver, OSEO_SET_ITERATOR_ENTRY);
347
+ }
348
+
349
+ static OseoResult set_values(OseoContext *context, OseoValue receiver) {
350
+ OseoResult checked = set_receiver(context, receiver);
351
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
352
+ return set_iterator_create(context, receiver, OSEO_SET_ITERATOR_VALUE);
353
+ }
354
+
355
+ static OseoResult set_iterator_next(
356
+ OseoContext *context,
357
+ OseoValue receiver
358
+ ) {
359
+ if (!is_set_iterator(receiver)) {
360
+ return oseo_internal_throw_error(
361
+ context,
362
+ OSEO_ERROR_TYPE,
363
+ "Set iterator next requires a Set iterator receiver."
364
+ );
365
+ }
366
+ OseoValue slots[3] = {receiver, oseo_undefined(), oseo_undefined()};
367
+ OseoRootFrame frame = {NULL, slots, 3u};
368
+ oseo_roots_push(context, &frame);
369
+ OseoSetIterator *iterator = set_iterator_object(slots[0]);
370
+ if (iterator->done || !is_set(iterator->set)) {
371
+ iterator->done = true;
372
+ iterator->set = oseo_undefined();
373
+ oseo_roots_pop(context, &frame);
374
+ return oseo_internal_iterator_result(context, oseo_undefined(), true);
375
+ }
376
+ slots[1] = iterator->set;
377
+ OseoSet *set = set_object(slots[1]);
378
+ while (iterator->index < set->element_count) {
379
+ size_t index = iterator->index;
380
+ iterator->index += 1u;
381
+ if (!set->elements[index].present) continue;
382
+ slots[2] = set->elements[index].value;
383
+ OseoResult result = normal(slots[2]);
384
+ if (iterator->kind == OSEO_SET_ITERATOR_ENTRY) {
385
+ result = oseo_array_create(context, 0u);
386
+ if (result.status == OSEO_STATUS_NORMAL) {
387
+ OseoValue pair = result.value;
388
+ OseoRootFrame pair_frame = {NULL, &pair, 1u};
389
+ oseo_roots_push(context, &pair_frame);
390
+ result = oseo_array_append(context, pair, slots[2]);
391
+ if (result.status == OSEO_STATUS_NORMAL) {
392
+ result = oseo_array_append(context, pair, slots[2]);
393
+ }
394
+ oseo_roots_pop(context, &pair_frame);
395
+ if (result.status == OSEO_STATUS_NORMAL) slots[2] = pair;
396
+ }
397
+ }
398
+ if (result.status == OSEO_STATUS_NORMAL) {
399
+ result = oseo_internal_iterator_result(context, slots[2], false);
400
+ }
401
+ oseo_roots_pop(context, &frame);
402
+ return result;
403
+ }
404
+ iterator = set_iterator_object(slots[0]);
405
+ iterator->done = true;
406
+ iterator->set = oseo_undefined();
407
+ oseo_roots_pop(context, &frame);
408
+ return oseo_internal_iterator_result(context, oseo_undefined(), true);
409
+ }
410
+
411
+ static OseoResult set_for_each(
412
+ OseoContext *context,
413
+ OseoValue receiver,
414
+ size_t argument_count,
415
+ const OseoValue *arguments
416
+ ) {
417
+ OseoResult checked = set_receiver(context, receiver);
418
+ if (checked.status != OSEO_STATUS_NORMAL) return checked;
419
+ OseoValue callback = argument_count > 0u
420
+ ? arguments[0]
421
+ : oseo_undefined();
422
+ if (!is_callable(callback)) {
423
+ return oseo_internal_throw_error(
424
+ context,
425
+ OSEO_ERROR_TYPE,
426
+ "Set.prototype.forEach callback is not callable."
427
+ );
428
+ }
429
+ OseoRootFrame frame = {NULL, NULL, 0u};
430
+ OseoResult result = oseo_roots_allocate(context, &frame, 6u);
431
+ if (result.status != OSEO_STATUS_NORMAL) return result;
432
+ frame.slots[0] = receiver;
433
+ frame.slots[1] = callback;
434
+ frame.slots[2] = argument_count > 1u
435
+ ? arguments[1]
436
+ : oseo_undefined();
437
+ size_t index = 0u;
438
+ while (result.status == OSEO_STATUS_NORMAL) {
439
+ OseoSet *set = set_object(frame.slots[0]);
440
+ while (index < set->element_count &&
441
+ !set->elements[index].present) {
442
+ index += 1u;
443
+ }
444
+ if (index >= set->element_count) break;
445
+ frame.slots[3] = set->elements[index].value;
446
+ index += 1u;
447
+ frame.slots[4] = frame.slots[3];
448
+ frame.slots[5] = frame.slots[0];
449
+ result = oseo_call_function(
450
+ context,
451
+ frame.slots[1],
452
+ frame.slots[2],
453
+ 3u,
454
+ &frame.slots[3],
455
+ oseo_undefined()
456
+ );
457
+ }
458
+ if (result.status == OSEO_STATUS_NORMAL) {
459
+ result = normal(oseo_undefined());
460
+ }
461
+ oseo_roots_release(context, &frame);
462
+ return result;
463
+ }
464
+
465
+ /* AddEntriesFromIterable with the Set instance's observable `add` method. */
466
+ static OseoResult set_constructor(
467
+ OseoContext *context,
468
+ size_t argument_count,
469
+ const OseoValue *arguments,
470
+ OseoValue new_target
471
+ ) {
472
+ if (tag_of(new_target) == OSEO_TAG_UNDEFINED) {
473
+ return oseo_internal_throw_error(
474
+ context,
475
+ OSEO_ERROR_TYPE,
476
+ "Set must be called with new."
477
+ );
478
+ }
479
+ OseoRootFrame frame = {NULL, NULL, 0u};
480
+ OseoResult result = oseo_roots_allocate(context, &frame, 6u);
481
+ if (result.status != OSEO_STATUS_NORMAL) return result;
482
+ frame.slots[0] = new_target;
483
+ frame.slots[1] = argument_count > 0u
484
+ ? arguments[0]
485
+ : oseo_undefined();
486
+ result = set_prototype_from_target(
487
+ context,
488
+ frame.slots[0],
489
+ &frame.slots[2]
490
+ );
491
+ if (result.status == OSEO_STATUS_NORMAL) {
492
+ result = set_allocate(context, frame.slots[2]);
493
+ frame.slots[2] = result.value;
494
+ }
495
+ if (result.status != OSEO_STATUS_NORMAL || is_nullish(frame.slots[1])) {
496
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[2];
497
+ oseo_roots_release(context, &frame);
498
+ return result;
499
+ }
500
+ result = oseo_internal_ascii_string(context, "add");
501
+ frame.slots[3] = result.value;
502
+ if (result.status == OSEO_STATUS_NORMAL) {
503
+ result = oseo_object_get(context, frame.slots[2], frame.slots[3]);
504
+ frame.slots[3] = result.value;
505
+ }
506
+ if (result.status == OSEO_STATUS_NORMAL &&
507
+ !is_callable(frame.slots[3])) {
508
+ result = oseo_internal_throw_error(
509
+ context,
510
+ OSEO_ERROR_TYPE,
511
+ "The Set add property is not callable."
512
+ );
513
+ }
514
+ if (result.status == OSEO_STATUS_NORMAL) {
515
+ result = oseo_iterator_get(
516
+ context,
517
+ frame.slots[1],
518
+ &frame.slots[5]
519
+ );
520
+ frame.slots[4] = result.value;
521
+ }
522
+ bool done = false;
523
+ while (result.status == OSEO_STATUS_NORMAL && !done) {
524
+ result = oseo_iterator_next(
525
+ context,
526
+ frame.slots[4],
527
+ frame.slots[5],
528
+ &frame.slots[1],
529
+ &done
530
+ );
531
+ bool close_iterator = false;
532
+ if (result.status == OSEO_STATUS_NORMAL && !done) {
533
+ close_iterator = true;
534
+ result = oseo_call_function(
535
+ context,
536
+ frame.slots[3],
537
+ frame.slots[2],
538
+ 1u,
539
+ &frame.slots[1],
540
+ oseo_undefined()
541
+ );
542
+ }
543
+ if (close_iterator && result.status == OSEO_STATUS_THROW &&
544
+ !context->has_diagnostic && is_object(frame.slots[4])) {
545
+ OseoResult completion = result;
546
+ frame.slots[1] = result.value;
547
+ oseo_context_clear_language_error(context);
548
+ OseoResult closed = oseo_iterator_close(
549
+ context,
550
+ frame.slots[4],
551
+ true
552
+ );
553
+ result = closed.status == OSEO_STATUS_THROW &&
554
+ context->has_diagnostic
555
+ ? closed
556
+ : completion;
557
+ }
558
+ }
559
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[2];
560
+ oseo_roots_release(context, &frame);
561
+ return result;
562
+ }
563
+
564
+ /*
565
+ * Root frame layout shared by the seven methods that combine or compare a
566
+ * Set with a set-like argument. Every value one of them reads back after
567
+ * calling user code lives in one of these slots.
568
+ */
569
+ enum {
570
+ SET_OPERATION_RECEIVER = 0,
571
+ SET_OPERATION_OTHER = 1,
572
+ SET_OPERATION_HAS = 2,
573
+ SET_OPERATION_KEYS = 3,
574
+ SET_OPERATION_RESULT = 4,
575
+ SET_OPERATION_ITERATOR = 5,
576
+ SET_OPERATION_NEXT = 6,
577
+ SET_OPERATION_VALUE = 7,
578
+ SET_OPERATION_SCRATCH = 8,
579
+ SET_OPERATION_SLOT_COUNT = 9,
580
+ };
581
+
582
+ /* Reads one named property of the set-like argument into a rooted slot. */
583
+ static OseoResult set_record_property(
584
+ OseoContext *context,
585
+ OseoValue *slots,
586
+ const char *name,
587
+ size_t slot
588
+ ) {
589
+ OseoResult result = oseo_internal_ascii_string(context, name);
590
+ slots[slot] = result.value;
591
+ if (result.status == OSEO_STATUS_NORMAL) {
592
+ result = oseo_object_get(
593
+ context,
594
+ slots[SET_OPERATION_OTHER],
595
+ slots[slot]
596
+ );
597
+ slots[slot] = result.value;
598
+ }
599
+ return result;
600
+ }
601
+
602
+ /*
603
+ * GetSetRecord(other). The size is read and converted before `has` and
604
+ * `keys` are read, and ToIntegerOrInfinity keeps an infinite size, so the
605
+ * record's size is a non-negative integral double rather than a count.
606
+ */
607
+ static OseoResult set_record_get(
608
+ OseoContext *context,
609
+ OseoValue *slots,
610
+ double *size
611
+ ) {
612
+ if (!is_object(slots[SET_OPERATION_OTHER])) {
613
+ return oseo_internal_throw_error(
614
+ context,
615
+ OSEO_ERROR_TYPE,
616
+ "The Set method argument is not an object."
617
+ );
618
+ }
619
+ OseoResult result = set_record_property(
620
+ context,
621
+ slots,
622
+ "size",
623
+ SET_OPERATION_SCRATCH
624
+ );
625
+ if (result.status == OSEO_STATUS_NORMAL) {
626
+ result = oseo_internal_to_number(
627
+ context,
628
+ slots[SET_OPERATION_SCRATCH]
629
+ );
630
+ }
631
+ if (result.status != OSEO_STATUS_NORMAL) return result;
632
+ double number = number_value(result.value);
633
+ if (isnan(number)) {
634
+ return oseo_internal_throw_error(
635
+ context,
636
+ OSEO_ERROR_TYPE,
637
+ "The set-like size is not a number."
638
+ );
639
+ }
640
+ if (isfinite(number)) number = trunc(number);
641
+ if (number < 0.0) {
642
+ return oseo_internal_throw_error(
643
+ context,
644
+ OSEO_ERROR_RANGE,
645
+ "The set-like size is negative."
646
+ );
647
+ }
648
+ *size = number == 0.0 ? 0.0 : number;
649
+ result = set_record_property(context, slots, "has", SET_OPERATION_HAS);
650
+ if (result.status == OSEO_STATUS_NORMAL &&
651
+ !is_callable(slots[SET_OPERATION_HAS])) {
652
+ result = oseo_internal_throw_error(
653
+ context,
654
+ OSEO_ERROR_TYPE,
655
+ "The set-like has property is not callable."
656
+ );
657
+ }
658
+ if (result.status == OSEO_STATUS_NORMAL) {
659
+ result = set_record_property(
660
+ context,
661
+ slots,
662
+ "keys",
663
+ SET_OPERATION_KEYS
664
+ );
665
+ }
666
+ if (result.status == OSEO_STATUS_NORMAL &&
667
+ !is_callable(slots[SET_OPERATION_KEYS])) {
668
+ result = oseo_internal_throw_error(
669
+ context,
670
+ OSEO_ERROR_TYPE,
671
+ "The set-like keys property is not callable."
672
+ );
673
+ }
674
+ return result;
675
+ }
676
+
677
+ /*
678
+ * RequireInternalSlot(O, [[SetData]]) followed by GetSetRecord(other). On
679
+ * a normal completion the caller owns the allocated frame and releases it.
680
+ */
681
+ static OseoResult set_operation_begin(
682
+ OseoContext *context,
683
+ OseoValue receiver,
684
+ size_t argument_count,
685
+ const OseoValue *arguments,
686
+ OseoRootFrame *frame,
687
+ double *other_size
688
+ ) {
689
+ OseoResult result = set_receiver(context, receiver);
690
+ if (result.status != OSEO_STATUS_NORMAL) return result;
691
+ result = oseo_roots_allocate(context, frame, SET_OPERATION_SLOT_COUNT);
692
+ if (result.status != OSEO_STATUS_NORMAL) return result;
693
+ frame->slots[SET_OPERATION_RECEIVER] = receiver;
694
+ frame->slots[SET_OPERATION_OTHER] = argument_count > 0u
695
+ ? arguments[0]
696
+ : oseo_undefined();
697
+ result = set_record_get(context, frame->slots, other_size);
698
+ if (result.status != OSEO_STATUS_NORMAL) {
699
+ oseo_roots_release(context, frame);
700
+ }
701
+ return result;
702
+ }
703
+
704
+ /*
705
+ * GetIteratorFromMethod(other, keys). The next method is read once and
706
+ * reused by every step; a non-callable one throws when the first step
707
+ * calls it.
708
+ */
709
+ static OseoResult set_keys_iterator(OseoContext *context, OseoValue *slots) {
710
+ OseoResult result = oseo_call_function(
711
+ context,
712
+ slots[SET_OPERATION_KEYS],
713
+ slots[SET_OPERATION_OTHER],
714
+ 0u,
715
+ NULL,
716
+ oseo_undefined()
717
+ );
718
+ slots[SET_OPERATION_ITERATOR] = result.value;
719
+ if (result.status == OSEO_STATUS_NORMAL &&
720
+ !is_object(slots[SET_OPERATION_ITERATOR])) {
721
+ result = oseo_internal_throw_error(
722
+ context,
723
+ OSEO_ERROR_TYPE,
724
+ "The set-like keys iterator is not an object."
725
+ );
726
+ }
727
+ if (result.status == OSEO_STATUS_NORMAL) {
728
+ result = oseo_internal_ascii_string(context, "next");
729
+ slots[SET_OPERATION_SCRATCH] = result.value;
730
+ }
731
+ if (result.status == OSEO_STATUS_NORMAL) {
732
+ result = oseo_object_get(
733
+ context,
734
+ slots[SET_OPERATION_ITERATOR],
735
+ slots[SET_OPERATION_SCRATCH]
736
+ );
737
+ slots[SET_OPERATION_NEXT] = result.value;
738
+ }
739
+ return result;
740
+ }
741
+
742
+ /* IteratorStepValue over the captured keys iterator. */
743
+ static OseoResult set_keys_step(
744
+ OseoContext *context,
745
+ OseoValue *slots,
746
+ bool *done
747
+ ) {
748
+ return oseo_iterator_next(
749
+ context,
750
+ slots[SET_OPERATION_ITERATOR],
751
+ slots[SET_OPERATION_NEXT],
752
+ &slots[SET_OPERATION_VALUE],
753
+ done
754
+ );
755
+ }
756
+
757
+ /* Call(otherRec.[[Has]], otherRec.[[SetObject]], « value »), as a Boolean. */
758
+ static OseoResult set_other_has(
759
+ OseoContext *context,
760
+ OseoValue *slots,
761
+ bool *present
762
+ ) {
763
+ OseoResult result = oseo_call_function(
764
+ context,
765
+ slots[SET_OPERATION_HAS],
766
+ slots[SET_OPERATION_OTHER],
767
+ 1u,
768
+ &slots[SET_OPERATION_VALUE],
769
+ oseo_undefined()
770
+ );
771
+ *present = result.status == OSEO_STATUS_NORMAL &&
772
+ oseo_to_boolean(result.value);
773
+ return result;
774
+ }
775
+
776
+ /* OrdinaryObjectCreate(%Set.prototype%) with an empty element vector. The
777
+ * result never consults the receiver's constructor or Symbol.species. */
778
+ static OseoResult set_result_create(OseoContext *context, OseoValue *slots) {
779
+ OseoResult result = oseo_internal_intrinsic(
780
+ context,
781
+ OSEO_INTRINSIC_SET_PROTOTYPE
782
+ );
783
+ slots[SET_OPERATION_RESULT] = result.value;
784
+ if (result.status == OSEO_STATUS_NORMAL) {
785
+ result = set_allocate(context, slots[SET_OPERATION_RESULT]);
786
+ slots[SET_OPERATION_RESULT] = result.value;
787
+ }
788
+ return result;
789
+ }
790
+
791
+ /* Copies the receiver's live elements into the fresh result in order.
792
+ * Tombstones are not copied: the result is unobservable until returned,
793
+ * so its slot positions carry no meaning. */
794
+ static OseoResult set_result_copy(OseoContext *context, OseoValue *slots) {
795
+ OseoResult result = normal(slots[SET_OPERATION_RESULT]);
796
+ size_t count = set_object(slots[SET_OPERATION_RECEIVER])->element_count;
797
+ for (size_t index = 0u;
798
+ result.status == OSEO_STATUS_NORMAL && index < count;
799
+ index += 1u) {
800
+ OseoSet *source = set_object(slots[SET_OPERATION_RECEIVER]);
801
+ if (!source->elements[index].present) continue;
802
+ slots[SET_OPERATION_VALUE] = source->elements[index].value;
803
+ result = set_append(
804
+ context,
805
+ slots[SET_OPERATION_RESULT],
806
+ slots[SET_OPERATION_VALUE]
807
+ );
808
+ }
809
+ return result;
810
+ }
811
+
812
+ /* Releases an operation frame and reports either its abrupt completion or
813
+ * the given normal value. */
814
+ static OseoResult set_operation_end(
815
+ OseoContext *context,
816
+ OseoRootFrame *frame,
817
+ OseoResult result,
818
+ OseoValue value
819
+ ) {
820
+ if (result.status == OSEO_STATUS_NORMAL) result = normal(value);
821
+ oseo_roots_release(context, frame);
822
+ return result;
823
+ }
824
+
825
+ static OseoResult set_union(
826
+ OseoContext *context,
827
+ OseoValue receiver,
828
+ size_t argument_count,
829
+ const OseoValue *arguments
830
+ ) {
831
+ OseoRootFrame frame = {NULL, NULL, 0u};
832
+ double other_size = 0.0;
833
+ OseoResult result = set_operation_begin(
834
+ context,
835
+ receiver,
836
+ argument_count,
837
+ arguments,
838
+ &frame,
839
+ &other_size
840
+ );
841
+ if (result.status != OSEO_STATUS_NORMAL) return result;
842
+ OseoValue *slots = frame.slots;
843
+ result = set_keys_iterator(context, slots);
844
+ if (result.status == OSEO_STATUS_NORMAL) {
845
+ result = set_result_create(context, slots);
846
+ }
847
+ if (result.status == OSEO_STATUS_NORMAL) {
848
+ result = set_result_copy(context, slots);
849
+ }
850
+ bool done = false;
851
+ while (result.status == OSEO_STATUS_NORMAL && !done) {
852
+ result = set_keys_step(context, slots, &done);
853
+ if (result.status != OSEO_STATUS_NORMAL || done) break;
854
+ slots[SET_OPERATION_VALUE] =
855
+ normalize_set_value(slots[SET_OPERATION_VALUE]);
856
+ if (set_find(
857
+ set_object(slots[SET_OPERATION_RESULT]),
858
+ slots[SET_OPERATION_VALUE]
859
+ ) == SIZE_MAX) {
860
+ result = set_append(
861
+ context,
862
+ slots[SET_OPERATION_RESULT],
863
+ slots[SET_OPERATION_VALUE]
864
+ );
865
+ }
866
+ }
867
+ return set_operation_end(
868
+ context,
869
+ &frame,
870
+ result,
871
+ slots[SET_OPERATION_RESULT]
872
+ );
873
+ }
874
+
875
+ /* What a receiver-driven visit does with each `has` answer. */
876
+ typedef enum {
877
+ SET_VISIT_INTERSECT = 0,
878
+ SET_VISIT_SUBSET = 1,
879
+ SET_VISIT_DISJOINT = 2,
880
+ } OseoSetVisitKind;
881
+
882
+ /*
883
+ * Visits the receiver's live elements by index while `has` may mutate it.
884
+ * The loop rereads the vector length after every call, as the
885
+ * specification's thisSize does, so an element appended during a call is
886
+ * visited and a deleted then re-added one can be visited twice.
887
+ */
888
+ static OseoResult set_visit_receiver(
889
+ OseoContext *context,
890
+ OseoValue *slots,
891
+ OseoSetVisitKind kind,
892
+ bool *answer
893
+ ) {
894
+ OseoResult result = normal(oseo_undefined());
895
+ *answer = true;
896
+ size_t index = 0u;
897
+ while (result.status == OSEO_STATUS_NORMAL) {
898
+ OseoSet *set = set_object(slots[SET_OPERATION_RECEIVER]);
899
+ if (index >= set->element_count) break;
900
+ bool live = set->elements[index].present;
901
+ slots[SET_OPERATION_VALUE] = set->elements[index].value;
902
+ index += 1u;
903
+ if (!live) continue;
904
+ bool in_other = false;
905
+ result = set_other_has(context, slots, &in_other);
906
+ if (result.status != OSEO_STATUS_NORMAL) break;
907
+ if (kind == SET_VISIT_SUBSET && !in_other) {
908
+ *answer = false;
909
+ break;
910
+ }
911
+ if (kind == SET_VISIT_DISJOINT && in_other) {
912
+ *answer = false;
913
+ break;
914
+ }
915
+ if (kind == SET_VISIT_INTERSECT && in_other &&
916
+ set_find(
917
+ set_object(slots[SET_OPERATION_RESULT]),
918
+ slots[SET_OPERATION_VALUE]
919
+ ) == SIZE_MAX) {
920
+ result = set_append(
921
+ context,
922
+ slots[SET_OPERATION_RESULT],
923
+ slots[SET_OPERATION_VALUE]
924
+ );
925
+ }
926
+ }
927
+ return result;
928
+ }
929
+
930
+ static OseoResult set_intersection(
931
+ OseoContext *context,
932
+ OseoValue receiver,
933
+ size_t argument_count,
934
+ const OseoValue *arguments
935
+ ) {
936
+ OseoRootFrame frame = {NULL, NULL, 0u};
937
+ double other_size = 0.0;
938
+ OseoResult result = set_operation_begin(
939
+ context,
940
+ receiver,
941
+ argument_count,
942
+ arguments,
943
+ &frame,
944
+ &other_size
945
+ );
946
+ if (result.status != OSEO_STATUS_NORMAL) return result;
947
+ OseoValue *slots = frame.slots;
948
+ result = set_result_create(context, slots);
949
+ if (result.status == OSEO_STATUS_NORMAL &&
950
+ (double)set_object(slots[SET_OPERATION_RECEIVER])->size <=
951
+ other_size) {
952
+ bool unused = true;
953
+ result = set_visit_receiver(
954
+ context,
955
+ slots,
956
+ SET_VISIT_INTERSECT,
957
+ &unused
958
+ );
959
+ } else if (result.status == OSEO_STATUS_NORMAL) {
960
+ result = set_keys_iterator(context, slots);
961
+ bool done = false;
962
+ while (result.status == OSEO_STATUS_NORMAL && !done) {
963
+ result = set_keys_step(context, slots, &done);
964
+ if (result.status != OSEO_STATUS_NORMAL || done) break;
965
+ slots[SET_OPERATION_VALUE] =
966
+ normalize_set_value(slots[SET_OPERATION_VALUE]);
967
+ if (set_find(
968
+ set_object(slots[SET_OPERATION_RECEIVER]),
969
+ slots[SET_OPERATION_VALUE]
970
+ ) != SIZE_MAX &&
971
+ set_find(
972
+ set_object(slots[SET_OPERATION_RESULT]),
973
+ slots[SET_OPERATION_VALUE]
974
+ ) == SIZE_MAX) {
975
+ result = set_append(
976
+ context,
977
+ slots[SET_OPERATION_RESULT],
978
+ slots[SET_OPERATION_VALUE]
979
+ );
980
+ }
981
+ }
982
+ }
983
+ return set_operation_end(
984
+ context,
985
+ &frame,
986
+ result,
987
+ slots[SET_OPERATION_RESULT]
988
+ );
989
+ }
990
+
991
+ static OseoResult set_difference(
992
+ OseoContext *context,
993
+ OseoValue receiver,
994
+ size_t argument_count,
995
+ const OseoValue *arguments
996
+ ) {
997
+ OseoRootFrame frame = {NULL, NULL, 0u};
998
+ double other_size = 0.0;
999
+ OseoResult result = set_operation_begin(
1000
+ context,
1001
+ receiver,
1002
+ argument_count,
1003
+ arguments,
1004
+ &frame,
1005
+ &other_size
1006
+ );
1007
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1008
+ OseoValue *slots = frame.slots;
1009
+ result = set_result_create(context, slots);
1010
+ if (result.status == OSEO_STATUS_NORMAL) {
1011
+ result = set_result_copy(context, slots);
1012
+ }
1013
+ if (result.status == OSEO_STATUS_NORMAL &&
1014
+ (double)set_object(slots[SET_OPERATION_RECEIVER])->size <=
1015
+ other_size) {
1016
+ /* The copy fixes the visited elements, so a call that mutates the
1017
+ * receiver changes neither the count nor the values visited. */
1018
+ size_t count = set_object(slots[SET_OPERATION_RESULT])->element_count;
1019
+ for (size_t index = 0u;
1020
+ result.status == OSEO_STATUS_NORMAL && index < count;
1021
+ index += 1u) {
1022
+ slots[SET_OPERATION_VALUE] =
1023
+ set_object(slots[SET_OPERATION_RESULT])->elements[index].value;
1024
+ bool in_other = false;
1025
+ result = set_other_has(context, slots, &in_other);
1026
+ if (result.status == OSEO_STATUS_NORMAL && in_other) {
1027
+ set_remove_at(set_object(slots[SET_OPERATION_RESULT]), index);
1028
+ }
1029
+ }
1030
+ } else if (result.status == OSEO_STATUS_NORMAL) {
1031
+ result = set_keys_iterator(context, slots);
1032
+ bool done = false;
1033
+ while (result.status == OSEO_STATUS_NORMAL && !done) {
1034
+ result = set_keys_step(context, slots, &done);
1035
+ if (result.status != OSEO_STATUS_NORMAL || done) break;
1036
+ OseoSet *copy = set_object(slots[SET_OPERATION_RESULT]);
1037
+ size_t index = set_find(copy, slots[SET_OPERATION_VALUE]);
1038
+ if (index != SIZE_MAX) set_remove_at(copy, index);
1039
+ }
1040
+ }
1041
+ return set_operation_end(
1042
+ context,
1043
+ &frame,
1044
+ result,
1045
+ slots[SET_OPERATION_RESULT]
1046
+ );
1047
+ }
1048
+
1049
+ static OseoResult set_symmetric_difference(
1050
+ OseoContext *context,
1051
+ OseoValue receiver,
1052
+ size_t argument_count,
1053
+ const OseoValue *arguments
1054
+ ) {
1055
+ OseoRootFrame frame = {NULL, NULL, 0u};
1056
+ double other_size = 0.0;
1057
+ OseoResult result = set_operation_begin(
1058
+ context,
1059
+ receiver,
1060
+ argument_count,
1061
+ arguments,
1062
+ &frame,
1063
+ &other_size
1064
+ );
1065
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1066
+ OseoValue *slots = frame.slots;
1067
+ result = set_keys_iterator(context, slots);
1068
+ if (result.status == OSEO_STATUS_NORMAL) {
1069
+ result = set_result_create(context, slots);
1070
+ }
1071
+ if (result.status == OSEO_STATUS_NORMAL) {
1072
+ result = set_result_copy(context, slots);
1073
+ }
1074
+ bool done = false;
1075
+ while (result.status == OSEO_STATUS_NORMAL && !done) {
1076
+ result = set_keys_step(context, slots, &done);
1077
+ if (result.status != OSEO_STATUS_NORMAL || done) break;
1078
+ slots[SET_OPERATION_VALUE] =
1079
+ normalize_set_value(slots[SET_OPERATION_VALUE]);
1080
+ OseoSet *copy = set_object(slots[SET_OPERATION_RESULT]);
1081
+ size_t index = set_find(copy, slots[SET_OPERATION_VALUE]);
1082
+ bool in_receiver = set_find(
1083
+ set_object(slots[SET_OPERATION_RECEIVER]),
1084
+ slots[SET_OPERATION_VALUE]
1085
+ ) != SIZE_MAX;
1086
+ if (in_receiver) {
1087
+ if (index != SIZE_MAX) set_remove_at(copy, index);
1088
+ } else if (index == SIZE_MAX) {
1089
+ result = set_append(
1090
+ context,
1091
+ slots[SET_OPERATION_RESULT],
1092
+ slots[SET_OPERATION_VALUE]
1093
+ );
1094
+ }
1095
+ }
1096
+ return set_operation_end(
1097
+ context,
1098
+ &frame,
1099
+ result,
1100
+ slots[SET_OPERATION_RESULT]
1101
+ );
1102
+ }
1103
+
1104
+ /*
1105
+ * The keys-iterator half of isSupersetOf and isDisjointFrom. The walk
1106
+ * stops at the first value whose receiver membership equals `stop_when`
1107
+ * and closes the iterator with a normal completion, so an abrupt return
1108
+ * method replaces the Boolean answer.
1109
+ */
1110
+ static OseoResult set_scan_keys(
1111
+ OseoContext *context,
1112
+ OseoValue *slots,
1113
+ bool stop_when,
1114
+ bool *answer
1115
+ ) {
1116
+ *answer = true;
1117
+ OseoResult result = set_keys_iterator(context, slots);
1118
+ bool done = false;
1119
+ while (result.status == OSEO_STATUS_NORMAL && !done) {
1120
+ result = set_keys_step(context, slots, &done);
1121
+ if (result.status != OSEO_STATUS_NORMAL || done) break;
1122
+ bool in_receiver = set_find(
1123
+ set_object(slots[SET_OPERATION_RECEIVER]),
1124
+ slots[SET_OPERATION_VALUE]
1125
+ ) != SIZE_MAX;
1126
+ if (in_receiver == stop_when) {
1127
+ *answer = false;
1128
+ result = oseo_iterator_close(
1129
+ context,
1130
+ slots[SET_OPERATION_ITERATOR],
1131
+ false
1132
+ );
1133
+ break;
1134
+ }
1135
+ }
1136
+ return result;
1137
+ }
1138
+
1139
+ static OseoResult set_is_subset_of(
1140
+ OseoContext *context,
1141
+ OseoValue receiver,
1142
+ size_t argument_count,
1143
+ const OseoValue *arguments
1144
+ ) {
1145
+ OseoRootFrame frame = {NULL, NULL, 0u};
1146
+ double other_size = 0.0;
1147
+ OseoResult result = set_operation_begin(
1148
+ context,
1149
+ receiver,
1150
+ argument_count,
1151
+ arguments,
1152
+ &frame,
1153
+ &other_size
1154
+ );
1155
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1156
+ bool answer = false;
1157
+ if ((double)set_object(frame.slots[SET_OPERATION_RECEIVER])->size <=
1158
+ other_size) {
1159
+ result = set_visit_receiver(
1160
+ context,
1161
+ frame.slots,
1162
+ SET_VISIT_SUBSET,
1163
+ &answer
1164
+ );
1165
+ }
1166
+ return set_operation_end(
1167
+ context,
1168
+ &frame,
1169
+ result,
1170
+ oseo_boolean(answer)
1171
+ );
1172
+ }
1173
+
1174
+ static OseoResult set_is_superset_of(
1175
+ OseoContext *context,
1176
+ OseoValue receiver,
1177
+ size_t argument_count,
1178
+ const OseoValue *arguments
1179
+ ) {
1180
+ OseoRootFrame frame = {NULL, NULL, 0u};
1181
+ double other_size = 0.0;
1182
+ OseoResult result = set_operation_begin(
1183
+ context,
1184
+ receiver,
1185
+ argument_count,
1186
+ arguments,
1187
+ &frame,
1188
+ &other_size
1189
+ );
1190
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1191
+ bool answer = false;
1192
+ if ((double)set_object(frame.slots[SET_OPERATION_RECEIVER])->size >=
1193
+ other_size) {
1194
+ result = set_scan_keys(context, frame.slots, false, &answer);
1195
+ }
1196
+ return set_operation_end(
1197
+ context,
1198
+ &frame,
1199
+ result,
1200
+ oseo_boolean(answer)
1201
+ );
1202
+ }
1203
+
1204
+ static OseoResult set_is_disjoint_from(
1205
+ OseoContext *context,
1206
+ OseoValue receiver,
1207
+ size_t argument_count,
1208
+ const OseoValue *arguments
1209
+ ) {
1210
+ OseoRootFrame frame = {NULL, NULL, 0u};
1211
+ double other_size = 0.0;
1212
+ OseoResult result = set_operation_begin(
1213
+ context,
1214
+ receiver,
1215
+ argument_count,
1216
+ arguments,
1217
+ &frame,
1218
+ &other_size
1219
+ );
1220
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1221
+ bool answer = true;
1222
+ if ((double)set_object(frame.slots[SET_OPERATION_RECEIVER])->size <=
1223
+ other_size) {
1224
+ result = set_visit_receiver(
1225
+ context,
1226
+ frame.slots,
1227
+ SET_VISIT_DISJOINT,
1228
+ &answer
1229
+ );
1230
+ } else {
1231
+ result = set_scan_keys(context, frame.slots, true, &answer);
1232
+ }
1233
+ return set_operation_end(
1234
+ context,
1235
+ &frame,
1236
+ result,
1237
+ oseo_boolean(answer)
1238
+ );
1239
+ }
1240
+
1241
+ OseoResult oseo_internal_set_builtin_dispatch(
1242
+ OseoContext *context,
1243
+ size_t code_id,
1244
+ OseoValue callee,
1245
+ OseoValue receiver,
1246
+ size_t argument_count,
1247
+ const OseoValue *arguments,
1248
+ OseoValue new_target
1249
+ ) {
1250
+ (void)callee;
1251
+ if (code_id == OSEO_SET_CONSTRUCTOR_CODE_ID) {
1252
+ return set_constructor(
1253
+ context,
1254
+ argument_count,
1255
+ arguments,
1256
+ new_target
1257
+ );
1258
+ }
1259
+ OseoValue value = argument_count > 0u
1260
+ ? arguments[0]
1261
+ : oseo_undefined();
1262
+ if (code_id == OSEO_SET_ADD_CODE_ID) {
1263
+ return set_add(context, receiver, value);
1264
+ }
1265
+ if (code_id == OSEO_SET_CLEAR_CODE_ID) {
1266
+ return set_clear(context, receiver);
1267
+ }
1268
+ if (code_id == OSEO_SET_DELETE_CODE_ID) {
1269
+ return set_delete(context, receiver, value);
1270
+ }
1271
+ if (code_id == OSEO_SET_ENTRIES_CODE_ID) {
1272
+ return set_entries(context, receiver);
1273
+ }
1274
+ if (code_id == OSEO_SET_FOR_EACH_CODE_ID) {
1275
+ return set_for_each(context, receiver, argument_count, arguments);
1276
+ }
1277
+ if (code_id == OSEO_SET_HAS_CODE_ID) {
1278
+ return set_has(context, receiver, value);
1279
+ }
1280
+ if (code_id == OSEO_SET_SIZE_CODE_ID) {
1281
+ return set_size(context, receiver);
1282
+ }
1283
+ if (code_id == OSEO_SET_VALUES_CODE_ID) {
1284
+ return set_values(context, receiver);
1285
+ }
1286
+ if (code_id == OSEO_SET_ITERATOR_NEXT_CODE_ID) {
1287
+ return set_iterator_next(context, receiver);
1288
+ }
1289
+ if (code_id == OSEO_SET_SPECIES_CODE_ID) return normal(receiver);
1290
+ if (code_id == OSEO_SET_UNION_CODE_ID) {
1291
+ return set_union(context, receiver, argument_count, arguments);
1292
+ }
1293
+ if (code_id == OSEO_SET_INTERSECTION_CODE_ID) {
1294
+ return set_intersection(context, receiver, argument_count, arguments);
1295
+ }
1296
+ if (code_id == OSEO_SET_DIFFERENCE_CODE_ID) {
1297
+ return set_difference(context, receiver, argument_count, arguments);
1298
+ }
1299
+ if (code_id == OSEO_SET_SYMMETRIC_DIFFERENCE_CODE_ID) {
1300
+ return set_symmetric_difference(
1301
+ context,
1302
+ receiver,
1303
+ argument_count,
1304
+ arguments
1305
+ );
1306
+ }
1307
+ if (code_id == OSEO_SET_IS_SUBSET_OF_CODE_ID) {
1308
+ return set_is_subset_of(context, receiver, argument_count, arguments);
1309
+ }
1310
+ if (code_id == OSEO_SET_IS_SUPERSET_OF_CODE_ID) {
1311
+ return set_is_superset_of(
1312
+ context,
1313
+ receiver,
1314
+ argument_count,
1315
+ arguments
1316
+ );
1317
+ }
1318
+ if (code_id == OSEO_SET_IS_DISJOINT_FROM_CODE_ID) {
1319
+ return set_is_disjoint_from(
1320
+ context,
1321
+ receiver,
1322
+ argument_count,
1323
+ arguments
1324
+ );
1325
+ }
1326
+ return oseo_unknown_function(context, code_id);
1327
+ }
1328
+
1329
+ static OseoResult create_set_builtin(
1330
+ OseoContext *context,
1331
+ size_t code_id,
1332
+ const char *name,
1333
+ size_t length,
1334
+ OseoFunctionKind kind,
1335
+ OseoFunctionNamePrefix prefix
1336
+ ) {
1337
+ size_t name_length = strlen(name);
1338
+ if (name_length > 31u) {
1339
+ return failure(context, "OSEO2001", "Built-in name is too long.");
1340
+ }
1341
+ uint16_t units[31];
1342
+ for (size_t index = 0u; index < name_length; index += 1u) {
1343
+ units[index] = (uint16_t)(unsigned char)name[index];
1344
+ }
1345
+ OseoValue environment = oseo_undefined();
1346
+ OseoRootFrame frame = {NULL, &environment, 1u};
1347
+ oseo_roots_push(context, &frame);
1348
+ OseoResult result = oseo_environment_create(context, 0u);
1349
+ environment = result.value;
1350
+ if (result.status == OSEO_STATUS_NORMAL) {
1351
+ result = oseo_function_create(
1352
+ context,
1353
+ code_id,
1354
+ environment,
1355
+ units,
1356
+ name_length,
1357
+ length,
1358
+ kind,
1359
+ oseo_undefined(),
1360
+ oseo_undefined(),
1361
+ prefix
1362
+ );
1363
+ }
1364
+ oseo_roots_pop(context, &frame);
1365
+ return result;
1366
+ }
1367
+
1368
+ static OseoResult define_set_property(
1369
+ OseoContext *context,
1370
+ OseoValue object,
1371
+ OseoValue key,
1372
+ OseoValue value,
1373
+ OseoPropertyAttributes attributes
1374
+ ) {
1375
+ OseoValue slots[3] = {object, key, value};
1376
+ OseoRootFrame frame = {NULL, slots, 3u};
1377
+ oseo_roots_push(context, &frame);
1378
+ OseoResult result = oseo_object_define(
1379
+ context,
1380
+ slots[0],
1381
+ slots[1],
1382
+ slots[2],
1383
+ attributes
1384
+ );
1385
+ oseo_roots_pop(context, &frame);
1386
+ return result;
1387
+ }
1388
+
1389
+ static OseoResult define_set_ascii_property(
1390
+ OseoContext *context,
1391
+ OseoValue object,
1392
+ const char *name,
1393
+ OseoValue value,
1394
+ OseoPropertyAttributes attributes
1395
+ ) {
1396
+ OseoValue slots[3] = {object, value, oseo_undefined()};
1397
+ OseoRootFrame frame = {NULL, slots, 3u};
1398
+ oseo_roots_push(context, &frame);
1399
+ OseoResult result = oseo_internal_ascii_string(context, name);
1400
+ slots[2] = result.value;
1401
+ if (result.status == OSEO_STATUS_NORMAL) {
1402
+ result = oseo_object_define(
1403
+ context,
1404
+ slots[0],
1405
+ slots[2],
1406
+ slots[1],
1407
+ attributes
1408
+ );
1409
+ }
1410
+ oseo_roots_pop(context, &frame);
1411
+ return result;
1412
+ }
1413
+
1414
+ static OseoResult set_intrinsic_build(OseoContext *context) {
1415
+ OseoValue *marker = &context->realm->intrinsics[OSEO_INTRINSIC_SET_SPECIES];
1416
+ if (tag_of(*marker) == OSEO_TAG_UNINITIALIZED) {
1417
+ return failure(
1418
+ context,
1419
+ "OSEO2001",
1420
+ "The Set intrinsic cluster is already being built."
1421
+ );
1422
+ }
1423
+ if (tag_of(*marker) != OSEO_TAG_UNDEFINED) return normal(*marker);
1424
+ size_t entry_allocations = context->allocations;
1425
+ OseoRootFrame frame = {NULL, NULL, 0u};
1426
+ OseoResult result = oseo_roots_allocate(context, &frame, 7u);
1427
+ if (result.status != OSEO_STATUS_NORMAL) return result;
1428
+ *marker = oseo_uninitialized();
1429
+ result = oseo_internal_intrinsic(
1430
+ context,
1431
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
1432
+ );
1433
+ frame.slots[0] = result.value;
1434
+ if (result.status == OSEO_STATUS_NORMAL) {
1435
+ result = oseo_object_create(context, frame.slots[0]);
1436
+ frame.slots[1] = result.value;
1437
+ }
1438
+ if (result.status == OSEO_STATUS_NORMAL) {
1439
+ context->realm->intrinsics[OSEO_INTRINSIC_SET_PROTOTYPE] =
1440
+ frame.slots[1];
1441
+ result = oseo_internal_intrinsic(
1442
+ context,
1443
+ OSEO_INTRINSIC_ITERATOR_PROTOTYPE
1444
+ );
1445
+ frame.slots[2] = result.value;
1446
+ }
1447
+ if (result.status == OSEO_STATUS_NORMAL) {
1448
+ result = oseo_object_create(context, frame.slots[2]);
1449
+ frame.slots[2] = result.value;
1450
+ }
1451
+ if (result.status == OSEO_STATUS_NORMAL) {
1452
+ context->realm->intrinsics[OSEO_INTRINSIC_SET_ITERATOR_PROTOTYPE] =
1453
+ frame.slots[2];
1454
+ result = create_set_builtin(
1455
+ context,
1456
+ OSEO_SET_CONSTRUCTOR_CODE_ID,
1457
+ "Set",
1458
+ 0u,
1459
+ OSEO_FUNCTION_ORDINARY,
1460
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1461
+ );
1462
+ frame.slots[3] = result.value;
1463
+ }
1464
+ const OseoPropertyAttributes method = {true, false, true, false};
1465
+ if (result.status == OSEO_STATUS_NORMAL) {
1466
+ context->realm->intrinsics[OSEO_INTRINSIC_SET] = frame.slots[3];
1467
+ OseoFunction *constructor = function_object(frame.slots[3]);
1468
+ constructor->prototype_object = frame.slots[1];
1469
+ constructor->prototype_writable = false;
1470
+ result = define_set_ascii_property(
1471
+ context,
1472
+ frame.slots[1],
1473
+ "constructor",
1474
+ frame.slots[3],
1475
+ method
1476
+ );
1477
+ }
1478
+ static const OseoIntrinsic method_intrinsics[] = {
1479
+ OSEO_INTRINSIC_SET_ADD,
1480
+ OSEO_INTRINSIC_SET_CLEAR,
1481
+ OSEO_INTRINSIC_SET_DELETE,
1482
+ OSEO_INTRINSIC_SET_ENTRIES,
1483
+ OSEO_INTRINSIC_SET_FOR_EACH,
1484
+ OSEO_INTRINSIC_SET_HAS,
1485
+ };
1486
+ static const size_t method_codes[] = {
1487
+ OSEO_SET_ADD_CODE_ID,
1488
+ OSEO_SET_CLEAR_CODE_ID,
1489
+ OSEO_SET_DELETE_CODE_ID,
1490
+ OSEO_SET_ENTRIES_CODE_ID,
1491
+ OSEO_SET_FOR_EACH_CODE_ID,
1492
+ OSEO_SET_HAS_CODE_ID,
1493
+ };
1494
+ static const char *const method_names[] = {
1495
+ "add", "clear", "delete", "entries", "forEach", "has",
1496
+ };
1497
+ static const size_t method_lengths[] = {1u, 0u, 1u, 0u, 1u, 1u};
1498
+ for (size_t index = 0u;
1499
+ result.status == OSEO_STATUS_NORMAL && index < 6u;
1500
+ index += 1u) {
1501
+ result = create_set_builtin(
1502
+ context,
1503
+ method_codes[index],
1504
+ method_names[index],
1505
+ method_lengths[index],
1506
+ OSEO_FUNCTION_INTERNAL,
1507
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1508
+ );
1509
+ frame.slots[4] = result.value;
1510
+ if (result.status != OSEO_STATUS_NORMAL) break;
1511
+ context->realm->intrinsics[method_intrinsics[index]] = frame.slots[4];
1512
+ result = define_set_ascii_property(
1513
+ context,
1514
+ frame.slots[1],
1515
+ method_names[index],
1516
+ frame.slots[4],
1517
+ method
1518
+ );
1519
+ }
1520
+ /* The composition methods own no realm intrinsic slot: nothing in the
1521
+ * runtime refers to them except their prototype properties. */
1522
+ static const size_t composition_codes[] = {
1523
+ OSEO_SET_UNION_CODE_ID,
1524
+ OSEO_SET_INTERSECTION_CODE_ID,
1525
+ OSEO_SET_DIFFERENCE_CODE_ID,
1526
+ OSEO_SET_SYMMETRIC_DIFFERENCE_CODE_ID,
1527
+ OSEO_SET_IS_SUBSET_OF_CODE_ID,
1528
+ OSEO_SET_IS_SUPERSET_OF_CODE_ID,
1529
+ OSEO_SET_IS_DISJOINT_FROM_CODE_ID,
1530
+ };
1531
+ static const char *const composition_names[] = {
1532
+ "union",
1533
+ "intersection",
1534
+ "difference",
1535
+ "symmetricDifference",
1536
+ "isSubsetOf",
1537
+ "isSupersetOf",
1538
+ "isDisjointFrom",
1539
+ };
1540
+ for (size_t index = 0u;
1541
+ result.status == OSEO_STATUS_NORMAL && index < 7u;
1542
+ index += 1u) {
1543
+ result = create_set_builtin(
1544
+ context,
1545
+ composition_codes[index],
1546
+ composition_names[index],
1547
+ 1u,
1548
+ OSEO_FUNCTION_INTERNAL,
1549
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1550
+ );
1551
+ frame.slots[4] = result.value;
1552
+ if (result.status != OSEO_STATUS_NORMAL) break;
1553
+ result = define_set_ascii_property(
1554
+ context,
1555
+ frame.slots[1],
1556
+ composition_names[index],
1557
+ frame.slots[4],
1558
+ method
1559
+ );
1560
+ }
1561
+ if (result.status == OSEO_STATUS_NORMAL) {
1562
+ result = create_set_builtin(
1563
+ context,
1564
+ OSEO_SET_SIZE_CODE_ID,
1565
+ "size",
1566
+ 0u,
1567
+ OSEO_FUNCTION_INTERNAL,
1568
+ OSEO_FUNCTION_NAME_PREFIX_GET
1569
+ );
1570
+ frame.slots[4] = result.value;
1571
+ }
1572
+ if (result.status == OSEO_STATUS_NORMAL) {
1573
+ context->realm->intrinsics[OSEO_INTRINSIC_SET_SIZE] = frame.slots[4];
1574
+ result = oseo_internal_ascii_string(context, "size");
1575
+ frame.slots[5] = result.value;
1576
+ }
1577
+ if (result.status == OSEO_STATUS_NORMAL) {
1578
+ result = oseo_object_define_accessor(
1579
+ context,
1580
+ frame.slots[1],
1581
+ frame.slots[5],
1582
+ frame.slots[4],
1583
+ oseo_undefined(),
1584
+ true,
1585
+ false,
1586
+ (OseoPropertyAttributes){true, false, false, true}
1587
+ );
1588
+ }
1589
+ if (result.status == OSEO_STATUS_NORMAL) {
1590
+ result = create_set_builtin(
1591
+ context,
1592
+ OSEO_SET_VALUES_CODE_ID,
1593
+ "values",
1594
+ 0u,
1595
+ OSEO_FUNCTION_INTERNAL,
1596
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1597
+ );
1598
+ frame.slots[4] = result.value;
1599
+ }
1600
+ if (result.status == OSEO_STATUS_NORMAL) {
1601
+ context->realm->intrinsics[OSEO_INTRINSIC_SET_VALUES] = frame.slots[4];
1602
+ result = define_set_ascii_property(
1603
+ context,
1604
+ frame.slots[1],
1605
+ "values",
1606
+ frame.slots[4],
1607
+ method
1608
+ );
1609
+ }
1610
+ if (result.status == OSEO_STATUS_NORMAL) {
1611
+ result = define_set_ascii_property(
1612
+ context,
1613
+ frame.slots[1],
1614
+ "keys",
1615
+ frame.slots[4],
1616
+ method
1617
+ );
1618
+ }
1619
+ if (result.status == OSEO_STATUS_NORMAL) {
1620
+ result = oseo_internal_well_known_symbol(
1621
+ context,
1622
+ OSEO_WELL_KNOWN_ITERATOR
1623
+ );
1624
+ frame.slots[5] = result.value;
1625
+ }
1626
+ if (result.status == OSEO_STATUS_NORMAL) {
1627
+ result = define_set_property(
1628
+ context,
1629
+ frame.slots[1],
1630
+ frame.slots[5],
1631
+ frame.slots[4],
1632
+ method
1633
+ );
1634
+ }
1635
+ if (result.status == OSEO_STATUS_NORMAL) {
1636
+ result = oseo_internal_well_known_symbol(
1637
+ context,
1638
+ OSEO_WELL_KNOWN_TO_STRING_TAG
1639
+ );
1640
+ frame.slots[5] = result.value;
1641
+ }
1642
+ if (result.status == OSEO_STATUS_NORMAL) {
1643
+ result = oseo_internal_ascii_string(context, "Set");
1644
+ frame.slots[6] = result.value;
1645
+ }
1646
+ if (result.status == OSEO_STATUS_NORMAL) {
1647
+ result = define_set_property(
1648
+ context,
1649
+ frame.slots[1],
1650
+ frame.slots[5],
1651
+ frame.slots[6],
1652
+ (OseoPropertyAttributes){true, false, false, false}
1653
+ );
1654
+ }
1655
+ if (result.status == OSEO_STATUS_NORMAL) {
1656
+ result = create_set_builtin(
1657
+ context,
1658
+ OSEO_SET_ITERATOR_NEXT_CODE_ID,
1659
+ "next",
1660
+ 0u,
1661
+ OSEO_FUNCTION_INTERNAL,
1662
+ OSEO_FUNCTION_NAME_PREFIX_NONE
1663
+ );
1664
+ frame.slots[4] = result.value;
1665
+ }
1666
+ if (result.status == OSEO_STATUS_NORMAL) {
1667
+ context->realm->intrinsics[OSEO_INTRINSIC_SET_ITERATOR_NEXT] =
1668
+ frame.slots[4];
1669
+ result = define_set_ascii_property(
1670
+ context,
1671
+ frame.slots[2],
1672
+ "next",
1673
+ frame.slots[4],
1674
+ method
1675
+ );
1676
+ }
1677
+ if (result.status == OSEO_STATUS_NORMAL) {
1678
+ result = oseo_internal_well_known_symbol(
1679
+ context,
1680
+ OSEO_WELL_KNOWN_TO_STRING_TAG
1681
+ );
1682
+ frame.slots[5] = result.value;
1683
+ }
1684
+ if (result.status == OSEO_STATUS_NORMAL) {
1685
+ result = oseo_internal_ascii_string(context, "Set Iterator");
1686
+ frame.slots[6] = result.value;
1687
+ }
1688
+ if (result.status == OSEO_STATUS_NORMAL) {
1689
+ result = define_set_property(
1690
+ context,
1691
+ frame.slots[2],
1692
+ frame.slots[5],
1693
+ frame.slots[6],
1694
+ (OseoPropertyAttributes){true, false, false, false}
1695
+ );
1696
+ }
1697
+ if (result.status == OSEO_STATUS_NORMAL) {
1698
+ result = create_set_builtin(
1699
+ context,
1700
+ OSEO_SET_SPECIES_CODE_ID,
1701
+ "[Symbol.species]",
1702
+ 0u,
1703
+ OSEO_FUNCTION_INTERNAL,
1704
+ OSEO_FUNCTION_NAME_PREFIX_GET
1705
+ );
1706
+ frame.slots[4] = result.value;
1707
+ }
1708
+ if (result.status == OSEO_STATUS_NORMAL) {
1709
+ result = oseo_internal_well_known_symbol(
1710
+ context,
1711
+ OSEO_WELL_KNOWN_SPECIES
1712
+ );
1713
+ frame.slots[5] = result.value;
1714
+ }
1715
+ if (result.status == OSEO_STATUS_NORMAL) {
1716
+ result = oseo_object_define_accessor(
1717
+ context,
1718
+ frame.slots[3],
1719
+ frame.slots[5],
1720
+ frame.slots[4],
1721
+ oseo_undefined(),
1722
+ true,
1723
+ false,
1724
+ (OseoPropertyAttributes){true, false, false, true}
1725
+ );
1726
+ }
1727
+ if (result.status != OSEO_STATUS_NORMAL) {
1728
+ for (size_t index = OSEO_INTRINSIC_SET_PROTOTYPE;
1729
+ index <= OSEO_INTRINSIC_SET_SPECIES;
1730
+ index += 1u) {
1731
+ context->realm->intrinsics[index] = oseo_undefined();
1732
+ }
1733
+ oseo_roots_release(context, &frame);
1734
+ return result;
1735
+ }
1736
+ OseoValue species = frame.slots[4];
1737
+ context->realm->intrinsics[OSEO_INTRINSIC_SET_SPECIES] = species;
1738
+ if (context->observe_specialization) {
1739
+ context->allocations = entry_allocations;
1740
+ }
1741
+ oseo_roots_release(context, &frame);
1742
+ return normal(species);
1743
+ }
1744
+
1745
+ OseoResult oseo_internal_set_intrinsic(OseoContext *context) {
1746
+ OseoResult built = set_intrinsic_build(context);
1747
+ if (built.status != OSEO_STATUS_NORMAL) return built;
1748
+ return normal(context->realm->intrinsics[OSEO_INTRINSIC_SET]);
1749
+ }
1750
+
1751
+ OseoResult oseo_internal_install_set_global(
1752
+ OseoContext *context,
1753
+ OseoValue global
1754
+ ) {
1755
+ OseoValue slots[3] = {global, oseo_undefined(), oseo_undefined()};
1756
+ OseoRootFrame frame = {NULL, slots, 3u};
1757
+ oseo_roots_push(context, &frame);
1758
+ OseoResult result = oseo_internal_set_intrinsic(context);
1759
+ slots[1] = result.value;
1760
+ if (result.status == OSEO_STATUS_NORMAL) {
1761
+ result = oseo_internal_ascii_string(context, "Set");
1762
+ slots[2] = result.value;
1763
+ }
1764
+ if (result.status == OSEO_STATUS_NORMAL) {
1765
+ result = oseo_object_define(
1766
+ context,
1767
+ slots[0],
1768
+ slots[2],
1769
+ slots[1],
1770
+ (OseoPropertyAttributes){true, false, true, false}
1771
+ );
1772
+ }
1773
+ oseo_roots_pop(context, &frame);
1774
+ return result.status == OSEO_STATUS_NORMAL ? normal(slots[0]) : result;
1775
+ }