@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,973 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /* Copyright (c) 2026 Hong Minhee; see ../MIT-LICENSE.txt. */
3
+
4
+ #include "runtime_internal.h"
5
+
6
+ #include <stdio.h>
7
+ #include <stdlib.h>
8
+ #include <string.h>
9
+
10
+ OseoResult oseo_internal_error_builtin_dispatch(
11
+ OseoContext *context,
12
+ size_t code_id,
13
+ OseoValue callee,
14
+ OseoValue receiver,
15
+ size_t argument_count,
16
+ const OseoValue *arguments,
17
+ OseoValue new_target
18
+ ) {
19
+ if (code_id >= OSEO_ERROR_CONSTRUCT_FIRST_CODE_ID &&
20
+ code_id <= OSEO_ERROR_CONSTRUCT_LAST_CODE_ID) {
21
+ return oseo_internal_error_construct(
22
+ context,
23
+ callee,
24
+ new_target,
25
+ code_id,
26
+ argument_count,
27
+ arguments
28
+ );
29
+ }
30
+ if (code_id == OSEO_ERROR_TO_STRING_CODE_ID) {
31
+ return oseo_internal_error_to_string(context, receiver);
32
+ }
33
+ if (code_id == OSEO_ERROR_IS_ERROR_CODE_ID) {
34
+ /*
35
+ * Error.isError, 20.5.2.1, reports [[ErrorData]] alone. Every
36
+ * object header carries the flag, so a proxy answers false
37
+ * whatever its target is, and an object that only inherits
38
+ * from an error prototype answers false too.
39
+ */
40
+ OseoValue argument =
41
+ argument_count > 0u && arguments != NULL
42
+ ? arguments[0]
43
+ : oseo_undefined();
44
+ return normal(oseo_boolean(
45
+ is_object(argument) && ordinary_object(argument)->error_data
46
+ ));
47
+ }
48
+ return oseo_unknown_function(context, code_id);
49
+ }
50
+
51
+ /*
52
+ * Named error intrinsics: lazily created constructor and prototype
53
+ * pairs, typed runtime error creation, the Error.prototype.toString
54
+ * algorithm, and unhandled-throw rendering.
55
+ */
56
+
57
+ static const char *const error_names[OSEO_ERROR_KIND_COUNT] = {
58
+ "Error",
59
+ "EvalError",
60
+ "RangeError",
61
+ "ReferenceError",
62
+ "SyntaxError",
63
+ "TypeError",
64
+ "URIError",
65
+ "AggregateError",
66
+ "SuppressedError",
67
+ };
68
+
69
+ static const OseoIntrinsic error_prototype_intrinsics[
70
+ OSEO_ERROR_KIND_COUNT
71
+ ] = {
72
+ OSEO_INTRINSIC_ERROR_PROTOTYPE,
73
+ OSEO_INTRINSIC_EVAL_ERROR_PROTOTYPE,
74
+ OSEO_INTRINSIC_RANGE_ERROR_PROTOTYPE,
75
+ OSEO_INTRINSIC_REFERENCE_ERROR_PROTOTYPE,
76
+ OSEO_INTRINSIC_SYNTAX_ERROR_PROTOTYPE,
77
+ OSEO_INTRINSIC_TYPE_ERROR_PROTOTYPE,
78
+ OSEO_INTRINSIC_URI_ERROR_PROTOTYPE,
79
+ OSEO_INTRINSIC_AGGREGATE_ERROR_PROTOTYPE,
80
+ OSEO_INTRINSIC_SUPPRESSED_ERROR_PROTOTYPE,
81
+ };
82
+
83
+ static const OseoIntrinsic error_constructor_intrinsics[
84
+ OSEO_ERROR_KIND_COUNT
85
+ ] = {
86
+ OSEO_INTRINSIC_ERROR,
87
+ OSEO_INTRINSIC_EVAL_ERROR,
88
+ OSEO_INTRINSIC_RANGE_ERROR,
89
+ OSEO_INTRINSIC_REFERENCE_ERROR,
90
+ OSEO_INTRINSIC_SYNTAX_ERROR,
91
+ OSEO_INTRINSIC_TYPE_ERROR,
92
+ OSEO_INTRINSIC_URI_ERROR,
93
+ OSEO_INTRINSIC_AGGREGATE_ERROR,
94
+ OSEO_INTRINSIC_SUPPRESSED_ERROR,
95
+ };
96
+
97
+ static OseoResult ascii_runtime_string(
98
+ OseoContext *context,
99
+ const char *text
100
+ ) {
101
+ size_t length = strlen(text);
102
+ OseoResult valid = oseo_internal_validate_string_length(context, length);
103
+ if (valid.status != OSEO_STATUS_NORMAL) return valid;
104
+ uint16_t *units = NULL;
105
+ if (length > 0u) {
106
+ units = malloc(length * sizeof(uint16_t));
107
+ if (units == NULL) {
108
+ return failure(
109
+ context,
110
+ "OSEO2001",
111
+ "String allocation failed."
112
+ );
113
+ }
114
+ for (size_t index = 0u; index < length; index += 1u) {
115
+ units[index] = (uint16_t)(unsigned char)text[index];
116
+ }
117
+ }
118
+ OseoResult result = oseo_internal_allocate_string(context, units, length);
119
+ free(units);
120
+ return result;
121
+ }
122
+
123
+ /* Define one ASCII-named data property; object and value stay rooted. */
124
+ static OseoResult define_ascii_property(
125
+ OseoContext *context,
126
+ OseoValue object,
127
+ const char *name,
128
+ OseoValue value,
129
+ OseoPropertyAttributes attributes
130
+ ) {
131
+ OseoValue slots[3] = {object, value, oseo_undefined()};
132
+ OseoRootFrame frame = {NULL, slots, 3u};
133
+ oseo_roots_push(context, &frame);
134
+ OseoResult result = ascii_runtime_string(context, name);
135
+ if (result.status == OSEO_STATUS_NORMAL) {
136
+ slots[2] = result.value;
137
+ result = oseo_object_define(
138
+ context,
139
+ slots[0],
140
+ slots[2],
141
+ slots[1],
142
+ attributes
143
+ );
144
+ }
145
+ oseo_roots_pop(context, &frame);
146
+ return result;
147
+ }
148
+
149
+ static OseoResult error_intrinsic_pair(
150
+ OseoContext *context,
151
+ OseoErrorKind kind
152
+ ) {
153
+ OseoValue *constructor_cache =
154
+ &context->realm->intrinsics[error_constructor_intrinsics[kind]];
155
+ OseoValue *prototype_cache =
156
+ &context->realm->intrinsics[error_prototype_intrinsics[kind]];
157
+ if (tag_of(*constructor_cache) != OSEO_TAG_UNDEFINED) {
158
+ return normal(*constructor_cache);
159
+ }
160
+ size_t entry_allocations = context->allocations;
161
+ if (kind != OSEO_ERROR_ERROR) {
162
+ OseoResult base = error_intrinsic_pair(context, OSEO_ERROR_ERROR);
163
+ if (base.status != OSEO_STATUS_NORMAL) return base;
164
+ }
165
+ const OseoPropertyAttributes hidden = {true, false, true, false};
166
+ OseoRootFrame frame = {NULL, NULL, 0u};
167
+ OseoResult result = oseo_roots_allocate(context, &frame, 5u);
168
+ if (result.status != OSEO_STATUS_NORMAL) return result;
169
+ OseoValue parent = kind == OSEO_ERROR_ERROR
170
+ ? context->realm->intrinsics[OSEO_INTRINSIC_OBJECT_PROTOTYPE]
171
+ : context->realm->intrinsics[OSEO_INTRINSIC_ERROR_PROTOTYPE];
172
+ if (kind == OSEO_ERROR_ERROR) {
173
+ result = oseo_internal_intrinsic(
174
+ context,
175
+ OSEO_INTRINSIC_OBJECT_PROTOTYPE
176
+ );
177
+ parent = result.value;
178
+ }
179
+ if (result.status == OSEO_STATUS_NORMAL) {
180
+ result = oseo_object_create(context, parent);
181
+ }
182
+ frame.slots[0] = result.value;
183
+ if (result.status == OSEO_STATUS_NORMAL) {
184
+ result = ascii_runtime_string(context, error_names[kind]);
185
+ frame.slots[1] = result.value;
186
+ }
187
+ if (result.status == OSEO_STATUS_NORMAL) {
188
+ result = define_ascii_property(
189
+ context,
190
+ frame.slots[0],
191
+ "name",
192
+ frame.slots[1],
193
+ hidden
194
+ );
195
+ }
196
+ if (result.status == OSEO_STATUS_NORMAL) {
197
+ result = ascii_runtime_string(context, "");
198
+ frame.slots[2] = result.value;
199
+ }
200
+ if (result.status == OSEO_STATUS_NORMAL) {
201
+ result = define_ascii_property(
202
+ context,
203
+ frame.slots[0],
204
+ "message",
205
+ frame.slots[2],
206
+ hidden
207
+ );
208
+ }
209
+ if (result.status == OSEO_STATUS_NORMAL && kind == OSEO_ERROR_ERROR) {
210
+ result = oseo_environment_create(context, 0u);
211
+ frame.slots[2] = result.value;
212
+ if (result.status == OSEO_STATUS_NORMAL) {
213
+ static const uint16_t to_string_units[] = {
214
+ 't', 'o', 'S', 't', 'r', 'i', 'n', 'g',
215
+ };
216
+ result = oseo_function_create(
217
+ context,
218
+ OSEO_ERROR_TO_STRING_CODE_ID,
219
+ frame.slots[2],
220
+ to_string_units,
221
+ sizeof(to_string_units) / sizeof(*to_string_units),
222
+ 0u,
223
+ OSEO_FUNCTION_INTERNAL,
224
+ oseo_undefined(),
225
+ oseo_undefined(),
226
+ OSEO_FUNCTION_NAME_PREFIX_NONE
227
+ );
228
+ frame.slots[3] = result.value;
229
+ }
230
+ if (result.status == OSEO_STATUS_NORMAL) {
231
+ result = define_ascii_property(
232
+ context,
233
+ frame.slots[0],
234
+ "toString",
235
+ frame.slots[3],
236
+ hidden
237
+ );
238
+ }
239
+ }
240
+ if (result.status == OSEO_STATUS_NORMAL) {
241
+ result = oseo_environment_create(context, 0u);
242
+ frame.slots[2] = result.value;
243
+ }
244
+ if (result.status == OSEO_STATUS_NORMAL) {
245
+ uint16_t name_units[16];
246
+ size_t name_length = strlen(error_names[kind]);
247
+ for (size_t index = 0u; index < name_length; index += 1u) {
248
+ name_units[index] =
249
+ (uint16_t)(unsigned char)error_names[kind][index];
250
+ }
251
+ result = oseo_function_create(
252
+ context,
253
+ OSEO_ERROR_CONSTRUCT_LAST_CODE_ID - (size_t)kind,
254
+ frame.slots[2],
255
+ name_units,
256
+ name_length,
257
+ kind == OSEO_ERROR_AGGREGATE
258
+ ? 2u
259
+ : kind == OSEO_ERROR_SUPPRESSED ? 3u : 1u,
260
+ OSEO_FUNCTION_ORDINARY,
261
+ oseo_undefined(),
262
+ oseo_undefined(),
263
+ OSEO_FUNCTION_NAME_PREFIX_NONE
264
+ );
265
+ frame.slots[3] = result.value;
266
+ }
267
+ if (result.status == OSEO_STATUS_NORMAL) {
268
+ OseoFunction *constructor = function_object(frame.slots[3]);
269
+ constructor->prototype_object = frame.slots[0];
270
+ constructor->prototype_writable = false;
271
+ if (kind != OSEO_ERROR_ERROR) {
272
+ constructor->ordinary.prototype =
273
+ context->realm->intrinsics[OSEO_INTRINSIC_ERROR];
274
+ }
275
+ result = define_ascii_property(
276
+ context,
277
+ frame.slots[0],
278
+ "constructor",
279
+ frame.slots[3],
280
+ hidden
281
+ );
282
+ }
283
+ if (result.status == OSEO_STATUS_NORMAL && kind == OSEO_ERROR_ERROR) {
284
+ result = oseo_environment_create(context, 0u);
285
+ frame.slots[2] = result.value;
286
+ if (result.status == OSEO_STATUS_NORMAL) {
287
+ static const uint16_t is_error_units[] = {
288
+ 'i', 's', 'E', 'r', 'r', 'o', 'r',
289
+ };
290
+ result = oseo_function_create(
291
+ context,
292
+ OSEO_ERROR_IS_ERROR_CODE_ID,
293
+ frame.slots[2],
294
+ is_error_units,
295
+ sizeof(is_error_units) / sizeof(*is_error_units),
296
+ 1u,
297
+ OSEO_FUNCTION_INTERNAL,
298
+ oseo_undefined(),
299
+ oseo_undefined(),
300
+ OSEO_FUNCTION_NAME_PREFIX_NONE
301
+ );
302
+ frame.slots[4] = result.value;
303
+ }
304
+ if (result.status == OSEO_STATUS_NORMAL) {
305
+ result = define_ascii_property(
306
+ context,
307
+ frame.slots[3],
308
+ "isError",
309
+ frame.slots[4],
310
+ hidden
311
+ );
312
+ }
313
+ }
314
+ if (result.status == OSEO_STATUS_NORMAL) {
315
+ *prototype_cache = frame.slots[0];
316
+ *constructor_cache = frame.slots[3];
317
+ result.value = frame.slots[3];
318
+ if (context->observe_specialization) {
319
+ context->allocations = entry_allocations;
320
+ }
321
+ }
322
+ oseo_roots_release(context, &frame);
323
+ return result;
324
+ }
325
+
326
+ OseoResult oseo_error_intrinsic(OseoContext *context, OseoErrorKind kind) {
327
+ if ((size_t)kind >= OSEO_ERROR_KIND_COUNT) {
328
+ return failure(context, "OSEO2001", "Unknown error intrinsic.");
329
+ }
330
+ return error_intrinsic_pair(context, kind);
331
+ }
332
+
333
+ OseoResult oseo_internal_error_prototype(
334
+ OseoContext *context,
335
+ OseoErrorKind kind
336
+ ) {
337
+ OseoResult result = error_intrinsic_pair(context, kind);
338
+ if (result.status != OSEO_STATUS_NORMAL) return result;
339
+ return normal(context->realm->intrinsics[error_prototype_intrinsics[kind]]);
340
+ }
341
+
342
+ OseoResult oseo_internal_throw_error(
343
+ OseoContext *context,
344
+ OseoErrorKind kind,
345
+ const char *message
346
+ ) {
347
+ OseoResult result = oseo_internal_error_prototype(context, kind);
348
+ if (result.status != OSEO_STATUS_NORMAL) return result;
349
+ OseoValue slots[2] = {result.value, oseo_undefined()};
350
+ OseoRootFrame frame = {NULL, slots, 2u};
351
+ oseo_roots_push(context, &frame);
352
+ result = oseo_object_create(context, slots[0]);
353
+ if (result.status == OSEO_STATUS_NORMAL) {
354
+ slots[1] = result.value;
355
+ ordinary_object(slots[1])->error_data = true;
356
+ result = ascii_runtime_string(context, message);
357
+ }
358
+ if (result.status == OSEO_STATUS_NORMAL) {
359
+ const OseoPropertyAttributes hidden = {true, false, true, false};
360
+ result = define_ascii_property(
361
+ context,
362
+ slots[1],
363
+ "message",
364
+ result.value,
365
+ hidden
366
+ );
367
+ }
368
+ oseo_roots_pop(context, &frame);
369
+ if (result.status != OSEO_STATUS_NORMAL) return result;
370
+ context->error_code = "OSEO2001";
371
+ context->error_message = message;
372
+ context->has_diagnostic = false;
373
+ result.status = OSEO_STATUS_THROW;
374
+ result.value = slots[1];
375
+ return result;
376
+ }
377
+
378
+ /* InstallErrorCause after message conversion and before aggregate iteration. */
379
+ static OseoResult install_error_cause(
380
+ OseoContext *context,
381
+ OseoValue object,
382
+ OseoValue options
383
+ ) {
384
+ if (!is_object(options)) return normal(object);
385
+ const OseoPropertyAttributes hidden = {true, false, true, false};
386
+ OseoValue slots[3] = {object, options, oseo_undefined()};
387
+ OseoRootFrame frame = {NULL, slots, 3u};
388
+ oseo_roots_push(context, &frame);
389
+ OseoResult result = ascii_runtime_string(context, "cause");
390
+ slots[2] = result.value;
391
+ if (result.status == OSEO_STATUS_NORMAL) {
392
+ result = oseo_has_property(context, slots[2], slots[1]);
393
+ }
394
+ if (result.status == OSEO_STATUS_NORMAL && oseo_to_boolean(result.value)) {
395
+ result = oseo_object_get(context, slots[1], slots[2]);
396
+ if (result.status == OSEO_STATUS_NORMAL) {
397
+ result = define_ascii_property(
398
+ context,
399
+ slots[0],
400
+ "cause",
401
+ result.value,
402
+ hidden
403
+ );
404
+ }
405
+ }
406
+ oseo_roots_pop(context, &frame);
407
+ return result;
408
+ }
409
+
410
+ OseoResult oseo_internal_error_construct(
411
+ OseoContext *context,
412
+ OseoValue callee,
413
+ OseoValue new_target,
414
+ size_t code_id,
415
+ size_t argument_count,
416
+ const OseoValue *arguments
417
+ ) {
418
+ OseoErrorKind kind =
419
+ (OseoErrorKind)(OSEO_ERROR_CONSTRUCT_LAST_CODE_ID - code_id);
420
+ const OseoPropertyAttributes hidden = {true, false, true, false};
421
+ OseoRootFrame frame = {NULL, NULL, 0u};
422
+ OseoResult result = oseo_roots_allocate(context, &frame, 8u);
423
+ if (result.status != OSEO_STATUS_NORMAL) return result;
424
+ bool aggregate = kind == OSEO_ERROR_AGGREGATE;
425
+ /* SuppressedError(error, suppressed, message), 20.5.7.1, takes no
426
+ * options argument and so installs no `cause`. */
427
+ bool suppressed = kind == OSEO_ERROR_SUPPRESSED;
428
+ frame.slots[0] = (aggregate || suppressed) && argument_count > 0u
429
+ ? arguments[0]
430
+ : oseo_undefined();
431
+ size_t message_index = aggregate ? 1u : suppressed ? 2u : 0u;
432
+ size_t options_index = aggregate ? 2u : 1u;
433
+ frame.slots[1] = argument_count > message_index
434
+ ? arguments[message_index]
435
+ : oseo_undefined();
436
+ frame.slots[2] = argument_count > options_index && !suppressed
437
+ ? arguments[options_index]
438
+ : oseo_undefined();
439
+ if (suppressed) {
440
+ frame.slots[6] = argument_count > 1u
441
+ ? arguments[1]
442
+ : oseo_undefined();
443
+ }
444
+ /* OrdinaryCreateFromConstructor takes the prototype from the new
445
+ * target, so `class E extends Error {}` gives its instances
446
+ * `E.prototype`. A plain call supplies no new target, and `new Error`
447
+ * supplies the callee itself, so both keep the callee's prototype.
448
+ * 20.5.1.1 runs this before ToString(message), and the read is a
449
+ * real Get: `Reflect.construct` can name a bound function, which
450
+ * owns no synthetic `prototype` slot and can carry an observable
451
+ * accessor instead. */
452
+ frame.slots[7] = function_is_constructible(new_target)
453
+ ? new_target
454
+ : callee;
455
+ result = oseo_internal_constructor_prototype(context, frame.slots[7]);
456
+ frame.slots[4] = result.value;
457
+ if (result.status == OSEO_STATUS_NORMAL && !is_object(frame.slots[4])) {
458
+ result = oseo_internal_constructor_realm_default(
459
+ context,
460
+ frame.slots[7],
461
+ error_prototype_intrinsics[kind]
462
+ );
463
+ frame.slots[4] = result.value;
464
+ }
465
+ if (result.status == OSEO_STATUS_NORMAL) {
466
+ result = oseo_object_create(context, frame.slots[4]);
467
+ frame.slots[3] = result.value;
468
+ frame.slots[4] = oseo_undefined();
469
+ }
470
+ if (result.status == OSEO_STATUS_NORMAL) {
471
+ ordinary_object(frame.slots[3])->error_data = true;
472
+ }
473
+ if (result.status == OSEO_STATUS_NORMAL &&
474
+ tag_of(frame.slots[1]) != OSEO_TAG_UNDEFINED) {
475
+ result = oseo_internal_value_string(context, frame.slots[1]);
476
+ if (result.status == OSEO_STATUS_NORMAL) {
477
+ result = define_ascii_property(
478
+ context,
479
+ frame.slots[3],
480
+ "message",
481
+ result.value,
482
+ hidden
483
+ );
484
+ }
485
+ }
486
+ if (result.status == OSEO_STATUS_NORMAL && !suppressed) {
487
+ result = install_error_cause(
488
+ context,
489
+ frame.slots[3],
490
+ frame.slots[2]
491
+ );
492
+ }
493
+ if (result.status == OSEO_STATUS_NORMAL && suppressed) {
494
+ result = define_ascii_property(
495
+ context,
496
+ frame.slots[3],
497
+ "error",
498
+ frame.slots[0],
499
+ hidden
500
+ );
501
+ }
502
+ if (result.status == OSEO_STATUS_NORMAL && suppressed) {
503
+ result = define_ascii_property(
504
+ context,
505
+ frame.slots[3],
506
+ "suppressed",
507
+ frame.slots[6],
508
+ hidden
509
+ );
510
+ }
511
+ if (result.status == OSEO_STATUS_NORMAL && aggregate) {
512
+ result = oseo_array_create(context, 0u);
513
+ frame.slots[4] = result.value;
514
+ }
515
+ if (result.status == OSEO_STATUS_NORMAL && aggregate) {
516
+ result = oseo_iterator_get(
517
+ context,
518
+ frame.slots[0],
519
+ &frame.slots[6]
520
+ );
521
+ frame.slots[5] = result.value;
522
+ }
523
+ bool done = false;
524
+ while (result.status == OSEO_STATUS_NORMAL && aggregate && !done) {
525
+ result = oseo_iterator_next(
526
+ context,
527
+ frame.slots[5],
528
+ frame.slots[6],
529
+ &frame.slots[7],
530
+ &done
531
+ );
532
+ if (result.status == OSEO_STATUS_NORMAL && !done) {
533
+ result = oseo_array_append(
534
+ context,
535
+ frame.slots[4],
536
+ frame.slots[7]
537
+ );
538
+ }
539
+ }
540
+ if (result.status == OSEO_STATUS_NORMAL && aggregate) {
541
+ result = define_ascii_property(
542
+ context,
543
+ frame.slots[3],
544
+ "errors",
545
+ frame.slots[4],
546
+ hidden
547
+ );
548
+ }
549
+ if (result.status == OSEO_STATUS_NORMAL) {
550
+ result.value = frame.slots[3];
551
+ }
552
+ oseo_roots_release(context, &frame);
553
+ return result;
554
+ }
555
+
556
+ OseoResult oseo_internal_suppressed_error_create(
557
+ OseoContext *context,
558
+ OseoValue error,
559
+ OseoValue suppressed
560
+ ) {
561
+ const OseoPropertyAttributes hidden = {true, false, true, false};
562
+ OseoValue slots[3] = {error, suppressed, oseo_undefined()};
563
+ OseoRootFrame frame = {NULL, slots, 3u};
564
+ oseo_roots_push(context, &frame);
565
+ OseoResult result = oseo_internal_error_prototype(
566
+ context,
567
+ OSEO_ERROR_SUPPRESSED
568
+ );
569
+ if (result.status == OSEO_STATUS_NORMAL) {
570
+ result = oseo_object_create(context, result.value);
571
+ slots[2] = result.value;
572
+ }
573
+ if (result.status == OSEO_STATUS_NORMAL) {
574
+ ordinary_object(slots[2])->error_data = true;
575
+ result = define_ascii_property(
576
+ context,
577
+ slots[2],
578
+ "error",
579
+ slots[0],
580
+ hidden
581
+ );
582
+ }
583
+ if (result.status == OSEO_STATUS_NORMAL) {
584
+ result = define_ascii_property(
585
+ context,
586
+ slots[2],
587
+ "suppressed",
588
+ slots[1],
589
+ hidden
590
+ );
591
+ }
592
+ if (result.status == OSEO_STATUS_NORMAL) result.value = slots[2];
593
+ oseo_roots_pop(context, &frame);
594
+ return result;
595
+ }
596
+
597
+ /* Concatenate "name: message" without entering generic addition. */
598
+ static OseoResult error_text(
599
+ OseoContext *context,
600
+ OseoValue name,
601
+ OseoValue message
602
+ ) {
603
+ OseoString *name_string = string_object(name);
604
+ OseoString *message_string = string_object(message);
605
+ if (name_string->length == 0u) return normal(message);
606
+ if (message_string->length == 0u) return normal(name);
607
+ size_t maximum = (SIZE_MAX - sizeof(OseoString)) / sizeof(uint16_t);
608
+ if (name_string->length > maximum - 2u ||
609
+ message_string->length > maximum - 2u - name_string->length) {
610
+ return failure(context, "OSEO2001", "String allocation is too large.");
611
+ }
612
+ size_t length = name_string->length + 2u + message_string->length;
613
+ OseoResult valid = oseo_internal_validate_string_length(context, length);
614
+ if (valid.status != OSEO_STATUS_NORMAL) return valid;
615
+ uint16_t *units = malloc(length * sizeof(uint16_t));
616
+ if (units == NULL) {
617
+ return failure(context, "OSEO2001", "String allocation failed.");
618
+ }
619
+ memcpy(
620
+ units,
621
+ name_string->units,
622
+ name_string->length * sizeof(uint16_t)
623
+ );
624
+ units[name_string->length] = ':';
625
+ units[name_string->length + 1u] = ' ';
626
+ memcpy(
627
+ units + name_string->length + 2u,
628
+ message_string->units,
629
+ message_string->length * sizeof(uint16_t)
630
+ );
631
+ OseoValue slots[2] = {name, message};
632
+ OseoRootFrame frame = {NULL, slots, 2u};
633
+ oseo_roots_push(context, &frame);
634
+ OseoResult result = oseo_internal_allocate_string(context, units, length);
635
+ oseo_roots_pop(context, &frame);
636
+ free(units);
637
+ return result;
638
+ }
639
+
640
+ OseoResult oseo_internal_error_to_string(
641
+ OseoContext *context,
642
+ OseoValue receiver
643
+ ) {
644
+ if (!is_object(receiver)) {
645
+ return oseo_internal_throw_error(
646
+ context,
647
+ OSEO_ERROR_TYPE,
648
+ "Error.prototype.toString requires an object receiver."
649
+ );
650
+ }
651
+ OseoRootFrame frame = {NULL, NULL, 0u};
652
+ OseoResult result = oseo_roots_allocate(context, &frame, 4u);
653
+ if (result.status != OSEO_STATUS_NORMAL) return result;
654
+ frame.slots[0] = receiver;
655
+ result = ascii_runtime_string(context, "name");
656
+ frame.slots[1] = result.value;
657
+ if (result.status == OSEO_STATUS_NORMAL) {
658
+ result = oseo_object_get(context, frame.slots[0], frame.slots[1]);
659
+ frame.slots[2] = result.value;
660
+ }
661
+ if (result.status == OSEO_STATUS_NORMAL) {
662
+ result = tag_of(frame.slots[2]) == OSEO_TAG_UNDEFINED
663
+ ? ascii_runtime_string(context, "Error")
664
+ : oseo_internal_value_string(context, frame.slots[2]);
665
+ frame.slots[2] = result.value;
666
+ }
667
+ if (result.status == OSEO_STATUS_NORMAL) {
668
+ result = ascii_runtime_string(context, "message");
669
+ frame.slots[1] = result.value;
670
+ }
671
+ if (result.status == OSEO_STATUS_NORMAL) {
672
+ result = oseo_object_get(context, frame.slots[0], frame.slots[1]);
673
+ frame.slots[3] = result.value;
674
+ }
675
+ if (result.status == OSEO_STATUS_NORMAL) {
676
+ result = tag_of(frame.slots[3]) == OSEO_TAG_UNDEFINED
677
+ ? ascii_runtime_string(context, "")
678
+ : oseo_internal_value_string(context, frame.slots[3]);
679
+ frame.slots[3] = result.value;
680
+ }
681
+ if (result.status == OSEO_STATUS_NORMAL) {
682
+ result = error_text(context, frame.slots[2], frame.slots[3]);
683
+ }
684
+ oseo_roots_release(context, &frame);
685
+ return result;
686
+ }
687
+
688
+ static int write_stderr_code_point(uint32_t point) {
689
+ unsigned char bytes[4];
690
+ size_t length;
691
+ if (point <= UINT32_C(0x7f)) {
692
+ bytes[0] = (unsigned char)point;
693
+ length = 1u;
694
+ } else if (point <= UINT32_C(0x7ff)) {
695
+ bytes[0] = (unsigned char)(UINT32_C(0xc0) | (point >> 6u));
696
+ bytes[1] = (unsigned char)(UINT32_C(0x80) | (point & 0x3fu));
697
+ length = 2u;
698
+ } else if (point <= UINT32_C(0xffff)) {
699
+ bytes[0] = (unsigned char)(UINT32_C(0xe0) | (point >> 12u));
700
+ bytes[1] = (unsigned char)(UINT32_C(0x80) | ((point >> 6u) & 0x3fu));
701
+ bytes[2] = (unsigned char)(UINT32_C(0x80) | (point & 0x3fu));
702
+ length = 3u;
703
+ } else {
704
+ bytes[0] = (unsigned char)(UINT32_C(0xf0) | (point >> 18u));
705
+ bytes[1] = (unsigned char)(UINT32_C(0x80) | ((point >> 12u) & 0x3fu));
706
+ bytes[2] = (unsigned char)(UINT32_C(0x80) | ((point >> 6u) & 0x3fu));
707
+ bytes[3] = (unsigned char)(UINT32_C(0x80) | (point & 0x3fu));
708
+ length = 4u;
709
+ }
710
+ return fwrite(bytes, 1u, length, stderr) == length ? 0 : 1;
711
+ }
712
+
713
+ static void write_stderr_string(OseoValue value) {
714
+ OseoString *string = string_object(value);
715
+ for (size_t index = 0u; index < string->length; index += 1u) {
716
+ uint32_t point = string->units[index];
717
+ if (point >= UINT32_C(0xd800) && point <= UINT32_C(0xdbff) &&
718
+ index + 1u < string->length) {
719
+ uint32_t low = string->units[index + 1u];
720
+ if (low >= UINT32_C(0xdc00) && low <= UINT32_C(0xdfff)) {
721
+ point = UINT32_C(0x10000) +
722
+ ((point - UINT32_C(0xd800)) << 10u) +
723
+ (low - UINT32_C(0xdc00));
724
+ index += 1u;
725
+ } else {
726
+ point = UINT32_C(0xfffd);
727
+ }
728
+ } else if (point >= UINT32_C(0xd800) &&
729
+ point <= UINT32_C(0xdfff)) {
730
+ point = UINT32_C(0xfffd);
731
+ }
732
+ if (write_stderr_code_point(point) != 0) return;
733
+ }
734
+ }
735
+
736
+ /*
737
+ * The intrinsic error kind of a thrown value, taken from the first
738
+ * intrinsic error prototype on its chain, or OSEO_ERROR_KIND_COUNT for
739
+ * a value that is not an error instance. This is the stable identity
740
+ * used for the machine-readable throw marker, independent of the
741
+ * mutable name property.
742
+ */
743
+ static size_t error_instance_kind(OseoContext *context, OseoValue thrown) {
744
+ if (!is_object(thrown)) return OSEO_ERROR_KIND_COUNT;
745
+ /*
746
+ * The walk starts at the thrown value itself so a thrown intrinsic
747
+ * prototype object, such as TypeError.prototype, keeps its exact
748
+ * identity rather than reporting its own prototype's kind.
749
+ */
750
+ OseoValue current = thrown;
751
+ while (is_object(current)) {
752
+ for (size_t kind = 0u; kind < OSEO_ERROR_KIND_COUNT; kind += 1u) {
753
+ OseoValue prototype = context->realm->intrinsics[
754
+ error_prototype_intrinsics[kind]
755
+ ];
756
+ if (prototype == current) return kind;
757
+ }
758
+ current = ordinary_object(current)->prototype;
759
+ }
760
+ return OSEO_ERROR_KIND_COUNT;
761
+ }
762
+
763
+ /*
764
+ * Read one ASCII-named ordinary property of a thrown value. The key
765
+ * string is allocated and rooted across the read, which can run a user
766
+ * getter, so the caller only needs the receiver itself rooted.
767
+ */
768
+ static OseoResult thrown_property(
769
+ OseoContext *context,
770
+ OseoValue receiver,
771
+ const char *name
772
+ ) {
773
+ OseoResult key = ascii_runtime_string(context, name);
774
+ if (key.status != OSEO_STATUS_NORMAL) return key;
775
+ OseoValue slots[2] = {receiver, key.value};
776
+ OseoRootFrame frame = {NULL, slots, 2u};
777
+ oseo_roots_push(context, &frame);
778
+ OseoResult result = oseo_object_get(context, slots[0], slots[1]);
779
+ oseo_roots_pop(context, &frame);
780
+ return result;
781
+ }
782
+
783
+ /*
784
+ * Whether a string can be the machine-readable marker of a thrown value
785
+ * with no intrinsic error identity. The marker is one whitespace-free
786
+ * token on its own line, so only an ASCII identifier has a
787
+ * representation there. A name outside that shape suppresses both the
788
+ * marker and the rendered text, which keeps the human diagnostic and the
789
+ * marker of such a value in agreement.
790
+ */
791
+ static bool identifier_marker(OseoValue value) {
792
+ if (!is_string(value)) return false;
793
+ OseoString *string = string_object(value);
794
+ if (string->length == 0u) return false;
795
+ for (size_t index = 0u; index < string->length; index += 1u) {
796
+ uint16_t unit = string->units[index];
797
+ if ((unit >= 'A' && unit <= 'Z') || (unit >= 'a' && unit <= 'z') ||
798
+ unit == '_' || unit == '$') {
799
+ continue;
800
+ }
801
+ if (index > 0u && unit >= '0' && unit <= '9') continue;
802
+ return false;
803
+ }
804
+ return true;
805
+ }
806
+
807
+ /*
808
+ * The identity of a thrown object that is not an intrinsic error
809
+ * instance, taken from the name of its `constructor` property. A user
810
+ * error class such as the test262 harness `Test262Error` has no
811
+ * intrinsic identity, so that constructor name is the only identity
812
+ * ECMAScript exposes for it. The result is an identifier-shaped string,
813
+ * or undefined when no such name is reachable.
814
+ */
815
+ static OseoValue thrown_identity(OseoContext *context, OseoValue thrown) {
816
+ OseoResult result = thrown_property(context, thrown, "constructor");
817
+ if (result.status != OSEO_STATUS_NORMAL || !is_object(result.value)) {
818
+ return oseo_undefined();
819
+ }
820
+ OseoValue slots[1] = {result.value};
821
+ OseoRootFrame frame = {NULL, slots, 1u};
822
+ oseo_roots_push(context, &frame);
823
+ result = thrown_property(context, slots[0], "name");
824
+ oseo_roots_pop(context, &frame);
825
+ if (result.status != OSEO_STATUS_NORMAL) return oseo_undefined();
826
+ return identifier_marker(result.value) ? result.value : oseo_undefined();
827
+ }
828
+
829
+ /*
830
+ * The rendered text of a thrown object with an identity but no intrinsic
831
+ * error identity: `identity: message` with the generic string conversion
832
+ * applied to a present `message` property, and the identity alone when
833
+ * no message renders. The caller keeps `identity` rooted.
834
+ */
835
+ static OseoResult thrown_text(
836
+ OseoContext *context,
837
+ OseoValue thrown,
838
+ OseoValue identity
839
+ ) {
840
+ OseoResult message = thrown_property(context, thrown, "message");
841
+ if (message.status != OSEO_STATUS_NORMAL ||
842
+ tag_of(message.value) == OSEO_TAG_UNDEFINED) {
843
+ return normal(identity);
844
+ }
845
+ OseoValue slots[2] = {identity, message.value};
846
+ OseoRootFrame frame = {NULL, slots, 2u};
847
+ oseo_roots_push(context, &frame);
848
+ OseoResult text = oseo_internal_value_string(context, slots[1]);
849
+ if (text.status == OSEO_STATUS_NORMAL && is_string(text.value)) {
850
+ slots[1] = text.value;
851
+ text = error_text(context, slots[0], slots[1]);
852
+ }
853
+ oseo_roots_pop(context, &frame);
854
+ if (text.status != OSEO_STATUS_NORMAL || !is_string(text.value)) {
855
+ return normal(identity);
856
+ }
857
+ return text;
858
+ }
859
+
860
+ void oseo_context_print_thrown(OseoContext *context, OseoValue thrown) {
861
+ size_t kind = error_instance_kind(context, thrown);
862
+ bool identified = kind < OSEO_ERROR_KIND_COUNT;
863
+ if (context->has_diagnostic || (!identified && !is_object(thrown))) {
864
+ oseo_context_print_error(context);
865
+ return;
866
+ }
867
+ /*
868
+ * The stored diagnostic is the only thing that distinguishes an
869
+ * unhandled throw from an unhandled rejection. An unhandled
870
+ * rejection is its own reviewed host-policy boundary, reported by
871
+ * that text rather than by the rejected value, so a value with no
872
+ * intrinsic error identity leaves the rejection path exactly as it
873
+ * was: the boundary text alone, with no identity read and no
874
+ * marker. Only an ordinary throw renders the value.
875
+ */
876
+ bool render_identity = context->error_message != NULL &&
877
+ strcmp(context->error_message, OSEO_UNHANDLED_THROW_MESSAGE) == 0;
878
+ if (!identified && !render_identity) {
879
+ oseo_context_print_error(context);
880
+ return;
881
+ }
882
+ /*
883
+ * A rejected intrinsic error instance keeps the boundary text as
884
+ * well, so the host-policy boundary reads the same whatever the
885
+ * reason is. Its intrinsic kind marker still follows, which keeps
886
+ * the rejected value observable; reading that kind runs no user
887
+ * JavaScript.
888
+ */
889
+ bool rejection = context->error_message != NULL &&
890
+ strcmp(context->error_message, OSEO_UNHANDLED_REJECTION_MESSAGE) == 0;
891
+ if (identified && rejection) {
892
+ oseo_context_print_error(context);
893
+ (void)fprintf(stderr, "OSEO_THROWN %s\n", error_names[kind]);
894
+ return;
895
+ }
896
+ /*
897
+ * Converting an object-valued name or message, and reading the
898
+ * constructor identity of a value that is not an error instance, run
899
+ * user JavaScript, which moves the context's source location. The
900
+ * original throw or rejection site is restored so the diagnostic
901
+ * points there rather than into the conversion method.
902
+ */
903
+ const char *error_code = context->error_code;
904
+ const char *error_message = context->error_message;
905
+ const char *source_id = context->source_id;
906
+ size_t source_id_length = context->source_id_length;
907
+ size_t line = context->line;
908
+ size_t column = context->column;
909
+ OseoValue slots[3] = {thrown, oseo_undefined(), oseo_undefined()};
910
+ OseoRootFrame frame = {NULL, slots, 3u};
911
+ oseo_roots_push(context, &frame);
912
+ OseoResult result;
913
+ if (identified) {
914
+ result = oseo_internal_error_to_string(context, slots[0]);
915
+ } else {
916
+ slots[1] = thrown_identity(context, slots[0]);
917
+ result = tag_of(slots[1]) == OSEO_TAG_UNDEFINED
918
+ ? normal(oseo_undefined())
919
+ : thrown_text(context, slots[0], slots[1]);
920
+ }
921
+ slots[2] = result.value;
922
+ oseo_roots_pop(context, &frame);
923
+ context->error_code = error_code;
924
+ context->error_message = error_message;
925
+ context->source_id = source_id;
926
+ context->source_id_length = source_id_length;
927
+ context->line = line;
928
+ context->column = column;
929
+ context->has_diagnostic = false;
930
+ if (result.status != OSEO_STATUS_NORMAL || !is_string(slots[2]) ||
931
+ string_object(slots[2])->length == 0u) {
932
+ /*
933
+ * An empty or failed rendering falls back to the stored
934
+ * diagnostic. An error instance keeps its typed marker either
935
+ * way, and a value whose identity was read keeps that marker.
936
+ */
937
+ oseo_context_print_error(context);
938
+ } else {
939
+ (void)fwrite(
940
+ context->source_id,
941
+ 1u,
942
+ context->source_id_length,
943
+ stderr
944
+ );
945
+ (void)fprintf(
946
+ stderr,
947
+ ":%zu:%zu: error[%s]: ",
948
+ context->line,
949
+ context->column,
950
+ context->error_code
951
+ );
952
+ write_stderr_string(slots[2]);
953
+ (void)fprintf(stderr, "\n");
954
+ }
955
+ /*
956
+ * A stable machine-readable marker records the thrown value's
957
+ * identity separately from the human diagnostic, whose name and
958
+ * message can be mutated to arbitrary or non-identifier values. An
959
+ * error instance reports its intrinsic kind; any other object
960
+ * reports its identifier-shaped constructor name, which is what
961
+ * makes a thrown value such as a test262 `Test262Error` observable.
962
+ * It is the last line the process ever writes, which is what lets
963
+ * tooling tell it apart from a marker-shaped line a rendered
964
+ * message or the program itself produced.
965
+ */
966
+ if (identified) {
967
+ (void)fprintf(stderr, "OSEO_THROWN %s\n", error_names[kind]);
968
+ } else if (tag_of(slots[1]) != OSEO_TAG_UNDEFINED) {
969
+ (void)fprintf(stderr, "OSEO_THROWN ");
970
+ write_stderr_string(slots[1]);
971
+ (void)fprintf(stderr, "\n");
972
+ }
973
+ }