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,422 @@
1
+ /*
2
+ * xbintsc runtime — shared JavaScript surface for the worker APIs.
3
+ *
4
+ * One worker object serves both platforms:
5
+ *
6
+ * Node `worker_threads` Web Worker
7
+ * ------------------------------- --------------------------------------
8
+ * worker.on("message", fn) worker.onmessage = fn
9
+ * worker.postMessage(value) worker.postMessage(value)
10
+ * worker.terminate(): Promise worker.terminate()
11
+ * worker.threadId worker.addEventListener("message", fn)
12
+ *
13
+ * Node listeners receive the raw value; `onmessage` / `addEventListener`
14
+ * listeners receive a `MessageEvent`-shaped object (`{ type, data, target }`).
15
+ * Both kinds are stored in the same emitter, which is why a program may mix
16
+ * them. The scope object (`self` in a worker, `parentPort` in Node) is the same
17
+ * shape, so a worker may answer through either convention.
18
+ */
19
+
20
+ #include "worker_internal.h"
21
+
22
+ #include <stdio.h>
23
+ #include <stdlib.h>
24
+ #include <string.h>
25
+
26
+ /* -- object helpers ------------------------------------------------------- */
27
+
28
+ static xt_value xt_wrk_str(const char *text) { return xt_string_from_cstr(text); }
29
+
30
+ static void xt_wrk_put(xt_value object, const char *key, xt_value value) {
31
+ xt_object_set(object, xt_wrk_str(key), value);
32
+ }
33
+
34
+ static xt_value xt_wrk_fetch(xt_value object, const char *key) {
35
+ return xt_object_get_cstr(object, key);
36
+ }
37
+
38
+ static const char *xt_wrk_text(xt_value value) {
39
+ return XT_IS_STRING(value) ? xt_string_data(value) : NULL;
40
+ }
41
+
42
+ static void xt_wrk_method(xt_value object, const char *name, void *fn) {
43
+ xt_wrk_put(object, name, xt_closure_new(fn, 0, NULL));
44
+ }
45
+
46
+ static void xt_wrk_accessor(xt_value object, const char *name, void *getter, void *setter) {
47
+ xt_object_define_getter(object, xt_wrk_str(name), xt_closure_new(getter, 0, NULL));
48
+ if (setter) xt_object_define_setter(object, xt_wrk_str(name), xt_closure_new(setter, 0, NULL));
49
+ }
50
+
51
+ /* The link pointer travels in a hidden property. Its tag bits are 0x0000 (no
52
+ * value tag), so the collector treats it as a number and skips it. */
53
+ static xt_wrk_link *xt_wrk_owner(xt_value object) {
54
+ return (xt_wrk_link *)(uintptr_t)xt_wrk_fetch(object, "__xt_link");
55
+ }
56
+
57
+ static void xt_wrk_attach(xt_value object, xt_wrk_link *link) {
58
+ xt_object_set(object, xt_wrk_str("__xt_link"), (xt_value)(uintptr_t)link);
59
+ }
60
+
61
+ /* -- EventEmitter methods ------------------------------------------------- */
62
+
63
+ static xt_value xt_wrk_on_method(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
64
+ (void)env;
65
+ const char *event = xt_wrk_text(xt_arg_at(argc, argv, 0));
66
+ if (event) xt_wrk_on(self, event, xt_arg_at(argc, argv, 1), 0);
67
+ return self;
68
+ }
69
+
70
+ static xt_value xt_wrk_once_method(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
71
+ (void)env;
72
+ const char *event = xt_wrk_text(xt_arg_at(argc, argv, 0));
73
+ if (event) xt_wrk_on(self, event, xt_arg_at(argc, argv, 1), 1);
74
+ return self;
75
+ }
76
+
77
+ static xt_value xt_wrk_off_method(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
78
+ (void)env;
79
+ const char *event = xt_wrk_text(xt_arg_at(argc, argv, 0));
80
+ if (event) xt_wrk_off(self, event, xt_arg_at(argc, argv, 1));
81
+ return self;
82
+ }
83
+
84
+ static xt_value xt_wrk_remove_all_method(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
85
+ (void)env;
86
+ const char *event = xt_wrk_text(xt_arg_at(argc, argv, 0));
87
+ if (event) {
88
+ xt_wrk_remove_all(self, event);
89
+ } else {
90
+ xt_value names = xt_object_keys(xt_wrk_fetch(self, "__xt_events"));
91
+ xt_array *list = xt_as_array(names);
92
+ if (list) {
93
+ for (uint32_t i = 0; i < list->length; i++) {
94
+ const char *name = xt_wrk_text(list->items[i]);
95
+ if (name) xt_wrk_remove_all(self, name);
96
+ }
97
+ }
98
+ }
99
+ return self;
100
+ }
101
+
102
+ static xt_value xt_wrk_emit_method(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
103
+ (void)env;
104
+ const char *event = xt_wrk_text(xt_arg_at(argc, argv, 0));
105
+ if (!event) return XT_FALSE;
106
+ xt_wrk_emit(self, event, argc > 1 ? argc - 1 : 0, argc > 1 ? argv + 1 : NULL);
107
+ return XT_TRUE;
108
+ }
109
+
110
+ void xt_wrk_install_emitter(xt_value object) {
111
+ xt_wrk_method(object, "on", (void *)xt_wrk_on_method);
112
+ xt_wrk_method(object, "addListener", (void *)xt_wrk_on_method);
113
+ xt_wrk_method(object, "once", (void *)xt_wrk_once_method);
114
+ xt_wrk_method(object, "off", (void *)xt_wrk_off_method);
115
+ xt_wrk_method(object, "removeListener", (void *)xt_wrk_off_method);
116
+ xt_wrk_method(object, "removeAllListeners", (void *)xt_wrk_remove_all_method);
117
+ xt_wrk_method(object, "emit", (void *)xt_wrk_emit_method);
118
+ }
119
+
120
+ /* -- MessageEvent wrappers ------------------------------------------------ */
121
+
122
+ static xt_value xt_wrk_event_object(const char *type, xt_value target) {
123
+ xt_value event = xt_object_new();
124
+ xt_wrk_put(event, "type", xt_wrk_str(type));
125
+ xt_wrk_put(event, "target", target);
126
+ xt_wrk_put(event, "currentTarget", target);
127
+ return event;
128
+ }
129
+
130
+ static xt_value xt_wrk_message_call(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
131
+ xt_value handler = xt_closure_env(env, 0);
132
+ xt_value event = xt_wrk_event_object("message", self);
133
+ xt_wrk_put(event, "data", xt_arg_at(argc, argv, 0));
134
+ return xt_call_with_this(handler, self, 1, &event);
135
+ }
136
+
137
+ static xt_value xt_wrk_error_call(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
138
+ xt_value handler = xt_closure_env(env, 0);
139
+ xt_value error = xt_arg_at(argc, argv, 0);
140
+ xt_value event = xt_wrk_event_object("error", self);
141
+ xt_wrk_put(event, "error", error);
142
+ xt_wrk_put(event, "message", xt_to_string(error));
143
+ return xt_call_with_this(handler, self, 1, &event);
144
+ }
145
+
146
+ /* A wrapper is installed instead of the handler so the browser listener sees a
147
+ * MessageEvent while a Node listener on the same object still sees the value. */
148
+ static void xt_wrk_listen_dom(xt_value self, const char *type, xt_value handler) {
149
+ if (!XT_IS_FUNCTION(handler) || !type) return;
150
+ void *entry = strcmp(type, "message") == 0 ? (void *)xt_wrk_message_call : (void *)xt_wrk_error_call;
151
+ xt_value environment[1] = {handler};
152
+ xt_value wrapper = xt_closure_new(entry, 1, environment);
153
+ xt_wrk_put(wrapper, "__xt_original", handler);
154
+ xt_wrk_on(self, type, wrapper, 0);
155
+ }
156
+
157
+ static xt_value xt_wrk_add_listener(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
158
+ (void)env;
159
+ const char *type = xt_wrk_text(xt_arg_at(argc, argv, 0));
160
+ xt_value handler = xt_arg_at(argc, argv, 1);
161
+ if (type && XT_IS_FUNCTION(handler)) {
162
+ if (strcmp(type, "message") == 0 || strcmp(type, "error") == 0 ||
163
+ strcmp(type, "messageerror") == 0) {
164
+ xt_wrk_listen_dom(self, type, handler);
165
+ } else {
166
+ xt_wrk_on(self, type, handler, 0);
167
+ }
168
+ }
169
+ return XT_UNDEFINED;
170
+ }
171
+
172
+ static xt_value xt_wrk_remove_listener(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
173
+ (void)env;
174
+ const char *type = xt_wrk_text(xt_arg_at(argc, argv, 0));
175
+ if (type) xt_wrk_off(self, type, xt_arg_at(argc, argv, 1));
176
+ return XT_UNDEFINED;
177
+ }
178
+
179
+ static xt_value xt_wrk_dispatch(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
180
+ (void)env;
181
+ xt_value event = xt_arg_at(argc, argv, 0);
182
+ const char *type = XT_IS_OBJECT(event) ? xt_wrk_text(xt_wrk_fetch(event, "type")) : NULL;
183
+ if (type) xt_wrk_emit(self, type, 1, &event);
184
+ return XT_TRUE;
185
+ }
186
+
187
+ /* `onmessage = fn`: an accessor rather than a data property, because assigning
188
+ * it must register (or replace) a listener. */
189
+ static xt_value xt_wrk_on_accessor(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
190
+ (void)env;
191
+ (void)argc;
192
+ (void)argv;
193
+ return xt_wrk_fetch(self, "__xt_on_message");
194
+ }
195
+
196
+ static xt_value xt_wrk_on_setter(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
197
+ (void)env;
198
+ xt_value handler = xt_arg_at(argc, argv, 0);
199
+ xt_value previous = xt_wrk_fetch(self, "__xt_on_message");
200
+ if (XT_IS_FUNCTION(previous)) xt_wrk_off(self, "message", previous);
201
+ xt_wrk_put(self, "__xt_on_message", handler);
202
+ if (XT_IS_FUNCTION(handler)) xt_wrk_listen_dom(self, "message", handler);
203
+ return handler;
204
+ }
205
+
206
+ static xt_value xt_wrk_onerror_get(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
207
+ (void)env;
208
+ (void)argc;
209
+ (void)argv;
210
+ return xt_wrk_fetch(self, "__xt_on_error");
211
+ }
212
+
213
+ static xt_value xt_wrk_onerror_set(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
214
+ (void)env;
215
+ xt_value handler = xt_arg_at(argc, argv, 0);
216
+ xt_value previous = xt_wrk_fetch(self, "__xt_on_error");
217
+ if (XT_IS_FUNCTION(previous)) xt_wrk_off(self, "error", previous);
218
+ xt_wrk_put(self, "__xt_on_error", handler);
219
+ if (XT_IS_FUNCTION(handler)) xt_wrk_listen_dom(self, "error", handler);
220
+ return handler;
221
+ }
222
+
223
+ void xt_wrk_install_dom_events(xt_value object) {
224
+ xt_wrk_method(object, "addEventListener", (void *)xt_wrk_add_listener);
225
+ xt_wrk_method(object, "removeEventListener", (void *)xt_wrk_remove_listener);
226
+ xt_wrk_method(object, "dispatchEvent", (void *)xt_wrk_dispatch);
227
+ xt_wrk_accessor(object, "onmessage", (void *)xt_wrk_on_accessor, (void *)xt_wrk_on_setter);
228
+ xt_wrk_accessor(object, "onerror", (void *)xt_wrk_onerror_get, (void *)xt_wrk_onerror_set);
229
+ xt_wrk_accessor(object, "onmessageerror", (void *)xt_wrk_onerror_get,
230
+ (void *)xt_wrk_onerror_set);
231
+ }
232
+
233
+ /* -- worker object -------------------------------------------------------- */
234
+
235
+ static xt_value xt_wrk_worker_post(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
236
+ (void)env;
237
+ xt_wrk_link *link = xt_wrk_owner(self);
238
+ xt_wrk_post(link, xt_arg_at(argc, argv, 0), argc > 1 ? argc - 1 : 0, argc > 1 ? argv + 1 : NULL);
239
+ return XT_UNDEFINED;
240
+ }
241
+
242
+ static xt_value xt_wrk_worker_terminate(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
243
+ (void)env;
244
+ (void)argc;
245
+ (void)argv;
246
+ xt_wrk_link *link = xt_wrk_owner(self);
247
+ if (!link) return XT_UNDEFINED;
248
+ return xt_wrk_terminate_promise(link);
249
+ }
250
+
251
+ static xt_value xt_wrk_worker_ref(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
252
+ (void)env;
253
+ (void)argc;
254
+ (void)argv;
255
+ xt_wrk_ref(xt_wrk_owner(self));
256
+ return self;
257
+ }
258
+
259
+ static xt_value xt_wrk_worker_unref(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
260
+ (void)env;
261
+ (void)argc;
262
+ (void)argv;
263
+ xt_wrk_unref(xt_wrk_owner(self));
264
+ return self;
265
+ }
266
+
267
+ static void xt_wrk_install_worker_object(xt_value object) {
268
+ xt_wrk_put(object, "__xt_worker", XT_TRUE);
269
+ xt_wrk_install_emitter(object);
270
+ xt_wrk_install_dom_events(object);
271
+ xt_wrk_method(object, "postMessage", (void *)xt_wrk_worker_post);
272
+ xt_wrk_method(object, "terminate", (void *)xt_wrk_worker_terminate);
273
+ xt_wrk_method(object, "ref", (void *)xt_wrk_worker_ref);
274
+ xt_wrk_method(object, "unref", (void *)xt_wrk_worker_unref);
275
+ }
276
+
277
+ xt_value xt_wrk_new_worker(int32_t argc, xt_value *argv, int web_mode) {
278
+ xt_value script = xt_arg_at(argc, argv, 0);
279
+ xt_wrk_require_self_script(script);
280
+ xt_value options = xt_arg_at(argc, argv, 1);
281
+ xt_value data = XT_UNDEFINED;
282
+ const char *name = "";
283
+ if (XT_IS_OBJECT(options)) {
284
+ data = xt_wrk_fetch(options, "workerData");
285
+ xt_value name_value = xt_wrk_fetch(options, "name");
286
+ if (XT_IS_STRING(name_value)) name = xt_string_data(name_value);
287
+ }
288
+ xt_value object = xt_object_new();
289
+ xt_wrk_install_worker_object(object);
290
+ char *error = NULL;
291
+ xt_wrk_link *link = xt_wrk_spawn(xt_wrk_program_path(), data, name, web_mode, object, &error);
292
+ if (!link) {
293
+ char message[512];
294
+ snprintf(message, sizeof(message), "Error: %s", error ? error : "failed to start worker");
295
+ free(error);
296
+ xt_throw(xt_error_from_message(message));
297
+ return XT_UNDEFINED;
298
+ }
299
+ xt_wrk_attach(object, link);
300
+ xt_wrk_put(object, "threadId", xt_number(xt_wrk_thread_id(link)));
301
+ return object;
302
+ }
303
+
304
+ /* -- scope object (`self` in a worker, `parentPort` in Node) --------------- */
305
+
306
+ static xt_value xt_wrk_scope_post_method(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
307
+ (void)env;
308
+ xt_wrk_scope_post(xt_arg_at(argc, argv, 0));
309
+ (void)self;
310
+ return XT_UNDEFINED;
311
+ }
312
+
313
+ static xt_value xt_wrk_scope_close_method(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
314
+ (void)env;
315
+ (void)argc;
316
+ (void)argv;
317
+ (void)self;
318
+ xt_wrk_scope_close();
319
+ return XT_UNDEFINED;
320
+ }
321
+
322
+ static xt_value xt_wrk_scope_start_method(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
323
+ (void)env;
324
+ (void)argc;
325
+ (void)argv;
326
+ (void)self;
327
+ xt_wrk_scope_listen();
328
+ return XT_UNDEFINED;
329
+ }
330
+
331
+ static void xt_wrk_install_scope_object(xt_value object) {
332
+ if (xt_truthy(xt_wrk_fetch(object, "__xt_scope_api"))) return;
333
+ xt_wrk_put(object, "__xt_scope_api", XT_TRUE);
334
+ xt_wrk_install_emitter(object);
335
+ xt_wrk_install_dom_events(object);
336
+ xt_wrk_method(object, "postMessage", (void *)xt_wrk_scope_post_method);
337
+ xt_wrk_method(object, "close", (void *)xt_wrk_scope_close_method);
338
+ xt_wrk_method(object, "start", (void *)xt_wrk_scope_start_method);
339
+ xt_wrk_method(object, "ref", (void *)xt_wrk_worker_ref);
340
+ xt_wrk_method(object, "unref", (void *)xt_wrk_worker_unref);
341
+ xt_wrk_put(object, "name", xt_wrk_str(xt_wrk_scope_name()));
342
+ }
343
+
344
+ xt_value xt_wrk_scope_api_object(void) {
345
+ xt_value object = xt_wrk_scope_object();
346
+ xt_wrk_install_scope_object(object);
347
+ return object;
348
+ }
349
+
350
+ /* -- Web Worker globals --------------------------------------------------- */
351
+
352
+ /* A global value is read as a function value; `new Worker(...)` then goes
353
+ * through the ordinary `xt_new`. Cache the constructor so `Worker` keeps its
354
+ * identity across reads. */
355
+ static xt_value xt_webworker_worker_value = XT_UNDEFINED;
356
+
357
+ static xt_value xt_webworker_worker_ctor(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
358
+ (void)self;
359
+ (void)env;
360
+ return xt_wrk_new_worker(argc, argv, 1);
361
+ }
362
+
363
+ xt_value xt_webworker_worker(int32_t argc, xt_value *argv) {
364
+ (void)argc;
365
+ (void)argv;
366
+ if (!XT_IS_FUNCTION(xt_webworker_worker_value)) {
367
+ xt_webworker_worker_value = xt_closure_new((void *)xt_webworker_worker_ctor, 0, NULL);
368
+ xt_gc_add_root(&xt_webworker_worker_value);
369
+ }
370
+ return xt_webworker_worker_value;
371
+ }
372
+
373
+ xt_value xt_webworker_worker_new(int32_t argc, xt_value *argv) {
374
+ return xt_wrk_new_worker(argc, argv, 1);
375
+ }
376
+
377
+ xt_value xt_webworker_self(int32_t argc, xt_value *argv) {
378
+ (void)argc;
379
+ (void)argv;
380
+ return xt_wrk_scope_api_object();
381
+ }
382
+
383
+ xt_value xt_webworker_is_main_thread(int32_t argc, xt_value *argv) {
384
+ (void)argc;
385
+ (void)argv;
386
+ return xt_bool(xt_wrk_is_main_thread());
387
+ }
388
+
389
+ xt_value xt_webworker_post_message(int32_t argc, xt_value *argv) {
390
+ xt_wrk_scope_post(xt_arg_at(argc, argv, 0));
391
+ return XT_UNDEFINED;
392
+ }
393
+
394
+ /* -- node:worker_threads exports ------------------------------------------ */
395
+
396
+ xt_value xt_wt_worker(int32_t argc, xt_value *argv) { return xt_wrk_new_worker(argc, argv, 0); }
397
+
398
+ xt_value xt_wt_is_main_thread(int32_t argc, xt_value *argv) {
399
+ (void)argc;
400
+ (void)argv;
401
+ return xt_bool(xt_wrk_is_main_thread());
402
+ }
403
+
404
+ xt_value xt_wt_worker_data(int32_t argc, xt_value *argv) {
405
+ (void)argc;
406
+ (void)argv;
407
+ if (xt_wrk_is_main_thread()) return XT_NULL;
408
+ return xt_wrk_worker_data();
409
+ }
410
+
411
+ xt_value xt_wt_parent_port(int32_t argc, xt_value *argv) {
412
+ (void)argc;
413
+ (void)argv;
414
+ if (xt_wrk_is_main_thread()) return XT_NULL;
415
+ return xt_wrk_scope_api_object();
416
+ }
417
+
418
+ xt_value xt_wt_mark_as_uncloneable(int32_t argc, xt_value *argv) {
419
+ (void)argc;
420
+ (void)argv;
421
+ return XT_UNDEFINED;
422
+ }