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.
Files changed (224) hide show
  1. package/AGENTS.md +1 -1
  2. package/dist/src/cli/extensions.js +11 -3
  3. package/dist/src/cli/extensions.js.map +1 -1
  4. package/dist/src/cli/help.d.ts +1 -1
  5. package/dist/src/cli/help.js +1 -1
  6. package/dist/src/codegen/generator/state.js +7 -0
  7. package/dist/src/codegen/generator/state.js.map +1 -1
  8. package/dist/src/codegen/generator/tables.js +19 -1
  9. package/dist/src/codegen/generator/tables.js.map +1 -1
  10. package/dist/src/driver/bundler/graph.js +9 -5
  11. package/dist/src/driver/bundler/graph.js.map +1 -1
  12. package/dist/src/driver/fingerprint.d.ts +2 -2
  13. package/dist/src/driver/fingerprint.js +3 -3
  14. package/dist/src/driver/fingerprint.js.map +1 -1
  15. package/dist/src/driver/link-features.js +1 -1
  16. package/dist/src/driver/link-features.js.map +1 -1
  17. package/dist/src/driver/paths.js +8 -4
  18. package/dist/src/driver/paths.js.map +1 -1
  19. package/dist/src/driver/runtime-objects.d.ts +1 -1
  20. package/dist/src/driver/runtime-objects.js +13 -1
  21. package/dist/src/driver/runtime-objects.js.map +1 -1
  22. package/dist/src/driver/version.d.ts +1 -1
  23. package/dist/src/driver/version.js +1 -1
  24. package/dist/src/extensions/catalog.js +5 -1
  25. package/dist/src/extensions/catalog.js.map +1 -1
  26. package/dist/src/extensions/gui/index.js +0 -1
  27. package/dist/src/extensions/gui/index.js.map +1 -1
  28. package/dist/src/extensions/link-feature.d.ts +8 -0
  29. package/dist/src/extensions/node/stream-web/index.d.ts +6 -1
  30. package/dist/src/extensions/node/stream-web/index.js +59 -4
  31. package/dist/src/extensions/node/stream-web/index.js.map +1 -1
  32. package/dist/src/extensions/node/worker_threads/index.d.ts +14 -4
  33. package/dist/src/extensions/node/worker_threads/index.js +24 -11
  34. package/dist/src/extensions/node/worker_threads/index.js.map +1 -1
  35. package/dist/src/extensions/registry.d.ts +9 -1
  36. package/dist/src/extensions/registry.js +4 -1
  37. package/dist/src/extensions/registry.js.map +1 -1
  38. package/dist/src/extensions/wasm-asset.d.ts +16 -0
  39. package/dist/src/extensions/wasm-asset.js +28 -0
  40. package/dist/src/extensions/wasm-asset.js.map +1 -0
  41. package/dist/src/extensions/wasm.d.ts +17 -0
  42. package/dist/src/extensions/wasm.js +68 -0
  43. package/dist/src/extensions/wasm.js.map +1 -0
  44. package/dist/src/extensions/webworker/index.d.ts +24 -0
  45. package/dist/src/extensions/webworker/index.js +54 -0
  46. package/dist/src/extensions/webworker/index.js.map +1 -0
  47. package/dist/src/index.d.ts +1 -0
  48. package/dist/src/index.js +1 -0
  49. package/dist/src/index.js.map +1 -1
  50. package/dist/tests/driver/link-features.test.js +9 -0
  51. package/dist/tests/driver/link-features.test.js.map +1 -1
  52. package/dist/tests/e2e/async-fs.test.js +44 -0
  53. package/dist/tests/e2e/async-fs.test.js.map +1 -1
  54. package/dist/tests/e2e/harness.d.ts +6 -0
  55. package/dist/tests/e2e/harness.js +13 -1
  56. package/dist/tests/e2e/harness.js.map +1 -1
  57. package/dist/tests/e2e/node-crypto-random.test.d.ts +8 -0
  58. package/dist/tests/e2e/node-crypto-random.test.js +148 -0
  59. package/dist/tests/e2e/node-crypto-random.test.js.map +1 -0
  60. package/dist/tests/e2e/node-modules.test.js +1 -1
  61. package/dist/tests/e2e/node-modules.test.js.map +1 -1
  62. package/dist/tests/e2e/web-streams.test.d.ts +8 -0
  63. package/dist/tests/e2e/web-streams.test.js +274 -0
  64. package/dist/tests/e2e/web-streams.test.js.map +1 -0
  65. package/dist/tests/e2e/web-worker.test.d.ts +7 -0
  66. package/dist/tests/e2e/web-worker.test.js +74 -0
  67. package/dist/tests/e2e/web-worker.test.js.map +1 -0
  68. package/dist/tests/e2e/webassembly.test.d.ts +8 -0
  69. package/dist/tests/e2e/webassembly.test.js +113 -0
  70. package/dist/tests/e2e/webassembly.test.js.map +1 -0
  71. package/dist/tests/e2e/worker-api.test.d.ts +14 -0
  72. package/dist/tests/e2e/worker-api.test.js +460 -0
  73. package/dist/tests/e2e/worker-api.test.js.map +1 -0
  74. package/dist/tests/e2e/worker-threads.test.d.ts +8 -0
  75. package/dist/tests/e2e/worker-threads.test.js +160 -0
  76. package/dist/tests/e2e/worker-threads.test.js.map +1 -0
  77. package/dist/tests/extensions/wasm.test.d.ts +1 -0
  78. package/dist/tests/extensions/wasm.test.js +53 -0
  79. package/dist/tests/extensions/wasm.test.js.map +1 -0
  80. package/doc/ai/cli.md +1 -1
  81. package/doc/ai/contributing.md +11 -4
  82. package/doc/ai/extensions.md +25 -2
  83. package/doc/ai/language-support.md +14 -2
  84. package/doc/ai/zh-CN/cli.md +1 -1
  85. package/doc/ai/zh-CN/contributing.md +8 -3
  86. package/doc/ai/zh-CN/extensions.md +23 -2
  87. package/doc/ai/zh-CN/language-support.md +13 -2
  88. package/doc/gui/api.md +1 -1
  89. package/doc/implemented/cli.md +1 -1
  90. package/doc/implemented/extensions.md +6 -5
  91. package/doc/implemented/node/stream-web.md +31 -0
  92. package/doc/implemented/node/worker-threads.md +88 -5
  93. package/doc/implemented/node.md +2 -1
  94. package/doc/implemented/streams.md +82 -0
  95. package/doc/implemented/wasm.md +80 -18
  96. package/doc/implemented/web-worker.md +84 -0
  97. package/doc/implemented.md +6 -2
  98. package/doc/unimplemented/deviations.md +1 -0
  99. package/doc/unimplemented/node/worker-threads.md +23 -0
  100. package/doc/unimplemented/node.md +1 -0
  101. package/doc/unimplemented/stdlib.md +2 -1
  102. package/doc/unimplemented/wasm.md +21 -9
  103. package/doc/unimplemented.md +1 -1
  104. package/doc/zh-CN/gui/api.md +1 -1
  105. package/doc/zh-CN/implemented/cli.md +1 -1
  106. package/doc/zh-CN/implemented/extensions.md +7 -6
  107. package/doc/zh-CN/implemented/node/stream-web.md +24 -0
  108. package/doc/zh-CN/implemented/node/worker-threads.md +77 -4
  109. package/doc/zh-CN/implemented/node.md +2 -1
  110. package/doc/zh-CN/implemented/streams.md +60 -0
  111. package/doc/zh-CN/implemented/wasm.md +73 -16
  112. package/doc/zh-CN/implemented/web-worker.md +79 -0
  113. package/doc/zh-CN/implemented.md +6 -2
  114. package/doc/zh-CN/unimplemented/deviations.md +1 -0
  115. package/doc/zh-CN/unimplemented/node/worker-threads.md +20 -0
  116. package/doc/zh-CN/unimplemented/node.md +1 -0
  117. package/doc/zh-CN/unimplemented/stdlib.md +2 -1
  118. package/doc/zh-CN/unimplemented/wasm.md +16 -8
  119. package/doc/zh-CN/unimplemented.md +1 -1
  120. package/llms.txt +2 -0
  121. package/package.json +8 -7
  122. package/runtime/ext_gui/browser.cpp +1 -1
  123. package/runtime/ext_node/stream_web/stream_web.c +20 -6
  124. package/runtime/worker/worker_api.c +422 -0
  125. package/runtime/worker/worker_codec.c +489 -0
  126. package/runtime/worker/worker_core.c +445 -0
  127. package/runtime/worker/worker_internal.h +223 -0
  128. package/runtime/worker/worker_platform.c +344 -0
  129. package/runtime/worker/worker_ports.c +203 -0
  130. package/runtime/worker/worker_transport.c +464 -0
  131. package/runtime/worker/worker_wire.c +150 -0
  132. package/runtime/xt_builtins.c +15 -0
  133. package/runtime/xt_constructors.c +49 -2
  134. package/runtime/xt_stdlib2/error.inc +4 -0
  135. package/runtime/xt_streams/common.inc +232 -0
  136. package/runtime/xt_streams/constructors.inc +232 -0
  137. package/runtime/xt_streams/encoding.inc +124 -0
  138. package/runtime/xt_streams/readable-extra.inc +372 -0
  139. package/runtime/xt_streams/readable.inc +415 -0
  140. package/runtime/xt_streams/transform.inc +195 -0
  141. package/runtime/xt_streams/writable.inc +542 -0
  142. package/runtime/xt_streams.c +30 -0
  143. package/runtime/xt_wasm/wasm.h +148 -0
  144. package/runtime/xt_wasm/wasm_api.c +357 -0
  145. package/runtime/xt_wasm/wasm_api.h +37 -0
  146. package/runtime/xt_wasm/wasm_bridge.c +30 -0
  147. package/runtime/xt_wasm/wasm_instance.c +591 -0
  148. package/runtime/xt_wasm/wasm_module.c +270 -0
  149. package/runtime/xt_wasm/wasm_values.c +177 -0
  150. package/runtime/xt_webassembly.c +15 -4
  151. package/scripts/fetch-vendored.ts +187 -20
  152. package/scripts/package-release.ts +3 -2
  153. package/src/cli/extensions.ts +11 -3
  154. package/src/cli/help.ts +1 -1
  155. package/src/codegen/generator/state.ts +7 -0
  156. package/src/codegen/generator/tables.ts +19 -1
  157. package/src/driver/bundler/graph.ts +9 -5
  158. package/src/driver/fingerprint.ts +3 -3
  159. package/src/driver/link-features.ts +1 -1
  160. package/src/driver/paths.ts +8 -4
  161. package/src/driver/runtime-objects.ts +13 -1
  162. package/src/driver/version.ts +1 -1
  163. package/src/extensions/catalog.ts +5 -1
  164. package/src/extensions/gui/index.ts +0 -1
  165. package/src/extensions/link-feature.ts +8 -0
  166. package/src/extensions/node/stream-web/index.ts +59 -4
  167. package/src/extensions/node/worker_threads/index.ts +24 -11
  168. package/src/extensions/registry.ts +13 -2
  169. package/src/extensions/wasm-asset.ts +32 -0
  170. package/src/extensions/wasm.ts +74 -0
  171. package/src/extensions/webworker/index.ts +58 -0
  172. package/src/index.ts +1 -0
  173. package/vendor/wasm3/.pin +1 -0
  174. package/vendor/wasm3/LICENSE +21 -0
  175. package/vendor/wasm3/m3_api_libc.c +265 -0
  176. package/vendor/wasm3/m3_api_libc.h +19 -0
  177. package/vendor/wasm3/m3_api_meta_wasi.c +1107 -0
  178. package/vendor/wasm3/m3_api_tracer.c +173 -0
  179. package/vendor/wasm3/m3_api_tracer.h +19 -0
  180. package/vendor/wasm3/m3_api_uvwasi.c +1290 -0
  181. package/vendor/wasm3/m3_api_wasi.c +1207 -0
  182. package/vendor/wasm3/m3_api_wasi.h +74 -0
  183. package/vendor/wasm3/m3_api_wasi_host.h +137 -0
  184. package/vendor/wasm3/m3_api_wasi_posix.h +979 -0
  185. package/vendor/wasm3/m3_api_wasi_win32.h +1093 -0
  186. package/vendor/wasm3/m3_atomic.h +114 -0
  187. package/vendor/wasm3/m3_bind.c +174 -0
  188. package/vendor/wasm3/m3_bind.h +20 -0
  189. package/vendor/wasm3/m3_code.c +232 -0
  190. package/vendor/wasm3/m3_code.h +74 -0
  191. package/vendor/wasm3/m3_compile.c +6603 -0
  192. package/vendor/wasm3/m3_compile.h +354 -0
  193. package/vendor/wasm3/m3_config.h +448 -0
  194. package/vendor/wasm3/m3_config_platforms.h +345 -0
  195. package/vendor/wasm3/m3_core.c +1034 -0
  196. package/vendor/wasm3/m3_core.h +526 -0
  197. package/vendor/wasm3/m3_deterministic.c +65 -0
  198. package/vendor/wasm3/m3_deterministic.h +59 -0
  199. package/vendor/wasm3/m3_env.c +3146 -0
  200. package/vendor/wasm3/m3_env.h +748 -0
  201. package/vendor/wasm3/m3_exception.h +33 -0
  202. package/vendor/wasm3/m3_exec.c +8 -0
  203. package/vendor/wasm3/m3_exec.h +4110 -0
  204. package/vendor/wasm3/m3_exec_atomic.h +146 -0
  205. package/vendor/wasm3/m3_exec_defs.h +76 -0
  206. package/vendor/wasm3/m3_function.c +278 -0
  207. package/vendor/wasm3/m3_function.h +244 -0
  208. package/vendor/wasm3/m3_host.h +226 -0
  209. package/vendor/wasm3/m3_host_none.h +76 -0
  210. package/vendor/wasm3/m3_host_posix.h +543 -0
  211. package/vendor/wasm3/m3_host_win32.h +253 -0
  212. package/vendor/wasm3/m3_info.c +616 -0
  213. package/vendor/wasm3/m3_info.h +38 -0
  214. package/vendor/wasm3/m3_math_utils.h +560 -0
  215. package/vendor/wasm3/m3_module.c +431 -0
  216. package/vendor/wasm3/m3_parse.c +1549 -0
  217. package/vendor/wasm3/m3_snapshot.c +3825 -0
  218. package/vendor/wasm3/m3_validate.c +1770 -0
  219. package/vendor/wasm3/m3_validate.h +25 -0
  220. package/vendor/wasm3/m3_wasi.h +10 -0
  221. package/vendor/wasm3/m3_xxh64.c +211 -0
  222. package/vendor/wasm3/wasm3.h +684 -0
  223. package/vendor/wasm3/wasm3_defs.h +350 -0
  224. package/runtime/ext_node/worker_threads/worker_threads.c +0 -214
