xbintsc 0.6.24 → 0.6.28
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/AGENTS.md +1 -1
- package/dist/src/cli/extensions.js +11 -3
- package/dist/src/cli/extensions.js.map +1 -1
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/cli/help.js +1 -1
- package/dist/src/codegen/generator/state.js +7 -0
- package/dist/src/codegen/generator/state.js.map +1 -1
- package/dist/src/codegen/generator/tables.js +19 -1
- package/dist/src/codegen/generator/tables.js.map +1 -1
- package/dist/src/driver/bundler/graph.js +9 -5
- package/dist/src/driver/bundler/graph.js.map +1 -1
- package/dist/src/driver/fingerprint.d.ts +2 -2
- package/dist/src/driver/fingerprint.js +3 -3
- package/dist/src/driver/fingerprint.js.map +1 -1
- package/dist/src/driver/link-features.js +1 -1
- package/dist/src/driver/link-features.js.map +1 -1
- package/dist/src/driver/paths.js +8 -4
- package/dist/src/driver/paths.js.map +1 -1
- package/dist/src/driver/runtime-objects.d.ts +1 -1
- package/dist/src/driver/runtime-objects.js +13 -1
- package/dist/src/driver/runtime-objects.js.map +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/extensions/catalog.js +5 -1
- package/dist/src/extensions/catalog.js.map +1 -1
- package/dist/src/extensions/gui/index.js +0 -1
- package/dist/src/extensions/gui/index.js.map +1 -1
- package/dist/src/extensions/link-feature.d.ts +8 -0
- package/dist/src/extensions/node/stream-web/index.d.ts +6 -1
- package/dist/src/extensions/node/stream-web/index.js +59 -4
- package/dist/src/extensions/node/stream-web/index.js.map +1 -1
- package/dist/src/extensions/node/worker_threads/index.d.ts +14 -4
- package/dist/src/extensions/node/worker_threads/index.js +24 -11
- package/dist/src/extensions/node/worker_threads/index.js.map +1 -1
- package/dist/src/extensions/registry.d.ts +9 -1
- package/dist/src/extensions/registry.js +4 -1
- package/dist/src/extensions/registry.js.map +1 -1
- package/dist/src/extensions/wasm-asset.d.ts +16 -0
- package/dist/src/extensions/wasm-asset.js +28 -0
- package/dist/src/extensions/wasm-asset.js.map +1 -0
- package/dist/src/extensions/wasm.d.ts +17 -0
- package/dist/src/extensions/wasm.js +68 -0
- package/dist/src/extensions/wasm.js.map +1 -0
- package/dist/src/extensions/webworker/index.d.ts +24 -0
- package/dist/src/extensions/webworker/index.js +54 -0
- package/dist/src/extensions/webworker/index.js.map +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/tests/driver/link-features.test.js +9 -0
- package/dist/tests/driver/link-features.test.js.map +1 -1
- package/dist/tests/e2e/async-fs.test.js +44 -0
- package/dist/tests/e2e/async-fs.test.js.map +1 -1
- package/dist/tests/e2e/harness.d.ts +6 -0
- package/dist/tests/e2e/harness.js +13 -1
- package/dist/tests/e2e/harness.js.map +1 -1
- package/dist/tests/e2e/node-crypto-random.test.d.ts +8 -0
- package/dist/tests/e2e/node-crypto-random.test.js +148 -0
- package/dist/tests/e2e/node-crypto-random.test.js.map +1 -0
- package/dist/tests/e2e/node-modules.test.js +1 -1
- package/dist/tests/e2e/node-modules.test.js.map +1 -1
- package/dist/tests/e2e/web-streams.test.d.ts +8 -0
- package/dist/tests/e2e/web-streams.test.js +274 -0
- package/dist/tests/e2e/web-streams.test.js.map +1 -0
- package/dist/tests/e2e/web-worker.test.d.ts +7 -0
- package/dist/tests/e2e/web-worker.test.js +74 -0
- package/dist/tests/e2e/web-worker.test.js.map +1 -0
- package/dist/tests/e2e/webassembly.test.d.ts +8 -0
- package/dist/tests/e2e/webassembly.test.js +113 -0
- package/dist/tests/e2e/webassembly.test.js.map +1 -0
- package/dist/tests/e2e/worker-api.test.d.ts +14 -0
- package/dist/tests/e2e/worker-api.test.js +460 -0
- package/dist/tests/e2e/worker-api.test.js.map +1 -0
- package/dist/tests/e2e/worker-threads.test.d.ts +8 -0
- package/dist/tests/e2e/worker-threads.test.js +160 -0
- package/dist/tests/e2e/worker-threads.test.js.map +1 -0
- package/dist/tests/extensions/wasm.test.d.ts +1 -0
- package/dist/tests/extensions/wasm.test.js +53 -0
- package/dist/tests/extensions/wasm.test.js.map +1 -0
- package/doc/ai/cli.md +1 -1
- package/doc/ai/contributing.md +11 -4
- package/doc/ai/extensions.md +25 -2
- package/doc/ai/language-support.md +14 -2
- package/doc/ai/zh-CN/cli.md +1 -1
- package/doc/ai/zh-CN/contributing.md +8 -3
- package/doc/ai/zh-CN/extensions.md +23 -2
- package/doc/ai/zh-CN/language-support.md +13 -2
- package/doc/gui/api.md +1 -1
- package/doc/implemented/cli.md +1 -1
- package/doc/implemented/extensions.md +6 -5
- package/doc/implemented/node/stream-web.md +31 -0
- package/doc/implemented/node/worker-threads.md +88 -5
- package/doc/implemented/node.md +2 -1
- package/doc/implemented/streams.md +82 -0
- package/doc/implemented/wasm.md +80 -18
- package/doc/implemented/web-worker.md +84 -0
- package/doc/implemented.md +6 -2
- package/doc/unimplemented/deviations.md +1 -0
- package/doc/unimplemented/node/worker-threads.md +23 -0
- package/doc/unimplemented/node.md +1 -0
- package/doc/unimplemented/stdlib.md +2 -1
- package/doc/unimplemented/wasm.md +21 -9
- package/doc/unimplemented.md +1 -1
- package/doc/zh-CN/gui/api.md +1 -1
- package/doc/zh-CN/implemented/cli.md +1 -1
- package/doc/zh-CN/implemented/extensions.md +7 -6
- package/doc/zh-CN/implemented/node/stream-web.md +24 -0
- package/doc/zh-CN/implemented/node/worker-threads.md +77 -4
- package/doc/zh-CN/implemented/node.md +2 -1
- package/doc/zh-CN/implemented/streams.md +60 -0
- package/doc/zh-CN/implemented/wasm.md +73 -16
- package/doc/zh-CN/implemented/web-worker.md +79 -0
- package/doc/zh-CN/implemented.md +6 -2
- package/doc/zh-CN/unimplemented/deviations.md +1 -0
- package/doc/zh-CN/unimplemented/node/worker-threads.md +20 -0
- package/doc/zh-CN/unimplemented/node.md +1 -0
- package/doc/zh-CN/unimplemented/stdlib.md +2 -1
- package/doc/zh-CN/unimplemented/wasm.md +16 -8
- package/doc/zh-CN/unimplemented.md +1 -1
- package/llms.txt +2 -0
- package/package.json +8 -7
- package/runtime/ext_gui/browser.cpp +1 -1
- package/runtime/ext_node/stream_web/stream_web.c +20 -6
- package/runtime/worker/worker_api.c +422 -0
- package/runtime/worker/worker_codec.c +489 -0
- package/runtime/worker/worker_core.c +445 -0
- package/runtime/worker/worker_internal.h +223 -0
- package/runtime/worker/worker_platform.c +344 -0
- package/runtime/worker/worker_ports.c +203 -0
- package/runtime/worker/worker_transport.c +464 -0
- package/runtime/worker/worker_wire.c +150 -0
- package/runtime/xt_builtins.c +15 -0
- package/runtime/xt_constructors.c +49 -2
- package/runtime/xt_stdlib2/error.inc +4 -0
- package/runtime/xt_streams/common.inc +232 -0
- package/runtime/xt_streams/constructors.inc +232 -0
- package/runtime/xt_streams/encoding.inc +124 -0
- package/runtime/xt_streams/readable-extra.inc +372 -0
- package/runtime/xt_streams/readable.inc +415 -0
- package/runtime/xt_streams/transform.inc +195 -0
- package/runtime/xt_streams/writable.inc +542 -0
- package/runtime/xt_streams.c +30 -0
- package/runtime/xt_wasm/wasm.h +148 -0
- package/runtime/xt_wasm/wasm_api.c +357 -0
- package/runtime/xt_wasm/wasm_api.h +37 -0
- package/runtime/xt_wasm/wasm_bridge.c +30 -0
- package/runtime/xt_wasm/wasm_instance.c +591 -0
- package/runtime/xt_wasm/wasm_module.c +270 -0
- package/runtime/xt_wasm/wasm_values.c +177 -0
- package/runtime/xt_webassembly.c +15 -4
- package/scripts/fetch-vendored.ts +187 -20
- package/scripts/package-release.ts +3 -2
- package/src/cli/extensions.ts +11 -3
- package/src/cli/help.ts +1 -1
- package/src/codegen/generator/state.ts +7 -0
- package/src/codegen/generator/tables.ts +19 -1
- package/src/driver/bundler/graph.ts +9 -5
- package/src/driver/fingerprint.ts +3 -3
- package/src/driver/link-features.ts +1 -1
- package/src/driver/paths.ts +8 -4
- package/src/driver/runtime-objects.ts +13 -1
- package/src/driver/version.ts +1 -1
- package/src/extensions/catalog.ts +5 -1
- package/src/extensions/gui/index.ts +0 -1
- package/src/extensions/link-feature.ts +8 -0
- package/src/extensions/node/stream-web/index.ts +59 -4
- package/src/extensions/node/worker_threads/index.ts +24 -11
- package/src/extensions/registry.ts +13 -2
- package/src/extensions/wasm-asset.ts +32 -0
- package/src/extensions/wasm.ts +74 -0
- package/src/extensions/webworker/index.ts +58 -0
- package/src/index.ts +1 -0
- package/vendor/wasm3/.pin +1 -0
- package/vendor/wasm3/LICENSE +21 -0
- package/vendor/wasm3/m3_api_libc.c +265 -0
- package/vendor/wasm3/m3_api_libc.h +19 -0
- package/vendor/wasm3/m3_api_meta_wasi.c +1107 -0
- package/vendor/wasm3/m3_api_tracer.c +173 -0
- package/vendor/wasm3/m3_api_tracer.h +19 -0
- package/vendor/wasm3/m3_api_uvwasi.c +1290 -0
- package/vendor/wasm3/m3_api_wasi.c +1207 -0
- package/vendor/wasm3/m3_api_wasi.h +74 -0
- package/vendor/wasm3/m3_api_wasi_host.h +137 -0
- package/vendor/wasm3/m3_api_wasi_posix.h +979 -0
- package/vendor/wasm3/m3_api_wasi_win32.h +1093 -0
- package/vendor/wasm3/m3_atomic.h +114 -0
- package/vendor/wasm3/m3_bind.c +174 -0
- package/vendor/wasm3/m3_bind.h +20 -0
- package/vendor/wasm3/m3_code.c +232 -0
- package/vendor/wasm3/m3_code.h +74 -0
- package/vendor/wasm3/m3_compile.c +6603 -0
- package/vendor/wasm3/m3_compile.h +354 -0
- package/vendor/wasm3/m3_config.h +448 -0
- package/vendor/wasm3/m3_config_platforms.h +345 -0
- package/vendor/wasm3/m3_core.c +1034 -0
- package/vendor/wasm3/m3_core.h +526 -0
- package/vendor/wasm3/m3_deterministic.c +65 -0
- package/vendor/wasm3/m3_deterministic.h +59 -0
- package/vendor/wasm3/m3_env.c +3146 -0
- package/vendor/wasm3/m3_env.h +748 -0
- package/vendor/wasm3/m3_exception.h +33 -0
- package/vendor/wasm3/m3_exec.c +8 -0
- package/vendor/wasm3/m3_exec.h +4110 -0
- package/vendor/wasm3/m3_exec_atomic.h +146 -0
- package/vendor/wasm3/m3_exec_defs.h +76 -0
- package/vendor/wasm3/m3_function.c +278 -0
- package/vendor/wasm3/m3_function.h +244 -0
- package/vendor/wasm3/m3_host.h +226 -0
- package/vendor/wasm3/m3_host_none.h +76 -0
- package/vendor/wasm3/m3_host_posix.h +543 -0
- package/vendor/wasm3/m3_host_win32.h +253 -0
- package/vendor/wasm3/m3_info.c +616 -0
- package/vendor/wasm3/m3_info.h +38 -0
- package/vendor/wasm3/m3_math_utils.h +560 -0
- package/vendor/wasm3/m3_module.c +431 -0
- package/vendor/wasm3/m3_parse.c +1549 -0
- package/vendor/wasm3/m3_snapshot.c +3825 -0
- package/vendor/wasm3/m3_validate.c +1770 -0
- package/vendor/wasm3/m3_validate.h +25 -0
- package/vendor/wasm3/m3_wasi.h +10 -0
- package/vendor/wasm3/m3_xxh64.c +211 -0
- package/vendor/wasm3/wasm3.h +684 -0
- package/vendor/wasm3/wasm3_defs.h +350 -0
- package/runtime/ext_node/worker_threads/worker_threads.c +0 -214
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc runtime — worker transport, operating-system half.
|
|
3
|
+
*
|
|
4
|
+
* Everything that differs between POSIX and Windows lives here:
|
|
5
|
+
*
|
|
6
|
+
* POSIX Windows
|
|
7
|
+
* ----------------------------- -----------------------------------------
|
|
8
|
+
* socket()/close() SOCKET/closesocket() (Winsock is started
|
|
9
|
+
* by `xt_set_program_args` at startup)
|
|
10
|
+
* fork() + execvp() _spawnvp(_P_NOWAIT) -> process HANDLE
|
|
11
|
+
* close(3..255) in the child only inheritable handles cross the
|
|
12
|
+
* boundary, so the control socket is marked
|
|
13
|
+
* non-inheritable before the spawn
|
|
14
|
+
* waitpid(pid, WNOHANG) WaitForSingleObject(handle, 0)
|
|
15
|
+
* kill(pid, SIGKILL) TerminateProcess(handle, 1)
|
|
16
|
+
* setenv/unsetenv _putenv_s
|
|
17
|
+
*
|
|
18
|
+
* The control channel is a loopback TCP socket on both platforms: Windows
|
|
19
|
+
* `select(2)` only accepts sockets, and one transport keeps the protocol
|
|
20
|
+
* single. The worker's stdout/stderr stay inherited, so `console.log` inside a
|
|
21
|
+
* worker still reaches the terminal and never corrupts the protocol.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
#include "worker_internal.h"
|
|
25
|
+
|
|
26
|
+
#include <errno.h>
|
|
27
|
+
#include <stdio.h>
|
|
28
|
+
#include <stdlib.h>
|
|
29
|
+
#include <string.h>
|
|
30
|
+
#include <time.h>
|
|
31
|
+
|
|
32
|
+
#if defined(_WIN32)
|
|
33
|
+
#include <process.h>
|
|
34
|
+
#include <winsock2.h>
|
|
35
|
+
#include <ws2tcpip.h>
|
|
36
|
+
#include <windows.h>
|
|
37
|
+
typedef int xt_socklen_t;
|
|
38
|
+
#else
|
|
39
|
+
#include <arpa/inet.h>
|
|
40
|
+
#include <fcntl.h>
|
|
41
|
+
#include <netinet/in.h>
|
|
42
|
+
#include <signal.h>
|
|
43
|
+
#include <sys/select.h>
|
|
44
|
+
#include <sys/socket.h>
|
|
45
|
+
#include <sys/time.h>
|
|
46
|
+
#include <sys/types.h>
|
|
47
|
+
#include <sys/wait.h>
|
|
48
|
+
#include <unistd.h>
|
|
49
|
+
typedef socklen_t xt_socklen_t;
|
|
50
|
+
#endif
|
|
51
|
+
|
|
52
|
+
char *xt_wrk_duplicate(const char *text) {
|
|
53
|
+
size_t length = text ? strlen(text) : 0;
|
|
54
|
+
char *copy = (char *)malloc(length + 1);
|
|
55
|
+
if (!copy) return NULL;
|
|
56
|
+
if (length > 0) memcpy(copy, text, length);
|
|
57
|
+
copy[length] = 0;
|
|
58
|
+
return copy;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/* -- environment ---------------------------------------------------------- */
|
|
62
|
+
|
|
63
|
+
int xt_wrk_env_set(const char *key, const char *value) {
|
|
64
|
+
if (!value) value = "";
|
|
65
|
+
#if defined(_WIN32)
|
|
66
|
+
return _putenv_s(key, value) == 0;
|
|
67
|
+
#else
|
|
68
|
+
return setenv(key, value, 1) == 0;
|
|
69
|
+
#endif
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
void xt_wrk_env_unset(const char *key) {
|
|
73
|
+
#if defined(_WIN32)
|
|
74
|
+
_putenv_s(key, "");
|
|
75
|
+
#else
|
|
76
|
+
unsetenv(key);
|
|
77
|
+
#endif
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const char *xt_wrk_env_get(const char *key) { return getenv(key); }
|
|
81
|
+
|
|
82
|
+
static int xt_wrk_os_pid(void) {
|
|
83
|
+
#if defined(_WIN32)
|
|
84
|
+
return _getpid();
|
|
85
|
+
#else
|
|
86
|
+
return (int)getpid();
|
|
87
|
+
#endif
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/* -- sockets -------------------------------------------------------------- */
|
|
91
|
+
|
|
92
|
+
int xt_wrk_would_block(void) {
|
|
93
|
+
#if defined(_WIN32)
|
|
94
|
+
return WSAGetLastError() == WSAEWOULDBLOCK;
|
|
95
|
+
#else
|
|
96
|
+
return errno == EAGAIN || errno == EWOULDBLOCK;
|
|
97
|
+
#endif
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
void xt_wrk_close_fd(int fd) {
|
|
101
|
+
if (fd < 0) return;
|
|
102
|
+
#if defined(_WIN32)
|
|
103
|
+
closesocket((SOCKET)(uintptr_t)fd);
|
|
104
|
+
#else
|
|
105
|
+
close(fd);
|
|
106
|
+
#endif
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/* Keep the control socket out of the child when the host inherits handles. On
|
|
110
|
+
* POSIX the child closes 3..255 itself after fork instead. */
|
|
111
|
+
static void xt_wrk_no_inherit(int fd) {
|
|
112
|
+
#if defined(_WIN32)
|
|
113
|
+
SetHandleInformation((HANDLE)(uintptr_t)fd, HANDLE_FLAG_INHERIT, 0);
|
|
114
|
+
#else
|
|
115
|
+
(void)fd;
|
|
116
|
+
#endif
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
int xt_wrk_set_nonblocking(int fd) {
|
|
120
|
+
#if defined(_WIN32)
|
|
121
|
+
u_long mode = 1;
|
|
122
|
+
return ioctlsocket((SOCKET)(uintptr_t)fd, FIONBIO, &mode) == 0;
|
|
123
|
+
#else
|
|
124
|
+
int flags = fcntl(fd, F_GETFL, 0);
|
|
125
|
+
if (flags < 0) return 0;
|
|
126
|
+
return fcntl(fd, F_SETFL, flags | O_NONBLOCK) == 0;
|
|
127
|
+
#endif
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/* A peer may disappear between the check and the write; never let that kill the
|
|
131
|
+
* process through SIGPIPE. Linux takes MSG_NOSIGNAL per call, macOS/BSD set the
|
|
132
|
+
* socket option once. */
|
|
133
|
+
static void xt_wrk_no_sigpipe(int fd) {
|
|
134
|
+
#if defined(SO_NOSIGPIPE)
|
|
135
|
+
int yes = 1;
|
|
136
|
+
setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, (const char *)&yes, sizeof(yes));
|
|
137
|
+
#else
|
|
138
|
+
(void)fd;
|
|
139
|
+
#endif
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
int xt_wrk_listen_loopback(int *out_port) {
|
|
143
|
+
*out_port = 0;
|
|
144
|
+
int fd = (int)(intptr_t)socket(AF_INET, SOCK_STREAM, 0);
|
|
145
|
+
if (fd < 0) return -1;
|
|
146
|
+
int yes = 1;
|
|
147
|
+
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&yes, sizeof(yes));
|
|
148
|
+
struct sockaddr_in address;
|
|
149
|
+
memset(&address, 0, sizeof(address));
|
|
150
|
+
address.sin_family = AF_INET;
|
|
151
|
+
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
152
|
+
address.sin_port = 0;
|
|
153
|
+
if (bind(fd, (struct sockaddr *)&address, sizeof(address)) != 0 ||
|
|
154
|
+
listen(fd, 8) != 0) {
|
|
155
|
+
xt_wrk_close_fd(fd);
|
|
156
|
+
return -1;
|
|
157
|
+
}
|
|
158
|
+
xt_socklen_t length = (xt_socklen_t)sizeof(address);
|
|
159
|
+
if (getsockname(fd, (struct sockaddr *)&address, &length) != 0) {
|
|
160
|
+
xt_wrk_close_fd(fd);
|
|
161
|
+
return -1;
|
|
162
|
+
}
|
|
163
|
+
*out_port = (int)ntohs(address.sin_port);
|
|
164
|
+
xt_wrk_no_sigpipe(fd);
|
|
165
|
+
xt_wrk_no_inherit(fd);
|
|
166
|
+
/* `accept` is also called when a worker is drained after exit, when no
|
|
167
|
+
* connection may be pending; a blocking listener would hang the program. */
|
|
168
|
+
xt_wrk_set_nonblocking(fd);
|
|
169
|
+
return fd;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
int xt_wrk_accept(int listen_fd) {
|
|
173
|
+
struct sockaddr_in address;
|
|
174
|
+
xt_socklen_t length = (xt_socklen_t)sizeof(address);
|
|
175
|
+
int fd = (int)(intptr_t)accept(listen_fd, (struct sockaddr *)&address, &length);
|
|
176
|
+
if (fd < 0) return -1;
|
|
177
|
+
xt_wrk_no_inherit(fd);
|
|
178
|
+
xt_wrk_set_nonblocking(fd);
|
|
179
|
+
return fd;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
int xt_wrk_connect_loopback(int port) {
|
|
183
|
+
int fd = (int)(intptr_t)socket(AF_INET, SOCK_STREAM, 0);
|
|
184
|
+
if (fd < 0) return -1;
|
|
185
|
+
xt_wrk_no_inherit(fd);
|
|
186
|
+
struct sockaddr_in address;
|
|
187
|
+
memset(&address, 0, sizeof(address));
|
|
188
|
+
address.sin_family = AF_INET;
|
|
189
|
+
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
190
|
+
address.sin_port = htons((unsigned short)port);
|
|
191
|
+
if (connect(fd, (struct sockaddr *)&address, sizeof(address)) != 0) {
|
|
192
|
+
xt_wrk_close_fd(fd);
|
|
193
|
+
return -1;
|
|
194
|
+
}
|
|
195
|
+
xt_wrk_no_sigpipe(fd);
|
|
196
|
+
xt_wrk_set_nonblocking(fd);
|
|
197
|
+
return fd;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/* Send as much as the socket accepts. Returns >= 0 bytes written, -1 on error.
|
|
201
|
+
* A short write is not an error; the caller keeps the remainder. */
|
|
202
|
+
long xt_wrk_send_some(int fd, const char *data, size_t length) {
|
|
203
|
+
if (length == 0) return 0;
|
|
204
|
+
if (length > 1u << 20) length = 1u << 20;
|
|
205
|
+
#if defined(_WIN32)
|
|
206
|
+
int written = send((SOCKET)(uintptr_t)fd, data, (int)length, 0);
|
|
207
|
+
#elif defined(MSG_NOSIGNAL)
|
|
208
|
+
ssize_t written = send(fd, data, length, MSG_NOSIGNAL);
|
|
209
|
+
#else
|
|
210
|
+
ssize_t written = send(fd, data, length, 0);
|
|
211
|
+
#endif
|
|
212
|
+
if (written < 0) return -1;
|
|
213
|
+
return (long)written;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/* Receive one chunk. > 0 bytes, 0 on clean EOF, -1 on error, -2 would block. */
|
|
217
|
+
long xt_wrk_recv_some(int fd, char *data, size_t capacity) {
|
|
218
|
+
#if defined(_WIN32)
|
|
219
|
+
int got = recv((SOCKET)(uintptr_t)fd, data, (int)capacity, 0);
|
|
220
|
+
#else
|
|
221
|
+
ssize_t got = recv(fd, data, capacity, 0);
|
|
222
|
+
#endif
|
|
223
|
+
if (got > 0) return (long)got;
|
|
224
|
+
if (got == 0) return 0;
|
|
225
|
+
return xt_wrk_would_block() ? -2 : -1;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/* Used only on shutdown, to give a queued reply a chance to leave the socket
|
|
229
|
+
* before it is closed. */
|
|
230
|
+
int xt_wrk_wait_writable(int fd, int timeout_ms) {
|
|
231
|
+
fd_set write_set;
|
|
232
|
+
FD_ZERO(&write_set);
|
|
233
|
+
#if defined(_WIN32)
|
|
234
|
+
FD_SET((SOCKET)(uintptr_t)fd, &write_set);
|
|
235
|
+
#else
|
|
236
|
+
FD_SET(fd, &write_set);
|
|
237
|
+
#endif
|
|
238
|
+
struct timeval timeout;
|
|
239
|
+
timeout.tv_sec = timeout_ms / 1000;
|
|
240
|
+
timeout.tv_usec = (timeout_ms % 1000) * 1000;
|
|
241
|
+
return select(fd + 1, NULL, &write_set, NULL, &timeout) > 0;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/* Wait for data (or EOF) when a link is drained outside the reactor, e.g. after
|
|
245
|
+
* its process was reaped. 1 when readable. */
|
|
246
|
+
int xt_wrk_wait_readable(int fd, int timeout_ms) {
|
|
247
|
+
fd_set read_set;
|
|
248
|
+
FD_ZERO(&read_set);
|
|
249
|
+
#if defined(_WIN32)
|
|
250
|
+
FD_SET((SOCKET)(uintptr_t)fd, &read_set);
|
|
251
|
+
#else
|
|
252
|
+
FD_SET(fd, &read_set);
|
|
253
|
+
#endif
|
|
254
|
+
struct timeval timeout;
|
|
255
|
+
timeout.tv_sec = timeout_ms / 1000;
|
|
256
|
+
timeout.tv_usec = (timeout_ms % 1000) * 1000;
|
|
257
|
+
return select(fd + 1, &read_set, NULL, NULL, &timeout) > 0;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/* -- processes ------------------------------------------------------------ */
|
|
261
|
+
|
|
262
|
+
xt_wrk_pid xt_wrk_spawn_process(const char *path, char **error) {
|
|
263
|
+
*error = NULL;
|
|
264
|
+
#if defined(_WIN32)
|
|
265
|
+
const char *argv[2] = {path, NULL};
|
|
266
|
+
intptr_t handle = _spawnvp(_P_NOWAIT, path, argv);
|
|
267
|
+
if (handle == -1) {
|
|
268
|
+
char buffer[256];
|
|
269
|
+
snprintf(buffer, sizeof(buffer), "failed to start worker process: %s", strerror(errno));
|
|
270
|
+
*error = xt_wrk_duplicate(buffer);
|
|
271
|
+
return 0;
|
|
272
|
+
}
|
|
273
|
+
return handle;
|
|
274
|
+
#else
|
|
275
|
+
pid_t pid = fork();
|
|
276
|
+
if (pid < 0) {
|
|
277
|
+
char buffer[256];
|
|
278
|
+
snprintf(buffer, sizeof(buffer), "failed to fork worker process: %s", strerror(errno));
|
|
279
|
+
*error = xt_wrk_duplicate(buffer);
|
|
280
|
+
return 0;
|
|
281
|
+
}
|
|
282
|
+
if (pid == 0) {
|
|
283
|
+
/* Only stdio belongs to the worker. An inherited listener or client socket
|
|
284
|
+
* from another worker would delay that worker's EOF, so drop 3..255. The
|
|
285
|
+
* program path is absolute (`import.meta.filename`), so `execv` avoids the
|
|
286
|
+
* PATH search's allocation after a fork in a multithreaded process. */
|
|
287
|
+
for (int fd = 3; fd < 256; fd++) close(fd);
|
|
288
|
+
char *const argv[2] = {(char *)path, NULL};
|
|
289
|
+
execv(path, argv);
|
|
290
|
+
_exit(127);
|
|
291
|
+
}
|
|
292
|
+
return (xt_wrk_pid)pid;
|
|
293
|
+
#endif
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
int xt_wrk_process_exited(xt_wrk_pid pid, int *exit_code) {
|
|
297
|
+
*exit_code = -1;
|
|
298
|
+
if (pid <= 0) return 1;
|
|
299
|
+
#if defined(_WIN32)
|
|
300
|
+
if (WaitForSingleObject((HANDLE)(uintptr_t)pid, 0) == WAIT_TIMEOUT) return 0;
|
|
301
|
+
DWORD code = 0;
|
|
302
|
+
if (GetExitCodeProcess((HANDLE)(uintptr_t)pid, &code)) *exit_code = (int)code;
|
|
303
|
+
CloseHandle((HANDLE)(uintptr_t)pid);
|
|
304
|
+
return 1;
|
|
305
|
+
#else
|
|
306
|
+
int status = 0;
|
|
307
|
+
pid_t result = waitpid((pid_t)pid, &status, WNOHANG);
|
|
308
|
+
if (result == 0) return 0;
|
|
309
|
+
if (result < 0) {
|
|
310
|
+
*exit_code = -1;
|
|
311
|
+
return 1;
|
|
312
|
+
}
|
|
313
|
+
if (WIFEXITED(status)) *exit_code = WEXITSTATUS(status);
|
|
314
|
+
else if (WIFSIGNALED(status)) *exit_code = -WTERMSIG(status);
|
|
315
|
+
return 1;
|
|
316
|
+
#endif
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
void xt_wrk_kill_process(xt_wrk_pid pid) {
|
|
320
|
+
if (pid <= 0) return;
|
|
321
|
+
#if defined(_WIN32)
|
|
322
|
+
TerminateProcess((HANDLE)(uintptr_t)pid, 1);
|
|
323
|
+
#else
|
|
324
|
+
kill((pid_t)pid, SIGKILL);
|
|
325
|
+
#endif
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/* -- token ---------------------------------------------------------------- */
|
|
329
|
+
|
|
330
|
+
void xt_wrk_make_token(char *out, size_t capacity) {
|
|
331
|
+
static unsigned long counter = 0;
|
|
332
|
+
unsigned long long seed =
|
|
333
|
+
(unsigned long long)time(NULL) ^ ((unsigned long long)xt_wrk_os_pid() << 16) ^
|
|
334
|
+
(unsigned long long)(counter++ * 2654435761u);
|
|
335
|
+
static const char digits[] = "0123456789abcdef";
|
|
336
|
+
size_t written = 0;
|
|
337
|
+
for (int i = 0; i < 8 && written + 2 < capacity; i++) {
|
|
338
|
+
seed = seed * 6364136223846793005ULL + 1442695040888963407ULL;
|
|
339
|
+
unsigned long value = (unsigned long)((seed >> 33) & 0xff);
|
|
340
|
+
out[written++] = digits[value >> 4];
|
|
341
|
+
out[written++] = digits[value & 0x0f];
|
|
342
|
+
}
|
|
343
|
+
out[written] = 0;
|
|
344
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc runtime — `MessageChannel` / `MessagePort`.
|
|
3
|
+
*
|
|
4
|
+
* Both worker APIs expose the same channel:
|
|
5
|
+
*
|
|
6
|
+
* const { port1, port2 } = new MessageChannel();
|
|
7
|
+
* port1.onmessage = (event) => ...; // Web Worker
|
|
8
|
+
* port2.on("message", (value) => ...); // node:worker_threads
|
|
9
|
+
* port2.postMessage(value);
|
|
10
|
+
*
|
|
11
|
+
* Delivery is asynchronous (a zero-delay timer), like the platform API, and
|
|
12
|
+
* every value is structured-cloned on the way in.
|
|
13
|
+
*
|
|
14
|
+
* A port is local to the process. Sending a port to a worker (`postMessage`
|
|
15
|
+
* with a transfer list) is not supported: the codec rejects a MessagePort
|
|
16
|
+
* instead of silently copying an object that would never deliver. The transfer
|
|
17
|
+
* list argument itself is accepted and the value is copied, exactly like
|
|
18
|
+
* `structuredClone`.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
#include "worker_internal.h"
|
|
22
|
+
|
|
23
|
+
#include <stdlib.h>
|
|
24
|
+
#include <string.h>
|
|
25
|
+
|
|
26
|
+
static xt_value xt_wrk_str(const char *text) { return xt_string_from_cstr(text); }
|
|
27
|
+
|
|
28
|
+
static void xt_wrk_put(xt_value object, const char *key, xt_value value) {
|
|
29
|
+
xt_object_set(object, xt_wrk_str(key), value);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
static xt_value xt_wrk_fetch(xt_value object, const char *key) {
|
|
33
|
+
return xt_object_get_cstr(object, key);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/* -- delivery ------------------------------------------------------------- */
|
|
37
|
+
|
|
38
|
+
static xt_value xt_wrk_port_drain(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
39
|
+
(void)self;
|
|
40
|
+
(void)argc;
|
|
41
|
+
(void)argv;
|
|
42
|
+
xt_value port = xt_closure_env(env, 0);
|
|
43
|
+
xt_wrk_put(port, "__xt_scheduled", XT_FALSE);
|
|
44
|
+
if (xt_truthy(xt_wrk_fetch(port, "__xt_closed"))) return XT_UNDEFINED;
|
|
45
|
+
xt_value queue = xt_wrk_fetch(port, "__xt_queue");
|
|
46
|
+
if (!XT_IS_ARRAY(queue) || xt_array_size(queue) == 0) return XT_UNDEFINED;
|
|
47
|
+
xt_wrk_put(port, "__xt_queue", xt_array_new(0, NULL));
|
|
48
|
+
int32_t count = xt_array_size(queue);
|
|
49
|
+
xt_value *items = xt_array_items(queue);
|
|
50
|
+
for (int32_t i = 0; i < count; i++) xt_wrk_emit1(port, "message", items[i]);
|
|
51
|
+
return XT_UNDEFINED;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static void xt_wrk_port_schedule(xt_value port) {
|
|
55
|
+
if (xt_truthy(xt_wrk_fetch(port, "__xt_scheduled"))) return;
|
|
56
|
+
xt_wrk_put(port, "__xt_scheduled", XT_TRUE);
|
|
57
|
+
xt_value environment[1] = {port};
|
|
58
|
+
xt_value arguments[2] = {xt_closure_new((void *)xt_wrk_port_drain, 1, environment),
|
|
59
|
+
xt_number(0)};
|
|
60
|
+
xt_set_timeout(2, arguments);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static xt_value xt_wrk_port_post(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
64
|
+
(void)env;
|
|
65
|
+
if (xt_truthy(xt_wrk_fetch(self, "__xt_closed"))) return XT_UNDEFINED;
|
|
66
|
+
xt_value peer = xt_wrk_fetch(self, "__xt_peer");
|
|
67
|
+
if (!XT_IS_OBJECT(peer)) return XT_UNDEFINED;
|
|
68
|
+
xt_value value = xt_arg_at(argc, argv, 0);
|
|
69
|
+
xt_value clone = xt_structured_clone(1, &value);
|
|
70
|
+
xt_value queue = xt_wrk_fetch(peer, "__xt_queue");
|
|
71
|
+
if (!XT_IS_ARRAY(queue)) {
|
|
72
|
+
queue = xt_array_new(0, NULL);
|
|
73
|
+
xt_wrk_put(peer, "__xt_queue", queue);
|
|
74
|
+
}
|
|
75
|
+
xt_array_push(queue, clone);
|
|
76
|
+
xt_wrk_port_schedule(peer);
|
|
77
|
+
return XT_UNDEFINED;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
static xt_value xt_wrk_port_close(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
81
|
+
(void)env;
|
|
82
|
+
(void)argc;
|
|
83
|
+
(void)argv;
|
|
84
|
+
xt_wrk_put(self, "__xt_closed", XT_TRUE);
|
|
85
|
+
xt_value peer = xt_wrk_fetch(self, "__xt_peer");
|
|
86
|
+
if (XT_IS_OBJECT(peer)) xt_wrk_put(peer, "__xt_closed", XT_TRUE);
|
|
87
|
+
xt_wrk_put(self, "__xt_peer", XT_UNDEFINED);
|
|
88
|
+
xt_wrk_put(self, "__xt_queue", xt_array_new(0, NULL));
|
|
89
|
+
return XT_UNDEFINED;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
static xt_value xt_wrk_port_start(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
93
|
+
(void)env;
|
|
94
|
+
(void)argc;
|
|
95
|
+
(void)argv;
|
|
96
|
+
xt_wrk_port_schedule(self);
|
|
97
|
+
return XT_UNDEFINED;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/* -- object shape --------------------------------------------------------- */
|
|
101
|
+
|
|
102
|
+
static xt_value xt_wrk_port_new(void) {
|
|
103
|
+
xt_value port = xt_object_new();
|
|
104
|
+
xt_wrk_put(port, "__xt_port", XT_TRUE);
|
|
105
|
+
xt_wrk_put(port, "__xt_queue", xt_array_new(0, NULL));
|
|
106
|
+
xt_wrk_put(port, "__xt_scheduled", XT_FALSE);
|
|
107
|
+
xt_wrk_install_emitter(port);
|
|
108
|
+
xt_wrk_install_dom_events(port);
|
|
109
|
+
xt_object_set(port, xt_wrk_str("postMessage"), xt_closure_new((void *)xt_wrk_port_post, 0, NULL));
|
|
110
|
+
xt_object_set(port, xt_wrk_str("close"), xt_closure_new((void *)xt_wrk_port_close, 0, NULL));
|
|
111
|
+
xt_object_set(port, xt_wrk_str("start"), xt_closure_new((void *)xt_wrk_port_start, 0, NULL));
|
|
112
|
+
xt_object_set(port, xt_wrk_str("ref"), xt_closure_new((void *)xt_wrk_port_start, 0, NULL));
|
|
113
|
+
xt_object_set(port, xt_wrk_str("unref"), xt_closure_new((void *)xt_wrk_port_start, 0, NULL));
|
|
114
|
+
return port;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
xt_value xt_wrk_new_channel(void) {
|
|
118
|
+
xt_value port1 = xt_wrk_port_new();
|
|
119
|
+
xt_value port2 = xt_wrk_port_new();
|
|
120
|
+
xt_wrk_put(port1, "__xt_peer", port2);
|
|
121
|
+
xt_wrk_put(port2, "__xt_peer", port1);
|
|
122
|
+
xt_value channel = xt_object_new();
|
|
123
|
+
xt_object_set(channel, xt_wrk_str("port1"), port1);
|
|
124
|
+
xt_object_set(channel, xt_wrk_str("port2"), port2);
|
|
125
|
+
return channel;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/* -- exported constructors ------------------------------------------------ */
|
|
129
|
+
|
|
130
|
+
static xt_value xt_wrk_channel_ctor(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
131
|
+
(void)self;
|
|
132
|
+
(void)env;
|
|
133
|
+
(void)argc;
|
|
134
|
+
(void)argv;
|
|
135
|
+
return xt_wrk_new_channel();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
static xt_value xt_wrk_port_ctor(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
|
|
139
|
+
(void)self;
|
|
140
|
+
(void)env;
|
|
141
|
+
(void)argc;
|
|
142
|
+
(void)argv;
|
|
143
|
+
xt_throw(xt_error_from_message("TypeError: MessagePort is not constructible"));
|
|
144
|
+
return XT_UNDEFINED;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
static xt_value xt_webworker_channel_value = XT_UNDEFINED;
|
|
148
|
+
static xt_value xt_webworker_port_value = XT_UNDEFINED;
|
|
149
|
+
|
|
150
|
+
xt_value xt_webworker_message_channel(int32_t argc, xt_value *argv) {
|
|
151
|
+
(void)argc;
|
|
152
|
+
(void)argv;
|
|
153
|
+
if (!XT_IS_FUNCTION(xt_webworker_channel_value)) {
|
|
154
|
+
xt_webworker_channel_value = xt_closure_new((void *)xt_wrk_channel_ctor, 0, NULL);
|
|
155
|
+
xt_gc_add_root(&xt_webworker_channel_value);
|
|
156
|
+
}
|
|
157
|
+
return xt_webworker_channel_value;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
xt_value xt_webworker_message_port(int32_t argc, xt_value *argv) {
|
|
161
|
+
(void)argc;
|
|
162
|
+
(void)argv;
|
|
163
|
+
if (!XT_IS_FUNCTION(xt_webworker_port_value)) {
|
|
164
|
+
xt_webworker_port_value = xt_closure_new((void *)xt_wrk_port_ctor, 0, NULL);
|
|
165
|
+
xt_gc_add_root(&xt_webworker_port_value);
|
|
166
|
+
}
|
|
167
|
+
return xt_webworker_port_value;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
xt_value xt_webworker_message_channel_new(int32_t argc, xt_value *argv) {
|
|
171
|
+
(void)argc;
|
|
172
|
+
(void)argv;
|
|
173
|
+
return xt_wrk_new_channel();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
xt_value xt_webworker_message_port_new(int32_t argc, xt_value *argv) {
|
|
177
|
+
(void)argc;
|
|
178
|
+
(void)argv;
|
|
179
|
+
xt_throw(xt_error_from_message("TypeError: MessagePort is not constructible"));
|
|
180
|
+
return XT_UNDEFINED;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
xt_value xt_wt_message_channel(int32_t argc, xt_value *argv) {
|
|
184
|
+
(void)argc;
|
|
185
|
+
(void)argv;
|
|
186
|
+
return xt_wrk_new_channel();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
xt_value xt_wt_message_port(int32_t argc, xt_value *argv) {
|
|
190
|
+
(void)argc;
|
|
191
|
+
(void)argv;
|
|
192
|
+
xt_throw(xt_error_from_message("TypeError: MessagePort is not constructible"));
|
|
193
|
+
return XT_UNDEFINED;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/* `receiveMessageOnPort(port)` exists in Node; a local port already delivers on
|
|
197
|
+
* a timer, so the synchronous read returns `undefined` rather than pretending
|
|
198
|
+
* the queue is empty forever. */
|
|
199
|
+
xt_value xt_wt_receive_message_on_port(int32_t argc, xt_value *argv) {
|
|
200
|
+
(void)argc;
|
|
201
|
+
(void)argv;
|
|
202
|
+
return XT_UNDEFINED;
|
|
203
|
+
}
|