emit_check.sh raw

   1  #!/bin/bash
   2  # Check the bundle a build PRODUCES, not the repository it came from.
   3  #
   4  # `moxie build -target wasm` stages the page-side half of the module beside it:
   5  # the JS runtime in $runtime/ and the page bootstrap <name>-wasm-host.mjs. This
   6  # builds a fixture, checks the staged set against jsruntime/, and then loads
   7  # THAT bootstrap in Chromium - so what is proven is the scaffolding an app
   8  # ships, not the checkout's own copy.
   9  #
  10  # Usage: emit_check.sh [fixture.mx]      (default: the DOM round-trip fixture)
  11  set -uo pipefail
  12  
  13  ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
  14  MOXIE="${MOXIE:-$ROOT/moxie}"
  15  FIXTURE="${1:-$ROOT/tests/regressions/should_compile/web_dom_roundtrip.mx}"
  16  EXPECTED="$ROOT/tests/wasm/expected/$(basename "$FIXTURE" .mx).txt"
  17  WORK="$ROOT/tests/wasm/tmp/emit"
  18  NAME="$(basename "$FIXTURE" .mx)"
  19  
  20  fail() {
  21  	echo "  FAIL: $*"
  22  	exit 1
  23  }
  24  
  25  rm -rf "$WORK"
  26  mkdir -p "$WORK/pkg"
  27  cp "$FIXTURE" "$WORK/pkg/main.mx"
  28  
  29  if ! (cd "$ROOT" && MOXIEROOT="$ROOT" "$MOXIE" build -target wasm \
  30  	-o "$WORK/$NAME.wasm" "$WORK/pkg") >"$WORK/build.log" 2>&1; then
  31  	fail "wasm build failed"
  32  	tail -3 "$WORK/build.log" | sed 's/^/      /'
  33  fi
  34  
  35  [ -f "$WORK/$NAME.wasm" ] || fail "no module at $WORK/$NAME.wasm"
  36  [ -d "$WORK/\$runtime" ] || fail "the build staged no \$runtime/ beside the module"
  37  [ -f "$WORK/$NAME-wasm-host.mjs" ] || fail "the build wrote no $NAME-wasm-host.mjs"
  38  
  39  # The staged runtime is the whole JS runtime, byte for byte. A partial copy is
  40  # the failure mode this check exists for: a bundle that loads in the checkout
  41  # and 404s in a deployed directory.
  42  want=$(ls "$ROOT"/jsruntime/*.mjs | wc -l)
  43  have=$(ls "$WORK/\$runtime"/*.mjs 2>/dev/null | wc -l)
  44  [ "$have" = "$want" ] || fail "staged $have runtime modules, jsruntime/ has $want"
  45  for f in "$ROOT"/jsruntime/*.mjs; do
  46  	b="$(basename "$f")"
  47  	cmp -s "$f" "$WORK/\$runtime/$b" || fail "staged $b differs from jsruntime/$b"
  48  done
  49  
  50  grep -q "\./$NAME\.wasm" "$WORK/$NAME-wasm-host.mjs" || fail "the bootstrap does not point at $NAME.wasm"
  51  
  52  # Serve the emitted directory as /tmp/emit/ (run.py maps tests/wasm/ to /).
  53  URL="/tmp/emit/$NAME-wasm-host.mjs"
  54  timeout "${RUN_TIMEOUT:-60}" python3 "$ROOT/tests/wasm/run.py" -q --host "$URL" \
  55  	>"$WORK/browser.out" 2>&1
  56  rc=$?
  57  [ "$rc" -eq 0 ] || { fail "the emitted bootstrap failed to run (rc=$rc)"; tail -3 "$WORK/browser.out" | sed 's/^/      /'; }
  58  
  59  if [ -f "$EXPECTED" ]; then
  60  	diff -q "$EXPECTED" "$WORK/browser.out" >/dev/null || {
  61  		fail "the emitted bundle produced different output"
  62  		diff "$EXPECTED" "$WORK/browser.out" | head -6 | sed 's/^/      /'
  63  	}
  64  fi
  65  
  66  # The bootstrap text exists twice, once per compiler, as string literals. The
  67  # two must describe the same file or a bundle built by legacy would differ from
  68  # one built by stage4 - the bilateral rule, checked at the only place it can be
  69  # without linking the legacy binary (which needs LLVM 22, absent here).
  70  python3 - "$ROOT" <<'PYEOF' || fail "the legacy and stage4 emitters no longer describe the same bootstrap"
  71  import pathlib, re, sys
  72  
  73  def literals(path, start_marker):
  74      src = pathlib.Path(path).read_text()
  75      i = src.index(start_marker)
  76      # Bound to the function body: the next top-level closing brace.
  77      body = src[i:]
  78      body = body[:body.index("\n}\n")]
  79      # The function's own string literals, in source order, unescaped.
  80      out = []
  81      for lit in re.findall(r'"((?:[^"\\]|\\.)*)"', body):
  82          out.append(lit.encode().decode('unicode_escape'))
  83      return "".join(out)
  84  
  85  stage4 = literals(sys.argv[1] + "/_mxc_stage4/main.mx", "func wasmHostModule")
  86  legacy = literals(sys.argv[1] + "/legacy/builder/wasmhost.go", "func wasmHostModule")
  87  
  88  def code(text):
  89      # The parts outside a function body: the comment is documentation, the
  90      # executable lines are what a page runs.
  91      return [l for l in text.splitlines()
  92              if l and not l.lstrip().startswith("//")]
  93  
  94  if code(stage4) != code(legacy):
  95      print("  stage4:", code(stage4))
  96      print("  legacy:", code(legacy))
  97      raise SystemExit(1)
  98  if len(code(stage4)) == 0:
  99      raise SystemExit("no bootstrap code found to compare")
 100  PYEOF
 101  
 102  # The service worker's half of the same bundle. Its host is staged by the same
 103  # build; this builds the module the host instantiates, registers the worker from
 104  # a page through the emitted glue, and requires activation with the module
 105  # actually fetched - a worker whose host or module is broken never gets there.
 106  SWFIX="$ROOT/tests/regressions/should_compile/web_sw_boot.mx"
 107  if [ -f "$SWFIX" ]; then
 108  	ls "$WORK/\$sw" >/dev/null 2>&1 || fail "the build staged no \$sw/ beside the module"
 109  	[ -f "$WORK/\$sw/wasm-host-sw.mjs" ] || fail "the build staged no \$sw/wasm-host-sw.mjs"
 110  	[ -f "$WORK/sw-register.js" ] || fail "the build wrote no sw-register.js"
 111  	mkdir -p "$WORK/swpkg"
 112  	cp "$SWFIX" "$WORK/swpkg/main.mx"
 113  	if ! (cd "$ROOT" && MOXIEROOT="$ROOT" "$MOXIE" build -target wasm \
 114  		-o "$WORK/\$sw/sw.wasm" "$WORK/swpkg") >"$WORK/sw-build.log" 2>&1; then
 115  		fail "service-worker module build failed"
 116  		tail -3 "$WORK/sw-build.log" | sed 's/^/      /'
 117  	fi
 118  	timeout "${RUN_TIMEOUT:-60}" python3 "$ROOT/tests/wasm/run.py" -q \
 119  		--host "/tmp/emit/$NAME-wasm-host.mjs" --sw /tmp/emit/sw-register.js \
 120  		>"$WORK/sw-browser.out" 2>"$WORK/sw-browser.err"
 121  	rc=$?
 122  	[ "$rc" -eq 0 ] || {
 123  		fail "the emitted service worker did not boot (rc=$rc)"
 124  		tail -3 "$WORK/sw-browser.err" | sed 's/^/      /'
 125  	}
 126  	grep -q "state=activated module_fetched=true" "$WORK/sw-browser.err" || {
 127  		fail "the emitted service worker did not activate with its module fetched"
 128  		tail -3 "$WORK/sw-browser.err" | sed 's/^/      /'
 129  	}
 130  	echo "  the emitted service worker booted: $(tail -1 "$WORK/sw-browser.err")"
 131  fi
 132  
 133  # A bundle whose module imports symbols only its app can answer: built here, run
 134  # with the app's own bridge module, and diffed against its expected output. The
 135  # generic phase-5 loop cannot run this one (the plain host has no app bridge by
 136  # definition), which is why it lives with the emitted-bundle checks.
 137  APPFIX="$ROOT/tests/wasm/fixtures/app_bridge/main.mx"
 138  if [ -f "$APPFIX" ]; then
 139  	mkdir -p "$WORK/apppkg"
 140  	cp "$APPFIX" "$WORK/apppkg/main.mx"
 141  	if ! (cd "$ROOT" && MOXIEROOT="$ROOT" "$MOXIE" build -target wasm \
 142  		-o "$WORK/app_bridge.wasm" "$WORK/apppkg") >"$WORK/app-build.log" 2>&1; then
 143  		fail "app-bridge fixture build failed"
 144  		tail -3 "$WORK/app-build.log" | sed 's/^/      /'
 145  	fi
 146  	timeout "${RUN_TIMEOUT:-60}" python3 "$ROOT/tests/wasm/run.py" -q \
 147  		--app /app-bridge-probe.mjs "$WORK/app_bridge.wasm" >"$WORK/app-browser.out" 2>&1
 148  	rc=$?
 149  	[ "$rc" -eq 0 ] || { fail "the bundle with an app bridge did not run (rc=$rc)"; tail -3 "$WORK/app-browser.out" | sed 's/^/      /'; }
 150  	EXPECTED="$ROOT/tests/wasm/expected/web_app_bridge.txt"
 151  	if [ -f "$EXPECTED" ]; then
 152  		diff -q "$EXPECTED" "$WORK/app-browser.out" >/dev/null || {
 153  			fail "the app bridge produced different output"
 154  			diff "$EXPECTED" "$WORK/app-browser.out" | head -4 | sed 's/^/      /'
 155  		}
 156  	fi
 157  	echo "  the app bridge answered an import the host does not provide: $(cat "$WORK/app-browser.out")"
 158  fi
 159  
 160  # A SECOND bundle the page holds a port to: built here, started through the
 161  # runtime staged beside it, and checked in both directions (the page must receive
 162  # the module's message, and the module's stdout must show the page's reply).
 163  BFIX="$ROOT/tests/wasm/fixtures/bundle_port/main.mx"
 164  if [ -f "$BFIX" ]; then
 165  	mkdir -p "$WORK/bundlepkg"
 166  	cp "$BFIX" "$WORK/bundlepkg/main.mx"
 167  	if ! (cd "$ROOT" && MOXIEROOT="$ROOT" "$MOXIE" build -target wasm \
 168  		-o "$WORK/bundle_port.wasm" "$WORK/bundlepkg") >"$WORK/bundle-build.log" 2>&1; then
 169  		fail "bundle-port fixture build failed"
 170  		tail -3 "$WORK/bundle-build.log" | sed 's/^/      /'
 171  	fi
 172  	# The bridge is the app's file, placed beside the bundle as an app would.
 173  	cp "$ROOT/tests/wasm/bundle-port-bridge.mjs" "$WORK/bundle-port-bridge.mjs"
 174  	timeout "${RUN_TIMEOUT:-60}" python3 "$ROOT/tests/wasm/run.py" -q \
 175  		--bundle /tmp/emit/bundle_port.wasm --bridge /tmp/emit/bundle-port-bridge.mjs \
 176  		>"$WORK/bundle-browser.out" 2>"$WORK/bundle-browser.err"
 177  	rc=$?
 178  	[ "$rc" -eq 0 ] || {
 179  		fail "the second bundle did not answer the page (rc=$rc)"
 180  		tail -3 "$WORK/bundle-browser.err" | sed 's/^/      /'
 181  	}
 182  	EXPECTED="$ROOT/tests/wasm/expected/bundle_port.txt"
 183  	if [ -f "$EXPECTED" ]; then
 184  		diff -q "$EXPECTED" "$WORK/bundle-browser.out" >/dev/null || {
 185  			fail "the second bundle produced different output"
 186  			diff "$EXPECTED" "$WORK/bundle-browser.out" | head -4 | sed 's/^/      /'
 187  		}
 188  	fi
 189  	echo "  the second bundle answered the page: $(head -1 "$WORK/bundle-browser.err")"
 190  fi
 191  
 192  echo "  the build staged $have runtime modules and a loadable bootstrap"
 193  exit 0
 194