-
Notifications
You must be signed in to change notification settings - Fork 613
Expand file tree
/
Copy pathwasm-run
More file actions
executable file
·228 lines (210 loc) · 8.3 KB
/
Copy pathwasm-run
File metadata and controls
executable file
·228 lines (210 loc) · 8.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env sh
# wasm-run -- launch an Emscripten-built barretenberg binary under Node.
#
# Usage:
# wasm-run [--dir=PATH ...] [--mem=BYTES] PROGRAM [ARGS...]
#
# CLI surface:
# * --dir=PATH (repeatable) — exposes the directory(ies) to the wasm process
# and chdirs into the FIRST --dir so absolute and
# relative path lookups resolve identically to
# the prior runtime's --dir behaviour. NODERAWFS=1
# is set so Emscripten resolves host paths
# directly (no virtual FS staging).
# * --mem=BYTES — INFORMATIONAL ONLY. Surfaces a requested
# INITIAL_MEMORY via BB_WASM_INITIAL_MEMORY for
# caller code that wants to read it. Under
# Emscripten with MODULARIZE=1 the wasm
# binary's INITIAL_MEMORY is a link-time
# setting baked into the memory section; the
# loader does NOT honor a runtime override.
# To change INITIAL_MEMORY, edit
# cmake/toolchains/wasm-emscripten.cmake and
# rebuild.
#
# A leading `run` keyword (a vestige of the older wasm test harness) is
# rejected with a hard error; do not silently strip it.
#
# PROGRAM may be either the Emscripten loader (`prog.js`) or the basename of
# the binary. We always launch Node on the `.js` glue; the sibling `.wasm` is
# loaded by Emscripten itself.
set -eu
usage() {
cat <<'EOF' >&2
usage: wasm-run [--dir=PATH ...] [--mem=BYTES] PROGRAM [ARGS...]
Launches PROGRAM (either prog.js or its bare name) under Node. The first
--dir=PATH is used as the cwd; additional --dir entries are aggregated into
BB_WASM_DIRS for binaries that consult an explicit allowlist.
--dir=PATH expose PATH to the wasm process (repeatable; first sets cwd)
--mem=BYTES INFORMATIONAL: surfaces a requested INITIAL_MEMORY budget
via BB_WASM_INITIAL_MEMORY. INITIAL_MEMORY is a link-time
constant baked into the wasm binary by the toolchain
(cmake/toolchains/wasm-emscripten.cmake); to actually
change it, edit the toolchain and rebuild.
PROGRAM is resolved to its sibling .js (Emscripten emits prog.js + prog.wasm).
EOF
}
dirs=""
first_dir=""
mem=""
while [ $# -gt 0 ]; do
case "$1" in
--dir=*)
d=${1#--dir=}
if [ -z "$first_dir" ]; then
first_dir="$d"
dirs="$d"
else
dirs="$dirs:$d"
fi
shift
;;
--mem=*)
mem=${1#--mem=}
shift
;;
--help|-h)
usage
exit 0
;;
--)
shift
break
;;
--*)
echo "wasm-run: unknown option: $1" >&2
usage
exit 2
;;
run)
echo "wasm-run: leading 'run' keyword is no longer accepted." >&2
echo " The CLI is 'wasm-run [opts] PROGRAM [args]', not" >&2
echo " 'wasm-run run PROGRAM [args]'. Drop the 'run'." >&2
exit 2
;;
*)
break
;;
esac
done
if [ $# -lt 1 ]; then
echo "wasm-run: missing PROGRAM argument" >&2
usage
exit 2
fi
program=$1
shift
# Resolve to the .js loader. Emscripten emits prog.js + prog.wasm; if a caller
# passed the .wasm we still launch the .js.
case "$program" in
*.js)
loader=$program
;;
*.wasm)
loader=${program%.wasm}.js
;;
*)
if [ -f "$program.js" ]; then
loader="$program.js"
elif [ -f "$program" ] && [ -r "$program" ]; then
loader=$program
else
echo "wasm-run: cannot find loader for '$program' (expected '$program.js' next to it)." >&2
exit 2
fi
;;
esac
if [ ! -f "$loader" ]; then
echo "wasm-run: loader '$loader' does not exist." >&2
exit 2
fi
# Resolve the loader to an absolute path BEFORE any cwd change so the chdir
# below does not invalidate the relative path the caller supplied.
case "$loader" in
/*) abs_loader=$loader ;;
*) abs_loader="$PWD/$loader" ;;
esac
# Forward configuration via the environment.
# - NODERAWFS=1 lets Emscripten resolve host paths directly.
# - BB_WASM_DIRS exposes the requested allowlist to test code that wants to
# enumerate which host paths it may touch.
export NODERAWFS=1
if [ -n "$dirs" ]; then
export BB_WASM_DIRS="$dirs"
fi
# NOTE on --mem: under Emscripten with MODULARIZE=1, INITIAL_MEMORY is a
# LINK-TIME setting baked into the wasm binary's memory section -- the loader
# does NOT honor a runtime override. The toolchain's link-time
# INITIAL_MEMORY (see cmake/toolchains/wasm-emscripten.cmake) is the
# authoritative source. We accept --mem here only as a sanity-check (rejects
# obvious garbage like "--mem=abc") and surface it via BB_WASM_INITIAL_MEMORY
# for any caller code that wants to read its requested budget. We deliberately
# do NOT generate a preamble that pretends to override INITIAL_MEMORY -- that
# pattern leaks tmp files (exec defeats EXIT traps) AND is a no-op under
# MODULARIZE=1 (preamble file would set globalThis.Module which the loader
# does not consult).
if [ -n "$mem" ]; then
case "$mem" in
''|*[!0-9]*)
echo "wasm-run: --mem=BYTES must be a positive integer (got '$mem')" >&2
exit 2
;;
esac
export BB_WASM_INITIAL_MEMORY="$mem"
echo "wasm-run: note: --mem=$mem is informational only; INITIAL_MEMORY is a link-time" >&2
echo " setting baked into the wasm binary (see cmake/toolchains/wasm-emscripten.cmake)." >&2
fi
# Default Node flags:
# --no-warnings keeps gtest output readable. WebAssembly threads are on by
# default in Node >= 22, so we do NOT pass --experimental-wasm-threads
# (Node 22+ prints a deprecation warning for it).
# --max-old-space-size mirrors the historical wasm test heap budget.
NODE_BIN=${NODE:-node}
# Choose cwd: if --dir was given, run under the first directory so absolute
# and relative path lookups resolve identically to the prior runtime's
# --dir behaviour. Otherwise inherit the caller's cwd.
if [ -n "$first_dir" ]; then
cd "$first_dir"
fi
# With MODULARIZE=1 + EXPORT_ES6=1, the loader is a *factory module* — the
# program is not invoked by importing it. Generate a small launcher that
# imports the factory, calls it with the gtest argv, and propagates the
# Emscripten exit code. We use a temp file (no `node --eval` heredoc) so
# `import.meta.url` inside the loader resolves correctly via a real path.
launcher=$(mktemp --suffix=.mjs)
trap 'rm -f "$launcher"' EXIT INT TERM
cat > "$launcher" <<'LAUNCHER_MJS'
import { pathToFileURL } from 'node:url';
const target = process.argv[2];
const m = await import(pathToFileURL(target).href);
const factory = m.default ?? m.createBarretenbergModule;
process.exitCode = 0;
try {
await factory({
arguments: process.argv.slice(3),
// Forward the host process env so getenv("HOME") / getenv("CRS_PATH") /
// BB_* etc. resolve to the caller's actual values. Emscripten otherwise
// defaults Module.ENV.HOME to "/home/web_user", which breaks any code
// (notably srs/global_crs.cpp's ~/.bb-crs lookup) that derives a path
// from HOME. NODERAWFS=1 means we're already exposing the host FS, so
// the host env values are the right thing to use. Module.ENV is the
// table getenv() reads from; it gets populated by Emscripten in
// staticInit, so we override in preRun (which fires after staticInit
// and before main).
preRun: [(Module) => { Object.assign(Module.ENV, process.env); }],
print: (...a) => process.stdout.write(a.join(' ') + '\n'),
printErr: (...a) => process.stderr.write(a.join(' ') + '\n'),
onExit: (code) => { process.exitCode = code; },
});
} catch (e) {
if (e && typeof e.status === 'number') process.exitCode = e.status;
else { console.error('wasm-run launcher: factory threw:', e); process.exitCode = 1; }
}
LAUNCHER_MJS
set +e
"$NODE_BIN" \
--no-warnings \
--max-old-space-size=8192 \
"$launcher" "$abs_loader" "$@"
status=$?
exit "$status"