xbintsc 0.6.23 → 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 (249) 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/concatenate.d.ts +23 -0
  11. package/dist/src/driver/bundler/concatenate.js +210 -0
  12. package/dist/src/driver/bundler/concatenate.js.map +1 -0
  13. package/dist/src/driver/bundler/graph.js +9 -5
  14. package/dist/src/driver/bundler/graph.js.map +1 -1
  15. package/dist/src/driver/bundler/merge.d.ts +4 -4
  16. package/dist/src/driver/bundler/merge.js +24 -207
  17. package/dist/src/driver/bundler/merge.js.map +1 -1
  18. package/dist/src/driver/bundler/{namespace-import.d.ts → namespace-object.d.ts} +5 -1
  19. package/dist/src/driver/bundler/{namespace-import.js → namespace-object.js} +6 -2
  20. package/dist/src/driver/bundler/namespace-object.js.map +1 -0
  21. package/dist/src/driver/bundler/{reexport-fixup.d.ts → reexport-resolution.d.ts} +9 -3
  22. package/dist/src/driver/bundler/{reexport-fixup.js → reexport-resolution.js} +16 -6
  23. package/dist/src/driver/bundler/reexport-resolution.js.map +1 -0
  24. package/dist/src/driver/fingerprint.d.ts +2 -2
  25. package/dist/src/driver/fingerprint.js +3 -3
  26. package/dist/src/driver/fingerprint.js.map +1 -1
  27. package/dist/src/driver/link-features.js +1 -1
  28. package/dist/src/driver/link-features.js.map +1 -1
  29. package/dist/src/driver/modules.d.ts +12 -5
  30. package/dist/src/driver/modules.js +12 -5
  31. package/dist/src/driver/modules.js.map +1 -1
  32. package/dist/src/driver/paths.js +8 -4
  33. package/dist/src/driver/paths.js.map +1 -1
  34. package/dist/src/driver/runtime-objects.d.ts +1 -1
  35. package/dist/src/driver/runtime-objects.js +13 -1
  36. package/dist/src/driver/runtime-objects.js.map +1 -1
  37. package/dist/src/driver/version.d.ts +1 -1
  38. package/dist/src/driver/version.js +1 -1
  39. package/dist/src/extensions/catalog.js +5 -1
  40. package/dist/src/extensions/catalog.js.map +1 -1
  41. package/dist/src/extensions/gui/index.js +0 -1
  42. package/dist/src/extensions/gui/index.js.map +1 -1
  43. package/dist/src/extensions/link-feature.d.ts +8 -0
  44. package/dist/src/extensions/node/stream-web/index.d.ts +6 -1
  45. package/dist/src/extensions/node/stream-web/index.js +59 -4
  46. package/dist/src/extensions/node/stream-web/index.js.map +1 -1
  47. package/dist/src/extensions/node/worker_threads/index.d.ts +14 -4
  48. package/dist/src/extensions/node/worker_threads/index.js +24 -11
  49. package/dist/src/extensions/node/worker_threads/index.js.map +1 -1
  50. package/dist/src/extensions/registry.d.ts +9 -1
  51. package/dist/src/extensions/registry.js +4 -1
  52. package/dist/src/extensions/registry.js.map +1 -1
  53. package/dist/src/extensions/wasm-asset.d.ts +16 -0
  54. package/dist/src/extensions/wasm-asset.js +28 -0
  55. package/dist/src/extensions/wasm-asset.js.map +1 -0
  56. package/dist/src/extensions/wasm.d.ts +17 -0
  57. package/dist/src/extensions/wasm.js +68 -0
  58. package/dist/src/extensions/wasm.js.map +1 -0
  59. package/dist/src/extensions/webworker/index.d.ts +24 -0
  60. package/dist/src/extensions/webworker/index.js +54 -0
  61. package/dist/src/extensions/webworker/index.js.map +1 -0
  62. package/dist/src/index.d.ts +1 -0
  63. package/dist/src/index.js +1 -0
  64. package/dist/src/index.js.map +1 -1
  65. package/dist/tests/driver/deferred-reexport.test.js +25 -0
  66. package/dist/tests/driver/deferred-reexport.test.js.map +1 -1
  67. package/dist/tests/driver/link-features.test.js +9 -0
  68. package/dist/tests/driver/link-features.test.js.map +1 -1
  69. package/dist/tests/e2e/async-fs.test.js +44 -0
  70. package/dist/tests/e2e/async-fs.test.js.map +1 -1
  71. package/dist/tests/e2e/harness.d.ts +6 -0
  72. package/dist/tests/e2e/harness.js +13 -1
  73. package/dist/tests/e2e/harness.js.map +1 -1
  74. package/dist/tests/e2e/node-crypto-random.test.d.ts +8 -0
  75. package/dist/tests/e2e/node-crypto-random.test.js +148 -0
  76. package/dist/tests/e2e/node-crypto-random.test.js.map +1 -0
  77. package/dist/tests/e2e/node-modules.test.js +1 -1
  78. package/dist/tests/e2e/node-modules.test.js.map +1 -1
  79. package/dist/tests/e2e/web-streams.test.d.ts +8 -0
  80. package/dist/tests/e2e/web-streams.test.js +274 -0
  81. package/dist/tests/e2e/web-streams.test.js.map +1 -0
  82. package/dist/tests/e2e/web-worker.test.d.ts +7 -0
  83. package/dist/tests/e2e/web-worker.test.js +74 -0
  84. package/dist/tests/e2e/web-worker.test.js.map +1 -0
  85. package/dist/tests/e2e/webassembly.test.d.ts +8 -0
  86. package/dist/tests/e2e/webassembly.test.js +113 -0
  87. package/dist/tests/e2e/webassembly.test.js.map +1 -0
  88. package/dist/tests/e2e/worker-api.test.d.ts +14 -0
  89. package/dist/tests/e2e/worker-api.test.js +460 -0
  90. package/dist/tests/e2e/worker-api.test.js.map +1 -0
  91. package/dist/tests/e2e/worker-threads.test.d.ts +8 -0
  92. package/dist/tests/e2e/worker-threads.test.js +160 -0
  93. package/dist/tests/e2e/worker-threads.test.js.map +1 -0
  94. package/dist/tests/extensions/wasm.test.d.ts +1 -0
  95. package/dist/tests/extensions/wasm.test.js +53 -0
  96. package/dist/tests/extensions/wasm.test.js.map +1 -0
  97. package/doc/ai/cli.md +1 -1
  98. package/doc/ai/contributing.md +11 -4
  99. package/doc/ai/extensions.md +25 -2
  100. package/doc/ai/language-support.md +14 -2
  101. package/doc/ai/zh-CN/cli.md +1 -1
  102. package/doc/ai/zh-CN/contributing.md +8 -3
  103. package/doc/ai/zh-CN/extensions.md +23 -2
  104. package/doc/ai/zh-CN/language-support.md +13 -2
  105. package/doc/gui/api.md +1 -1
  106. package/doc/implemented/cli.md +1 -1
  107. package/doc/implemented/extensions.md +6 -5
  108. package/doc/implemented/node/stream-web.md +31 -0
  109. package/doc/implemented/node/worker-threads.md +88 -5
  110. package/doc/implemented/node.md +2 -1
  111. package/doc/implemented/streams.md +82 -0
  112. package/doc/implemented/wasm.md +80 -18
  113. package/doc/implemented/web-worker.md +84 -0
  114. package/doc/implemented.md +6 -2
  115. package/doc/unimplemented/deviations.md +1 -0
  116. package/doc/unimplemented/node/worker-threads.md +23 -0
  117. package/doc/unimplemented/node.md +1 -0
  118. package/doc/unimplemented/stdlib.md +2 -1
  119. package/doc/unimplemented/wasm.md +21 -9
  120. package/doc/unimplemented.md +1 -1
  121. package/doc/zh-CN/gui/api.md +1 -1
  122. package/doc/zh-CN/implemented/cli.md +1 -1
  123. package/doc/zh-CN/implemented/extensions.md +7 -6
  124. package/doc/zh-CN/implemented/node/stream-web.md +24 -0
  125. package/doc/zh-CN/implemented/node/worker-threads.md +77 -4
  126. package/doc/zh-CN/implemented/node.md +2 -1
  127. package/doc/zh-CN/implemented/streams.md +60 -0
  128. package/doc/zh-CN/implemented/wasm.md +73 -16
  129. package/doc/zh-CN/implemented/web-worker.md +79 -0
  130. package/doc/zh-CN/implemented.md +6 -2
  131. package/doc/zh-CN/unimplemented/deviations.md +1 -0
  132. package/doc/zh-CN/unimplemented/node/worker-threads.md +20 -0
  133. package/doc/zh-CN/unimplemented/node.md +1 -0
  134. package/doc/zh-CN/unimplemented/stdlib.md +2 -1
  135. package/doc/zh-CN/unimplemented/wasm.md +16 -8
  136. package/doc/zh-CN/unimplemented.md +1 -1
  137. package/llms.txt +2 -0
  138. package/package.json +8 -7
  139. package/runtime/ext_gui/browser.cpp +1 -1
  140. package/runtime/ext_node/stream_web/stream_web.c +20 -6
  141. package/runtime/worker/worker_api.c +422 -0
  142. package/runtime/worker/worker_codec.c +489 -0
  143. package/runtime/worker/worker_core.c +445 -0
  144. package/runtime/worker/worker_internal.h +223 -0
  145. package/runtime/worker/worker_platform.c +344 -0
  146. package/runtime/worker/worker_ports.c +203 -0
  147. package/runtime/worker/worker_transport.c +464 -0
  148. package/runtime/worker/worker_wire.c +150 -0
  149. package/runtime/xt_builtins.c +15 -0
  150. package/runtime/xt_constructors.c +49 -2
  151. package/runtime/xt_stdlib2/error.inc +4 -0
  152. package/runtime/xt_streams/common.inc +232 -0
  153. package/runtime/xt_streams/constructors.inc +232 -0
  154. package/runtime/xt_streams/encoding.inc +124 -0
  155. package/runtime/xt_streams/readable-extra.inc +372 -0
  156. package/runtime/xt_streams/readable.inc +415 -0
  157. package/runtime/xt_streams/transform.inc +195 -0
  158. package/runtime/xt_streams/writable.inc +542 -0
  159. package/runtime/xt_streams.c +30 -0
  160. package/runtime/xt_wasm/wasm.h +148 -0
  161. package/runtime/xt_wasm/wasm_api.c +357 -0
  162. package/runtime/xt_wasm/wasm_api.h +37 -0
  163. package/runtime/xt_wasm/wasm_bridge.c +30 -0
  164. package/runtime/xt_wasm/wasm_instance.c +591 -0
  165. package/runtime/xt_wasm/wasm_module.c +270 -0
  166. package/runtime/xt_wasm/wasm_values.c +177 -0
  167. package/runtime/xt_webassembly.c +15 -4
  168. package/scripts/fetch-vendored.ts +187 -20
  169. package/scripts/package-release.ts +3 -2
  170. package/src/cli/extensions.ts +11 -3
  171. package/src/cli/help.ts +1 -1
  172. package/src/codegen/generator/state.ts +7 -0
  173. package/src/codegen/generator/tables.ts +19 -1
  174. package/src/driver/bundler/README.md +55 -0
  175. package/src/driver/bundler/concatenate.ts +244 -0
  176. package/src/driver/bundler/graph.ts +9 -5
  177. package/src/driver/bundler/merge.ts +24 -215
  178. package/src/driver/bundler/{namespace-import.ts → namespace-object.ts} +5 -1
  179. package/src/driver/bundler/{reexport-fixup.ts → reexport-resolution.ts} +15 -5
  180. package/src/driver/fingerprint.ts +3 -3
  181. package/src/driver/link-features.ts +1 -1
  182. package/src/driver/modules.ts +12 -5
  183. package/src/driver/paths.ts +8 -4
  184. package/src/driver/runtime-objects.ts +13 -1
  185. package/src/driver/version.ts +1 -1
  186. package/src/extensions/catalog.ts +5 -1
  187. package/src/extensions/gui/index.ts +0 -1
  188. package/src/extensions/link-feature.ts +8 -0
  189. package/src/extensions/node/stream-web/index.ts +59 -4
  190. package/src/extensions/node/worker_threads/index.ts +24 -11
  191. package/src/extensions/registry.ts +13 -2
  192. package/src/extensions/wasm-asset.ts +32 -0
  193. package/src/extensions/wasm.ts +74 -0
  194. package/src/extensions/webworker/index.ts +58 -0
  195. package/src/index.ts +1 -0
  196. package/vendor/wasm3/.pin +1 -0
  197. package/vendor/wasm3/LICENSE +21 -0
  198. package/vendor/wasm3/m3_api_libc.c +265 -0
  199. package/vendor/wasm3/m3_api_libc.h +19 -0
  200. package/vendor/wasm3/m3_api_meta_wasi.c +1107 -0
  201. package/vendor/wasm3/m3_api_tracer.c +173 -0
  202. package/vendor/wasm3/m3_api_tracer.h +19 -0
  203. package/vendor/wasm3/m3_api_uvwasi.c +1290 -0
  204. package/vendor/wasm3/m3_api_wasi.c +1207 -0
  205. package/vendor/wasm3/m3_api_wasi.h +74 -0
  206. package/vendor/wasm3/m3_api_wasi_host.h +137 -0
  207. package/vendor/wasm3/m3_api_wasi_posix.h +979 -0
  208. package/vendor/wasm3/m3_api_wasi_win32.h +1093 -0
  209. package/vendor/wasm3/m3_atomic.h +114 -0
  210. package/vendor/wasm3/m3_bind.c +174 -0
  211. package/vendor/wasm3/m3_bind.h +20 -0
  212. package/vendor/wasm3/m3_code.c +232 -0
  213. package/vendor/wasm3/m3_code.h +74 -0
  214. package/vendor/wasm3/m3_compile.c +6603 -0
  215. package/vendor/wasm3/m3_compile.h +354 -0
  216. package/vendor/wasm3/m3_config.h +448 -0
  217. package/vendor/wasm3/m3_config_platforms.h +345 -0
  218. package/vendor/wasm3/m3_core.c +1034 -0
  219. package/vendor/wasm3/m3_core.h +526 -0
  220. package/vendor/wasm3/m3_deterministic.c +65 -0
  221. package/vendor/wasm3/m3_deterministic.h +59 -0
  222. package/vendor/wasm3/m3_env.c +3146 -0
  223. package/vendor/wasm3/m3_env.h +748 -0
  224. package/vendor/wasm3/m3_exception.h +33 -0
  225. package/vendor/wasm3/m3_exec.c +8 -0
  226. package/vendor/wasm3/m3_exec.h +4110 -0
  227. package/vendor/wasm3/m3_exec_atomic.h +146 -0
  228. package/vendor/wasm3/m3_exec_defs.h +76 -0
  229. package/vendor/wasm3/m3_function.c +278 -0
  230. package/vendor/wasm3/m3_function.h +244 -0
  231. package/vendor/wasm3/m3_host.h +226 -0
  232. package/vendor/wasm3/m3_host_none.h +76 -0
  233. package/vendor/wasm3/m3_host_posix.h +543 -0
  234. package/vendor/wasm3/m3_host_win32.h +253 -0
  235. package/vendor/wasm3/m3_info.c +616 -0
  236. package/vendor/wasm3/m3_info.h +38 -0
  237. package/vendor/wasm3/m3_math_utils.h +560 -0
  238. package/vendor/wasm3/m3_module.c +431 -0
  239. package/vendor/wasm3/m3_parse.c +1549 -0
  240. package/vendor/wasm3/m3_snapshot.c +3825 -0
  241. package/vendor/wasm3/m3_validate.c +1770 -0
  242. package/vendor/wasm3/m3_validate.h +25 -0
  243. package/vendor/wasm3/m3_wasi.h +10 -0
  244. package/vendor/wasm3/m3_xxh64.c +211 -0
  245. package/vendor/wasm3/wasm3.h +684 -0
  246. package/vendor/wasm3/wasm3_defs.h +350 -0
  247. package/dist/src/driver/bundler/namespace-import.js.map +0 -1
  248. package/dist/src/driver/bundler/reexport-fixup.js.map +0 -1
  249. package/runtime/ext_node/worker_threads/worker_threads.c +0 -214
