xbintsc 0.6.28 → 0.7.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.
Files changed (280) hide show
  1. package/dist/src/binder/declarations.d.ts +1 -0
  2. package/dist/src/binder/declarations.js +37 -0
  3. package/dist/src/binder/declarations.js.map +1 -1
  4. package/dist/src/binder/references.js +12 -0
  5. package/dist/src/binder/references.js.map +1 -1
  6. package/dist/src/checker/globals.js +1 -0
  7. package/dist/src/checker/globals.js.map +1 -1
  8. package/dist/src/checker/type-system.d.ts +5 -0
  9. package/dist/src/checker/type-system.js +22 -3
  10. package/dist/src/checker/type-system.js.map +1 -1
  11. package/dist/src/cli/help.d.ts +1 -1
  12. package/dist/src/codegen/generator/calls/access.js +25 -0
  13. package/dist/src/codegen/generator/calls/access.js.map +1 -1
  14. package/dist/src/codegen/generator/calls/invocation.js +13 -0
  15. package/dist/src/codegen/generator/calls/invocation.js.map +1 -1
  16. package/dist/src/codegen/generator/calls/super.js +3 -4
  17. package/dist/src/codegen/generator/calls/super.js.map +1 -1
  18. package/dist/src/codegen/generator/calls/unwrap.d.ts +18 -0
  19. package/dist/src/codegen/generator/calls/unwrap.js +45 -0
  20. package/dist/src/codegen/generator/calls/unwrap.js.map +1 -0
  21. package/dist/src/codegen/generator/expressions/assignment.js +14 -0
  22. package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
  23. package/dist/src/codegen/generator/expressions/module-values.d.ts +6 -0
  24. package/dist/src/codegen/generator/expressions/module-values.js +51 -17
  25. package/dist/src/codegen/generator/expressions/module-values.js.map +1 -1
  26. package/dist/src/codegen/generator/expressions/primary.js +9 -5
  27. package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
  28. package/dist/src/codegen/generator/generator.js +4 -0
  29. package/dist/src/codegen/generator/generator.js.map +1 -1
  30. package/dist/src/codegen/generator/lookup-tables.d.ts +22 -0
  31. package/dist/src/codegen/generator/lookup-tables.js +48 -0
  32. package/dist/src/codegen/generator/lookup-tables.js.map +1 -0
  33. package/dist/src/codegen/generator/module.js +38 -7
  34. package/dist/src/codegen/generator/module.js.map +1 -1
  35. package/dist/src/codegen/generator/state.js +17 -0
  36. package/dist/src/codegen/generator/state.js.map +1 -1
  37. package/dist/src/codegen/generator/tables.d.ts +2 -6
  38. package/dist/src/codegen/generator/tables.js +18 -12
  39. package/dist/src/codegen/generator/tables.js.map +1 -1
  40. package/dist/src/driver/bundler/commonjs.d.ts +9 -2
  41. package/dist/src/driver/bundler/commonjs.js +11 -4
  42. package/dist/src/driver/bundler/commonjs.js.map +1 -1
  43. package/dist/src/driver/bundler/concatenate.d.ts +20 -0
  44. package/dist/src/driver/bundler/concatenate.js +68 -4
  45. package/dist/src/driver/bundler/concatenate.js.map +1 -1
  46. package/dist/src/driver/bundler/deferred.d.ts +29 -1
  47. package/dist/src/driver/bundler/deferred.js +114 -5
  48. package/dist/src/driver/bundler/deferred.js.map +1 -1
  49. package/dist/src/driver/bundler/dynamic-import.d.ts +6 -0
  50. package/dist/src/driver/bundler/dynamic-import.js +59 -3
  51. package/dist/src/driver/bundler/dynamic-import.js.map +1 -1
  52. package/dist/src/driver/bundler/graph.d.ts +13 -0
  53. package/dist/src/driver/bundler/graph.js +2 -2
  54. package/dist/src/driver/bundler/graph.js.map +1 -1
  55. package/dist/src/driver/bundler/merge.js +32 -8
  56. package/dist/src/driver/bundler/merge.js.map +1 -1
  57. package/dist/src/driver/bundler/namespace-object.d.ts +11 -0
  58. package/dist/src/driver/bundler/namespace-object.js +32 -0
  59. package/dist/src/driver/bundler/namespace-object.js.map +1 -1
  60. package/dist/src/driver/bundler/reexport-resolution.js +8 -2
  61. package/dist/src/driver/bundler/reexport-resolution.js.map +1 -1
  62. package/dist/src/driver/bundler/symbols.d.ts +4 -0
  63. package/dist/src/driver/bundler/symbols.js +14 -1
  64. package/dist/src/driver/bundler/symbols.js.map +1 -1
  65. package/dist/src/driver/config.d.ts +1 -1
  66. package/dist/src/driver/options.d.ts +1 -4
  67. package/dist/src/driver/runtime-lib.d.ts +27 -0
  68. package/dist/src/driver/runtime-lib.js +78 -2
  69. package/dist/src/driver/runtime-lib.js.map +1 -1
  70. package/dist/src/driver/runtime-objects.d.ts +1 -1
  71. package/dist/src/driver/runtime-objects.js +24 -5
  72. package/dist/src/driver/runtime-objects.js.map +1 -1
  73. package/dist/src/driver/version.d.ts +1 -1
  74. package/dist/src/driver/version.js +1 -1
  75. package/dist/src/driver/version.js.map +1 -1
  76. package/dist/src/extensions/node/buffer/index.d.ts +5 -4
  77. package/dist/src/extensions/node/buffer/index.js +5 -4
  78. package/dist/src/extensions/node/buffer/index.js.map +1 -1
  79. package/dist/src/extensions/node/events/index.js +8 -1
  80. package/dist/src/extensions/node/events/index.js.map +1 -1
  81. package/dist/src/extensions/node/fs/callbacks.d.ts +9 -0
  82. package/dist/src/extensions/node/fs/callbacks.js +42 -0
  83. package/dist/src/extensions/node/fs/callbacks.js.map +1 -0
  84. package/dist/src/extensions/node/fs/index.d.ts +2 -3
  85. package/dist/src/extensions/node/fs/index.js +11 -4
  86. package/dist/src/extensions/node/fs/index.js.map +1 -1
  87. package/dist/src/extensions/node/process/index.d.ts +10 -3
  88. package/dist/src/extensions/node/process/index.js +30 -5
  89. package/dist/src/extensions/node/process/index.js.map +1 -1
  90. package/dist/src/extensions/registry.d.ts +7 -0
  91. package/dist/src/extensions/registry.js +13 -4
  92. package/dist/src/extensions/registry.js.map +1 -1
  93. package/dist/src/lexer/scanner.d.ts +7 -1
  94. package/dist/src/lexer/scanner.js +15 -0
  95. package/dist/src/lexer/scanner.js.map +1 -1
  96. package/dist/src/parser/context.d.ts +10 -0
  97. package/dist/src/parser/context.js +42 -3
  98. package/dist/src/parser/context.js.map +1 -1
  99. package/dist/src/parser/literals.js +6 -2
  100. package/dist/src/parser/literals.js.map +1 -1
  101. package/dist/tests/binder/binder.test.js +43 -0
  102. package/dist/tests/binder/binder.test.js.map +1 -1
  103. package/dist/tests/codegen/llvm.test.js +15 -0
  104. package/dist/tests/codegen/llvm.test.js.map +1 -1
  105. package/dist/tests/driver/bundler-resolve.test.js +12 -0
  106. package/dist/tests/driver/bundler-resolve.test.js.map +1 -1
  107. package/dist/tests/driver/commonjs-prelude.test.d.ts +11 -0
  108. package/dist/tests/driver/commonjs-prelude.test.js +69 -0
  109. package/dist/tests/driver/commonjs-prelude.test.js.map +1 -0
  110. package/dist/tests/driver/compiler.test.js +42 -1
  111. package/dist/tests/driver/compiler.test.js.map +1 -1
  112. package/dist/tests/driver/html-deferred-cycles.test.d.ts +10 -0
  113. package/dist/tests/driver/html-deferred-cycles.test.js +75 -0
  114. package/dist/tests/driver/html-deferred-cycles.test.js.map +1 -0
  115. package/dist/tests/driver/namespace-type-exports.test.d.ts +9 -0
  116. package/dist/tests/driver/namespace-type-exports.test.js +59 -0
  117. package/dist/tests/driver/namespace-type-exports.test.js.map +1 -0
  118. package/dist/tests/driver/runtime-lib.test.js +49 -1
  119. package/dist/tests/driver/runtime-lib.test.js.map +1 -1
  120. package/dist/tests/e2e/compile-basics.test.js +53 -0
  121. package/dist/tests/e2e/compile-basics.test.js.map +1 -1
  122. package/dist/tests/e2e/compile-deferred-marker.test.d.ts +14 -0
  123. package/dist/tests/e2e/compile-deferred-marker.test.js +68 -0
  124. package/dist/tests/e2e/compile-deferred-marker.test.js.map +1 -0
  125. package/dist/tests/e2e/compile-deferred-reexport.test.d.ts +12 -0
  126. package/dist/tests/e2e/compile-deferred-reexport.test.js +89 -0
  127. package/dist/tests/e2e/compile-deferred-reexport.test.js.map +1 -0
  128. package/dist/tests/e2e/compile-error-module.test.d.ts +9 -0
  129. package/dist/tests/e2e/compile-error-module.test.js +29 -0
  130. package/dist/tests/e2e/compile-error-module.test.js.map +1 -0
  131. package/dist/tests/e2e/compile-gc-promises.test.d.ts +11 -0
  132. package/dist/tests/e2e/compile-gc-promises.test.js +52 -0
  133. package/dist/tests/e2e/compile-gc-promises.test.js.map +1 -0
  134. package/dist/tests/e2e/compile-modules.test.js +40 -0
  135. package/dist/tests/e2e/compile-modules.test.js.map +1 -1
  136. package/dist/tests/e2e/compile-node-fs-callbacks.test.d.ts +6 -0
  137. package/dist/tests/e2e/compile-node-fs-callbacks.test.js +46 -0
  138. package/dist/tests/e2e/compile-node-fs-callbacks.test.js.map +1 -0
  139. package/dist/tests/e2e/compile-node-inheritance.test.d.ts +6 -0
  140. package/dist/tests/e2e/compile-node-inheritance.test.js +48 -0
  141. package/dist/tests/e2e/compile-node-inheritance.test.js.map +1 -0
  142. package/dist/tests/e2e/compile-node-require.test.d.ts +9 -0
  143. package/dist/tests/e2e/compile-node-require.test.js +29 -0
  144. package/dist/tests/e2e/compile-node-require.test.js.map +1 -0
  145. package/dist/tests/e2e/compile-node-stats.test.d.ts +8 -0
  146. package/dist/tests/e2e/compile-node-stats.test.js +26 -0
  147. package/dist/tests/e2e/compile-node-stats.test.js.map +1 -0
  148. package/dist/tests/e2e/compile-proxy.test.d.ts +6 -0
  149. package/dist/tests/e2e/compile-proxy.test.js +66 -0
  150. package/dist/tests/e2e/compile-proxy.test.js.map +1 -0
  151. package/dist/tests/e2e/compile-web-globals.test.d.ts +6 -0
  152. package/dist/tests/e2e/compile-web-globals.test.js +94 -0
  153. package/dist/tests/e2e/compile-web-globals.test.js.map +1 -0
  154. package/dist/tests/e2e/fetch.test.js +1 -1
  155. package/dist/tests/e2e/gui-script-import-order.test.js +24 -0
  156. package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
  157. package/dist/tests/e2e/json-stringify-to-json.test.d.ts +10 -0
  158. package/dist/tests/e2e/json-stringify-to-json.test.js +61 -0
  159. package/dist/tests/e2e/json-stringify-to-json.test.js.map +1 -0
  160. package/dist/tests/e2e/node-process.test.d.ts +7 -0
  161. package/dist/tests/e2e/node-process.test.js +50 -0
  162. package/dist/tests/e2e/node-process.test.js.map +1 -0
  163. package/dist/tests/e2e/runtime-coverage-buffer.test.js +31 -0
  164. package/dist/tests/e2e/runtime-coverage-buffer.test.js.map +1 -1
  165. package/dist/tests/e2e/web-streams.test.js +44 -29
  166. package/dist/tests/e2e/web-streams.test.js.map +1 -1
  167. package/dist/tests/parser/literals.test.js +19 -0
  168. package/dist/tests/parser/literals.test.js.map +1 -1
  169. package/dist/tests/parser/statements.test.js +13 -0
  170. package/dist/tests/parser/statements.test.js.map +1 -1
  171. package/doc/ai/cli.md +1 -0
  172. package/doc/ai/zh-CN/cli.md +1 -0
  173. package/doc/implemented/binder.md +2 -1
  174. package/doc/implemented/checker.md +5 -0
  175. package/doc/implemented/lexer.md +1 -1
  176. package/doc/implemented/modules.md +23 -1
  177. package/doc/implemented/node/buffer.md +18 -5
  178. package/doc/implemented/node/fs.md +22 -1
  179. package/doc/implemented/node/process.md +65 -5
  180. package/doc/implemented/node.md +7 -1
  181. package/doc/implemented/operators.md +3 -1
  182. package/doc/implemented/parser.md +1 -1
  183. package/doc/implemented/runtime-core.md +6 -2
  184. package/doc/implemented/stdlib.md +69 -2
  185. package/doc/unimplemented/classes.md +1 -0
  186. package/doc/unimplemented/modules.md +1 -0
  187. package/doc/unimplemented/stdlib.md +2 -1
  188. package/doc/xbintsc.config.schema.json +5 -0
  189. package/doc/zh-CN/implemented/binder.md +2 -1
  190. package/doc/zh-CN/implemented/checker.md +3 -0
  191. package/doc/zh-CN/implemented/lexer.md +1 -1
  192. package/doc/zh-CN/implemented/modules.md +17 -1
  193. package/doc/zh-CN/implemented/node/buffer.md +3 -1
  194. package/doc/zh-CN/implemented/node/fs.md +19 -1
  195. package/doc/zh-CN/implemented/node/process.md +59 -5
  196. package/doc/zh-CN/implemented/node.md +1 -1
  197. package/doc/zh-CN/implemented/operators.md +2 -1
  198. package/doc/zh-CN/implemented/parser.md +1 -1
  199. package/doc/zh-CN/implemented/runtime-core.md +3 -2
  200. package/doc/zh-CN/implemented/stdlib.md +58 -2
  201. package/doc/zh-CN/unimplemented/classes.md +1 -0
  202. package/doc/zh-CN/unimplemented/modules.md +1 -0
  203. package/doc/zh-CN/unimplemented/stdlib.md +2 -1
  204. package/package.json +7 -7
  205. package/runtime/ext_node/buffer/buffer.c +5 -4
  206. package/runtime/ext_node/buffer/parts/byte-access.inc +115 -11
  207. package/runtime/ext_node/buffer/parts/prototype.inc +4 -1
  208. package/runtime/ext_node/events/events.c +9 -0
  209. package/runtime/ext_node/fs/callbacks.c +138 -0
  210. package/runtime/ext_node/fs/fs_ops.c +28 -15
  211. package/runtime/ext_node/module/module.c +18 -9
  212. package/runtime/ext_node/process/process.c +117 -32
  213. package/runtime/ext_node/process/streams.c +370 -0
  214. package/runtime/ext_node/process/streams.h +20 -0
  215. package/runtime/rt.h +36 -0
  216. package/runtime/rt_internal.h +10 -10
  217. package/runtime/xt_alloc.c +67 -49
  218. package/runtime/xt_async_io.c +3 -0
  219. package/runtime/xt_builtins.c +26 -23
  220. package/runtime/xt_constructors.c +101 -2
  221. package/runtime/xt_containers/access.inc +29 -1
  222. package/runtime/xt_containers/function_prototypes.inc +58 -0
  223. package/runtime/xt_containers/objects.inc +18 -2
  224. package/runtime/xt_containers.c +1 -0
  225. package/runtime/xt_exit.c +34 -0
  226. package/runtime/xt_fetch/headers.inc +1 -0
  227. package/runtime/xt_fetch/response.inc +28 -8
  228. package/runtime/xt_fetch/webapi.inc +82 -0
  229. package/runtime/xt_fetch.c +5 -0
  230. package/runtime/xt_gc_verify.c +135 -0
  231. package/runtime/xt_intl.c +224 -0
  232. package/runtime/xt_io/exception.inc +40 -1
  233. package/runtime/xt_module_values.c +82 -0
  234. package/runtime/xt_proxy.c +140 -0
  235. package/runtime/xt_stdlib/dispatch.inc +98 -9
  236. package/runtime/xt_stdlib2/date.inc +7 -1
  237. package/runtime/xt_stdlib2/json.inc +28 -0
  238. package/runtime/xt_stdlib2/json_stringify.inc +70 -53
  239. package/runtime/xt_web.h +27 -0
  240. package/runtime/xt_web_abort.c +373 -0
  241. package/scripts/build-runtime.ts +8 -0
  242. package/src/binder/declarations.ts +34 -0
  243. package/src/binder/references.ts +12 -0
  244. package/src/checker/globals.ts +1 -0
  245. package/src/checker/type-system.ts +23 -3
  246. package/src/codegen/generator/calls/access.ts +23 -0
  247. package/src/codegen/generator/calls/invocation.ts +12 -0
  248. package/src/codegen/generator/calls/super.ts +5 -4
  249. package/src/codegen/generator/calls/unwrap.ts +47 -0
  250. package/src/codegen/generator/expressions/assignment.ts +15 -0
  251. package/src/codegen/generator/expressions/module-values.ts +57 -19
  252. package/src/codegen/generator/expressions/primary.ts +9 -9
  253. package/src/codegen/generator/generator.ts +4 -0
  254. package/src/codegen/generator/lookup-tables.ts +63 -0
  255. package/src/codegen/generator/module.ts +38 -7
  256. package/src/codegen/generator/state.ts +17 -0
  257. package/src/codegen/generator/tables.ts +18 -12
  258. package/src/driver/bundler/commonjs.ts +12 -5
  259. package/src/driver/bundler/concatenate.ts +84 -7
  260. package/src/driver/bundler/deferred.ts +122 -6
  261. package/src/driver/bundler/dynamic-import.ts +75 -2
  262. package/src/driver/bundler/graph.ts +2 -2
  263. package/src/driver/bundler/merge.ts +31 -5
  264. package/src/driver/bundler/namespace-object.ts +44 -0
  265. package/src/driver/bundler/reexport-resolution.ts +12 -2
  266. package/src/driver/bundler/symbols.ts +17 -0
  267. package/src/driver/config.ts +1 -1
  268. package/src/driver/options.ts +1 -4
  269. package/src/driver/runtime-lib.ts +87 -2
  270. package/src/driver/runtime-objects.ts +24 -5
  271. package/src/driver/version.ts +1 -1
  272. package/src/extensions/node/buffer/index.ts +5 -4
  273. package/src/extensions/node/events/index.ts +8 -1
  274. package/src/extensions/node/fs/callbacks.ts +45 -0
  275. package/src/extensions/node/fs/index.ts +11 -4
  276. package/src/extensions/node/process/index.ts +30 -5
  277. package/src/extensions/registry.ts +21 -4
  278. package/src/lexer/scanner.ts +16 -1
  279. package/src/parser/context.ts +39 -3
  280. package/src/parser/literals.ts +6 -2
