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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE +674 -0
  2. package/MIT-LICENSE.txt +21 -0
  3. package/README.md +1677 -2
  4. package/UNICODE-LICENSE.txt +39 -0
  5. package/dist/index.d.ts +7 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +269 -0
  8. package/dist/index.js.map +1 -0
  9. package/native/oseo_runtime.h +2594 -0
  10. package/native/runtime_agent.c +1693 -0
  11. package/native/runtime_arguments.c +355 -0
  12. package/native/runtime_array.c +5742 -0
  13. package/native/runtime_array_buffer.c +1724 -0
  14. package/native/runtime_async_generator.c +948 -0
  15. package/native/runtime_atomics.c +1259 -0
  16. package/native/runtime_bigint.c +1363 -0
  17. package/native/runtime_bigint_object.c +602 -0
  18. package/native/runtime_binding.c +1005 -0
  19. package/native/runtime_boolean.c +296 -0
  20. package/native/runtime_clock.c +157 -0
  21. package/native/runtime_clock_posix.c +575 -0
  22. package/native/runtime_core.c +464 -0
  23. package/native/runtime_data_view.c +1111 -0
  24. package/native/runtime_date.c +1973 -0
  25. package/native/runtime_descriptor.c +1080 -0
  26. package/native/runtime_disposal.c +1643 -0
  27. package/native/runtime_enumeration.c +612 -0
  28. package/native/runtime_error.c +973 -0
  29. package/native/runtime_event_loop.c +629 -0
  30. package/native/runtime_function.c +3643 -0
  31. package/native/runtime_generator.c +1096 -0
  32. package/native/runtime_internal.h +5554 -0
  33. package/native/runtime_iterator.c +5355 -0
  34. package/native/runtime_json.c +1968 -0
  35. package/native/runtime_map.c +1539 -0
  36. package/native/runtime_math.c +894 -0
  37. package/native/runtime_memory.c +1341 -0
  38. package/native/runtime_module.c +860 -0
  39. package/native/runtime_number.c +2053 -0
  40. package/native/runtime_object.c +399 -0
  41. package/native/runtime_object_builtin.c +3877 -0
  42. package/native/runtime_primitive.c +2244 -0
  43. package/native/runtime_process.c +720 -0
  44. package/native/runtime_promise.c +3354 -0
  45. package/native/runtime_property.c +791 -0
  46. package/native/runtime_proxy.c +1533 -0
  47. package/native/runtime_realm.c +411 -0
  48. package/native/runtime_reflect.c +624 -0
  49. package/native/runtime_regexp.c +3495 -0
  50. package/native/runtime_regexp_matcher.c +3535 -0
  51. package/native/runtime_regexp_symbol.c +1471 -0
  52. package/native/runtime_set.c +1751 -0
  53. package/native/runtime_string.c +2862 -0
  54. package/native/runtime_string_match.c +1161 -0
  55. package/native/runtime_symbol.c +986 -0
  56. package/native/runtime_typed_array.c +5113 -0
  57. package/native/runtime_unicode_tables.h +14647 -0
  58. package/native/runtime_uri.c +676 -0
  59. package/native/runtime_weak_collection.c +1275 -0
  60. package/package.json +38 -3