@@ -0,0 +1,270 @@
1
+ /*
2
+ * xbintsc runtime — WebAssembly module parsing and metadata.
3
+ *
4
+ * A `WebAssembly.Module` keeps its bytes because wasm3 parses lazily: the
5
+ * interpreter holds a pointer into them and compiles each function body on
6
+ * first use. Parsing here validates eagerly and copies the export/import
7
+ * descriptors the JS `Module` statics expose; the wasm3 module itself is
8
+ * discarded after inspection.
9
+ */
10
+
11
+ #include "wasm.h"
12
+
13
+ #include <stdio.h>
14
+ #include <stdlib.h>
15
+ #include <string.h>
16
+
17
+ static char *xt_wasm_dup_range(const char *text, uint32_t length) {
18
+ if (text == NULL) return NULL;
19
+ char *copy = (char *)malloc((size_t)length + 1);
20
+ if (copy == NULL) return NULL;
21
+ memcpy(copy, text, length);
22
+ copy[length] = '\0';
23
+ return copy;
24
+ }
25
+
26
+ static uint32_t xt_wasm_text_length(const char *text) {
27
+ return text == NULL ? 0 : (uint32_t)strlen(text);
28
+ }
29
+
30
+ static const char *xt_wasm_export_kind_name(uint8_t kind) {
31
+ switch (kind) {
32
+ case d_externalKind_function:
33
+ return "function";
34
+ case d_externalKind_table:
35
+ return "table";
36
+ case d_externalKind_memory:
37
+ return "memory";
38
+ case d_externalKind_global:
39
+ return "global";
40
+ case d_externalKind_tag:
41
+ return "tag";
42
+ default:
43
+ return "unknown";
44
+ }
45
+ }
46
+
47
+ static uint32_t xt_wasm_import_count(const M3Module *module) {
48
+ uint32_t count = module->numFuncImports;
49
+ for (uint32_t i = 0; i < module->numMemories; i++) {
50
+ if (module->memories[i] != NULL && module->memories[i]->imported) count++;
51
+ }
52
+ for (uint32_t i = 0; i < module->numTables; i++) {
53
+ if (module->tables[i] != NULL && module->tables[i]->imported) count++;
54
+ }
55
+ for (uint32_t i = 0; i < module->numGlobals; i++) {
56
+ if (module->globals[i].import.moduleUtf8 != NULL ||
57
+ module->globals[i].import.fieldUtf8 != NULL) {
58
+ count++;
59
+ }
60
+ }
61
+ #if d_m3HasExceptionHandling || d_m3HasStackSwitching
62
+ for (uint32_t i = 0; i < module->numTags; i++) {
63
+ if (module->tags[i].imported) count++;
64
+ }
65
+ #endif
66
+ return count;
67
+ }
68
+
69
+ static int xt_wasm_collect_imports(wasm_module *out, const M3Module *module) {
70
+ uint32_t count = xt_wasm_import_count(module);
71
+ out->imports = count == 0 ? NULL : (wasm_import_desc *)calloc(count, sizeof(wasm_import_desc));
72
+ if (count != 0 && out->imports == NULL) return 0;
73
+ uint32_t at = 0;
74
+ for (uint32_t i = 0; i < module->numFuncImports && at < count; i++) {
75
+ const M3Function *function = &module->functions[i];
76
+ if (function->import.moduleUtf8 == NULL && function->import.fieldUtf8 == NULL) continue;
77
+ out->imports[at].module = xt_wasm_dup_range(function->import.moduleUtf8,
78
+ xt_wasm_text_length(function->import.moduleUtf8));
79
+ out->imports[at].name = xt_wasm_dup_range(function->import.fieldUtf8,
80
+ xt_wasm_text_length(function->import.fieldUtf8));
81
+ out->imports[at].name_length = xt_wasm_text_length(function->import.fieldUtf8);
82
+ out->imports[at].kind = d_externalKind_function;
83
+ at++;
84
+ }
85
+ for (uint32_t i = 0; i < module->numMemories && at < count; i++) {
86
+ const M3Memory *memory = module->memories[i];
87
+ if (memory == NULL || !memory->imported) continue;
88
+ out->imports[at].module = xt_wasm_dup_range(memory->import.moduleUtf8,
89
+ xt_wasm_text_length(memory->import.moduleUtf8));
90
+ out->imports[at].name = xt_wasm_dup_range(memory->import.fieldUtf8,
91
+ xt_wasm_text_length(memory->import.fieldUtf8));
92
+ out->imports[at].name_length = xt_wasm_text_length(memory->import.fieldUtf8);
93
+ out->imports[at].kind = d_externalKind_memory;
94
+ at++;
95
+ }
96
+ for (uint32_t i = 0; i < module->numTables && at < count; i++) {
97
+ const M3Table *table = module->tables[i];
98
+ if (table == NULL || !table->imported) continue;
99
+ out->imports[at].module = xt_wasm_dup_range(table->import.moduleUtf8,
100
+ xt_wasm_text_length(table->import.moduleUtf8));
101
+ out->imports[at].name = xt_wasm_dup_range(table->import.fieldUtf8,
102
+ xt_wasm_text_length(table->import.fieldUtf8));
103
+ out->imports[at].name_length = xt_wasm_text_length(table->import.fieldUtf8);
104
+ out->imports[at].kind = d_externalKind_table;
105
+ at++;
106
+ }
107
+ for (uint32_t i = 0; i < module->numGlobals && at < count; i++) {
108
+ const M3Global *global = &module->globals[i];
109
+ if (global->import.moduleUtf8 == NULL && global->import.fieldUtf8 == NULL) continue;
110
+ out->imports[at].module = xt_wasm_dup_range(global->import.moduleUtf8,
111
+ xt_wasm_text_length(global->import.moduleUtf8));
112
+ out->imports[at].name = xt_wasm_dup_range(global->import.fieldUtf8,
113
+ xt_wasm_text_length(global->import.fieldUtf8));
114
+ out->imports[at].name_length = xt_wasm_text_length(global->import.fieldUtf8);
115
+ out->imports[at].kind = d_externalKind_global;
116
+ at++;
117
+ }
118
+ #if d_m3HasExceptionHandling || d_m3HasStackSwitching
119
+ for (uint32_t i = 0; i < module->numTags && at < count; i++) {
120
+ const M3Tag *tag = &module->tags[i];
121
+ if (!tag->imported) continue;
122
+ out->imports[at].module = xt_wasm_dup_range(tag->import.moduleUtf8,
123
+ xt_wasm_text_length(tag->import.moduleUtf8));
124
+ out->imports[at].name = xt_wasm_dup_range(tag->import.fieldUtf8,
125
+ xt_wasm_text_length(tag->import.fieldUtf8));
126
+ out->imports[at].name_length = xt_wasm_text_length(tag->import.fieldUtf8);
127
+ out->imports[at].kind = d_externalKind_tag;
128
+ at++;
129
+ }
130
+ #endif
131
+ out->import_count = at;
132
+ return 1;
133
+ }
134
+
135
+ static int xt_wasm_collect_exports(wasm_module *out, const M3Module *module) {
136
+ out->export_count = module->numExports;
137
+ out->exports = module->numExports == 0
138
+ ? NULL
139
+ : (wasm_export_desc *)calloc(module->numExports, sizeof(wasm_export_desc));
140
+ if (module->numExports != 0 && out->exports == NULL) return 0;
141
+ for (uint32_t i = 0; i < module->numExports; i++) {
142
+ const M3Export *exported = &module->exports[i];
143
+ out->exports[i].name = xt_wasm_dup_range(exported->name, exported->nameLength);
144
+ out->exports[i].name_length = exported->nameLength;
145
+ out->exports[i].kind = exported->kind;
146
+ out->exports[i].index = exported->index;
147
+ }
148
+ return 1;
149
+ }
150
+
151
+ void xt_wasm_module_release(wasm_module *module) {
152
+ if (module == NULL) return;
153
+ for (uint32_t i = 0; i < module->export_count; i++) free(module->exports[i].name);
154
+ for (uint32_t i = 0; i < module->import_count; i++) {
155
+ free(module->imports[i].module);
156
+ free(module->imports[i].name);
157
+ }
158
+ free(module->exports);
159
+ free(module->imports);
160
+ free(module->bytes);
161
+ free(module);
162
+ }
163
+
164
+ wasm_module *xt_wasm_module_parse(const uint8_t *bytes, uint32_t length, char **error) {
165
+ wasm_module *module = (wasm_module *)calloc(1, sizeof(wasm_module));
166
+ if (module == NULL) {
167
+ xt_wasm_set_error(error, "WebAssembly: out of memory");
168
+ return NULL;
169
+ }
170
+ module->bytes = (uint8_t *)malloc(length == 0 ? 1 : length);
171
+ if (module->bytes == NULL) {
172
+ xt_wasm_set_error(error, "WebAssembly: out of memory");
173
+ free(module);
174
+ return NULL;
175
+ }
176
+ memcpy(module->bytes, bytes, length);
177
+ module->length = length;
178
+
179
+ IM3Environment environment = m3_NewEnvironment();
180
+ if (environment == NULL) {
181
+ xt_wasm_set_error(error, "WebAssembly: could not create the interpreter environment");
182
+ xt_wasm_module_release(module);
183
+ return NULL;
184
+ }
185
+ IM3Module parsed = NULL;
186
+ M3Result result = m3_ParseModule(environment, &parsed, module->bytes, length);
187
+ if (result != NULL) {
188
+ xt_wasm_set_error(error, "%s", result);
189
+ m3_FreeEnvironment(environment);
190
+ xt_wasm_module_release(module);
191
+ return NULL;
192
+ }
193
+ int ok = xt_wasm_collect_exports(module, parsed) && xt_wasm_collect_imports(module, parsed);
194
+ m3_FreeModule(parsed);
195
+ m3_FreeEnvironment(environment);
196
+ if (!ok) {
197
+ xt_wasm_set_error(error, "WebAssembly: out of memory while reading module metadata");
198
+ xt_wasm_module_release(module);
199
+ return NULL;
200
+ }
201
+ return module;
202
+ }
203
+
204
+ wasm_module *xt_wasm_module_of(xt_value value) {
205
+ if (!XT_IS_OBJECT(value)) return NULL;
206
+ xt_value pointer = xt_get(value, xt_wasm_module_key());
207
+ return (wasm_module *)xt_wasm_value_ptr(pointer);
208
+ }
209
+
210
+ xt_value xt_wasm_module_object(wasm_module *module, char **error) {
211
+ (void)error;
212
+ xt_value object = xt_wasm_object_new(XT_WASM_KIND_MODULE);
213
+ xt_object_set(object, xt_wasm_module_key(), xt_wasm_ptr_value(module));
214
+ return object;
215
+ }
216
+
217
+ xt_value xt_wasm_compile_to_module(xt_value value, char **error) {
218
+ uint8_t *bytes = NULL;
219
+ uint32_t length = 0;
220
+ if (!xt_wasm_bytes_from_value(value, &bytes, &length, error)) return XT_UNDEFINED;
221
+ wasm_module *module = xt_wasm_module_parse(bytes, length, error);
222
+ free(bytes);
223
+ if (module == NULL) return XT_UNDEFINED;
224
+ return xt_wasm_module_object(module, error);
225
+ }
226
+
227
+ static xt_value xt_wasm_descriptor(const char *name, const char *kind) {
228
+ xt_value descriptor = xt_object_new();
229
+ xt_object_set(descriptor, xt_string_from_cstr("name"), xt_string_from_cstr(name));
230
+ xt_object_set(descriptor, xt_string_from_cstr("kind"), xt_string_from_cstr(kind));
231
+ return descriptor;
232
+ }
233
+
234
+ xt_value xt_wasm_module_exports_value(const wasm_module *module) {
235
+ xt_value result = xt_array_new(0, NULL);
236
+ for (uint32_t i = 0; i < module->export_count; i++) {
237
+ xt_value descriptor = xt_wasm_descriptor(module->exports[i].name,
238
+ xt_wasm_export_kind_name(module->exports[i].kind));
239
+ xt_array_push(result, descriptor);
240
+ }
241
+ return result;
242
+ }
243
+
244
+ xt_value xt_wasm_module_imports_value(const wasm_module *module) {
245
+ xt_value result = xt_array_new(0, NULL);
246
+ for (uint32_t i = 0; i < module->import_count; i++) {
247
+ /* The spec order is module, name, kind; keeping it also makes the value
248
+ * comparable with Node's `JSON.stringify(WebAssembly.Module.imports(...))`. */
249
+ xt_value descriptor = xt_object_new();
250
+ xt_object_set(descriptor, xt_string_from_cstr("module"),
251
+ module->imports[i].module == NULL
252
+ ? XT_UNDEFINED
253
+ : xt_string_from_cstr(module->imports[i].module));
254
+ xt_object_set(descriptor, xt_string_from_cstr("name"),
255
+ xt_string_from_cstr(module->imports[i].name));
256
+ xt_object_set(descriptor, xt_string_from_cstr("kind"),
257
+ xt_string_from_cstr(xt_wasm_export_kind_name(module->imports[i].kind)));
258
+ xt_array_push(result, descriptor);
259
+ }
260
+ return result;
261
+ }
262
+
263
+ const wasm_export_desc *xt_wasm_module_find_export(const wasm_module *module,
264
+ const char *name) {
265
+ if (module == NULL || name == NULL) return NULL;
266
+ for (uint32_t i = 0; i < module->export_count; i++) {
267
+ if (strcmp(module->exports[i].name, name) == 0) return &module->exports[i];
268
+ }
269
+ return NULL;
270
+ }
@@ -0,0 +1,177 @@
1
+ /*
2
+ * xbintsc runtime — value conversions shared by the WebAssembly bridge.
3
+ */
4
+
5
+ #include "wasm.h"
6
+
7
+ #include <inttypes.h>
8
+ #include <stdio.h>
9
+ #include <stdlib.h>
10
+ #include <string.h>
11
+
12
+ void xt_wasm_set_error(char **error, const char *format, ...) {
13
+ if (error == NULL) return;
14
+ free(*error);
15
+ *error = NULL;
16
+ va_list args;
17
+ va_start(args, format);
18
+ va_list copy;
19
+ va_copy(copy, args);
20
+ int length = vsnprintf(NULL, 0, format, copy);
21
+ va_end(copy);
22
+ if (length < 0) {
23
+ va_end(args);
24
+ return;
25
+ }
26
+ char *buffer = (char *)malloc((size_t)length + 1);
27
+ if (buffer != NULL) vsnprintf(buffer, (size_t)length + 1, format, args);
28
+ va_end(args);
29
+ *error = buffer;
30
+ }
31
+
32
+ xt_value xt_wasm_error(const char *kind, const char *message) {
33
+ if (message == NULL) message = "unknown error";
34
+ size_t kindLength = strlen(kind);
35
+ size_t messageLength = strlen(message);
36
+ char *text = (char *)malloc(kindLength + 2 + messageLength + 1);
37
+ if (text == NULL) return xt_error_from_message(kind);
38
+ memcpy(text, kind, kindLength);
39
+ memcpy(text + kindLength, ": ", 2);
40
+ memcpy(text + kindLength + 2, message, messageLength + 1);
41
+ xt_value error = xt_error_from_message(text);
42
+ free(text);
43
+ return error;
44
+ }
45
+
46
+ xt_value xt_wasm_type_error(const char *message) {
47
+ xt_value argument = xt_string_from_cstr(message);
48
+ return xt_type_error_ctor(1, &argument);
49
+ }
50
+
51
+ static uint32_t xt_wasm_typed_array_width(int kind) {
52
+ switch (kind) {
53
+ case 0:
54
+ case 1:
55
+ case 2:
56
+ return 1;
57
+ case 3:
58
+ case 4:
59
+ return 2;
60
+ case 5:
61
+ case 6:
62
+ case 7:
63
+ return 4;
64
+ case 8:
65
+ return 8;
66
+ default:
67
+ return 0;
68
+ }
69
+ }
70
+
71
+ int xt_wasm_is_buffer_source(xt_value value) {
72
+ if (xt_is_array_buffer(value) || xt_typed_array_kind_of(value) >= 0) return 1;
73
+ if (XT_IS_OBJECT(value)) {
74
+ /* DataView (a plain object that names its backing ArrayBuffer). */
75
+ if (xt_is_array_buffer(xt_get(value, xt_string_from_cstr("buffer")))) return 1;
76
+ /* xbintsc's Node Buffer is a plain object with indexed bytes and a
77
+ * `length` property, not a typed array. Accept that shape too so the
78
+ * common `readFileSync`/`Buffer.from` input works. */
79
+ xt_value length = xt_get(value, xt_string_from_cstr("length"));
80
+ return XT_IS_NUMBER(length);
81
+ }
82
+ return 0;
83
+ }
84
+
85
+ int xt_wasm_bytes_from_value(xt_value value, uint8_t **bytes, uint32_t *length,
86
+ char **error) {
87
+ *bytes = NULL;
88
+ *length = 0;
89
+ uint32_t byteLength = 0;
90
+ uint32_t byteOffset = 0;
91
+ xt_value buffer = value;
92
+ int bufferLike = 0;
93
+ if (xt_is_array_buffer(value)) {
94
+ byteLength = xt_array_buffer_byte_length(value);
95
+ } else {
96
+ int kind = xt_typed_array_kind_of(value);
97
+ uint32_t width = kind >= 0 ? xt_wasm_typed_array_width(kind) : 0;
98
+ if (kind >= 0 && width != 0) {
99
+ buffer = xt_typed_array_backing_buffer(value);
100
+ byteOffset = xt_typed_array_view_offset(value);
101
+ byteLength = xt_typed_array_length(value) * width;
102
+ } else if (XT_IS_OBJECT(value) &&
103
+ xt_is_array_buffer(xt_get(value, xt_string_from_cstr("buffer")))) {
104
+ buffer = xt_get(value, xt_string_from_cstr("buffer"));
105
+ xt_value offset = xt_get(value, xt_string_from_cstr("byteOffset"));
106
+ xt_value viewLength = xt_get(value, xt_string_from_cstr("byteLength"));
107
+ byteOffset = XT_IS_NUMBER(offset) ? (uint32_t)xt_to_int32(offset) : 0;
108
+ byteLength = XT_IS_NUMBER(viewLength) ? (uint32_t)xt_to_int32(viewLength) : 0;
109
+ } else if (XT_IS_OBJECT(value)) {
110
+ xt_value lengthValue = xt_get(value, xt_string_from_cstr("length"));
111
+ if (!XT_IS_NUMBER(lengthValue)) {
112
+ xt_wasm_set_error(error, "WebAssembly: argument must be a BufferSource");
113
+ return 0;
114
+ }
115
+ byteLength = (uint32_t)xt_to_int32(lengthValue);
116
+ bufferLike = 1;
117
+ } else {
118
+ xt_wasm_set_error(error, "WebAssembly: argument must be a BufferSource");
119
+ return 0;
120
+ }
121
+ }
122
+ uint8_t *copy = (uint8_t *)malloc(byteLength == 0 ? 1 : byteLength);
123
+ if (copy == NULL) {
124
+ xt_wasm_set_error(error, "WebAssembly: out of memory while copying the module");
125
+ return 0;
126
+ }
127
+ if (bufferLike) {
128
+ for (uint32_t i = 0; i < byteLength; i++) {
129
+ copy[i] = (uint8_t)(int32_t)xt_to_number(
130
+ xt_get(value, xt_number((double)i)));
131
+ }
132
+ } else {
133
+ uint32_t copied = xt_array_buffer_copy_bytes_out(buffer, byteOffset, copy, byteLength);
134
+ if (copied != byteLength) {
135
+ free(copy);
136
+ xt_wasm_set_error(error, "WebAssembly: could not read the module bytes");
137
+ return 0;
138
+ }
139
+ }
140
+ *bytes = copy;
141
+ *length = byteLength;
142
+ return 1;
143
+ }
144
+
145
+ int64_t xt_wasm_to_i64(xt_value value) {
146
+ if (!XT_IS_BIGINT(value)) {
147
+ xt_throw(xt_wasm_type_error("WebAssembly: i64 arguments must be BigInt values"));
148
+ return 0;
149
+ }
150
+ xt_value text = xt_to_string(value);
151
+ const char *digits = xt_string_data(text);
152
+ if (digits == NULL) return 0;
153
+ int negative = digits[0] == '-';
154
+ if (negative) digits++;
155
+ uint64_t magnitude = 0;
156
+ for (const char *cursor = digits; *cursor >= '0' && *cursor <= '9'; cursor++) {
157
+ magnitude = magnitude * 10u + (uint64_t)(*cursor - '0');
158
+ }
159
+ return (int64_t)(negative ? (uint64_t)0 - magnitude : magnitude);
160
+ }
161
+
162
+ xt_value xt_wasm_from_i64(int64_t value) {
163
+ char text[32];
164
+ int length = snprintf(text, sizeof(text), "%" PRId64, value);
165
+ if (length < 0) return xt_bigint_from_string("0", 1);
166
+ return xt_bigint_from_string(text, (size_t)length);
167
+ }
168
+
169
+ xt_value xt_wasm_number_arg(int32_t argc, xt_value *argv, int32_t index) {
170
+ return xt_arg_at(argc, argv, index);
171
+ }
172
+
173
+ xt_value xt_wasm_object_arg(int32_t argc, xt_value *argv, int32_t index) {
174
+ xt_value value = xt_arg_at(argc, argv, index);
175
+ if (XT_IS_OBJECT(value) || XT_IS_ARRAY(value) || XT_IS_FUNCTION(value)) return value;
176
+ return XT_UNDEFINED;
177
+ }
@@ -1,16 +1,25 @@
1
1
  /*
2
2
  * xbintsc runtime — the `WebAssembly` namespace.
3
3
  *
4
- * The WebAssembly JavaScript API surface is recognized (so code that reaches
5
- * for `WebAssembly.compile`, `new WebAssembly.Module`, … compiles), but this
6
- * build has no WebAssembly interpreter: every entry point throws a clear
7
- * "not supported" error instead of executing bytecode.
4
+ * The namespace object and the dispatch entry points are core: every build
5
+ * recognises `WebAssembly`, so code that references it still compiles. The
6
+ * interpreter behind them is an optional link feature (`wasm`): its bridge
7
+ * installs `XtWasmApi` from a constructor before `main`. Without the feature
8
+ * the pointers are NULL and every entry point throws the original "not
9
+ * supported in this build" error, which is the behaviour a `--no-feature wasm`
10
+ * build intentionally keeps.
8
11
  */
