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