xbintsc 0.6.24 → 0.6.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (224) hide show
  1. package/AGENTS.md +1 -1
  2. package/dist/src/cli/extensions.js +11 -3
  3. package/dist/src/cli/extensions.js.map +1 -1
  4. package/dist/src/cli/help.d.ts +1 -1
  5. package/dist/src/cli/help.js +1 -1
  6. package/dist/src/codegen/generator/state.js +7 -0
  7. package/dist/src/codegen/generator/state.js.map +1 -1
  8. package/dist/src/codegen/generator/tables.js +19 -1
  9. package/dist/src/codegen/generator/tables.js.map +1 -1
  10. package/dist/src/driver/bundler/graph.js +9 -5
  11. package/dist/src/driver/bundler/graph.js.map +1 -1
  12. package/dist/src/driver/fingerprint.d.ts +2 -2
  13. package/dist/src/driver/fingerprint.js +3 -3
  14. package/dist/src/driver/fingerprint.js.map +1 -1
  15. package/dist/src/driver/link-features.js +1 -1
  16. package/dist/src/driver/link-features.js.map +1 -1
  17. package/dist/src/driver/paths.js +8 -4
  18. package/dist/src/driver/paths.js.map +1 -1
  19. package/dist/src/driver/runtime-objects.d.ts +1 -1
  20. package/dist/src/driver/runtime-objects.js +13 -1
  21. package/dist/src/driver/runtime-objects.js.map +1 -1
  22. package/dist/src/driver/version.d.ts +1 -1
  23. package/dist/src/driver/version.js +1 -1
  24. package/dist/src/extensions/catalog.js +5 -1
  25. package/dist/src/extensions/catalog.js.map +1 -1
  26. package/dist/src/extensions/gui/index.js +0 -1
  27. package/dist/src/extensions/gui/index.js.map +1 -1
  28. package/dist/src/extensions/link-feature.d.ts +8 -0
  29. package/dist/src/extensions/node/stream-web/index.d.ts +6 -1
  30. package/dist/src/extensions/node/stream-web/index.js +59 -4
  31. package/dist/src/extensions/node/stream-web/index.js.map +1 -1
  32. package/dist/src/extensions/node/worker_threads/index.d.ts +14 -4
  33. package/dist/src/extensions/node/worker_threads/index.js +24 -11
  34. package/dist/src/extensions/node/worker_threads/index.js.map +1 -1
  35. package/dist/src/extensions/registry.d.ts +9 -1
  36. package/dist/src/extensions/registry.js +4 -1
  37. package/dist/src/extensions/registry.js.map +1 -1
  38. package/dist/src/extensions/wasm-asset.d.ts +16 -0
  39. package/dist/src/extensions/wasm-asset.js +28 -0
  40. package/dist/src/extensions/wasm-asset.js.map +1 -0
  41. package/dist/src/extensions/wasm.d.ts +17 -0
  42. package/dist/src/extensions/wasm.js +68 -0
  43. package/dist/src/extensions/wasm.js.map +1 -0
  44. package/dist/src/extensions/webworker/index.d.ts +24 -0
  45. package/dist/src/extensions/webworker/index.js +54 -0
  46. package/dist/src/extensions/webworker/index.js.map +1 -0
  47. package/dist/src/index.d.ts +1 -0
  48. package/dist/src/index.js +1 -0
  49. package/dist/src/index.js.map +1 -1
  50. package/dist/tests/driver/link-features.test.js +9 -0
  51. package/dist/tests/driver/link-features.test.js.map +1 -1
  52. package/dist/tests/e2e/async-fs.test.js +44 -0
  53. package/dist/tests/e2e/async-fs.test.js.map +1 -1
  54. package/dist/tests/e2e/harness.d.ts +6 -0
  55. package/dist/tests/e2e/harness.js +13 -1
  56. package/dist/tests/e2e/harness.js.map +1 -1
  57. package/dist/tests/e2e/node-crypto-random.test.d.ts +8 -0
  58. package/dist/tests/e2e/node-crypto-random.test.js +148 -0
  59. package/dist/tests/e2e/node-crypto-random.test.js.map +1 -0
  60. package/dist/tests/e2e/node-modules.test.js +1 -1
  61. package/dist/tests/e2e/node-modules.test.js.map +1 -1
  62. package/dist/tests/e2e/web-streams.test.d.ts +8 -0
  63. package/dist/tests/e2e/web-streams.test.js +274 -0
  64. package/dist/tests/e2e/web-streams.test.js.map +1 -0
  65. package/dist/tests/e2e/web-worker.test.d.ts +7 -0
  66. package/dist/tests/e2e/web-worker.test.js +74 -0
  67. package/dist/tests/e2e/web-worker.test.js.map +1 -0
  68. package/dist/tests/e2e/webassembly.test.d.ts +8 -0
  69. package/dist/tests/e2e/webassembly.test.js +113 -0
  70. package/dist/tests/e2e/webassembly.test.js.map +1 -0
  71. package/dist/tests/e2e/worker-api.test.d.ts +14 -0
  72. package/dist/tests/e2e/worker-api.test.js +460 -0
  73. package/dist/tests/e2e/worker-api.test.js.map +1 -0
  74. package/dist/tests/e2e/worker-threads.test.d.ts +8 -0
  75. package/dist/tests/e2e/worker-threads.test.js +160 -0
  76. package/dist/tests/e2e/worker-threads.test.js.map +1 -0
  77. package/dist/tests/extensions/wasm.test.d.ts +1 -0
  78. package/dist/tests/extensions/wasm.test.js +53 -0
  79. package/dist/tests/extensions/wasm.test.js.map +1 -0
  80. package/doc/ai/cli.md +1 -1
  81. package/doc/ai/contributing.md +11 -4
  82. package/doc/ai/extensions.md +25 -2
  83. package/doc/ai/language-support.md +14 -2
  84. package/doc/ai/zh-CN/cli.md +1 -1
  85. package/doc/ai/zh-CN/contributing.md +8 -3
  86. package/doc/ai/zh-CN/extensions.md +23 -2
  87. package/doc/ai/zh-CN/language-support.md +13 -2
  88. package/doc/gui/api.md +1 -1
  89. package/doc/implemented/cli.md +1 -1
  90. package/doc/implemented/extensions.md +6 -5
  91. package/doc/implemented/node/stream-web.md +31 -0
  92. package/doc/implemented/node/worker-threads.md +88 -5
  93. package/doc/implemented/node.md +2 -1
  94. package/doc/implemented/streams.md +82 -0
  95. package/doc/implemented/wasm.md +80 -18
  96. package/doc/implemented/web-worker.md +84 -0
  97. package/doc/implemented.md +6 -2
  98. package/doc/unimplemented/deviations.md +1 -0
  99. package/doc/unimplemented/node/worker-threads.md +23 -0
  100. package/doc/unimplemented/node.md +1 -0
  101. package/doc/unimplemented/stdlib.md +2 -1
  102. package/doc/unimplemented/wasm.md +21 -9
  103. package/doc/unimplemented.md +1 -1
  104. package/doc/zh-CN/gui/api.md +1 -1
  105. package/doc/zh-CN/implemented/cli.md +1 -1
  106. package/doc/zh-CN/implemented/extensions.md +7 -6
  107. package/doc/zh-CN/implemented/node/stream-web.md +24 -0
  108. package/doc/zh-CN/implemented/node/worker-threads.md +77 -4
  109. package/doc/zh-CN/implemented/node.md +2 -1
  110. package/doc/zh-CN/implemented/streams.md +60 -0
  111. package/doc/zh-CN/implemented/wasm.md +73 -16
  112. package/doc/zh-CN/implemented/web-worker.md +79 -0
  113. package/doc/zh-CN/implemented.md +6 -2
  114. package/doc/zh-CN/unimplemented/deviations.md +1 -0
  115. package/doc/zh-CN/unimplemented/node/worker-threads.md +20 -0
  116. package/doc/zh-CN/unimplemented/node.md +1 -0
  117. package/doc/zh-CN/unimplemented/stdlib.md +2 -1
  118. package/doc/zh-CN/unimplemented/wasm.md +16 -8
  119. package/doc/zh-CN/unimplemented.md +1 -1
  120. package/llms.txt +2 -0
  121. package/package.json +8 -7
  122. package/runtime/ext_gui/browser.cpp +1 -1
  123. package/runtime/ext_node/stream_web/stream_web.c +20 -6
  124. package/runtime/worker/worker_api.c +422 -0
  125. package/runtime/worker/worker_codec.c +489 -0
  126. package/runtime/worker/worker_core.c +445 -0
  127. package/runtime/worker/worker_internal.h +223 -0
  128. package/runtime/worker/worker_platform.c +344 -0
  129. package/runtime/worker/worker_ports.c +203 -0
  130. package/runtime/worker/worker_transport.c +464 -0
  131. package/runtime/worker/worker_wire.c +150 -0
  132. package/runtime/xt_builtins.c +15 -0
  133. package/runtime/xt_constructors.c +49 -2
  134. package/runtime/xt_stdlib2/error.inc +4 -0
  135. package/runtime/xt_streams/common.inc +232 -0
  136. package/runtime/xt_streams/constructors.inc +232 -0
  137. package/runtime/xt_streams/encoding.inc +124 -0
  138. package/runtime/xt_streams/readable-extra.inc +372 -0
  139. package/runtime/xt_streams/readable.inc +415 -0
  140. package/runtime/xt_streams/transform.inc +195 -0
  141. package/runtime/xt_streams/writable.inc +542 -0
  142. package/runtime/xt_streams.c +30 -0
  143. package/runtime/xt_wasm/wasm.h +148 -0
  144. package/runtime/xt_wasm/wasm_api.c +357 -0
  145. package/runtime/xt_wasm/wasm_api.h +37 -0
  146. package/runtime/xt_wasm/wasm_bridge.c +30 -0
  147. package/runtime/xt_wasm/wasm_instance.c +591 -0
  148. package/runtime/xt_wasm/wasm_module.c +270 -0
  149. package/runtime/xt_wasm/wasm_values.c +177 -0
  150. package/runtime/xt_webassembly.c +15 -4
  151. package/scripts/fetch-vendored.ts +187 -20
  152. package/scripts/package-release.ts +3 -2
  153. package/src/cli/extensions.ts +11 -3
  154. package/src/cli/help.ts +1 -1
  155. package/src/codegen/generator/state.ts +7 -0
  156. package/src/codegen/generator/tables.ts +19 -1
  157. package/src/driver/bundler/graph.ts +9 -5
  158. package/src/driver/fingerprint.ts +3 -3
  159. package/src/driver/link-features.ts +1 -1
  160. package/src/driver/paths.ts +8 -4
  161. package/src/driver/runtime-objects.ts +13 -1
  162. package/src/driver/version.ts +1 -1
  163. package/src/extensions/catalog.ts +5 -1
  164. package/src/extensions/gui/index.ts +0 -1
  165. package/src/extensions/link-feature.ts +8 -0
  166. package/src/extensions/node/stream-web/index.ts +59 -4
  167. package/src/extensions/node/worker_threads/index.ts +24 -11
  168. package/src/extensions/registry.ts +13 -2
  169. package/src/extensions/wasm-asset.ts +32 -0
  170. package/src/extensions/wasm.ts +74 -0
  171. package/src/extensions/webworker/index.ts +58 -0
  172. package/src/index.ts +1 -0
  173. package/vendor/wasm3/.pin +1 -0
  174. package/vendor/wasm3/LICENSE +21 -0
  175. package/vendor/wasm3/m3_api_libc.c +265 -0
  176. package/vendor/wasm3/m3_api_libc.h +19 -0
  177. package/vendor/wasm3/m3_api_meta_wasi.c +1107 -0
  178. package/vendor/wasm3/m3_api_tracer.c +173 -0
  179. package/vendor/wasm3/m3_api_tracer.h +19 -0
  180. package/vendor/wasm3/m3_api_uvwasi.c +1290 -0
  181. package/vendor/wasm3/m3_api_wasi.c +1207 -0
  182. package/vendor/wasm3/m3_api_wasi.h +74 -0
  183. package/vendor/wasm3/m3_api_wasi_host.h +137 -0
  184. package/vendor/wasm3/m3_api_wasi_posix.h +979 -0
  185. package/vendor/wasm3/m3_api_wasi_win32.h +1093 -0
  186. package/vendor/wasm3/m3_atomic.h +114 -0
  187. package/vendor/wasm3/m3_bind.c +174 -0
  188. package/vendor/wasm3/m3_bind.h +20 -0
  189. package/vendor/wasm3/m3_code.c +232 -0
  190. package/vendor/wasm3/m3_code.h +74 -0
  191. package/vendor/wasm3/m3_compile.c +6603 -0
  192. package/vendor/wasm3/m3_compile.h +354 -0
  193. package/vendor/wasm3/m3_config.h +448 -0
  194. package/vendor/wasm3/m3_config_platforms.h +345 -0
  195. package/vendor/wasm3/m3_core.c +1034 -0
  196. package/vendor/wasm3/m3_core.h +526 -0
  197. package/vendor/wasm3/m3_deterministic.c +65 -0
  198. package/vendor/wasm3/m3_deterministic.h +59 -0
  199. package/vendor/wasm3/m3_env.c +3146 -0
  200. package/vendor/wasm3/m3_env.h +748 -0
  201. package/vendor/wasm3/m3_exception.h +33 -0
  202. package/vendor/wasm3/m3_exec.c +8 -0
  203. package/vendor/wasm3/m3_exec.h +4110 -0
  204. package/vendor/wasm3/m3_exec_atomic.h +146 -0
  205. package/vendor/wasm3/m3_exec_defs.h +76 -0
  206. package/vendor/wasm3/m3_function.c +278 -0
  207. package/vendor/wasm3/m3_function.h +244 -0
  208. package/vendor/wasm3/m3_host.h +226 -0
  209. package/vendor/wasm3/m3_host_none.h +76 -0
  210. package/vendor/wasm3/m3_host_posix.h +543 -0
  211. package/vendor/wasm3/m3_host_win32.h +253 -0
  212. package/vendor/wasm3/m3_info.c +616 -0
  213. package/vendor/wasm3/m3_info.h +38 -0
  214. package/vendor/wasm3/m3_math_utils.h +560 -0
  215. package/vendor/wasm3/m3_module.c +431 -0
  216. package/vendor/wasm3/m3_parse.c +1549 -0
  217. package/vendor/wasm3/m3_snapshot.c +3825 -0
  218. package/vendor/wasm3/m3_validate.c +1770 -0
  219. package/vendor/wasm3/m3_validate.h +25 -0
  220. package/vendor/wasm3/m3_wasi.h +10 -0
  221. package/vendor/wasm3/m3_xxh64.c +211 -0
  222. package/vendor/wasm3/wasm3.h +684 -0
  223. package/vendor/wasm3/wasm3_defs.h +350 -0
  224. package/runtime/ext_node/worker_threads/worker_threads.c +0 -214
