diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-21 05:29:51 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-21 05:29:51 -0300 |
| commit | 63538d28cee238ff53c731f965c22e615d43bec1 (patch) | |
| tree | fb00203f05512a9f14d0e8089a2f4b1919edffc9 /abi | |
| parent | 638122f18a66669c433e332c91fc8b068019681e (diff) | |
| parent | d04f4acfb9af968caaa53d6cf10a486622c95400 (diff) | |
| download | pardes-63538d28cee238ff53c731f965c22e615d43bec1.tar.gz pardes-63538d28cee238ff53c731f965c22e615d43bec1.zip | |
integration: the library for a browser reader on the gathered line: pardes-syntax-lib (`wqlkznns`: a dependent package names its languages, the patch view without the editor as formatPatch, build.zig's wasmModule and abi/wasm.zig, the manual) and pardes-reorg (`ukkqwpqu`: the terminals' line, the wire at 26); no conflict; its gate: .scratch/sketch/reorg/merge47, the numbers in PARDES.md (2026-11-21)
Co-Authored-By: Claude Fable 5.2 <[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; +} |
