//! 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` or `patchFile`. //! - `patchFile(text, len, file, file_len)` (a module with a language): //! `patch` with the text of the file the diff's first section names (its //! new text or its old), so a hunk gets the declarations it sits in from //! the file's own parse; the diff's other files stack as with none. //! //! 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 no parse of a file, // and so no tree-sitter. if (options.language) { @export(&highlight, .{ .name = "highlight" }); @export(&patchFile, .{ .name = "patchFile" }); } } 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 start() void { if (!started) ps.start(gpa); started = true; } fn highlight(path: [*]const u8, path_len: usize, text: [*]const u8, len: usize) callconv(.c) ?[*]u8 { start(); 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 line end (`\n`, and a `\r` before it), 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 = arena_state.allocator() }, text[0..len]) catch null; } fn patchFile(text: [*]const u8, len: usize, file: [*]const u8, file_len: usize) callconv(.c) ?*const Header { start(); _ = arena_state.reset(.retain_capacity); var given: Given = .{ .bytes = file[0..file_len] }; return stack(&.{ .arena = arena_state.allocator(), .source = Given.source, .declarations = ps.PatchCtx.parsed, .host = &given, }, text[0..len]) catch null; } /// The one file `patchFile` is handed: the first section's. A text the /// hunk is not in is no source (`formatPatch` looks for the hunk's lines). const Given = struct { bytes: []const u8, path: ?[]const u8 = null, fn source(ctx: *const ps.PatchCtx, _: []const u8, path: []const u8) ?ps.PatchSource { const given: *Given = @ptrCast(@alignCast(ctx.host)); const first = given.path orelse path; given.path = first; return if (std.mem.eql(u8, path, first)) .{ .path = path, .bytes = given.bytes } else null; } }; fn stack(ctx: *const ps.PatchCtx, diff: []const u8) !*const Header { const arena = ctx.arena; const p = try ps.formatPatch(ctx, "", 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; }