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,1549 @@
1
+ //
2
+ // m3_parse.c
3
+ //
4
+ // Created by Steven Massey on 4/19/19.
5
+ // Copyright © 2019 Steven Massey. All rights reserved.
6
+ //
7
+
8
+ #include "m3_env.h"
9
+ #include "m3_compile.h"
10
+ #include "m3_exception.h"
11
+ #include "m3_info.h"
12
+
13
+
14
+ // elem type + limits, shared by the table section and by table imports.
15
+ // The declared minimum is the table's initial size; table.grow can raise it up
16
+ // to the maximum, so an element segment reaching past the current size is out
17
+ // of bounds.
18
+ #if d_m3HasTypedRefs
19
+
20
+ // Reads a heap type: 'func', 'extern', or the index of a function type the
21
+ // module has already defined. Yields the heap-type bits of an m3type_t, which
22
+ // carry the canonical index rather than the module's own, so that structurally
23
+ // equal types compare equal across modules.
24
+ M3Result ParseHeapType (IM3Module i_module, m3type_t* o_heapBits, bytes_t* io_bytes, cbytes_t i_end)
25
+ {
26
+ M3Result result = m3Err_none;
27
+
28
+ i64 heap;
29
+ _ (ReadLebSigned(&heap, 33, io_bytes, i_end));
30
+
31
+ if (heap == -d_waType_funcref) {
32
+ *o_heapBits = d_m3Type_heapAbstract;
33
+ } else if (heap == -d_waType_externref) {
34
+ *o_heapBits = d_m3Type_refExtern | d_m3Type_heapAbstract;
35
+ # if d_m3HasStackSwitching
36
+ } else if (heap == -d_waType_heap_cont) {
37
+ *o_heapBits = d_m3Type_refCont | d_m3Type_heapAbstract;
38
+ } else if (heap == -d_waType_heap_nocont) {
39
+ *o_heapBits = d_m3Type_refCont | d_m3Type_heapNone;
40
+ # endif
41
+ } else if (heap >= 0) {
42
+ _throwif(m3Err_wasmMalformed, not i_module or (u64) heap >= i_module->numFuncTypes);
43
+
44
+ IM3FuncType ftype = i_module->funcTypes[heap];
45
+
46
+ // null while the type section is still being read: a type may only name
47
+ // one that precedes it, recursion belongs to a later proposal
48
+ _throwif(m3Err_wasmMalformed, not ftype);
49
+
50
+ *o_heapBits = (ftype->isContinuation ? d_m3Type_refCont : 0) | ftype->canonicalIndex;
51
+ } else {
52
+ _throw(m3Err_invalidTypeId);
53
+ }
54
+
55
+ _catch: return result;
56
+ }
57
+
58
+ #endif // d_m3HasTypedRefs
59
+
60
+
61
+ // Reads a value type, including the reference types the function references
62
+ // proposal spells out in full: (ref ht) and (ref null ht).
63
+ M3Result ParseValueType (IM3Module i_module, m3type_t* o_type, bytes_t* io_bytes, cbytes_t i_end)
64
+ {
65
+ M3Result result = m3Err_none;
66
+
67
+ _throwif(m3Err_wasmUnderrun, *io_bytes >= i_end);
68
+
69
+ #if d_m3HasTypedRefs
70
+ if (**io_bytes == d_waEncode_ref or **io_bytes == d_waEncode_refNull) {
71
+ u8 lead = *(*io_bytes)++;
72
+
73
+ m3type_t heapBits = d_m3Type_heapAbstract;
74
+ _ (ParseHeapType(i_module, &heapBits, io_bytes, i_end));
75
+
76
+ *o_type = d_m3Type_ref | heapBits | ((lead == d_waEncode_ref) ? d_m3Type_refNonNull : 0);
77
+
78
+ return result;
79
+ }
80
+ #endif
81
+
82
+ i8 wasmType;
83
+ u8 plainType;
84
+ _ (ReadLEB_i7(&wasmType, io_bytes, i_end));
85
+ _ (NormalizeType(&plainType, wasmType));
86
+
87
+ // 0x40 is the empty block type, not a value type. NormalizeType has to
88
+ // accept it for the block forms, so every valtype position turns it away here.
89
+ _throwif(m3Err_invalidTypeId, plainType == c_m3Type_none);
90
+
91
+ *o_type = plainType;
92
+
93
+ _catch: return result;
94
+ }
95
+
96
+
97
+ // Reads an elem type and limits without registering anything: an import
98
+ // descriptor holds on to the fields and only adds the table once it has a name.
99
+ static
100
+ M3Result ReadType_TableType (IM3Module i_module, M3TableInfo* o_info, bytes_t* io_bytes, cbytes_t i_end)
101
+ {
102
+ M3Result result = m3Err_none;
103
+
104
+ m3type_t elemType = c_m3Type_none;
105
+ u8 flag = 0;
106
+ u64 initSize = 0, maxSize = 0;
107
+
108
+ // the width of the limits, and of every index into the table
109
+ u32 addrBits = 32;
110
+
111
+ _ (ParseValueType(i_module, &elemType, io_bytes, i_end));
112
+ #if d_m3HasRefTypes
113
+ _throwif(m3Err_wasmMalformed, not IsRefType(BaseTypeOf(elemType)));
114
+ #else
115
+ _throwif(m3Err_wasmMalformed, elemType != c_m3Type_funcref);
116
+ #endif
117
+
118
+ _ (ReadLEB_u7(&flag, io_bytes, i_end));
119
+
120
+ // bit 0: has max, bit 2: table64 - the same flag memory64 gives a memory
121
+ #if d_m3HasMemory64
122
+ _throwif(m3Err_wasmMalformed, flag & ~0x05u);
123
+ #else
124
+ _throwif(m3Err_wasmMalformed, flag & ~0x01u);
125
+ #endif
126
+
127
+ if (flag & 0x04u) {
128
+ addrBits = 64;
129
+ }
130
+
131
+ _ (ReadLebUnsigned(&initSize, addrBits, io_bytes, i_end));
132
+ _throwif("table overflow", initSize > d_m3MaxSaneTableSize);
133
+
134
+ if (flag & 0x01u) {
135
+ _ (ReadLebUnsigned(&maxSize, addrBits, io_bytes, i_end));
136
+ _throwif(m3Err_wasmMalformed, maxSize < initSize);
137
+ _throwif("table overflow", maxSize > d_m3MaxSaneTableSize);
138
+ }
139
+
140
+ _catch:
141
+
142
+ o_info->elemType = elemType;
143
+ o_info->initSize = (u32)initSize;
144
+ o_info->maxSize = (u32)maxSize;
145
+ o_info->hasMax = (flag & 0x01u) != 0;
146
+ o_info->isTable64 = (flag & 0x04u) != 0;
147
+
148
+ return result;
149
+ }
150
+
151
+
152
+ static
153
+ M3Result ParseType_TableType (IM3Module io_module, bytes_t* io_bytes, cbytes_t i_end)
154
+ {
155
+ M3Result result = m3Err_none;
156
+
157
+ M3TableInfo info;
158
+
159
+ _ (ReadType_TableType(io_module, &info, io_bytes, i_end));
160
+ _ (Module_AddTable(io_module, NULL, &info, false /* isImport */));
161
+
162
+ _catch: return result;
163
+ }
164
+
165
+
166
+ M3Result Parse_InitExprTyped (M3Module* io_module, bytes_t* io_bytes, cbytes_t i_end, m3type_t* o_type);
167
+
168
+ M3Result ParseType_Table (IM3Module io_module, bytes_t i_bytes, cbytes_t i_end)
169
+ {
170
+ M3Result result = m3Err_none;
171
+
172
+ u32 numTables;
173
+ _ (ReadLEB_u32(&numTables, &i_bytes, i_end)); m3log (parse, "** Table [%d]", numTables);
174
+
175
+ _throwif("too many tables", (u64)numTables + io_module->numTables > d_m3MaxSaneTableCount);
176
+
177
+ for (u32 i = 0; i < numTables; ++i) {
178
+ #if d_m3HasTypedRefs
179
+ // function-references: 0x40 0x00 introduces a table with an explicit
180
+ // initializer. A reference type never starts with 0x40, so the two
181
+ // forms are told apart by the first byte; the second is reserved.
182
+ bool hasInitExpr = (i_bytes < i_end) and (*i_bytes == 0x40);
183
+ u32 tableIndex = io_module->numTables;
184
+
185
+ if (hasInitExpr) {
186
+ u8 reserved;
187
+ ++i_bytes;
188
+ _ (Read_u8(&reserved, &i_bytes, i_end));
189
+ _throwif(m3Err_wasmMalformed, reserved != 0x00);
190
+ }
191
+
192
+ _ (ParseType_TableType(io_module, &i_bytes, i_end));
193
+
194
+ if (hasInitExpr) {
195
+ IM3Table table = io_module->tables[tableIndex];
196
+
197
+ m3type_t initType;
198
+
199
+ table->initExpr = i_bytes;
200
+ _ (Parse_InitExprTyped(io_module, &i_bytes, i_end, &initType));
201
+ table->initExprSize = (u32)(i_bytes - table->initExpr);
202
+ _throwif(m3Err_wasmMissingInitExpr, table->initExprSize <= 1);
203
+
204
+ _throwif(m3Err_typeMismatch, not IsSubTypeOf(initType, table->type));
205
+ } else {
206
+ // no initializer means null, which only a nullable type can hold
207
+ _throwif("table of non-nullable type requires an initializer",
208
+ not IsNullableRef(io_module->tables[tableIndex]->type));
209
+ }
210
+ #else
211
+ _ (ParseType_TableType(io_module, &i_bytes, i_end));
212
+ #endif
213
+ }
214
+
215
+ _throwif(m3Err_wasmMalformed, i_bytes != i_end); // section size mismatch
216
+
217
+ _catch: return result;
218
+ }
219
+
220
+
221
+ M3Result ParseType_Memory (M3MemoryInfo* o_memory, bytes_t* io_bytes, cbytes_t i_end)
222
+ {
223
+ M3Result result = m3Err_none;
224
+
225
+ u8 flag;
226
+
227
+ // The custom page sizes proposal admits only 1 and 65536, but nothing in
228
+ // the engine cares which power of two it is, so any of them is accepted.
229
+ // Declared up here so the throws below don't jump over its initialization.
230
+ u32 logPageSize = 16;
231
+
232
+ // The width of the limits, and of everything that addresses the memory:
233
+ // 32 unless bit 2 says memory64.
234
+ u32 addrBits = 32;
235
+
236
+ _ (ReadLEB_u7(&flag, io_bytes, i_end));
237
+
238
+ // bit 0: has max, bit 1: shared, bit 2: memory64, bit 3: custom page size
239
+ {
240
+ u32 known = 0x09u;
241
+ #if d_m3HasAtomics
242
+ known |= 0x02u;
243
+ #endif
244
+ #if d_m3HasMemory64
245
+ known |= 0x04u;
246
+ #endif
247
+ _throwif(m3Err_wasmMalformed, flag & ~known);
248
+ }
249
+
250
+ o_memory->isShared = (flag & (1u << 1)) != 0;
251
+
252
+ o_memory->isMemory64 = (flag & (1u << 2)) != 0;
253
+ if (o_memory->isMemory64) {
254
+ addrBits = 64;
255
+ }
256
+
257
+ _ (ReadLebUnsigned(&o_memory->initPages, addrBits, io_bytes, i_end));
258
+
259
+ o_memory->maxPages = 0;
260
+ o_memory->hasMax = (flag & (1u << 0)) != 0;
261
+ if (o_memory->hasMax) {
262
+ _ (ReadLebUnsigned(&o_memory->maxPages, addrBits, io_bytes, i_end));
263
+
264
+ // Spec: memory limits validation - max must not be less than init
265
+ _throwif(m3Err_wasmMalformed, o_memory->maxPages < o_memory->initPages);
266
+ }
267
+
268
+ _throwif("shared memory must have maximum", o_memory->isShared and not o_memory->hasMax);
269
+
270
+ o_memory->pageSize = 0;
271
+ if (flag & (1u << 3)) {
272
+ _ (ReadLEB_u32(&logPageSize, io_bytes, i_end));
273
+
274
+ _throwif("invalid custom page size", logPageSize > 16);
275
+
276
+ o_memory->pageSize = 1u << logPageSize;
277
+ }
278
+
279
+ // Spec: memory limits must be valid within range 2^|addrtype|/pagesize.
280
+ // That is 65536 pages at the default page size, 2^48 for a 64-bit memory,
281
+ // and a whole address space of them when a page is a single byte.
282
+ {
283
+ u64 maxPagesAllowed = (logPageSize or addrBits < 64)
284
+ ? (UINT64_C(1) << (addrBits - logPageSize))
285
+ : UINT64_MAX;
286
+
287
+ _throwif(m3Err_wasmMalformed, o_memory->initPages > maxPagesAllowed);
288
+ if (o_memory->hasMax) {
289
+ _throwif(m3Err_wasmMalformed, o_memory->maxPages > maxPagesAllowed);
290
+ }
291
+ }
292
+
293
+ _catch: return result;
294
+ }
295
+
296
+
297
+ // One func type: [numArgs] argtypes [numRets] rettypes, with the results
298
+ // stored ahead of the arguments the way M3FuncType wants them.
299
+ static
300
+ M3Result ParseFuncType (IM3Module io_module, IM3FuncType* o_type, bytes_t* io_bytes, cbytes_t i_end)
301
+ {
302
+ IM3FuncType ftype = NULL;
303
+ _try {
304
+ u32 numArgs;
305
+ _ (ReadLEB_u32(&numArgs, io_bytes, i_end));
306
+
307
+ _throwif(m3Err_tooManyArgsRets, numArgs > d_m3MaxSaneFunctionArgRetCount);
308
+ #if M3_HAS_VLA
309
+ m3type_t argTypes[numArgs + 1]; // make ubsan happy
310
+ #else
311
+ m3type_t argTypes[d_m3MaxSaneFunctionArgRetCount];
312
+ #endif
313
+ for (u32 a = 0; a < numArgs; ++a) {
314
+ _ (ParseValueType(io_module, &argTypes[a], io_bytes, i_end));
315
+ }
316
+
317
+ u32 numRets;
318
+ _ (ReadLEB_u32(&numRets, io_bytes, i_end));
319
+ _throwif(m3Err_tooManyArgsRets, (u64)(numRets) + numArgs > d_m3MaxSaneFunctionArgRetCount);
320
+
321
+ _ (AllocFuncType(&ftype, numRets + numArgs));
322
+ ftype->numArgs = numArgs;
323
+ ftype->numRets = numRets;
324
+
325
+ for (u32 r = 0; r < numRets; ++r) {
326
+ _ (ParseValueType(io_module, &ftype->types[r], io_bytes, i_end));
327
+ }
328
+ memcpy(ftype->types + numRets, argTypes, numArgs * sizeof(m3type_t));
329
+
330
+ *o_type = ftype;
331
+ ftype = NULL;
332
+
333
+ } _catch:
334
+ m3_Free(ftype);
335
+ return result;
336
+ }
337
+
338
+
339
+ #if d_m3HasStackSwitching
340
+
341
+ // cont $ft. A continuation carries the shape of the function it runs, so that
342
+ // resume and the rest can read the parameters and results straight off it.
343
+ static
344
+ M3Result ParseContType (IM3Module io_module, IM3FuncType* o_type, bytes_t* io_bytes, cbytes_t i_end)
345
+ {
346
+ IM3FuncType ftype = NULL;
347
+ _try {
348
+ i64 ftIndex;
349
+ _ (ReadLebSigned(&ftIndex, 33, io_bytes, i_end));
350
+ _throwif(m3Err_wasmMalformed, ftIndex < 0 or (u64) ftIndex >= io_module->numFuncTypes);
351
+
352
+ IM3FuncType underlying = io_module->funcTypes[ftIndex];
353
+ _throwif(m3Err_wasmMalformed, not underlying or underlying->isContinuation);
354
+
355
+ u32 total = underlying->numRets + underlying->numArgs;
356
+ _ (AllocFuncType(&ftype, total));
357
+ ftype->numRets = underlying->numRets;
358
+ ftype->numArgs = underlying->numArgs;
359
+ ftype->isContinuation = true;
360
+ ftype->contFuncType = underlying;
361
+
362
+ if (total) {
363
+ memcpy(ftype->types, underlying->types, total * sizeof(m3type_t));
364
+ }
365
+
366
+ *o_type = ftype;
367
+ ftype = NULL;
368
+
369
+ } _catch:
370
+ m3_Free(ftype);
371
+ return result;
372
+ }
373
+
374
+ #endif // d_m3HasStackSwitching
375
+
376
+
377
+ static
378
+ M3Result ParseTypeDefinition (IM3Module io_module, i8 i_form, IM3FuncType* o_type,
379
+ bytes_t* io_bytes, cbytes_t i_end)
380
+ {
381
+ #if d_m3HasStackSwitching
382
+ if (i_form == -35) {
383
+ return ParseContType(io_module, o_type, io_bytes, i_end);
384
+ }
385
+ #endif
386
+ if (i_form != -32) { // for Wasm MVP
387
+ return m3Err_wasmMalformed;
388
+ }
389
+
390
+ return ParseFuncType(io_module, o_type, io_bytes, i_end);
391
+ }
392
+
393
+
394
+ #if d_m3HasStackSwitching
395
+
396
+ // Walks one type definition without resolving anything, which is how the first
397
+ // pass over a recursive group learns what each member is before any of them can
398
+ // be built: the members are free to name each other, so nothing can be resolved
399
+ // until every index in the group is spoken for.
400
+ static
401
+ M3Result SkipTypeDefinition (i8 i_form, bytes_t* io_bytes, cbytes_t i_end)
402
+ {
403
+ _try {
404
+ if (i_form == -35) {
405
+ i64 ftIndex;
406
+ _ (ReadLebSigned(&ftIndex, 33, io_bytes, i_end));
407
+ return result;
408
+ }
409
+
410
+ _throwif(m3Err_wasmMalformed, i_form != -32);
411
+
412
+ for (u32 half = 0; half < 2; ++half) {
413
+ u32 count;
414
+ _ (ReadLEB_u32(&count, io_bytes, i_end));
415
+ _throwif(m3Err_tooManyArgsRets, count > d_m3MaxSaneFunctionArgRetCount);
416
+
417
+ for (u32 v = 0; v < count; ++v) {
418
+ u8 encoding;
419
+ _ (Read_u8(&encoding, io_bytes, i_end));
420
+
421
+ if (encoding == d_waEncode_ref or encoding == d_waEncode_refNull) {
422
+ i64 heap;
423
+ _ (ReadLebSigned(&heap, 33, io_bytes, i_end));
424
+ }
425
+ }
426
+ }
427
+
428
+ } _catch: return result;
429
+ }
430
+
431
+
432
+ // A recursive type group. Its members may name each other, in either order, so
433
+ // the indices they will occupy have to exist before any body is read: the group
434
+ // is scanned once for shapes, its indices are reserved, and only then are the
435
+ // definitions built - func types first, because a cont type is spelled in terms
436
+ // of one.
437
+ static
438
+ M3Result ParseRecGroup (IM3Module io_module, u32 i_count, u32 i_firstIndex,
439
+ bytes_t* io_bytes, cbytes_t i_end)
440
+ {
441
+ IM3FuncType* placeholders = NULL;
442
+ _try {
443
+ bytes_t groupStart = *io_bytes;
444
+
445
+ placeholders = m3_AllocArray(IM3FuncType, i_count);
446
+ _throwifnull(placeholders);
447
+
448
+ // pass one: what each member is, without looking at what it refers to
449
+ for (u32 j = 0; j < i_count; ++j) {
450
+ i8 form;
451
+ _ (ReadLEB_i7(&form, io_bytes, i_end));
452
+ _ (SkipTypeDefinition(form, io_bytes, i_end));
453
+
454
+ _ (AllocFuncType(&placeholders[j], 0));
455
+ placeholders[j]->isContinuation = (form == -35);
456
+ }
457
+
458
+ u16 firstCanonical;
459
+ _ (Environment_ReserveFuncTypes(io_module->environment, i_count, &firstCanonical));
460
+
461
+ for (u32 j = 0; j < i_count; ++j) {
462
+ placeholders[j]->canonicalIndex = (u16)(firstCanonical + j);
463
+ io_module->funcTypes[i_firstIndex + j] = placeholders[j];
464
+ }
465
+
466
+ // pass two: the definitions themselves. A cont type copies the shape of the
467
+ // func type it names, so every func type in the group has to be standing
468
+ // before any cont type in it is read.
469
+ for (u32 pass = 0; pass < 2; ++pass) {
470
+ bytes_t bytes = groupStart;
471
+
472
+ for (u32 j = 0; j < i_count; ++j) {
473
+ i8 form;
474
+ _ (ReadLEB_i7(&form, &bytes, i_end));
475
+
476
+ if ((form == -35) != (pass == 1)) {
477
+ _ (SkipTypeDefinition(form, &bytes, i_end));
478
+ continue;
479
+ }
480
+
481
+ IM3FuncType ftype = NULL;
482
+ _ (ParseTypeDefinition(io_module, form, &ftype, &bytes, i_end));
483
+
484
+ Environment_AdoptFuncType(io_module->environment, ftype, placeholders[j]->canonicalIndex);
485
+
486
+ m3_Free(placeholders[j]);
487
+ placeholders[j] = NULL;
488
+ io_module->funcTypes[i_firstIndex + j] = ftype;
489
+ }
490
+
491
+ if (pass == 1) {
492
+ *io_bytes = bytes;
493
+ }
494
+ }
495
+
496
+ } _catch:
497
+ if (placeholders) {
498
+ for (u32 j = 0; j < i_count; ++j) {
499
+ m3_Free(placeholders[j]);
500
+ }
501
+ m3_Free(placeholders);
502
+ }
503
+ return result;
504
+ }
505
+
506
+ #endif // d_m3HasStackSwitching
507
+
508
+
509
+ M3Result ParseSection_Type (IM3Module io_module, bytes_t i_bytes, cbytes_t i_end)
510
+ {
511
+ IM3FuncType ftype = NULL;
512
+
513
+ _try {
514
+ u32 numTypes;
515
+ _ (ReadLEB_u32(&numTypes, &i_bytes, i_end)); m3log (parse, "** Type [%d]", numTypes);
516
+
517
+ _throwif("too many types", numTypes > d_m3MaxSaneTypesCount);
518
+
519
+ if (numTypes) {
520
+ // table of IM3FuncType (that point to the actual M3FuncType struct in the Environment)
521
+ io_module->funcTypes = m3_AllocArray(IM3FuncType, numTypes);
522
+ _throwifnull(io_module->funcTypes);
523
+ io_module->numFuncTypes = numTypes;
524
+
525
+ // a recursive group is one entry of the section but any number of type
526
+ // indices, so the two counts part ways as soon as one appears
527
+ #if d_m3HasStackSwitching
528
+ u32 capacity = numTypes;
529
+ #endif
530
+ u32 index = 0;
531
+
532
+ for (u32 i = 0; i < numTypes; ++i) {
533
+ i8 form;
534
+ _ (ReadLEB_i7(&form, &i_bytes, i_end));
535
+
536
+ #if d_m3HasStackSwitching
537
+ if (form == -50) { // rec
538
+ u32 count;
539
+ _ (ReadLEB_u32(&count, &i_bytes, i_end));
540
+
541
+ // room for this group, and for one more entry per section
542
+ // entry still to come
543
+ u64 needed = (u64)index + count + (numTypes - 1 - i);
544
+
545
+ _throwif("too many types", needed > d_m3MaxSaneTypesCount);
546
+
547
+ if (needed > capacity) {
548
+ u32 grown = (u32)needed;
549
+
550
+ IM3FuncType* types =
551
+ m3_ReallocArray(IM3FuncType, io_module->funcTypes, grown, capacity);
552
+ _throwifnull(types);
553
+
554
+ io_module->funcTypes = types;
555
+ capacity = grown;
556
+ io_module->numFuncTypes = grown;
557
+ }
558
+
559
+ // a group of one says nothing a plain definition does not, and
560
+ // has to be able to share an entry with one
561
+ if (count == 1) {
562
+ _ (ReadLEB_i7(&form, &i_bytes, i_end));
563
+ } else {
564
+ _ (ParseRecGroup(io_module, count, index, &i_bytes, i_end));
565
+ index += count;
566
+ continue;
567
+ }
568
+ }
569
+ #endif
570
+
571
+ _ (ParseTypeDefinition(io_module, form, &ftype, &i_bytes, i_end)); m3log (parse, " type %2d: %s", index, SPrintFuncTypeSignature (ftype));
572
+
573
+ Environment_AddFuncType(io_module->environment, &ftype);
574
+ io_module->funcTypes[index++] = ftype;
575
+ ftype = NULL; // ownership transferred to environment
576
+ }
577
+
578
+ io_module->numFuncTypes = index;
579
+ }
580
+
581
+ _throwif(m3Err_wasmMalformed, i_bytes != i_end); // section size mismatch
582
+
583
+ } _catch:
584
+
585
+ if (result) {
586
+ m3_Free(ftype);
587
+ // FIX: M3FuncTypes in the table are leaked
588
+ m3_Free(io_module->funcTypes);
589
+ io_module->numFuncTypes = 0;
590
+ }
591
+
592
+ return result;
593
+ }
594
+
595
+
596
+ M3Result ParseSection_Function (IM3Module io_module, bytes_t i_bytes, cbytes_t i_end)
597
+ {
598
+ M3Result result = m3Err_none;
599
+
600
+ u32 numFunctions;
601
+ _ (ReadLEB_u32(&numFunctions, &i_bytes, i_end)); m3log (parse, "** Function [%d]", numFunctions);
602
+
603
+ _throwif("too many functions", numFunctions > d_m3MaxSaneFunctionsCount);
604
+
605
+ _ (Module_PreallocFunctions(io_module, io_module->numFunctions + numFunctions));
606
+
607
+ for (u32 i = 0; i < numFunctions; ++i) {
608
+ u32 funcTypeIndex;
609
+ _ (ReadLEB_u32(&funcTypeIndex, &i_bytes, i_end));
610
+
611
+ _ (Module_AddFunction(io_module, funcTypeIndex, NULL /* import info */));
612
+ }
613
+
614
+ _throwif(m3Err_wasmMalformed, i_bytes != i_end); // section size mismatch
615
+
616
+ _catch: return result;
617
+ }
618
+
619
+
620
+ // One externtype, read but not yet registered. The compact encoding that shares
621
+ // a type across a run needs the two halves apart: the type sits ahead of the
622
+ // item names, so it is read once and then registered for each of them.
623
+ typedef struct M3ImportDesc {
624
+ u8 kind;
625
+ u32 typeIndex; // function
626
+ m3type_t type; // global type
627
+ u8 isMutable; // global
628
+ M3TableInfo table;
629
+ M3MemoryInfo memory;
630
+ } M3ImportDesc;
631
+
632
+
633
+ #if d_m3HasExceptionHandling || d_m3HasStackSwitching
634
+
635
+ // A tag_type: a reserved attribute byte that must be 0 (the exception
636
+ // attribute) and the index of the function type giving the payload. The
637
+ // proposal reserves the byte so later kinds of tag can share the encoding.
638
+ static
639
+ M3Result ReadTagType (IM3Module i_module, u32* o_typeIndex, bytes_t* io_bytes, cbytes_t i_end)
640
+ {
641
+ M3Result result = m3Err_none;
642
+
643
+ u8 attribute;
644
+ _ (Read_u8(&attribute, io_bytes, i_end));
645
+ _throwif(m3Err_wasmMalformed, attribute != 0);
646
+
647
+ _ (ReadLEB_u32(o_typeIndex, io_bytes, i_end));
648
+ _throwif(m3Err_unknownType, not i_module or *o_typeIndex >= i_module->numFuncTypes);
649
+
650
+ # if !d_m3HasStackSwitching
651
+ // an exception tag's type describes its payload, so it yields nothing
652
+ _throwif(m3Err_wasmMalformed, GetFuncTypeNumResults(i_module->funcTypes[*o_typeIndex]) != 0);
653
+ # endif
654
+
655
+ _catch: return result;
656
+ }
657
+
658
+ #endif // d_m3HasExceptionHandling || d_m3HasStackSwitching
659
+
660
+
661
+ // Reads and validates one externtype. Mutates nothing in the module, so an
662
+ // externtype is checked even when the compact run that shares it is empty.
663
+ static
664
+ M3Result ReadImportDesc (IM3Module i_module, M3ImportDesc* o_desc, bytes_t* io_bytes, cbytes_t i_end)
665
+ {
666
+ M3Result result = m3Err_none;
667
+
668
+ _ (Read_u8(&o_desc->kind, io_bytes, i_end));
669
+
670
+ switch (o_desc->kind) {
671
+ case d_externalKind_function:
672
+ _ (ReadLEB_u32(&o_desc->typeIndex, io_bytes, i_end));
673
+ break;
674
+
675
+ case d_externalKind_table:
676
+ _ (ReadType_TableType(i_module, &o_desc->table, io_bytes, i_end));
677
+ break;
678
+
679
+ case d_externalKind_memory:
680
+ _ (ParseType_Memory(&o_desc->memory, io_bytes, i_end));
681
+ break;
682
+
683
+ case d_externalKind_global:
684
+ _ (ParseValueType(i_module, &o_desc->type, io_bytes, i_end));
685
+ _ (ReadLEB_u7(&o_desc->isMutable, io_bytes, i_end)); m3log (parse, " global: %s mutable=%d", c_waTypes [BaseTypeOf(o_desc->type)], (u32) o_desc->isMutable);
686
+ _throwif(m3Err_wasmMalformed, o_desc->isMutable > 1);
687
+ break;
688
+
689
+ #if d_m3HasExceptionHandling || d_m3HasStackSwitching
690
+ case d_externalKind_tag:
691
+ _ (ReadTagType(i_module, &o_desc->typeIndex, io_bytes, i_end));
692
+ break;
693
+ #endif
694
+
695
+ default:
696
+ _throw(m3Err_wasmMalformed);
697
+ }
698
+
699
+ _catch: return result;
700
+ }
701
+
702
+
703
+ // Registers the import that i_desc describes. Takes ownership of *io_import
704
+ // wherever the module keeps the strings, clearing the struct so the caller's
705
+ // FreeImportInfo() doesn't free what was handed over.
706
+ static
707
+ M3Result ApplyImportDesc (IM3Module io_module, const M3ImportDesc* i_desc, M3ImportInfo* io_import)
708
+ {
709
+ M3Result result = m3Err_none;
710
+
711
+ M3ImportInfo clearImport = { NULL, NULL }; m3log (parse, " kind: %d '%s.%s' ",
712
+ (u32) i_desc->kind, io_import->moduleUtf8, io_import->fieldUtf8);
713
+ switch (i_desc->kind) {
714
+ case d_externalKind_function: {
715
+ _ (Module_AddFunction(io_module, i_desc->typeIndex, io_import))
716
+ *io_import = clearImport;
717
+
718
+ io_module->numFuncImports++;
719
+ } break;
720
+
721
+ case d_externalKind_table: {
722
+ IM3Table table;
723
+ _ (Module_AddTable(io_module, &table, &i_desc->table, true /* isImport */));
724
+ table->import = *io_import;
725
+ *io_import = clearImport;
726
+ } break;
727
+
728
+ case d_externalKind_memory: {
729
+ IM3Memory memory;
730
+
731
+ #if d_m3HasMultiMemory
732
+ _throwif(m3Err_tooManyMemorySections, io_module->numMemories >= d_m3MaxSaneMemoriesCount);
733
+ #else
734
+ // MVP: one memory per module, imported or declared
735
+ _throwif(m3Err_tooManyMemorySections, io_module->numMemories >= 1);
736
+ #endif
737
+
738
+ _ (Module_AddMemory(io_module, &memory, &i_desc->memory, true /* isImport */));
739
+ memory->import = *io_import;
740
+ *io_import = clearImport;
741
+ } break;
742
+
743
+ case d_externalKind_global: {
744
+ IM3Global global;
745
+ _ (Module_AddGlobal(io_module, &global, i_desc->type, i_desc->isMutable, true /* isImport */));
746
+ global->import = *io_import;
747
+ *io_import = clearImport;
748
+ } break;
749
+
750
+ #if d_m3HasExceptionHandling || d_m3HasStackSwitching
751
+ case d_externalKind_tag: {
752
+ IM3Tag tag;
753
+ _ (Module_AddTag(io_module, &tag, io_module->funcTypes[i_desc->typeIndex], true /* isImport */));
754
+ tag->import = *io_import;
755
+ *io_import = clearImport;
756
+ } break;
757
+ #endif
758
+
759
+ default:
760
+ _throw(m3Err_wasmMalformed);
761
+ }
762
+
763
+ _catch: return result;
764
+ }
765
+
766
+
767
+ #if d_m3HasCompactImports
768
+
769
+ // The compact encodings only apply when the item name is empty. Read_utf8()
770
+ // hands back a C string, so a name that merely begins with U+0000 would look
771
+ // empty; the length prefix is what has to be tested.
772
+ static
773
+ bool IsEmptyName (bytes_t i_bytes, cbytes_t i_end)
774
+ {
775
+ u32 length;
776
+ return (not ReadLEB_u32(&length, &i_bytes, i_end)) and length == 0;
777
+ }
778
+
779
+ #endif // d_m3HasCompactImports
780
+
781
+
782
+ #if d_m3HasExceptionHandling || d_m3HasStackSwitching
783
+
784
+ M3Result ParseSection_Tag (IM3Module io_module, bytes_t i_bytes, cbytes_t i_end)
785
+ {
786
+ M3Result result = m3Err_none;
787
+
788
+ u32 numTags;
789
+ _ (ReadLEB_u32(&numTags, &i_bytes, i_end)); m3log (parse, "** Tag [%d]", numTags);
790
+
791
+ _throwif("too many tags", numTags > d_m3MaxSaneTagsCount);
792
+
793
+ for (u32 i = 0; i < numTags; ++i) {
794
+ u32 typeIndex;
795
+ _ (ReadTagType(io_module, &typeIndex, &i_bytes, i_end)); m3log (parse, " tag: [%d] type: %d", i, typeIndex);
796
+
797
+ _ (Module_AddTag(io_module, NULL, io_module->funcTypes[typeIndex], false));
798
+ }
799
+
800
+ _throwif(m3Err_wasmMalformed, i_bytes != i_end); // section size mismatch
801
+
802
+ _catch: return result;
803
+ }
804
+
805
+ #endif // d_m3HasExceptionHandling
806
+
807
+
808
+ M3Result ParseSection_Import (IM3Module io_module, bytes_t i_bytes, cbytes_t i_end)
809
+ {
810
+ M3Result result = m3Err_none;
811
+
812
+ M3ImportInfo import = { NULL, NULL };
813
+
814
+ // the limit is on the imports the section declares, which a compact run
815
+ // multiplies well past the entry count. Declared up here so the throws
816
+ // below don't jump over its initialization.
817
+ u32 numImports = 0;
818
+
819
+ // A count of entries, not of imports: under the compact encodings a single
820
+ // entry stands for a whole run of them.
821
+ u32 numEntries;
822
+ _ (ReadLEB_u32(&numEntries, &i_bytes, i_end)); m3log (parse, "** Import [%d]", numEntries);
823
+
824
+ _throwif("too many imports", numEntries > d_m3MaxSaneImportsCount);
825
+
826
+ // Most imports are functions, so we won't waste much space anyway (if any)
827
+ _ (Module_PreallocFunctions(io_module, io_module->numFunctions + numEntries));
828
+
829
+ for (u32 i = 0; i < numEntries; ++i) {
830
+ M3ImportDesc desc;
831
+
832
+ _ (Read_utf8(&import.moduleUtf8, &i_bytes, i_end));
833
+
834
+ #if d_m3HasCompactImports
835
+ bytes_t fieldStart = i_bytes;
836
+ #endif
837
+ _ (Read_utf8(&import.fieldUtf8, &i_bytes, i_end));
838
+
839
+ #if d_m3HasCompactImports
840
+ u8 compact = (i_bytes < i_end) ? *i_bytes : 0;
841
+
842
+ // An empty item name where an externtype should start marks a compact
843
+ // run sharing this module name. Neither marker is a valid externtype,
844
+ // so nothing that used to parse changes meaning.
845
+ if ((compact == d_compactImports_perItemType or compact == d_compactImports_sharedType) and
846
+ IsEmptyName(fieldStart, i_end)) {
847
+ ++i_bytes;
848
+
849
+ m3_Free(import.fieldUtf8); // it only marked the compact form
850
+ import.fieldUtf8 = NULL;
851
+
852
+ // the shared form puts its one externtype ahead of the item names,
853
+ // so reading it here is also what validates it
854
+ if (compact == d_compactImports_sharedType) {
855
+ _ (ReadImportDesc(io_module, &desc, &i_bytes, i_end));
856
+ }
857
+
858
+ u32 numItems;
859
+ _ (ReadLEB_u32(&numItems, &i_bytes, i_end)); m3log (parse, " compact: %d import(s) of '%s'", numItems, import.moduleUtf8);
860
+
861
+ _throwif("too many imports", (u64)numImports + numItems > d_m3MaxSaneImportsCount);
862
+ numImports += numItems;
863
+
864
+ _ (Module_PreallocFunctions(io_module, io_module->numFunctions + numItems));
865
+
866
+ for (u32 j = 0; j < numItems; ++j) {
867
+ M3ImportInfo item = { NULL, NULL };
868
+
869
+ item.moduleUtf8 = (cstr_t)m3_CopyMem(import.moduleUtf8, strlen(import.moduleUtf8) + 1);
870
+
871
+ if (item.moduleUtf8) {
872
+ result = Read_utf8(&item.fieldUtf8, &i_bytes, i_end);
873
+
874
+ // the shared form reuses the externtype read above
875
+ if (not result and compact == d_compactImports_perItemType) {
876
+ result = ReadImportDesc(io_module, &desc, &i_bytes, i_end);
877
+ }
878
+
879
+ if (not result) {
880
+ result = ApplyImportDesc(io_module, &desc, &item);
881
+ }
882
+ } else {
883
+ result = m3Err_mallocFailed;
884
+ }
885
+
886
+ FreeImportInfo(&item);
887
+ _throwif(result, result);
888
+ }
889
+
890
+ FreeImportInfo(&import);
891
+ continue;
892
+ }
893
+ #endif // d_m3HasCompactImports
894
+
895
+ _throwif("too many imports", numImports >= d_m3MaxSaneImportsCount);
896
+ ++numImports;
897
+
898
+ _ (ReadImportDesc(io_module, &desc, &i_bytes, i_end));
899
+ _ (ApplyImportDesc(io_module, &desc, &import));
900
+
901
+ FreeImportInfo(&import);
902
+ }
903
+
904
+ _throwif(m3Err_wasmMalformed, i_bytes != i_end); // section size mismatch
905
+
906
+ _catch:
907
+
908
+ FreeImportInfo(&import);
909
+
910
+ return result;
911
+ }
912
+
913
+
914
+ M3Result ParseSection_Export (IM3Module io_module, bytes_t i_bytes, cbytes_t i_end)
915
+ {
916
+ M3Result result = m3Err_none;
917
+ const char* utf8 = NULL;
918
+
919
+ u32 numExports;
920
+ _ (ReadLEB_u32(&numExports, &i_bytes, i_end)); m3log (parse, "** Export [%d]", numExports);
921
+
922
+ _throwif("too many exports", numExports > d_m3MaxSaneExportsCount);
923
+
924
+ // the section order check lets this section through once, so this is
925
+ // every export the module has
926
+ if (numExports) {
927
+ io_module->exports = m3_AllocArray(M3Export, numExports);
928
+ _throwifnull(io_module->exports);
929
+ }
930
+
931
+ for (u32 i = 0; i < numExports; ++i) {
932
+ u8 exportKind;
933
+ u32 index;
934
+ u32 nameLength;
935
+
936
+ // Read_utf8 takes the length along with the name, so read it ahead
937
+ {
938
+ bytes_t lengthBytes = i_bytes;
939
+ _ (ReadLEB_u32(&nameLength, &lengthBytes, i_end));
940
+ }
941
+
942
+ _ (Read_utf8(&utf8, &i_bytes, i_end));
943
+ _ (Read_u8(&exportKind, &i_bytes, i_end));
944
+ _ (ReadLEB_u32(&index, &i_bytes, i_end)); m3log (parse, " index: %3d; kind: %d; export: '%s'; ", index, (u32) exportKind, utf8);
945
+
946
+ #if d_m3EnableValidation
947
+ // Spec: all export names must be different - compared by length, since
948
+ // a name may hold a NUL
949
+ for (u32 j = 0; j < i; ++j) {
950
+ const M3Export* other = &io_module->exports[j];
951
+
952
+ _throwif(m3Err_wasmMalformed, other->nameLength == nameLength and memcmp(other->name, utf8, nameLength) == 0);
953
+ }
954
+ #endif
955
+
956
+ if (exportKind == d_externalKind_function) {
957
+ _throwif(m3Err_wasmMalformed, index >= io_module->numFunctions);
958
+ IM3Function func = &(io_module->functions[index]);
959
+ if (func->numNames < d_m3MaxDuplicateFunctionImpl) {
960
+ // a name of its own: names are freed with the function
961
+ cstr_t name = (cstr_t)m3_CopyMem(utf8, nameLength + 1);
962
+ _throwifnull(name);
963
+ func->names[func->numNames++] = name;
964
+ }
965
+ _ (Module_DeclareFunction(io_module, index));
966
+ } else if (exportKind == d_externalKind_global) {
967
+ _throwif(m3Err_wasmMalformed, index >= io_module->numGlobals);
968
+ } else if (exportKind == d_externalKind_memory) {
969
+ _throwif(m3Err_wasmMalformed, index >= io_module->numMemories);
970
+ } else if (exportKind == d_externalKind_table) {
971
+ _throwif(m3Err_wasmMalformed, index >= io_module->numTables);
972
+ }
973
+ #if d_m3HasExceptionHandling || d_m3HasStackSwitching
974
+ else if (exportKind == d_externalKind_tag) {
975
+ _throwif(m3Err_wasmMalformed, index >= io_module->numTags);
976
+ }
977
+ #endif
978
+
979
+ M3Export* entry = &io_module->exports[io_module->numExports++];
980
+ entry->name = utf8;
981
+ entry->nameLength = nameLength;
982
+ entry->index = index;
983
+ entry->kind = exportKind;
984
+
985
+ utf8 = NULL; // ownership transferred to M3Module
986
+ }
987
+
988
+ _throwif(m3Err_wasmMalformed, i_bytes != i_end); // section size mismatch
989
+
990
+ _catch:
991
+ m3_Free(utf8);
992
+ return result;
993
+ }
994
+
995
+
996
+ M3Result ParseSection_Start (IM3Module io_module, bytes_t i_bytes, cbytes_t i_end)
997
+ {
998
+ M3Result result = m3Err_none;
999
+
1000
+ u32 startFuncIndex;
1001
+ _ (ReadLEB_u32(&startFuncIndex, &i_bytes, i_end)); m3log (parse, "** Start Function: %d", startFuncIndex);
1002
+
1003
+ if (startFuncIndex < io_module->numFunctions) {
1004
+ // Spec: start function type must be [] -> []
1005
+ IM3Function func = &io_module->functions[startFuncIndex];
1006
+ if (func->funcType) {
1007
+ _throwif(m3Err_wasmMalformed,
1008
+ func->funcType->numArgs != 0 || func->funcType->numRets != 0);
1009
+ }
1010
+
1011
+ io_module->startFunction = startFuncIndex;
1012
+ } else {
1013
+ result = "start function index out of bounds";
1014
+ }
1015
+
1016
+ _throwif(m3Err_wasmMalformed, i_bytes != i_end); // section size mismatch
1017
+
1018
+ _catch: return result;
1019
+ }
1020
+
1021
+
1022
+ // o_type, when asked for, reports what the expression leaves on the stack, so
1023
+ // the caller can check it against the type the expression is initializing.
1024
+ M3Result Parse_InitExprTyped (M3Module* io_module, bytes_t* io_bytes, cbytes_t i_end, m3type_t* o_type)
1025
+ {
1026
+ M3Result result = m3Err_none;
1027
+
1028
+ // this doesn't generate code pages. just walks the wasm bytecode to find the end
1029
+
1030
+ #if defined(d_m3PreferStaticAlloc)
1031
+ static M3Compilation compilation;
1032
+ #else
1033
+ M3Compilation compilation;
1034
+ #endif
1035
+ compilation = (M3Compilation){ .runtime = NULL, .module = io_module, .wasm = *io_bytes, .wasmEnd = i_end, .isInitExpr = true };
1036
+
1037
+ result = CompileBlockStatements(&compilation);
1038
+
1039
+ *io_bytes = compilation.wasm;
1040
+
1041
+ if (o_type) {
1042
+ *o_type = compilation.stackIndex ? compilation.typeStack[compilation.stackIndex - 1]
1043
+ : (m3type_t)c_m3Type_none;
1044
+ }
1045
+
1046
+ return result;
1047
+ }
1048
+
1049
+
1050
+ M3Result Parse_InitExpr (M3Module* io_module, bytes_t* io_bytes, cbytes_t i_end)
1051
+ {
1052
+ return Parse_InitExprTyped(io_module, io_bytes, i_end, NULL);
1053
+ }
1054
+
1055
+
1056
+ M3Result ParseSection_Element (IM3Module io_module, bytes_t i_bytes, cbytes_t i_end)
1057
+ {
1058
+ M3Result result = m3Err_none;
1059
+
1060
+ u32 numSegments;
1061
+ bytes_t pos;
1062
+ _ (ReadLEB_u32(&numSegments, &i_bytes, i_end)); m3log (parse, "** Element [%d]", numSegments);
1063
+
1064
+ _throwif("too many element segments", numSegments > d_m3MaxSaneElementSegments);
1065
+
1066
+ io_module->elementSegments = m3_AllocArray(M3ElementSegment, numSegments);
1067
+ _throwifnull(io_module->elementSegments);
1068
+ io_module->numElementSegments = numSegments;
1069
+ io_module->elementSectionEnd = i_end;
1070
+
1071
+ // Records where each segment's parts live; the elements are resolved later,
1072
+ // in InitTableAndElements.
1073
+ pos = i_bytes;
1074
+ for (u32 i = 0; i < numSegments; ++i) {
1075
+ M3ElementSegment* segment = &io_module->elementSegments[i];
1076
+
1077
+ u32 flags;
1078
+ _ (ReadLEB_u32(&flags, &pos, i_end));
1079
+ #if d_m3HasRefTypes
1080
+ _throwif(m3Err_wasmMalformed, flags > 7);
1081
+ #else
1082
+ _throwif(m3Err_wasmMalformed, flags != c_m3Elem_active);
1083
+ #endif
1084
+
1085
+ segment->mode = flags & 0x3;
1086
+ segment->isExpr = (flags & 0x4) != 0;
1087
+ segment->type = c_m3Type_funcref;
1088
+
1089
+ if (segment->mode == c_m3Elem_activeIdx) {
1090
+ _ (ReadLEB_u32(&segment->tableIndex, &pos, i_end));
1091
+ }
1092
+
1093
+ bool isActive = (segment->mode == c_m3Elem_active or segment->mode == c_m3Elem_activeIdx);
1094
+
1095
+ if (isActive) {
1096
+ _throwif(m3Err_wasmMalformed, segment->tableIndex >= io_module->numTables);
1097
+
1098
+ segment->initExpr = pos;
1099
+ _ (Parse_InitExpr(io_module, &pos, i_end));
1100
+ segment->initExprSize = (u32)(pos - segment->initExpr);
1101
+ _throwif(m3Err_wasmMissingInitExpr, segment->initExprSize <= 1);
1102
+ }
1103
+
1104
+ // Only the table-0 active form leaves the element type implicit
1105
+ if (segment->mode != c_m3Elem_active) {
1106
+ if (segment->isExpr) {
1107
+ _ (ParseValueType(io_module, &segment->type, &pos, i_end));
1108
+ _throwif(m3Err_wasmMalformed, not IsRefType(BaseTypeOf(segment->type)));
1109
+ } else {
1110
+ u8 elemKind;
1111
+ _ (Read_u8(&elemKind, &pos, i_end));
1112
+ _throwif(m3Err_wasmMalformed, elemKind != 0x00); // funcref
1113
+ }
1114
+ }
1115
+
1116
+ if (isActive) {
1117
+ _throwif(m3Err_typeMismatch, io_module->tables[segment->tableIndex]->type != segment->type);
1118
+ }
1119
+
1120
+ _ (ReadLEB_u32(&segment->numElements, &pos, i_end));
1121
+ _throwif("table overflow", segment->numElements > d_m3MaxSaneTableSize);
1122
+
1123
+ segment->elements = pos;
1124
+
1125
+ for (u32 e = 0; e < segment->numElements; ++e) {
1126
+ if (segment->isExpr) {
1127
+ _ (Parse_InitExpr(io_module, &pos, i_end))
1128
+ } else {
1129
+ u32 funcIndex;
1130
+ _ (ReadLEB_u32(&funcIndex, &pos, i_end));
1131
+ _throwif("function index out of range", funcIndex >= io_module->numFunctions);
1132
+ _ (Module_DeclareFunction(io_module, funcIndex));
1133
+ }
1134
+ }
1135
+ }
1136
+
1137
+ _throwif(m3Err_wasmMalformed, pos != i_end); // section size mismatch
1138
+
1139
+ _catch: return result;
1140
+ }
1141
+
1142
+
1143
+ M3Result ParseSection_Code (M3Module* io_module, bytes_t i_bytes, cbytes_t i_end)
1144
+ {
1145
+ M3Result result;
1146
+
1147
+ u32 numFunctions;
1148
+ _ (ReadLEB_u32(&numFunctions, &i_bytes, i_end)); m3log (parse, "** Code [%d]", numFunctions);
1149
+
1150
+ if (numFunctions != io_module->numFunctions - io_module->numFuncImports) {
1151
+ _throw("mismatched function count in code section");
1152
+ }
1153
+
1154
+ for (u32 f = 0; f < numFunctions; ++f) {
1155
+ const u8* start = i_bytes;
1156
+
1157
+ u32 size;
1158
+ _ (ReadLEB_u32(&size, &i_bytes, i_end));
1159
+
1160
+ if (size) {
1161
+ i_bytes += size;
1162
+
1163
+ if (i_bytes <= i_end) {
1164
+ /*
1165
+ const u8 * ptr = i_bytes - size;
1166
+
1167
+ u32 numLocalBlocks;
1168
+ _ (ReadLEB_u32 (& numLocalBlocks, & ptr, i_end)); m3log (parse, " code size: %-4d", size);
1169
+
1170
+ u32 numLocals = 0;
1171
+
1172
+ for (u32 l = 0; l < numLocalBlocks; ++l)
1173
+ {
1174
+ u32 varCount;
1175
+ i8 wasmType;
1176
+ u8 normalType;
1177
+
1178
+ _ (ReadLEB_u32 (& varCount, & ptr, i_end));
1179
+ _ (ReadLEB_i7 (& wasmType, & ptr, i_end));
1180
+ _ (NormalizeType (& normalType, wasmType));
1181
+
1182
+ numLocals += varCount; m3log (parse, " %2d locals; type: '%s'", varCount, c_waTypes [normalType]);
1183
+ }
1184
+ */
1185
+
1186
+ IM3Function func = Module_GetFunction(io_module, f + io_module->numFuncImports);
1187
+
1188
+ func->module = io_module;
1189
+ func->wasm = start;
1190
+ func->wasmEnd = i_bytes;
1191
+ //func->ownsWasmCode = io_module->hasWasmCodeCopy;
1192
+ // func->numLocals = numLocals;
1193
+ } else {
1194
+ _throw(m3Err_wasmSectionOverrun);
1195
+ }
1196
+ }
1197
+ }
1198
+
1199
+ _catch:
1200
+
1201
+ if (not result and i_bytes != i_end) {
1202
+ result = m3Err_wasmSectionUnderrun;
1203
+ }
1204
+
1205
+ return result;
1206
+ }
1207
+
1208
+
1209
+ M3Result ParseSection_Data (M3Module* io_module, bytes_t i_bytes, cbytes_t i_end)
1210
+ {
1211
+ M3Result result = m3Err_none;
1212
+
1213
+ u32 numDataSegments;
1214
+ _ (ReadLEB_u32(&numDataSegments, &i_bytes, i_end)); m3log (parse, "** Data [%d]", numDataSegments);
1215
+
1216
+ _throwif("too many data segments", numDataSegments > d_m3MaxSaneDataSegments);
1217
+
1218
+ io_module->dataSegments = m3_AllocArray(M3DataSegment, numDataSegments);
1219
+ _throwifnull(io_module->dataSegments);
1220
+ io_module->numDataSegments = numDataSegments;
1221
+
1222
+ for (u32 i = 0; i < numDataSegments; ++i) {
1223
+ M3DataSegment* segment = &io_module->dataSegments[i];
1224
+
1225
+ // 0: active in memory 0, 1: passive, 2: active with an explicit memory index
1226
+ u32 flags;
1227
+ _ (ReadLEB_u32(&flags, &i_bytes, i_end));
1228
+ _throwif(m3Err_wasmMalformed, flags > 2);
1229
+
1230
+ segment->isPassive = (flags == 1);
1231
+
1232
+ if (flags == 2) {
1233
+ _ (ReadLEB_u32(&segment->memoryRegion, &i_bytes, i_end));
1234
+ }
1235
+
1236
+ if (not segment->isPassive) {
1237
+ // The segment names the memory it initializes; only multi-memory
1238
+ // lets that be anything other than 0.
1239
+ #if !d_m3HasMultiMemory
1240
+ _throwif(m3Err_wasmMalformed, segment->memoryRegion != 0);
1241
+ #endif
1242
+ _throwif(m3Err_wasmMalformed, segment->memoryRegion >= io_module->numMemories);
1243
+
1244
+ segment->initExpr = i_bytes;
1245
+ _ (Parse_InitExpr(io_module, &i_bytes, i_end));
1246
+ segment->initExprSize = (u32)(i_bytes - segment->initExpr);
1247
+
1248
+ _throwif(m3Err_wasmMissingInitExpr, segment->initExprSize <= 1);
1249
+ }
1250
+
1251
+ _ (ReadLEB_u32(&segment->size, &i_bytes, i_end));
1252
+ segment->data = i_bytes; m3log (parse, " segment [%u] memory: %u; expr-size: %d; size: %d",
1253
+ i, segment->memoryRegion, segment->initExprSize, segment->size);
1254
+ i_bytes += segment->size;
1255
+
1256
+ _throwif("data segment underflow", i_bytes > i_end);
1257
+ }
1258
+
1259
+ _throwif(m3Err_wasmMalformed, i_bytes != i_end); // section size mismatch
1260
+
1261
+ _catch:
1262
+
1263
+ return result;
1264
+ }
1265
+
1266
+
1267
+ M3Result ParseSection_DataCount (M3Module* io_module, bytes_t i_bytes, cbytes_t i_end)
1268
+ {
1269
+ M3Result result = m3Err_none;
1270
+
1271
+ _ (ReadLEB_u32(&io_module->dataCount, &i_bytes, i_end)); m3log (parse, "** DataCount [%d]", io_module->dataCount);
1272
+
1273
+ io_module->hasDataCount = true;
1274
+
1275
+ _throwif(m3Err_wasmMalformed, i_bytes != i_end); // section size mismatch
1276
+
1277
+ _catch: return result;
1278
+ }
1279
+
1280
+
1281
+ M3Result ParseSection_Memory (M3Module* io_module, bytes_t i_bytes, cbytes_t i_end)
1282
+ {
1283
+ M3Result result = m3Err_none;
1284
+
1285
+ u32 numMemories;
1286
+ _ (ReadLEB_u32(&numMemories, &i_bytes, i_end)); m3log (parse, "** Memory [%d]", numMemories);
1287
+
1288
+ #if d_m3HasMultiMemory
1289
+ _throwif(m3Err_tooManyMemorySections,
1290
+ (u64)numMemories + io_module->numMemories > d_m3MaxSaneMemoriesCount);
1291
+ #else
1292
+ // MVP: at most one memory, counting any that was already imported
1293
+ _throwif(m3Err_tooManyMemorySections, numMemories > 1);
1294
+ _throwif(m3Err_tooManyMemorySections, (u64)numMemories + io_module->numMemories > 1);
1295
+ #endif
1296
+
1297
+ for (u32 i = 0; i < numMemories; ++i) {
1298
+ M3MemoryInfo info;
1299
+ _ (ParseType_Memory(&info, &i_bytes, i_end));
1300
+ _ (Module_AddMemory(io_module, NULL, &info, false /* isImport */));
1301
+ }
1302
+
1303
+ _throwif(m3Err_wasmMalformed, i_bytes != i_end); // section size mismatch
1304
+
1305
+ _catch: return result;
1306
+ }
1307
+
1308
+
1309
+ M3Result ParseSection_Global (M3Module* io_module, bytes_t i_bytes, cbytes_t i_end)
1310
+ {
1311
+ M3Result result = m3Err_none;
1312
+
1313
+ u32 numGlobals;
1314
+ _ (ReadLEB_u32(&numGlobals, &i_bytes, i_end)); m3log (parse, "** Global [%d]", numGlobals);
1315
+
1316
+ _throwif("too many globals", numGlobals > d_m3MaxSaneGlobalsCount);
1317
+
1318
+ for (u32 i = 0; i < numGlobals; ++i) {
1319
+ m3type_t type = c_m3Type_none;
1320
+ u8 isMutable;
1321
+
1322
+ _ (ParseValueType(io_module, &type, &i_bytes, i_end));
1323
+ _ (ReadLEB_u7(&isMutable, &i_bytes, i_end)); m3log (parse, " global: [%d] %s mutable: %d", i, c_waTypes [BaseTypeOf(type)], (u32) isMutable);
1324
+ _throwif(m3Err_wasmMalformed, isMutable > 1);
1325
+
1326
+ IM3Global global;
1327
+ _ (Module_AddGlobal(io_module, &global, type, isMutable, false /* isImport */));
1328
+
1329
+ global->initExpr = i_bytes;
1330
+ _ (Parse_InitExpr(io_module, &i_bytes, i_end));
1331
+ global->initExprSize = (u32)(i_bytes - global->initExpr);
1332
+
1333
+ _throwif(m3Err_wasmMissingInitExpr, global->initExprSize <= 1);
1334
+ }
1335
+
1336
+ _throwif(m3Err_wasmMalformed, i_bytes != i_end); // section size mismatch
1337
+
1338
+ _catch: return result;
1339
+ }
1340
+
1341
+
1342
+ M3Result ParseSection_Name (M3Module* io_module, bytes_t i_bytes, cbytes_t i_end)
1343
+ {
1344
+ M3Result result = m3Err_none;
1345
+
1346
+ cstr_t name = NULL;
1347
+
1348
+ while (i_bytes < i_end) {
1349
+ u8 nameType;
1350
+ u32 payloadLength;
1351
+
1352
+ _ (ReadLEB_u7(&nameType, &i_bytes, i_end));
1353
+ _ (ReadLEB_u32(&payloadLength, &i_bytes, i_end));
1354
+
1355
+ bytes_t start = i_bytes;
1356
+ _throwif(m3Err_wasmSectionOverrun, payloadLength > (size_t)(i_end - start));
1357
+
1358
+ if (nameType == 1) {
1359
+ u32 numNames;
1360
+ _ (ReadLEB_u32(&numNames, &i_bytes, i_end));
1361
+
1362
+ _throwif("too many names", numNames > d_m3MaxSaneFunctionsCount);
1363
+
1364
+ for (u32 i = 0; i < numNames; ++i) {
1365
+ u32 index;
1366
+ _ (ReadLEB_u32(&index, &i_bytes, i_end));
1367
+ _ (Read_utf8(&name, &i_bytes, i_end));
1368
+
1369
+ if (index < io_module->numFunctions) {
1370
+ IM3Function func = &(io_module->functions[index]);
1371
+ if (func->numNames == 0) {
1372
+ func->names[0] = name; m3log (parse, " naming function%5d: %s", index, name);
1373
+ func->numNames = 1;
1374
+
1375
+ name = NULL; // transfer ownership
1376
+ }
1377
+ // else m3log (parse, "prenamed: %s", io_module->functions [index].name);
1378
+ }
1379
+
1380
+ m3_Free(name);
1381
+ }
1382
+ }
1383
+
1384
+ i_bytes = start + payloadLength;
1385
+ }
1386
+
1387
+ _catch: return result;
1388
+ }
1389
+
1390
+
1391
+ M3Result ParseSection_Custom (M3Module* io_module, bytes_t i_bytes, cbytes_t i_end)
1392
+ {
1393
+ M3Result result = m3Err_none;
1394
+
1395
+ cstr_t name = NULL;
1396
+ _ (Read_utf8(&name, &i_bytes, i_end));
1397
+ m3log (parse, "** Custom: '%s'", name);
1398
+ if (strcmp(name, "name") == 0) {
1399
+ _ (ParseSection_Name(io_module, i_bytes, i_end));
1400
+ #if d_m3HasSnapshots
1401
+ // "snapshot" is the unnamed default, "snapshot.<name>" a named one. A bare
1402
+ // "snapshot." names nothing and is not either of them, so it is left to the
1403
+ // embedder's handler.
1404
+ } else if (strcmp(name, "snapshot") == 0 || (strlen(name) > 9 && strncmp(name, "snapshot.", 9) == 0)) {
1405
+ // Two of the same name are kept, not refused: a custom section does not
1406
+ // decide whether the module is valid. Selecting that name is what fails.
1407
+ cstr_t snapName = (name[8] == '.') ? name + 9 : "";
1408
+ M3EmbeddedSnapshot* snap = m3_AllocStruct(M3EmbeddedSnapshot);
1409
+ _throwifnull(snap);
1410
+ snap->name = (cstr_t)m3_CopyMem(snapName, strlen(snapName) + 1);
1411
+ if (!snap->name) {
1412
+ m3_Free(snap);
1413
+ _throw(m3Err_mallocFailed);
1414
+ }
1415
+ snap->data = i_bytes;
1416
+ snap->size = (u32)(i_end - i_bytes);
1417
+ snap->next = io_module->snapshots;
1418
+ io_module->snapshots = snap;
1419
+ #endif
1420
+ } else if (io_module->environment->customSectionHandler) {
1421
+ _ (io_module->environment->customSectionHandler(io_module, name, i_bytes, i_end));
1422
+ }
1423
+
1424
+ // the section name is freed on the error path too: a malformed "name"
1425
+ // section, or a handler that rejects the payload, still throws past here
1426
+ _catch:
1427
+
1428
+ m3_Free(name);
1429
+
1430
+ return result;
1431
+ }
1432
+
1433
+
1434
+ M3Result ParseModuleSection (M3Module* o_module, u8 i_sectionType, bytes_t i_bytes, u32 i_numBytes)
1435
+ {
1436
+ M3Result result = m3Err_none;
1437
+
1438
+ typedef M3Result (*M3Parser)(M3Module*, bytes_t, cbytes_t);
1439
+
1440
+ static M3Parser s_parsers[] = {
1441
+ ParseSection_Custom, // 0
1442
+ ParseSection_Type, // 1
1443
+ ParseSection_Import, // 2
1444
+ ParseSection_Function, // 3
1445
+ ParseType_Table, // 4
1446
+ ParseSection_Memory, // 5
1447
+ ParseSection_Global, // 6
1448
+ ParseSection_Export, // 7
1449
+ ParseSection_Start, // 8
1450
+ ParseSection_Element, // 9
1451
+ ParseSection_Code, // 10
1452
+ ParseSection_Data, // 11
1453
+ ParseSection_DataCount, // 12
1454
+ #if d_m3HasExceptionHandling || d_m3HasStackSwitching
1455
+ ParseSection_Tag, // 13
1456
+ #endif
1457
+ };
1458
+
1459
+ M3Parser parser = NULL;
1460
+
1461
+ if (i_sectionType < M3_COUNT_OF(s_parsers)) {
1462
+ parser = s_parsers[i_sectionType];
1463
+ }
1464
+
1465
+ if (parser) {
1466
+ cbytes_t end = i_bytes + i_numBytes;
1467
+ result = parser(o_module, i_bytes, end);
1468
+ } else {
1469
+ m3log(parse, " skipped section type: %d", (u32)i_sectionType);
1470
+ }
1471
+
1472
+ return result;
1473
+ }
1474
+
1475
+
1476
+ M3Result m3_ParseModule (IM3Environment i_environment, IM3Module* o_module, cbytes_t i_bytes, u32 i_numBytes)
1477
+ {
1478
+ IM3Module module; m3log (parse, "load module: %d bytes", i_numBytes);
1479
+ _try {
1480
+ module = m3_AllocStruct(M3Module);
1481
+ _throwifnull(module);
1482
+ module->name = ".unnamed"; m3log (parse, "load module: %d bytes", i_numBytes);
1483
+ module->startFunction = -1;
1484
+ //module->hasWasmCodeCopy = false;
1485
+ module->environment = i_environment;
1486
+
1487
+ const u8* pos = i_bytes;
1488
+ const u8* end = pos + i_numBytes;
1489
+
1490
+ module->wasmStart = pos;
1491
+ module->wasmEnd = end;
1492
+
1493
+ u32 magic, version;
1494
+ _ (Read_u32(&magic, &pos, end));
1495
+ _ (Read_u32(&version, &pos, end));
1496
+
1497
+ _throwif(m3Err_wasmMalformed, magic != 0x6d736100);
1498
+ _throwif(m3Err_incompatibleWasmVersion, version != 1);
1499
+
1500
+ #if d_m3HasExceptionHandling || d_m3HasStackSwitching
1501
+ // the tag section sits between memory (5) and global (6)
1502
+ static const u8 sectionsOrder[] = { 1, 2, 3, 4, 5, 13, 6, 7, 8, 9, 12, 10, 11, 0 }; // 0 is a placeholder
1503
+ #else
1504
+ static const u8 sectionsOrder[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 10, 11, 0 }; // 0 is a placeholder
1505
+ #endif
1506
+ u8 expectedSection = 0;
1507
+
1508
+ while (pos < end) {
1509
+ u8 section;
1510
+ _ (ReadLEB_u7(&section, &pos, end));
1511
+
1512
+ if (section != 0) {
1513
+ // Ensure sections appear only once and in order
1514
+ while (sectionsOrder[expectedSection++] != section) {
1515
+ _throwif(m3Err_misorderedWasmSection, expectedSection >= M3_COUNT_OF(sectionsOrder) - 1);
1516
+ }
1517
+ }
1518
+
1519
+ u32 sectionLength;
1520
+ _ (ReadLEB_u32(&sectionLength, &pos, end));
1521
+ _throwif(m3Err_wasmMalformed, pos + sectionLength > end);
1522
+
1523
+ _ (ParseModuleSection(module, section, pos, sectionLength));
1524
+
1525
+ pos += sectionLength;
1526
+ }
1527
+
1528
+ // Spec: if a function section exists, a code section must also exist with
1529
+ // matching count (and vice versa). ParseSection_Code checks the other
1530
+ // direction; this covers the case where the code section is missing entirely.
1531
+ if (module->numFunctions > module->numFuncImports) {
1532
+ IM3Function firstNonImport = &module->functions[module->numFuncImports];
1533
+ _throwif(m3Err_wasmMalformed, firstNonImport->wasm == NULL);
1534
+ }
1535
+
1536
+ // Spec: the data count section must agree with the data section, which may be absent
1537
+ _throwif(m3Err_wasmMalformed, module->hasDataCount and module->dataCount != module->numDataSegments);
1538
+
1539
+ } _catch:
1540
+
1541
+ if (result) {
1542
+ m3_FreeModule(module);
1543
+ module = NULL;
1544
+ }
1545
+
1546
+ *o_module = module;
1547
+
1548
+ return result;
1549
+ }