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,1107 @@
1
+ //
2
+ // m3_api_meta_wasi.c
3
+ //
4
+ // Created by Volodymyr Shymanskyy on 01/08/20.
5
+ // Copyright © 2020 Volodymyr Shymanskyy. All rights reserved.
6
+ //
7
+
8
+ #include "m3_api_wasi.h"
9
+
10
+ #include "m3_env.h"
11
+ #include "m3_exception.h"
12
+
13
+ #if defined(d_m3HasMetaWASI)
14
+
15
+ // NOTE: MetaWASI mostly redirects WASI calls to the host WASI environment
16
+
17
+ # if !defined(__wasi__)
18
+ # error "MetaWASI is only supported on WASI target"
19
+ # endif
20
+
21
+ # if __has_include("wasi/api.h")
22
+ # include <wasi/api.h>
23
+ # define USE_NEW_WASI
24
+ # define WASI_STAT_FIELD(f) f
25
+
26
+ # elif __has_include("wasi/core.h")
27
+ # warning "Using legacy WASI headers"
28
+ # include <wasi/core.h>
29
+ # define __WASI_ERRNO_SUCCESS __WASI_ESUCCESS
30
+ # define __WASI_ERRNO_INVAL __WASI_EINVAL
31
+ # define WASI_STAT_FIELD(f) st_##f
32
+
33
+ # else
34
+ # error "Missing WASI headers"
35
+ # endif
36
+
37
+ static m3_wasi_context_t* wasi_context;
38
+
39
+ typedef size_t __wasi_size_t;
40
+
41
+ // The WASI ABI passes paths as (pointer, length) with no terminator, but wasi-libc's
42
+ // __wasi_path_* wrappers take NUL-terminated strings, so every path has to be copied out
43
+ // of guest memory and terminated first.
44
+ # ifdef PATH_MAX
45
+ # define d_m3WasiPathMax PATH_MAX
46
+ # else
47
+ # define d_m3WasiPathMax 1024
48
+ # endif
49
+
50
+ // Declares NAME as a NUL-terminated copy of the guest's path, or returns ENAMETOOLONG
51
+ # define d_m3WasiPath(NAME, PTR, LEN) \
52
+ char NAME [d_m3WasiPathMax]; \
53
+ if ((LEN) >= sizeof (NAME)) m3ApiReturn (__WASI_ERRNO_NAMETOOLONG); \
54
+ memcpy (NAME, (PTR), (LEN)); \
55
+ NAME [(LEN)] = 0;
56
+
57
+ static inline
58
+ const void* copy_iov_to_host (IM3Runtime runtime, void* _mem, __wasi_iovec_t* host_iov, __wasi_iovec_t* wasi_iov, int32_t iovs_len)
59
+ {
60
+ // Convert wasi memory offsets to host addresses
61
+ for (int i = 0; i < iovs_len; i++) {
62
+ host_iov[i].buf = m3ApiOffsetToPtr(wasi_iov[i].buf);
63
+ host_iov[i].buf_len = wasi_iov[i].buf_len;
64
+ m3ApiCheckMem(host_iov[i].buf, host_iov[i].buf_len);
65
+ }
66
+ m3ApiSuccess();
67
+ }
68
+
69
+ # if d_m3EnableWasiTracing
70
+
71
+ const char* wasi_errno2str (__wasi_errno_t err)
72
+ {
73
+ // clang-format off
74
+ switch (err) {
75
+ case 0: return "ESUCCESS";
76
+ case 1: return "E2BIG";
77
+ case 2: return "EACCES";
78
+ case 3: return "EADDRINUSE";
79
+ case 4: return "EADDRNOTAVAIL";
80
+ case 5: return "EAFNOSUPPORT";
81
+ case 6: return "EAGAIN";
82
+ case 7: return "EALREADY";
83
+ case 8: return "EBADF";
84
+ case 9: return "EBADMSG";
85
+ case 10: return "EBUSY";
86
+ case 11: return "ECANCELED";
87
+ case 12: return "ECHILD";
88
+ case 13: return "ECONNABORTED";
89
+ case 14: return "ECONNREFUSED";
90
+ case 15: return "ECONNRESET";
91
+ case 16: return "EDEADLK";
92
+ case 17: return "EDESTADDRREQ";
93
+ case 18: return "EDOM";
94
+ case 19: return "EDQUOT";
95
+ case 20: return "EEXIST";
96
+ case 21: return "EFAULT";
97
+ case 22: return "EFBIG";
98
+ case 23: return "EHOSTUNREACH";
99
+ case 24: return "EIDRM";
100
+ case 25: return "EILSEQ";
101
+ case 26: return "EINPROGRESS";
102
+ case 27: return "EINTR";
103
+ case 28: return "EINVAL";
104
+ case 29: return "EIO";
105
+ case 30: return "EISCONN";
106
+ case 31: return "EISDIR";
107
+ case 32: return "ELOOP";
108
+ case 33: return "EMFILE";
109
+ case 34: return "EMLINK";
110
+ case 35: return "EMSGSIZE";
111
+ case 36: return "EMULTIHOP";
112
+ case 37: return "ENAMETOOLONG";
113
+ case 38: return "ENETDOWN";
114
+ case 39: return "ENETRESET";
115
+ case 40: return "ENETUNREACH";
116
+ case 41: return "ENFILE";
117
+ case 42: return "ENOBUFS";
118
+ case 43: return "ENODEV";
119
+ case 44: return "ENOENT";
120
+ case 45: return "ENOEXEC";
121
+ case 46: return "ENOLCK";
122
+ case 47: return "ENOLINK";
123
+ case 48: return "ENOMEM";
124
+ case 49: return "ENOMSG";
125
+ case 50: return "ENOPROTOOPT";
126
+ case 51: return "ENOSPC";
127
+ case 52: return "ENOSYS";
128
+ case 53: return "ENOTCONN";
129
+ case 54: return "ENOTDIR";
130
+ case 55: return "ENOTEMPTY";
131
+ case 56: return "ENOTRECOVERABLE";
132
+ case 57: return "ENOTSOCK";
133
+ case 58: return "ENOTSUP";
134
+ case 59: return "ENOTTY";
135
+ case 60: return "ENXIO";
136
+ case 61: return "EOVERFLOW";
137
+ case 62: return "EOWNERDEAD";
138
+ case 63: return "EPERM";
139
+ case 64: return "EPIPE";
140
+ case 65: return "EPROTO";
141
+ case 66: return "EPROTONOSUPPORT";
142
+ case 67: return "EPROTOTYPE";
143
+ case 68: return "ERANGE";
144
+ case 69: return "EROFS";
145
+ case 70: return "ESPIPE";
146
+ case 71: return "ESRCH";
147
+ case 72: return "ESTALE";
148
+ case 73: return "ETIMEDOUT";
149
+ case 74: return "ETXTBSY";
150
+ case 75: return "EXDEV";
151
+ case 76: return "ENOTCAPABLE";
152
+ default: return "<unknown>";
153
+ }
154
+ // clang-format on
155
+ }
156
+
157
+ const char* wasi_whence2str (__wasi_whence_t whence)
158
+ {
159
+ switch (whence) {
160
+ case __WASI_WHENCE_SET: return "SET";
161
+ case __WASI_WHENCE_CUR: return "CUR";
162
+ case __WASI_WHENCE_END: return "END";
163
+ default: return "<unknown>";
164
+ }
165
+ }
166
+
167
+ # define WASI_TRACE(fmt, ...) { fprintf(stderr, "%s " fmt, __FUNCTION__+16, ##__VA_ARGS__); fprintf(stderr, " => %s\n", wasi_errno2str(ret)); }
168
+ # else
169
+ # define WASI_TRACE(fmt, ...)
170
+ # endif
171
+
172
+ /*
173
+ * WASI API implementation
174
+ */
175
+
176
+ m3ApiRawFunction(m3_wasi_p1_args_get)
177
+ {
178
+ m3ApiReturnType(uint32_t)
179
+ m3ApiGetArgMem(uint32_t*, argv)
180
+ m3ApiGetArgMem(char*, argv_buf)
181
+
182
+ m3_wasi_context_t* context = (m3_wasi_context_t*)(_ctx->userdata);
183
+
184
+ if (context == NULL) {
185
+ m3ApiReturn(__WASI_ERRNO_INVAL);
186
+ }
187
+
188
+ m3ApiCheckMem(argv, context->argc * sizeof(uint32_t));
189
+
190
+ for (u32 i = 0; i < context->argc; ++i) {
191
+ m3ApiWriteMem32(&argv[i], m3ApiPtrToOffset(argv_buf));
192
+
193
+ size_t len = strlen(context->argv[i]);
194
+
195
+ m3ApiCheckMem(argv_buf, len + 1); // include the terminating `\0`
196
+ memcpy(argv_buf, context->argv[i], len);
197
+ argv_buf += len;
198
+ *argv_buf++ = 0;
199
+ }
200
+
201
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
202
+ }
203
+
204
+ m3ApiRawFunction(m3_wasi_p1_args_sizes_get)
205
+ {
206
+ m3ApiReturnType(uint32_t)
207
+ m3ApiGetArgMem(__wasi_size_t*, argc)
208
+ m3ApiGetArgMem(__wasi_size_t*, argv_buf_size)
209
+
210
+ m3ApiCheckMem(argc, sizeof(__wasi_size_t));
211
+ m3ApiCheckMem(argv_buf_size, sizeof(__wasi_size_t));
212
+
213
+ m3_wasi_context_t* context = (m3_wasi_context_t*)(_ctx->userdata);
214
+
215
+ if (context == NULL) {
216
+ m3ApiReturn(__WASI_ERRNO_INVAL);
217
+ }
218
+
219
+ __wasi_size_t buf_len = 0;
220
+ for (u32 i = 0; i < context->argc; ++i) {
221
+ buf_len += strlen(context->argv[i]) + 1;
222
+ }
223
+
224
+ m3ApiWriteMem32(argc, context->argc);
225
+ m3ApiWriteMem32(argv_buf_size, buf_len);
226
+
227
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
228
+ }
229
+
230
+ m3ApiRawFunction(m3_wasi_p1_environ_get)
231
+ {
232
+ m3ApiReturnType(uint32_t)
233
+ m3ApiGetArgMem(uint32_t*, env)
234
+ m3ApiGetArgMem(char*, env_buf)
235
+
236
+ __wasi_errno_t ret;
237
+ __wasi_size_t env_count, env_buf_size;
238
+
239
+ ret = __wasi_environ_sizes_get(&env_count, &env_buf_size);
240
+ if (ret != __WASI_ERRNO_SUCCESS) {
241
+ m3ApiReturn(ret);
242
+ }
243
+
244
+ m3ApiCheckMem(env, env_count * sizeof(uint32_t));
245
+ m3ApiCheckMem(env_buf, env_buf_size);
246
+
247
+ // the array is the guest's, but the host fills it with its own pointers, which
248
+ // are what m3ApiPtrToOffset turns back into guest offsets below
249
+ uint8_t** host_env = (uint8_t**)env;
250
+
251
+ ret = __wasi_environ_get(host_env, (uint8_t*)env_buf);
252
+ if (ret != __WASI_ERRNO_SUCCESS) {
253
+ m3ApiReturn(ret);
254
+ }
255
+
256
+ for (u32 i = 0; i < env_count; ++i) {
257
+ m3ApiWriteMem32(&env[i], m3ApiPtrToOffset(host_env[i]));
258
+ }
259
+
260
+ m3ApiReturn(ret);
261
+ }
262
+
263
+ m3ApiRawFunction(m3_wasi_p1_environ_sizes_get)
264
+ {
265
+ m3ApiReturnType(uint32_t)
266
+ m3ApiGetArgMem(__wasi_size_t*, env_count)
267
+ m3ApiGetArgMem(__wasi_size_t*, env_buf_size)
268
+
269
+ m3ApiCheckMem(env_count, sizeof(__wasi_size_t));
270
+ m3ApiCheckMem(env_buf_size, sizeof(__wasi_size_t));
271
+
272
+ __wasi_errno_t ret = __wasi_environ_sizes_get(env_count, env_buf_size);
273
+
274
+ m3ApiReturn(ret);
275
+ }
276
+
277
+ m3ApiRawFunction(m3_wasi_p1_fd_prestat_dir_name)
278
+ {
279
+ m3ApiReturnType(uint32_t)
280
+ m3ApiGetArg(__wasi_fd_t, fd)
281
+ m3ApiGetArgMem(char*, path)
282
+ m3ApiGetArg(__wasi_size_t, path_len)
283
+
284
+ m3ApiCheckMem(path, path_len);
285
+
286
+ __wasi_errno_t ret = __wasi_fd_prestat_dir_name(fd, (uint8_t*)path, path_len);
287
+
288
+ WASI_TRACE("fd:%d, len:%d | path:%s", fd, path_len, path);
289
+
290
+ m3ApiReturn(ret);
291
+ }
292
+
293
+ m3ApiRawFunction(m3_wasi_p1_fd_prestat_get)
294
+ {
295
+ m3ApiReturnType(uint32_t)
296
+ m3ApiGetArg(__wasi_fd_t, fd)
297
+ m3ApiGetArgMem(__wasi_prestat_t*, buf)
298
+
299
+ m3ApiCheckMem(buf, sizeof(__wasi_prestat_t));
300
+
301
+ __wasi_errno_t ret = __wasi_fd_prestat_get(fd, buf);
302
+
303
+ WASI_TRACE("fd:%d | type:%d, name_len:%d", fd, buf->pr_type, buf->u.dir.pr_name_len);
304
+
305
+ m3ApiReturn(ret);
306
+ }
307
+
308
+ m3ApiRawFunction(m3_wasi_p1_fd_fdstat_get)
309
+ {
310
+ m3ApiReturnType(uint32_t)
311
+ m3ApiGetArg(__wasi_fd_t, fd)
312
+ m3ApiGetArgMem(uint8_t*, fdstat)
313
+
314
+ m3ApiCheckMem(fdstat, 24); // wasi_fdstat_t
315
+
316
+ // The guest's buffer lands wherever its linear memory happens to sit, so it can't be
317
+ // handed to the host directly: fdstat_t is 8-aligned and strict runtimes reject a
318
+ // misaligned pointer. Bounce it through a local, as with filestat below.
319
+ __wasi_fdstat_t stat;
320
+
321
+ __wasi_errno_t ret = __wasi_fd_fdstat_get(fd, &stat);
322
+
323
+ WASI_TRACE("fd:%d", fd);
324
+
325
+ if (ret != __WASI_ERRNO_SUCCESS) {
326
+ m3ApiReturn(ret);
327
+ }
328
+
329
+ memset(fdstat, 0, 24);
330
+ m3ApiWriteMem8(fdstat + 0, stat.fs_filetype);
331
+ m3ApiWriteMem16(fdstat + 2, stat.fs_flags);
332
+ m3ApiWriteMem64(fdstat + 8, stat.fs_rights_base);
333
+ m3ApiWriteMem64(fdstat + 16, stat.fs_rights_inheriting);
334
+
335
+ m3ApiReturn(ret);
336
+ }
337
+
338
+ m3ApiRawFunction(m3_wasi_p1_fd_fdstat_set_flags)
339
+ {
340
+ m3ApiReturnType(uint32_t)
341
+ m3ApiGetArg(__wasi_fd_t, fd)
342
+ m3ApiGetArg(__wasi_fdflags_t, flags)
343
+
344
+ __wasi_errno_t ret = __wasi_fd_fdstat_set_flags(fd, flags);
345
+
346
+ WASI_TRACE("fd:%d, flags:0x%x", fd, flags);
347
+
348
+ m3ApiReturn(ret);
349
+ }
350
+
351
+ m3ApiRawFunction(m3_wasi_p1_fd_fdstat_set_rights)
352
+ {
353
+ m3ApiReturnType(uint32_t)
354
+ m3ApiGetArg(__wasi_fd_t, fd)
355
+ m3ApiGetArg(__wasi_rights_t, rights_base)
356
+ m3ApiGetArg(__wasi_rights_t, rights_inheriting)
357
+
358
+ # if 0
359
+ __wasi_errno_t ret = __wasi_fd_fdstat_set_rights(fd, rights_base, rights_inheriting);
360
+ # else
361
+ __wasi_errno_t ret = __WASI_ERRNO_INVAL;
362
+ # endif
363
+
364
+ WASI_TRACE("fd:%d, base:0x%" PRIx64 ", inheriting:0x%" PRIx64, fd, rights_base, rights_inheriting);
365
+
366
+ m3ApiReturn(ret);
367
+ }
368
+
369
+ m3ApiRawFunction(m3_wasi_p1_fd_filestat_set_size)
370
+ {
371
+ m3ApiReturnType(uint32_t)
372
+ m3ApiGetArg(__wasi_fd_t, fd)
373
+ m3ApiGetArg(__wasi_filesize_t, size)
374
+
375
+ __wasi_errno_t ret = __wasi_fd_filestat_set_size(fd, size);
376
+
377
+ WASI_TRACE("fd:%d, size:%" PRIu64, fd, size);
378
+
379
+ m3ApiReturn(ret);
380
+ }
381
+
382
+ m3ApiRawFunction(m3_wasi_p1_fd_filestat_set_times)
383
+ {
384
+ m3ApiReturnType(uint32_t)
385
+ m3ApiGetArg(__wasi_fd_t, fd)
386
+ m3ApiGetArg(__wasi_timestamp_t, atim)
387
+ m3ApiGetArg(__wasi_timestamp_t, mtim)
388
+ m3ApiGetArg(__wasi_fstflags_t, fst_flags)
389
+
390
+ __wasi_errno_t ret = __wasi_fd_filestat_set_times(fd, atim, mtim, fst_flags);
391
+
392
+ WASI_TRACE("fd:%d, atim:%" PRIu64 ", mtim:%" PRIu64 ", flags:%d", fd, atim, mtim, fst_flags);
393
+
394
+ m3ApiReturn(ret);
395
+ }
396
+
397
+ m3ApiRawFunction(m3_wasi_p1_fd_filestat_get)
398
+ {
399
+ m3ApiReturnType(uint32_t)
400
+ m3ApiGetArg(__wasi_fd_t, fd)
401
+ m3ApiGetArgMem(uint8_t*, buf)
402
+
403
+ m3ApiCheckMem(buf, 64); // wasi_filestat_t
404
+
405
+ __wasi_filestat_t stat;
406
+
407
+ __wasi_errno_t ret = __wasi_fd_filestat_get(fd, &stat);
408
+
409
+ WASI_TRACE("fd:%d | fs.size:%" PRIu64, fd, stat.WASI_STAT_FIELD(size));
410
+
411
+ if (ret != __WASI_ERRNO_SUCCESS) {
412
+ m3ApiReturn(ret);
413
+ }
414
+
415
+ memset(buf, 0, 64);
416
+ m3ApiWriteMem64(buf + 0, stat.WASI_STAT_FIELD(dev));
417
+ m3ApiWriteMem64(buf + 8, stat.WASI_STAT_FIELD(ino));
418
+ m3ApiWriteMem8(buf + 16, stat.WASI_STAT_FIELD(filetype));
419
+ m3ApiWriteMem64(buf + 24, stat.WASI_STAT_FIELD(nlink));
420
+ m3ApiWriteMem64(buf + 32, stat.WASI_STAT_FIELD(size));
421
+ m3ApiWriteMem64(buf + 40, stat.WASI_STAT_FIELD(atim));
422
+ m3ApiWriteMem64(buf + 48, stat.WASI_STAT_FIELD(mtim));
423
+ m3ApiWriteMem64(buf + 56, stat.WASI_STAT_FIELD(ctim));
424
+
425
+ m3ApiReturn(ret);
426
+ }
427
+
428
+ m3ApiRawFunction(m3_wasi_p1_fd_seek)
429
+ {
430
+ m3ApiReturnType(uint32_t)
431
+ m3ApiGetArg(__wasi_fd_t, fd)
432
+ m3ApiGetArg(__wasi_filedelta_t, offset)
433
+ m3ApiGetArg(uint32_t, wasi_whence)
434
+ m3ApiGetArgMem(__wasi_filesize_t*, result)
435
+
436
+ m3ApiCheckMem(result, sizeof(__wasi_filesize_t));
437
+
438
+ __wasi_whence_t whence = -1;
439
+ switch (wasi_whence) {
440
+ case 0: whence = __WASI_WHENCE_SET; break;
441
+ case 1: whence = __WASI_WHENCE_CUR; break;
442
+ case 2: whence = __WASI_WHENCE_END; break;
443
+ }
444
+
445
+ __wasi_filesize_t pos;
446
+ __wasi_errno_t ret = __wasi_fd_seek(fd, offset, whence, &pos);
447
+
448
+ WASI_TRACE("fd:%d, offset:%" PRIu64 ", whence:%s | result:%" PRIu64,
449
+ fd, offset, wasi_whence2str(whence), pos);
450
+
451
+ *result = pos;
452
+
453
+ m3ApiReturn(ret);
454
+ }
455
+
456
+ m3ApiRawFunction(m3_wasi_p1_fd_renumber)
457
+ {
458
+ m3ApiReturnType(uint32_t)
459
+ m3ApiGetArg(__wasi_fd_t, from)
460
+ m3ApiGetArg(__wasi_fd_t, to)
461
+
462
+ __wasi_errno_t ret = __wasi_fd_renumber(from, to);
463
+
464
+ WASI_TRACE("from:%d, to:%d", from, to);
465
+
466
+ m3ApiReturn(ret);
467
+ }
468
+
469
+ m3ApiRawFunction(m3_wasi_p1_fd_sync)
470
+ {
471
+ m3ApiReturnType(uint32_t)
472
+ m3ApiGetArg(__wasi_fd_t, fd)
473
+
474
+ __wasi_errno_t ret = __wasi_fd_sync(fd);
475
+
476
+ WASI_TRACE("fd:%d", fd);
477
+
478
+ m3ApiReturn(ret);
479
+ }
480
+
481
+ m3ApiRawFunction(m3_wasi_p1_fd_tell)
482
+ {
483
+ m3ApiReturnType(uint32_t)
484
+ m3ApiGetArg(__wasi_fd_t, fd)
485
+ m3ApiGetArgMem(__wasi_filesize_t*, result)
486
+
487
+ m3ApiCheckMem(result, sizeof(__wasi_filesize_t));
488
+
489
+ __wasi_filesize_t pos;
490
+ __wasi_errno_t ret = __wasi_fd_tell(fd, &pos);
491
+
492
+ WASI_TRACE("fd:%d | result:%" PRIu64, fd, pos);
493
+
494
+ *result = pos;
495
+
496
+ m3ApiReturn(ret);
497
+ }
498
+
499
+ m3ApiRawFunction(m3_wasi_p1_path_create_directory)
500
+ {
501
+ m3ApiReturnType(uint32_t)
502
+ m3ApiGetArg(__wasi_fd_t, fd)
503
+ m3ApiGetArgMem(const char*, path)
504
+ m3ApiGetArg(__wasi_size_t, path_len)
505
+
506
+ m3ApiCheckMem(path, path_len);
507
+
508
+ d_m3WasiPath(p, path, path_len)
509
+
510
+ __wasi_errno_t ret = __wasi_path_create_directory(fd, p);
511
+
512
+ WASI_TRACE("fd:%d, path:%.*s", fd, (int)path_len, path);
513
+
514
+ m3ApiReturn(ret);
515
+ }
516
+
517
+ m3ApiRawFunction(m3_wasi_p1_path_readlink)
518
+ {
519
+ m3ApiReturnType(uint32_t)
520
+ m3ApiGetArg(__wasi_fd_t, fd)
521
+ m3ApiGetArgMem(const char*, path)
522
+ m3ApiGetArg(__wasi_size_t, path_len)
523
+ m3ApiGetArgMem(char*, buf)
524
+ m3ApiGetArg(__wasi_size_t, buf_len)
525
+ m3ApiGetArgMem(__wasi_size_t*, bufused)
526
+
527
+ m3ApiCheckMem(path, path_len);
528
+ m3ApiCheckMem(buf, buf_len);
529
+ m3ApiCheckMem(bufused, sizeof(__wasi_size_t));
530
+
531
+ d_m3WasiPath(p, path, path_len)
532
+
533
+ __wasi_errno_t ret = __wasi_path_readlink(fd, p, (uint8_t*)buf, buf_len, bufused);
534
+
535
+ WASI_TRACE("fd:%d, path:%.*s | buf:%s, bufused:%d", fd, (int)path_len, path, buf, *bufused);
536
+
537
+ m3ApiReturn(ret);
538
+ }
539
+
540
+ m3ApiRawFunction(m3_wasi_p1_path_remove_directory)
541
+ {
542
+ m3ApiReturnType(uint32_t)
543
+ m3ApiGetArg(__wasi_fd_t, fd)
544
+ m3ApiGetArgMem(const char*, path)
545
+ m3ApiGetArg(__wasi_size_t, path_len)
546
+
547
+ m3ApiCheckMem(path, path_len);
548
+
549
+ d_m3WasiPath(p, path, path_len)
550
+
551
+ __wasi_errno_t ret = __wasi_path_remove_directory(fd, p);
552
+
553
+ WASI_TRACE("fd:%d, path:%.*s", fd, (int)path_len, path);
554
+
555
+ m3ApiReturn(ret);
556
+ }
557
+
558
+ m3ApiRawFunction(m3_wasi_p1_path_rename)
559
+ {
560
+ m3ApiReturnType(uint32_t)
561
+ m3ApiGetArg(__wasi_fd_t, old_fd)
562
+ m3ApiGetArgMem(const char*, old_path)
563
+ m3ApiGetArg(__wasi_size_t, old_path_len)
564
+ m3ApiGetArg(__wasi_fd_t, new_fd)
565
+ m3ApiGetArgMem(const char*, new_path)
566
+ m3ApiGetArg(__wasi_size_t, new_path_len)
567
+
568
+ m3ApiCheckMem(old_path, old_path_len);
569
+ m3ApiCheckMem(new_path, new_path_len);
570
+
571
+ d_m3WasiPath(op, old_path, old_path_len)
572
+ d_m3WasiPath(np, new_path, new_path_len)
573
+
574
+ __wasi_errno_t ret = __wasi_path_rename(old_fd, op,
575
+ new_fd, np);
576
+
577
+ WASI_TRACE("old_fd:%d, old_path:%.*s, new_fd:%d, new_path:%.*s",
578
+ old_fd, (int)old_path_len, old_path, new_fd, (int)new_path_len, new_path);
579
+
580
+ m3ApiReturn(ret);
581
+ }
582
+
583
+ m3ApiRawFunction(m3_wasi_p1_path_symlink)
584
+ {
585
+ m3ApiReturnType(uint32_t)
586
+ m3ApiGetArgMem(const char*, old_path)
587
+ m3ApiGetArg(__wasi_size_t, old_path_len)
588
+ m3ApiGetArg(__wasi_fd_t, fd)
589
+ m3ApiGetArgMem(const char*, new_path)
590
+ m3ApiGetArg(__wasi_size_t, new_path_len)
591
+
592
+ m3ApiCheckMem(old_path, old_path_len);
593
+ m3ApiCheckMem(new_path, new_path_len);
594
+
595
+ d_m3WasiPath(op, old_path, old_path_len)
596
+ d_m3WasiPath(np, new_path, new_path_len)
597
+
598
+ __wasi_errno_t ret = __wasi_path_symlink(op,
599
+ fd, np);
600
+
601
+ WASI_TRACE("old_path:%.*s, fd:%d, new_path:%.*s",
602
+ (int)old_path_len, old_path, fd, (int)new_path_len, new_path);
603
+
604
+ m3ApiReturn(ret);
605
+ }
606
+
607
+ m3ApiRawFunction(m3_wasi_p1_path_unlink_file)
608
+ {
609
+ m3ApiReturnType(uint32_t)
610
+ m3ApiGetArg(__wasi_fd_t, fd)
611
+ m3ApiGetArgMem(const char*, path)
612
+ m3ApiGetArg(__wasi_size_t, path_len)
613
+
614
+ m3ApiCheckMem(path, path_len);
615
+
616
+ d_m3WasiPath(p, path, path_len)
617
+
618
+ __wasi_errno_t ret = __wasi_path_unlink_file(fd, p);
619
+
620
+ WASI_TRACE("fd:%d, path:%.*s", fd, (int)path_len, path);
621
+
622
+ m3ApiReturn(ret);
623
+ }
624
+
625
+ m3ApiRawFunction(m3_wasi_p1_path_open)
626
+ {
627
+ m3ApiReturnType(uint32_t)
628
+ m3ApiGetArg(__wasi_fd_t, dirfd)
629
+ m3ApiGetArg(__wasi_lookupflags_t, dirflags)
630
+ m3ApiGetArgMem(const char*, path)
631
+ m3ApiGetArg(__wasi_size_t, path_len)
632
+ m3ApiGetArg(__wasi_oflags_t, oflags)
633
+ m3ApiGetArg(__wasi_rights_t, fs_rights_base)
634
+ m3ApiGetArg(__wasi_rights_t, fs_rights_inheriting)
635
+ m3ApiGetArg(__wasi_fdflags_t, fs_flags)
636
+ m3ApiGetArgMem(__wasi_fd_t*, fd)
637
+
638
+ m3ApiCheckMem(path, path_len);
639
+ m3ApiCheckMem(fd, sizeof(__wasi_fd_t));
640
+
641
+ d_m3WasiPath(p, path, path_len)
642
+
643
+ __wasi_errno_t ret = __wasi_path_open(dirfd,
644
+ dirflags,
645
+ p,
646
+ oflags,
647
+ fs_rights_base,
648
+ fs_rights_inheriting,
649
+ fs_flags,
650
+ fd);
651
+
652
+ WASI_TRACE("dirfd:%d, dirflags:0x%x, path:%.*s, oflags:0x%x, fs_flags:0x%x | fd:%d",
653
+ dirfd, dirflags, (int)path_len, path, oflags, fs_flags, *fd);
654
+
655
+ m3ApiReturn(ret);
656
+ }
657
+
658
+ m3ApiRawFunction(m3_wasi_p1_path_filestat_get)
659
+ {
660
+ m3ApiReturnType(uint32_t)
661
+ m3ApiGetArg(__wasi_fd_t, fd)
662
+ m3ApiGetArg(__wasi_lookupflags_t, flags)
663
+ m3ApiGetArgMem(const char*, path)
664
+ m3ApiGetArg(uint32_t, path_len)
665
+ m3ApiGetArgMem(uint8_t*, buf)
666
+
667
+ m3ApiCheckMem(path, path_len);
668
+ m3ApiCheckMem(buf, 64); // wasi_filestat_t
669
+
670
+ __wasi_filestat_t stat;
671
+
672
+ d_m3WasiPath(p, path, path_len)
673
+
674
+ __wasi_errno_t ret = __wasi_path_filestat_get(fd, flags, p, &stat);
675
+
676
+ WASI_TRACE("fd:%d, flags:0x%x, path:%.*s | fs.size:%" PRIu64,
677
+ fd, flags, (int)path_len, path, stat.WASI_STAT_FIELD(size));
678
+
679
+ if (ret != __WASI_ERRNO_SUCCESS) {
680
+ m3ApiReturn(ret);
681
+ }
682
+
683
+ memset(buf, 0, 64);
684
+ m3ApiWriteMem64(buf + 0, stat.WASI_STAT_FIELD(dev));
685
+ m3ApiWriteMem64(buf + 8, stat.WASI_STAT_FIELD(ino));
686
+ m3ApiWriteMem8(buf + 16, stat.WASI_STAT_FIELD(filetype));
687
+ m3ApiWriteMem64(buf + 24, stat.WASI_STAT_FIELD(nlink));
688
+ m3ApiWriteMem64(buf + 32, stat.WASI_STAT_FIELD(size));
689
+ m3ApiWriteMem64(buf + 40, stat.WASI_STAT_FIELD(atim));
690
+ m3ApiWriteMem64(buf + 48, stat.WASI_STAT_FIELD(mtim));
691
+ m3ApiWriteMem64(buf + 56, stat.WASI_STAT_FIELD(ctim));
692
+
693
+ m3ApiReturn(ret);
694
+ }
695
+
696
+ m3ApiRawFunction(m3_wasi_p1_fd_pread)
697
+ {
698
+ m3ApiReturnType(uint32_t)
699
+ m3ApiGetArg(__wasi_fd_t, fd)
700
+ m3ApiGetArgMem(__wasi_iovec_t*, wasi_iovs)
701
+ m3ApiGetArg(__wasi_size_t, iovs_len)
702
+ m3ApiGetArg(__wasi_filesize_t, offset)
703
+ m3ApiGetArgMem(__wasi_size_t*, nread)
704
+
705
+ m3ApiCheckMem(wasi_iovs, iovs_len * sizeof(__wasi_iovec_t));
706
+ m3ApiCheckMem(nread, sizeof(__wasi_size_t));
707
+
708
+ __wasi_iovec_t iovs[iovs_len];
709
+ const void* mem_check = copy_iov_to_host(runtime, _mem, iovs, wasi_iovs, iovs_len);
710
+ if (mem_check != m3Err_none) {
711
+ return mem_check;
712
+ }
713
+
714
+ __wasi_errno_t ret = __wasi_fd_pread(fd, iovs, iovs_len, offset, nread);
715
+
716
+ WASI_TRACE("fd:%d | nread:%d", fd, *nread);
717
+
718
+ m3ApiReturn(ret);
719
+ }
720
+
721
+ m3ApiRawFunction(m3_wasi_p1_fd_read)
722
+ {
723
+ m3ApiReturnType(uint32_t)
724
+ m3ApiGetArg(__wasi_fd_t, fd)
725
+ m3ApiGetArgMem(__wasi_iovec_t*, wasi_iovs)
726
+ m3ApiGetArg(__wasi_size_t, iovs_len)
727
+ m3ApiGetArgMem(__wasi_size_t*, nread)
728
+
729
+ m3ApiCheckMem(wasi_iovs, iovs_len * sizeof(__wasi_iovec_t));
730
+ m3ApiCheckMem(nread, sizeof(__wasi_size_t));
731
+
732
+ __wasi_iovec_t iovs[iovs_len];
733
+ const void* mem_check = copy_iov_to_host(runtime, _mem, iovs, wasi_iovs, iovs_len);
734
+ if (mem_check != m3Err_none) {
735
+ return mem_check;
736
+ }
737
+
738
+ __wasi_errno_t ret = __wasi_fd_read(fd, iovs, iovs_len, nread);
739
+
740
+ WASI_TRACE("fd:%d | nread:%d", fd, *nread);
741
+
742
+ m3ApiReturn(ret);
743
+ }
744
+
745
+ m3ApiRawFunction(m3_wasi_p1_fd_write)
746
+ {
747
+ m3ApiReturnType(uint32_t)
748
+ m3ApiGetArg(__wasi_fd_t, fd)
749
+ m3ApiGetArgMem(__wasi_iovec_t*, wasi_iovs)
750
+ m3ApiGetArg(__wasi_size_t, iovs_len)
751
+ m3ApiGetArgMem(__wasi_size_t*, nwritten)
752
+
753
+ m3ApiCheckMem(wasi_iovs, iovs_len * sizeof(__wasi_iovec_t));
754
+ m3ApiCheckMem(nwritten, sizeof(__wasi_size_t));
755
+
756
+ __wasi_iovec_t iovs[iovs_len];
757
+ const void* mem_check = copy_iov_to_host(runtime, _mem, iovs, wasi_iovs, iovs_len);
758
+ if (mem_check != m3Err_none) {
759
+ return mem_check;
760
+ }
761
+
762
+ __wasi_errno_t ret = __wasi_fd_write(fd, (__wasi_ciovec_t*)iovs, iovs_len, nwritten);
763
+
764
+ WASI_TRACE("fd:%d | nwritten:%d", fd, *nwritten);
765
+
766
+ m3ApiReturn(ret);
767
+ }
768
+
769
+ m3ApiRawFunction(m3_wasi_p1_fd_pwrite)
770
+ {
771
+ m3ApiReturnType(uint32_t)
772
+ m3ApiGetArg(__wasi_fd_t, fd)
773
+ m3ApiGetArgMem(__wasi_iovec_t*, wasi_iovs)
774
+ m3ApiGetArg(__wasi_size_t, iovs_len)
775
+ m3ApiGetArg(__wasi_filesize_t, offset)
776
+ m3ApiGetArgMem(__wasi_size_t*, nwritten)
777
+
778
+ m3ApiCheckMem(wasi_iovs, iovs_len * sizeof(__wasi_iovec_t));
779
+ m3ApiCheckMem(nwritten, sizeof(__wasi_size_t));
780
+
781
+ __wasi_iovec_t iovs[iovs_len];
782
+ const void* mem_check = copy_iov_to_host(runtime, _mem, iovs, wasi_iovs, iovs_len);
783
+ if (mem_check != m3Err_none) {
784
+ return mem_check;
785
+ }
786
+
787
+ __wasi_errno_t ret = __wasi_fd_pwrite(fd, (__wasi_ciovec_t*)iovs, iovs_len, offset, nwritten);
788
+
789
+ WASI_TRACE("fd:%d | nwritten:%d", fd, *nwritten);
790
+
791
+ m3ApiReturn(ret);
792
+ }
793
+
794
+ m3ApiRawFunction(m3_wasi_p1_fd_readdir)
795
+ {
796
+ m3ApiReturnType(uint32_t)
797
+ m3ApiGetArg(__wasi_fd_t, fd)
798
+ m3ApiGetArgMem(void*, buf)
799
+ m3ApiGetArg(__wasi_size_t, buf_len)
800
+ m3ApiGetArg(__wasi_dircookie_t, cookie)
801
+ m3ApiGetArgMem(__wasi_size_t*, bufused)
802
+
803
+ m3ApiCheckMem(buf, buf_len);
804
+ m3ApiCheckMem(bufused, sizeof(__wasi_size_t));
805
+
806
+ __wasi_errno_t ret = __wasi_fd_readdir(fd, buf, buf_len, cookie, bufused);
807
+
808
+ WASI_TRACE("fd:%d | bufused:%d", fd, *bufused);
809
+
810
+ m3ApiReturn(ret);
811
+ }
812
+
813
+ m3ApiRawFunction(m3_wasi_p1_fd_advise)
814
+ {
815
+ m3ApiReturnType(uint32_t)
816
+ m3ApiGetArg(__wasi_fd_t, fd)
817
+ m3ApiGetArg(__wasi_filesize_t, offset)
818
+ m3ApiGetArg(__wasi_filesize_t, length)
819
+ m3ApiGetArg(__wasi_advice_t, advice)
820
+
821
+ __wasi_errno_t ret = __wasi_fd_advise(fd, offset, length, advice);
822
+
823
+ WASI_TRACE("fd:%d, offset:%" PRIu64 ", length:%" PRIu64 ", advice:%d", fd, offset, length, advice);
824
+
825
+ m3ApiReturn(ret);
826
+ }
827
+
828
+ m3ApiRawFunction(m3_wasi_p1_fd_allocate)
829
+ {
830
+ m3ApiReturnType(uint32_t)
831
+ m3ApiGetArg(__wasi_fd_t, fd)
832
+ m3ApiGetArg(__wasi_filesize_t, offset)
833
+ m3ApiGetArg(__wasi_filesize_t, length)
834
+
835
+ __wasi_errno_t ret = __wasi_fd_allocate(fd, offset, length);
836
+
837
+ WASI_TRACE("fd:%d, offset:%" PRIu64 ", length:%" PRIu64, fd, offset, length);
838
+
839
+ m3ApiReturn(ret);
840
+ }
841
+
842
+ m3ApiRawFunction(m3_wasi_p1_fd_close)
843
+ {
844
+ m3ApiReturnType(uint32_t)
845
+ m3ApiGetArg(__wasi_fd_t, fd)
846
+
847
+ __wasi_errno_t ret = __wasi_fd_close(fd);
848
+
849
+ WASI_TRACE("fd:%d", fd);
850
+
851
+ m3ApiReturn(ret);
852
+ }
853
+
854
+ m3ApiRawFunction(m3_wasi_p1_fd_datasync)
855
+ {
856
+ m3ApiReturnType(uint32_t)
857
+ m3ApiGetArg(__wasi_fd_t, fd)
858
+
859
+ __wasi_errno_t ret = __wasi_fd_datasync(fd);
860
+
861
+ WASI_TRACE("fd:%d", fd);
862
+
863
+ m3ApiReturn(ret);
864
+ }
865
+
866
+ m3ApiRawFunction(m3_wasi_p1_random_get)
867
+ {
868
+ m3ApiReturnType(uint32_t)
869
+ m3ApiGetArgMem(uint8_t*, buf)
870
+ m3ApiGetArg(__wasi_size_t, buf_len)
871
+
872
+ m3ApiCheckMem(buf, buf_len);
873
+
874
+ __wasi_errno_t ret = __wasi_random_get(buf, buf_len);
875
+
876
+ WASI_TRACE("len:%d", buf_len);
877
+
878
+ m3ApiReturn(ret);
879
+ }
880
+
881
+ m3ApiRawFunction(m3_wasi_p1_clock_res_get)
882
+ {
883
+ m3ApiReturnType(uint32_t)
884
+ m3ApiGetArg(__wasi_clockid_t, wasi_clk_id)
885
+ m3ApiGetArgMem(__wasi_timestamp_t*, resolution)
886
+
887
+ m3ApiCheckMem(resolution, sizeof(__wasi_timestamp_t));
888
+
889
+ __wasi_timestamp_t t;
890
+ __wasi_errno_t ret = __wasi_clock_res_get(wasi_clk_id, &t);
891
+
892
+ WASI_TRACE("clk_id:%d | res:%" PRIu64, wasi_clk_id, t);
893
+
894
+ *resolution = t;
895
+
896
+ m3ApiReturn(ret);
897
+ }
898
+
899
+ m3ApiRawFunction(m3_wasi_p1_clock_time_get)
900
+ {
901
+ m3ApiReturnType(uint32_t)
902
+ m3ApiGetArg(__wasi_clockid_t, wasi_clk_id)
903
+ m3ApiGetArg(__wasi_timestamp_t, precision)
904
+ m3ApiGetArgMem(__wasi_timestamp_t*, time)
905
+
906
+ m3ApiCheckMem(time, sizeof(__wasi_timestamp_t));
907
+
908
+ __wasi_timestamp_t t;
909
+ __wasi_errno_t ret = __wasi_clock_time_get(wasi_clk_id, precision, &t);
910
+
911
+ WASI_TRACE("clk_id:%d | res:%" PRIu64, wasi_clk_id, t);
912
+
913
+ *time = t;
914
+
915
+ m3ApiReturn(ret);
916
+ }
917
+
918
+ m3ApiRawFunction(m3_wasi_p1_poll_oneoff)
919
+ {
920
+ m3ApiReturnType(uint32_t)
921
+ m3ApiGetArgMem(const __wasi_subscription_t*, in)
922
+ m3ApiGetArgMem(__wasi_event_t*, out)
923
+ m3ApiGetArg(__wasi_size_t, nsubscriptions)
924
+ m3ApiGetArgMem(__wasi_size_t*, nevents)
925
+
926
+ m3ApiCheckMem(in, nsubscriptions * sizeof(__wasi_subscription_t));
927
+ m3ApiCheckMem(out, nsubscriptions * sizeof(__wasi_event_t));
928
+ m3ApiCheckMem(nevents, sizeof(__wasi_size_t));
929
+
930
+ __wasi_errno_t ret = __wasi_poll_oneoff(in, out, nsubscriptions, nevents);
931
+
932
+ WASI_TRACE("nsubscriptions:%d | nevents:%d", nsubscriptions, *nevents);
933
+
934
+ m3ApiReturn(ret);
935
+ }
936
+
937
+ m3ApiRawFunction(m3_wasi_p1_proc_exit)
938
+ {
939
+ m3ApiGetArg(uint32_t, code)
940
+
941
+ m3_wasi_context_t* context = (m3_wasi_context_t*)(_ctx->userdata);
942
+
943
+ if (context) {
944
+ context->exit_code = code;
945
+ }
946
+
947
+ m3ApiTrap(m3Err_trapWasiExit);
948
+ }
949
+
950
+ m3ApiRawFunction(m3_wasi_p1_proc_raise)
951
+ {
952
+ m3ApiReturnType(uint32_t)
953
+ // proc_raise was dropped from preview1, so newer wasi-libc no longer has
954
+ // __wasi_signal_t; the import signature is "i(i)" either way
955
+ m3ApiGetArg(uint32_t, sig)
956
+
957
+ __wasi_errno_t ret = __WASI_ERRNO_INVAL;
958
+ # if 0
959
+ ret = __wasi_proc_raise(sig);
960
+ # endif
961
+
962
+ WASI_TRACE("sig:%d", sig);
963
+
964
+ m3ApiReturn(ret);
965
+ }
966
+
967
+ m3ApiRawFunction(m3_wasi_p1_sched_yield)
968
+ {
969
+ m3ApiReturnType(uint32_t)
970
+ __wasi_errno_t ret = __wasi_sched_yield();
971
+
972
+ WASI_TRACE("");
973
+
974
+ m3ApiReturn(ret);
975
+ }
976
+
977
+
978
+ static
979
+ M3Result SuppressLookupFailure (M3Result i_result)
980
+ {
981
+ if (i_result == m3Err_functionLookupFailed) {
982
+ return m3Err_none;
983
+ } else {
984
+ return i_result;
985
+ }
986
+ }
987
+
988
+ m3_wasi_context_t* m3_GetWasiContext ()
989
+ {
990
+ return wasi_context;
991
+ }
992
+
993
+ void m3_FreeWasi (m3_wasi_context_t* wasi)
994
+ {
995
+ m3_Free(wasi);
996
+ }
997
+
998
+ static inline
999
+ M3Result _linkWASI (IM3Module module, m3_wasi_context_t* wasi_context)
1000
+ {
1001
+ M3Result result = m3Err_none;
1002
+
1003
+ static const char* namespaces[] = { "wasi_snapshot_preview1" };
1004
+
1005
+ // clang-format off
1006
+
1007
+ for (size_t i = 0; i < M3_COUNT_OF(namespaces); i++) {
1008
+ const char* wasi = namespaces[i];
1009
+
1010
+ _ (SuppressLookupFailure(m3_LinkRawFunctionEx(module, wasi, "args_get", "i(**)", &m3_wasi_p1_args_get, wasi_context)));
1011
+ _ (SuppressLookupFailure(m3_LinkRawFunctionEx(module, wasi, "args_sizes_get", "i(**)", &m3_wasi_p1_args_sizes_get, wasi_context)));
1012
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "clock_res_get", "i(i*)", &m3_wasi_p1_clock_res_get)));
1013
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "clock_time_get", "i(iI*)", &m3_wasi_p1_clock_time_get)));
1014
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "environ_get", "i(**)", &m3_wasi_p1_environ_get)));
1015
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "environ_sizes_get", "i(**)", &m3_wasi_p1_environ_sizes_get)));
1016
+
1017
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_advise", "i(iIIi)", &m3_wasi_p1_fd_advise)));
1018
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_allocate", "i(iII)", &m3_wasi_p1_fd_allocate)));
1019
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_close", "i(i)", &m3_wasi_p1_fd_close)));
1020
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_datasync", "i(i)", &m3_wasi_p1_fd_datasync)));
1021
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_fdstat_get", "i(i*)", &m3_wasi_p1_fd_fdstat_get)));
1022
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_fdstat_set_flags", "i(ii)", &m3_wasi_p1_fd_fdstat_set_flags)));
1023
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_fdstat_set_rights", "i(iII)", &m3_wasi_p1_fd_fdstat_set_rights)));
1024
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_filestat_get", "i(i*)", &m3_wasi_p1_fd_filestat_get)));
1025
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_filestat_set_size", "i(iI)", &m3_wasi_p1_fd_filestat_set_size)));
1026
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_filestat_set_times","i(iIIi)", &m3_wasi_p1_fd_filestat_set_times)));
1027
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_pread", "i(i*iI*)",&m3_wasi_p1_fd_pread)));
1028
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_prestat_get", "i(i*)", &m3_wasi_p1_fd_prestat_get)));
1029
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_prestat_dir_name", "i(i*i)", &m3_wasi_p1_fd_prestat_dir_name)));
1030
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_pwrite", "i(i*iI*)",&m3_wasi_p1_fd_pwrite)));
1031
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_read", "i(i*i*)", &m3_wasi_p1_fd_read)));
1032
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_readdir", "i(i*iI*)",&m3_wasi_p1_fd_readdir)));
1033
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_renumber", "i(ii)", &m3_wasi_p1_fd_renumber)));
1034
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_sync", "i(i)", &m3_wasi_p1_fd_sync)));
1035
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_seek", "i(iIi*)", &m3_wasi_p1_fd_seek)));
1036
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_tell", "i(i*)", &m3_wasi_p1_fd_tell)));
1037
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_write", "i(i*i*)", &m3_wasi_p1_fd_write)));
1038
+
1039
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "path_create_directory", "i(i*i)", &m3_wasi_p1_path_create_directory)));
1040
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "path_filestat_get", "i(ii*i*)", &m3_wasi_p1_path_filestat_get)));
1041
+ //_ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "path_filestat_set_times", "i(ii*iIIi)", )));
1042
+ //_ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "path_link", "i(ii*ii*i)", )));
1043
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "path_open", "i(ii*iiIIi*)", &m3_wasi_p1_path_open)));
1044
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "path_readlink", "i(i*i*i*)", &m3_wasi_p1_path_readlink)));
1045
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "path_remove_directory", "i(i*i)", &m3_wasi_p1_path_remove_directory)));
1046
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "path_rename", "i(i*ii*i)", &m3_wasi_p1_path_rename)));
1047
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "path_symlink", "i(*ii*i)", &m3_wasi_p1_path_symlink)));
1048
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "path_unlink_file", "i(i*i)", &m3_wasi_p1_path_unlink_file)));
1049
+
1050
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "poll_oneoff", "i(**i*)", &m3_wasi_p1_poll_oneoff)));
1051
+ _ (SuppressLookupFailure(m3_LinkRawFunctionEx(module, wasi, "proc_exit", "v(i)", &m3_wasi_p1_proc_exit, wasi_context)));
1052
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "proc_raise", "i(i)", &m3_wasi_p1_proc_raise)));
1053
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "random_get", "i(*i)", &m3_wasi_p1_random_get)));
1054
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "sched_yield", "i()", &m3_wasi_p1_sched_yield)));
1055
+
1056
+ //_ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "sock_recv", "i(i*ii**)", )));
1057
+ //_ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "sock_send", "i(i*ii*)", )));
1058
+ //_ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "sock_shutdown", "i(ii)", )));
1059
+ }
1060
+ // clang-format off
1061
+
1062
+ // WASI addresses the memory the module exports as "memory", not whichever
1063
+ // one the calling code happens to be running against. Resolve it once, here,
1064
+ // rather than per call. A module that linked none of the above imports has
1065
+ // no WASI to satisfy, so it is not asked for the export - link_all offers
1066
+ // WASI to every module, and most have neither the imports nor a memory.
1067
+ if (Module_HasLinkedHostImport(module, "wasi_snapshot_preview1")) {
1068
+ u32 memoryIndex;
1069
+
1070
+ if (m3_FindExportedMemory(module, "memory", &memoryIndex)) {
1071
+ _throw("WASI requires the module to export its memory as \"memory\"");
1072
+ }
1073
+
1074
+ _ (m3_BindImportMemory(module, "wasi_snapshot_preview1", memoryIndex));
1075
+ }
1076
+
1077
+ _catch:
1078
+ return result;
1079
+ }
1080
+
1081
+ M3Result m3_NewCustomWASI (M3WASI* wasi_p)
1082
+ {
1083
+ *wasi_p = m3_AllocStruct(m3_wasi_context_t);
1084
+
1085
+ return m3Err_none;
1086
+ }
1087
+
1088
+ void m3_FreeCustomWASI (M3WASI wasi)
1089
+ {
1090
+ m3_Free(wasi);
1091
+ }
1092
+
1093
+ M3Result m3_LinkCustomWASI (IM3Module module, M3WASI wasi)
1094
+ {
1095
+ return _linkWASI(module, wasi);
1096
+ }
1097
+
1098
+ M3Result m3_LinkWASI (IM3Module module)
1099
+ {
1100
+ if (!wasi_context) {
1101
+ wasi_context = m3_AllocStruct(m3_wasi_context_t);
1102
+ }
1103
+
1104
+ return _linkWASI(module, wasi_context);
1105
+ }
1106
+
1107
+ #endif // d_m3HasMetaWASI