xbintsc 0.6.24 → 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 (472) hide show
  1. package/AGENTS.md +1 -1
  2. package/dist/src/binder/declarations.d.ts +1 -0
  3. package/dist/src/binder/declarations.js +37 -0
  4. package/dist/src/binder/declarations.js.map +1 -1
  5. package/dist/src/binder/references.js +12 -0
  6. package/dist/src/binder/references.js.map +1 -1
  7. package/dist/src/checker/globals.js +1 -0
  8. package/dist/src/checker/globals.js.map +1 -1
  9. package/dist/src/checker/type-system.d.ts +5 -0
  10. package/dist/src/checker/type-system.js +22 -3
  11. package/dist/src/checker/type-system.js.map +1 -1
  12. package/dist/src/cli/extensions.js +11 -3
  13. package/dist/src/cli/extensions.js.map +1 -1
  14. package/dist/src/cli/help.d.ts +1 -1
  15. package/dist/src/cli/help.js +1 -1
  16. package/dist/src/codegen/generator/calls/access.js +25 -0
  17. package/dist/src/codegen/generator/calls/access.js.map +1 -1
  18. package/dist/src/codegen/generator/calls/invocation.js +13 -0
  19. package/dist/src/codegen/generator/calls/invocation.js.map +1 -1
  20. package/dist/src/codegen/generator/calls/super.js +3 -4
  21. package/dist/src/codegen/generator/calls/super.js.map +1 -1
  22. package/dist/src/codegen/generator/calls/unwrap.d.ts +18 -0
  23. package/dist/src/codegen/generator/calls/unwrap.js +45 -0
  24. package/dist/src/codegen/generator/calls/unwrap.js.map +1 -0
  25. package/dist/src/codegen/generator/expressions/assignment.js +14 -0
  26. package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
  27. package/dist/src/codegen/generator/expressions/module-values.d.ts +6 -0
  28. package/dist/src/codegen/generator/expressions/module-values.js +51 -17
  29. package/dist/src/codegen/generator/expressions/module-values.js.map +1 -1
  30. package/dist/src/codegen/generator/expressions/primary.js +9 -5
  31. package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
  32. package/dist/src/codegen/generator/generator.js +4 -0
  33. package/dist/src/codegen/generator/generator.js.map +1 -1
  34. package/dist/src/codegen/generator/lookup-tables.d.ts +22 -0
  35. package/dist/src/codegen/generator/lookup-tables.js +48 -0
  36. package/dist/src/codegen/generator/lookup-tables.js.map +1 -0
  37. package/dist/src/codegen/generator/module.js +38 -7
  38. package/dist/src/codegen/generator/module.js.map +1 -1
  39. package/dist/src/codegen/generator/state.js +24 -0
  40. package/dist/src/codegen/generator/state.js.map +1 -1
  41. package/dist/src/codegen/generator/tables.d.ts +2 -6
  42. package/dist/src/codegen/generator/tables.js +37 -13
  43. package/dist/src/codegen/generator/tables.js.map +1 -1
  44. package/dist/src/driver/bundler/commonjs.d.ts +9 -2
  45. package/dist/src/driver/bundler/commonjs.js +11 -4
  46. package/dist/src/driver/bundler/commonjs.js.map +1 -1
  47. package/dist/src/driver/bundler/concatenate.d.ts +20 -0
  48. package/dist/src/driver/bundler/concatenate.js +68 -4
  49. package/dist/src/driver/bundler/concatenate.js.map +1 -1
  50. package/dist/src/driver/bundler/deferred.d.ts +29 -1
  51. package/dist/src/driver/bundler/deferred.js +114 -5
  52. package/dist/src/driver/bundler/deferred.js.map +1 -1
  53. package/dist/src/driver/bundler/dynamic-import.d.ts +6 -0
  54. package/dist/src/driver/bundler/dynamic-import.js +59 -3
  55. package/dist/src/driver/bundler/dynamic-import.js.map +1 -1
  56. package/dist/src/driver/bundler/graph.d.ts +13 -0
  57. package/dist/src/driver/bundler/graph.js +11 -7
  58. package/dist/src/driver/bundler/graph.js.map +1 -1
  59. package/dist/src/driver/bundler/merge.js +32 -8
  60. package/dist/src/driver/bundler/merge.js.map +1 -1
  61. package/dist/src/driver/bundler/namespace-object.d.ts +11 -0
  62. package/dist/src/driver/bundler/namespace-object.js +32 -0
  63. package/dist/src/driver/bundler/namespace-object.js.map +1 -1
  64. package/dist/src/driver/bundler/reexport-resolution.js +8 -2
  65. package/dist/src/driver/bundler/reexport-resolution.js.map +1 -1
  66. package/dist/src/driver/bundler/symbols.d.ts +4 -0
  67. package/dist/src/driver/bundler/symbols.js +14 -1
  68. package/dist/src/driver/bundler/symbols.js.map +1 -1
  69. package/dist/src/driver/config.d.ts +1 -1
  70. package/dist/src/driver/fingerprint.d.ts +2 -2
  71. package/dist/src/driver/fingerprint.js +3 -3
  72. package/dist/src/driver/fingerprint.js.map +1 -1
  73. package/dist/src/driver/link-features.js +1 -1
  74. package/dist/src/driver/link-features.js.map +1 -1
  75. package/dist/src/driver/options.d.ts +1 -4
  76. package/dist/src/driver/paths.js +8 -4
  77. package/dist/src/driver/paths.js.map +1 -1
  78. package/dist/src/driver/runtime-lib.d.ts +27 -0
  79. package/dist/src/driver/runtime-lib.js +78 -2
  80. package/dist/src/driver/runtime-lib.js.map +1 -1
  81. package/dist/src/driver/runtime-objects.d.ts +1 -1
  82. package/dist/src/driver/runtime-objects.js +37 -6
  83. package/dist/src/driver/runtime-objects.js.map +1 -1
  84. package/dist/src/driver/version.d.ts +1 -1
  85. package/dist/src/driver/version.js +1 -1
  86. package/dist/src/driver/version.js.map +1 -1
  87. package/dist/src/extensions/catalog.js +5 -1
  88. package/dist/src/extensions/catalog.js.map +1 -1
  89. package/dist/src/extensions/gui/index.js +0 -1
  90. package/dist/src/extensions/gui/index.js.map +1 -1
  91. package/dist/src/extensions/link-feature.d.ts +8 -0
  92. package/dist/src/extensions/node/buffer/index.d.ts +5 -4
  93. package/dist/src/extensions/node/buffer/index.js +5 -4
  94. package/dist/src/extensions/node/buffer/index.js.map +1 -1
  95. package/dist/src/extensions/node/events/index.js +8 -1
  96. package/dist/src/extensions/node/events/index.js.map +1 -1
  97. package/dist/src/extensions/node/fs/callbacks.d.ts +9 -0
  98. package/dist/src/extensions/node/fs/callbacks.js +42 -0
  99. package/dist/src/extensions/node/fs/callbacks.js.map +1 -0
  100. package/dist/src/extensions/node/fs/index.d.ts +2 -3
  101. package/dist/src/extensions/node/fs/index.js +11 -4
  102. package/dist/src/extensions/node/fs/index.js.map +1 -1
  103. package/dist/src/extensions/node/process/index.d.ts +10 -3
  104. package/dist/src/extensions/node/process/index.js +30 -5
  105. package/dist/src/extensions/node/process/index.js.map +1 -1
  106. package/dist/src/extensions/node/stream-web/index.d.ts +6 -1
  107. package/dist/src/extensions/node/stream-web/index.js +59 -4
  108. package/dist/src/extensions/node/stream-web/index.js.map +1 -1
  109. package/dist/src/extensions/node/worker_threads/index.d.ts +14 -4
  110. package/dist/src/extensions/node/worker_threads/index.js +24 -11
  111. package/dist/src/extensions/node/worker_threads/index.js.map +1 -1
  112. package/dist/src/extensions/registry.d.ts +16 -1
  113. package/dist/src/extensions/registry.js +17 -5
  114. package/dist/src/extensions/registry.js.map +1 -1
  115. package/dist/src/extensions/wasm-asset.d.ts +16 -0
  116. package/dist/src/extensions/wasm-asset.js +28 -0
  117. package/dist/src/extensions/wasm-asset.js.map +1 -0
  118. package/dist/src/extensions/wasm.d.ts +17 -0
  119. package/dist/src/extensions/wasm.js +68 -0
  120. package/dist/src/extensions/wasm.js.map +1 -0
  121. package/dist/src/extensions/webworker/index.d.ts +24 -0
  122. package/dist/src/extensions/webworker/index.js +54 -0
  123. package/dist/src/extensions/webworker/index.js.map +1 -0
  124. package/dist/src/index.d.ts +1 -0
  125. package/dist/src/index.js +1 -0
  126. package/dist/src/index.js.map +1 -1
  127. package/dist/src/lexer/scanner.d.ts +7 -1
  128. package/dist/src/lexer/scanner.js +15 -0
  129. package/dist/src/lexer/scanner.js.map +1 -1
  130. package/dist/src/parser/context.d.ts +10 -0
  131. package/dist/src/parser/context.js +42 -3
  132. package/dist/src/parser/context.js.map +1 -1
  133. package/dist/src/parser/literals.js +6 -2
  134. package/dist/src/parser/literals.js.map +1 -1
  135. package/dist/tests/binder/binder.test.js +43 -0
  136. package/dist/tests/binder/binder.test.js.map +1 -1
  137. package/dist/tests/codegen/llvm.test.js +15 -0
  138. package/dist/tests/codegen/llvm.test.js.map +1 -1
  139. package/dist/tests/driver/bundler-resolve.test.js +12 -0
  140. package/dist/tests/driver/bundler-resolve.test.js.map +1 -1
  141. package/dist/tests/driver/commonjs-prelude.test.d.ts +11 -0
  142. package/dist/tests/driver/commonjs-prelude.test.js +69 -0
  143. package/dist/tests/driver/commonjs-prelude.test.js.map +1 -0
  144. package/dist/tests/driver/compiler.test.js +42 -1
  145. package/dist/tests/driver/compiler.test.js.map +1 -1
  146. package/dist/tests/driver/html-deferred-cycles.test.d.ts +10 -0
  147. package/dist/tests/driver/html-deferred-cycles.test.js +75 -0
  148. package/dist/tests/driver/html-deferred-cycles.test.js.map +1 -0
  149. package/dist/tests/driver/link-features.test.js +9 -0
  150. package/dist/tests/driver/link-features.test.js.map +1 -1
  151. package/dist/tests/driver/namespace-type-exports.test.d.ts +9 -0
  152. package/dist/tests/driver/namespace-type-exports.test.js +59 -0
  153. package/dist/tests/driver/namespace-type-exports.test.js.map +1 -0
  154. package/dist/tests/driver/runtime-lib.test.js +49 -1
  155. package/dist/tests/driver/runtime-lib.test.js.map +1 -1
  156. package/dist/tests/e2e/async-fs.test.js +44 -0
  157. package/dist/tests/e2e/async-fs.test.js.map +1 -1
  158. package/dist/tests/e2e/compile-basics.test.js +53 -0
  159. package/dist/tests/e2e/compile-basics.test.js.map +1 -1
  160. package/dist/tests/e2e/compile-deferred-marker.test.d.ts +14 -0
  161. package/dist/tests/e2e/compile-deferred-marker.test.js +68 -0
  162. package/dist/tests/e2e/compile-deferred-marker.test.js.map +1 -0
  163. package/dist/tests/e2e/compile-deferred-reexport.test.d.ts +12 -0
  164. package/dist/tests/e2e/compile-deferred-reexport.test.js +89 -0
  165. package/dist/tests/e2e/compile-deferred-reexport.test.js.map +1 -0
  166. package/dist/tests/e2e/compile-error-module.test.d.ts +9 -0
  167. package/dist/tests/e2e/compile-error-module.test.js +29 -0
  168. package/dist/tests/e2e/compile-error-module.test.js.map +1 -0
  169. package/dist/tests/e2e/compile-gc-promises.test.d.ts +11 -0
  170. package/dist/tests/e2e/compile-gc-promises.test.js +52 -0
  171. package/dist/tests/e2e/compile-gc-promises.test.js.map +1 -0
  172. package/dist/tests/e2e/compile-modules.test.js +40 -0
  173. package/dist/tests/e2e/compile-modules.test.js.map +1 -1
  174. package/dist/tests/e2e/compile-node-fs-callbacks.test.d.ts +6 -0
  175. package/dist/tests/e2e/compile-node-fs-callbacks.test.js +46 -0
  176. package/dist/tests/e2e/compile-node-fs-callbacks.test.js.map +1 -0
  177. package/dist/tests/e2e/compile-node-inheritance.test.d.ts +6 -0
  178. package/dist/tests/e2e/compile-node-inheritance.test.js +48 -0
  179. package/dist/tests/e2e/compile-node-inheritance.test.js.map +1 -0
  180. package/dist/tests/e2e/compile-node-require.test.d.ts +9 -0
  181. package/dist/tests/e2e/compile-node-require.test.js +29 -0
  182. package/dist/tests/e2e/compile-node-require.test.js.map +1 -0
  183. package/dist/tests/e2e/compile-node-stats.test.d.ts +8 -0
  184. package/dist/tests/e2e/compile-node-stats.test.js +26 -0
  185. package/dist/tests/e2e/compile-node-stats.test.js.map +1 -0
  186. package/dist/tests/e2e/compile-proxy.test.d.ts +6 -0
  187. package/dist/tests/e2e/compile-proxy.test.js +66 -0
  188. package/dist/tests/e2e/compile-proxy.test.js.map +1 -0
  189. package/dist/tests/e2e/compile-web-globals.test.d.ts +6 -0
  190. package/dist/tests/e2e/compile-web-globals.test.js +94 -0
  191. package/dist/tests/e2e/compile-web-globals.test.js.map +1 -0
  192. package/dist/tests/e2e/fetch.test.js +1 -1
  193. package/dist/tests/e2e/gui-script-import-order.test.js +24 -0
  194. package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
  195. package/dist/tests/e2e/harness.d.ts +6 -0
  196. package/dist/tests/e2e/harness.js +13 -1
  197. package/dist/tests/e2e/harness.js.map +1 -1
  198. package/dist/tests/e2e/json-stringify-to-json.test.d.ts +10 -0
  199. package/dist/tests/e2e/json-stringify-to-json.test.js +61 -0
  200. package/dist/tests/e2e/json-stringify-to-json.test.js.map +1 -0
  201. package/dist/tests/e2e/node-crypto-random.test.d.ts +8 -0
  202. package/dist/tests/e2e/node-crypto-random.test.js +148 -0
  203. package/dist/tests/e2e/node-crypto-random.test.js.map +1 -0
  204. package/dist/tests/e2e/node-modules.test.js +1 -1
  205. package/dist/tests/e2e/node-modules.test.js.map +1 -1
  206. package/dist/tests/e2e/node-process.test.d.ts +7 -0
  207. package/dist/tests/e2e/node-process.test.js +50 -0
  208. package/dist/tests/e2e/node-process.test.js.map +1 -0
  209. package/dist/tests/e2e/runtime-coverage-buffer.test.js +31 -0
  210. package/dist/tests/e2e/runtime-coverage-buffer.test.js.map +1 -1
  211. package/dist/tests/e2e/web-streams.test.d.ts +8 -0
  212. package/dist/tests/e2e/web-streams.test.js +289 -0
  213. package/dist/tests/e2e/web-streams.test.js.map +1 -0
  214. package/dist/tests/e2e/web-worker.test.d.ts +7 -0
  215. package/dist/tests/e2e/web-worker.test.js +74 -0
  216. package/dist/tests/e2e/web-worker.test.js.map +1 -0
  217. package/dist/tests/e2e/webassembly.test.d.ts +8 -0
  218. package/dist/tests/e2e/webassembly.test.js +113 -0
  219. package/dist/tests/e2e/webassembly.test.js.map +1 -0
  220. package/dist/tests/e2e/worker-api.test.d.ts +14 -0
  221. package/dist/tests/e2e/worker-api.test.js +460 -0
  222. package/dist/tests/e2e/worker-api.test.js.map +1 -0
  223. package/dist/tests/e2e/worker-threads.test.d.ts +8 -0
  224. package/dist/tests/e2e/worker-threads.test.js +160 -0
  225. package/dist/tests/e2e/worker-threads.test.js.map +1 -0
  226. package/dist/tests/extensions/wasm.test.d.ts +1 -0
  227. package/dist/tests/extensions/wasm.test.js +53 -0
  228. package/dist/tests/extensions/wasm.test.js.map +1 -0
  229. package/dist/tests/parser/literals.test.js +19 -0
  230. package/dist/tests/parser/literals.test.js.map +1 -1
  231. package/dist/tests/parser/statements.test.js +13 -0
  232. package/dist/tests/parser/statements.test.js.map +1 -1
  233. package/doc/ai/cli.md +2 -1
  234. package/doc/ai/contributing.md +11 -4
  235. package/doc/ai/extensions.md +25 -2
  236. package/doc/ai/language-support.md +14 -2
  237. package/doc/ai/zh-CN/cli.md +2 -1
  238. package/doc/ai/zh-CN/contributing.md +8 -3
  239. package/doc/ai/zh-CN/extensions.md +23 -2
  240. package/doc/ai/zh-CN/language-support.md +13 -2
  241. package/doc/gui/api.md +1 -1
  242. package/doc/implemented/binder.md +2 -1
  243. package/doc/implemented/checker.md +5 -0
  244. package/doc/implemented/cli.md +1 -1
  245. package/doc/implemented/extensions.md +6 -5
  246. package/doc/implemented/lexer.md +1 -1
  247. package/doc/implemented/modules.md +23 -1
  248. package/doc/implemented/node/buffer.md +18 -5
  249. package/doc/implemented/node/fs.md +22 -1
  250. package/doc/implemented/node/process.md +65 -5
  251. package/doc/implemented/node/stream-web.md +31 -0
  252. package/doc/implemented/node/worker-threads.md +88 -5
  253. package/doc/implemented/node.md +9 -2
  254. package/doc/implemented/operators.md +3 -1
  255. package/doc/implemented/parser.md +1 -1
  256. package/doc/implemented/runtime-core.md +6 -2
  257. package/doc/implemented/stdlib.md +69 -2
  258. package/doc/implemented/streams.md +82 -0
  259. package/doc/implemented/wasm.md +80 -18
  260. package/doc/implemented/web-worker.md +84 -0
  261. package/doc/implemented.md +6 -2
  262. package/doc/unimplemented/classes.md +1 -0
  263. package/doc/unimplemented/deviations.md +1 -0
  264. package/doc/unimplemented/modules.md +1 -0
  265. package/doc/unimplemented/node/worker-threads.md +23 -0
  266. package/doc/unimplemented/node.md +1 -0
  267. package/doc/unimplemented/stdlib.md +3 -1
  268. package/doc/unimplemented/wasm.md +21 -9
  269. package/doc/unimplemented.md +1 -1
  270. package/doc/xbintsc.config.schema.json +5 -0
  271. package/doc/zh-CN/gui/api.md +1 -1
  272. package/doc/zh-CN/implemented/binder.md +2 -1
  273. package/doc/zh-CN/implemented/checker.md +3 -0
  274. package/doc/zh-CN/implemented/cli.md +1 -1
  275. package/doc/zh-CN/implemented/extensions.md +7 -6
  276. package/doc/zh-CN/implemented/lexer.md +1 -1
  277. package/doc/zh-CN/implemented/modules.md +17 -1
  278. package/doc/zh-CN/implemented/node/buffer.md +3 -1
  279. package/doc/zh-CN/implemented/node/fs.md +19 -1
  280. package/doc/zh-CN/implemented/node/process.md +59 -5
  281. package/doc/zh-CN/implemented/node/stream-web.md +24 -0
  282. package/doc/zh-CN/implemented/node/worker-threads.md +77 -4
  283. package/doc/zh-CN/implemented/node.md +3 -2
  284. package/doc/zh-CN/implemented/operators.md +2 -1
  285. package/doc/zh-CN/implemented/parser.md +1 -1
  286. package/doc/zh-CN/implemented/runtime-core.md +3 -2
  287. package/doc/zh-CN/implemented/stdlib.md +58 -2
  288. package/doc/zh-CN/implemented/streams.md +60 -0
  289. package/doc/zh-CN/implemented/wasm.md +73 -16
  290. package/doc/zh-CN/implemented/web-worker.md +79 -0
  291. package/doc/zh-CN/implemented.md +6 -2
  292. package/doc/zh-CN/unimplemented/classes.md +1 -0
  293. package/doc/zh-CN/unimplemented/deviations.md +1 -0
  294. package/doc/zh-CN/unimplemented/modules.md +1 -0
  295. package/doc/zh-CN/unimplemented/node/worker-threads.md +20 -0
  296. package/doc/zh-CN/unimplemented/node.md +1 -0
  297. package/doc/zh-CN/unimplemented/stdlib.md +3 -1
  298. package/doc/zh-CN/unimplemented/wasm.md +16 -8
  299. package/doc/zh-CN/unimplemented.md +1 -1
  300. package/llms.txt +2 -0
  301. package/package.json +8 -7
  302. package/runtime/ext_gui/browser.cpp +1 -1
  303. package/runtime/ext_node/buffer/buffer.c +5 -4
  304. package/runtime/ext_node/buffer/parts/byte-access.inc +115 -11
  305. package/runtime/ext_node/buffer/parts/prototype.inc +4 -1
  306. package/runtime/ext_node/events/events.c +9 -0
  307. package/runtime/ext_node/fs/callbacks.c +138 -0
  308. package/runtime/ext_node/fs/fs_ops.c +28 -15
  309. package/runtime/ext_node/module/module.c +18 -9
  310. package/runtime/ext_node/process/process.c +117 -32
  311. package/runtime/ext_node/process/streams.c +370 -0
  312. package/runtime/ext_node/process/streams.h +20 -0
  313. package/runtime/ext_node/stream_web/stream_web.c +20 -6
  314. package/runtime/rt.h +36 -0
  315. package/runtime/rt_internal.h +10 -10
  316. package/runtime/worker/worker_api.c +422 -0
  317. package/runtime/worker/worker_codec.c +489 -0
  318. package/runtime/worker/worker_core.c +445 -0
  319. package/runtime/worker/worker_internal.h +223 -0
  320. package/runtime/worker/worker_platform.c +344 -0
  321. package/runtime/worker/worker_ports.c +203 -0
  322. package/runtime/worker/worker_transport.c +464 -0
  323. package/runtime/worker/worker_wire.c +150 -0
  324. package/runtime/xt_alloc.c +67 -49
  325. package/runtime/xt_async_io.c +3 -0
  326. package/runtime/xt_builtins.c +41 -23
  327. package/runtime/xt_constructors.c +148 -2
  328. package/runtime/xt_containers/access.inc +29 -1
  329. package/runtime/xt_containers/function_prototypes.inc +58 -0
  330. package/runtime/xt_containers/objects.inc +18 -2
  331. package/runtime/xt_containers.c +1 -0
  332. package/runtime/xt_exit.c +34 -0
  333. package/runtime/xt_fetch/headers.inc +1 -0
  334. package/runtime/xt_fetch/response.inc +28 -8
  335. package/runtime/xt_fetch/webapi.inc +82 -0
  336. package/runtime/xt_fetch.c +5 -0
  337. package/runtime/xt_gc_verify.c +135 -0
  338. package/runtime/xt_intl.c +224 -0
  339. package/runtime/xt_io/exception.inc +40 -1
  340. package/runtime/xt_module_values.c +82 -0
  341. package/runtime/xt_proxy.c +140 -0
  342. package/runtime/xt_stdlib/dispatch.inc +98 -9
  343. package/runtime/xt_stdlib2/date.inc +7 -1
  344. package/runtime/xt_stdlib2/error.inc +4 -0
  345. package/runtime/xt_stdlib2/json.inc +28 -0
  346. package/runtime/xt_stdlib2/json_stringify.inc +70 -53
  347. package/runtime/xt_streams/common.inc +232 -0
  348. package/runtime/xt_streams/constructors.inc +232 -0
  349. package/runtime/xt_streams/encoding.inc +124 -0
  350. package/runtime/xt_streams/readable-extra.inc +372 -0
  351. package/runtime/xt_streams/readable.inc +415 -0
  352. package/runtime/xt_streams/transform.inc +195 -0
  353. package/runtime/xt_streams/writable.inc +542 -0
  354. package/runtime/xt_streams.c +30 -0
  355. package/runtime/xt_wasm/wasm.h +148 -0
  356. package/runtime/xt_wasm/wasm_api.c +357 -0
  357. package/runtime/xt_wasm/wasm_api.h +37 -0
  358. package/runtime/xt_wasm/wasm_bridge.c +30 -0
  359. package/runtime/xt_wasm/wasm_instance.c +591 -0
  360. package/runtime/xt_wasm/wasm_module.c +270 -0
  361. package/runtime/xt_wasm/wasm_values.c +177 -0
  362. package/runtime/xt_web.h +27 -0
  363. package/runtime/xt_web_abort.c +373 -0
  364. package/runtime/xt_webassembly.c +15 -4
  365. package/scripts/build-runtime.ts +8 -0
  366. package/scripts/fetch-vendored.ts +187 -20
  367. package/scripts/package-release.ts +3 -2
  368. package/src/binder/declarations.ts +34 -0
  369. package/src/binder/references.ts +12 -0
  370. package/src/checker/globals.ts +1 -0
  371. package/src/checker/type-system.ts +23 -3
  372. package/src/cli/extensions.ts +11 -3
  373. package/src/cli/help.ts +1 -1
  374. package/src/codegen/generator/calls/access.ts +23 -0
  375. package/src/codegen/generator/calls/invocation.ts +12 -0
  376. package/src/codegen/generator/calls/super.ts +5 -4
  377. package/src/codegen/generator/calls/unwrap.ts +47 -0
  378. package/src/codegen/generator/expressions/assignment.ts +15 -0
  379. package/src/codegen/generator/expressions/module-values.ts +57 -19
  380. package/src/codegen/generator/expressions/primary.ts +9 -9
  381. package/src/codegen/generator/generator.ts +4 -0
  382. package/src/codegen/generator/lookup-tables.ts +63 -0
  383. package/src/codegen/generator/module.ts +38 -7
  384. package/src/codegen/generator/state.ts +24 -0
  385. package/src/codegen/generator/tables.ts +37 -13
  386. package/src/driver/bundler/commonjs.ts +12 -5
  387. package/src/driver/bundler/concatenate.ts +84 -7
  388. package/src/driver/bundler/deferred.ts +122 -6
  389. package/src/driver/bundler/dynamic-import.ts +75 -2
  390. package/src/driver/bundler/graph.ts +11 -7
  391. package/src/driver/bundler/merge.ts +31 -5
  392. package/src/driver/bundler/namespace-object.ts +44 -0
  393. package/src/driver/bundler/reexport-resolution.ts +12 -2
  394. package/src/driver/bundler/symbols.ts +17 -0
  395. package/src/driver/config.ts +1 -1
  396. package/src/driver/fingerprint.ts +3 -3
  397. package/src/driver/link-features.ts +1 -1
  398. package/src/driver/options.ts +1 -4
  399. package/src/driver/paths.ts +8 -4
  400. package/src/driver/runtime-lib.ts +87 -2
  401. package/src/driver/runtime-objects.ts +37 -6
  402. package/src/driver/version.ts +1 -1
  403. package/src/extensions/catalog.ts +5 -1
  404. package/src/extensions/gui/index.ts +0 -1
  405. package/src/extensions/link-feature.ts +8 -0
  406. package/src/extensions/node/buffer/index.ts +5 -4
  407. package/src/extensions/node/events/index.ts +8 -1
  408. package/src/extensions/node/fs/callbacks.ts +45 -0
  409. package/src/extensions/node/fs/index.ts +11 -4
  410. package/src/extensions/node/process/index.ts +30 -5
  411. package/src/extensions/node/stream-web/index.ts +59 -4
  412. package/src/extensions/node/worker_threads/index.ts +24 -11
  413. package/src/extensions/registry.ts +34 -6
  414. package/src/extensions/wasm-asset.ts +32 -0
  415. package/src/extensions/wasm.ts +74 -0
  416. package/src/extensions/webworker/index.ts +58 -0
  417. package/src/index.ts +1 -0
  418. package/src/lexer/scanner.ts +16 -1
  419. package/src/parser/context.ts +39 -3
  420. package/src/parser/literals.ts +6 -2
  421. package/vendor/wasm3/.pin +1 -0
  422. package/vendor/wasm3/LICENSE +21 -0
  423. package/vendor/wasm3/m3_api_libc.c +265 -0
  424. package/vendor/wasm3/m3_api_libc.h +19 -0
  425. package/vendor/wasm3/m3_api_meta_wasi.c +1107 -0
  426. package/vendor/wasm3/m3_api_tracer.c +173 -0
  427. package/vendor/wasm3/m3_api_tracer.h +19 -0
  428. package/vendor/wasm3/m3_api_uvwasi.c +1290 -0
  429. package/vendor/wasm3/m3_api_wasi.c +1207 -0
  430. package/vendor/wasm3/m3_api_wasi.h +74 -0
  431. package/vendor/wasm3/m3_api_wasi_host.h +137 -0
  432. package/vendor/wasm3/m3_api_wasi_posix.h +979 -0
  433. package/vendor/wasm3/m3_api_wasi_win32.h +1093 -0
  434. package/vendor/wasm3/m3_atomic.h +114 -0
  435. package/vendor/wasm3/m3_bind.c +174 -0
  436. package/vendor/wasm3/m3_bind.h +20 -0
  437. package/vendor/wasm3/m3_code.c +232 -0
  438. package/vendor/wasm3/m3_code.h +74 -0
  439. package/vendor/wasm3/m3_compile.c +6603 -0
  440. package/vendor/wasm3/m3_compile.h +354 -0
  441. package/vendor/wasm3/m3_config.h +448 -0
  442. package/vendor/wasm3/m3_config_platforms.h +345 -0
  443. package/vendor/wasm3/m3_core.c +1034 -0
  444. package/vendor/wasm3/m3_core.h +526 -0
  445. package/vendor/wasm3/m3_deterministic.c +65 -0
  446. package/vendor/wasm3/m3_deterministic.h +59 -0
  447. package/vendor/wasm3/m3_env.c +3146 -0
  448. package/vendor/wasm3/m3_env.h +748 -0
  449. package/vendor/wasm3/m3_exception.h +33 -0
  450. package/vendor/wasm3/m3_exec.c +8 -0
  451. package/vendor/wasm3/m3_exec.h +4110 -0
  452. package/vendor/wasm3/m3_exec_atomic.h +146 -0
  453. package/vendor/wasm3/m3_exec_defs.h +76 -0
  454. package/vendor/wasm3/m3_function.c +278 -0
  455. package/vendor/wasm3/m3_function.h +244 -0
  456. package/vendor/wasm3/m3_host.h +226 -0
  457. package/vendor/wasm3/m3_host_none.h +76 -0
  458. package/vendor/wasm3/m3_host_posix.h +543 -0
  459. package/vendor/wasm3/m3_host_win32.h +253 -0
  460. package/vendor/wasm3/m3_info.c +616 -0
  461. package/vendor/wasm3/m3_info.h +38 -0
  462. package/vendor/wasm3/m3_math_utils.h +560 -0
  463. package/vendor/wasm3/m3_module.c +431 -0
  464. package/vendor/wasm3/m3_parse.c +1549 -0
  465. package/vendor/wasm3/m3_snapshot.c +3825 -0
  466. package/vendor/wasm3/m3_validate.c +1770 -0
  467. package/vendor/wasm3/m3_validate.h +25 -0
  468. package/vendor/wasm3/m3_wasi.h +10 -0
  469. package/vendor/wasm3/m3_xxh64.c +211 -0
  470. package/vendor/wasm3/wasm3.h +684 -0
  471. package/vendor/wasm3/wasm3_defs.h +350 -0
  472. package/runtime/ext_node/worker_threads/worker_threads.c +0 -214