@@ -0,0 +1,370 @@
1
+ /*
2
+ * `process.stdin`, `process.stdout` and `process.stderr` for xbintsc.
3
+ *
4
+ * Each stream is an object carrying the base EventEmitter surface from
5
+ * `node_common.h`, so `stdout.on("resize", fn)` and `stdin.on("data", fn)`
6
+ * behave like Node's. The write side is a direct `fputs`/`fflush`; the read side
7
+ * has no portable readiness notification (Windows `select(2)` only understands
8
+ * sockets), so a blocking read runs on the shared async I/O pool and its
9
+ * completion — which executes on the main thread — decodes the bytes and emits
10
+ * the `data` event. Raw mode flips the console/terminal into single-key bytes so
11
+ * a TUI receives escape sequences instead of line-buffered text.
12
+ *
13
+ * The terminal mode is restored by an `atexit` handler: a compiled TUI that
14
+ * exits without calling `setRawMode(false)` (a crash, `process.exit`) must not
15
+ * leave the shell in raw mode.
16
+ */
17
+
18
+ #include "../node_common.h"
19
+ #include "streams.h"
20
+
21
+ #include <stdio.h>
22
+ #include <stdlib.h>
23
+ #include <string.h>
24
+
25
+ #if defined(_WIN32)
26
+ #include <fcntl.h>
27
+ #include <io.h>
28
+ #include <windows.h>
29
+ #define XT_STREAM_FILENO _fileno
30
+ #define XT_STREAM_ISATTY _isatty
31
+ #define XT_STREAM_READ _read
32
+ #if !defined(ENABLE_VIRTUAL_TERMINAL_INPUT)
33
+ #define ENABLE_VIRTUAL_TERMINAL_INPUT 0x0200
34
+ #endif
35
+ #else
36
+ #include <errno.h>
37
+ #include <sys/ioctl.h>
38
+ #include <termios.h>
39
+ #include <unistd.h>
40
+ #define XT_STREAM_FILENO fileno
41
+ #define XT_STREAM_ISATTY isatty
42
+ #define XT_STREAM_READ read
43
+ #endif
44
+
45
+ /** Bytes requested per stdin read; also the largest chunk a `data` event sees. */
46
+ #define XT_STDIN_CHUNK 4096
47
+
48
+ static xt_value xt_stream_stdout = 0;
49
+ static xt_value xt_stream_stderr = 0;
50
+ static xt_value xt_stream_stdin = 0;
51
+
52
+ /* -- terminal geometry ---------------------------------------------------- */
53
+
54
+ static int xt_stream_is_tty(FILE *stream) {
55
+ int fd = XT_STREAM_FILENO(stream);
56
+ return fd >= 0 && XT_STREAM_ISATTY(fd) ? 1 : 0;
57
+ }
58
+
59
+ static int xt_stream_columns(FILE *stream) {
60
+ #if defined(_WIN32)
61
+ (void)stream;
62
+ #endif
63
+ const char *configured = getenv("COLUMNS");
64
+ if (configured) {
65
+ int columns = atoi(configured);
66
+ if (columns > 0) return columns;
67
+ }
68
+ #if defined(_WIN32)
69
+ CONSOLE_SCREEN_BUFFER_INFO info;
70
+ if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info)) {
71
+ return (int)(info.srWindow.Right - info.srWindow.Left + 1);
72
+ }
73
+ #else
74
+ struct winsize size;
75
+ if (ioctl(XT_STREAM_FILENO(stream), TIOCGWINSZ, &size) == 0 && size.ws_col > 0) return (int)size.ws_col;
76
+ #endif
77
+ return 80;
78
+ }
79
+
80
+ static int xt_stream_rows(FILE *stream) {
81
+ #if defined(_WIN32)
82
+ (void)stream;
83
+ #endif
84
+ const char *configured = getenv("LINES");
85
+ if (configured) {
86
+ int rows = atoi(configured);
87
+ if (rows > 0) return rows;
88
+ }
89
+ #if defined(_WIN32)
90
+ CONSOLE_SCREEN_BUFFER_INFO info;
91
+ if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info)) {
92
+ return (int)(info.srWindow.Bottom - info.srWindow.Top + 1);
93
+ }
94
+ #else
95
+ struct winsize size;
96
+ if (ioctl(XT_STREAM_FILENO(stream), TIOCGWINSZ, &size) == 0 && size.ws_row > 0) return (int)size.ws_row;
97
+ #endif
98
+ return 24;
99
+ }
100
+
101
+ /* -- output streams ------------------------------------------------------- */
102
+
103
+ static xt_value xt_stream_write(FILE *stream, int32_t argc, xt_value *argv) {
104
+ if (argc > 0) {
105
+ const char *text = xt_string_data(xt_to_string(argv[0]));
106
+ if (text) fputs(text, stream);
107
+ fflush(stream);
108
+ }
109
+ return xt_bool(1);
110
+ }
111
+
112
+ static xt_value xt_stream_stdout_write(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
113
+ (void)thisValue;
114
+ (void)env;
115
+ return xt_stream_write(stdout, argc, argv);
116
+ }
117
+
118
+ static xt_value xt_stream_stderr_write(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
119
+ (void)thisValue;
120
+ (void)env;
121
+ return xt_stream_write(stderr, argc, argv);
122
+ }
123
+
124
+ /** `end()` / `destroy()`: flushing is immediate, so ending is a no-op. */
125
+ static xt_value xt_stream_noop(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
126
+ (void)env;
127
+ (void)argc;
128
+ (void)argv;
129
+ return thisValue;
130
+ }
131
+
132
+ static xt_value xt_stream_output(FILE *stream, void *write_fn, int fd) {
133
+ xt_value object = xt_object_new();
134
+ xt_node_install_emitter(object);
135
+ xt_node_define_method(object, "write", write_fn);
136
+ xt_node_define_method(object, "end", (void *)xt_stream_noop);
137
+ xt_node_define_method(object, "destroy", (void *)xt_stream_noop);
138
+ xt_node_set(object, "fd", xt_number((double)fd));
139
+ xt_node_set(object, "isTTY", xt_bool(xt_stream_is_tty(stream)));
140
+ xt_node_set(object, "columns", xt_number((double)xt_stream_columns(stream)));
141
+ xt_node_set(object, "rows", xt_number((double)xt_stream_rows(stream)));
142
+ xt_node_set(object, "writable", xt_bool(1));
143
+ return object;
144
+ }
145
+
146
+ xt_value xt_process_stdout_object(void) {
147
+ xt_gc_add_root(&xt_stream_stdout);
148
+ if (!xt_stream_stdout) xt_stream_stdout = xt_stream_output(stdout, (void *)xt_stream_stdout_write, 1);
149
+ return xt_stream_stdout;
150
+ }
151
+
152
+ xt_value xt_process_stderr_object(void) {
153
+ xt_gc_add_root(&xt_stream_stderr);
154
+ if (!xt_stream_stderr) xt_stream_stderr = xt_stream_output(stderr, (void *)xt_stream_stderr_write, 2);
155
+ return xt_stream_stderr;
156
+ }
157
+
158
+ /* -- raw mode ------------------------------------------------------------- */
159
+
160
+ #if defined(_WIN32)
161
+ static DWORD xt_stdin_saved_mode;
162
+ static int xt_stdin_saved = 0;
163
+ #else
164
+ static struct termios xt_stdin_saved_termios;
165
+ static int xt_stdin_saved = 0;
166
+ #endif
167
+ static int xt_stdin_raw = 0;
168
+ static int xt_stream_atexit_installed = 0;
169
+
170
+ static void xt_stdin_apply_raw_mode(int enable);
171
+
172
+ static void xt_stdin_restore_terminal(void) { xt_stdin_apply_raw_mode(0); }
173
+
174
+ #if defined(_WIN32)
175
+ static void xt_stdin_apply_raw_mode(int enable) {
176
+ HANDLE handle = GetStdHandle(STD_INPUT_HANDLE);
177
+ DWORD mode;
178
+ if (handle == INVALID_HANDLE_VALUE || !GetConsoleMode(handle, &mode)) return;
179
+ if (!xt_stdin_saved) {
180
+ xt_stdin_saved_mode = mode;
181
+ xt_stdin_saved = 1;
182
+ }
183
+ if (enable) {
184
+ mode &= ~(DWORD)(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
185
+ mode |= ENABLE_VIRTUAL_TERMINAL_INPUT;
186
+ } else if (xt_stdin_saved) {
187
+ mode = xt_stdin_saved_mode;
188
+ }
189
+ SetConsoleMode(handle, mode);
190
+ xt_stdin_raw = enable;
191
+ }
192
+ #else
193
+ static void xt_stdin_apply_raw_mode(int enable) {
194
+ if (!XT_STREAM_ISATTY(0)) return;
195
+ if (!xt_stdin_saved) {
196
+ if (tcgetattr(0, &xt_stdin_saved_termios) != 0) return;
197
+ xt_stdin_saved = 1;
198
+ }
199
+ if (enable) {
200
+ struct termios raw = xt_stdin_saved_termios;
201
+ raw.c_iflag &= ~(tcflag_t)(IXON | ICRNL | BRKINT | INPCK | ISTRIP);
202
+ raw.c_lflag &= ~(tcflag_t)(ICANON | ECHO | ISIG);
203
+ raw.c_cc[VMIN] = 1;
204
+ raw.c_cc[VTIME] = 0;
205
+ tcsetattr(0, TCSAFLUSH, &raw);
206
+ } else {
207
+ tcsetattr(0, TCSAFLUSH, &xt_stdin_saved_termios);
208
+ }
209
+ xt_stdin_raw = enable;
210
+ }
211
+ #endif
212
+
213
+ /* -- stdin reader --------------------------------------------------------- */
214
+
215
+ typedef struct {
216
+ xt_async_job job;
217
+ char *buffer;
218
+ int32_t length;
219
+ int eof;
220
+ } xt_stdin_job;
221
+
222
+ static int xt_stdin_active = 0;
223
+ static int xt_stdin_in_flight = 0;
224
+ static int xt_stdin_eof = 0;
225
+
226
+ static void xt_stdin_job_release(xt_async_job *job) {
227
+ free(((xt_stdin_job *)job)->buffer);
228
+ free(job);
229
+ }
230
+
231
+ /* Worker thread: only plain memory and a blocking read. */
232
+ static void xt_stdin_job_run(xt_async_job *job) {
233
+ xt_stdin_job *req = (xt_stdin_job *)job;
234
+ int32_t received = 0;
235
+ for (;;) {
236
+ int got = (int)XT_STREAM_READ(0, req->buffer, (unsigned)XT_STDIN_CHUNK);
237
+ if (got > 0) {
238
+ received = (int32_t)got;
239
+ break;
240
+ }
241
+ #if !defined(_WIN32)
242
+ if (got < 0 && errno == EINTR) continue;
243
+ #endif
244
+ received = 0;
245
+ break;
246
+ }
247
+ req->length = received;
248
+ req->eof = received <= 0;
249
+ }
250
+
251
+ static void xt_stdin_submit(void);
252
+
253
+ /* Main thread: turn the bytes into a `data` event and queue the next read. */
254
+ static void xt_stdin_job_complete(xt_async_job *job) {
255
+ xt_stdin_job *req = (xt_stdin_job *)job;
256
+ xt_stdin_in_flight = 0;
257
+ xt_value stream = xt_process_stdin_object();
258
+ if (req->length > 0) {
259
+ xt_value chunk = xt_string_new(req->buffer, (size_t)req->length);
260
+ xt_node_emit(stream, "data", 1, &chunk);
261
+ }
262
+ if (req->eof) {
263
+ xt_stdin_eof = 1;
264
+ xt_stdin_active = 0;
265
+ xt_node_emit0(stream, "end");
266
+ xt_node_emit0(stream, "close");
267
+ return;
268
+ }
269
+ if (xt_stdin_active) xt_stdin_submit();
270
+ }
271
+
272
+ static void xt_stdin_submit(void) {
273
+ if (xt_stdin_in_flight || xt_stdin_eof) return;
274
+ xt_stdin_job *req = (xt_stdin_job *)calloc(1, sizeof(xt_stdin_job));
275
+ if (!req) return;
276
+ req->buffer = (char *)malloc(XT_STDIN_CHUNK);
277
+ if (!req->buffer) {
278
+ free(req);
279
+ return;
280
+ }
281
+ req->job.run = xt_stdin_job_run;
282
+ req->job.complete = xt_stdin_job_complete;
283
+ req->job.release = xt_stdin_job_release;
284
+ /* Keep the stream (and therefore its listeners) alive while the read runs. */
285
+ req->job.roots[0] = xt_process_stdin_object();
286
+ xt_stdin_in_flight = 1;
287
+ xt_async_submit(&req->job);
288
+ }
289
+
290
+ /* -- stdin methods -------------------------------------------------------- */
291
+
292
+ static xt_value xt_stdin_resume(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
293
+ (void)env;
294
+ (void)argc;
295
+ (void)argv;
296
+ xt_stdin_active = 1;
297
+ xt_stdin_submit();
298
+ return thisValue;
299
+ }
300
+
301
+ static xt_value xt_stdin_pause(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
302
+ (void)env;
303
+ (void)argc;
304
+ (void)argv;
305
+ xt_stdin_active = 0;
306
+ return thisValue;
307
+ }
308
+
309
+ static xt_value xt_stdin_set_encoding(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
310
+ (void)env;
311
+ xt_value encoding = xt_arg(argc, argv, 0);
312
+ xt_node_set(thisValue, "encoding", XT_IS_STRING(encoding) ? encoding : xt_string_from_cstr("utf8"));
313
+ return thisValue;
314
+ }
315
+
316
+ static xt_value xt_stdin_set_raw_mode(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
317
+ (void)env;
318
+ int enable = argc > 0 && xt_truthy(argv[0]);
319
+ if (!xt_stream_atexit_installed) {
320
+ xt_stream_atexit_installed = 1;
321
+ atexit(xt_stdin_restore_terminal);
322
+ }
323
+ xt_stdin_apply_raw_mode(enable);
324
+ xt_node_set(thisValue, "isRaw", xt_bool(xt_stdin_raw));
325
+ return thisValue;
326
+ }
327
+
328
+ /* `stdin.on("data", ...)` switches the stream to flowing mode, like Node. */
329
+ static xt_value xt_stdin_on(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
330
+ xt_value result = xt_node_on_impl(thisValue, env, argc, argv);
331
+ const char *event = xt_node_cstr(xt_arg(argc, argv, 0));
332
+ if (event && strcmp(event, "data") == 0) {
333
+ xt_stdin_active = 1;
334
+ xt_stdin_submit();
335
+ }
336
+ return result;
337
+ }
338
+
339
+ static xt_value xt_stdin_destroy(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
340
+ (void)env;
341
+ (void)argc;
342
+ (void)argv;
343
+ xt_stdin_active = 0;
344
+ return thisValue;
345
+ }
346
+
347
+ xt_value xt_process_stdin_object(void) {
348
+ xt_gc_add_root(&xt_stream_stdin);
349
+ if (xt_stream_stdin) return xt_stream_stdin;
350
+ #if defined(_WIN32)
351
+ /* Raw reads must not be CRLF-translated by the CRT. */
352
+ _setmode(_fileno(stdin), _O_BINARY);
353
+ #endif
354
+ xt_value object = xt_object_new();
355
+ xt_node_install_emitter(object);
356
+ xt_node_define_method(object, "on", (void *)xt_stdin_on);
357
+ xt_node_define_method(object, "addListener", (void *)xt_stdin_on);
358
+ xt_node_define_method(object, "resume", (void *)xt_stdin_resume);
359
+ xt_node_define_method(object, "pause", (void *)xt_stdin_pause);
360
+ xt_node_define_method(object, "setEncoding", (void *)xt_stdin_set_encoding);
361
+ xt_node_define_method(object, "setRawMode", (void *)xt_stdin_set_raw_mode);
362
+ xt_node_define_method(object, "destroy", (void *)xt_stdin_destroy);
363
+ xt_node_set(object, "fd", xt_number(0));
364
+ xt_node_set(object, "isTTY", xt_bool(xt_stream_is_tty(stdin)));
365
+ xt_node_set(object, "isRaw", xt_bool(0));
366
+ xt_node_set(object, "readable", xt_bool(1));
367
+ xt_node_set(object, "encoding", xt_string_from_cstr("utf8"));
368
+ xt_stream_stdin = object;
369
+ return xt_stream_stdin;
370
+ }
@@ -0,0 +1,20 @@
1
+ /*
2
+ * The `process.stdin` / `process.stdout` / `process.stderr` singletons.
3
+ *
4
+ * Declared separately from `process.c` so the module stays within the
5
+ * repository's file-size budget; both translation units are compiled by
6
+ * `src/extensions/node/process/index.ts`.
7
+ */
8
+ #ifndef XT_PROCESS_STREAMS_H
9
+ #define XT_PROCESS_STREAMS_H
10
+
11
+ #include "rt.h"
12
+
13
+ /** The cached `process.stdout` object (an EventEmitter with `write`). */
14
+ xt_value xt_process_stdout_object(void);
15
+ /** The cached `process.stderr` object. */
16
+ xt_value xt_process_stderr_object(void);
17
+ /** The cached `process.stdin` object (an EventEmitter feeding "data" chunks). */
18
+ xt_value xt_process_stdin_object(void);
19
+
20
+ #endif /* XT_PROCESS_STREAMS_H */
package/runtime/rt.h CHANGED
@@ -129,6 +129,8 @@ typedef xt_value (*xt_code_fn)(xt_value thisValue, xt_value env, int32_t argc, x
129
129
 
130
130
  xt_value xt_arg(int32_t argc, xt_value *argv, int32_t index);
131
131
  xt_value xt_closure_new(void *fn, int32_t env_count, xt_value *env);
132
+ /** `import(<expr>)`: resolve a specifier against the linked module table. */
133
+ xt_value xt_dynamic_import(xt_value table, xt_value specifier);
132
134
  xt_value xt_closure_call(xt_value fn, int32_t argc, xt_value *argv);
133
135
  xt_value xt_call_with_this(xt_value fn, xt_value thisValue, int32_t argc, xt_value *argv);
134
136
  xt_value xt_closure_env(xt_value fn, int32_t index);
@@ -136,6 +138,13 @@ int32_t xt_closure_arity(xt_value fn);
136
138
  xt_value xt_function_set_metadata(xt_value fn, xt_value name, int32_t arity);
137
139
  xt_value xt_this(void);
138
140
  xt_value xt_new(xt_value ctor, int32_t argc, xt_value *argv);
141
+ /** The global object value (`globalThis` / Node's `global`), a plain bag. */
142
+ xt_value xt_global_object(void);
143
+ /** The prototype a class chains onto for its `extends` clause. */
144
+ xt_value xt_parent_prototype(xt_value ctor);
145
+ /** `super(...)` from a derived constructor: see `xt_constructors.c`. */
146
+ xt_value xt_super_construct(xt_value thisValue, xt_value owner, xt_value parentProto,
147
+ int32_t argc, xt_value *argv);
139
148
  xt_value xt_instance_of(xt_value value, xt_value ctor);
140
149
  /** `value instanceof <built-in constructor>`; id from `BUILTIN_INSTANCEOF`. */
141
150
  xt_value xt_instance_of_builtin(xt_value value, int32_t id);
@@ -215,6 +224,16 @@ xt_value xt_object_set_hooks(xt_value obj, const xt_object_hooks *hooks, void *d
215
224
  xt_value xt_object_prototype(void);
216
225
  /** The shared `Function.prototype` object. */
217
226
  xt_value xt_function_prototype(void);
227
+ /** A function value's own `.prototype` object, created (with `constructor`) on
228
+ * first use so `Foo.prototype.bar = ...` and `new Foo()` agree. */
229
+ xt_value xt_function_prototype_object(xt_value value);
230
+ /**
231
+ * Install the built-in methods of `kind` (a constructor name such as "Map") on
232
+ * `proto` as unbound first-class closures, so `Map.prototype.get.bind(m)` works.
233
+ */
234
+ void xt_install_proto_methods(xt_value proto, const char *kind);
235
+ /** `Function.prototype[Symbol.hasInstance]` (cached). */
236
+ xt_value xt_function_has_instance_method(void);
218
237
  xt_value xt_object_get(xt_value obj, xt_value key);
219
238
  xt_value xt_object_get_prototype(xt_value obj);
220
239
  xt_value xt_object_set_prototype(xt_value obj, xt_value proto);
@@ -410,6 +429,23 @@ int xt_loop_poll(int timeout_ms);
410
429
  typedef void (*xt_main_loop_fn)(void);
411
430
  void xt_loop_set_main(xt_main_loop_fn fn);
412
431
 
432
+ /* -- program termination --------------------------------------------------- */
433
+ /*
434
+ * The generated `main` ends with `xt_finish()`, which runs the registered exit
435
+ * hook once (Node's `process` "exit" listeners) and yields the status the
436
+ * program asked for, so `process.exitCode` and `process.exit(n)` reach the
437
+ * shell. Both are core runtime entry points; a program that never touches them
438
+ * links the same code and exits 0.
439
+ */
440
+ typedef void (*xt_exit_hook)(void);
441
+ /** Register the hook `xt_finish` runs once; replaces any previous hook. */
442
+ void xt_at_exit(xt_exit_hook hook);
443
+ /** Run the exit hook (at most once) and return the program's exit status. */
444
+ int xt_finish(void);
445
+ /** Status reported by `xt_finish`; `process.exitCode = n` sets it. */
446
+ void xt_set_exit_status(int status);
447
+ int xt_get_exit_status(void);
448
+
413
449
  /* -- timers --------------------------------------------------------------- */
414
450
  /* The JS timer globals, implemented on top of the reactor above. Each returns
415
451
  * a numeric id (or `undefined` for the `clear*` pair); callbacks are ordinary
@@ -38,11 +38,9 @@
38
38
  #define XT_RT_INTERNAL_H
39
39
 
40
40
  #include "rt.h"
41
-
42
41
  #include <setjmp.h>
43
42
  #include <stddef.h>
44
43
  #include <stdint.h>
45
-
46
44
  /* Allocation kinds recorded in `xt_header.kind`. */
47
45
  #define XT_OBJECT_KIND_STRING 1
48
46
  #define XT_OBJECT_KIND_OBJECT 2
@@ -212,21 +210,23 @@ void xt_gc_arm(void);
212
210
  void xt_gc_collect(void);
213
211
  /* Override the automatic trigger threshold (bytes of live heap). */
214
212
  void xt_gc_set_threshold(size_t bytes);
215
-
213
+ extern _Thread_local int xt_gc_worker_thread;
214
+ void xt_gc_verify_payloads(void); /* XT_GC_VERIFY=1 */
215
+ void xt_gc_debug_record_free(uintptr_t address, size_t size, int kind);
216
+ int xt_gc_debug_describe(uintptr_t address, char *out, size_t capacity);
217
+ void xt_gc_verify_rejected(uintptr_t address); /* XT_GC_VERIFY */
216
218
  /* Explicit roots: the address of a slot that always holds a live value (a
217
219
  * module global, a runtime prototype cache, ...). */
218
220
  void xt_gc_add_root(xt_value *slot);
219
- void xt_gc_add_root_range(xt_value *base, size_t count);
220
221
  /* Subsystems whose live set is not reachable through value slots (the event
221
222
  * loop's timers, the promise microtask queue, ...) register a provider that
222
223
  * marks its reachable objects. Providers run during every collection. */
223
224
  void xt_gc_register_root_provider(void (*provider)(void));
224
225
 
225
- /* Track the active C stack so the conservative scan has an upper bound. The
226
- * initial region is installed by `xt_gc_init`; each generator pushes its own
227
- * private stack while it runs. Before switching to a deeper region the caller
228
- * records where its own stack was suspended, so every active region is scanned
229
- * and suspended callers stay reachable. */
226
+ /* Track the active C stack so the conservative scan has an upper bound. A
227
+ * generator runs on its own private stack; before switching to a deeper
228
+ * region the caller records where its stack was suspended, so every active
229
+ * region is scanned and suspended callers stay reachable. */
230
230
  void xt_gc_push_stack(uintptr_t low, uintptr_t high);
231
231
  /* Mark the currently-executing region as suspended at `sp` (its frames above
232
232
  * `sp` stay live while a deeper generator region runs). */
@@ -595,6 +595,6 @@ uint32_t xt_array_buffer_byte_length(xt_value value);
595
595
  uint8_t xt_array_buffer_get_byte(xt_value buffer, uint32_t index);
596
596
  void xt_array_buffer_set_byte(xt_value buffer, uint32_t index, uint8_t value);
597
597
  xt_value xt_array_buffer_ctor(int32_t argc, xt_value *argv);
598
+ #include "xt_web.h"
598
599
  #include "xt_async_io.h"
599
-
600
600
  #endif /* XT_RT_INTERNAL_H */