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