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