mirror of
https://github.com/neovim/neovim.git
synced 2026-08-17 17:04:47 -05:00
Merge pull request #41186 from Rawan10101/fix-wasm-browser
feat(wasm): track and fix ongoing issues + add WASM build workflow
This commit is contained in:
Executable
+81
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Build nvim.wasm
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
|
||||
OUTDIR="$ROOT/build-wasm"
|
||||
|
||||
source "$ROOT/.emsdk/emsdk_env.sh"
|
||||
|
||||
mkdir -p "$OUTDIR"
|
||||
|
||||
if ! command -v emcc >/dev/null 2>&1; then
|
||||
echo "ERROR: emcc not found"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! command -v zig >/dev/null 2>&1; then
|
||||
echo "ERROR: zig not found"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
emcc --version
|
||||
zig version
|
||||
|
||||
# Install Binaryen if it is not already available.
|
||||
BINARYEN_VERSION=132
|
||||
BINARYEN_DIR="$ROOT/.binaryen"
|
||||
BINARYEN_BIN="$BINARYEN_DIR/binaryen-version_${BINARYEN_VERSION}/bin"
|
||||
|
||||
if [ ! -x "$BINARYEN_BIN/wasm-opt" ]; then
|
||||
echo "Installing Binaryen ${BINARYEN_VERSION}..."
|
||||
|
||||
mkdir -p "$BINARYEN_DIR"
|
||||
|
||||
curl -L \
|
||||
"https://github.com/WebAssembly/binaryen/releases/download/version_${BINARYEN_VERSION}/binaryen-version_${BINARYEN_VERSION}-x86_64-linux.tar.gz" \
|
||||
-o /tmp/binaryen.tar.gz
|
||||
|
||||
tar -xf /tmp/binaryen.tar.gz -C "$BINARYEN_DIR"
|
||||
fi
|
||||
|
||||
export PATH="$BINARYEN_BIN:$PATH"
|
||||
|
||||
wasm-opt --version
|
||||
|
||||
zig build nvim_bin \
|
||||
-Dtarget=wasm32-emscripten \
|
||||
-Dcpu=generic+atomics+bulk_memory+mutable_globals \
|
||||
-Demscripten-sysroot="$EMSDK/upstream/emscripten/cache/sysroot" \
|
||||
-Doptimize=ReleaseSmall
|
||||
|
||||
ZIG_OUT="$ROOT/zig-out/bin"
|
||||
|
||||
# Optimize the final WASM artifact with Binaryen.
|
||||
wasm-opt -Oz -o "$ZIG_OUT/nvim.wasm" "$ZIG_OUT/nvim.wasm"
|
||||
|
||||
required=(
|
||||
nvim.wasm
|
||||
nvim.js
|
||||
nvim.data
|
||||
)
|
||||
|
||||
missing=()
|
||||
|
||||
for name in "${required[@]}"; do
|
||||
if [ -f "$ZIG_OUT/$name" ]; then
|
||||
cp "$ZIG_OUT/$name" "$OUTDIR/$name"
|
||||
else
|
||||
missing+=("$name")
|
||||
fi
|
||||
done
|
||||
|
||||
if [ "${#missing[@]}" -ne 0 ]; then
|
||||
echo "ERROR: Missing required WASM artifacts: ${missing[*]}"
|
||||
ls -la "$ZIG_OUT" || true
|
||||
exit 2
|
||||
fi
|
||||
|
||||
echo "build-wasm.sh finished successfully."
|
||||
@@ -0,0 +1,62 @@
|
||||
name: nvim.wasm build
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches:
|
||||
- 'master'
|
||||
- 'release-[0-9]+.[0-9]+'
|
||||
paths:
|
||||
- 'build.zig'
|
||||
- 'build.zig.zon'
|
||||
- '.github/scripts/build_wasm.sh'
|
||||
- '.github/workflows/build_wasm.yml'
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
build-wasm:
|
||||
name: Build nvim.wasm (Emscripten + Zig)
|
||||
runs-on: ubuntu-24.04
|
||||
timeout-minutes: 90
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v7
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
- name: Set up Zig
|
||||
uses: mlugg/setup-zig@d1434d08867e3ee9daa34448df10607b98908d29 # v2.2.1
|
||||
with:
|
||||
version: 0.16.0
|
||||
use-cache: false
|
||||
|
||||
- name: Cache emsdk, Binaryen & build outputs
|
||||
uses: actions/cache@v6.1.0
|
||||
with:
|
||||
path: |
|
||||
./.emsdk
|
||||
./.binaryen
|
||||
key: ${{ runner.os }}-wasm-${{ hashFiles('build.zig', 'build.zig.zon', '.github/scripts/build_wasm.sh') }}
|
||||
|
||||
- name: Install system deps
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y \
|
||||
python3 python3-pip curl unzip
|
||||
|
||||
- name: Install Emscripten SDK
|
||||
run: |
|
||||
set -euo pipefail
|
||||
if [ ! -d ./.emsdk ]; then
|
||||
git clone https://github.com/emscripten-core/emsdk.git ./.emsdk
|
||||
cd ./.emsdk
|
||||
./emsdk install 6.0.2
|
||||
./emsdk activate 6.0.2
|
||||
cd -
|
||||
fi
|
||||
|
||||
- name: Build nvim.wasm
|
||||
run: .github/scripts/build_wasm.sh
|
||||
@@ -190,8 +190,49 @@ jobs:
|
||||
build/${{ matrix.archive_name }}.msi
|
||||
retention-days: 1
|
||||
|
||||
wasm:
|
||||
needs: setup
|
||||
runs-on: ubuntu-24.04
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v7
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
- name: Set up Zig
|
||||
uses: mlugg/setup-zig@d1434d08867e3ee9daa34448df10607b98908d29 # v2.2.1
|
||||
with:
|
||||
version: 0.16.0
|
||||
use-cache: false
|
||||
|
||||
- name: Install system deps
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y \
|
||||
python3 python3-pip curl unzip
|
||||
|
||||
- name: Install Emscripten SDK
|
||||
run: |
|
||||
set -euo pipefail
|
||||
git clone https://github.com/emscripten-core/emsdk.git ./.emsdk
|
||||
cd ./.emsdk
|
||||
./emsdk install 6.0.2
|
||||
./emsdk activate 6.0.2
|
||||
cd -
|
||||
|
||||
- name: Build nvim.wasm
|
||||
run: .github/scripts/build_wasm.sh
|
||||
|
||||
- name: Upload wasm artifact
|
||||
uses: actions/upload-artifact@v7
|
||||
with:
|
||||
name: nvim-wasm
|
||||
path: build-wasm/*
|
||||
retention-days: 1
|
||||
|
||||
publish:
|
||||
needs: [linux, macos, windows]
|
||||
needs: [linux, macos, windows, wasm]
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
GH_REPO: ${{ github.repository }}
|
||||
@@ -244,6 +285,6 @@ jobs:
|
||||
run: |
|
||||
envsubst < "$GITHUB_WORKSPACE/.github/workflows/notes.md" > "$RUNNER_TEMP/notes.md"
|
||||
if [ "$TAG_NAME" != "nightly" ]; then
|
||||
gh release create stable $PRERELEASE --notes-file "$RUNNER_TEMP/notes.md" --title "$SUBJECT" --target $GITHUB_SHA nvim-macos-x86_64/* nvim-macos-arm64/* nvim-linux-x86_64/* nvim-linux-arm64/* nvim-appimage-x86_64/* nvim-appimage-arm64/* nvim-win-x86_64/* nvim-win-arm64/*
|
||||
gh release create stable $PRERELEASE --notes-file "$RUNNER_TEMP/notes.md" --title "$SUBJECT" --target $GITHUB_SHA nvim-macos-x86_64/* nvim-macos-arm64/* nvim-linux-x86_64/* nvim-linux-arm64/* nvim-appimage-x86_64/* nvim-appimage-arm64/* nvim-win-x86_64/* nvim-win-arm64/* nvim-wasm/*
|
||||
fi
|
||||
gh release create $TAG_NAME $PRERELEASE --notes-file "$RUNNER_TEMP/notes.md" --title "$SUBJECT" --target $GITHUB_SHA nvim-macos-x86_64/* nvim-macos-arm64/* nvim-linux-x86_64/* nvim-linux-arm64/* nvim-appimage-x86_64/* nvim-appimage-arm64/* nvim-win-x86_64/* nvim-win-arm64/*
|
||||
gh release create $TAG_NAME $PRERELEASE --notes-file "$RUNNER_TEMP/notes.md" --title "$SUBJECT" --target $GITHUB_SHA nvim-macos-x86_64/* nvim-macos-arm64/* nvim-linux-x86_64/* nvim-linux-arm64/* nvim-appimage-x86_64/* nvim-appimage-arm64/* nvim-win-x86_64/* nvim-win-arm64/* nvim-wasm/*
|
||||
|
||||
+20
-2
@@ -295,7 +295,7 @@ function translateKey(ev) {
|
||||
};
|
||||
if (named[ev.key]) return named[ev.key];
|
||||
if (ev.key.length === 1) {
|
||||
let char = ev.shiftKey ? ev.key : ev.key.toLowerCase();
|
||||
let char = ev.key;
|
||||
if (char === "<") char = "<lt>";
|
||||
if (!isCtrl && !isAlt) return char;
|
||||
let mod = "";
|
||||
@@ -368,13 +368,22 @@ function main() {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
transport.onExit((code) => {
|
||||
setStatus(`Neovim exited (code ${code}). Refresh the page to start a new session.`);
|
||||
gridEl.style.opacity = "0.5";
|
||||
});
|
||||
nvim.on("redraw", (params) =>
|
||||
handleRedrawEvents(uiState, gridEl, modeEl, params),
|
||||
);
|
||||
|
||||
gridEl.addEventListener("click", () => gridEl.focus());
|
||||
gridEl.addEventListener("keydown", (ev) => {
|
||||
const isPasteShortcut =
|
||||
(ev.ctrlKey && ev.shiftKey && ev.key.toLowerCase() === "v") || // Linux/Win: Ctrl+Shift+V
|
||||
(ev.metaKey && ev.key.toLowerCase() === "v"); // macOS: Cmd+V
|
||||
if (isPasteShortcut) {
|
||||
return;
|
||||
}
|
||||
const keys = translateKey(ev);
|
||||
if (!keys) return;
|
||||
ev.preventDefault();
|
||||
@@ -382,6 +391,15 @@ function main() {
|
||||
.request("nvim_input", [keys])
|
||||
.catch((e) => console.error("nvim_input failed", e));
|
||||
});
|
||||
gridEl.addEventListener("paste", (ev) => {
|
||||
ev.preventDefault();
|
||||
const text = ev.clipboardData.getData("text/plain");
|
||||
if (!text) return;
|
||||
nvim
|
||||
.request("nvim_paste", [text, true, -1])
|
||||
.catch((e) => console.error("paste nvim_input failed", e));
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
if (!crossOriginIsolated) {
|
||||
|
||||
+27
-4
@@ -356,7 +356,6 @@ self.onmessage = async (ev) => {
|
||||
m.ENV.TERM = "xterm-256color";
|
||||
m.ENV.HOME = "/home/user";
|
||||
m.ENV.VIMRUNTIME = "/runtime";
|
||||
m.ENV.NVIM_LOG_FILE = "/home/user/nvim.log";
|
||||
},
|
||||
],
|
||||
});
|
||||
@@ -511,7 +510,7 @@ self.onmessage = async (ev) => {
|
||||
);
|
||||
};
|
||||
|
||||
const { argc, argv } = makeArgv(m, ["nvim", "--embed", "--clean"]);
|
||||
const { argc, argv } = makeArgv(m, ["nvim", "--embed", "--cmd", "set noautoread"]);
|
||||
|
||||
let ret;
|
||||
|
||||
@@ -535,14 +534,38 @@ self.onmessage = async (ev) => {
|
||||
ret = await nvimPromise;
|
||||
|
||||
console.log("NVIM EXITED");
|
||||
postMessage({ type: "exited", code: ret });
|
||||
try {
|
||||
moduleRef.ccall("emscripten_force_exit", null, ["number"], [ret]);
|
||||
} catch (e) {
|
||||
safeStatus("force_exit failed: " + e);
|
||||
}
|
||||
const unread = unreadCount();
|
||||
safeStatus(
|
||||
`_nvim_main RETURNED ret=${ret}, unread bytes still in buffer=${unread}`,
|
||||
);
|
||||
} catch (e) {
|
||||
console.log('CAUGHT EXIT EXCEPTION', e, 'name=', e && e.name, 'status=', e && e.status, 'constructor=', e && e.constructor && e.constructor.name);
|
||||
|
||||
flushAll();
|
||||
safeStatus("EXCEPTION: " + e.message + "\nSTACK:\n" + e.stack);
|
||||
throw e;
|
||||
if (e && e.name === 'ExitStatus') {
|
||||
safeStatus(`Neovim exited cleanly, code=${e.status}`);
|
||||
ret = e.status;
|
||||
|
||||
// Notify app.js so it can show a session ended state instead of
|
||||
// leaving the UI looking frozen.
|
||||
postMessage({ type: "exited", code: ret });
|
||||
try {
|
||||
moduleRef.ccall("emscripten_force_exit", null, ["number"], [ret]);
|
||||
} catch (fe) {
|
||||
safeStatus("force_exit failed: " + fe);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
safeStatus('EXCEPTION: ' + e.message + '\nSTACK:\n' + e.stack);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
flushAll();
|
||||
|
||||
@@ -10,6 +10,7 @@ class WorkerTransport {
|
||||
this._bytesHandlers = [];
|
||||
this._statusHandlers = [];
|
||||
this._readyHandlers = [];
|
||||
this._exitHandlers = [];
|
||||
|
||||
this.worker = new Worker(workerPath);
|
||||
this.worker.onmessage = (ev) => {
|
||||
@@ -48,6 +49,10 @@ class WorkerTransport {
|
||||
console.log("[main] worker signaled ready, RPC channel should be live");
|
||||
this._readyHandlers.forEach((h) => h());
|
||||
}
|
||||
else if (msg.type === "exited") {
|
||||
console.log("worker signaled nvim exited, code=", msg.code);
|
||||
this._exitHandlers.forEach((h) => h(msg.code));
|
||||
}
|
||||
}
|
||||
|
||||
_emitStatus(text) {
|
||||
@@ -123,6 +128,9 @@ class WorkerTransport {
|
||||
onReady(cb) {
|
||||
this._readyHandlers.push(cb);
|
||||
}
|
||||
onExit(cb) {
|
||||
this._exitHandlers.push(cb);
|
||||
}
|
||||
persist() {
|
||||
this.worker.postMessage({ type: "persist" });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user