@@ -0,0 +1,464 @@
1
+ /*
2
+ * xbintsc runtime — worker transport, parent half.
3
+ *
4
+ * `new Worker()` listens on 127.0.0.1:0, writes the port and a one-shot token
5
+ * into the environment, and spawns the executable again (see
6
+ * `worker_platform.c` for the POSIX/Windows split). The child connects back and
7
+ * sends the token as its first bytes; from then on both directions carry
8
+ * length-prefixed frames.
9
+ *
10
+ * Lifetime follows Node: the parent stays alive while a worker runs, and the
11
+ * worker's `exit` is detected by polling the child's status — a socket EOF only
12
+ * says the port closed, not that the process ended.
13
+ */
14
+
15
+ #include "worker_internal.h"
16
+
17
+ #include <stdio.h>
18
+ #include <stdlib.h>
19
+ #include <string.h>
20
+
21
+ /* -- frame plumbing ------------------------------------------------------- */
22
+
23
+ void xt_wrk_in_append(xt_wrk_link *link, const char *data, size_t length) {
24
+ if (link->in_length + length + 1 > link->in_capacity) {
25
+ size_t next = link->in_capacity ? link->in_capacity : 256;
26
+ while (next < link->in_length + length + 1) next *= 2;
27
+ char *grown = (char *)realloc(link->in, next);
28
+ if (!grown) abort();
29
+ link->in = grown;
30
+ link->in_capacity = next;
31
+ }
32
+ memcpy(link->in + link->in_length, data, length);
33
+ link->in_length += length;
34
+ }
35
+
36
+ static void xt_wrk_in_consume(xt_wrk_link *link, size_t count) {
37
+ if (count >= link->in_length) {
38
+ link->in_length = 0;
39
+ return;
40
+ }
41
+ memmove(link->in, link->in + count, link->in_length - count);
42
+ link->in_length -= count;
43
+ }
44
+
45
+ static void xt_wrk_out_append(xt_wrk_link *link, const char *data, size_t length) {
46
+ if (link->out_length + length + 1 > link->out_capacity) {
47
+ size_t next = link->out_capacity ? link->out_capacity : 256;
48
+ while (next < link->out_length + length + 1) next *= 2;
49
+ char *grown = (char *)realloc(link->out, next);
50
+ if (!grown) abort();
51
+ link->out = grown;
52
+ link->out_capacity = next;
53
+ }
54
+ memcpy(link->out + link->out_length, data, length);
55
+ link->out_length += length;
56
+ }
57
+
58
+ static void xt_wrk_watch_update(xt_wrk_link *link) {
59
+ if (!link->listening || link->fd < 0) return;
60
+ int events = XT_IO_READ;
61
+ if (link->out_length > 0) events |= XT_IO_WRITE;
62
+ xt_loop_update(link->fd, events);
63
+ }
64
+
65
+ void xt_wrk_flush(xt_wrk_link *link) {
66
+ if (!link->connected || link->fd < 0 || link->handshaking) return;
67
+ while (link->out_length > 0) {
68
+ long sent = xt_wrk_send_some(link->fd, link->out, link->out_length);
69
+ if (sent < 0) {
70
+ if (xt_wrk_would_block()) break;
71
+ xt_wrk_link_close(link);
72
+ return;
73
+ }
74
+ if (sent == 0) break;
75
+ memmove(link->out, link->out + sent, link->out_length - (size_t)sent);
76
+ link->out_length -= (size_t)sent;
77
+ }
78
+ xt_wrk_watch_update(link);
79
+ }
80
+
81
+ void xt_wrk_queue_frame(xt_wrk_link *link, const char *payload, size_t length) {
82
+ char header[4];
83
+ header[0] = (char)(length & 0xff);
84
+ header[1] = (char)((length >> 8) & 0xff);
85
+ header[2] = (char)((length >> 16) & 0xff);
86
+ header[3] = (char)((length >> 24) & 0xff);
87
+ xt_wrk_out_append(link, header, 4);
88
+ xt_wrk_out_append(link, payload, length);
89
+ xt_wrk_flush(link);
90
+ }
91
+
92
+ void xt_wrk_consume_frames(xt_wrk_link *link) {
93
+ while (link->in_length >= 4) {
94
+ uint32_t length = (uint32_t)(unsigned char)link->in[0] |
95
+ ((uint32_t)(unsigned char)link->in[1] << 8) |
96
+ ((uint32_t)(unsigned char)link->in[2] << 16) |
97
+ ((uint32_t)(unsigned char)link->in[3] << 24);
98
+ if (length > XT_WRK_MAX_FRAME) {
99
+ xt_wrk_link_close(link);
100
+ return;
101
+ }
102
+ if (link->in_length < 4 + (size_t)length) return;
103
+ void *frame = xt_try_enter();
104
+ if (xt_try_setjmp(frame) == 0) {
105
+ xt_value message = xt_wrk_decode(link->in + 4, length);
106
+ xt_try_leave(frame);
107
+ xt_wrk_emit1(link->target, "message", message);
108
+ } else {
109
+ xt_value error = xt_try_exception(frame);
110
+ xt_try_leave(frame);
111
+ xt_wrk_emit1(link->target, "messageerror", error);
112
+ }
113
+ xt_wrk_in_consume(link, 4 + (size_t)length);
114
+ }
115
+ }
116
+
117
+ void xt_wrk_link_close(xt_wrk_link *link) {
118
+ /* A worker that replies and then closes (or `parentPort.close()`) may still
119
+ * have a small frame queued; give it a moment to leave the socket before the
120
+ * descriptor is closed, or the parent would lose the last message. */
121
+ int rounds = 0;
122
+ while (link->connected && link->fd >= 0 && link->out_length > 0 && rounds < 20) {
123
+ long sent = xt_wrk_send_some(link->fd, link->out, link->out_length);
124
+ if (sent > 0) {
125
+ memmove(link->out, link->out + sent, link->out_length - (size_t)sent);
126
+ link->out_length -= (size_t)sent;
127
+ continue;
128
+ }
129
+ if (sent < 0 && xt_wrk_would_block() && xt_wrk_wait_writable(link->fd, 50)) {
130
+ rounds++;
131
+ continue;
132
+ }
133
+ break;
134
+ }
135
+ if (link->listening && link->fd >= 0) {
136
+ xt_loop_remove(link->fd);
137
+ link->listening = 0;
138
+ }
139
+ if (link->fd >= 0) {
140
+ xt_wrk_close_fd(link->fd);
141
+ link->fd = -1;
142
+ }
143
+ if (link->listen_fd >= 0) {
144
+ xt_loop_remove(link->listen_fd);
145
+ xt_wrk_close_fd(link->listen_fd);
146
+ link->listen_fd = -1;
147
+ }
148
+ if (link->waiter_box) {
149
+ free(link->waiter_box);
150
+ link->waiter_box = NULL;
151
+ }
152
+ link->connected = 0;
153
+ }
154
+
155
+ /* -- inbound frames ------------------------------------------------------- */
156
+
157
+ /* Read everything the peer has written so far. Returns 1 once the socket
158
+ * reached EOF or failed, 0 when it is merely idle (would block). */
159
+ static int xt_wrk_read_inbound(xt_wrk_link *link) {
160
+ char chunk[4096];
161
+ for (;;) {
162
+ long got = xt_wrk_recv_some(link->fd, chunk, sizeof(chunk));
163
+ if (got > 0) {
164
+ xt_wrk_in_append(link, chunk, (size_t)got);
165
+ continue;
166
+ }
167
+ return got == -2 ? 0 : 1;
168
+ }
169
+ }
170
+
171
+ /* Authenticate the child's first bytes. Returns 1 when frames may be parsed,
172
+ * 0 while the token is still incomplete or after a handshake failure. */
173
+ static int xt_wrk_verify_handshake(xt_wrk_link *link) {
174
+ if (!link->handshaking) return 1;
175
+ if (link->in_length < link->token_length) return 0;
176
+ if (memcmp(link->in, link->token, link->token_length) != 0) {
177
+ xt_wrk_emit1(link->target, "error",
178
+ xt_error_from_message("Error: worker handshake failed"));
179
+ xt_wrk_link_close(link);
180
+ return 0;
181
+ }
182
+ xt_wrk_in_consume(link, link->token_length);
183
+ link->handshaking = 0;
184
+ return 1;
185
+ }
186
+
187
+ /* -- parent transport ----------------------------------------------------- */
188
+
189
+ static void xt_wrk_arm_poll(void);
190
+
191
+ /*
192
+ * A worker may post its last message and exit immediately. By then the exit
193
+ * poll can run before the socket watcher delivered that frame — or even before
194
+ * the pending connection was accepted. Accept and read whatever the child wrote
195
+ * before the link is torn down, so `exit` never overtakes a message that was
196
+ * already sent.
197
+ */
198
+ static void xt_wrk_drain_inbound(xt_wrk_link *link) {
199
+ if (!link->connected && link->listen_fd >= 0) {
200
+ int fd = xt_wrk_accept(link->listen_fd);
201
+ if (fd >= 0) {
202
+ xt_loop_remove(link->listen_fd);
203
+ xt_wrk_close_fd(link->listen_fd);
204
+ link->listen_fd = -1;
205
+ link->fd = fd;
206
+ link->connected = 1;
207
+ link->handshaking = 1;
208
+ /* The ordinary accept path reports `online` before the first message;
209
+ * keep that order when the connection is only picked up here. */
210
+ xt_wrk_emit0(link->target, "online");
211
+ }
212
+ }
213
+ if (!link->connected || link->fd < 0) return;
214
+ /* The process has ended, so its data is buffered or gone; a short bounded
215
+ * wait only covers a frame still in flight. */
216
+ for (int round = 0; round < 20; round++) {
217
+ int closed = xt_wrk_read_inbound(link);
218
+ if (link->handshaking && !xt_wrk_verify_handshake(link)) {
219
+ if (closed || link->fd < 0) return;
220
+ }
221
+ if (!link->handshaking) xt_wrk_consume_frames(link);
222
+ if (closed || link->fd < 0) return;
223
+ if (!xt_wrk_wait_readable(link->fd, 50)) return;
224
+ }
225
+ }
226
+
227
+ static void xt_wrk_finish(xt_wrk_link *link) {
228
+ if (link->exited) return;
229
+ link->exited = 1;
230
+ /* Node reports 1 for a terminated worker; SIGKILL (-9) is an implementation
231
+ * detail of the emulation, not something a program should branch on. */
232
+ if (link->terminating) link->exit_code = 1;
233
+ xt_wrk_drain_inbound(link);
234
+ xt_wrk_link_close(link);
235
+ if (link->terminate_promise != XT_UNDEFINED) {
236
+ xt_promise_fulfill(link->terminate_promise, xt_number((double)link->exit_code));
237
+ }
238
+ xt_wrk_emit1(link->target, "exit", xt_number((double)link->exit_code));
239
+ }
240
+
241
+ static void xt_wrk_poll_tick(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
242
+ (void)thisValue;
243
+ (void)env;
244
+ (void)argc;
245
+ (void)argv;
246
+ xt_wrk_poll_timer = 0;
247
+ int live = 0;
248
+ for (xt_wrk_link *link = xt_wrk_parents; link; link = link->next) {
249
+ if (link->exited) continue;
250
+ int code = -1;
251
+ if (xt_wrk_process_exited(link->pid, &code)) {
252
+ link->pid = 0;
253
+ link->exit_code = code;
254
+ xt_wrk_finish(link);
255
+ } else {
256
+ live = 1;
257
+ }
258
+ }
259
+ if (live) xt_wrk_arm_poll();
260
+ }
261
+
262
+ static void xt_wrk_arm_poll(void) {
263
+ if (xt_wrk_poll_timer != 0) return;
264
+ xt_value arguments[2] = {xt_closure_new((void *)xt_wrk_poll_tick, 0, NULL), xt_number(20)};
265
+ xt_value id = xt_set_timeout(2, arguments);
266
+ xt_wrk_poll_timer = (uint64_t)xt_to_number(id);
267
+ }
268
+
269
+ static void xt_wrk_read_ready(void *userdata, int events);
270
+
271
+ static void xt_wrk_start_reading(xt_wrk_link *link) {
272
+ if (link->listening || link->fd < 0) return;
273
+ if (!link->waiter_box) link->waiter_box = xt_wrk_box(link);
274
+ /* While the token is outstanding only read: queued messages must not reach a
275
+ * connection the child has not authenticated yet. */
276
+ int events = XT_IO_READ;
277
+ if (!link->handshaking && link->out_length > 0) events |= XT_IO_WRITE;
278
+ if (xt_loop_add(link->fd, events, xt_wrk_read_ready, link->waiter_box) != 0) {
279
+ return;
280
+ }
281
+ link->listening = 1;
282
+ }
283
+
284
+ static void xt_wrk_accept_ready(void *userdata, int events) {
285
+ (void)events;
286
+ xt_wrk_link *link = xt_wrk_unbox(userdata);
287
+ if (!link || link->listen_fd < 0) return;
288
+ int fd = xt_wrk_accept(link->listen_fd);
289
+ if (fd < 0) return;
290
+ xt_loop_remove(link->listen_fd);
291
+ xt_wrk_close_fd(link->listen_fd);
292
+ link->listen_fd = -1;
293
+ free(link->waiter_box);
294
+ link->waiter_box = NULL;
295
+ link->fd = fd;
296
+ link->connected = 1;
297
+ link->handshaking = 1;
298
+ xt_wrk_start_reading(link);
299
+ xt_wrk_emit0(link->target, "online");
300
+ }
301
+
302
+ static void xt_wrk_read_ready(void *userdata, int events) {
303
+ xt_wrk_link *link = xt_wrk_unbox(userdata);
304
+ if (!link || link->fd < 0) return;
305
+ if (events & XT_IO_READ) {
306
+ int closed = xt_wrk_read_inbound(link);
307
+ if (link->handshaking) {
308
+ if (!xt_wrk_verify_handshake(link)) {
309
+ if (closed) xt_wrk_link_close(link);
310
+ return;
311
+ }
312
+ /* The child is authenticated: release whatever was posted before it
313
+ * connected. */
314
+ xt_wrk_flush(link);
315
+ }
316
+ /* Frames that arrived in the same read as EOF are still messages: parse
317
+ * them before the socket is closed. */
318
+ xt_wrk_consume_frames(link);
319
+ if (closed) {
320
+ xt_wrk_link_close(link);
321
+ return;
322
+ }
323
+ }
324
+ if (events & XT_IO_WRITE) xt_wrk_flush(link);
325
+ }
326
+
327
+ /* -- environment guard ---------------------------------------------------- */
328
+
329
+ typedef struct {
330
+ const char *key;
331
+ char *value;
332
+ int present;
333
+ } xt_wrk_env_entry;
334
+
335
+ static void xt_wrk_env_save(xt_wrk_env_entry *entry, const char *key) {
336
+ entry->key = key;
337
+ const char *value = xt_wrk_env_get(key);
338
+ entry->present = value != NULL;
339
+ entry->value = value ? xt_wrk_duplicate(value) : NULL;
340
+ }
341
+
342
+ static void xt_wrk_env_restore(xt_wrk_env_entry *entry) {
343
+ if (entry->present && entry->value) xt_wrk_env_set(entry->key, entry->value);
344
+ else xt_wrk_env_unset(entry->key);
345
+ free(entry->value);
346
+ entry->value = NULL;
347
+ }
348
+
349
+ xt_wrk_link *xt_wrk_spawn(const char *path, xt_value data, const char *name, int web_mode,
350
+ xt_value target, char **error) {
351
+ *error = NULL;
352
+ xt_wrk_gc_register();
353
+ /* Encode before creating anything that a throw would leak. `workerData` goes
354
+ * through the environment, so an uncloneable value must fail here. */
355
+ size_t data_length = 0;
356
+ char *encoded = xt_wrk_encode_data(data, &data_length);
357
+ int port = 0;
358
+ int listen_fd = xt_wrk_listen_loopback(&port);
359
+ if (listen_fd < 0) {
360
+ free(encoded);
361
+ *error = xt_wrk_duplicate("failed to create the worker control socket");
362
+ return NULL;
363
+ }
364
+
365
+ xt_wrk_link *link = (xt_wrk_link *)calloc(1, sizeof(xt_wrk_link));
366
+ if (!link) abort();
367
+ link->target = target;
368
+ link->terminate_promise = XT_UNDEFINED;
369
+ link->listen_fd = listen_fd;
370
+ link->fd = -1;
371
+ link->web_mode = web_mode;
372
+ link->thread_id = xt_wrk_thread_counter++;
373
+ xt_wrk_make_token(link->token, sizeof(link->token));
374
+ link->token_length = strlen(link->token);
375
+
376
+ xt_wrk_env_entry guard[6];
377
+ xt_wrk_env_save(&guard[0], XT_WRK_ENV_MARKER);
378
+ xt_wrk_env_save(&guard[1], XT_WRK_ENV_PORT);
379
+ xt_wrk_env_save(&guard[2], XT_WRK_ENV_TOKEN);
380
+ xt_wrk_env_save(&guard[3], XT_WRK_ENV_NAME);
381
+ xt_wrk_env_save(&guard[4], XT_WRK_ENV_DATA);
382
+ xt_wrk_env_save(&guard[5], XT_WRK_ENV_MODE);
383
+
384
+ char port_text[16];
385
+ snprintf(port_text, sizeof(port_text), "%d", port);
386
+ xt_wrk_env_set(XT_WRK_ENV_MARKER, "1");
387
+ xt_wrk_env_set(XT_WRK_ENV_PORT, port_text);
388
+ xt_wrk_env_set(XT_WRK_ENV_TOKEN, link->token);
389
+ xt_wrk_env_set(XT_WRK_ENV_NAME, name ? name : "");
390
+ xt_wrk_env_set(XT_WRK_ENV_MODE, web_mode ? "web" : "node");
391
+
392
+ /* Windows caps an environment variable at 32 KiB; report that instead of
393
+ * silently starting a worker with no `workerData`. */
394
+ int data_ok = encoded ? xt_wrk_env_set(XT_WRK_ENV_DATA, encoded) : 1;
395
+ if (!data_ok) {
396
+ for (int i = 5; i >= 0; i--) xt_wrk_env_restore(&guard[i]);
397
+ free(encoded);
398
+ xt_wrk_close_fd(listen_fd);
399
+ free(link);
400
+ *error = xt_wrk_duplicate(
401
+ "workerData is too large for the worker environment (32 KiB limit)");
402
+ return NULL;
403
+ }
404
+
405
+ xt_wrk_pid pid = xt_wrk_spawn_process(path, error);
406
+
407
+ for (int i = 5; i >= 0; i--) xt_wrk_env_restore(&guard[i]);
408
+ free(encoded);
409
+ if (pid <= 0) {
410
+ xt_wrk_close_fd(listen_fd);
411
+ free(link);
412
+ return NULL;
413
+ }
414
+ link->pid = pid;
415
+ link->next = xt_wrk_parents;
416
+ xt_wrk_parents = link;
417
+
418
+ link->waiter_box = xt_wrk_box(link);
419
+ if (xt_loop_add(listen_fd, XT_IO_READ, xt_wrk_accept_ready, link->waiter_box) != 0) {
420
+ xt_wrk_kill_process(pid);
421
+ }
422
+ xt_wrk_arm_poll();
423
+ return link;
424
+ }
425
+
426
+ void xt_wrk_post(xt_wrk_link *link, xt_value value, int32_t argc, xt_value *argv) {
427
+ (void)argc;
428
+ (void)argv; /* transfer lists are accepted but copy, like structured clone */
429
+ if (!link || link->exited) {
430
+ xt_throw(xt_error_from_message("Error: worker is not running"));
431
+ return;
432
+ }
433
+ size_t length = 0;
434
+ char *encoded = xt_wrk_encode(value, &length);
435
+ xt_wrk_queue_frame(link, encoded, length);
436
+ free(encoded);
437
+ }
438
+
439
+ xt_value xt_wrk_terminate_promise(xt_wrk_link *link) {
440
+ if (link->terminate_promise == XT_UNDEFINED) link->terminate_promise = xt_promise_create();
441
+ xt_value promise = link->terminate_promise;
442
+ if (link->exited) {
443
+ xt_promise_fulfill(promise, xt_number((double)link->exit_code));
444
+ } else {
445
+ link->terminating = 1;
446
+ xt_wrk_kill_process(link->pid);
447
+ xt_wrk_arm_poll();
448
+ }
449
+ return promise;
450
+ }
451
+
452
+ void xt_wrk_terminate(xt_wrk_link *link) {
453
+ if (!link || link->exited) return;
454
+ link->terminating = 1;
455
+ xt_wrk_kill_process(link->pid);
456
+ xt_wrk_arm_poll();
457
+ }
458
+
459
+ double xt_wrk_thread_id(xt_wrk_link *link) { return link ? (double)link->thread_id : 0; }
460
+
461
+ int xt_wrk_link_alive(xt_wrk_link *link) { return link && !link->exited; }
462
+
463
+ void xt_wrk_ref(xt_wrk_link *link) { (void)link; }
464
+ void xt_wrk_unref(xt_wrk_link *link) { (void)link; }
@@ -0,0 +1,150 @@
1
+ /*
2
+ * xbintsc runtime — worker wire helpers.
3
+ *
4
+ * Growable text buffers, JSON string escaping and base64, shared by the message
5
+ * codec (`worker_codec.c`). Kept apart from the codec so each file stays small
6
+ * and the byte-level helpers can be tested through the same round trips.
7
+ */
8
+
9
+ #include "worker_internal.h"
10
+
11
+ #include <stdio.h>
12
+ #include <stdlib.h>
13
+ #include <string.h>
14
+
15
+ void xt_wrk_buffer_reserve(xt_wrk_buffer *buffer, size_t extra) {
16
+ if (buffer->length + extra + 1 <= buffer->capacity) return;
17
+ size_t next = buffer->capacity ? buffer->capacity : 128;
18
+ while (next < buffer->length + extra + 1) next *= 2;
19
+ char *grown = (char *)realloc(buffer->data, next);
20
+ if (!grown) abort();
21
+ buffer->data = grown;
22
+ buffer->capacity = next;
23
+ }
24
+
25
+ void xt_wrk_buffer_put(xt_wrk_buffer *buffer, const char *text, size_t length) {
26
+ if (length == 0) return;
27
+ xt_wrk_buffer_reserve(buffer, length);
28
+ memcpy(buffer->data + buffer->length, text, length);
29
+ buffer->length += length;
30
+ buffer->data[buffer->length] = 0;
31
+ }
32
+
33
+ void xt_wrk_buffer_puts(xt_wrk_buffer *buffer, const char *text) {
34
+ xt_wrk_buffer_put(buffer, text, text ? strlen(text) : 0);
35
+ }
36
+
37
+ void xt_wrk_buffer_putc(xt_wrk_buffer *buffer, char value) {
38
+ xt_wrk_buffer_reserve(buffer, 1);
39
+ buffer->data[buffer->length++] = value;
40
+ buffer->data[buffer->length] = 0;
41
+ }
42
+
43
+ void xt_wrk_buffer_put_uint(xt_wrk_buffer *buffer, unsigned long value) {
44
+ char text[32];
45
+ snprintf(text, sizeof(text), "%lu", value);
46
+ xt_wrk_buffer_puts(buffer, text);
47
+ }
48
+
49
+ /* JSON string escaping. Non-ASCII bytes are copied through (the runtime keeps
50
+ * UTF-8/WTF-8 valid text); control characters become `\u00XX`. */
51
+ void xt_wrk_buffer_put_string(xt_wrk_buffer *buffer, const char *text, size_t length) {
52
+ xt_wrk_buffer_putc(buffer, '"');
53
+ for (size_t i = 0; i < length; i++) {
54
+ unsigned char byte = (unsigned char)text[i];
55
+ switch (byte) {
56
+ case '"': xt_wrk_buffer_put(buffer, "\\\"", 2); break;
57
+ case '\\': xt_wrk_buffer_put(buffer, "\\\\", 2); break;
58
+ case '\b': xt_wrk_buffer_put(buffer, "\\b", 2); break;
59
+ case '\f': xt_wrk_buffer_put(buffer, "\\f", 2); break;
60
+ case '\n': xt_wrk_buffer_put(buffer, "\\n", 2); break;
61
+ case '\r': xt_wrk_buffer_put(buffer, "\\r", 2); break;
62
+ case '\t': xt_wrk_buffer_put(buffer, "\\t", 2); break;
63
+ default:
64
+ if (byte < 0x20) {
65
+ char escape[7];
66
+ snprintf(escape, sizeof(escape), "\\u%04x", byte);
67
+ xt_wrk_buffer_puts(buffer, escape);
68
+ } else {
69
+ xt_wrk_buffer_putc(buffer, (char)byte);
70
+ }
71
+ }
72
+ }
73
+ xt_wrk_buffer_putc(buffer, '"');
74
+ }
75
+
76
+ /* -- base64 --------------------------------------------------------------- */
77
+
78
+ static const char xt_wrk_base64_alphabet[] =
79
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
80
+
81
+ void xt_wrk_buffer_put_base64(xt_wrk_buffer *buffer, const uint8_t *data, size_t length) {
82
+ size_t i = 0;
83
+ while (i + 2 < length) {
84
+ uint32_t chunk = ((uint32_t)data[i] << 16) | ((uint32_t)data[i + 1] << 8) | data[i + 2];
85
+ char out[4] = {xt_wrk_base64_alphabet[(chunk >> 18) & 63],
86
+ xt_wrk_base64_alphabet[(chunk >> 12) & 63],
87
+ xt_wrk_base64_alphabet[(chunk >> 6) & 63],
88
+ xt_wrk_base64_alphabet[chunk & 63]};
89
+ xt_wrk_buffer_put(buffer, out, 4);
90
+ i += 3;
91
+ }
92
+ if (length - i == 1) {
93
+ uint32_t chunk = (uint32_t)data[i] << 16;
94
+ char out[4] = {xt_wrk_base64_alphabet[(chunk >> 18) & 63],
95
+ xt_wrk_base64_alphabet[(chunk >> 12) & 63], '=', '='};
96
+ xt_wrk_buffer_put(buffer, out, 4);
97
+ } else if (length - i == 2) {
98
+ uint32_t chunk = ((uint32_t)data[i] << 16) | ((uint32_t)data[i + 1] << 8);
99
+ char out[4] = {xt_wrk_base64_alphabet[(chunk >> 18) & 63],
100
+ xt_wrk_base64_alphabet[(chunk >> 12) & 63],
101
+ xt_wrk_base64_alphabet[(chunk >> 6) & 63], '='};
102
+ xt_wrk_buffer_put(buffer, out, 4);
103
+ }
104
+ }
105
+
106
+ static int xt_wrk_base64_value(char c) {
107
+ if (c >= 'A' && c <= 'Z') return c - 'A';
108
+ if (c >= 'a' && c <= 'z') return c - 'a' + 26;
109
+ if (c >= '0' && c <= '9') return c - '0' + 52;
110
+ if (c == '+') return 62;
111
+ if (c == '/') return 63;
112
+ return -1;
113
+ }
114
+
115
+ uint8_t *xt_wrk_decode_base64(const char *text, size_t length, size_t *out_length) {
116
+ uint8_t *bytes = (uint8_t *)malloc(length / 4 * 3 + 4);
117
+ if (!bytes) abort();
118
+ size_t written = 0;
119
+ uint32_t accumulator = 0;
120
+ int bits = 0;
121
+ for (size_t i = 0; i < length; i++) {
122
+ if (text[i] == '=') break;
123
+ int value = xt_wrk_base64_value(text[i]);
124
+ if (value < 0) continue;
125
+ accumulator = (accumulator << 6) | (uint32_t)value;
126
+ bits += 6;
127
+ if (bits >= 8) {
128
+ bits -= 8;
129
+ bytes[written++] = (uint8_t)((accumulator >> bits) & 0xff);
130
+ }
131
+ }
132
+ *out_length = written;
133
+ return bytes;
134
+ }
135
+
136
+ /* Byte width of a typed-array element, or 0 for an unknown name. */
137
+ int xt_wrk_width_of_typed_array(const char *name) {
138
+ if (!name) return 0;
139
+ if (strcmp(name, "Uint8Array") == 0 || strcmp(name, "Int8Array") == 0 ||
140
+ strcmp(name, "Uint8ClampedArray") == 0) {
141
+ return 1;
142
+ }
143
+ if (strcmp(name, "Uint16Array") == 0 || strcmp(name, "Int16Array") == 0) return 2;
144
+ if (strcmp(name, "Uint32Array") == 0 || strcmp(name, "Int32Array") == 0 ||
145
+ strcmp(name, "Float32Array") == 0) {
146
+ return 4;
147
+ }
148
+ if (strcmp(name, "Float64Array") == 0) return 8;
149
+ return 0;
150
+ }
@@ -524,10 +524,19 @@ enum {
524
524
  XT_INSTANCE_REGEXP = 6,
525
525
  XT_INSTANCE_PROMISE = 7,
526
526
  XT_INSTANCE_WEAK_MAP = 8, XT_INSTANCE_WEAK_SET = 9, XT_INSTANCE_WEAK_REF = 10,
527
+ XT_INSTANCE_READABLE_STREAM = 11,
528
+ XT_INSTANCE_WRITABLE_STREAM = 12,
529
+ XT_INSTANCE_TRANSFORM_STREAM = 13,
530
+ XT_INSTANCE_TEXT_ENCODER_STREAM = 14,
531
+ XT_INSTANCE_TEXT_DECODER_STREAM = 15,
527
532
  XT_INSTANCE_TYPED_ARRAY_FIRST = 16,
528
533
  XT_INSTANCE_TYPED_ARRAY_LAST = 24
529
534
  };