@@ -0,0 +1,1005 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /* Copyright (c) 2026 Hong Minhee; see ../MIT-LICENSE.txt. */
3
+
4
+ #include "runtime_internal.h"
5
+
6
+ #include <math.h>
7
+ #include <stdio.h>
8
+ #include <stdlib.h>
9
+ #include <string.h>
10
+
11
+ /*
12
+ * Environments, binding cells, and module namespaces.
13
+ */
14
+
15
+ OseoResult oseo_environment_create(OseoContext *context, size_t slot_count) {
16
+ if (slot_count >
17
+ (SIZE_MAX - sizeof(OseoEnvironment)) / sizeof(OseoValue)) {
18
+ return failure(
19
+ context,
20
+ "OSEO2001",
21
+ "Environment allocation is too large."
22
+ );
23
+ }
24
+ size_t slot_bytes = slot_count * sizeof(OseoValue);
25
+ size_t word_count = slot_count / 64u + (slot_count % 64u != 0u);
26
+ if (word_count >
27
+ (SIZE_MAX - sizeof(OseoEnvironment) - slot_bytes) / sizeof(uint64_t)) {
28
+ return failure(
29
+ context,
30
+ "OSEO2001",
31
+ "Environment allocation is too large."
32
+ );
33
+ }
34
+ size_t size = sizeof(OseoEnvironment) + slot_bytes +
35
+ word_count * sizeof(uint64_t);
36
+ OseoEnvironment *environment =
37
+ oseo_internal_allocate_heap_bytes(context, size);
38
+ if (environment == NULL) {
39
+ return failure(context, "OSEO2001", "Environment allocation failed.");
40
+ }
41
+ environment->slot_count = slot_count;
42
+ environment->heap_word_count = word_count;
43
+ for (size_t index = 0u; index < slot_count; index += 1u) {
44
+ environment->slots[index] = oseo_undefined();
45
+ }
46
+ uint64_t *bitmap = environment_heap_bitmap(environment);
47
+ for (size_t index = 0u; index < word_count; index += 1u) {
48
+ bitmap[index] = UINT64_C(0);
49
+ }
50
+ return oseo_internal_publish_heap(
51
+ context,
52
+ &environment->header,
53
+ OSEO_HEAP_ENVIRONMENT
54
+ );
55
+ }
56
+
57
+ OseoResult oseo_environment_get(
58
+ OseoContext *context,
59
+ OseoValue environment_value,
60
+ size_t index
61
+ ) {
62
+ if (!is_environment(environment_value)) {
63
+ return failure(context, "OSEO2001", "Value is not an environment.");
64
+ }
65
+ OseoEnvironment *environment = environment_object(environment_value);
66
+ if (index >= environment->slot_count) {
67
+ return failure(context, "OSEO2001", "Environment index is invalid.");
68
+ }
69
+ return normal(environment->slots[index]);
70
+ }
71
+
72
+ OseoResult oseo_environment_set(
73
+ OseoContext *context,
74
+ OseoValue environment_value,
75
+ size_t index,
76
+ OseoValue value
77
+ ) {
78
+ if (!is_environment(environment_value)) {
79
+ return failure(context, "OSEO2001", "Value is not an environment.");
80
+ }
81
+ OseoEnvironment *environment = environment_object(environment_value);
82
+ if (index >= environment->slot_count) {
83
+ return failure(context, "OSEO2001", "Environment index is invalid.");
84
+ }
85
+ environment->slots[index] = value;
86
+ uint64_t *word = &environment_heap_bitmap(environment)[index / 64u];
87
+ uint64_t bit = UINT64_C(1) << (index % 64u);
88
+ if (tag_of(value) == OSEO_TAG_HEAP) {
89
+ *word |= bit;
90
+ } else {
91
+ *word &= ~bit;
92
+ }
93
+ return normal(value);
94
+ }
95
+
96
+ OseoResult oseo_environment_clone(
97
+ OseoContext *context,
98
+ OseoValue environment_value
99
+ ) {
100
+ if (!is_environment(environment_value)) {
101
+ return failure(context, "OSEO2001", "Value is not an environment.");
102
+ }
103
+ size_t slot_count = environment_object(environment_value)->slot_count;
104
+ OseoResult created = oseo_environment_create(context, slot_count);
105
+ if (created.status != OSEO_STATUS_NORMAL) return created;
106
+ OseoEnvironment *source = environment_object(environment_value);
107
+ OseoEnvironment *target = environment_object(created.value);
108
+ if (slot_count > 0u) {
109
+ memcpy(
110
+ target->slots,
111
+ source->slots,
112
+ slot_count * sizeof(*target->slots)
113
+ );
114
+ }
115
+ if (source->heap_word_count > 0u) {
116
+ memcpy(
117
+ environment_heap_bitmap(target),
118
+ environment_heap_bitmap(source),
119
+ source->heap_word_count * sizeof(uint64_t)
120
+ );
121
+ }
122
+ return created;
123
+ }
124
+
125
+ OseoResult oseo_cell_create(OseoContext *context, OseoValue value) {
126
+ OseoCell *cell = oseo_internal_allocate_heap_bytes(context, sizeof(*cell));
127
+ if (cell == NULL) {
128
+ return failure(context, "OSEO2001", "Binding cell allocation failed.");
129
+ }
130
+ cell->value = value;
131
+ cell->object = oseo_undefined();
132
+ cell->key = oseo_undefined();
133
+ cell->object_environment = false;
134
+ cell->writable = true;
135
+ cell->unresolvable_message = NULL;
136
+ return oseo_internal_publish_heap(context, &cell->header, OSEO_HEAP_CELL);
137
+ }
138
+
139
+ OseoResult oseo_unresolvable_cell_create(
140
+ OseoContext *context,
141
+ const char *message
142
+ ) {
143
+ if (message == NULL) {
144
+ return failure(context, "OSEO2001", "Invalid unresolvable reference.");
145
+ }
146
+ OseoResult result = oseo_cell_create(context, oseo_uninitialized());
147
+ if (result.status != OSEO_STATUS_NORMAL) return result;
148
+ cell_object(result.value)->unresolvable_message = message;
149
+ return result;
150
+ }
151
+
152
+ /* Object Environment Record HasBinding uses [[HasProperty]], including
153
+ * inherited properties. The key is already a property key, so this walk
154
+ * needs neither coercion nor an allocation safepoint. */
155
+ static bool object_environment_has_property(
156
+ OseoValue object_value,
157
+ OseoValue key
158
+ ) {
159
+ OseoValue current = object_value;
160
+ while (is_object(current)) {
161
+ OseoValue value = oseo_undefined();
162
+ OseoPropertyAttributes attributes = {false, false, false, false};
163
+ OseoValue getter = oseo_undefined();
164
+ OseoValue setter = oseo_undefined();
165
+ if (oseo_internal_own_descriptor(
166
+ current,
167
+ key,
168
+ &value,
169
+ &attributes,
170
+ &getter,
171
+ &setter
172
+ )) {
173
+ return true;
174
+ }
175
+ current = ordinary_object(current)->prototype;
176
+ }
177
+ return false;
178
+ }
179
+
180
+ OseoResult oseo_cell_get(OseoContext *context, OseoValue cell_value) {
181
+ if (!is_cell(cell_value)) {
182
+ return failure(context, "OSEO2001", "Value is not a binding cell.");
183
+ }
184
+ OseoCell *cell = cell_object(cell_value);
185
+ /* GetValue on an unresolvable Reference throws before any binding or
186
+ * property lookup, and this cell exists only to reach that throw. */
187
+ if (cell->unresolvable_message != NULL) {
188
+ return oseo_internal_throw_error(
189
+ context,
190
+ OSEO_ERROR_REFERENCE,
191
+ cell->unresolvable_message
192
+ );
193
+ }
194
+ if (cell->object_environment) {
195
+ if (!object_environment_has_property(cell->object, cell->key)) {
196
+ return oseo_internal_throw_error(
197
+ context,
198
+ OSEO_ERROR_REFERENCE,
199
+ "Global binding does not exist."
200
+ );
201
+ }
202
+ return oseo_object_get(context, cell->object, cell->key);
203
+ }
204
+ return oseo_read_binding(context, cell->value);
205
+ }
206
+
207
+ OseoResult oseo_cell_set(
208
+ OseoContext *context,
209
+ OseoValue cell_value,
210
+ OseoValue value
211
+ ) {
212
+ if (!is_cell(cell_value)) {
213
+ return failure(context, "OSEO2001", "Value is not a binding cell.");
214
+ }
215
+ if (tag_of(cell_object(cell_value)->value) == OSEO_TAG_UNINITIALIZED) {
216
+ return oseo_internal_throw_error(
217
+ context,
218
+ OSEO_ERROR_REFERENCE,
219
+ "Binding was assigned before initialization."
220
+ );
221
+ }
222
+ cell_object(cell_value)->value = value;
223
+ return normal(value);
224
+ }
225
+
226
+ OseoResult oseo_cell_initialize(
227
+ OseoContext *context,
228
+ OseoValue cell_value,
229
+ OseoValue value
230
+ ) {
231
+ if (!is_cell(cell_value)) {
232
+ return failure(context, "OSEO2001", "Value is not a binding cell.");
233
+ }
234
+ if (tag_of(cell_object(cell_value)->value) != OSEO_TAG_UNINITIALIZED) {
235
+ return failure(context, "OSEO2001", "Binding is already initialized.");
236
+ }
237
+ cell_object(cell_value)->value = value;
238
+ return normal(value);
239
+ }
240
+
241
+ OseoResult oseo_module_namespace_create(
242
+ OseoContext *context,
243
+ OseoValue environment,
244
+ size_t count,
245
+ const uint16_t *const *names,
246
+ const size_t *name_lengths,
247
+ const size_t *binding_ids
248
+ ) {
249
+ if (count > 0u &&
250
+ (names == NULL || name_lengths == NULL || binding_ids == NULL)) {
251
+ return failure(context, "OSEO2001", "Invalid module namespace.");
252
+ }
253
+ OseoRootFrame frame = {NULL, NULL, 0u};
254
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
255
+ if (result.status != OSEO_STATUS_NORMAL) return result;
256
+ result = oseo_object_create(context, oseo_null());
257
+ frame.slots[0] = result.value;
258
+ for (size_t index = 0u;
259
+ result.status == OSEO_STATUS_NORMAL && index < count;
260
+ index += 1u) {
261
+ result = oseo_string_from_units(
262
+ context,
263
+ names[index],
264
+ name_lengths[index]
265
+ );
266
+ frame.slots[1] = result.value;
267
+ if (result.status != OSEO_STATUS_NORMAL) break;
268
+ result = oseo_environment_get(
269
+ context,
270
+ environment,
271
+ binding_ids[index]
272
+ );
273
+ frame.slots[2] = result.value;
274
+ if (result.status != OSEO_STATUS_NORMAL) break;
275
+ result = oseo_object_define(
276
+ context,
277
+ frame.slots[0],
278
+ frame.slots[1],
279
+ frame.slots[2],
280
+ (OseoPropertyAttributes){false, true, true, false}
281
+ );
282
+ }
283
+ if (result.status == OSEO_STATUS_NORMAL) {
284
+ result = oseo_internal_well_known_symbol(
285
+ context,
286
+ OSEO_WELL_KNOWN_TO_STRING_TAG
287
+ );
288
+ frame.slots[1] = result.value;
289
+ }
290
+ if (result.status == OSEO_STATUS_NORMAL) {
291
+ result = oseo_internal_ascii_string(context, "Module");
292
+ frame.slots[2] = result.value;
293
+ }
294
+ if (result.status == OSEO_STATUS_NORMAL) {
295
+ result = oseo_object_define(
296
+ context,
297
+ frame.slots[0],
298
+ frame.slots[1],
299
+ frame.slots[2],
300
+ (OseoPropertyAttributes){false, false, false, false}
301
+ );
302
+ }
303
+ if (result.status == OSEO_STATUS_NORMAL) {
304
+ OseoOrdinaryObject *object = ordinary_object(frame.slots[0]);
305
+ object->dictionary = true;
306
+ object->extensible = false;
307
+ object->module_namespace = true;
308
+ result.value = frame.slots[0];
309
+ }
310
+ oseo_roots_release(context, &frame);
311
+ return result;
312
+ }
313
+
314
+ /*
315
+ * The realm's global this value, created on first use. It is an
316
+ * ordinary extensible object created the way this profile creates an object
317
+ * literal, so it reaches the realm-owned %Object.prototype%. A temporary
318
+ * root owns it while standard globals are installed. Publishing only the
319
+ * completed object keeps one permanent realm identity and lets a failed
320
+ * installation retry instead of exposing a partial global.
321
+ *
322
+ * The object is not put in dictionary mode. A var-scoped Script property
323
+ * stores its binding cell rather than its value, but the property cache
324
+ * excludes those slots individually, so the cache still serves the
325
+ * ordinary properties a program adds to this object.
326
+ */
327
+ static OseoResult global_this_object(OseoContext *context) {
328
+ if (tag_of(context->realm->global_this) != OSEO_TAG_UNDEFINED) {
329
+ return normal(context->realm->global_this);
330
+ }
331
+ OseoResult result = oseo_object_literal_create(context);
332
+ if (result.status != OSEO_STATUS_NORMAL) return result;
333
+ OseoValue slots[3] = {
334
+ result.value,
335
+ oseo_undefined(),
336
+ oseo_undefined(),
337
+ };
338
+ OseoRootFrame frame = {NULL, slots, 3u};
339
+ oseo_roots_push(context, &frame);
340
+ ordinary_object(frame.slots[0])->global_object = true;
341
+ result = oseo_internal_install_object_global(context, frame.slots[0]);
342
+ if (result.status == OSEO_STATUS_NORMAL) {
343
+ result = oseo_internal_install_boolean_global(
344
+ context,
345
+ frame.slots[0]
346
+ );
347
+ }
348
+ if (result.status == OSEO_STATUS_NORMAL) {
349
+ result = oseo_internal_install_array_global(
350
+ context,
351
+ frame.slots[0]
352
+ );
353
+ }
354
+ if (result.status == OSEO_STATUS_NORMAL) {
355
+ result = oseo_internal_install_number_global(
356
+ context,
357
+ frame.slots[0]
358
+ );
359
+ }
360
+ if (result.status == OSEO_STATUS_NORMAL) {
361
+ result = oseo_internal_install_string_global(
362
+ context,
363
+ frame.slots[0]
364
+ );
365
+ }
366
+ if (result.status == OSEO_STATUS_NORMAL) {
367
+ result = oseo_internal_install_promise_global(
368
+ context,
369
+ frame.slots[0]
370
+ );
371
+ }
372
+ if (result.status == OSEO_STATUS_NORMAL) {
373
+ result = oseo_internal_install_array_buffer_global(
374
+ context,
375
+ frame.slots[0]
376
+ );
377
+ }
378
+ if (result.status == OSEO_STATUS_NORMAL) {
379
+ result = oseo_internal_install_typed_array_globals(
380
+ context,
381
+ frame.slots[0]
382
+ );
383
+ }
384
+ if (result.status == OSEO_STATUS_NORMAL) {
385
+ result = oseo_internal_install_map_global(
386
+ context,
387
+ frame.slots[0]
388
+ );
389
+ }
390
+ if (result.status == OSEO_STATUS_NORMAL) {
391
+ result = oseo_internal_install_set_global(
392
+ context,
393
+ frame.slots[0]
394
+ );
395
+ }
396
+ if (result.status == OSEO_STATUS_NORMAL) {
397
+ result = oseo_internal_install_weak_collection_globals(
398
+ context,
399
+ frame.slots[0]
400
+ );
401
+ }
402
+ if (result.status == OSEO_STATUS_NORMAL) {
403
+ result = oseo_internal_install_bigint_global(
404
+ context,
405
+ frame.slots[0]
406
+ );
407
+ }
408
+ if (result.status == OSEO_STATUS_NORMAL) {
409
+ result = oseo_internal_install_data_view_global(
410
+ context,
411
+ frame.slots[0]
412
+ );
413
+ }
414
+ if (result.status == OSEO_STATUS_NORMAL) {
415
+ result = oseo_internal_install_date_global(
416
+ context,
417
+ frame.slots[0]
418
+ );
419
+ }
420
+ if (result.status == OSEO_STATUS_NORMAL) {
421
+ result = oseo_internal_install_regexp_global(
422
+ context,
423
+ frame.slots[0]
424
+ );
425
+ }
426
+ if (result.status == OSEO_STATUS_NORMAL) {
427
+ result = oseo_internal_install_math_global(
428
+ context,
429
+ frame.slots[0]
430
+ );
431
+ }
432
+ if (result.status == OSEO_STATUS_NORMAL) {
433
+ result = oseo_internal_install_uri_global(
434
+ context,
435
+ frame.slots[0]
436
+ );
437
+ }
438
+ if (result.status == OSEO_STATUS_NORMAL) {
439
+ result = oseo_internal_install_global_numeric_functions(
440
+ context,
441
+ frame.slots[0]
442
+ );
443
+ }
444
+ if (result.status == OSEO_STATUS_NORMAL) {
445
+ result = oseo_internal_install_reflect_global(
446
+ context,
447
+ frame.slots[0]
448
+ );
449
+ }
450
+ if (result.status == OSEO_STATUS_NORMAL) {
451
+ result = oseo_internal_install_proxy_global(
452
+ context,
453
+ frame.slots[0]
454
+ );
455
+ }
456
+ if (result.status == OSEO_STATUS_NORMAL) {
457
+ result = oseo_internal_install_json_global(
458
+ context,
459
+ frame.slots[0]
460
+ );
461
+ }
462
+ if (result.status == OSEO_STATUS_NORMAL) {
463
+ result = oseo_internal_install_shared_array_buffer_global(
464
+ context,
465
+ frame.slots[0]
466
+ );
467
+ }
468
+ if (result.status == OSEO_STATUS_NORMAL) {
469
+ result = oseo_internal_install_atomics_global(
470
+ context,
471
+ frame.slots[0]
472
+ );
473
+ }
474
+ if (result.status == OSEO_STATUS_NORMAL) {
475
+ result = oseo_internal_install_disposal_globals(
476
+ context,
477
+ frame.slots[0]
478
+ );
479
+ }
480
+ if (result.status == OSEO_STATUS_NORMAL &&
481
+ context->realm == &context->initial_realm) {
482
+ result = oseo_internal_install_process_global(context, frame.slots[0]);
483
+ }
484
+ static const char *const intrinsic_names[] = {
485
+ "Function",
486
+ "Symbol",
487
+ "Error",
488
+ "EvalError",
489
+ "RangeError",
490
+ "ReferenceError",
491
+ "SyntaxError",
492
+ "TypeError",
493
+ "URIError",
494
+ "AggregateError",
495
+ "SuppressedError",
496
+ "Iterator",
497
+ "eval",
498
+ };
499
+ static const OseoIntrinsic intrinsic_values[] = {
500
+ OSEO_INTRINSIC_FUNCTION,
501
+ OSEO_INTRINSIC_SYMBOL,
502
+ OSEO_INTRINSIC_ERROR,
503
+ OSEO_INTRINSIC_EVAL_ERROR,
504
+ OSEO_INTRINSIC_RANGE_ERROR,
505
+ OSEO_INTRINSIC_REFERENCE_ERROR,
506
+ OSEO_INTRINSIC_SYNTAX_ERROR,
507
+ OSEO_INTRINSIC_TYPE_ERROR,
508
+ OSEO_INTRINSIC_URI_ERROR,
509
+ OSEO_INTRINSIC_AGGREGATE_ERROR,
510
+ OSEO_INTRINSIC_SUPPRESSED_ERROR,
511
+ OSEO_INTRINSIC_ITERATOR,
512
+ OSEO_INTRINSIC_EVAL,
513
+ };
514
+ _Static_assert(
515
+ sizeof(intrinsic_names) / sizeof(intrinsic_names[0]) ==
516
+ sizeof(intrinsic_values) / sizeof(intrinsic_values[0]),
517
+ "Intrinsic global tables must stay aligned."
518
+ );
519
+ const size_t intrinsic_count =
520
+ sizeof(intrinsic_names) / sizeof(intrinsic_names[0]);
521
+ for (size_t index = 0u;
522
+ result.status == OSEO_STATUS_NORMAL && index < intrinsic_count;
523
+ index += 1u) {
524
+ result = oseo_internal_intrinsic(context, intrinsic_values[index]);
525
+ frame.slots[1] = result.value;
526
+ if (result.status != OSEO_STATUS_NORMAL) break;
527
+ result = oseo_internal_ascii_string(context, intrinsic_names[index]);
528
+ frame.slots[2] = result.value;
529
+ if (result.status != OSEO_STATUS_NORMAL) break;
530
+ result = oseo_object_define(
531
+ context,
532
+ frame.slots[0],
533
+ frame.slots[2],
534
+ frame.slots[1],
535
+ (OseoPropertyAttributes){true, false, true, false}
536
+ );
537
+ }
538
+ static const char *const value_names[] = {
539
+ "Infinity",
540
+ "NaN",
541
+ "undefined",
542
+ };
543
+ const OseoValue value_values[] = {
544
+ oseo_number(INFINITY),
545
+ oseo_number(NAN),
546
+ oseo_undefined(),
547
+ };
548
+ _Static_assert(
549
+ sizeof(value_names) / sizeof(value_names[0]) ==
550
+ sizeof(value_values) / sizeof(value_values[0]),
551
+ "Intrinsic value tables must stay aligned."
552
+ );
553
+ const size_t value_count =
554
+ sizeof(value_names) / sizeof(value_names[0]);
555
+ for (size_t index = 0u;
556
+ result.status == OSEO_STATUS_NORMAL && index < value_count;
557
+ index += 1u) {
558
+ result = oseo_internal_ascii_string(context, value_names[index]);
559
+ frame.slots[1] = result.value;
560
+ if (result.status != OSEO_STATUS_NORMAL) break;
561
+ result = oseo_cell_create(context, value_values[index]);
562
+ frame.slots[2] = result.value;
563
+ if (result.status != OSEO_STATUS_NORMAL) break;
564
+ cell_object(frame.slots[2])->writable = false;
565
+ result = oseo_object_define(
566
+ context,
567
+ frame.slots[0],
568
+ frame.slots[1],
569
+ frame.slots[2],
570
+ (OseoPropertyAttributes){false, false, false, false}
571
+ );
572
+ }
573
+ /* The global object binds itself as `globalThis`, a writable,
574
+ * non-enumerable, configurable data property. It is installed last so
575
+ * that a failed intrinsic installation leaves no realm holding a
576
+ * partially populated global object through its own name. */
577
+ if (result.status == OSEO_STATUS_NORMAL) {
578
+ result = oseo_internal_ascii_string(context, "globalThis");
579
+ frame.slots[1] = result.value;
580
+ }
581
+ if (result.status == OSEO_STATUS_NORMAL) {
582
+ result = oseo_object_define(
583
+ context,
584
+ frame.slots[0],
585
+ frame.slots[1],
586
+ frame.slots[0],
587
+ (OseoPropertyAttributes){true, false, true, false}
588
+ );
589
+ }
590
+ if (result.status == OSEO_STATUS_NORMAL) {
591
+ context->realm->global_this = frame.slots[0];
592
+ result.value = frame.slots[0];
593
+ }
594
+ oseo_roots_pop(context, &frame);
595
+ return result;
596
+ }
597
+
598
+ OseoResult oseo_this_value(OseoContext *context, OseoValue receiver) {
599
+ if (is_nullish(receiver)) return global_this_object(context);
600
+ return oseo_internal_to_object(context, receiver);
601
+ }
602
+
603
+ OseoResult oseo_global_object_create(
604
+ OseoContext *context,
605
+ OseoValue environment,
606
+ size_t lexical_count,
607
+ const uint16_t *const *lexical_names,
608
+ const size_t *lexical_name_lengths,
609
+ const size_t *lexical_lines,
610
+ const size_t *lexical_columns,
611
+ size_t binding_count,
612
+ const uint16_t *const *names,
613
+ const size_t *name_lengths,
614
+ const size_t *binding_lines,
615
+ const size_t *binding_columns,
616
+ const size_t *binding_ids,
617
+ const bool *function_declarations,
618
+ const bool *web_legacy_functions
619
+ ) {
620
+ if ((lexical_count > 0u &&
621
+ (lexical_names == NULL || lexical_name_lengths == NULL ||
622
+ lexical_lines == NULL || lexical_columns == NULL)) ||
623
+ (binding_count > 0u &&
624
+ (names == NULL || name_lengths == NULL || binding_lines == NULL ||
625
+ binding_columns == NULL || binding_ids == NULL ||
626
+ function_declarations == NULL))) {
627
+ return failure(context, "OSEO2001", "Invalid global object.");
628
+ }
629
+ OseoRootFrame frame = {NULL, NULL, 0u};
630
+ OseoResult result = oseo_roots_allocate(context, &frame, 3u);
631
+ if (result.status != OSEO_STATUS_NORMAL) return result;
632
+ result = global_this_object(context);
633
+ frame.slots[0] = result.value;
634
+ /* HasRestrictedGlobalProperty runs for every lexical name before any
635
+ * var-scoped declaration changes the global object. */
636
+ for (size_t index = 0u;
637
+ result.status == OSEO_STATUS_NORMAL && index < lexical_count;
638
+ index += 1u) {
639
+ oseo_context_location(
640
+ context,
641
+ lexical_lines[index],
642
+ lexical_columns[index]
643
+ );
644
+ result = oseo_string_from_units(
645
+ context,
646
+ lexical_names[index],
647
+ lexical_name_lengths[index]
648
+ );
649
+ frame.slots[1] = result.value;
650
+ if (result.status != OSEO_STATUS_NORMAL) break;
651
+ OseoValue lexicals = context->realm->global_lexicals;
652
+ if (is_object(lexicals) &&
653
+ oseo_internal_own_property_index(
654
+ ordinary_object(lexicals), frame.slots[1]
655
+ ) != SIZE_MAX) {
656
+ result = oseo_internal_throw_error(
657
+ context, OSEO_ERROR_SYNTAX,
658
+ "A Script declaration conflicts with a lexical binding."
659
+ );
660
+ break;
661
+ }
662
+ OseoValue value = oseo_undefined();
663
+ OseoPropertyAttributes attributes = {false, false, false, false};
664
+ OseoValue getter = oseo_undefined();
665
+ OseoValue setter = oseo_undefined();
666
+ bool exists = oseo_internal_own_descriptor(
667
+ frame.slots[0],
668
+ frame.slots[1],
669
+ &value,
670
+ &attributes,
671
+ &getter,
672
+ &setter
673
+ );
674
+ if (exists && !attributes.configurable) {
675
+ result = oseo_internal_throw_error(
676
+ context,
677
+ OSEO_ERROR_SYNTAX,
678
+ "A lexical declaration shadows a restricted global."
679
+ );
680
+ }
681
+ }
682
+ /* CanDeclareGlobalFunction and CanDeclareGlobalVar validate the whole
683
+ * declaration set before CreateGlobal*Binding mutates any property.
684
+ * An Annex B.3.2.2 name is never rejected; creation decides whether
685
+ * it is bound. */
686
+ for (size_t index = 0u;
687
+ result.status == OSEO_STATUS_NORMAL && index < binding_count;
688
+ index += 1u) {
689
+ if (web_legacy_functions != NULL && web_legacy_functions[index]) {
690
+ continue;
691
+ }
692
+ oseo_context_location(
693
+ context,
694
+ binding_lines[index],
695
+ binding_columns[index]
696
+ );
697
+ result = oseo_string_from_units(
698
+ context,
699
+ names[index],
700
+ name_lengths[index]
701
+ );
702
+ frame.slots[1] = result.value;
703
+ if (result.status != OSEO_STATUS_NORMAL) break;
704
+ OseoValue lexicals = context->realm->global_lexicals;
705
+ if (is_object(lexicals) &&
706
+ oseo_internal_own_property_index(
707
+ ordinary_object(lexicals), frame.slots[1]
708
+ ) != SIZE_MAX) {
709
+ result = oseo_internal_throw_error(
710
+ context, OSEO_ERROR_SYNTAX,
711
+ "A Script declaration conflicts with a lexical binding."
712
+ );
713
+ break;
714
+ }
715
+ OseoValue value = oseo_undefined();
716
+ OseoPropertyAttributes attributes = {false, false, false, false};
717
+ OseoValue getter = oseo_undefined();
718
+ OseoValue setter = oseo_undefined();
719
+ bool exists = oseo_internal_own_descriptor(
720
+ frame.slots[0],
721
+ frame.slots[1],
722
+ &value,
723
+ &attributes,
724
+ &getter,
725
+ &setter
726
+ );
727
+ bool allowed = exists
728
+ ? !function_declarations[index] || attributes.configurable ||
729
+ (!attributes.accessor && attributes.writable &&
730
+ attributes.enumerable)
731
+ : ordinary_object(frame.slots[0])->extensible;
732
+ if (!allowed) {
733
+ result = oseo_internal_throw_error(
734
+ context,
735
+ OSEO_ERROR_TYPE,
736
+ "A global declaration is not permitted."
737
+ );
738
+ }
739
+ }
740
+ for (size_t index = 0u;
741
+ result.status == OSEO_STATUS_NORMAL && index < binding_count;
742
+ index += 1u) {
743
+ result = oseo_string_from_units(
744
+ context,
745
+ names[index],
746
+ name_lengths[index]
747
+ );
748
+ frame.slots[1] = result.value;
749
+ if (result.status != OSEO_STATUS_NORMAL) break;
750
+ result = oseo_environment_get(
751
+ context,
752
+ environment,
753
+ binding_ids[index]
754
+ );
755
+ frame.slots[2] = result.value;
756
+ if (result.status != OSEO_STATUS_NORMAL) break;
757
+ if (!is_cell(frame.slots[2])) {
758
+ result = failure(
759
+ context,
760
+ "OSEO2001",
761
+ "A global object property needs a binding cell."
762
+ );
763
+ break;
764
+ }
765
+ OseoValue current = oseo_undefined();
766
+ OseoPropertyAttributes attributes = {false, false, false, false};
767
+ OseoValue getter = oseo_undefined();
768
+ OseoValue setter = oseo_undefined();
769
+ bool exists = oseo_internal_own_descriptor(
770
+ frame.slots[0],
771
+ frame.slots[1],
772
+ &current,
773
+ &attributes,
774
+ &getter,
775
+ &setter
776
+ );
777
+ if (web_legacy_functions != NULL && web_legacy_functions[index]) {
778
+ /* B.3.2.2 binds the name only when HasLexicalDeclaration is
779
+ * false and CanDeclareGlobalVar is true. Otherwise the cell
780
+ * stays detached: host Script references resolve the name
781
+ * through the realm, so the copy its evaluation writes there
782
+ * is never observed. */
783
+ OseoValue lexicals = context->realm->global_lexicals;
784
+ bool lexical = is_object(lexicals) &&
785
+ oseo_internal_own_property_index(
786
+ ordinary_object(lexicals), frame.slots[1]
787
+ ) != SIZE_MAX;
788
+ if (lexical ||
789
+ (!exists && !ordinary_object(frame.slots[0])->extensible)) {
790
+ continue;
791
+ }
792
+ }
793
+ if (!exists) {
794
+ result = oseo_object_define(
795
+ context,
796
+ frame.slots[0],
797
+ frame.slots[1],
798
+ frame.slots[2],
799
+ (OseoPropertyAttributes){false, true, true, false}
800
+ );
801
+ continue;
802
+ }
803
+ OseoOrdinaryObject *object = ordinary_object(frame.slots[0]);
804
+ size_t property_index = oseo_internal_own_property_index(
805
+ object,
806
+ frame.slots[1]
807
+ );
808
+ if (property_index == SIZE_MAX) {
809
+ result = failure(context, "OSEO2001", "Global property vanished.");
810
+ break;
811
+ }
812
+ OseoValue cell = object->properties[property_index].value;
813
+ if (!oseo_internal_cell_backed_property(frame.slots[0], cell)) {
814
+ OseoCell *binding = cell_object(frame.slots[2]);
815
+ binding->object = frame.slots[0];
816
+ binding->key = frame.slots[1];
817
+ binding->object_environment = true;
818
+ cell = frame.slots[2];
819
+ } else {
820
+ result = oseo_environment_set(
821
+ context,
822
+ environment,
823
+ binding_ids[index],
824
+ cell
825
+ );
826
+ if (result.status != OSEO_STATUS_NORMAL) break;
827
+ }
828
+ object = ordinary_object(frame.slots[0]);
829
+ OseoProperty *property = &object->properties[property_index];
830
+ if (function_declarations[index] &&
831
+ !cell_object(cell)->object_environment) {
832
+ property->attributes =
833
+ (OseoPropertyAttributes){false, true, true, false};
834
+ object->dictionary = true;
835
+ object->shape_id = context->next_shape_id;
836
+ context->next_shape_id += 1u;
837
+ }
838
+ if (!cell_object(cell)->object_environment) {
839
+ cell_object(cell)->writable = property->attributes.writable;
840
+ }
841
+ }
842
+ if (result.status == OSEO_STATUS_NORMAL) result.value = frame.slots[0];
843
+ oseo_roots_release(context, &frame);
844
+ return result;
845
+ }
846
+
847
+ OseoResult oseo_global_binding_set(
848
+ OseoContext *context,
849
+ OseoValue cell_value,
850
+ OseoValue value,
851
+ bool strict
852
+ ) {
853
+ if (!is_cell(cell_value)) {
854
+ return failure(context, "OSEO2001", "Value is not a binding cell.");
855
+ }
856
+ OseoCell *cell = cell_object(cell_value);
857
+ if (cell->object_environment) {
858
+ bool exists =
859
+ object_environment_has_property(cell->object, cell->key);
860
+ if (!exists && strict) {
861
+ return oseo_internal_throw_error(
862
+ context,
863
+ OSEO_ERROR_REFERENCE,
864
+ "Global binding does not exist."
865
+ );
866
+ }
867
+ return oseo_object_set(
868
+ context,
869
+ cell->object,
870
+ cell->key,
871
+ value,
872
+ strict
873
+ );
874
+ }
875
+ /*
876
+ * A global var or function binding is an object environment record
877
+ * binding, so SetMutableBinding writes through the global object's
878
+ * property. Once [[DefineOwnProperty]] made that property
879
+ * non-writable, the assignment fails: silently outside strict code
880
+ * and with a TypeError inside it.
881
+ */
882
+ if (!cell_object(cell_value)->writable) {
883
+ return strict
884
+ ? oseo_internal_throw_error(
885
+ context,
886
+ OSEO_ERROR_TYPE,
887
+ "Cannot assign to a read-only property."
888
+ )
889
+ : normal(value);
890
+ }
891
+ return oseo_cell_set(context, cell_value, value);
892
+ }
893
+
894
+ OseoResult oseo_global_binding_initialize(
895
+ OseoContext *context,
896
+ OseoValue cell_value,
897
+ OseoValue value
898
+ ) {
899
+ if (!is_cell(cell_value)) {
900
+ return failure(context, "OSEO2001", "Value is not a binding cell.");
901
+ }
902
+ if (cell_object(cell_value)->object_environment) return normal(value);
903
+ if (tag_of(cell_object(cell_value)->value) != OSEO_TAG_UNINITIALIZED) {
904
+ return normal(cell_object(cell_value)->value);
905
+ }
906
+ return oseo_cell_initialize(context, cell_value, value);
907
+ }
908
+
909
+ OseoResult oseo_global_function_initialize(
910
+ OseoContext *context,
911
+ OseoValue cell_value,
912
+ OseoValue value
913
+ ) {
914
+ if (!is_cell(cell_value)) {
915
+ return failure(context, "OSEO2001", "Value is not a binding cell.");
916
+ }
917
+ OseoCell *cell = cell_object(cell_value);
918
+ if (cell->object_environment) {
919
+ return oseo_object_define(
920
+ context,
921
+ cell->object,
922
+ cell->key,
923
+ value,
924
+ (OseoPropertyAttributes){false, true, true, false}
925
+ );
926
+ }
927
+ cell->value = value;
928
+ return normal(value);
929
+ }
930
+
931
+ /* ResolveBinding selects its record before any assignment operand runs. */
932
+ OseoResult oseo_global_environment(OseoContext *context, OseoValue key) {
933
+ OseoValue lexicals = context->realm->global_lexicals;
934
+ if (is_object(lexicals) && oseo_internal_own_property_index(
935
+ ordinary_object(lexicals), key) != SIZE_MAX) {
936
+ return normal(lexicals);
937
+ }
938
+ return global_this_object(context);
939
+ }
940
+
941
+ OseoResult oseo_script_lexicals_install(
942
+ OseoContext *context, OseoValue environment, size_t count,
943
+ const uint16_t *const *names, const size_t *lengths,
944
+ const size_t *ids, const bool *mutable_bindings
945
+ ) {
946
+ OseoValue slots[3] = {environment, oseo_undefined(), oseo_undefined()};
947
+ OseoRootFrame frame = {NULL, slots, 3u};
948
+ oseo_roots_push(context, &frame);
949
+ OseoResult result = normal(oseo_undefined());
950
+ if (!is_object(context->realm->global_lexicals)) {
951
+ result = oseo_object_create(context, oseo_null());
952
+ if (result.status == OSEO_STATUS_NORMAL) {
953
+ context->realm->global_lexicals = result.value;
954
+ ordinary_object(result.value)->global_object = true;
955
+ ordinary_object(result.value)->dictionary = true;
956
+ }
957
+ }
958
+ for (size_t index = 0u;
959
+ result.status == OSEO_STATUS_NORMAL && index < count; index += 1u) {
960
+ result = oseo_string_from_units(context, names[index], lengths[index]);
961
+ slots[1] = result.value;
962
+ if (result.status != OSEO_STATUS_NORMAL) break;
963
+ result = oseo_environment_get(context, slots[0], ids[index]);
964
+ slots[2] = result.value;
965
+ if (result.status != OSEO_STATUS_NORMAL) break;
966
+ result = oseo_object_define(context, context->realm->global_lexicals,
967
+ slots[1], slots[2],
968
+ (OseoPropertyAttributes){false, false, mutable_bindings[index],
969
+ false});
970
+ }
971
+ oseo_roots_pop(context, &frame);
972
+ return result;
973
+ }
974
+
975
+ /* Keep the caller's existing roots and error routing across the composition. */
976
+ bool oseo_generated_binding_read(
977
+ OseoContext *context, OseoValue environment, size_t index,
978
+ OseoResult *result, OseoValue *output
979
+ ) {
980
+ *result = oseo_environment_get(context, environment, index);
981
+ if (result->status != OSEO_STATUS_NORMAL) return false;
982
+ *result = oseo_cell_get(context, result->value);
983
+ *output = result->value;
984
+ return true;
985
+ }
986
+
987
+ void oseo_generated_operation_location(
988
+ OseoContext *context, const OseoGeneratedOperation *operation
989
+ ) {
990
+ if (operation->source_id != NULL) {
991
+ oseo_context_source_location(context, operation->source_id,
992
+ operation->source_id_length, operation->line, operation->column);
993
+ } else {
994
+ oseo_context_location(context, operation->line, operation->column);
995
+ }
996
+ }
997
+
998
+ bool oseo_generated_binding_read_at(
999
+ OseoContext *context, OseoValue *roots,
1000
+ const OseoGeneratedOperation *operation, OseoResult *result
1001
+ ) {
1002
+ oseo_generated_operation_location(context, operation);
1003
+ return oseo_generated_binding_read(context, roots[operation->first],
1004
+ operation->second, result, &roots[operation->output]);
1005
+ }