pcbjam/scripts/common/apply-asyncify.sh
Viktor Vaczi b5be072461 asyncify: add wasm-opt -O2 pass + bump wxwidgets (modal promise fix)
Two related fixes for Chrome-specific WASM-runtime issues reported when
running a manually-loaded session (line tool wouldn't even toggle on click;
log filled with 'Uncaught (in promise) unwind' and stderr-tagged
[WASM_FCONTEXT]/[DIAG_*] spam):

1) scripts/common/apply-asyncify.sh — run 'wasm-opt -O2' as a separate
   pass after '--asyncify'.  Without this, large asyncify-instrumented
   coroutine-entry trampolines (notably libcontext's wasm_fcontext_entry
   and COROUTINE<int,TOOL_EVENT const&>::callerStub) exceed V8's
   per-function locals limit and silently stall on first fiber entry,
   leaving the toolbar click dispatched in C++ but the tool never
   activating its 'running=1'/[checked] state in the user's Chrome.
   Firefox tolerates the unoptimised version, so tests on Firefox passed
   while real Chrome stalled.  The -O2 pass shrinks every instrumented
   function back under the threshold, fixing the family of stalls
   systemically (no more per-function removelist whack-a-mole).
   The removelist still contains setupUIConditions() etc. as a safety
   net — they're now redundant under -O2 but harmless.
   Bundle: 338 MB -> 187 MB raw (~45% smaller); test runtime nearly
   halves because parse is faster.  See DEBUG.md §7 and
   memory/bundle-size-asyncify-optimization.md.

2) wxwidgets submodule bump (d1d1627 -> a998a8d) — wasm/dialog.cpp:
   startModal()'s setTimeout-based runEventLoop now awaits
   ccall('ProcessEvents', ..., {async:true}) so the Promise rejection
   from an asyncify-suspended ProcessEvents is caught by the existing
   try/catch instead of escaping as an 'Uncaught (in promise) unwind'
   page error.

Verified: npm run test:kicad:chrome and test:kicad:firefox both pass on
the rebuilt wasm; zero pageerror events; user-reported manual flow now
selects the Draw Lines tool and draws successfully.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-01 08:32:32 +02:00

76 lines
2.9 KiB
Shell
Executable file

#!/bin/bash
# Apply asyncify transformation to KiCad WASM
#
# Usage: ./scripts/common/apply-asyncify.sh <input.wasm> <output.wasm>
#
# This script is called by docker/build.sh but can also be run standalone
# for debugging asyncify issues.
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
# Get wasm-opt path
WASM_OPT=$("${SCRIPT_DIR}/get-wasm-opt.sh")
INPUT_WASM="${1:-output/pcbnew.wasm}"
OUTPUT_WASM="${2:-${INPUT_WASM}}"
if [ ! -f "${INPUT_WASM}" ]; then
echo "ERROR: Input file not found: ${INPUT_WASM}"
exit 1
fi
echo "Applying asyncify transformation..."
echo " Input: ${INPUT_WASM}"
echo " Output: ${OUTPUT_WASM}"
echo " Tool: ${WASM_OPT}"
# Asyncify import patterns (functions that trigger async suspension)
# - env.invoke_* : Exception handling trampolines
# - env.__asyncjs__* : EM_ASYNC_JS functions (like startModal())
ASYNCIFY_IMPORTS="env.invoke_*,env.__asyncjs__*,env.emscripten_fiber_swap"
# Functions to exclude from asyncify instrumentation
# These are large functions that inflate beyond V8's local-count limits.
ASYNCIFY_REMOVE=$(cat << 'REMOVELIST'
COLOR_SETTINGS::COLOR_SETTINGS(wxString const&, bool)
BuildBitmapInfo(std::__2::unordered_map<BITMAPS, std::__2::vector<BITMAP_INFO, std::__2::allocator<BITMAP_INFO>>, std::__2::hash<BITMAPS>, std::__2::equal_to<BITMAPS>, std::__2::allocator<std::__2::pair<BITMAPS const, std::__2::vector<BITMAP_INFO, std::__2::allocator<BITMAP_INFO>>>>>&)
match
DIALOG_PAD_PROPERTIES_BASE::DIALOG_PAD_PROPERTIES_BASE(wxWindow*, int, wxString const&, wxPoint const&, wxSize const&, long)
buildKicadAboutBanner(EDA_BASE_FRAME*, ABOUT_APP_INFO&)
IGESToBRep_CurveAndSurface::TransferGeometry(opencascade::handle<IGESData_IGESEntity> const&, Message_ProgressRange const&)
StepAP214_Protocol::StepAP214_Protocol()
BRepCheck_ParallelAnalyzer::operator()(int) const
ShapeFix_Wire::FixGap3d(int, bool)
ShapeFix_Wire::FixGap2d(int, bool)
PCB_EDIT_FRAME::setupUIConditions()
REMOVELIST
)
ASYNCIFY_REMOVE_ARG=$(echo "${ASYNCIFY_REMOVE}" | tr '\n' ',' | sed 's/,$//')
echo ""
echo "Running wasm-opt --asyncify..."
echo "This may take several minutes and use significant RAM..."
"${WASM_OPT}" --asyncify \
"--pass-arg=asyncify-imports@${ASYNCIFY_IMPORTS}" \
"--pass-arg=asyncify-removelist@${ASYNCIFY_REMOVE_ARG}" \
--pass-arg=asyncify-propagate-addlist \
"${INPUT_WASM}" -o "${OUTPUT_WASM}"
echo ""
echo "Running wasm-opt -O2 on the asyncified wasm..."
echo " Purpose: shrink asyncify-instrumented functions back under V8's"
echo " per-function locals limit (otherwise large coroutine-entry and"
echo " similar functions silently stall in Chrome's V8). See DEBUG.md §7"
echo " and memory/bundle-size-asyncify-optimization.md."
echo " This pass also takes several minutes and ~10-15 GB RAM."
"${WASM_OPT}" -O2 "${OUTPUT_WASM}" -o "${OUTPUT_WASM}"
echo ""
echo "Asyncify + -O2 complete: ${OUTPUT_WASM}"
ls -lh "${OUTPUT_WASM}"