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,244 @@
1
+ //
2
+ // m3_function.h
3
+ //
4
+ // Created by Steven Massey on 4/7/21.
5
+ // Copyright © 2021 Steven Massey. All rights reserved.
6
+ //
7
+
8
+ #ifndef m3_function_h
9
+ #define m3_function_h
10
+
11
+ #include "m3_core.h"
12
+
13
+ d_m3BeginExternC
14
+
15
+ //---------------------------------------------------------------------------------------------------------------------------------
16
+
17
+ typedef struct M3FuncType {
18
+ struct M3FuncType* next;
19
+
20
+ u16 numRets;
21
+ u16 numArgs;
22
+
23
+ // Position in the environment's list of distinct function types. Equal
24
+ // types share one M3FuncType, so this doubles as the canonical heap type
25
+ // index a (ref $t) carries, and comparing two of them is exactly the
26
+ // structural equivalence the spec asks for.
27
+ u16 canonicalIndex;
28
+
29
+ bool isContinuation;
30
+ struct M3FuncType* contFuncType;
31
+
32
+ // A member of a recursive group. The group is its identity, so it never
33
+ // shares an entry with a type that merely matches it field for field -
34
+ // not one outside any group, and not one in a group spelled the same way.
35
+ bool inRecGroup;
36
+
37
+ m3type_t types[]; // returns, then args
38
+ } M3FuncType;
39
+
40
+ typedef M3FuncType* IM3FuncType;
41
+
42
+
43
+ M3Result AllocFuncType (IM3FuncType* o_functionType, u32 i_numTypes);
44
+ bool AreFuncTypesEqual (const IM3FuncType i_typeA, const IM3FuncType i_typeB);
45
+
46
+ u16 GetFuncTypeNumParams (const IM3FuncType i_funcType);
47
+ m3type_t GetFuncTypeParamType (const IM3FuncType i_funcType, u16 i_index);
48
+
49
+ u16 GetFuncTypeNumResults (const IM3FuncType i_funcType);
50
+ m3type_t GetFuncTypeResultType (const IM3FuncType i_funcType, u16 i_index);
51
+
52
+ #if d_m3HasTypedRefs
53
+ // The type a (ref $t) / (ref null $t) naming this function type is spelled as
54
+ m3type_t RefTypeOfFuncType (const IM3FuncType i_funcType, bool i_nonNull);
55
+ #endif
56
+
57
+ //---------------------------------------------------------------------------------------------------------------------------------
58
+
59
+ // The ways a function's frame can be left standing while it waits to go on.
60
+ // Two of them can share a pc - a call's return address can be the very next
61
+ // operation, which is itself one that suspends - and they do not agree on
62
+ // what the frame holds there, so a pc alone does not name one.
63
+ //
64
+ // A snapshot writes these down, so the numbering is part of the DMP format.
65
+ typedef enum M3SafePointKind {
66
+ safepoint_op, // at a loop back edge, which suspends in place
67
+ safepoint_suspend, // just past a suspend or a switch
68
+ safepoint_call, // a call waiting on its callee
69
+ safepoint_resume, // a resume waiting on the continuation it runs
70
+ safepoint_entry, // at the start of a function's body, its locals set
71
+ } M3SafePointKind;
72
+
73
+ #if d_m3HasSnapshots
74
+
75
+ // What a snapshot needs to know about a function's compiled code, recorded by
76
+ // the compiler when the runtime is suspendable and nowhere else.
77
+ //
78
+ // A snapshot is written in Wasm's terms, not this build's: a place in a
79
+ // function is the offset of a Wasm instruction, and the frame is the Wasm
80
+ // locals and operand stack as typed values. The map is what translates. It
81
+ // says, at every place a frame can be left standing, which slot or register
82
+ // this build keeps each of those values in - so the values can be read out of
83
+ // one build's frame and scattered into another's, whatever layout each chose.
84
+
85
+ // A value the frame holds: where this build keeps it, and its type
86
+ typedef struct M3SlotValue {
87
+ u16 slot; // relative to the frame, or a register alias (d_m3Reg0SlotAlias, d_m3Fp0SlotAlias)
88
+ m3type_t type; // full Wasm type, including reference heap type and nullability
89
+ } M3SlotValue;
90
+
91
+ // Values that go with a safepoint
92
+ # define d_m3SafePointTakenBranch 0x1 // resumes a br_if that was taken: _r0 has to hold its condition
93
+ # define d_m3SafePointTailCall 0x2 // a return_call compiled as a call: saving walks past it, and it names no frame
94
+
95
+ // A place a function can be suspended at, or be waiting on something that was:
96
+ // a loop back edge, a function's entry, a call, a suspend, a switch or a resume.
97
+ //
98
+ // Its identity in a snapshot is the instruction's offset and its kind, and for
99
+ // a back edge - since one br_table can branch back to several loops - the loop
100
+ // it goes to. Safepoints are recorded in the order the body is read, so they
101
+ // are sorted by that offset.
102
+ typedef struct M3SafePoint {
103
+ pc_t pc;
104
+ u32 wasmOffset; // the instruction's, from the start of the function's body
105
+ u32 firstValue; // into M3SnapshotMap.values
106
+ u32 firstLocalValue; // local types at this point, or UINT32_MAX for the map's shared locals
107
+ u16 numValues; // the live operand stack, bottom up; the locals are the map's own
108
+ u16 numResults; // suspend: the top values are the slots it waits in | call, resume: results not yet written
109
+ u16 aux; // call: where the callee's frame starts, in slots above this one | resume: its handler count
110
+ // | back edge: the loop it goes to, in M3SnapshotMap.blocks
111
+ m3type_t resumeType; // resume: expected continuation type, including the result contract
112
+ u8 kind; // M3SafePointKind
113
+ u8 flags;
114
+ } M3SafePoint;
115
+
116
+ // A loop or a try_table, which the interpreter keeps a native frame for while
117
+ // its body runs, and which a snapshot names by the instruction's offset.
118
+ //
119
+ // The region runs from the opening instruction to its end, and the ones that
120
+ // enclose a safepoint are those whose region holds its offset, both ends
121
+ // excluded - so a catch clause, which records its safepoints at its try_table,
122
+ // is outside that try_table. Recorded in the order they open, so enclosing
123
+ // regions come outermost first.
124
+ typedef struct M3BlockStart {
125
+ pc_t pc; // what the frame records: a loop's body, a try_table's clause table
126
+ u32 wasmOffset;
127
+ u32 wasmEnd; // the offset of its end
128
+ u16 numClauses; // try_table
129
+ u8 opcode; // c_waOp_loop or c_waOp_tryTable
130
+ } M3BlockStart;
131
+
132
+ typedef struct M3SnapshotMap {
133
+ M3SlotValue* locals; // the arguments, then the declared locals
134
+ u32 numLocals;
135
+
136
+ M3SafePoint* safePoints;
137
+ u32 numSafePoints;
138
+
139
+ M3SlotValue* values;
140
+ u32 numValues;
141
+
142
+ M3BlockStart* blocks;
143
+ u32 numBlocks;
144
+ } M3SnapshotMap;
145
+
146
+ void SnapshotMap_Free (M3SnapshotMap* i_map);
147
+
148
+ #endif // d_m3HasSnapshots
149
+
150
+ typedef struct M3Function {
151
+ struct M3Module* module;
152
+
153
+ M3ImportInfo import;
154
+
155
+ // The linear memory a host function bound to this import addresses. Set to
156
+ // the module's memory 0 when the host function is bound - what a bare i32
157
+ // guest pointer means when nothing says otherwise - and repointed by
158
+ // m3_BindImportMemory for a host module whose ABI names a memory instead.
159
+ // Holds the memory rather than its index: a slot can alias another module's
160
+ // memory, and growing one reallocates behind it. NULL for anything that is
161
+ // not an import bound to a host function.
162
+ struct M3Memory* hostMemory;
163
+
164
+ // An import linked to another module's export: the function that actually
165
+ // implements it. Everything that calls through an import resolves to this
166
+ // first, so the call carries the defining module with it - which is what
167
+ // says whose linear memory the body runs against. NULL for a function with
168
+ // a body of its own, and for an import bound to a host function (that one
169
+ // runs against the importing module's memory).
170
+ struct M3Function* resolved;
171
+
172
+ bytes_t wasm;
173
+ bytes_t wasmEnd;
174
+
175
+ // What traces and errors call the function - its import name, or its first
176
+ // few export names, or else the name section's - and what m3_FindFunction
177
+ // falls back to once no export matches. Not a list of exports: those are
178
+ // M3Module.exports, which keeps every one.
179
+ cstr_t names[d_m3MaxDuplicateFunctionImpl];
180
+ u16 numNames; // maximum of d_m3MaxDuplicateFunctionImpl
181
+
182
+ IM3FuncType funcType;
183
+
184
+ pc_t compiled;
185
+
186
+ #if (d_m3EnableCodePageRefCounting)
187
+ IM3CodePage* codePageRefs; // array of all pages used
188
+ u32 numCodePageRefs;
189
+ #endif
190
+
191
+ #if defined(DEBUG)
192
+ u32 hits;
193
+ u32 index;
194
+ #endif
195
+
196
+ u16 maxStackSlots;
197
+
198
+ u16 numRetSlots;
199
+ u16 numRetAndArgSlots;
200
+
201
+ u16 numLocals; // not including args
202
+ u32 numLocalBytes;
203
+
204
+ bool ownsWasmCode;
205
+
206
+ u16 numConstantBytes;
207
+ void* constants;
208
+
209
+ #if d_m3HasSnapshots
210
+ M3SnapshotMap* snapshotMap; // NULL unless compiled suspendable
211
+ #endif
212
+ } M3Function;
213
+
214
+
215
+ // The function that actually runs when this one is called: an import linked to
216
+ // another module resolves to that module's function, everything else to itself.
217
+ static inline
218
+ IM3Function Function_Implementation (IM3Function i_function)
219
+ {
220
+ return i_function->resolved ? i_function->resolved : i_function;
221
+ }
222
+
223
+ void Function_Release (IM3Function i_function);
224
+ void Function_FreeCompiledCode (IM3Function i_function);
225
+
226
+ cstr_t GetFunctionImportModuleName (IM3Function i_function);
227
+ cstr_t* GetFunctionNames (IM3Function i_function, u16* o_numNames);
228
+ u16 GetFunctionNumArgs (IM3Function i_function);
229
+ m3type_t GetFunctionArgType (IM3Function i_function, u32 i_index);
230
+
231
+ u16 GetFunctionNumReturns (IM3Function i_function);
232
+ u8 GetFunctionReturnType (const IM3Function i_function, u16 i_index);
233
+
234
+ u32 GetFunctionNumArgsAndLocals (IM3Function i_function);
235
+
236
+ cstr_t SPrintFunctionArgList (IM3Function i_function, m3stack_t i_sp);
237
+ cstr_t SPrintFunctionRetList (IM3FuncType i_funcType, m3stack_t i_sp);
238
+
239
+ //---------------------------------------------------------------------------------------------------------------------------------
240
+
241
+
242
+ d_m3EndExternC
243
+
244
+ #endif /* m3_function_h */
@@ -0,0 +1,226 @@
1
+ //
2
+ // m3_host.h
3
+ //
4
+ // What the engine asks of the system it is hosted on.
5
+ //
6
+ // Everything here is something only the operating system can answer: how much native
7
+ // stack this thread still has, how to get at a file's bytes and replace a file whole,
8
+ // and - where the build
9
+ // asks for guarded memories - how to reserve address space, commit part of it, and
10
+ // turn the fault from a read past the committed part back into a Wasm trap.
11
+ //
12
+ // Exactly one implementation is ever built:
13
+ //
14
+ // m3_host_win32.h VirtualAlloc/VirtualQuery, file mappings, and structured
15
+ // exception handling
16
+ // m3_host_posix.h mmap/mprotect, file mappings, and a SIGSEGV handler
17
+ // m3_host_none.h a system with none of that, which is most of platforms/:
18
+ // it reads a file rather than mapping one, and says it cannot
19
+ // measure a stack
20
+ //
21
+ // All three are headers, and m3_core.c includes whichever one it is built for: a new
22
+ // .c would have to be added to every source list that names its sources one by one,
23
+ // and most of those are build scripts outside this repository. Unlike the WASI host
24
+ // split, the callers here are in other translation units, so these are real
25
+ // definitions rather than static ones - this header is the only declaration of them
26
+ // there is.
27
+ //
28
+ // Every call is allowed to fail, and failing is not an error: m3_HostStackBase
29
+ // answers NULL on a system that cannot be asked, and the engine falls back to the
30
+ // compile-time budget. Only the reservation calls report failure the caller has to
31
+ // act on, and only a build with d_m3GuardedMemory has any.
32
+ //
33
+
34
+ #ifndef m3_host_h
35
+ #define m3_host_h
36
+
37
+ #include "m3_core.h"
38
+
39
+ d_m3BeginExternC
40
+
41
+ // The lowest address the calling thread's stack can reach, or NULL when this
42
+ // build cannot ask (see d_m3HasThreadStackProbe). What sits at the very bottom
43
+ // is the system's business - a guard page usually - so the answer bounds the
44
+ // stack rather than describing what is safe to touch; d_m3NativeStackMargin is
45
+ // what keeps the difference.
46
+ void* m3_HostStackBase (void);
47
+
48
+ // Suspension interrupt handlers (Ctrl+Z / SIGTSTP / SIGINT / Console Ctrl)
49
+ void m3_HostInstallInterruptHandler (IM3Runtime io_runtime);
50
+ void m3_HostRemoveInterruptHandler (void);
51
+
52
+ // The wall clock, in milliseconds since the Unix epoch, UTC. 0 when there is no
53
+ // clock to ask. Only ever written down - a snapshot says when it was taken - so
54
+ // nothing depends on it being right.
55
+ u64 m3_HostTimeMs (void);
56
+
57
+ // Blocks the calling thread for at least the given time. A host with no clock to
58
+ // wait on returns at once, as though the time had passed.
59
+ void m3_HostSleepNs (u64 i_ns);
60
+
61
+
62
+ // A file's bytes and how they were come by.
63
+ //
64
+ // 'mapped' is the one that says who gives the bytes back, and it is not the same
65
+ // question as whether there is a handle: a Win32 mapping keeps nothing open, because
66
+ // the view holds the section which holds the file object and the share mode with it,
67
+ // while POSIX has to hold the descriptor its lock lives on. So a mapped file may have
68
+ // no handle, and d_m3HostNoHandle is what that looks like.
69
+ #define d_m3HostNoHandle ((intptr_t) -1)
70
+
71
+ typedef struct M3HostFile {
72
+ void* data;
73
+ size_t size;
74
+ intptr_t handle; // the host layer's, and d_m3HostNoHandle when there is none
75
+ bool mapped;
76
+ } M3HostFile;
77
+
78
+ // The module's bytes, however this system can best get at them: mapped where it can
79
+ // map, read onto the heap where it cannot - a system with no mapping at all, a
80
+ // filesystem that will not, or something that is not a file, which is what a pipe
81
+ // from a process substitution is. The caller is told which only by 'mapped', and
82
+ // needs to know only because m3_HostUnmapFile is what gives the bytes back either
83
+ // way.
84
+ //
85
+ // i_maxBytes refuses a file larger than the caller is willing to take, before
86
+ // anything is allocated for it; 0 accepts any size the system will hand over. False
87
+ // is that, or the file not being readable at all, and o_file is written on success
88
+ // only.
89
+ bool m3_HostMapFile (const char* i_path, size_t i_maxBytes, M3HostFile* o_file);
90
+
91
+ // Give the bytes back, releasing the mapping and its lock, or the heap block
92
+ void m3_HostUnmapFile (M3HostFile* io_file);
93
+
94
+ // Move the file at i_from over i_to, replacing whatever is there. Where the system
95
+ // can, in one step: a reader of i_to sees the old file or the new one, never one
96
+ // half written, and a writer that dies part way leaves i_to as it was. False when
97
+ // the move failed, with i_from left where it is.
98
+ bool m3_HostReplaceFile (const char* i_from, const char* i_to);
99
+
100
+ // The reading half on its own: what each m3_HostMapFile falls back to, and the whole
101
+ // of what a system with no mapping does. The same on every system, so it is here
102
+ // rather than in any of them - and inline, so that a translation unit which never
103
+ // calls it is not the one that drags stdio's FILE machinery into the link.
104
+ static inline
105
+ bool m3_HostReadFile (const char* i_path, size_t i_maxBytes, M3HostFile* o_file)
106
+ {
107
+ FILE* f = fopen(i_path, "rb");
108
+ if (f == NULL) {
109
+ return false;
110
+ }
111
+
112
+ // The size is taken before anything is allocated, so that a file too big to be
113
+ // wanted is refused rather than read
114
+ fseek(f, 0, SEEK_END);
115
+ long tell = ftell(f);
116
+ fseek(f, 0, SEEK_SET);
117
+
118
+ if (tell < 0 or (i_maxBytes and (size_t) tell > i_maxBytes)) {
119
+ fclose(f);
120
+ return false;
121
+ }
122
+
123
+ size_t size = (size_t)tell;
124
+ void* data = m3_Malloc("Host File", size ? size : 1);
125
+
126
+ if (data == NULL) {
127
+ fclose(f);
128
+ return false;
129
+ }
130
+
131
+ bool read = (fread(data, 1, size, f) == size);
132
+ fclose(f);
133
+
134
+ if (not read) {
135
+ m3_Free(data);
136
+ return false;
137
+ }
138
+
139
+ o_file->data = data;
140
+ o_file->size = size;
141
+ o_file->handle = d_m3HostNoHandle;
142
+ o_file->mapped = false;
143
+ return true;
144
+ }
145
+
146
+ // A module is parsed in place and keeps pointing into the mapped bytes for as long
147
+ // as it is loaded, so this is where a module's own size stops being paid for twice:
148
+ // what is mapped stays in the page cache, shared with anything else running the same
149
+ // file, and is only ever read as far as the module is walked.
150
+ //
151
+ // What is mapped is the file itself, so a file that changes underneath the mapping
152
+ // changes under a running module. Each system is asked to prevent that as far as it
153
+ // can, and the two can do very different amounts:
154
+ //
155
+ // Win32 a share mode the kernel enforces. Another process opening the file for
156
+ // writing is refused outright while the mapping lives, so this is a real
157
+ // guarantee. Other readers - another wasm3 on the same module - are fine.
158
+ // POSIX a shared advisory lock, which stops only a writer that asks for a lock
159
+ // of its own. Nothing stops a plain open-and-write, because POSIX has no
160
+ // portable mandatory locking: Linux removed what it had in 5.15, and
161
+ // O_EXLOCK is a BSD extension that is advisory as well.
162
+ //
163
+ // So on POSIX this narrows the window rather than closing it.
164
+
165
+ #if d_m3GuardedMemory
166
+
167
+ # if M3_SIZEOF_PTR < 8
168
+ # error "d_m3GuardedMemory needs a 64-bit address space"
169
+ # endif
170
+ # if d_m3FixedHeap
171
+ # error "d_m3GuardedMemory and d_m3FixedHeap are two different places for a linear memory to come from"
172
+ # endif
173
+ # if defined(_WIN32) && !defined(_MSC_VER)
174
+ # error "d_m3GuardedMemory on Windows needs __try/__except - build with MSVC or clang-cl"
175
+ # endif
176
+
177
+ // The system's allocation granularity: what a reservation's size and a commit's
178
+ // bounds are rounded to. Never zero.
179
+ size_t m3_HostPageSize (void);
180
+
181
+ // Reserve i_bytes of address space without backing any of it. Reading or writing it
182
+ // faults until m3_HostCommit says otherwise, which is the whole point: the fault is
183
+ // what a Wasm access past the end of its memory turns into. NULL if the system will
184
+ // not.
185
+ void* m3_HostReserve (size_t i_bytes);
186
+
187
+ // Back [i_address, i_address + i_bytes) with pages, which read as zero. The range
188
+ // has to be inside a reservation from m3_HostReserve; committing what is already
189
+ // committed is not an error and changes nothing.
190
+ bool m3_HostCommit (void* i_address, size_t i_bytes);
191
+
192
+ // Undo m3_HostCommit for [i_address, i_address + i_bytes), giving the pages back to
193
+ // the system and leaving the range reserved and unreachable again. What was written
194
+ // there is gone: committing it once more reads as zero.
195
+ bool m3_HostDecommit (void* i_address, size_t i_bytes);
196
+
197
+ // Give back a whole reservation. i_bytes is what was reserved.
198
+ void m3_HostRelease (void* i_address, size_t i_bytes);
199
+
200
+ // Run i_body(i_context) with a fault inside [i_guardLow, i_guardLow + i_guardBytes)
201
+ // caught and reported rather than killing the process: false means the body touched
202
+ // that range and did not finish. Any other fault is left to whatever would have
203
+ // handled it, so an engine bug still looks like an engine bug.
204
+ //
205
+ // Nests, so a host function calling back into Wasm gets a region of its own, and is
206
+ // per-thread. What the body was doing when it faulted is abandoned where it stands -
207
+ // the caller has to be able to carry on without it, which for the interpreter means
208
+ // the Wasm stack and the memory, both of which are just data.
209
+ bool m3_HostProtectedCall (void (*i_body)(void*), void* i_context,
210
+ void* i_guardLow, size_t i_guardBytes);
211
+
212
+ // Whether m3_HostProtectedCall can actually catch that fault, which is not a given:
213
+ // a sanitizer takes the signal exclusively and a process is free to install whatever
214
+ // it likes afterwards. Memory operations carry no bounds check in a guarded build, so
215
+ // a false here means there is nothing underneath them and no Wasm may run.
216
+ //
217
+ // Puts the catching in place where it is not there yet, so this is also the call that
218
+ // installs it. Settled once, when it goes in: something that takes the fault away
219
+ // afterwards is not noticed, which is the bargain any handler in a process makes.
220
+ bool m3_HostGuardsActive (void);
221
+
222
+ #endif // d_m3GuardedMemory
223
+
224
+ d_m3EndExternC
225
+
226
+ #endif // m3_host_h
@@ -0,0 +1,76 @@
1
+ //
2
+ // m3_host_none.h
3
+ //
4
+ // The implementation of m3_host.h for a system with neither a POSIX nor a Win32
5
+ // face: a bare-metal target, an RTOS, or anything else. Most of the ports under
6
+ // platforms/ land here.
7
+ //
8
+ // There is no address space to reserve and no stack to measure, and "cannot say" is
9
+ // a supported answer to both, so a build gets those for nothing. A file is the one
10
+ // thing it still does, by reading it - which is the whole of what such a system can
11
+ // do, and costs it stdio.
12
+ //
13
+ // A header rather than a translation unit of its own, and included by m3_core.c
14
+ // alone: a new .c would have to be added to every source list that names them one by
15
+ // one, and most of those are in build scripts outside this repository.
16
+ //
17
+
18
+ #ifndef m3_host_none_h
19
+ #define m3_host_none_h
20
+
21
+ #include "m3_core.h"
22
+ #include "m3_host.h"
23
+
24
+ void* m3_HostStackBase (void)
25
+ {
26
+ return NULL;
27
+ }
28
+
29
+ void m3_HostInstallInterruptHandler (IM3Runtime io_runtime)
30
+ {
31
+ (void)io_runtime;
32
+ }
33
+
34
+ void m3_HostRemoveInterruptHandler (void)
35
+ {
36
+ }
37
+
38
+ // Plenty of these systems have no idea what time it is
39
+ u64 m3_HostTimeMs (void)
40
+ {
41
+ return 0;
42
+ }
43
+
44
+ void m3_HostSleepNs (u64 i_ns)
45
+ {
46
+ (void)i_ns;
47
+ }
48
+
49
+ // No mmap here, so the module's bytes are read directly onto the heap
50
+ bool m3_HostMapFile (const char* i_path, size_t i_maxBytes, M3HostFile* o_file)
51
+ {
52
+ return m3_HostReadFile(i_path, i_maxBytes, o_file);
53
+ }
54
+
55
+ void m3_HostUnmapFile (M3HostFile* io_file)
56
+ {
57
+ m3_Free(io_file->data);
58
+
59
+ io_file->size = 0;
60
+ io_file->handle = d_m3HostNoHandle;
61
+ io_file->mapped = false;
62
+ }
63
+
64
+ // Standard C leaves it to the system whether rename replaces a file that
65
+ // exists, so the target goes first. Two steps: in between, there is no i_to.
66
+ bool m3_HostReplaceFile (const char* i_from, const char* i_to)
67
+ {
68
+ remove(i_to);
69
+ return rename(i_from, i_to) == 0;
70
+ }
71
+
72
+ #if d_m3GuardedMemory
73
+ # error "d_m3GuardedMemory needs a system that can reserve address space; this one has none"
74
+ #endif
75
+
76
+ #endif // m3_host_none_h