@@ -0,0 +1,1207 @@
1
+ //
2
+ // m3_api_wasi.c
3
+ //
4
+ // Created by Volodymyr Shymanskyy on 11/20/19.
5
+ // Copyright © 2019 Volodymyr Shymanskyy. All rights reserved.
6
+ //
7
+
8
+ #include "m3_api_wasi.h"
9
+
10
+ #include "m3_api_wasi_host.h"
11
+ #include "m3_env.h"
12
+ #include "m3_deterministic.h"
13
+ #include "m3_exception.h"
14
+
15
+ #if defined(d_m3HasWASI)
16
+
17
+ # include <string.h>
18
+
19
+ // The system underneath, of which exactly one is ever built. They are headers rather
20
+ // than translation units of their own so that neither has to be added to a source
21
+ // list - and most of those are build scripts outside this repository.
22
+ # if defined(_WIN32)
23
+ # include "m3_api_wasi_win32.h"
24
+ # else
25
+ # include "m3_api_wasi_posix.h"
26
+ # endif
27
+
28
+ // This file is the WASI interface and nothing below it: it decodes the guest's
29
+ // arguments, bounds-checks every pointer against guest memory, and writes results
30
+ // back in the layout the calling revision asks for. The system underneath is reached
31
+ // through m3_api_wasi_host.h, which m3_api_wasi_posix.h and m3_api_wasi_win32.h
32
+ // implement independently of each other.
33
+
34
+ static m3_wasi_context_t* wasi_context;
35
+
36
+ // A scatter/gather list as the guest lays one out
37
+ typedef struct wasi_iovec_t {
38
+ __wasi_size_t buf;
39
+ __wasi_size_t buf_len;
40
+ } wasi_iovec_t;
41
+
42
+ // The records the guest passes by address, sized as the ABI lays them out rather
43
+ // than as this host happens to - see write_filestat, read_subscriptions, write_event.
44
+ # define d_m3WasiFilestatSize 64
45
+ # define d_m3WasiSubscriptionSize 48
46
+ # define d_m3WasiEventSize 32
47
+
48
+ static const char* DEFAULT_ENVIRONMENT[] = {
49
+ d_m3WasiDefaultEnvironment,
50
+ NULL,
51
+ };
52
+
53
+ // Copies a path out of guest memory so it can be handed to the host NUL-terminated.
54
+ // The caller has already bounds-checked (i_path, i_pathLen) against guest memory.
55
+ static
56
+ int copy_path (char* o_path, const char* i_path, __wasi_size_t i_pathLen)
57
+ {
58
+ if (i_pathLen >= d_m3WasiMaxPath) {
59
+ return 0;
60
+ }
61
+ memcpy(o_path, i_path, i_pathLen);
62
+ o_path[i_pathLen] = '\0';
63
+ return 1;
64
+ }
65
+
66
+ // Writes a filestat out: dev 0, ino 8, filetype 16, nlink 24, size 32, atim 40,
67
+ // mtim 48, ctim 56. The caller has checked the buffer for d_m3WasiFilestatSize.
68
+ static
69
+ void write_filestat (uint8_t* o_buf, const m3_wasi_filestat_t* i_stat)
70
+ {
71
+ memset(o_buf, 0, d_m3WasiFilestatSize);
72
+
73
+ m3ApiWriteMem64(o_buf + 0, i_stat->dev);
74
+ m3ApiWriteMem64(o_buf + 8, i_stat->ino);
75
+ m3ApiWriteMem8(o_buf + 16, i_stat->filetype);
76
+ m3ApiWriteMem64(o_buf + 24, i_stat->nlink);
77
+ m3ApiWriteMem64(o_buf + 32, i_stat->size);
78
+ m3ApiWriteMem64(o_buf + 40, i_stat->atim);
79
+ m3ApiWriteMem64(o_buf + 48, i_stat->mtim);
80
+ m3ApiWriteMem64(o_buf + 56, i_stat->ctim);
81
+ }
82
+
83
+ // Resolves the guest's scatter/gather list to host addresses, checking each buffer
84
+ // against guest memory, and adds up how much they ask for. The host layer is handed
85
+ // the result and never sees a guest offset.
86
+ static
87
+ const void* resolve_iovs (IM3Runtime runtime, void* _mem, m3_wasi_iovec_t* o_iovs,
88
+ const wasi_iovec_t* i_wasiIovs, __wasi_size_t i_count,
89
+ __wasi_size_t* o_want)
90
+ {
91
+ *o_want = 0;
92
+
93
+ __wasi_size_t want = 0;
94
+
95
+ for (__wasi_size_t i = 0; i < i_count; i++) {
96
+ o_iovs[i].buf = m3ApiOffsetToPtr(m3ApiReadMem32(&i_wasiIovs[i].buf));
97
+ o_iovs[i].len = m3ApiReadMem32(&i_wasiIovs[i].buf_len);
98
+ m3ApiCheckMem(o_iovs[i].buf, o_iovs[i].len);
99
+ want += (__wasi_size_t)o_iovs[i].len;
100
+ }
101
+
102
+ *o_want = want;
103
+ m3ApiSuccess();
104
+ }
105
+
106
+ /*
107
+ * WASI API implementation
108
+ */
109
+
110
+ m3ApiRawFunction(m3_wasi_p1_args_get)
111
+ {
112
+ m3ApiReturnType(uint32_t)
113
+ m3ApiGetArgMem(uint32_t*, argv)
114
+ m3ApiGetArgMem(char*, argv_buf)
115
+
116
+ m3_wasi_context_t* context = (m3_wasi_context_t*)(_ctx->userdata);
117
+
118
+ if (context == NULL) {
119
+ m3ApiReturn(__WASI_ERRNO_INVAL);
120
+ }
121
+
122
+ m3ApiCheckMem(argv, context->argc * sizeof(uint32_t));
123
+
124
+ for (u32 i = 0; i < context->argc; ++i) {
125
+ m3ApiWriteMem32(&argv[i], m3ApiPtrToOffset(argv_buf));
126
+
127
+ size_t len = strlen(context->argv[i]);
128
+
129
+ m3ApiCheckMem(argv_buf, len + 1); // include the terminating `\0`
130
+ memcpy(argv_buf, context->argv[i], len);
131
+ argv_buf += len;
132
+ *argv_buf++ = 0;
133
+ }
134
+
135
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
136
+ }
137
+
138
+ m3ApiRawFunction(m3_wasi_p1_args_sizes_get)
139
+ {
140
+ m3ApiReturnType(uint32_t)
141
+ m3ApiGetArgMem(__wasi_size_t*, argc)
142
+ m3ApiGetArgMem(__wasi_size_t*, argv_buf_size)
143
+
144
+ m3ApiCheckMem(argc, sizeof(__wasi_size_t));
145
+ m3ApiCheckMem(argv_buf_size, sizeof(__wasi_size_t));
146
+
147
+ m3_wasi_context_t* context = (m3_wasi_context_t*)(_ctx->userdata);
148
+
149
+ if (context == NULL) {
150
+ m3ApiReturn(__WASI_ERRNO_INVAL);
151
+ }
152
+
153
+ __wasi_size_t buf_len = 0;
154
+ for (u32 i = 0; i < context->argc; ++i) {
155
+ buf_len += (__wasi_size_t)strlen(context->argv[i]) + 1;
156
+ }
157
+
158
+ m3ApiWriteMem32(argc, context->argc);
159
+ m3ApiWriteMem32(argv_buf_size, buf_len);
160
+
161
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
162
+ }
163
+
164
+ m3ApiRawFunction(m3_wasi_p1_environ_get)
165
+ {
166
+ m3ApiReturnType(uint32_t)
167
+ m3ApiGetArgMem(uint32_t*, env)
168
+ m3ApiGetArgMem(char*, env_buf)
169
+
170
+ __wasi_size_t count = 0;
171
+ for (const char** e = DEFAULT_ENVIRONMENT; *e; ++e) {
172
+ count++;
173
+ }
174
+
175
+ m3ApiCheckMem(env, count * sizeof(uint32_t));
176
+
177
+ for (u32 i = 0; i < count; ++i) {
178
+ m3ApiWriteMem32(&env[i], m3ApiPtrToOffset(env_buf));
179
+
180
+ size_t len = strlen(DEFAULT_ENVIRONMENT[i]);
181
+
182
+ m3ApiCheckMem(env_buf, len + 1); // include the terminating `\0`
183
+ memcpy(env_buf, DEFAULT_ENVIRONMENT[i], len);
184
+ env_buf += len;
185
+ *env_buf++ = 0;
186
+ }
187
+
188
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
189
+ }
190
+
191
+ m3ApiRawFunction(m3_wasi_p1_environ_sizes_get)
192
+ {
193
+ m3ApiReturnType(uint32_t)
194
+ m3ApiGetArgMem(__wasi_size_t*, env_count)
195
+ m3ApiGetArgMem(__wasi_size_t*, env_buf_size)
196
+
197
+ m3ApiCheckMem(env_count, sizeof(__wasi_size_t));
198
+ m3ApiCheckMem(env_buf_size, sizeof(__wasi_size_t));
199
+
200
+ __wasi_size_t count = 0, buf_len = 0;
201
+ for (const char** e = DEFAULT_ENVIRONMENT; *e; ++e) {
202
+ count++;
203
+ buf_len += (__wasi_size_t)strlen(*e) + 1;
204
+ }
205
+
206
+ m3ApiWriteMem32(env_count, count);
207
+ m3ApiWriteMem32(env_buf_size, buf_len);
208
+
209
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
210
+ }
211
+
212
+ m3ApiRawFunction(m3_wasi_p1_fd_prestat_dir_name)
213
+ {
214
+ m3ApiReturnType(uint32_t)
215
+ m3ApiGetArg(__wasi_fd_t, fd)
216
+ m3ApiGetArgMem(char*, path)
217
+ m3ApiGetArg(__wasi_size_t, path_len)
218
+
219
+ m3ApiCheckMem(path, path_len);
220
+
221
+ if (fd < 3 || fd >= d_m3WasiPreopenCount) {
222
+ m3ApiReturn(__WASI_ERRNO_BADF);
223
+ }
224
+ size_t slen = strlen(m3_wasi_preopen[fd].path) + 1;
225
+ memcpy(path, m3_wasi_preopen[fd].path, M3_MIN(slen, path_len));
226
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
227
+ }
228
+
229
+ m3ApiRawFunction(m3_wasi_p1_fd_prestat_get)
230
+ {
231
+ m3ApiReturnType(uint32_t)
232
+ m3ApiGetArg(__wasi_fd_t, fd)
233
+ m3ApiGetArgMem(uint8_t*, buf)
234
+
235
+ m3ApiCheckMem(buf, 8);
236
+
237
+ if (fd < 3 || fd >= d_m3WasiPreopenCount) {
238
+ m3ApiReturn(__WASI_ERRNO_BADF);
239
+ }
240
+
241
+ m3ApiWriteMem32(buf + 0, __WASI_PREOPENTYPE_DIR);
242
+ m3ApiWriteMem32(buf + 4, (u32)strlen(m3_wasi_preopen[fd].path) + 1);
243
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
244
+ }
245
+
246
+ m3ApiRawFunction(m3_wasi_p1_fd_fdstat_get)
247
+ {
248
+ m3ApiReturnType(uint32_t)
249
+ m3ApiGetArg(__wasi_fd_t, fd)
250
+ m3ApiGetArgMem(__wasi_fdstat_t*, fdstat)
251
+
252
+ m3ApiCheckMem(fdstat, sizeof(__wasi_fdstat_t));
253
+
254
+ __wasi_filetype_t filetype;
255
+ __wasi_fdflags_t flags;
256
+
257
+ __wasi_errno_t err = m3_wasi_host_fd_fdstat(fd, &filetype, &flags);
258
+ if (err != __WASI_ERRNO_SUCCESS) {
259
+ m3ApiReturn(err);
260
+ }
261
+
262
+ fdstat->fs_filetype = filetype;
263
+ m3ApiWriteMem16(&fdstat->fs_flags, flags);
264
+
265
+ fdstat->fs_rights_base = (uint64_t)-1; // all rights
266
+
267
+ // Make descriptors 0,1,2 look like a TTY
268
+ if (fd <= 2) {
269
+ fdstat->fs_rights_base &= ~(__WASI_RIGHTS_FD_SEEK | __WASI_RIGHTS_FD_TELL);
270
+ }
271
+
272
+ fdstat->fs_rights_inheriting = (uint64_t)-1; // all rights
273
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
274
+ }
275
+
276
+ m3ApiRawFunction(m3_wasi_p1_fd_fdstat_set_flags)
277
+ {
278
+ m3ApiReturnType(uint32_t)
279
+ m3ApiGetArg(__wasi_fd_t, fd)
280
+ m3ApiGetArg(__wasi_fdflags_t, flags)
281
+
282
+ m3ApiReturn(m3_wasi_host_fd_fdstat_set_flags(fd, flags));
283
+ }
284
+
285
+ m3ApiRawFunction(m3_wasi_p1_fd_advise)
286
+ {
287
+ m3ApiReturnType(uint32_t)
288
+ m3ApiGetArg(__wasi_fd_t, fd)
289
+ m3ApiGetArg(__wasi_filesize_t, offset)
290
+ m3ApiGetArg(__wasi_filesize_t, length)
291
+ m3ApiGetArg(__wasi_advice_t, advice)
292
+
293
+ m3ApiReturn(m3_wasi_host_fd_advise(fd, offset, length, advice));
294
+ }
295
+
296
+ m3ApiRawFunction(m3_wasi_p1_fd_filestat_get)
297
+ {
298
+ m3ApiReturnType(uint32_t)
299
+ m3ApiGetArg(__wasi_fd_t, fd)
300
+ m3ApiGetArgMem(uint8_t*, buf)
301
+
302
+ m3ApiCheckMem(buf, d_m3WasiFilestatSize);
303
+
304
+ m3_wasi_filestat_t fd_stat;
305
+
306
+ __wasi_errno_t err = m3_wasi_host_fd_filestat(fd, &fd_stat);
307
+ if (err != __WASI_ERRNO_SUCCESS) {
308
+ m3ApiReturn(err);
309
+ }
310
+
311
+ write_filestat(buf, &fd_stat);
312
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
313
+ }
314
+
315
+ m3ApiRawFunction(m3_wasi_p1_path_filestat_get)
316
+ {
317
+ m3ApiReturnType(uint32_t)
318
+ m3ApiGetArg(__wasi_fd_t, dirfd)
319
+ m3ApiGetArg(__wasi_lookupflags_t, dirflags)
320
+ m3ApiGetArgMem(const char*, path)
321
+ m3ApiGetArg(__wasi_size_t, path_len)
322
+ m3ApiGetArgMem(uint8_t*, buf)
323
+
324
+ m3ApiCheckMem(path, path_len);
325
+ m3ApiCheckMem(buf, d_m3WasiFilestatSize);
326
+
327
+ char host_path[d_m3WasiMaxPath];
328
+ if (!copy_path(host_path, path, path_len)) {
329
+ m3ApiReturn(__WASI_ERRNO_NAMETOOLONG);
330
+ }
331
+
332
+ m3_wasi_filestat_t path_stat;
333
+ bool follow = (dirflags & __WASI_LOOKUPFLAGS_SYMLINK_FOLLOW) != 0;
334
+
335
+ __wasi_errno_t err = m3_wasi_host_path_filestat(dirfd, host_path, follow, &path_stat);
336
+ if (err != __WASI_ERRNO_SUCCESS) {
337
+ m3ApiReturn(err);
338
+ }
339
+
340
+ write_filestat(buf, &path_stat);
341
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
342
+ }
343
+
344
+ m3ApiRawFunction(m3_wasi_p1_fd_filestat_set_size)
345
+ {
346
+ m3ApiReturnType(uint32_t)
347
+ m3ApiGetArg(__wasi_fd_t, fd)
348
+ m3ApiGetArg(__wasi_filesize_t, size)
349
+
350
+ m3ApiReturn(m3_wasi_host_fd_set_size(fd, size));
351
+ }
352
+
353
+ m3ApiRawFunction(m3_wasi_p1_fd_filestat_set_times)
354
+ {
355
+ m3ApiReturnType(uint32_t)
356
+ m3ApiGetArg(__wasi_fd_t, fd)
357
+ m3ApiGetArg(__wasi_timestamp_t, atim)
358
+ m3ApiGetArg(__wasi_timestamp_t, mtim)
359
+ m3ApiGetArg(__wasi_fstflags_t, flags)
360
+
361
+ m3ApiReturn(m3_wasi_host_fd_set_times(fd, atim, mtim, flags));
362
+ }
363
+
364
+ m3ApiRawFunction(m3_wasi_p1_path_link)
365
+ {
366
+ m3ApiReturnType(uint32_t)
367
+ m3ApiGetArg(__wasi_fd_t, old_dirfd)
368
+ m3ApiGetArg(__wasi_lookupflags_t, old_flags)
369
+ m3ApiGetArgMem(const char*, old_path)
370
+ m3ApiGetArg(__wasi_size_t, old_path_len)
371
+ m3ApiGetArg(__wasi_fd_t, new_dirfd)
372
+ m3ApiGetArgMem(const char*, new_path)
373
+ m3ApiGetArg(__wasi_size_t, new_path_len)
374
+
375
+ m3ApiCheckMem(old_path, old_path_len);
376
+ m3ApiCheckMem(new_path, new_path_len);
377
+
378
+ char host_old[d_m3WasiMaxPath];
379
+ char host_new[d_m3WasiMaxPath];
380
+ if (!copy_path(host_old, old_path, old_path_len) ||
381
+ !copy_path(host_new, new_path, new_path_len)) {
382
+ m3ApiReturn(__WASI_ERRNO_NAMETOOLONG);
383
+ }
384
+
385
+ bool follow = (old_flags & __WASI_LOOKUPFLAGS_SYMLINK_FOLLOW) != 0;
386
+
387
+ m3ApiReturn(m3_wasi_host_path_link(old_dirfd, host_old, follow, new_dirfd, host_new));
388
+ }
389
+
390
+ m3ApiRawFunction(m3_wasi_p1_path_create_directory)
391
+ {
392
+ m3ApiReturnType(uint32_t)
393
+ m3ApiGetArg(__wasi_fd_t, dirfd)
394
+ m3ApiGetArgMem(const char*, path)
395
+ m3ApiGetArg(__wasi_size_t, path_len)
396
+
397
+ m3ApiCheckMem(path, path_len);
398
+
399
+ char host_path[d_m3WasiMaxPath];
400
+ if (!copy_path(host_path, path, path_len)) {
401
+ m3ApiReturn(__WASI_ERRNO_NAMETOOLONG);
402
+ }
403
+
404
+ m3ApiReturn(m3_wasi_host_path_create_directory(dirfd, host_path));
405
+ }
406
+
407
+ m3ApiRawFunction(m3_wasi_p1_path_remove_directory)
408
+ {
409
+ m3ApiReturnType(uint32_t)
410
+ m3ApiGetArg(__wasi_fd_t, dirfd)
411
+ m3ApiGetArgMem(const char*, path)
412
+ m3ApiGetArg(__wasi_size_t, path_len)
413
+
414
+ m3ApiCheckMem(path, path_len);
415
+
416
+ char host_path[d_m3WasiMaxPath];
417
+ if (!copy_path(host_path, path, path_len)) {
418
+ m3ApiReturn(__WASI_ERRNO_NAMETOOLONG);
419
+ }
420
+
421
+ m3ApiReturn(m3_wasi_host_path_remove_directory(dirfd, host_path));
422
+ }
423
+
424
+ m3ApiRawFunction(m3_wasi_p1_path_unlink_file)
425
+ {
426
+ m3ApiReturnType(uint32_t)
427
+ m3ApiGetArg(__wasi_fd_t, dirfd)
428
+ m3ApiGetArgMem(const char*, path)
429
+ m3ApiGetArg(__wasi_size_t, path_len)
430
+
431
+ m3ApiCheckMem(path, path_len);
432
+
433
+ char host_path[d_m3WasiMaxPath];
434
+ if (!copy_path(host_path, path, path_len)) {
435
+ m3ApiReturn(__WASI_ERRNO_NAMETOOLONG);
436
+ }
437
+
438
+ m3ApiReturn(m3_wasi_host_path_unlink_file(dirfd, host_path));
439
+ }
440
+
441
+ m3ApiRawFunction(m3_wasi_p1_path_rename)
442
+ {
443
+ m3ApiReturnType(uint32_t)
444
+ m3ApiGetArg(__wasi_fd_t, old_dirfd)
445
+ m3ApiGetArgMem(const char*, old_path)
446
+ m3ApiGetArg(__wasi_size_t, old_path_len)
447
+ m3ApiGetArg(__wasi_fd_t, new_dirfd)
448
+ m3ApiGetArgMem(const char*, new_path)
449
+ m3ApiGetArg(__wasi_size_t, new_path_len)
450
+
451
+ m3ApiCheckMem(old_path, old_path_len);
452
+ m3ApiCheckMem(new_path, new_path_len);
453
+
454
+ char host_old[d_m3WasiMaxPath];
455
+ char host_new[d_m3WasiMaxPath];
456
+ if (!copy_path(host_old, old_path, old_path_len) ||
457
+ !copy_path(host_new, new_path, new_path_len)) {
458
+ m3ApiReturn(__WASI_ERRNO_NAMETOOLONG);
459
+ }
460
+
461
+ m3ApiReturn(m3_wasi_host_path_rename(old_dirfd, host_old, new_dirfd, host_new));
462
+ }
463
+
464
+ m3ApiRawFunction(m3_wasi_p1_path_symlink)
465
+ {
466
+ m3ApiReturnType(uint32_t)
467
+ m3ApiGetArgMem(const char*, old_path)
468
+ m3ApiGetArg(__wasi_size_t, old_path_len)
469
+ m3ApiGetArg(__wasi_fd_t, dirfd)
470
+ m3ApiGetArgMem(const char*, new_path)
471
+ m3ApiGetArg(__wasi_size_t, new_path_len)
472
+
473
+ m3ApiCheckMem(old_path, old_path_len);
474
+ m3ApiCheckMem(new_path, new_path_len);
475
+
476
+ // the target is stored as given, so it is a string rather than a path to resolve
477
+ char host_old[d_m3WasiMaxPath];
478
+ char host_new[d_m3WasiMaxPath];
479
+ if (!copy_path(host_old, old_path, old_path_len) ||
480
+ !copy_path(host_new, new_path, new_path_len)) {
481
+ m3ApiReturn(__WASI_ERRNO_NAMETOOLONG);
482
+ }
483
+
484
+ m3ApiReturn(m3_wasi_host_path_symlink(host_old, dirfd, host_new));
485
+ }
486
+
487
+ m3ApiRawFunction(m3_wasi_p1_path_readlink)
488
+ {
489
+ m3ApiReturnType(uint32_t)
490
+ m3ApiGetArg(__wasi_fd_t, dirfd)
491
+ m3ApiGetArgMem(const char*, path)
492
+ m3ApiGetArg(__wasi_size_t, path_len)
493
+ m3ApiGetArgMem(char*, buf)
494
+ m3ApiGetArg(__wasi_size_t, buf_len)
495
+ m3ApiGetArgMem(__wasi_size_t*, bufused)
496
+
497
+ m3ApiCheckMem(path, path_len);
498
+ m3ApiCheckMem(buf, buf_len);
499
+ m3ApiCheckMem(bufused, sizeof(__wasi_size_t));
500
+
501
+ char host_path[d_m3WasiMaxPath];
502
+ if (!copy_path(host_path, path, path_len)) {
503
+ m3ApiReturn(__WASI_ERRNO_NAMETOOLONG);
504
+ }
505
+
506
+ __wasi_size_t used;
507
+
508
+ __wasi_errno_t err = m3_wasi_host_path_readlink(dirfd, host_path, buf, buf_len, &used);
509
+ if (err != __WASI_ERRNO_SUCCESS) {
510
+ m3ApiReturn(err);
511
+ }
512
+
513
+ m3ApiWriteMem32(bufused, used);
514
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
515
+ }
516
+
517
+ m3ApiRawFunction(m3_wasi_p1_fd_tell)
518
+ {
519
+ m3ApiReturnType(uint32_t)
520
+ m3ApiGetArg(__wasi_fd_t, fd)
521
+ m3ApiGetArgMem(__wasi_filesize_t*, result)
522
+
523
+ m3ApiCheckMem(result, sizeof(__wasi_filesize_t));
524
+
525
+ __wasi_filesize_t pos;
526
+
527
+ __wasi_errno_t err = m3_wasi_host_fd_seek(fd, 0, SEEK_CUR, &pos);
528
+ if (err != __WASI_ERRNO_SUCCESS) {
529
+ m3ApiReturn(err);
530
+ }
531
+
532
+ m3ApiWriteMem64(result, pos);
533
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
534
+ }
535
+
536
+ m3ApiRawFunction(m3_wasi_p1_fd_seek)
537
+ {
538
+ m3ApiReturnType(uint32_t)
539
+ m3ApiGetArg(__wasi_fd_t, fd)
540
+ m3ApiGetArg(__wasi_filedelta_t, offset)
541
+ m3ApiGetArg(uint32_t, wasi_whence)
542
+ m3ApiGetArgMem(__wasi_filesize_t*, result)
543
+
544
+ m3ApiCheckMem(result, sizeof(__wasi_filesize_t));
545
+
546
+ int whence;
547
+
548
+ switch (wasi_whence) {
549
+ case 0: whence = SEEK_SET; break;
550
+ case 1: whence = SEEK_CUR; break;
551
+ case 2: whence = SEEK_END; break;
552
+ default: m3ApiReturn(__WASI_ERRNO_INVAL);
553
+ }
554
+
555
+ __wasi_filesize_t pos;
556
+
557
+ __wasi_errno_t err = m3_wasi_host_fd_seek(fd, offset, whence, &pos);
558
+ if (err != __WASI_ERRNO_SUCCESS) {
559
+ m3ApiReturn(err);
560
+ }
561
+
562
+ m3ApiWriteMem64(result, pos);
563
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
564
+ }
565
+
566
+
567
+ m3ApiRawFunction(m3_wasi_p1_path_open)
568
+ {
569
+ m3ApiReturnType(uint32_t)
570
+ m3ApiGetArg(__wasi_fd_t, dirfd)
571
+ m3ApiGetArg(__wasi_lookupflags_t, dirflags)
572
+ m3ApiGetArgMem(const char*, path)
573
+ m3ApiGetArg(__wasi_size_t, path_len)
574
+ m3ApiGetArg(__wasi_oflags_t, oflags)
575
+ m3ApiGetArg(__wasi_rights_t, fs_rights_base)
576
+ m3ApiGetArg(__wasi_rights_t, fs_rights_inheriting)
577
+ m3ApiGetArg(__wasi_fdflags_t, fs_flags)
578
+ m3ApiGetArgMem(__wasi_fd_t*, fd)
579
+
580
+ m3ApiCheckMem(path, path_len);
581
+ m3ApiCheckMem(fd, sizeof(__wasi_fd_t));
582
+
583
+ char host_path[d_m3WasiMaxPath];
584
+ if (!copy_path(host_path, path, path_len)) {
585
+ m3ApiReturn(__WASI_ERRNO_NAMETOOLONG);
586
+ }
587
+
588
+ __wasi_fd_t host_fd;
589
+
590
+ __wasi_errno_t err = m3_wasi_host_path_open(dirfd, host_path, oflags, fs_rights_base,
591
+ fs_flags, &host_fd);
592
+ if (err != __WASI_ERRNO_SUCCESS) {
593
+ m3ApiReturn(err);
594
+ }
595
+
596
+ m3ApiWriteMem32(fd, host_fd);
597
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
598
+ }
599
+
600
+ // The four transfer calls all walk the guest's list a batch at a time - see
601
+ // d_m3WasiMaxIovs - and stop where a batch comes back short, as a short readv would
602
+ // have stopped the whole call.
603
+
604
+ m3ApiRawFunction(m3_wasi_p1_fd_read)
605
+ {
606
+ m3ApiReturnType(uint32_t)
607
+ m3ApiGetArg(__wasi_fd_t, fd)
608
+ m3ApiGetArgMem(wasi_iovec_t*, wasi_iovs)
609
+ m3ApiGetArg(__wasi_size_t, iovs_len)
610
+ m3ApiGetArgMem(__wasi_size_t*, nread)
611
+
612
+ m3ApiCheckMem(wasi_iovs, iovs_len * sizeof(wasi_iovec_t));
613
+ m3ApiCheckMem(nread, sizeof(__wasi_size_t));
614
+
615
+ m3_wasi_iovec_t iovs[d_m3WasiMaxIovs];
616
+ __wasi_size_t total = 0;
617
+
618
+ for (__wasi_size_t i = 0; i < iovs_len; i += d_m3WasiMaxIovs) {
619
+ __wasi_size_t count = M3_MIN(iovs_len - i, (__wasi_size_t)d_m3WasiMaxIovs);
620
+ __wasi_size_t want, done;
621
+
622
+ const void* mem_check = resolve_iovs(runtime, _mem, iovs, wasi_iovs + i, count, &want);
623
+ if (mem_check != m3Err_none) {
624
+ return mem_check;
625
+ }
626
+
627
+ __wasi_errno_t err = m3_wasi_host_fd_read(fd, iovs, count, &done);
628
+ if (err != __WASI_ERRNO_SUCCESS) {
629
+ m3ApiReturn(err);
630
+ }
631
+
632
+ total += done;
633
+ if (done < want) {
634
+ break;
635
+ }
636
+ }
637
+
638
+ m3ApiWriteMem32(nread, total);
639
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
640
+ }
641
+
642
+ m3ApiRawFunction(m3_wasi_p1_fd_write)
643
+ {
644
+ m3ApiReturnType(uint32_t)
645
+ m3ApiGetArg(__wasi_fd_t, fd)
646
+ m3ApiGetArgMem(wasi_iovec_t*, wasi_iovs)
647
+ m3ApiGetArg(__wasi_size_t, iovs_len)
648
+ m3ApiGetArgMem(__wasi_size_t*, nwritten)
649
+
650
+ m3ApiCheckMem(wasi_iovs, iovs_len * sizeof(wasi_iovec_t));
651
+ m3ApiCheckMem(nwritten, sizeof(__wasi_size_t));
652
+
653
+ m3_wasi_iovec_t iovs[d_m3WasiMaxIovs];
654
+ __wasi_size_t total = 0;
655
+
656
+ for (__wasi_size_t i = 0; i < iovs_len; i += d_m3WasiMaxIovs) {
657
+ __wasi_size_t count = M3_MIN(iovs_len - i, (__wasi_size_t)d_m3WasiMaxIovs);
658
+ __wasi_size_t want, done;
659
+
660
+ const void* mem_check = resolve_iovs(runtime, _mem, iovs, wasi_iovs + i, count, &want);
661
+ if (mem_check != m3Err_none) {
662
+ return mem_check;
663
+ }
664
+
665
+ __wasi_errno_t err = m3_wasi_host_fd_write(fd, iovs, count, &done);
666
+ if (err != __WASI_ERRNO_SUCCESS) {
667
+ m3ApiReturn(err);
668
+ }
669
+
670
+ total += done;
671
+ if (done < want) {
672
+ break;
673
+ }
674
+ }
675
+
676
+ m3ApiWriteMem32(nwritten, total);
677
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
678
+ }
679
+
680
+ m3ApiRawFunction(m3_wasi_p1_fd_pread)
681
+ {
682
+ m3ApiReturnType(uint32_t)
683
+ m3ApiGetArg(__wasi_fd_t, fd)
684
+ m3ApiGetArgMem(wasi_iovec_t*, wasi_iovs)
685
+ m3ApiGetArg(__wasi_size_t, iovs_len)
686
+ m3ApiGetArg(__wasi_filesize_t, offset)
687
+ m3ApiGetArgMem(__wasi_size_t*, nread)
688
+
689
+ m3ApiCheckMem(wasi_iovs, iovs_len * sizeof(wasi_iovec_t));
690
+ m3ApiCheckMem(nread, sizeof(__wasi_size_t));
691
+
692
+ m3_wasi_iovec_t iovs[d_m3WasiMaxIovs];
693
+ __wasi_size_t total = 0;
694
+
695
+ for (__wasi_size_t i = 0; i < iovs_len; i += d_m3WasiMaxIovs) {
696
+ __wasi_size_t count = M3_MIN(iovs_len - i, (__wasi_size_t)d_m3WasiMaxIovs);
697
+ __wasi_size_t want, done;
698
+
699
+ const void* mem_check = resolve_iovs(runtime, _mem, iovs, wasi_iovs + i, count, &want);
700
+ if (mem_check != m3Err_none) {
701
+ return mem_check;
702
+ }
703
+
704
+ // the offset walks with the data, so a short batch leaves no hole behind it
705
+ __wasi_errno_t err = m3_wasi_host_fd_pread(fd, iovs, count, offset + total, &done);
706
+ if (err != __WASI_ERRNO_SUCCESS) {
707
+ m3ApiReturn(err);
708
+ }
709
+
710
+ total += done;
711
+ if (done < want) {
712
+ break;
713
+ }
714
+ }
715
+
716
+ m3ApiWriteMem32(nread, total);
717
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
718
+ }
719
+
720
+ m3ApiRawFunction(m3_wasi_p1_fd_pwrite)
721
+ {
722
+ m3ApiReturnType(uint32_t)
723
+ m3ApiGetArg(__wasi_fd_t, fd)
724
+ m3ApiGetArgMem(wasi_iovec_t*, wasi_iovs)
725
+ m3ApiGetArg(__wasi_size_t, iovs_len)
726
+ m3ApiGetArg(__wasi_filesize_t, offset)
727
+ m3ApiGetArgMem(__wasi_size_t*, nwritten)
728
+
729
+ m3ApiCheckMem(wasi_iovs, iovs_len * sizeof(wasi_iovec_t));
730
+ m3ApiCheckMem(nwritten, sizeof(__wasi_size_t));
731
+
732
+ m3_wasi_iovec_t iovs[d_m3WasiMaxIovs];
733
+ __wasi_size_t total = 0;
734
+
735
+ for (__wasi_size_t i = 0; i < iovs_len; i += d_m3WasiMaxIovs) {
736
+ __wasi_size_t count = M3_MIN(iovs_len - i, (__wasi_size_t)d_m3WasiMaxIovs);
737
+ __wasi_size_t want, done;
738
+
739
+ const void* mem_check = resolve_iovs(runtime, _mem, iovs, wasi_iovs + i, count, &want);
740
+ if (mem_check != m3Err_none) {
741
+ return mem_check;
742
+ }
743
+
744
+ __wasi_errno_t err = m3_wasi_host_fd_pwrite(fd, iovs, count, offset + total, &done);
745
+ if (err != __WASI_ERRNO_SUCCESS) {
746
+ m3ApiReturn(err);
747
+ }
748
+
749
+ total += done;
750
+ if (done < want) {
751
+ break;
752
+ }
753
+ }
754
+
755
+ m3ApiWriteMem32(nwritten, total);
756
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
757
+ }
758
+
759
+ m3ApiRawFunction(m3_wasi_p1_fd_close)
760
+ {
761
+ m3ApiReturnType(uint32_t)
762
+ m3ApiGetArg(__wasi_fd_t, fd)
763
+
764
+ m3ApiReturn(m3_wasi_host_fd_close(fd));
765
+ }
766
+
767
+ m3ApiRawFunction(m3_wasi_p1_fd_datasync)
768
+ {
769
+ m3ApiReturnType(uint32_t)
770
+ m3ApiGetArg(__wasi_fd_t, fd)
771
+
772
+ m3ApiReturn(m3_wasi_host_fd_datasync(fd));
773
+ }
774
+
775
+ m3ApiRawFunction(m3_wasi_p1_random_get)
776
+ {
777
+ m3ApiReturnType(uint32_t)
778
+ m3ApiGetArgMem(uint8_t*, buf)
779
+ m3ApiGetArg(__wasi_size_t, buf_len)
780
+
781
+ m3ApiCheckMem(buf, buf_len);
782
+
783
+ # if d_m3DeterministicProfile
784
+ Deterministic_Random(runtime, buf, buf_len);
785
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
786
+ # else
787
+ m3ApiReturn(m3_wasi_host_random_get(buf, buf_len));
788
+ # endif
789
+ }
790
+
791
+ m3ApiRawFunction(m3_wasi_p1_clock_res_get)
792
+ {
793
+ m3ApiReturnType(uint32_t)
794
+ m3ApiGetArg(__wasi_clockid_t, wasi_clk_id)
795
+ m3ApiGetArgMem(__wasi_timestamp_t*, resolution)
796
+
797
+ m3ApiCheckMem(resolution, sizeof(__wasi_timestamp_t));
798
+
799
+ # if d_m3DeterministicProfile
800
+ // the deterministic clock's resolution is the step it moves in
801
+ m3ApiWriteMem64(resolution, d_m3DeterministicTickNs);
802
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
803
+ # else
804
+ __wasi_timestamp_t res;
805
+
806
+ __wasi_errno_t err = m3_wasi_host_clock_res_get(wasi_clk_id, &res);
807
+ if (err != __WASI_ERRNO_SUCCESS) {
808
+ m3ApiReturn(err);
809
+ }
810
+
811
+ m3ApiWriteMem64(resolution, res);
812
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
813
+ # endif
814
+ }
815
+
816
+ m3ApiRawFunction(m3_wasi_p1_clock_time_get)
817
+ {
818
+ m3ApiReturnType(uint32_t)
819
+ m3ApiGetArg(__wasi_clockid_t, wasi_clk_id)
820
+ m3ApiGetArg(__wasi_timestamp_t, precision)
821
+ m3ApiGetArgMem(__wasi_timestamp_t*, time)
822
+
823
+ m3ApiCheckMem(time, sizeof(__wasi_timestamp_t));
824
+
825
+ # if d_m3DeterministicProfile
826
+ // Every clock the guest can name is the same underlying count - a runtime that is
827
+ // not measuring the machine has nothing to tell process time and thread time apart
828
+ // with - and only where it is counted from differs
829
+ __wasi_timestamp_t now = Deterministic_Time(runtime);
830
+
831
+ if (wasi_clk_id == __WASI_CLOCKID_REALTIME) {
832
+ now += d_m3WasiDeterministicEpochNs;
833
+ }
834
+
835
+ m3ApiWriteMem64(time, now);
836
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
837
+ # else
838
+ __wasi_timestamp_t now;
839
+
840
+ __wasi_errno_t err = m3_wasi_host_clock_time_get(wasi_clk_id, &now);
841
+ if (err != __WASI_ERRNO_SUCCESS) {
842
+ m3ApiReturn(err);
843
+ }
844
+
845
+ m3ApiWriteMem64(time, now);
846
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
847
+ # endif
848
+ }
849
+
850
+ m3ApiRawFunction(m3_wasi_p1_proc_exit)
851
+ {
852
+ m3ApiGetArg(uint32_t, code)
853
+
854
+ m3_wasi_context_t* context = (m3_wasi_context_t*)(_ctx->userdata);
855
+
856
+ if (context) {
857
+ context->exit_code = code;
858
+ }
859
+
860
+ m3ApiTrap(m3Err_trapWasiExit);
861
+ }
862
+
863
+ m3ApiRawFunction(m3_wasi_p1_sched_yield)
864
+ {
865
+ m3ApiReturnType(uint32_t)
866
+
867
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
868
+ }
869
+
870
+ // Reads the guest's subscription list, allocating the host form of it. NULL if there
871
+ // is no memory for that; the caller frees what comes back.
872
+ //
873
+ // 48 bytes a record: userdata 0, type 8, fd 16, and a clock's fields from 16.
874
+ // $precision at 32 says how much longer the host may wait to coalesce this with
875
+ // another event, and waiting exactly as long as asked is always a valid answer to
876
+ // that, so it is not read.
877
+ static
878
+ m3_wasi_pollsub_t* read_subscriptions (const uint8_t* i_subs, __wasi_size_t i_count)
879
+ {
880
+ m3_wasi_pollsub_t* subs =
881
+ (m3_wasi_pollsub_t*)m3_Malloc("WASI subscriptions", i_count * sizeof(m3_wasi_pollsub_t));
882
+
883
+ if (subs == NULL) {
884
+ return NULL;
885
+ }
886
+
887
+ for (__wasi_size_t i = 0; i < i_count; i++) {
888
+ const uint8_t* s = i_subs + (size_t)i * d_m3WasiSubscriptionSize;
889
+
890
+ memset(&subs[i], 0, sizeof(subs[i]));
891
+
892
+ subs[i].userdata = m3ApiReadMem64(s + 0);
893
+ subs[i].type = m3ApiReadMem8(s + 8);
894
+
895
+ if (subs[i].type == __WASI_EVENTTYPE_CLOCK) {
896
+ subs[i].clockId = m3ApiReadMem32(s + 16);
897
+ subs[i].timeout = m3ApiReadMem64(s + 24);
898
+ subs[i].isAbsolute =
899
+ (m3ApiReadMem16(s + 40) & __WASI_SUBCLOCKFLAGS_SUBSCRIPTION_CLOCK_ABSTIME) != 0;
900
+ } else {
901
+ subs[i].fd = m3ApiReadMem32(s + 16);
902
+ }
903
+ }
904
+
905
+ return subs;
906
+ }
907
+
908
+ // Writes one event back. This record is the same in both revisions: userdata 0,
909
+ // error 8, type 10, nbytes 16, flags 24. What the union does not cover is zeroed
910
+ // rather than left as whatever the guest had there - an event for a clock says
911
+ // nothing about nbytes, and the guest should not read its own stale bytes back as
912
+ // one.
913
+ static
914
+ void write_event (uint8_t* o_rec, const m3_wasi_pollevent_t* i_event)
915
+ {
916
+ memset(o_rec, 0, d_m3WasiEventSize);
917
+
918
+ m3ApiWriteMem64(o_rec + 0, i_event->userdata);
919
+ m3ApiWriteMem16(o_rec + 8, i_event->error);
920
+ m3ApiWriteMem8(o_rec + 10, i_event->type);
921
+ m3ApiWriteMem64(o_rec + 16, i_event->nbytes);
922
+ m3ApiWriteMem16(o_rec + 24, i_event->flags);
923
+ }
924
+
925
+ # if d_m3DeterministicProfile
926
+
927
+ // poll_oneoff under the deterministic profile, which cannot wait on anything real. A
928
+ // descriptor is reported ready at once - which is what the spec says a regular file
929
+ // always is, and the only answer here that does not depend on how fast something
930
+ // else is going - and the clock is moved forward to the soonest deadline, so that a
931
+ // guest sleeping still sees the time it slept for.
932
+ static
933
+ void poll_deterministically (IM3Runtime io_runtime, const m3_wasi_pollsub_t* i_subs,
934
+ __wasi_size_t i_count, m3_wasi_pollevent_t* o_events,
935
+ __wasi_size_t* o_numEvents)
936
+ {
937
+ __wasi_size_t out = 0;
938
+ uint64_t soonest = UINT64_MAX;
939
+ uint64_t now = io_runtime->virtualTimeNs;
940
+
941
+ for (__wasi_size_t i = 0; i < i_count; i++) {
942
+ const m3_wasi_pollsub_t* sub = &i_subs[i];
943
+
944
+ if (sub->type != __WASI_EVENTTYPE_CLOCK) {
945
+ o_events[out].userdata = sub->userdata;
946
+ o_events[out].error = __WASI_ERRNO_SUCCESS;
947
+ o_events[out].type = sub->type;
948
+ o_events[out].nbytes = 1;
949
+ o_events[out].flags = 0;
950
+ out++;
951
+ continue;
952
+ }
953
+
954
+ // an absolute deadline is against the same virtual clock the guest reads
955
+ uint64_t deadline = sub->isAbsolute ? sub->timeout : now + sub->timeout;
956
+ soonest = M3_MIN(soonest, deadline);
957
+ }
958
+
959
+ // Something is ready already, so no time passes - which is what poll() would
960
+ // have done too
961
+ if (out) {
962
+ *o_numEvents = out;
963
+ return;
964
+ }
965
+
966
+ if (soonest > now) {
967
+ Deterministic_Advance(io_runtime, soonest - now);
968
+ }
969
+
970
+ now = io_runtime->virtualTimeNs;
971
+
972
+ for (__wasi_size_t i = 0; i < i_count; i++) {
973
+ const m3_wasi_pollsub_t* sub = &i_subs[i];
974
+
975
+ if (sub->type != __WASI_EVENTTYPE_CLOCK) {
976
+ continue;
977
+ }
978
+
979
+ uint64_t deadline = sub->isAbsolute ? sub->timeout : soonest;
980
+
981
+ if (deadline <= now) {
982
+ o_events[out].userdata = sub->userdata;
983
+ o_events[out].error = __WASI_ERRNO_SUCCESS;
984
+ o_events[out].type = __WASI_EVENTTYPE_CLOCK;
985
+ o_events[out].nbytes = 0;
986
+ o_events[out].flags = 0;
987
+ out++;
988
+ }
989
+ }
990
+
991
+ *o_numEvents = out;
992
+ }
993
+
994
+ # endif // d_m3DeterministicProfile
995
+
996
+ // Waits on the subscriptions, once whichever reader above has decoded them, and
997
+ // writes the events back. Knows nothing about revisions: the event record is the
998
+ // same in both, and the caller has bounds-checked o_events for i_count of them.
999
+ //
1000
+ // The host layer is handed the whole list at once rather than a batch at a time -
1001
+ // unlike the transfer calls - because waiting on half of a list is not waiting on
1002
+ // the list: an event on a descriptor in the second half would go unnoticed for as
1003
+ // long as the first half took.
1004
+ static
1005
+ __wasi_errno_t poll_subscriptions (IM3Runtime i_runtime, const m3_wasi_pollsub_t* i_subs,
1006
+ uint8_t* o_events, __wasi_size_t i_count,
1007
+ __wasi_size_t* o_numEvents)
1008
+ {
1009
+ *o_numEvents = 0;
1010
+
1011
+ m3_wasi_pollevent_t* evts =
1012
+ (m3_wasi_pollevent_t*)m3_Malloc("WASI events", i_count * sizeof(m3_wasi_pollevent_t));
1013
+
1014
+ if (evts == NULL) {
1015
+ return __WASI_ERRNO_NOMEM;
1016
+ }
1017
+
1018
+ __wasi_size_t numEvents = 0;
1019
+ __wasi_errno_t err = __WASI_ERRNO_SUCCESS;
1020
+
1021
+ # if d_m3DeterministicProfile
1022
+ poll_deterministically(i_runtime, i_subs, i_count, evts, &numEvents);
1023
+ # else
1024
+ err = m3_wasi_host_poll_oneoff(i_subs, i_count, evts, &numEvents);
1025
+ # endif
1026
+
1027
+ if (err == __WASI_ERRNO_SUCCESS) {
1028
+ for (__wasi_size_t i = 0; i < numEvents; i++) {
1029
+ write_event(o_events + (size_t)i * d_m3WasiEventSize, &evts[i]);
1030
+ }
1031
+ *o_numEvents = numEvents;
1032
+ }
1033
+
1034
+ m3_Free(evts);
1035
+
1036
+ return err;
1037
+ }
1038
+
1039
+ m3ApiRawFunction(m3_wasi_p1_poll_oneoff)
1040
+ {
1041
+ m3ApiReturnType(uint32_t)
1042
+ m3ApiGetArgMem(const uint8_t*, in)
1043
+ m3ApiGetArgMem(uint8_t*, out)
1044
+ m3ApiGetArg(__wasi_size_t, nsubscriptions)
1045
+ m3ApiGetArgMem(__wasi_size_t*, nevents)
1046
+
1047
+ m3ApiCheckMem(in, (uint64_t)nsubscriptions * d_m3WasiSubscriptionSize);
1048
+ m3ApiCheckMem(out, (uint64_t)nsubscriptions * d_m3WasiEventSize);
1049
+ m3ApiCheckMem(nevents, sizeof(__wasi_size_t));
1050
+
1051
+ if (nsubscriptions == 0) {
1052
+ m3ApiReturn(__WASI_ERRNO_INVAL);
1053
+ }
1054
+
1055
+ m3_wasi_pollsub_t* subs = read_subscriptions(in, nsubscriptions);
1056
+
1057
+ if (subs == NULL) {
1058
+ m3ApiReturn(__WASI_ERRNO_NOMEM);
1059
+ }
1060
+
1061
+ __wasi_size_t numEvents = 0;
1062
+ __wasi_errno_t err = poll_subscriptions(runtime, subs, out, nsubscriptions, &numEvents);
1063
+
1064
+ m3_Free(subs);
1065
+
1066
+ if (err != __WASI_ERRNO_SUCCESS) {
1067
+ m3ApiReturn(err);
1068
+ }
1069
+
1070
+ m3ApiWriteMem32(nevents, numEvents);
1071
+ m3ApiReturn(__WASI_ERRNO_SUCCESS);
1072
+ }
1073
+
1074
+
1075
+ static
1076
+ M3Result SuppressLookupFailure (M3Result i_result)
1077
+ {
1078
+ if (i_result == m3Err_functionLookupFailed) {
1079
+ return m3Err_none;
1080
+ } else {
1081
+ return i_result;
1082
+ }
1083
+ }
1084
+
1085
+ m3_wasi_context_t* m3_GetWasiContext ()
1086
+ {
1087
+ return wasi_context;
1088
+ }
1089
+
1090
+ void m3_FreeWasi (m3_wasi_context_t* wasi)
1091
+ {
1092
+ m3_Free(wasi);
1093
+ }
1094
+
1095
+ static inline
1096
+ M3Result _linkWASI (IM3Module module, m3_wasi_context_t* wasi_context)
1097
+ {
1098
+ M3Result result = m3Err_none;
1099
+
1100
+ m3_wasi_host_init();
1101
+
1102
+ static const char* namespaces[] = { "wasi_snapshot_preview1" };
1103
+
1104
+ // clang-format off
1105
+
1106
+ for (size_t i = 0; i < M3_COUNT_OF(namespaces); i++) {
1107
+ const char* wasi = namespaces[i];
1108
+
1109
+ _ (SuppressLookupFailure(m3_LinkRawFunctionEx(module, wasi, "args_get", "i(**)", &m3_wasi_p1_args_get, wasi_context)));
1110
+ _ (SuppressLookupFailure(m3_LinkRawFunctionEx(module, wasi, "args_sizes_get", "i(**)", &m3_wasi_p1_args_sizes_get, wasi_context)));
1111
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "clock_res_get", "i(i*)", &m3_wasi_p1_clock_res_get)));
1112
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "clock_time_get", "i(iI*)", &m3_wasi_p1_clock_time_get)));
1113
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "environ_get", "i(**)", &m3_wasi_p1_environ_get)));
1114
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "environ_sizes_get", "i(**)", &m3_wasi_p1_environ_sizes_get)));
1115
+
1116
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_advise", "i(iIIi)", &m3_wasi_p1_fd_advise)));
1117
+ //_ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_allocate", "i(iII)", )));
1118
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_close", "i(i)", &m3_wasi_p1_fd_close)));
1119
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_datasync", "i(i)", &m3_wasi_p1_fd_datasync)));
1120
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_fdstat_get", "i(i*)", &m3_wasi_p1_fd_fdstat_get)));
1121
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_fdstat_set_flags", "i(ii)", &m3_wasi_p1_fd_fdstat_set_flags)));
1122
+ //_ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_fdstat_set_rights", "i(iII)", )));
1123
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_filestat_get", "i(i*)", &m3_wasi_p1_fd_filestat_get)));
1124
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_filestat_set_size", "i(iI)", &m3_wasi_p1_fd_filestat_set_size)));
1125
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_filestat_set_times","i(iIIi)", &m3_wasi_p1_fd_filestat_set_times)));
1126
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_pread", "i(i*iI*)", &m3_wasi_p1_fd_pread)));
1127
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_prestat_get", "i(i*)", &m3_wasi_p1_fd_prestat_get)));
1128
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_prestat_dir_name", "i(i*i)", &m3_wasi_p1_fd_prestat_dir_name)));
1129
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_pwrite", "i(i*iI*)", &m3_wasi_p1_fd_pwrite)));
1130
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_read", "i(i*i*)", &m3_wasi_p1_fd_read)));
1131
+ //_ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_readdir", "i(i*iI*)",)));
1132
+ //_ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_renumber", "i(ii)", )));
1133
+ //_ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_sync", "i(i)", )));
1134
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_seek", "i(iIi*)", &m3_wasi_p1_fd_seek)));
1135
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_tell", "i(i*)", &m3_wasi_p1_fd_tell)));
1136
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "fd_write", "i(i*i*)", &m3_wasi_p1_fd_write)));
1137
+
1138
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "path_create_directory", "i(i*i)", &m3_wasi_p1_path_create_directory)));
1139
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "path_filestat_get", "i(ii*i*)", &m3_wasi_p1_path_filestat_get)));
1140
+ //_ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "path_filestat_set_times", "i(ii*iIIi)", )));
1141
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "path_link", "i(ii*ii*i)", &m3_wasi_p1_path_link)));
1142
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "path_open", "i(ii*iiIIi*)", &m3_wasi_p1_path_open)));
1143
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "path_readlink", "i(i*i*i*)", &m3_wasi_p1_path_readlink)));
1144
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "path_remove_directory", "i(i*i)", &m3_wasi_p1_path_remove_directory)));
1145
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "path_rename", "i(i*ii*i)", &m3_wasi_p1_path_rename)));
1146
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "path_symlink", "i(*ii*i)", &m3_wasi_p1_path_symlink)));
1147
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "path_unlink_file", "i(i*i)", &m3_wasi_p1_path_unlink_file)));
1148
+
1149
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "poll_oneoff", "i(**i*)", &m3_wasi_p1_poll_oneoff)));
1150
+ _ (SuppressLookupFailure(m3_LinkRawFunctionEx(module, wasi, "proc_exit", "v(i)", &m3_wasi_p1_proc_exit, wasi_context)));
1151
+ //_ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "proc_raise", "i(i)", )));
1152
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "random_get", "i(*i)", &m3_wasi_p1_random_get)));
1153
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "sched_yield", "i()", &m3_wasi_p1_sched_yield)));
1154
+
1155
+ //_ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "sock_recv", "i(i*ii**)", )));
1156
+ //_ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "sock_send", "i(i*ii*)", )));
1157
+ //_ (SuppressLookupFailure(m3_LinkRawFunction(module, wasi, "sock_shutdown", "i(ii)", )));
1158
+ }
1159
+
1160
+ // clang-format on
1161
+
1162
+ // WASI addresses the memory the module exports as "memory", not whichever
1163
+ // one the calling code happens to be running against. Resolve it once, here,
1164
+ // rather than per call. A module that linked none of the above imports has
1165
+ // no WASI to satisfy, so it is not asked for the export - link_all offers
1166
+ // WASI to every module, and most have neither the imports nor a memory.
1167
+ if (Module_HasLinkedHostImport(module, "wasi_snapshot_preview1")) {
1168
+ u32 memoryIndex;
1169
+
1170
+ if (m3_FindExportedMemory(module, "memory", &memoryIndex)) {
1171
+ _throw("WASI requires the module to export its memory as \"memory\"");
1172
+ }
1173
+
1174
+ _ (m3_BindImportMemory(module, "wasi_snapshot_preview1", memoryIndex));
1175
+ }
1176
+
1177
+ _catch:
1178
+ return result;
1179
+ }
1180
+
1181
+ M3Result m3_NewCustomWASI (M3WASI* wasi_p)
1182
+ {
1183
+ *wasi_p = m3_AllocStruct(m3_wasi_context_t);
1184
+
1185
+ return m3Err_none;
1186
+ }
1187
+
1188
+ void m3_FreeCustomWASI (M3WASI wasi)
1189
+ {
1190
+ m3_Free(wasi);
1191
+ }
1192
+
1193
+ M3Result m3_LinkCustomWASI (IM3Module module, M3WASI wasi)
1194
+ {
1195
+ return _linkWASI(module, wasi);
1196
+ }
1197
+
1198
+ M3Result m3_LinkWASI (IM3Module module)
1199
+ {
1200
+ if (!wasi_context) {
1201
+ wasi_context = m3_AllocStruct(m3_wasi_context_t);
1202
+ }
1203
+
1204
+ return _linkWASI(module, wasi_context);
1205
+ }
1206
+
1207
+ #endif // d_m3HasWASI