530
535
 
536
+ /* Web Streams live in xt_streams.c; the id namespace is shared with the table
537
+ * in src/codegen/generator/tables.ts. */
538
+ int xt_streams_instance_of(xt_value value, int32_t kind);
539
+
531
540
  static int xt_value_has_kind(xt_value value, uint8_t kind) {
532
541
  return XT_IS_OBJECT(value) && ((xt_object *)XT_GET_PTR(value))->header.kind == kind;
533
542
  }
@@ -548,6 +557,12 @@ xt_value xt_instance_of_builtin(xt_value value, int32_t id) {
548
557
  case XT_INSTANCE_DATE: return xt_bool(xt_value_has_kind(value, XT_OBJECT_KIND_DATE));
549
558
  case XT_INSTANCE_REGEXP: return xt_bool(xt_value_has_kind(value, XT_OBJECT_KIND_REGEXP));
550
559
  case XT_INSTANCE_PROMISE: return xt_bool(xt_value_has_kind(value, XT_OBJECT_KIND_PROMISE));
560
+ case XT_INSTANCE_READABLE_STREAM:
561
+ case XT_INSTANCE_WRITABLE_STREAM:
562
+ case XT_INSTANCE_TRANSFORM_STREAM:
563
+ case XT_INSTANCE_TEXT_ENCODER_STREAM:
564
+ case XT_INSTANCE_TEXT_DECODER_STREAM:
565
+ return xt_bool(xt_streams_instance_of(value, id - XT_INSTANCE_READABLE_STREAM));
551
566
  default:
552
567
  if (id >= XT_INSTANCE_TYPED_ARRAY_FIRST && id <= XT_INSTANCE_TYPED_ARRAY_LAST) {
553
568
  return xt_bool(xt_typed_array_kind_of(value) == id - XT_INSTANCE_TYPED_ARRAY_FIRST);