@@ -0,0 +1,445 @@
1
+ /*
2
+ * xbintsc runtime — worker transport core (shared by both worker APIs).
3
+ *
4
+ * This file owns:
5
+ *
6
+ * - the EventEmitter every worker object uses (`on`/`once`/`off` plus the
7
+ * pending-event buffer that lets a message arrive before the program has
8
+ * attached its handler),
9
+ * - the `workerData` / worker name handoff (environment -> value),
10
+ * - entry-point validation: a compiled binary can only run *itself* as a
11
+ * worker, so `new Worker(url)` accepts the executable's own path,
12
+ * - the worker side of the control channel (`self` / `parentPort`).
13
+ *
14
+ * The parent-side socket machinery lives in `worker_transport.c`, the wire
15
+ * format in `worker_codec.c`, and every OS difference in `worker_platform.c`.
16
+ */
17
+
18
+ #include "worker_internal.h"
19
+
20
+ #include <stdio.h>
21
+ #include <stdlib.h>
22
+ #include <string.h>
23
+
24
+ xt_wrk_link *xt_wrk_parents = NULL;
25
+ xt_wrk_link *xt_wrk_scope_channel = NULL;
26
+ uint64_t xt_wrk_poll_timer = 0;
27
+ int xt_wrk_thread_counter = 1;
28
+
29
+ static xt_value xt_wrk_data_cache = XT_UNDEFINED;
30
+ static int xt_wrk_gc_registered = 0;
31
+ static char *xt_wrk_program_path_cache = NULL;
32
+
33
+ /* -- small helpers -------------------------------------------------------- */
34
+
35
+ static xt_value xt_wrk_string(const char *text) { return xt_string_from_cstr(text); }
36
+
37
+ static void xt_wrk_set(xt_value object, const char *key, xt_value value) {
38
+ xt_object_set(object, xt_wrk_string(key), value);
39
+ }
40
+
41
+ static xt_value xt_wrk_get(xt_value object, const char *key) {
42
+ return xt_object_get_cstr(object, key);
43
+ }
44
+
45
+ /*
46
+ * `xt_loop_add` scans its userdata as a value slot. The transport needs the link
47
+ * pointer, and a raw pointer's tag bits are 0x0000, which no value tag uses, so
48
+ * the collector skips it exactly like the imported-function pointers the
49
+ * compiler stores in closure environments.
50
+ */
51
+ xt_value *xt_wrk_box(xt_wrk_link *link) {
52
+ xt_value *box = (xt_value *)malloc(sizeof(xt_value));
53
+ if (!box) abort();
54
+ *box = (xt_value)(uintptr_t)link;
55
+ return box;
56
+ }
57
+
58
+ xt_wrk_link *xt_wrk_unbox(void *userdata) {
59
+ if (!userdata) return NULL;
60
+ return (xt_wrk_link *)(uintptr_t)(*(xt_value *)userdata);
61
+ }
62
+
63
+ /* -- EventEmitter --------------------------------------------------------- */
64
+
65
+ static xt_value xt_wrk_event_map(xt_value target, int create) {
66
+ xt_value map = xt_wrk_get(target, "__xt_events");
67
+ if (!XT_IS_OBJECT(map) && create) {
68
+ map = xt_object_new();
69
+ xt_wrk_set(target, "__xt_events", map);
70
+ }
71
+ return map;
72
+ }
73
+
74
+ static xt_value xt_wrk_listeners(xt_value target, const char *name, int create) {
75
+ xt_value map = xt_wrk_event_map(target, create);
76
+ if (!XT_IS_OBJECT(map)) return XT_UNDEFINED;
77
+ xt_value key = xt_wrk_string(name);
78
+ xt_value array = xt_object_get(map, key);
79
+ if (!XT_IS_ARRAY(array) && create) {
80
+ array = xt_array_new(0, NULL);
81
+ xt_object_set(map, key, array);
82
+ }
83
+ return array;
84
+ }
85
+
86
+ static xt_value xt_wrk_pending_map(xt_value target, int create) {
87
+ xt_value map = xt_wrk_get(target, "__xt_pending");
88
+ if (!XT_IS_OBJECT(map) && create) {
89
+ map = xt_object_new();
90
+ xt_wrk_set(target, "__xt_pending", map);
91
+ }
92
+ return map;
93
+ }
94
+
95
+ void xt_wrk_emit(xt_value target, const char *name, int32_t argc, xt_value *argv) {
96
+ xt_value listeners = xt_wrk_listeners(target, name, 0);
97
+ int32_t count = XT_IS_ARRAY(listeners) ? xt_array_size(listeners) : 0;
98
+ if (count > 0) {
99
+ /* Copy the list: a listener may add or remove handlers, and a `once`
100
+ * handler removes itself before it runs. */
101
+ xt_value snapshot = xt_array_new(0, NULL);
102
+ for (int32_t i = 0; i < count; i++) xt_array_push(snapshot, xt_array_items(listeners)[i]);
103
+ count = xt_array_size(snapshot);
104
+ for (int32_t i = 0; i < count; i++) {
105
+ xt_value fn = xt_array_items(snapshot)[i];
106
+ if (!XT_IS_FUNCTION(fn)) continue;
107
+ if (xt_truthy(xt_wrk_get(fn, "__xt_once"))) xt_wrk_off(target, name, fn);
108
+ xt_call_with_this(fn, target, argc, argv);
109
+ }
110
+ return;
111
+ }
112
+ /* Buffer until a listener exists, mirroring the node extension's emitter. */
113
+ xt_value map = xt_wrk_pending_map(target, 1);
114
+ xt_value key = xt_wrk_string(name);
115
+ xt_value queue = xt_object_get(map, key);
116
+ if (!XT_IS_ARRAY(queue)) {
117
+ queue = xt_array_new(0, NULL);
118
+ xt_object_set(map, key, queue);
119
+ }
120
+ xt_array_push(queue, xt_array_new(argc, argv));
121
+ }
122
+
123
+ void xt_wrk_emit0(xt_value target, const char *name) { xt_wrk_emit(target, name, 0, NULL); }
124
+
125
+ void xt_wrk_emit1(xt_value target, const char *name, xt_value arg) {
126
+ xt_wrk_emit(target, name, 1, &arg);
127
+ }
128
+
129
+ /* `once` wraps the handler so the emitter can recognise and drop it after the
130
+ * first call without the handler needing a self-reference. */
131
+ static xt_value xt_wrk_once_call(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
132
+ (void)thisValue;
133
+ xt_value handler = xt_closure_env(env, 0);
134
+ return xt_call_with_this(handler, thisValue, argc, argv);
135
+ }
136
+
137
+ static void xt_wrk_flush_pending(xt_value target, const char *name) {
138
+ xt_value map = xt_wrk_pending_map(target, 0);
139
+ if (!XT_IS_OBJECT(map)) return;
140
+ xt_value key = xt_wrk_string(name);
141
+ xt_value queue = xt_object_get(map, key);
142
+ if (!XT_IS_ARRAY(queue) || xt_array_size(queue) == 0) return;
143
+ xt_object_set(map, key, xt_array_new(0, NULL));
144
+ int32_t count = xt_array_size(queue);
145
+ for (int32_t i = 0; i < count; i++) {
146
+ xt_value record = xt_array_items(queue)[i];
147
+ int32_t argc = xt_array_size(record);
148
+ if (argc > 8) argc = 8;
149
+ xt_wrk_emit(target, name, argc, xt_array_items(record));
150
+ }
151
+ }
152
+
153
+ void xt_wrk_on(xt_value target, const char *name, xt_value fn, int once) {
154
+ if (!XT_IS_FUNCTION(fn)) return;
155
+ xt_value listener = fn;
156
+ if (once) {
157
+ xt_value environment[1] = {fn};
158
+ listener = xt_closure_new((void *)xt_wrk_once_call, 1, environment);
159
+ xt_wrk_set(listener, "__xt_once", XT_TRUE);
160
+ }
161
+ xt_value array = xt_wrk_listeners(target, name, 1);
162
+ xt_array_push(array, listener);
163
+ xt_wrk_flush_pending(target, name);
164
+ if (strcmp(name, "message") == 0) xt_wrk_listen_for_target(target);
165
+ }
166
+
167
+ /* A listener is stored either directly or wrapped by `once`; `off` matches
168
+ * both the handler the program passed and the wrapper it was given. */
169
+ static int xt_wrk_listener_matches(xt_value item, xt_value fn) {
170
+ if (item == fn) return 1;
171
+ if (xt_wrk_get(item, "__xt_original") == fn) return 1;
172
+ return xt_truthy(xt_wrk_get(item, "__xt_once")) && xt_closure_env(item, 0) == fn;
173
+ }
174
+
175
+ void xt_wrk_off(xt_value target, const char *name, xt_value fn) {
176
+ xt_value array = xt_wrk_listeners(target, name, 0);
177
+ if (!XT_IS_ARRAY(array)) return;
178
+ xt_value kept = xt_array_new(0, NULL);
179
+ int32_t count = xt_array_size(array);
180
+ for (int32_t i = 0; i < count; i++) {
181
+ xt_value item = xt_array_items(array)[i];
182
+ if (!xt_wrk_listener_matches(item, fn)) xt_array_push(kept, item);
183
+ }
184
+ xt_value map = xt_wrk_event_map(target, 1);
185
+ xt_object_set(map, xt_wrk_string(name), kept);
186
+ }
187
+
188
+ void xt_wrk_remove_all(xt_value target, const char *name) {
189
+ xt_value map = xt_wrk_event_map(target, 1);
190
+ xt_object_set(map, xt_wrk_string(name), xt_array_new(0, NULL));
191
+ }
192
+
193
+ int32_t xt_wrk_listener_count(xt_value target, const char *name) {
194
+ xt_value array = xt_wrk_listeners(target, name, 0);
195
+ return XT_IS_ARRAY(array) ? xt_array_size(array) : 0;
196
+ }
197
+
198
+ /* -- collector roots ------------------------------------------------------ */
199
+
200
+ static void xt_wrk_gc_scan(void) {
201
+ for (xt_wrk_link *link = xt_wrk_parents; link; link = link->next) {
202
+ xt_gc_mark_value(link->target);
203
+ xt_gc_mark_value(link->terminate_promise);
204
+ }
205
+ if (xt_wrk_scope_channel) xt_gc_mark_value(xt_wrk_scope_channel->target);
206
+ xt_gc_mark_value(xt_wrk_data_cache);
207
+ }
208
+
209
+ void xt_wrk_gc_register(void) {
210
+ if (xt_wrk_gc_registered) return;
211
+ xt_wrk_gc_registered = 1;
212
+ xt_gc_register_root_provider(xt_wrk_gc_scan);
213
+ }
214
+
215
+ /* -- environment / worker data -------------------------------------------- */
216
+
217
+ int xt_wrk_is_worker(void) {
218
+ const char *marker = xt_wrk_env_get(XT_WRK_ENV_MARKER);
219
+ return marker != NULL && marker[0] != 0;
220
+ }
221
+
222
+ int xt_wrk_is_main_thread(void) { return !xt_wrk_is_worker(); }
223
+
224
+ const char *xt_wrk_scope_name(void) {
225
+ const char *name = xt_wrk_env_get(XT_WRK_ENV_NAME);
226
+ return name ? name : "";
227
+ }
228
+
229
+ xt_value xt_wrk_worker_data(void) {
230
+ if (!xt_wrk_is_worker()) return XT_UNDEFINED;
231
+ if (xt_wrk_data_cache != XT_UNDEFINED) return xt_wrk_data_cache;
232
+ const char *text = xt_wrk_env_get(XT_WRK_ENV_DATA);
233
+ if (!text || !text[0]) return XT_UNDEFINED;
234
+ xt_wrk_data_cache = xt_wrk_decode(text, strlen(text));
235
+ return xt_wrk_data_cache;
236
+ }
237
+
238
+ xt_value xt_wrk_scope_data(void) { return xt_wrk_worker_data(); }
239
+
240
+ char *xt_wrk_encode_data(xt_value value, size_t *length) {
241
+ if (value == XT_UNDEFINED) {
242
+ *length = 0;
243
+ return NULL;
244
+ }
245
+ return xt_wrk_encode(value, length);
246
+ }
247
+
248
+ /* -- entry validation ----------------------------------------------------- */
249
+
250
+ const char *xt_wrk_program_path(void) {
251
+ if (!xt_wrk_program_path_cache) {
252
+ xt_value value = xt_import_meta(xt_wrk_string("filename"));
253
+ const char *text = XT_IS_STRING(value) ? xt_string_data(value) : "";
254
+ xt_wrk_program_path_cache = xt_wrk_duplicate(text);
255
+ }
256
+ return xt_wrk_program_path_cache;
257
+ }
258
+
259
+ static const char *xt_wrk_basename(const char *path) {
260
+ const char *base = path;
261
+ for (const char *p = path; *p; p++) {
262
+ if (*p == '/' || *p == '\\') base = p + 1;
263
+ }
264
+ return base;
265
+ }
266
+
267
+ static int xt_wrk_name_equals(const char *a, const char *b) {
268
+ #if defined(_WIN32)
269
+ return _stricmp(a, b) == 0;
270
+ #else
271
+ return strcmp(a, b) == 0;
272
+ #endif
273
+ }
274
+
275
+ void xt_wrk_require_self_script(xt_value script) {
276
+ if (script == XT_UNDEFINED || script == XT_NULL) {
277
+ xt_throw(xt_error_from_message("TypeError: Worker requires a script URL"));
278
+ return;
279
+ }
280
+ xt_value text = xt_to_string(script);
281
+ xt_string *string = xt_as_string(text);
282
+ const char *raw = xt_string_bytes(string);
283
+ char path[4096];
284
+ size_t length = string->length < sizeof(path) - 1 ? string->length : sizeof(path) - 1;
285
+ memcpy(path, raw, length);
286
+ path[length] = 0;
287
+ char *cut = strpbrk(path, "?#");
288
+ if (cut) *cut = 0;
289
+ const char *base = xt_wrk_basename(path);
290
+ char *program = xt_wrk_duplicate(xt_wrk_basename(xt_wrk_program_path()));
291
+ int matches = program && xt_wrk_name_equals(base, program);
292
+ #if defined(_WIN32)
293
+ if (!matches && program) {
294
+ size_t n = strlen(program);
295
+ if (n > 4 && _stricmp(program + n - 4, ".exe") == 0) {
296
+ program[n - 4] = 0;
297
+ matches = xt_wrk_name_equals(base, program);
298
+ }
299
+ }
300
+ #endif
301
+ free(program);
302
+ if (matches) return;
303
+ char message[1024];
304
+ snprintf(message, sizeof(message),
305
+ "Error: worker script '%s' is not part of this binary. xbintsc compiles "
306
+ "every worker into the executable, so pass import.meta.filename and "
307
+ "branch on isMainThread.",
308
+ path);
309
+ xt_throw(xt_error_from_message(message));
310
+ }
311
+
312
+ /* -- worker scope (the `self` / `parentPort` side) ------------------------ */
313
+
314
+ /* Close the parent connection from the normal exit path. Letting the process
315
+ * teardown close it instead can abort the socket (TCP reset) before the parent
316
+ * has read the last frame, because the parent may still be starting other
317
+ * workers when this one finishes. */
318
+ static void xt_wrk_scope_shutdown(void) {
319
+ if (xt_wrk_scope_channel) xt_wrk_link_close(xt_wrk_scope_channel);
320
+ }
321
+
322
+ static void xt_wrk_scope_record_shutdown(void) {
323
+ static int installed = 0;
324
+ if (installed) return;
325
+ installed = 1;
326
+ atexit(xt_wrk_scope_shutdown);
327
+ }
328
+
329
+ static xt_wrk_link *xt_wrk_scope_record(void) {
330
+ if (xt_wrk_scope_channel) return xt_wrk_scope_channel;
331
+ xt_wrk_link *link = (xt_wrk_link *)calloc(1, sizeof(xt_wrk_link));
332
+ if (!link) abort();
333
+ link->target = XT_UNDEFINED;
334
+ link->terminate_promise = XT_UNDEFINED;
335
+ link->listen_fd = -1;
336
+ link->fd = -1;
337
+ const char *mode = xt_wrk_env_get(XT_WRK_ENV_MODE);
338
+ link->web_mode = mode != NULL && strcmp(mode, "web") == 0;
339
+ xt_wrk_scope_channel = link;
340
+ xt_wrk_gc_register();
341
+ xt_wrk_scope_record_shutdown();
342
+ return link;
343
+ }
344
+
345
+ static void xt_wrk_scope_read_ready(void *userdata, int events) {
346
+ xt_wrk_link *link = xt_wrk_unbox(userdata);
347
+ if (!link || link->fd < 0) return;
348
+ if (events & XT_IO_READ) {
349
+ char chunk[4096];
350
+ int closed = 0;
351
+ for (;;) {
352
+ long got = xt_wrk_recv_some(link->fd, chunk, sizeof(chunk));
353
+ if (got > 0) {
354
+ xt_wrk_in_append(link, chunk, (size_t)got);
355
+ continue;
356
+ }
357
+ if (got != -2) closed = 1;
358
+ break;
359
+ }
360
+ xt_wrk_consume_frames(link);
361
+ if (closed) {
362
+ xt_wrk_link_close(link);
363
+ return;
364
+ }
365
+ }
366
+ if (events & XT_IO_WRITE) xt_wrk_flush(link);
367
+ }
368
+
369
+ /* Connect to the parent on first use. The parent queues anything posted before
370
+ * the handshake, so connecting late is safe. */
371
+ int xt_wrk_scope_connect(void) {
372
+ xt_wrk_link *link = xt_wrk_scope_record();
373
+ if (link->connected) return 1;
374
+ if (!xt_wrk_is_worker()) return 0;
375
+ const char *port_text = xt_wrk_env_get(XT_WRK_ENV_PORT);
376
+ const char *token = xt_wrk_env_get(XT_WRK_ENV_TOKEN);
377
+ if (!port_text || !token) return 0;
378
+ int fd = xt_wrk_connect_loopback(atoi(port_text));
379
+ if (fd < 0) return 0;
380
+ size_t token_length = strlen(token);
381
+ size_t sent = 0;
382
+ while (sent < token_length) {
383
+ long written = xt_wrk_send_some(fd, token + sent, token_length - sent);
384
+ if (written <= 0) {
385
+ xt_wrk_close_fd(fd);
386
+ return 0;
387
+ }
388
+ sent += (size_t)written;
389
+ }
390
+ link->fd = fd;
391
+ link->connected = 1;
392
+ if (link->web_mode) xt_wrk_scope_listen();
393
+ return 1;
394
+ }
395
+
396
+ int xt_wrk_scope_connected(void) {
397
+ return xt_wrk_scope_channel != NULL && xt_wrk_scope_channel->connected;
398
+ }
399
+
400
+ void xt_wrk_scope_listen(void) {
401
+ if (!xt_wrk_scope_connect()) return;
402
+ xt_wrk_link *link = xt_wrk_scope_channel;
403
+ if (link->listening || link->fd < 0) return;
404
+ if (!link->waiter_box) link->waiter_box = xt_wrk_box(link);
405
+ if (xt_loop_add(link->fd, XT_IO_READ | (link->out_length ? XT_IO_WRITE : 0),
406
+ xt_wrk_scope_read_ready, link->waiter_box) != 0) {
407
+ return;
408
+ }
409
+ link->listening = 1;
410
+ }
411
+
412
+ xt_value xt_wrk_scope_object(void) {
413
+ xt_wrk_link *link = xt_wrk_scope_record();
414
+ if (link->target == XT_UNDEFINED) {
415
+ xt_value object = xt_object_new();
416
+ xt_object_set(object, xt_wrk_string("__xt_scope"), XT_TRUE);
417
+ link->target = object;
418
+ }
419
+ if (xt_wrk_is_worker() && !link->connected) xt_wrk_scope_connect();
420
+ return link->target;
421
+ }
422
+
423
+ void xt_wrk_scope_post(xt_value value) {
424
+ if (!xt_wrk_is_worker()) {
425
+ xt_throw(xt_error_from_message(
426
+ "Error: postMessage is only available inside a worker"));
427
+ return;
428
+ }
429
+ if (!xt_wrk_scope_connect()) {
430
+ xt_throw(xt_error_from_message("Error: worker is not connected to its parent"));
431
+ return;
432
+ }
433
+ size_t length = 0;
434
+ char *encoded = xt_wrk_encode(value, &length);
435
+ xt_wrk_queue_frame(xt_wrk_scope_channel, encoded, length);
436
+ free(encoded);
437
+ }
438
+
439
+ void xt_wrk_scope_close(void) {
440
+ if (xt_wrk_scope_channel) xt_wrk_link_close(xt_wrk_scope_channel);
441
+ }
442
+
443
+ void xt_wrk_listen_for_target(xt_value target) {
444
+ if (xt_wrk_scope_channel && xt_wrk_scope_channel->target == target) xt_wrk_scope_listen();
445
+ }
@@ -0,0 +1,223 @@
1
+ /*
2
+ * xbintsc runtime — worker transport internals.
3
+ *
4
+ * Shared by the two public worker APIs:
5
+ *
6
+ * - the Web Worker globals (`Worker`, `MessageChannel`, `self`) contributed
7
+ * by the `webworker` extension, and
8
+ * - Node's `worker_threads` module contributed by the `node` extension.
9
+ *
10
+ * xbintsc has no threads (the value model, the collector and the event loop are
11
+ * single-threaded), so a worker is a **second process** running the same
12
+ * executable. The two processes talk over a loopback TCP socket created by the
13
+ * parent; the port and a one-shot token travel through the environment, which
14
+ * is the only portable bootstrap channel (`fork`/`execvp` on POSIX,
15
+ * `_spawnvp`/`CreateProcess` on Windows).
16
+ *
17
+ * A socket (rather than stdin/stdout pipes) is what makes the design portable:
18
+ * the reactor selects on sockets on both platforms, Windows `select(2)` cannot
19
+ * wait on pipe handles, and the worker's own `console.log` keeps using the
20
+ * inherited stdout instead of corrupting a private protocol stream.
21
+ */
22
+ #ifndef XT_WORKER_INTERNAL_H
23
+ #define XT_WORKER_INTERNAL_H
24
+
25
+ #include "rt_internal.h"
26
+
27
+ #include <stdint.h>
28
+
29
+ /* Environment variables the parent sets on the child. */
30
+ #define XT_WRK_ENV_MARKER "XBINTSC_WORKER"
31
+ #define XT_WRK_ENV_PORT "XBINTSC_WORKER_PORT"
32
+ #define XT_WRK_ENV_TOKEN "XBINTSC_WORKER_TOKEN"
33
+ #define XT_WRK_ENV_NAME "XBINTSC_WORKER_NAME"
34
+ #define XT_WRK_ENV_DATA "XBINTSC_WORKER_DATA"
35
+ #define XT_WRK_ENV_MODE "XBINTSC_WORKER_MODE" /* "web" or "node" */
36
+
37
+ /** Largest frame either side accepts; guards against a corrupt length word. */
38
+ #define XT_WRK_MAX_FRAME (64u * 1024u * 1024u)
39
+
40
+ /* A live worker in the parent process. Opaque to the API layers. */
41
+ typedef struct xt_wrk_link xt_wrk_link;
42
+
43
+ /* A child process identifier: `pid_t` on POSIX, a process HANDLE on Windows.
44
+ * `intptr_t` holds either. */
45
+ typedef intptr_t xt_wrk_pid;
46
+
47
+ /* One control channel. Parent side: one per live worker. Worker side: the
48
+ * single connection to the parent. */
49
+ struct xt_wrk_link {
50
+ struct xt_wrk_link *next;
51
+ xt_value target;
52
+ xt_value terminate_promise;
53
+ xt_wrk_pid pid;
54
+ int listen_fd;
55
+ int fd;
56
+ int connected;
57
+ int handshaking; /* parent: the child's token has not been seen yet */
58
+ int listening; /* a read watcher is registered */
59
+ int exited;
60
+ int exit_code;
61
+ int terminating;
62
+ int web_mode;
63
+ int thread_id;
64
+ char token[24];
65
+ size_t token_length;
66
+ char *in;
67
+ size_t in_length;
68
+ size_t in_capacity;
69
+ char *out;
70
+ size_t out_length;
71
+ size_t out_capacity;
72
+ xt_value *waiter_box; /* `xt_loop_add` userdata (pointer kept in a value) */
73
+ };
74
+
75
+ /* -- operating-system half (worker_platform.c) ---------------------------- */
76
+ int xt_wrk_env_set(const char *key, const char *value);
77
+ void xt_wrk_env_unset(const char *key);
78
+ const char *xt_wrk_env_get(const char *key);
79
+ char *xt_wrk_duplicate(const char *text);
80
+ void xt_wrk_make_token(char *out, size_t capacity);
81
+ int xt_wrk_would_block(void);
82
+ void xt_wrk_close_fd(int fd);
83
+ int xt_wrk_set_nonblocking(int fd);
84
+ int xt_wrk_listen_loopback(int *out_port);
85
+ int xt_wrk_accept(int listen_fd);
86
+ int xt_wrk_connect_loopback(int port);
87
+ long xt_wrk_send_some(int fd, const char *data, size_t length);
88
+ long xt_wrk_recv_some(int fd, char *data, size_t capacity);
89
+ /** Wait until `fd` is writable (or the timeout expires). 1 on writable. */
90
+ int xt_wrk_wait_writable(int fd, int timeout_ms);
91
+ /** Wait until `fd` is readable or at EOF (or the timeout expires). 1 on ready. */
92
+ int xt_wrk_wait_readable(int fd, int timeout_ms);
93
+ xt_wrk_pid xt_wrk_spawn_process(const char *path, char **error);
94
+ /** 1 when ended (code in `*exit_code`), 0 while running. Reaps the child. */
95
+ int xt_wrk_process_exited(xt_wrk_pid pid, int *exit_code);
96
+ void xt_wrk_kill_process(xt_wrk_pid pid);
97
+
98
+ /* -- event delivery ------------------------------------------------------- */
99
+ /*
100
+ * A small EventEmitter shared by every worker object. Listeners live in an
101
+ * `__xt_events` property (event name -> array of functions); an event emitted
102
+ * before a listener exists is buffered in `__xt_pending` and replayed by the
103
+ * first `on`/`addEventListener`. That mirrors Node's emitter and keeps the
104
+ * browser surface working when a worker delivers a message before the program
105
+ * has attached its handler.
106
+ */
107
+ void xt_wrk_emit(xt_value target, const char *name, int32_t argc, xt_value *argv);
108
+ void xt_wrk_emit0(xt_value target, const char *name);
109
+ void xt_wrk_emit1(xt_value target, const char *name, xt_value arg);
110
+ void xt_wrk_on(xt_value target, const char *name, xt_value fn, int once);
111
+ void xt_wrk_off(xt_value target, const char *name, xt_value fn);
112
+ void xt_wrk_remove_all(xt_value target, const char *name);
113
+ int32_t xt_wrk_listener_count(xt_value target, const char *name);
114
+
115
+ /* -- parent side ---------------------------------------------------------- */
116
+
117
+ /** True when this process is the main program (no marker in the environment). */
118
+ int xt_wrk_is_main_thread(void);
119
+ /** `worker.name` / `self.name`; "" on the main thread. */
120
+ const char *xt_wrk_scope_name(void);
121
+ /** `workerData`, decoded once; `XT_UNDEFINED` on the main thread. */
122
+ xt_value xt_wrk_worker_data(void);
123
+ /** Decoded `workerData` for a spawn (never throws). */
124
+ char *xt_wrk_encode_data(xt_value value, size_t *length);
125
+
126
+ /*
127
+ * Spawn `path` as a worker. `data` is the workerData value (XT_UNDEFINED for
128
+ * none), `name` the worker name, `web_mode` selects browser lifetime rules
129
+ * (a Web Worker stays alive waiting for messages; a Node worker only does when
130
+ * a `message` listener keeps the port referenced). On failure returns NULL and
131
+ * stores a malloc'd message in `*error` (caller frees).
132
+ */
133
+ xt_wrk_link *xt_wrk_spawn(const char *path, xt_value data, const char *name, int web_mode,
134
+ xt_value target, char **error);
135
+ void xt_wrk_post(xt_wrk_link *link, xt_value value, int32_t argc, xt_value *argv);
136
+ void xt_wrk_terminate(xt_wrk_link *link);
137
+ double xt_wrk_thread_id(xt_wrk_link *link);
138
+ int xt_wrk_link_alive(xt_wrk_link *link);
139
+ /* Node `ref`/`unref`: whether the link keeps this process's event loop alive. */
140
+ void xt_wrk_ref(xt_wrk_link *link);
141
+ void xt_wrk_unref(xt_wrk_link *link);
142
+
143
+ /* -- worker side ---------------------------------------------------------- */
144
+
145
+ /** True when this process was started as a worker (marker present). */
146
+ int xt_wrk_is_worker(void);
147
+ /** Connect to the parent on first use. Returns 1 when connected. */
148
+ int xt_wrk_scope_connect(void);
149
+ /** Whether the scope socket is connected. */
150
+ int xt_wrk_scope_connected(void);
151
+ /** The `self` / `parentPort` object receiving messages. */
152
+ xt_value xt_wrk_scope_object(void);
153
+ xt_value xt_wrk_scope_data(void);
154
+ void xt_wrk_scope_post(xt_value value);
155
+ void xt_wrk_scope_close(void);
156
+ /** Start reading parent messages; called when a message listener is attached. */
157
+ void xt_wrk_scope_listen(void);
158
+
159
+ /* -- message codec -------------------------------------------------------- */
160
+
161
+ /** Growable text buffer used by the wire encoder. */
162
+ typedef struct {
163
+ char *data;
164
+ size_t length;
165
+ size_t capacity;
166
+ } xt_wrk_buffer;
167
+
168
+ void xt_wrk_buffer_reserve(xt_wrk_buffer *buffer, size_t extra);
169
+ void xt_wrk_buffer_put(xt_wrk_buffer *buffer, const char *text, size_t length);
170
+ void xt_wrk_buffer_puts(xt_wrk_buffer *buffer, const char *text);
171
+ void xt_wrk_buffer_putc(xt_wrk_buffer *buffer, char value);
172
+ void xt_wrk_buffer_put_uint(xt_wrk_buffer *buffer, unsigned long value);
173
+ void xt_wrk_buffer_put_string(xt_wrk_buffer *buffer, const char *text, size_t length);
174
+ void xt_wrk_buffer_put_base64(xt_wrk_buffer *buffer, const uint8_t *data, size_t length);
175
+ uint8_t *xt_wrk_decode_base64(const char *text, size_t length, size_t *out_length);
176
+ int xt_wrk_width_of_typed_array(const char *name);
177
+
178
+ /** Encode a value for the wire; returns a malloc'd, NUL-terminated buffer. */
179
+ char *xt_wrk_encode(xt_value value, size_t *length);
180
+ /** Decode one frame; throws a TypeError on malformed input. */
181
+ xt_value xt_wrk_decode(const char *bytes, size_t length);
182
+
183
+ /* -- frame plumbing (worker_core.c) --------------------------------------- */
184
+
185
+ /** Queue one length-prefixed frame and try to flush it. */
186
+ void xt_wrk_queue_frame(xt_wrk_link *link, const char *payload, size_t length);
187
+ void xt_wrk_flush(xt_wrk_link *link);
188
+ /** Parse every complete frame in `link->in`, delivering `message` events. */
189
+ void xt_wrk_consume_frames(xt_wrk_link *link);
190
+ /** Close the socket and stop watching it (the link object stays alive). */
191
+ void xt_wrk_link_close(xt_wrk_link *link);
192
+ /** Called by `xt_wrk_on` so a message listener starts reading. */
193
+ void xt_wrk_listen_for_target(xt_value target);
194
+
195
+ /* -- state shared by core and transport ----------------------------------- */
196
+ extern xt_wrk_link *xt_wrk_parents;
197
+ extern xt_wrk_link *xt_wrk_scope_channel;
198
+ extern uint64_t xt_wrk_poll_timer;
199
+ extern int xt_wrk_thread_counter;
200
+ void xt_wrk_gc_register(void);
201
+ xt_value *xt_wrk_box(xt_wrk_link *link);
202
+ xt_wrk_link *xt_wrk_unbox(void *userdata);
203
+ void xt_wrk_in_append(xt_wrk_link *link, const char *data, size_t length);
204
+ xt_value xt_wrk_terminate_promise(xt_wrk_link *link);
205
+
206
+ /* -- JavaScript surface shared by the two APIs (worker_api.c) ------------- */
207
+ void xt_wrk_install_emitter(xt_value object);
208
+ void xt_wrk_install_dom_events(xt_value object);
209
+ xt_value xt_wrk_new_worker(int32_t argc, xt_value *argv, int web_mode);
210
+ xt_value xt_wrk_scope_api_object(void);
211
+
212
+ /* -- entry-point validation ---------------------------------------------- */
213
+
214
+ /** The executable's own path (`import.meta.filename`). */
215
+ const char *xt_wrk_program_path(void);
216
+ /**
217
+ * Validate the `filename`/URL a Worker constructor was given. xbintsc compiles
218
+ * every worker into the same binary, so the only script that can run is this
219
+ * program; anything else throws with an actionable message.
220
+ */
221
+ void xt_wrk_require_self_script(xt_value script);
222
+
223
+ #endif /* XT_WORKER_INTERNAL_H */