diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-21 02:30:14 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-21 02:52:47 -0300 |
| commit | dea2920233bbb64b20f7ccebd42ac5beab14033d (patch) | |
| tree | 4d1622bffae6fbf653764ea33cb038afd6f02754 /abi | |
| parent | ee5b6aa3ef0bf398c5381498af40fa93d20cf0b3 (diff) | |
| download | pardes-dea2920233bbb64b20f7ccebd42ac5beab14033d.tar.gz pardes-dea2920233bbb64b20f7ccebd42ac5beab14033d.zip | |
library: a browser's module is pardes's build's to make: build.zig exports `wasmModule(b, .{ .language = "rust" })` (the user's ruling: the self-contained module is exposed to the consumer's build.zig): wasm32-wasi, a reactor, one language's grammar and what it needs with tree-sitter's runtime and the highlighter, or with no language the patch view alone; its exports are abi/wasm.zig's (beside the other boundaries: a module of its own whose few globals are the page's, not a core's), the same in every module: `reserve`/`release` (toilet's, at least one byte so reserve(0) is a pointer), `highlight(path, path_len, text, len)` (toilet's without its diff flag: diffs go to the patch view now), `patch(text, len)` (a 20-byte header: the rows' text, a style byte a byte, and nine-u32 records: kind, old, new (0 for none), code_start, the row's text and its path as offsets, flags bit 0 no newline), `memory`, `_initialize`; only those are exported (export_symbol_names, not rdynamic: tree-sitter's and libc's symbols stay inside), and a panic is one `fd_write` to stderr and a trap (std's handler writes through std.Io and imported 26 more WASI functions): a language's module imports fd_write, fd_seek, fd_close (libc's streams, tree-sitter's debug output), the patch view's none, 24,533 bytes (11,108 gzip; with tree-sitter's runtime in it, 139,337); rust's is 1,290,182 (183,568 gzip; its parser object alone 1,134,394); test/consumer makes both through `wasmModule` and runs them under node (wasm.mjs: a page with every WASI import a stub reads the header and records back: rows, numbers, paths, the changed `*`, a no-newline flag, reserve(0), a highlight), saying so when there is no node; `sizes` installs one module a language of the manifest
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'abi')
| -rw-r--r-- | abi/wasm.zig | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/abi/wasm.zig b/abi/wasm.zig new file mode 100644 index 00000000..d2b3889a --- /dev/null +++ b/abi/wasm.zig @@ -0,0 +1,146 @@ +//! A browser's module (build.zig `wasmModule`: wasm32-wasi, a reactor): the +//! functions a page calls, the same in every module. Every pointer and +//! length is a wasm32 address or count, an unsigned 32-bit integer; numbers +//! in memory are little-endian. docs/typ/building.typ has the layouts. +//! +//! - `reserve(len)`: `len` bytes (one at least, so a pointer even for 0) +//! for the page to write an argument into; `release(ptr, len)` gives them +//! back. +//! - `highlight(path, path_len, text, len)` (a module with a language): a +//! style byte for each byte of `text`, a `Syn` (0 none, 1 keyword, 2 +//! string, 3 number, 4 comment), as pardes colours the file `path` names +//! by its extension; all 0 for a file of another language. The module's +//! until the next `highlight`. +//! - `patch(text, len)`: the diff `text` stacked as pardes's patch view +//! shows it (`formatPatch`, with no file and no colours): a `Header`, its +//! rows' text, a style byte for each byte of it, and a `Record` a row. +//! The module's until the next `patch`. +//! +//! Each returns null (0) when its memory runs out. +const std = @import("std"); +const ps = @import("pardes-syntax"); +const options = @import("wasm_options"); + +const gpa = std.heap.wasm_allocator; + +/// A panic's message to stderr (fd 2) in one `fd_write`, then a trap: std's +/// own handler writes through std.Io, which imports 26 more of WASI's +/// functions a page would have to stub. +pub const panic = std.debug.FullPanic(struct { + fn write(message: []const u8, _: ?usize) noreturn { + var written: usize = 0; + _ = std.os.wasi.fd_write(2, &.{.{ .base = message.ptr, .len = message.len }}, 1, &written); + @trap(); + } +}.write); + +comptime { + // A module with no language has no highlighter, and so no tree-sitter. + if (options.highlight) @export(&highlight, .{ .name = "highlight" }); +} + +export fn reserve(len: usize) ?[*]u8 { + return (gpa.alloc(u8, @max(len, 1)) catch return null).ptr; +} + +export fn release(ptr: [*]u8, len: usize) void { + gpa.free(ptr[0..@max(len, 1)]); +} + +var started = false; +var styles: []u8 = &.{}; + +fn highlight(path: [*]const u8, path_len: usize, text: [*]const u8, len: usize) callconv(.c) ?[*]u8 { + if (!started) ps.start(gpa); + started = true; + gpa.free(styles); + styles = ps.highlightFileRange(gpa, path[0..path_len], text[0..len], 0, len) catch &.{}; + // A path in no language this module has, or an empty text: all plain. + if (styles.len == 0) { + styles = gpa.alloc(u8, @max(len, 1)) catch return null; + @memset(styles, 0); + } + return styles.ptr; +} + +/// What `patch` returns, 20 bytes. +pub const Header = extern struct { + /// The rows' text, a line a row, `text_len` bytes; the rows' paths + /// follow it in the same buffer. + text: u32, + text_len: u32, + /// A style byte for each byte of the text. + styles: u32, + /// `row_count` records of 36 bytes. + rows: u32, + row_count: u32, +}; + +/// A row, 36 bytes: nine u32. +pub const Record = extern struct { + /// 0 other, 1 meta, 2 hunk, 3 declaration, 4 context, 5 added, 6 removed + /// (`PatchRow.Kind`). + kind: u32, + /// Its numbers in the old and the new file, from 1; 0 where it has none. + old: u32, + new: u32, + /// Where its code starts, a byte of its text: after a label's + /// `path:line` and stars. + code_start: u32, + /// Its text, without its newline, and its section's path: offsets into + /// the header's `text`. + text_offset: u32, + text_len: u32, + path_offset: u32, + path_len: u32, + /// Bit 0: the diff has `\ No newline at end of file` after it. + flags: u32, +}; + +var arena_state: std.heap.ArenaAllocator = .init(gpa); + +export fn patch(text: [*]const u8, len: usize) ?*const Header { + _ = arena_state.reset(.retain_capacity); + return stack(arena_state.allocator(), text[0..len]) catch null; +} + +fn stack(arena: std.mem.Allocator, diff: []const u8) !*const Header { + const p = try ps.formatPatch(&.{ .arena = arena }, "", diff); + // The text, then each row's path where it changes from the row before. + var text: std.ArrayList(u8) = .empty; + try text.appendSlice(arena, p.text); + const marks = try arena.alloc(u8, p.text.len); + @memset(marks, 0); + const records = try arena.alloc(Record, p.rows.len); + var at: usize = 0; + var path: struct { []const u8, usize } = .{ "", 0 }; + for (p.rows, records) |row, *r| { + const line_len = std.mem.indexOfScalarPos(u8, p.text, at, '\n').? - at; + @memcpy(marks[at..][0..row.styles.len], row.styles); + if (!std.mem.eql(u8, row.path, path[0])) { + path = .{ row.path, text.items.len }; + try text.appendSlice(arena, row.path); + } + r.* = .{ + .kind = @backingInt(row.kind), + .old = @intCast(row.old orelse 0), + .new = @intCast(row.new orelse 0), + .code_start = @intCast(row.code_start), + .text_offset = @intCast(at), + .text_len = @intCast(line_len), + .path_offset = @intCast(path[1]), + .path_len = @intCast(row.path.len), + .flags = @intFromBool(row.no_newline), + }; + at += line_len + 1; + } + const header = try arena.create(Header); + header.* = .{ + .text = @intCast(@intFromPtr(text.items.ptr)), + .text_len = @intCast(p.text.len), + .styles = @intCast(@intFromPtr(marks.ptr)), + .rows = @intCast(@intFromPtr(records.ptr)), + .row_count = @intCast(records.len), + }; + return header; +} |
