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.
- package/AGENTS.md +1 -1
- package/dist/src/cli/extensions.js +11 -3
- package/dist/src/cli/extensions.js.map +1 -1
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/cli/help.js +1 -1
- package/dist/src/codegen/generator/state.js +7 -0
- package/dist/src/codegen/generator/state.js.map +1 -1
- package/dist/src/codegen/generator/tables.js +19 -1
- package/dist/src/codegen/generator/tables.js.map +1 -1
- package/dist/src/driver/bundler/graph.js +9 -5
- package/dist/src/driver/bundler/graph.js.map +1 -1
- package/dist/src/driver/fingerprint.d.ts +2 -2
- package/dist/src/driver/fingerprint.js +3 -3
- package/dist/src/driver/fingerprint.js.map +1 -1
- package/dist/src/driver/link-features.js +1 -1
- package/dist/src/driver/link-features.js.map +1 -1
- package/dist/src/driver/paths.js +8 -4
- package/dist/src/driver/paths.js.map +1 -1
- package/dist/src/driver/runtime-objects.d.ts +1 -1
- package/dist/src/driver/runtime-objects.js +13 -1
- package/dist/src/driver/runtime-objects.js.map +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/extensions/catalog.js +5 -1
- package/dist/src/extensions/catalog.js.map +1 -1
- package/dist/src/extensions/gui/index.js +0 -1
- package/dist/src/extensions/gui/index.js.map +1 -1
- package/dist/src/extensions/link-feature.d.ts +8 -0
- package/dist/src/extensions/node/stream-web/index.d.ts +6 -1
- package/dist/src/extensions/node/stream-web/index.js +59 -4
- package/dist/src/extensions/node/stream-web/index.js.map +1 -1
- package/dist/src/extensions/node/worker_threads/index.d.ts +14 -4
- package/dist/src/extensions/node/worker_threads/index.js +24 -11
- package/dist/src/extensions/node/worker_threads/index.js.map +1 -1
- package/dist/src/extensions/registry.d.ts +9 -1
- package/dist/src/extensions/registry.js +4 -1
- package/dist/src/extensions/registry.js.map +1 -1
- package/dist/src/extensions/wasm-asset.d.ts +16 -0
- package/dist/src/extensions/wasm-asset.js +28 -0
- package/dist/src/extensions/wasm-asset.js.map +1 -0
- package/dist/src/extensions/wasm.d.ts +17 -0
- package/dist/src/extensions/wasm.js +68 -0
- package/dist/src/extensions/wasm.js.map +1 -0
- package/dist/src/extensions/webworker/index.d.ts +24 -0
- package/dist/src/extensions/webworker/index.js +54 -0
- package/dist/src/extensions/webworker/index.js.map +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/tests/driver/link-features.test.js +9 -0
- package/dist/tests/driver/link-features.test.js.map +1 -1
- package/dist/tests/e2e/async-fs.test.js +44 -0
- package/dist/tests/e2e/async-fs.test.js.map +1 -1
- package/dist/tests/e2e/harness.d.ts +6 -0
- package/dist/tests/e2e/harness.js +13 -1
- package/dist/tests/e2e/harness.js.map +1 -1
- package/dist/tests/e2e/node-crypto-random.test.d.ts +8 -0
- package/dist/tests/e2e/node-crypto-random.test.js +148 -0
- package/dist/tests/e2e/node-crypto-random.test.js.map +1 -0
- package/dist/tests/e2e/node-modules.test.js +1 -1
- package/dist/tests/e2e/node-modules.test.js.map +1 -1
- package/dist/tests/e2e/web-streams.test.d.ts +8 -0
- package/dist/tests/e2e/web-streams.test.js +274 -0
- package/dist/tests/e2e/web-streams.test.js.map +1 -0
- package/dist/tests/e2e/web-worker.test.d.ts +7 -0
- package/dist/tests/e2e/web-worker.test.js +74 -0
- package/dist/tests/e2e/web-worker.test.js.map +1 -0
- package/dist/tests/e2e/webassembly.test.d.ts +8 -0
- package/dist/tests/e2e/webassembly.test.js +113 -0
- package/dist/tests/e2e/webassembly.test.js.map +1 -0
- package/dist/tests/e2e/worker-api.test.d.ts +14 -0
- package/dist/tests/e2e/worker-api.test.js +460 -0
- package/dist/tests/e2e/worker-api.test.js.map +1 -0
- package/dist/tests/e2e/worker-threads.test.d.ts +8 -0
- package/dist/tests/e2e/worker-threads.test.js +160 -0
- package/dist/tests/e2e/worker-threads.test.js.map +1 -0
- package/dist/tests/extensions/wasm.test.d.ts +1 -0
- package/dist/tests/extensions/wasm.test.js +53 -0
- package/dist/tests/extensions/wasm.test.js.map +1 -0
- package/doc/ai/cli.md +1 -1
- package/doc/ai/contributing.md +11 -4
- package/doc/ai/extensions.md +25 -2
- package/doc/ai/language-support.md +14 -2
- package/doc/ai/zh-CN/cli.md +1 -1
- package/doc/ai/zh-CN/contributing.md +8 -3
- package/doc/ai/zh-CN/extensions.md +23 -2
- package/doc/ai/zh-CN/language-support.md +13 -2
- package/doc/gui/api.md +1 -1
- package/doc/implemented/cli.md +1 -1
- package/doc/implemented/extensions.md +6 -5
- package/doc/implemented/node/stream-web.md +31 -0
- package/doc/implemented/node/worker-threads.md +88 -5
- package/doc/implemented/node.md +2 -1
- package/doc/implemented/streams.md +82 -0
- package/doc/implemented/wasm.md +80 -18
- package/doc/implemented/web-worker.md +84 -0
- package/doc/implemented.md +6 -2
- package/doc/unimplemented/deviations.md +1 -0
- package/doc/unimplemented/node/worker-threads.md +23 -0
- package/doc/unimplemented/node.md +1 -0
- package/doc/unimplemented/stdlib.md +2 -1
- package/doc/unimplemented/wasm.md +21 -9
- package/doc/unimplemented.md +1 -1
- package/doc/zh-CN/gui/api.md +1 -1
- package/doc/zh-CN/implemented/cli.md +1 -1
- package/doc/zh-CN/implemented/extensions.md +7 -6
- package/doc/zh-CN/implemented/node/stream-web.md +24 -0
- package/doc/zh-CN/implemented/node/worker-threads.md +77 -4
- package/doc/zh-CN/implemented/node.md +2 -1
- package/doc/zh-CN/implemented/streams.md +60 -0
- package/doc/zh-CN/implemented/wasm.md +73 -16
- package/doc/zh-CN/implemented/web-worker.md +79 -0
- package/doc/zh-CN/implemented.md +6 -2
- package/doc/zh-CN/unimplemented/deviations.md +1 -0
- package/doc/zh-CN/unimplemented/node/worker-threads.md +20 -0
- package/doc/zh-CN/unimplemented/node.md +1 -0
- package/doc/zh-CN/unimplemented/stdlib.md +2 -1
- package/doc/zh-CN/unimplemented/wasm.md +16 -8
- package/doc/zh-CN/unimplemented.md +1 -1
- package/llms.txt +2 -0
- package/package.json +8 -7
- package/runtime/ext_gui/browser.cpp +1 -1
- package/runtime/ext_node/stream_web/stream_web.c +20 -6
- package/runtime/worker/worker_api.c +422 -0
- package/runtime/worker/worker_codec.c +489 -0
- package/runtime/worker/worker_core.c +445 -0
- package/runtime/worker/worker_internal.h +223 -0
- package/runtime/worker/worker_platform.c +344 -0
- package/runtime/worker/worker_ports.c +203 -0
- package/runtime/worker/worker_transport.c +464 -0
- package/runtime/worker/worker_wire.c +150 -0
- package/runtime/xt_builtins.c +15 -0
- package/runtime/xt_constructors.c +49 -2
- package/runtime/xt_stdlib2/error.inc +4 -0
- package/runtime/xt_streams/common.inc +232 -0
- package/runtime/xt_streams/constructors.inc +232 -0
- package/runtime/xt_streams/encoding.inc +124 -0
- package/runtime/xt_streams/readable-extra.inc +372 -0
- package/runtime/xt_streams/readable.inc +415 -0
- package/runtime/xt_streams/transform.inc +195 -0
- package/runtime/xt_streams/writable.inc +542 -0
- package/runtime/xt_streams.c +30 -0
- package/runtime/xt_wasm/wasm.h +148 -0
- package/runtime/xt_wasm/wasm_api.c +357 -0
- package/runtime/xt_wasm/wasm_api.h +37 -0
- package/runtime/xt_wasm/wasm_bridge.c +30 -0
- package/runtime/xt_wasm/wasm_instance.c +591 -0
- package/runtime/xt_wasm/wasm_module.c +270 -0
- package/runtime/xt_wasm/wasm_values.c +177 -0
- package/runtime/xt_webassembly.c +15 -4
- package/scripts/fetch-vendored.ts +187 -20
- package/scripts/package-release.ts +3 -2
- package/src/cli/extensions.ts +11 -3
- package/src/cli/help.ts +1 -1
- package/src/codegen/generator/state.ts +7 -0
- package/src/codegen/generator/tables.ts +19 -1
- package/src/driver/bundler/graph.ts +9 -5
- package/src/driver/fingerprint.ts +3 -3
- package/src/driver/link-features.ts +1 -1
- package/src/driver/paths.ts +8 -4
- package/src/driver/runtime-objects.ts +13 -1
- package/src/driver/version.ts +1 -1
- package/src/extensions/catalog.ts +5 -1
- package/src/extensions/gui/index.ts +0 -1
- package/src/extensions/link-feature.ts +8 -0
- package/src/extensions/node/stream-web/index.ts +59 -4
- package/src/extensions/node/worker_threads/index.ts +24 -11
- package/src/extensions/registry.ts +13 -2
- package/src/extensions/wasm-asset.ts +32 -0
- package/src/extensions/wasm.ts +74 -0
- package/src/extensions/webworker/index.ts +58 -0
- package/src/index.ts +1 -0
- package/vendor/wasm3/.pin +1 -0
- package/vendor/wasm3/LICENSE +21 -0
- package/vendor/wasm3/m3_api_libc.c +265 -0
- package/vendor/wasm3/m3_api_libc.h +19 -0
- package/vendor/wasm3/m3_api_meta_wasi.c +1107 -0
- package/vendor/wasm3/m3_api_tracer.c +173 -0
- package/vendor/wasm3/m3_api_tracer.h +19 -0
- package/vendor/wasm3/m3_api_uvwasi.c +1290 -0
- package/vendor/wasm3/m3_api_wasi.c +1207 -0
- package/vendor/wasm3/m3_api_wasi.h +74 -0
- package/vendor/wasm3/m3_api_wasi_host.h +137 -0
- package/vendor/wasm3/m3_api_wasi_posix.h +979 -0
- package/vendor/wasm3/m3_api_wasi_win32.h +1093 -0
- package/vendor/wasm3/m3_atomic.h +114 -0
- package/vendor/wasm3/m3_bind.c +174 -0
- package/vendor/wasm3/m3_bind.h +20 -0
- package/vendor/wasm3/m3_code.c +232 -0
- package/vendor/wasm3/m3_code.h +74 -0
- package/vendor/wasm3/m3_compile.c +6603 -0
- package/vendor/wasm3/m3_compile.h +354 -0
- package/vendor/wasm3/m3_config.h +448 -0
- package/vendor/wasm3/m3_config_platforms.h +345 -0
- package/vendor/wasm3/m3_core.c +1034 -0
- package/vendor/wasm3/m3_core.h +526 -0
- package/vendor/wasm3/m3_deterministic.c +65 -0
- package/vendor/wasm3/m3_deterministic.h +59 -0
- package/vendor/wasm3/m3_env.c +3146 -0
- package/vendor/wasm3/m3_env.h +748 -0
- package/vendor/wasm3/m3_exception.h +33 -0
- package/vendor/wasm3/m3_exec.c +8 -0
- package/vendor/wasm3/m3_exec.h +4110 -0
- package/vendor/wasm3/m3_exec_atomic.h +146 -0
- package/vendor/wasm3/m3_exec_defs.h +76 -0
- package/vendor/wasm3/m3_function.c +278 -0
- package/vendor/wasm3/m3_function.h +244 -0
- package/vendor/wasm3/m3_host.h +226 -0
- package/vendor/wasm3/m3_host_none.h +76 -0
- package/vendor/wasm3/m3_host_posix.h +543 -0
- package/vendor/wasm3/m3_host_win32.h +253 -0
- package/vendor/wasm3/m3_info.c +616 -0
- package/vendor/wasm3/m3_info.h +38 -0
- package/vendor/wasm3/m3_math_utils.h +560 -0
- package/vendor/wasm3/m3_module.c +431 -0
- package/vendor/wasm3/m3_parse.c +1549 -0
- package/vendor/wasm3/m3_snapshot.c +3825 -0
- package/vendor/wasm3/m3_validate.c +1770 -0
- package/vendor/wasm3/m3_validate.h +25 -0
- package/vendor/wasm3/m3_wasi.h +10 -0
- package/vendor/wasm3/m3_xxh64.c +211 -0
- package/vendor/wasm3/wasm3.h +684 -0
- package/vendor/wasm3/wasm3_defs.h +350 -0
- 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
|