1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
//! 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;
}
|