lib-vt: wasm convenience functions and a simple example (#9309)

This adds a set of Wasm convenience functions to ease memory management.
These are all prefixed with `ghostty_wasm` and are documented as part of
the standard Doxygen docs.

I also added a very simple single-page HTML example that demonstrates
how to use the Wasm module for key encoding.

This also adds a bunch of safety checks to the C API to verify that
valid values are actually passed to the function. This is an easy to hit
bug.

**AI disclosure:** The example is AI-written with Amp. I read through
all the code and understand it but I can't claim there isn't a better
way, I'm far from a JS expert. It is simple and works currently though.
Happy to see improvements if anyone wants to contribute.
This commit is contained in:
Mitchell Hashimoto
2025-10-22 14:25:52 -07:00
committed by GitHub
parent 9dc2e5978f
commit c133fac7e7
13 changed files with 1027 additions and 2 deletions

View File

@@ -0,0 +1,76 @@
# WebAssembly Key Encoder Example
This example demonstrates how to use the Ghostty VT library from WebAssembly to encode key events into terminal escape sequences.
## What It Does
The example demonstrates using the Ghostty VT library from WebAssembly to encode key events:
1. Loads the `ghostty-vt.wasm` module
2. Creates a key encoder with Kitty keyboard protocol support
3. Creates a key event for left ctrl release
4. Queries the required buffer size (optional)
5. Encodes the event into a terminal escape sequence
6. Displays the result in both hexadecimal and string format
## Building
First, build the WebAssembly module:
```bash
zig build lib-vt -Dtarget=wasm32-freestanding -Doptimize=ReleaseSmall
```
This will create `zig-out/bin/ghostty-vt.wasm`.
## Running
**Important:** You must serve this via HTTP, not open it as a file directly. Browsers block loading WASM files from `file://` URLs.
From the **root of the ghostty repository**, serve with a local HTTP server:
```bash
# Using Python (recommended)
python3 -m http.server 8000
# Or using Node.js
npx serve .
# Or using PHP
php -S localhost:8000
```
Then open your browser to:
```
http://localhost:8000/example/wasm-key-encode/
```
Click "Run Example" to see the key encoding in action.
## Expected Output
```
Encoding event: left ctrl release with all Kitty flags enabled
Required buffer size: 12 bytes
Encoded 12 bytes
Hex: 1b 5b 35 37 3a 33 3b 32 3a 33 75
String: \x1b[57:3;2:3u
```
## Notes
- The example uses the convenience allocator functions exported by the wasm module
- Error handling is included to demonstrate proper usage patterns
- The encoded sequence `\x1b[57:3;2:3u` is a Kitty keyboard protocol sequence for left ctrl release with all features enabled
- The `env.log` function must be provided by the host environment for logging support
## Current Limitations
The current C API is verbose when called from WebAssembly because:
- Functions use output pointers requiring manual memory allocation in JavaScript
- Options must be set via pointers to values
- Buffer sizes require pointer parameters
See `WASM_API_PLAN.md` for proposed improvements to make the API more wasm-friendly.