authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-08-05 14:10:43-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-08-07 00:48:32-07:00
logef4c2193fc4b048da2afd3861bedb50451a31358
tree2ff2e98e4e5a4621387c14825543d547731524a7
parentdb69641061ae300baabd6107aa22da60e78f0f38

fuzzer web UI: navigate by source location index

This will help scroll the point of interest into view

2 files changed, 89 insertions(+), 50 deletions(-)

lib/fuzzer/main.js+21-31
......@@ -68,6 +68,8 @@
6868 }
6969
7070 function navigate(location_hash) {
71 domSectSource.classList.add("hidden");
72
7173 curNavLocation = null;
7274 curNavSearch = null;
7375
......@@ -82,23 +84,19 @@
8284 curNavSearch = decodeURIComponent(query.substring(qpos + 1));
8385 }
8486
85 if (nonSearchPart.length > 0) {
86 curNavLocation = nonSearchPart;
87 if (nonSearchPart[0] == "l") {
88 curNavLocation = +nonSearchPart.substring(1);
89 renderSource(curNavLocation);
8790 }
8891 }
8992
9093 render();
91
92 if (curNavLocation != null) {
93 // TODO
94 // scrollToSourceLocation(findSourceLocationIndex(curNavLocation));
95 }
9694 }
9795
9896 function connectWebSocket() {
99 const host = window.document.location.host;
100 const pathname = window.document.location.pathname;
101 const isHttps = window.document.location.protocol === 'https:';
97 const host = document.location.host;
98 const pathname = document.location.pathname;
99 const isHttps = document.location.protocol === 'https:';
102100 const match = host.match(/^(.+):(\d+)$/);
103101 const defaultPort = isHttps ? 443 : 80;
104102 const port = match ? parseInt(match[2], 10) : defaultPort;
......@@ -139,17 +137,12 @@
139137 }
140138
141139 function onSourceIndexChange() {
142 console.log("source location index metadata updated");
143140 render();
141 if (curNavLocation != null) renderSource(curNavLocation);
144142 }
145143
146144 function render() {
147145 domStatus.classList.add("hidden");
148 domSectSource.classList.add("hidden");
149
150 if (curNavLocation != null) {
151 renderSource(curNavLocation.split(":")[0]);
152 }
153146 }
154147
155148 function renderStats() {
......@@ -186,22 +179,23 @@
186179 return ((Number(a) / Number(b)) * 100).toFixed(1);
187180 }
188181
189 function renderSource(path) {
190 const decl_index = findFileRoot(path);
191 if (decl_index == null) throw new Error("file not found: " + path);
182 function renderSource(sourceLocationIndex) {
183 const pathName = unwrapString(wasm_exports.sourceLocationPath(sourceLocationIndex));
184 if (pathName.length === 0) return;
192185
193186 const h2 = domSectSource.children[0];
194 h2.innerText = path;
195 domSourceText.innerHTML = declSourceHtml(decl_index);
187 h2.innerText = pathName;
188 domSourceText.innerHTML = unwrapString(wasm_exports.sourceLocationFileHtml(sourceLocationIndex));
196189
197190 domSectSource.classList.remove("hidden");
198 }
199191
200 function findFileRoot(path) {
201 setInputString(path);
202 const result = wasm_exports.find_file_root();
203 if (result === -1) return null;
204 return result;
192 const slDom = document.getElementById("l" + sourceLocationIndex);
193 if (slDom != null) {
194 slDom.scrollIntoView({
195 behavior: "smooth",
196 block: "center",
197 });
198 }
205199 }
206200
207201 function decodeString(ptr, len) {
......@@ -224,10 +218,6 @@
224218 wasmArray.set(jsArray);
225219 }
226220
227 function declSourceHtml(decl_index) {
228 return unwrapString(wasm_exports.decl_source_html(decl_index));
229 }
230
231221 function unwrapString(bigint) {
232222 const ptr = Number(bigint & 0xffffffffn);
233223 const len = Number(bigint >> 32n);
lib/fuzzer/wasm/main.zig+68-19
......@@ -235,7 +235,59 @@ export fn entryPoints() Slice(u32) {
235235 return Slice(u32).init(entry_points.items);
236236}
237237
238/// Index into `coverage_source_locations`.
239const SourceLocationIndex = enum(u32) {
240 _,
241
242 fn haveCoverage(sli: SourceLocationIndex) bool {
243 return @intFromEnum(sli) < coverage_source_locations.items.len;
244 }
245
246 fn ptr(sli: SourceLocationIndex) *Coverage.SourceLocation {
247 return &coverage_source_locations.items[@intFromEnum(sli)];
248 }
249
250 fn sourceLocationLinkHtml(
251 sli: SourceLocationIndex,
252 out: *std.ArrayListUnmanaged(u8),
253 ) Allocator.Error!void {
254 const sl = sli.ptr();
255 try out.writer(gpa).print("<a href=\"#l{d}\">", .{@intFromEnum(sli)});
256 try sli.appendPath(out);
257 try out.writer(gpa).print(":{d}:{d}</a>", .{ sl.line, sl.column });
258 }
259
260 fn appendPath(sli: SourceLocationIndex, out: *std.ArrayListUnmanaged(u8)) Allocator.Error!void {
261 const sl = sli.ptr();
262 const file = coverage.fileAt(sl.file);
263 const file_name = coverage.stringAt(file.basename);
264 const dir_name = coverage.stringAt(coverage.directories.keys()[file.directory_index]);
265 try html_render.appendEscaped(out, dir_name);
266 try out.appendSlice(gpa, "/");
267 try html_render.appendEscaped(out, file_name);
268 }
269
270 fn toWalkFile(sli: SourceLocationIndex) ?Walk.File.Index {
271 var buf: std.ArrayListUnmanaged(u8) = .{};
272 defer buf.deinit(gpa);
273 sli.appendPath(&buf) catch @panic("OOM");
274 return @enumFromInt(Walk.files.getIndex(buf.items) orelse return null);
275 }
276
277 fn fileHtml(
278 sli: SourceLocationIndex,
279 out: *std.ArrayListUnmanaged(u8),
280 ) error{ OutOfMemory, SourceUnavailable }!void {
281 const walk_file_index = sli.toWalkFile() orelse return error.SourceUnavailable;
282 const root_node = walk_file_index.findRootDecl().get().ast_node;
283 html_render.fileSourceHtml(walk_file_index, out, root_node, .{}) catch |err| {
284 fatal("unable to render source: {s}", .{@errorName(err)});
285 };
286 }
287};
288
238289var coverage = Coverage.init;
290/// Index of type `SourceLocationIndex`.
239291var coverage_source_locations: std.ArrayListUnmanaged(Coverage.SourceLocation) = .{};
240292/// Contains the most recent coverage update message, unmodified.
241293var recent_coverage_update: std.ArrayListUnmanaged(u8) = .{};
......@@ -263,27 +315,24 @@ fn updateCoverage(
263315 try coverage.directories.reIndexContext(gpa, .{ .string_bytes = coverage.string_bytes.items });
264316}
265317
266export fn sourceLocationLinkHtml(index: u32) String {
318export fn sourceLocationLinkHtml(index: SourceLocationIndex) String {
267319 string_result.clearRetainingCapacity();
268 sourceLocationLinkHtmlFallible(index, &string_result) catch @panic("OOM");
320 index.sourceLocationLinkHtml(&string_result) catch @panic("OOM");
269321 return String.init(string_result.items);
270322}
271323
272fn sourceLocationLinkHtmlFallible(index: u32, out: *std.ArrayListUnmanaged(u8)) Allocator.Error!void {
273 const sl = coverage_source_locations.items[index];
274 const file = coverage.fileAt(sl.file);
275 const file_name = coverage.stringAt(file.basename);
276 const dir_name = coverage.stringAt(coverage.directories.keys()[file.directory_index]);
277
278 out.clearRetainingCapacity();
279 try out.appendSlice(gpa, "<a href=\"#");
280 _ = html_render.missing_feature_url_escape;
281 try out.writer(gpa).print("{s}/{s}:{d}:{d}", .{
282 dir_name, file_name, sl.line, sl.column,
283 });
284 try out.appendSlice(gpa, "\">");
285 try html_render.appendEscaped(out, dir_name);
286 try out.appendSlice(gpa, "/");
287 try html_render.appendEscaped(out, file_name);
288 try out.writer(gpa).print(":{d}:{d}</a>", .{ sl.line, sl.column });
324/// Returns empty string if coverage metadata is not available for this source location.
325export fn sourceLocationPath(sli: SourceLocationIndex) String {
326 string_result.clearRetainingCapacity();
327 if (sli.haveCoverage()) sli.appendPath(&string_result) catch @panic("OOM");
328 return String.init(string_result.items);
329}
330
331export fn sourceLocationFileHtml(sli: SourceLocationIndex) String {
332 string_result.clearRetainingCapacity();
333 sli.fileHtml(&string_result) catch |err| switch (err) {
334 error.OutOfMemory => @panic("OOM"),
335 error.SourceUnavailable => {},
336 };
337 return String.init(string_result.items);
289338}