9
12
 
10
13
  #include "rt_internal.h"
11
14
 
15
+ #include "xt_wasm/wasm_api.h"
16
+
12
17
  #include <string.h>
13
18
 
19
+ static const XtWasmApi *g_wasm_api = NULL;
20
+
21
+ void xt_wasm_install(const XtWasmApi *api) { g_wasm_api = api; }
22
+
14
23
  static xt_value xt_wasm_unsupported(xt_value thisValue, xt_value env, int32_t argc,
15
24
  xt_value *argv) {
16
25
  (void)thisValue;
@@ -22,6 +31,7 @@ static xt_value xt_wasm_unsupported(xt_value thisValue, xt_value env, int32_t ar
22
31
  }
23
32
 
24
33
  xt_value xt_webassembly_static(xt_value name, int32_t argc, xt_value *argv) {
34
+ if (g_wasm_api != NULL) return g_wasm_api->dispatch(name, argc, argv);
25
35
  (void)name;
26
36
  (void)argc;
27
37
  (void)argv;
@@ -30,6 +40,7 @@ xt_value xt_webassembly_static(xt_value name, int32_t argc, xt_value *argv) {
30
40
  }
31
41
 
32
42
  xt_value xt_webassembly_object(int32_t argc, xt_value *argv) {
43
+ if (g_wasm_api != NULL) return g_wasm_api->object(argc, argv);
33
44
  (void)argc;
34
45
  (void)argv;
35
46
  xt_value object = xt_object_new();