|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Detect `&mut self` or `&mut T` parameters on #[wasm_bindgen] boundaries. |
| 5 | +# These cause "recursive use of an object" runtime panics when JS callbacks |
| 6 | +# re-enter during the call. Use RefCell for interior mutability instead. |
| 7 | +# |
| 8 | +# Usage: |
| 9 | +# scripts/lint-wasm-mut.sh [--workspace-root DIR] [--github-annotations] |
| 10 | +# |
| 11 | +# Options: |
| 12 | +# --workspace-root DIR Root of the workspace (default: script's parent dir) |
| 13 | +# --github-annotations Format errors as GitHub Actions annotations |
| 14 | + |
| 15 | +workspace_root="" |
| 16 | +github_annotations=false |
| 17 | + |
| 18 | +while [[ $# -gt 0 ]]; do |
| 19 | + case "$1" in |
| 20 | + --workspace-root) workspace_root="$2"; shift 2 ;; |
| 21 | + --github-annotations) github_annotations=true; shift ;; |
| 22 | + *) echo "Unknown option: $1" >&2; exit 1 ;; |
| 23 | + esac |
| 24 | +done |
| 25 | + |
| 26 | +if [[ -z "$workspace_root" ]]; then |
| 27 | + workspace_root="$(cd "$(dirname "$0")/.." && pwd)" |
| 28 | +fi |
| 29 | + |
| 30 | +# Prefer gawk, fall back to awk (works on macOS and ubuntu-latest) |
| 31 | +if command -v gawk &>/dev/null; then |
| 32 | + AWK=gawk |
| 33 | +else |
| 34 | + AWK=awk |
| 35 | +fi |
| 36 | + |
| 37 | +rc=0 |
| 38 | + |
| 39 | +for crate_dir in "$workspace_root"/*_wasm; do |
| 40 | + [[ -d "$crate_dir/src" ]] || continue |
| 41 | + crate_name="$(basename "$crate_dir")" |
| 42 | + |
| 43 | + # State machine in awk: |
| 44 | + # - On `#[wasm_bindgen` attribute -> mark next fn as wasm-exported |
| 45 | + # - On `impl` block preceded by `#[wasm_bindgen` -> all fns in the block |
| 46 | + # are wasm-exported until brace depth returns to 0 |
| 47 | + # - Inside a wasm-exported context, flag any `&mut self` or `&mut <Ident>` |
| 48 | + # in fn parameter lists |
| 49 | + # |
| 50 | + # Intentionally conservative: may false-positive on private helpers inside |
| 51 | + # a wasm_bindgen impl block. That's fine — the fix (RefCell) is always safe, |
| 52 | + # and false negatives (missing a real problem) are worse. |
| 53 | + while IFS= read -r src_file; do |
| 54 | + $AWK -v github="$github_annotations" ' |
| 55 | + BEGIN { in_wb_impl = 0; brace_depth = 0; wb_attr = 0; found = 0 } |
| 56 | +
|
| 57 | + /^[[:space:]]*#\[wasm_bindgen/ { wb_attr = 1 } |
| 58 | +
|
| 59 | + /^[[:space:]]*(pub[[:space:]]+)?impl[[:space:]]/ { |
| 60 | + if (wb_attr) { in_wb_impl = 1; brace_depth = 0 } |
| 61 | + wb_attr = 0 |
| 62 | + } |
| 63 | +
|
| 64 | + in_wb_impl { |
| 65 | + n = split($0, chars, "") |
| 66 | + for (i = 1; i <= n; i++) { |
| 67 | + if (chars[i] == "{") brace_depth++ |
| 68 | + if (chars[i] == "}") { |
| 69 | + brace_depth-- |
| 70 | + if (brace_depth <= 0) { in_wb_impl = 0; brace_depth = 0 } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +
|
| 75 | + (in_wb_impl || wb_attr) && /fn[[:space:]]+[a-zA-Z_]/ { |
| 76 | + if (/&mut[[:space:]]/) { |
| 77 | + if (github == "true") |
| 78 | + printf "::error file=%s,line=%d::%s\n", FILENAME, NR, $0 |
| 79 | + else |
| 80 | + printf " %s:%d: %s\n", FILENAME, NR, $0 |
| 81 | + found = 1 |
| 82 | + } |
| 83 | + } |
| 84 | +
|
| 85 | + wb_attr && /^[[:space:]]*(pub[[:space:]]+)?fn[[:space:]]/ { |
| 86 | + if (/&mut[[:space:]]/) { |
| 87 | + if (github == "true") |
| 88 | + printf "::error file=%s,line=%d::%s\n", FILENAME, NR, $0 |
| 89 | + else |
| 90 | + printf " %s:%d: %s\n", FILENAME, NR, $0 |
| 91 | + found = 1 |
| 92 | + } |
| 93 | + wb_attr = 0 |
| 94 | + } |
| 95 | +
|
| 96 | + !/^[[:space:]]*#/ && !/^[[:space:]]*$/ && !/^[[:space:]]*(pub[[:space:]]+)?impl/ && !/^[[:space:]]*(pub[[:space:]]+)?fn/ { |
| 97 | + wb_attr = 0 |
| 98 | + } |
| 99 | +
|
| 100 | + END { exit found } |
| 101 | + ' "$src_file" || { |
| 102 | + echo "FAIL: found &mut on wasm_bindgen boundary in $crate_name" |
| 103 | + rc=1 |
| 104 | + } |
| 105 | + done < <(find "$crate_dir/src" -name '*.rs' -type f) |
| 106 | +done |
| 107 | + |
| 108 | +if [[ "$rc" -eq 0 ]]; then |
| 109 | + echo "✓ No &mut on wasm_bindgen boundaries" |
| 110 | +else |
| 111 | + echo "" |
| 112 | + echo "Fix: use RefCell for interior mutability so wasm_bindgen only" |
| 113 | + echo "takes shared borrows (&self). See automerge_sedimentree_wasm/" |
| 114 | + echo "src/fragment.rs for an example." |
| 115 | + exit 1 |
| 116 | +fi |
0 commit comments