@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,744 @@
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
+ #define _XOPEN_SOURCE 700
29
+ #include "runtime_internal.h"
30
+
31
+ #include <errno.h>
32
+ #include <math.h>
33
+ #include <signal.h>
34
+ #include <stdio.h>
35
+ #include <stdlib.h>
36
+ #include <unistd.h>
37
+ #if defined(__APPLE__)
38
+ #include <mach-o/dyld.h>
39
+ #include <crt_externs.h>
40
+ #else
41
+ extern char **environ;
42
+ #endif
43
+
44
+ /* Ordinary objects own all visible properties; intrinsic slots root the
45
+ * startup snapshots even after user code deletes or replaces a property. */
46
+ static OseoValue *process_slots(OseoContext *context) {
47
+ return context->initial_realm.intrinsics;
48
+ }
49
+
50
+ void oseo_process_start(OseoContext *context, int argc, char **argv) {
51
+ context->process_argc = argc;
52
+ context->process_argv = argv;
53
+ (void)signal(SIGPIPE, SIG_IGN);
54
+ }
55
+
56
+ /* Decode maximal valid UTF-8 prefixes, keeping a BOM and replacing each
57
+ * ill-formed subsequence as Node's UTF-8 decoder does. Input is host/work
58
+ * memory, never an unrooted pointer into the managed heap. Startup snapshot
59
+ * strings pass intern false so that they never enter the string table. */
60
+ static OseoResult process_decode(
61
+ OseoContext *context, const unsigned char *bytes, size_t length,
62
+ bool intern
63
+ ) {
64
+ if (length > OSEO_MAX_STRING_LENGTH ||
65
+ length > SIZE_MAX / sizeof(uint16_t)) {
66
+ return failure(context, "OSEO3001", "Process text is too large.");
67
+ }
68
+ uint16_t *units = length == 0u ? NULL :
69
+ oseo_internal_allocate_work_bytes(context, length * sizeof(*units));
70
+ if (length != 0u && units == NULL) {
71
+ return failure(context, "OSEO2001", "Process text allocation failed.");
72
+ }
73
+ size_t count = 0u;
74
+ size_t cursor = 0u;
75
+ while (cursor < length) {
76
+ unsigned char lead = bytes[cursor++];
77
+ uint32_t point = lead;
78
+ size_t trailing = 0u;
79
+ unsigned char low = 0x80u;
80
+ unsigned char high = 0xbfu;
81
+ if (lead < 0x80u) {
82
+ units[count++] = lead;
83
+ continue;
84
+ } else if (lead >= 0xc2u && lead <= 0xdfu) {
85
+ point = lead & 0x1fu;
86
+ trailing = 1u;
87
+ } else if (lead >= 0xe0u && lead <= 0xefu) {
88
+ point = lead & 0x0fu;
89
+ trailing = 2u;
90
+ if (lead == 0xe0u) low = 0xa0u;
91
+ if (lead == 0xedu) high = 0x9fu;
92
+ } else if (lead >= 0xf0u && lead <= 0xf4u) {
93
+ point = lead & 0x07u;
94
+ trailing = 3u;
95
+ if (lead == 0xf0u) low = 0x90u;
96
+ if (lead == 0xf4u) high = 0x8fu;
97
+ } else {
98
+ units[count++] = 0xfffdu;
99
+ continue;
100
+ }
101
+ bool valid = true;
102
+ for (size_t index = 0u; index < trailing; index += 1u) {
103
+ if (cursor == length || bytes[cursor] < low ||
104
+ bytes[cursor] > high) {
105
+ valid = false;
106
+ break;
107
+ }
108
+ point = (point << 6u) | (bytes[cursor++] & 0x3fu);
109
+ low = 0x80u;
110
+ high = 0xbfu;
111
+ }
112
+ if (!valid) point = 0xfffdu;
113
+ if (point <= 0xffffu) {
114
+ units[count++] = (uint16_t)point;
115
+ } else {
116
+ point -= 0x10000u;
117
+ units[count++] = (uint16_t)(0xd800u | (point >> 10u));
118
+ units[count++] = (uint16_t)(0xdc00u | (point & 0x3ffu));
119
+ }
120
+ }
121
+ OseoResult result = intern
122
+ ? oseo_string_from_units(context, units, count)
123
+ : oseo_internal_allocate_uninterned_string(context, units, count);
124
+ free(units);
125
+ return result;
126
+ }
127
+
128
+ static OseoResult process_define(
129
+ OseoContext *context, OseoValue object, const char *name, OseoValue value,
130
+ bool enumerable
131
+ ) {
132
+ OseoValue slots[3] = {object, value, oseo_undefined()};
133
+ OseoRootFrame frame = {NULL, slots, 3u};
134
+ oseo_roots_push(context, &frame);
135
+ OseoResult result = oseo_internal_ascii_string(context, name);
136
+ slots[2] = result.value;
137
+ if (result.status == OSEO_STATUS_NORMAL) {
138
+ result = oseo_object_define(context, slots[0], slots[2], slots[1],
139
+ (OseoPropertyAttributes){true, enumerable, true, false});
140
+ }
141
+ oseo_roots_pop(context, &frame);
142
+ return result;
143
+ }
144
+
145
+ static OseoResult process_function(
146
+ OseoContext *context, OseoIntrinsic intrinsic, size_t offset,
147
+ const char *name, size_t length
148
+ ) {
149
+ uint16_t units[32];
150
+ size_t count = strlen(name);
151
+ if (count > sizeof(units) / sizeof(units[0])) {
152
+ return failure(context, "OSEO2001", "Invalid process function name.");
153
+ }
154
+ for (size_t index = 0u; index < count; index += 1u) {
155
+ units[index] = (unsigned char)name[index];
156
+ }
157
+ OseoResult result = oseo_environment_create(context, 0u);
158
+ if (result.status != OSEO_STATUS_NORMAL) return result;
159
+ OseoValue slots[1] = {result.value};
160
+ OseoRootFrame frame = {NULL, slots, 1u};
161
+ oseo_roots_push(context, &frame);
162
+ result = oseo_function_create(context,
163
+ OSEO_PROCESS_CODE_ID_RANGE_LAST - offset,
164
+ slots[0], units, count, length, OSEO_FUNCTION_INTERNAL,
165
+ oseo_undefined(), oseo_undefined(), OSEO_FUNCTION_NAME_PREFIX_NONE);
166
+ if (result.status == OSEO_STATUS_NORMAL) {
167
+ process_slots(context)[intrinsic] = result.value;
168
+ }
169
+ oseo_roots_pop(context, &frame);
170
+ return result;
171
+ }
172
+
173
+ static OseoResult process_executable(OseoContext *context) {
174
+ size_t capacity = 256u;
175
+ for (;;) {
176
+ char *path = oseo_internal_allocate_work_bytes(context, capacity);
177
+ if (path == NULL) {
178
+ return failure(context, "OSEO2001",
179
+ "Executable path allocation failed.");
180
+ }
181
+ size_t length = 0u;
182
+ bool retry = false;
183
+ #if defined(__APPLE__)
184
+ uint32_t size = capacity > UINT32_MAX ? UINT32_MAX : (uint32_t)capacity;
185
+ if (_NSGetExecutablePath(path, &size) != 0) {
186
+ retry = true;
187
+ } else {
188
+ char *absolute = realpath(path, NULL);
189
+ if (absolute != NULL) {
190
+ OseoResult result = process_decode(context,
191
+ (const unsigned char *)absolute, strlen(absolute), false);
192
+ free(absolute);
193
+ free(path);
194
+ return result;
195
+ }
196
+ }
197
+ #else
198
+ ssize_t observed = readlink("/proc/self/exe", path, capacity);
199
+ if (observed >= 0) {
200
+ length = (size_t)observed;
201
+ retry = length == capacity;
202
+ }
203
+ #endif
204
+ if (!retry && length > 0u) {
205
+ OseoResult result = process_decode(context,
206
+ (const unsigned char *)path, length, false);
207
+ free(path);
208
+ return result;
209
+ }
210
+ free(path);
211
+ if (!retry || capacity > SIZE_MAX / 2u) {
212
+ return failure(context, "OSEO3001", "Cannot read executable path.");
213
+ }
214
+ capacity *= 2u;
215
+ }
216
+ }
217
+
218
+ static OseoResult process_arguments(OseoContext *context) {
219
+ OseoValue *slots = process_slots(context);
220
+ OseoResult result = oseo_array_create(context, 0u);
221
+ if (result.status != OSEO_STATUS_NORMAL) return result;
222
+ slots[OSEO_INTRINSIC_PROCESS_ARGV] = result.value;
223
+ OseoValue roots[1] = {oseo_undefined()};
224
+ OseoRootFrame frame = {NULL, roots, 1u};
225
+ oseo_roots_push(context, &frame);
226
+ result = process_executable(context);
227
+ roots[0] = result.value;
228
+ if (result.status == OSEO_STATUS_NORMAL) {
229
+ result = oseo_array_append(context,
230
+ slots[OSEO_INTRINSIC_PROCESS_ARGV], roots[0]);
231
+ }
232
+ for (int index = 0; result.status == OSEO_STATUS_NORMAL &&
233
+ index < context->process_argc; index += 1) {
234
+ const char *argument = context->process_argv[index];
235
+ result = process_decode(context,
236
+ (const unsigned char *)argument, strlen(argument), false);
237
+ roots[0] = result.value;
238
+ if (result.status == OSEO_STATUS_NORMAL) {
239
+ result = oseo_array_append(context,
240
+ slots[OSEO_INTRINSIC_PROCESS_ARGV], roots[0]);
241
+ }
242
+ }
243
+ /* Direct ABI users may not have a native invocation to supply. */
244
+ if (result.status == OSEO_STATUS_NORMAL && context->process_argc == 0) {
245
+ result = oseo_internal_ascii_string(context, "");
246
+ roots[0] = result.value;
247
+ if (result.status == OSEO_STATUS_NORMAL) {
248
+ result = oseo_array_append(context,
249
+ slots[OSEO_INTRINSIC_PROCESS_ARGV], roots[0]);
250
+ }
251
+ }
252
+ oseo_roots_pop(context, &frame);
253
+ return result;
254
+ }
255
+
256
+ static OseoResult process_environment(OseoContext *context) {
257
+ OseoResult result = oseo_object_literal_create(context);
258
+ if (result.status != OSEO_STATUS_NORMAL) return result;
259
+ process_slots(context)[OSEO_INTRINSIC_PROCESS_ENV] = result.value;
260
+ OseoValue roots[2] = {oseo_undefined(), oseo_undefined()};
261
+ OseoRootFrame frame = {NULL, roots, 2u};
262
+ oseo_roots_push(context, &frame);
263
+ #if defined(__APPLE__)
264
+ char **entries = *_NSGetEnviron();
265
+ #else
266
+ char **entries = environ;
267
+ #endif
268
+ for (size_t index = 0u; result.status == OSEO_STATUS_NORMAL &&
269
+ entries != NULL && entries[index] != NULL; index += 1u) {
270
+ const char *entry = entries[index];
271
+ const char *equal = strchr(entry, '=');
272
+ if (equal == NULL) continue;
273
+ result = process_decode(context, (const unsigned char *)entry,
274
+ (size_t)(equal - entry), false);
275
+ roots[0] = result.value;
276
+ if (result.status == OSEO_STATUS_NORMAL) {
277
+ result = process_decode(context,
278
+ (const unsigned char *)(equal + 1), strlen(equal + 1), false);
279
+ roots[1] = result.value;
280
+ }
281
+ if (result.status == OSEO_STATUS_NORMAL) {
282
+ result = oseo_object_define(context,
283
+ process_slots(context)[OSEO_INTRINSIC_PROCESS_ENV],
284
+ roots[0], roots[1],
285
+ (OseoPropertyAttributes){true, true, true, false});
286
+ }
287
+ }
288
+ oseo_roots_pop(context, &frame);
289
+ return result;
290
+ }
291
+
292
+ OseoResult oseo_internal_process_intrinsic(OseoContext *context) {
293
+ OseoValue *slots = process_slots(context);
294
+ if (is_object(slots[OSEO_INTRINSIC_PROCESS])) {
295
+ return normal(slots[OSEO_INTRINSIC_PROCESS]);
296
+ }
297
+ size_t allocations = context->allocations;
298
+ OseoResult result = process_arguments(context);
299
+ if (result.status == OSEO_STATUS_NORMAL) {
300
+ result = process_environment(context);
301
+ }
302
+ const OseoIntrinsic objects[] = {OSEO_INTRINSIC_PROCESS,
303
+ OSEO_INTRINSIC_PROCESS_STDOUT, OSEO_INTRINSIC_PROCESS_STDERR,
304
+ OSEO_INTRINSIC_PROCESS_FS};
305
+ for (size_t index = 0u; result.status == OSEO_STATUS_NORMAL &&
306
+ index < sizeof(objects) / sizeof(objects[0]); index += 1u) {
307
+ result = oseo_object_literal_create(context);
308
+ if (result.status == OSEO_STATUS_NORMAL) {
309
+ slots[objects[index]] = result.value;
310
+ }
311
+ }
312
+ const struct {
313
+ OseoIntrinsic intrinsic;
314
+ const char *name;
315
+ size_t length;
316
+ } functions[] = {
317
+ {OSEO_INTRINSIC_PROCESS_EXIT, "exit", 1u},
318
+ {OSEO_INTRINSIC_PROCESS_STDOUT_WRITE, "write", 1u},
319
+ {OSEO_INTRINSIC_PROCESS_STDERR_WRITE, "write", 1u},
320
+ {OSEO_INTRINSIC_PROCESS_READ_FILE_SYNC, "readFileSync", 2u},
321
+ {OSEO_INTRINSIC_PROCESS_EXIT_CODE_GET, "get exitCode", 0u},
322
+ {OSEO_INTRINSIC_PROCESS_EXIT_CODE_SET, "set exitCode", 1u},
323
+ };
324
+ for (size_t index = 0u; result.status == OSEO_STATUS_NORMAL &&
325
+ index < sizeof(functions) / sizeof(functions[0]); index += 1u) {
326
+ result = process_function(context, functions[index].intrinsic, index,
327
+ functions[index].name, functions[index].length);
328
+ }
329
+ const struct {
330
+ OseoIntrinsic object;
331
+ const char *name;
332
+ OseoIntrinsic value;
333
+ } properties[] = {
334
+ {OSEO_INTRINSIC_PROCESS, "argv", OSEO_INTRINSIC_PROCESS_ARGV},
335
+ {OSEO_INTRINSIC_PROCESS, "env", OSEO_INTRINSIC_PROCESS_ENV},
336
+ {OSEO_INTRINSIC_PROCESS, "exit", OSEO_INTRINSIC_PROCESS_EXIT},
337
+ {OSEO_INTRINSIC_PROCESS, "stdout", OSEO_INTRINSIC_PROCESS_STDOUT},
338
+ {OSEO_INTRINSIC_PROCESS, "stderr", OSEO_INTRINSIC_PROCESS_STDERR},
339
+ {OSEO_INTRINSIC_PROCESS_STDOUT, "write",
340
+ OSEO_INTRINSIC_PROCESS_STDOUT_WRITE},
341
+ {OSEO_INTRINSIC_PROCESS_STDERR, "write",
342
+ OSEO_INTRINSIC_PROCESS_STDERR_WRITE},
343
+ {OSEO_INTRINSIC_PROCESS_FS, "readFileSync",
344
+ OSEO_INTRINSIC_PROCESS_READ_FILE_SYNC},
345
+ };
346
+ for (size_t index = 0u; result.status == OSEO_STATUS_NORMAL &&
347
+ index < sizeof(properties) / sizeof(properties[0]); index += 1u) {
348
+ result = process_define(context, slots[properties[index].object],
349
+ properties[index].name, slots[properties[index].value], true);
350
+ }
351
+ if (result.status == OSEO_STATUS_NORMAL) {
352
+ OseoValue key = oseo_undefined();
353
+ OseoRootFrame frame = {NULL, &key, 1u};
354
+ oseo_roots_push(context, &frame);
355
+ result = oseo_internal_ascii_string(context, "exitCode");
356
+ key = result.value;
357
+ if (result.status == OSEO_STATUS_NORMAL) {
358
+ result = oseo_object_define_accessor(context,
359
+ slots[OSEO_INTRINSIC_PROCESS], key,
360
+ slots[OSEO_INTRINSIC_PROCESS_EXIT_CODE_GET],
361
+ slots[OSEO_INTRINSIC_PROCESS_EXIT_CODE_SET], true, true,
362
+ (OseoPropertyAttributes){true, true, false, true});
363
+ }
364
+ oseo_roots_pop(context, &frame);
365
+ }
366
+ if (result.status != OSEO_STATUS_NORMAL) {
367
+ for (size_t index = OSEO_INTRINSIC_PROCESS;
368
+ index <= OSEO_INTRINSIC_PROCESS_EXIT_CODE_SET; index += 1u) {
369
+ slots[index] = oseo_undefined();
370
+ }
371
+ } else if (context->observe_specialization) {
372
+ context->allocations = allocations;
373
+ }
374
+ return result.status == OSEO_STATUS_NORMAL
375
+ ? normal(slots[OSEO_INTRINSIC_PROCESS]) : result;
376
+ }
377
+
378
+ OseoResult oseo_internal_install_process_global(
379
+ OseoContext *context, OseoValue global
380
+ ) {
381
+ OseoValue roots[1] = {global};
382
+ OseoRootFrame frame = {NULL, roots, 1u};
383
+ oseo_roots_push(context, &frame);
384
+ OseoResult result = oseo_internal_process_intrinsic(context);
385
+ if (result.status == OSEO_STATUS_NORMAL) {
386
+ result = process_define(context, roots[0], "process",
387
+ result.value, false);
388
+ }
389
+ oseo_roots_pop(context, &frame);
390
+ return result;
391
+ }
392
+
393
+ OseoResult oseo_builtin_export(
394
+ OseoContext *context, size_t count, const OseoValue *arguments
395
+ ) {
396
+ if (count != 2u || !is_number(arguments[0]) || !is_number(arguments[1])) {
397
+ return failure(context, "OSEO2001", "Invalid builtin export request.");
398
+ }
399
+ const OseoIntrinsic process[] = {OSEO_INTRINSIC_PROCESS,
400
+ OSEO_INTRINSIC_PROCESS_ARGV, OSEO_INTRINSIC_PROCESS_ENV,
401
+ OSEO_INTRINSIC_PROCESS_EXIT, OSEO_INTRINSIC_PROCESS_STDOUT,
402
+ OSEO_INTRINSIC_PROCESS_STDERR};
403
+ const OseoIntrinsic fs[] = {OSEO_INTRINSIC_PROCESS_FS,
404
+ OSEO_INTRINSIC_PROCESS_READ_FILE_SYNC};
405
+ double module = number_value(arguments[0]);
406
+ double index = number_value(arguments[1]);
407
+ const OseoIntrinsic *table = module == 0.0 ? process : fs;
408
+ size_t length = module == 0.0
409
+ ? sizeof(process) / sizeof(process[0]) : sizeof(fs) / sizeof(fs[0]);
410
+ if ((module != 0.0 && module != 1.0) || !isfinite(index) ||
411
+ index < 0.0 || index >= (double)length || trunc(index) != index) {
412
+ return failure(context, "OSEO2001", "Unknown builtin export.");
413
+ }
414
+ OseoResult result = oseo_internal_process_intrinsic(context);
415
+ return result.status == OSEO_STATUS_NORMAL
416
+ ? normal(process_slots(context)[table[(size_t)index]]) : result;
417
+ }
418
+
419
+ static OseoResult process_write_error(
420
+ OseoContext *context, bool standard_error, bool strict, int error
421
+ ) {
422
+ if (error == EPIPE) {
423
+ if (standard_error) context->process_stderr_closed = true;
424
+ else context->process_stdout_closed = true;
425
+ if (!strict) return normal(oseo_undefined());
426
+ return failure(context, "OSEO3001",
427
+ "Standard stream write failed: EPIPE.");
428
+ }
429
+ return failure(context, "OSEO3001", "Standard stream write failed.");
430
+ }
431
+
432
+ OseoResult oseo_internal_process_write(
433
+ OseoContext *context, bool standard_error,
434
+ const unsigned char *bytes, size_t length, bool strict
435
+ ) {
436
+ bool closed = standard_error ? context->process_stderr_closed
437
+ : context->process_stdout_closed;
438
+ if (closed) {
439
+ return process_write_error(context, standard_error, strict, EPIPE);
440
+ }
441
+ FILE *stream = standard_error ? stderr : stdout;
442
+ errno = 0;
443
+ if (length != 0u && fwrite(bytes, 1u, length, stream) != length) {
444
+ int error = errno;
445
+ clearerr(stream);
446
+ return process_write_error(context, standard_error, strict, error);
447
+ }
448
+ return normal(oseo_undefined());
449
+ }
450
+
451
+ static OseoResult process_flush(
452
+ OseoContext *context, bool standard_error, bool strict
453
+ ) {
454
+ FILE *stream = standard_error ? stderr : stdout;
455
+ bool closed = standard_error ? context->process_stderr_closed
456
+ : context->process_stdout_closed;
457
+ if (closed) {
458
+ return process_write_error(context, standard_error, strict, EPIPE);
459
+ }
460
+ errno = 0;
461
+ if (fflush(stream) != 0) {
462
+ int error = errno;
463
+ clearerr(stream);
464
+ return process_write_error(context, standard_error, strict, error);
465
+ }
466
+ return normal(oseo_undefined());
467
+ }
468
+
469
+ OseoResult oseo_internal_process_write_string(
470
+ OseoContext *context, bool standard_error, OseoValue value, bool strict
471
+ ) {
472
+ OseoString *string = string_object(value);
473
+ for (size_t index = 0u; index < string->length; index += 1u) {
474
+ uint32_t point = string->units[index];
475
+ if (point >= 0xd800u && point <= 0xdbffu &&
476
+ index + 1u < string->length &&
477
+ string->units[index + 1u] >= 0xdc00u &&
478
+ string->units[index + 1u] <= 0xdfffu) {
479
+ point = 0x10000u + ((point - 0xd800u) << 10u) +
480
+ (string->units[++index] - 0xdc00u);
481
+ } else if (point >= 0xd800u && point <= 0xdfffu) {
482
+ point = 0xfffdu;
483
+ }
484
+ unsigned char bytes[4];
485
+ size_t length = 0u;
486
+ if (point <= 0x7fu) {
487
+ bytes[length++] = (unsigned char)point;
488
+ } else if (point <= 0x7ffu) {
489
+ bytes[length++] = (unsigned char)(0xc0u | (point >> 6u));
490
+ bytes[length++] = (unsigned char)(0x80u | (point & 0x3fu));
491
+ } else if (point <= 0xffffu) {
492
+ bytes[length++] = (unsigned char)(0xe0u | (point >> 12u));
493
+ bytes[length++] = (unsigned char)(0x80u | ((point >> 6u) & 0x3fu));
494
+ bytes[length++] = (unsigned char)(0x80u | (point & 0x3fu));
495
+ } else {
496
+ bytes[length++] = (unsigned char)(0xf0u | (point >> 18u));
497
+ bytes[length++] = (unsigned char)(0x80u | ((point >> 12u) & 0x3fu));
498
+ bytes[length++] = (unsigned char)(0x80u | ((point >> 6u) & 0x3fu));
499
+ bytes[length++] = (unsigned char)(0x80u | (point & 0x3fu));
500
+ }
501
+ OseoResult result = oseo_internal_process_write(context,
502
+ standard_error, bytes, length, strict);
503
+ if (result.status != OSEO_STATUS_NORMAL) return result;
504
+ }
505
+ return normal(oseo_undefined());
506
+ }
507
+
508
+ static bool process_encoding(OseoValue encoding) {
509
+ return is_string(encoding) &&
510
+ (oseo_internal_string_is_ascii(encoding, "utf8") ||
511
+ oseo_internal_string_is_ascii(encoding, "utf-8"));
512
+ }
513
+
514
+ static OseoResult process_code_error(
515
+ OseoContext *context, OseoErrorKind kind, const char *code
516
+ ) {
517
+ OseoResult result = oseo_internal_throw_error(context, kind,
518
+ "Invalid process exit code.");
519
+ if (result.status != OSEO_STATUS_THROW) return result;
520
+ OseoValue roots[1] = {result.value};
521
+ OseoRootFrame frame = {NULL, roots, 1u};
522
+ oseo_roots_push(context, &frame);
523
+ OseoResult text = oseo_internal_ascii_string(context, code);
524
+ if (text.status == OSEO_STATUS_NORMAL) {
525
+ text = process_define(context, roots[0], "code", text.value, true);
526
+ }
527
+ result = text.status == OSEO_STATUS_NORMAL
528
+ ? (OseoResult){OSEO_STATUS_THROW, roots[0]} : text;
529
+ oseo_roots_pop(context, &frame);
530
+ return result;
531
+ }
532
+
533
+ static OseoResult process_set_code(OseoContext *context, OseoValue code) {
534
+ if (is_nullish(code)) {
535
+ context->process_has_exit_code = false;
536
+ return normal(oseo_undefined());
537
+ }
538
+ if (!is_number(code) &&
539
+ (!is_string(code) || string_object(code)->length == 0u)) {
540
+ return process_code_error(context, OSEO_ERROR_TYPE,
541
+ "ERR_INVALID_ARG_TYPE");
542
+ }
543
+ OseoResult converted = oseo_internal_to_number(context, code);
544
+ if (converted.status != OSEO_STATUS_NORMAL) return converted;
545
+ double number = number_value(converted.value);
546
+ if (is_string(code) && isnan(number)) {
547
+ return process_code_error(context, OSEO_ERROR_TYPE,
548
+ "ERR_INVALID_ARG_TYPE");
549
+ }
550
+ if (!isfinite(number) || trunc(number) != number ||
551
+ fabs(number) > 9007199254740991.0) {
552
+ return process_code_error(context, OSEO_ERROR_RANGE,
553
+ "ERR_OUT_OF_RANGE");
554
+ }
555
+ double wrapped = fmod(number, 4294967296.0);
556
+ if (wrapped < 0.0) wrapped += 4294967296.0;
557
+ if (wrapped >= 2147483648.0) wrapped -= 4294967296.0;
558
+ context->process_exit_code = (int32_t)wrapped;
559
+ context->process_has_exit_code = true;
560
+ return normal(oseo_undefined());
561
+ }
562
+
563
+ int oseo_process_finish(OseoContext *context) {
564
+ OseoResult result = process_flush(context, false, false);
565
+ OseoResult error = process_flush(context, true, false);
566
+ if (result.status == OSEO_STATUS_NORMAL) result = error;
567
+ if (result.status != OSEO_STATUS_NORMAL) {
568
+ oseo_context_print_thrown(context, result.value);
569
+ return 1;
570
+ }
571
+ return context->process_has_exit_code
572
+ ? (int)((uint32_t)context->process_exit_code & 255u) : 0;
573
+ }
574
+
575
+ static OseoResult process_read(OseoContext *context, size_t count,
576
+ const OseoValue *arguments) {
577
+ if (count == 0u || !is_number(arguments[0]) ||
578
+ number_value(arguments[0]) != 0.0 || count > 2u) {
579
+ return failure(context, "OSEO3001",
580
+ "Supported input form: readFileSync(0[, utf8 options]).");
581
+ }
582
+ OseoValue roots[3] = {
583
+ count > 1u ? arguments[1] : oseo_undefined(),
584
+ oseo_undefined(), oseo_undefined(),
585
+ };
586
+ OseoRootFrame frame = {NULL, roots, 3u};
587
+ oseo_roots_push(context, &frame);
588
+ OseoResult result = normal(oseo_undefined());
589
+ if (is_object(roots[0])) {
590
+ result = oseo_internal_own_key_array(
591
+ context, roots[0], OSEO_OWN_KEY_ALL);
592
+ roots[1] = result.value;
593
+ if (result.status == OSEO_STATUS_NORMAL) {
594
+ result = oseo_internal_array_like_list(
595
+ context, roots[1], &roots[2]);
596
+ }
597
+ size_t key_count = 0u;
598
+ const OseoValue *keys = NULL;
599
+ if (result.status == OSEO_STATUS_NORMAL) {
600
+ result = oseo_argument_list_view(
601
+ context, roots[2], &key_count, &keys);
602
+ }
603
+ for (size_t index = 0u; result.status == OSEO_STATUS_NORMAL &&
604
+ index < key_count; index += 1u) {
605
+ if (!is_string(keys[index]) ||
606
+ !oseo_internal_string_is_ascii(keys[index], "encoding")) {
607
+ result = failure(context, "OSEO3001",
608
+ "Supported input options: encoding only.");
609
+ }
610
+ }
611
+ if (result.status == OSEO_STATUS_NORMAL) {
612
+ result = oseo_internal_ascii_string(context, "encoding");
613
+ }
614
+ if (result.status == OSEO_STATUS_NORMAL) {
615
+ result = oseo_object_get(context, roots[0], result.value);
616
+ roots[0] = result.value;
617
+ }
618
+ }
619
+ bool text = process_encoding(roots[0]);
620
+ if (result.status == OSEO_STATUS_NORMAL && !is_nullish(roots[0]) && !text) {
621
+ result = failure(context, "OSEO3001",
622
+ "Supported input encoding: utf8 or utf-8.");
623
+ }
624
+ size_t capacity = 0u;
625
+ size_t length = 0u;
626
+ unsigned char *bytes = NULL;
627
+ while (result.status == OSEO_STATUS_NORMAL) {
628
+ if (length == capacity) {
629
+ if (capacity > OSEO_MAX_STRING_LENGTH / 2u) {
630
+ result = failure(context, "OSEO3001",
631
+ "Standard input is too large.");
632
+ break;
633
+ }
634
+ size_t next = capacity == 0u ? 4096u : capacity * 2u;
635
+ unsigned char *grown = oseo_internal_allocate_work_bytes(
636
+ context, next);
637
+ if (grown == NULL) {
638
+ result = failure(context, "OSEO2001",
639
+ "Standard input allocation failed.");
640
+ break;
641
+ }
642
+ if (length != 0u) memcpy(grown, bytes, length);
643
+ free(bytes);
644
+ bytes = grown;
645
+ capacity = next;
646
+ }
647
+ size_t received = fread(bytes + length, 1u, capacity - length, stdin);
648
+ length += received;
649
+ if (feof(stdin)) break;
650
+ if (ferror(stdin)) {
651
+ if (errno == EINTR) { clearerr(stdin); continue; }
652
+ result = failure(context, "OSEO3001",
653
+ "Standard input read failed.");
654
+ break;
655
+ }
656
+ }
657
+ if (result.status == OSEO_STATUS_NORMAL && text) {
658
+ result = process_decode(context, bytes, length, true);
659
+ } else if (result.status == OSEO_STATUS_NORMAL) {
660
+ result = oseo_internal_intrinsic(context, OSEO_INTRINSIC_UINT8_ARRAY);
661
+ roots[1] = result.value;
662
+ if (result.status == OSEO_STATUS_NORMAL) {
663
+ OseoValue size = oseo_number((double)length);
664
+ result = oseo_call_function(context, roots[1], oseo_undefined(),
665
+ 1u, &size, roots[1]);
666
+ }
667
+ if (result.status == OSEO_STATUS_NORMAL && length != 0u) {
668
+ OseoTypedArray *view = typed_array_object(result.value);
669
+ memcpy(array_buffer_object(view->viewed_buffer)->data,
670
+ bytes, length);
671
+ }
672
+ }
673
+ free(bytes);
674
+ oseo_roots_pop(context, &frame);
675
+ return result;
676
+ }
677
+
678
+ OseoResult oseo_internal_process_builtin_dispatch(
679
+ OseoContext *context, size_t code_id, OseoValue callee,
680
+ OseoValue receiver, size_t count, const OseoValue *arguments,
681
+ OseoValue new_target
682
+ ) {
683
+ (void)callee; (void)receiver; (void)new_target;
684
+ size_t method = OSEO_PROCESS_CODE_ID_RANGE_LAST - code_id;
685
+ if (method == 4u) {
686
+ return normal(context->process_has_exit_code
687
+ ? oseo_number((double)context->process_exit_code)
688
+ : oseo_undefined());
689
+ }
690
+ if (method == 5u) {
691
+ return process_set_code(context,
692
+ count > 0u ? arguments[0] : oseo_undefined());
693
+ }
694
+ if (method == 0u) {
695
+ OseoResult result = count == 0u ? normal(oseo_undefined())
696
+ : process_set_code(context, arguments[0]);
697
+ if (result.status != OSEO_STATUS_NORMAL) return result;
698
+ int status = oseo_process_finish(context);
699
+ if (context->observe_specialization) {
700
+ oseo_context_print_observations(context);
701
+ }
702
+ oseo_context_destroy(context);
703
+ exit(status);
704
+ }
705
+ if (method == 3u) return process_read(context, count, arguments);
706
+ if (method == 1u || method == 2u) {
707
+ if (count == 0u || count > 2u ||
708
+ (count == 2u && !is_nullish(arguments[1]) &&
709
+ !process_encoding(arguments[1]))) {
710
+ return oseo_internal_throw_error(context, OSEO_ERROR_TYPE,
711
+ "A stream write accepts a chunk and "
712
+ "an optional UTF-8 encoding.");
713
+ }
714
+ bool standard_error = method == 2u;
715
+ OseoResult result;
716
+ if (is_string(arguments[0])) {
717
+ result = oseo_internal_process_write_string(context,
718
+ standard_error, arguments[0], true);
719
+ } else if (is_typed_array(arguments[0]) &&
720
+ typed_array_object(arguments[0])->element_kind ==
721
+ OSEO_TYPED_ARRAY_UINT8) {
722
+ size_t length = 0u;
723
+ result = oseo_internal_typed_array_validate(
724
+ context, arguments[0], &length);
725
+ if (result.status == OSEO_STATUS_NORMAL) {
726
+ OseoTypedArray *view = typed_array_object(arguments[0]);
727
+ result = oseo_internal_process_write(context, standard_error,
728
+ length == 0u ? NULL :
729
+ array_buffer_object(view->viewed_buffer)->data +
730
+ view->byte_offset,
731
+ length, true);
732
+ }
733
+ } else {
734
+ return oseo_internal_throw_error(context, OSEO_ERROR_TYPE,
735
+ "A stream write requires a string or Uint8Array.");
736
+ }
737
+ if (result.status == OSEO_STATUS_NORMAL) {
738
+ result = process_flush(context, standard_error, true);
739
+ }
740
+ return result.status == OSEO_STATUS_NORMAL
741
+ ? normal(oseo_boolean(true)) : result;
742
+ }
743
+ return failure(context, "OSEO2001", "Unknown process function.");
744
+ }