Lightpanda: the headless browser designed for AI and automation
improve js.writeStackTrace to add script name
Pierre Tachoiree3312a3a5b83
diff --git a/src/browser/js/Context.zig b/src/browser/js/Context.zigindex 678389e7c..6609b4413 100644--- a/src/browser/js/Context.zig+++ b/src/browser/js/Context.zig@@ -153,25 +153,19 @@ fn fromC(c_context: *const v8.Context) ?*Context {return @ptrCast(@alignCast(v8.v8__Context__GetAlignedPointerFromEmbedderData(c_context, 1)));}-/// The entered Context, or null when none is entered (e.g. a V8 interrupt-/// serviced outside script execution) or its Context struct is gone.-pub fn current(isolate: *v8.Isolate) ?*Context {- const v8_context = v8.v8__Isolate__GetCurrentContext(isolate) orelse return null;- return fromC(v8_context);-}-/// Returns the Context and v8::Context for the given isolate./// If the current context is from a destroyed Context (e.g., navigated-away iframe),/// falls back to the incumbent context (the calling context)./// Returns null if neither context has a valid Context struct (both were destroyed).pub fn fromIsolate(isolate: js.Isolate) ?struct { *Context, *const v8.Context } {- const v8_context = v8.v8__Isolate__GetCurrentContext(isolate.handle).?;+ const v8_context = v8.v8__Isolate__GetCurrentContext(isolate.handle) orelse return null;if (fromC(v8_context)) |ctx| {return .{ ctx, v8_context };}+// The current context's Context struct has been freed (e.g., iframe navigated away).// Fall back to the incumbent context (the calling context).- const v8_incumbent = v8.v8__Isolate__GetIncumbentContext(isolate.handle).?;+ const v8_incumbent = v8.v8__Isolate__GetIncumbentContext(isolate.handle) orelse return null;const ctx = fromC(v8_incumbent) orelse return null;return .{ ctx, v8_incumbent };}diff --git a/src/browser/js/Env.zig b/src/browser/js/Env.zigindex 34629f646..5cff3cf6c 100644--- a/src/browser/js/Env.zig+++ b/src/browser/js/Env.zig@@ -28,6 +28,7 @@ const Platform = @import("Platform.zig");const Inspector = @import("Inspector.zig");const App = @import("../../App.zig");+const string = @import("../../string.zig");const Frame = @import("../Frame.zig");const Window = @import("../webapi/Window.zig");@@ -738,9 +739,10 @@ fn stallReport(self: *Env, stack_buf: []u8) StallReport {defer v8.v8__HandleScope__DESTRUCT(&hs);var report: StallReport = .{};- if (Context.current(isolate)) |ctx| {- report.url = truncate(ctx.global.url(), STALL_URL_MAX);- report.page_url = truncate(switch (ctx.global) {+ if (Context.fromIsolate(self.isolate)) |entry| {+ const ctx, _ = entry;+ report.url = string.truncateUtf8(ctx.global.url(), STALL_URL_MAX);+ report.page_url = string.truncateUtf8(switch (ctx.global) {.frame => |frame| frame.page.frame.url,.worker => |worker| worker.page.frame.url,}, STALL_URL_MAX);@@ -749,7 +751,7 @@ fn stallReport(self: *Env, stack_buf: []u8) StallReport {var writer: std.Io.Writer = .fixed(stack_buf);if (v8.v8__StackTrace__CurrentStackTrace__STATIC(isolate, STALL_STACK_FRAMES)) |stack| {// A full buffer truncates the stack; the frames written are kept.- writeStallStack(isolate, stack, &writer) catch {};+ js.writeStackTrace(isolate, stack, &writer) catch {};}report.stack = writer.buffered();return report;@@ -768,37 +770,6 @@ fn logStall(self: *Env, requested_at: u64) void {});}-fn writeStallStack(isolate: *v8.Isolate, stack: *const v8.StackTrace, writer: *std.Io.Writer) !void {- const separator = log.separator();- const frame_count: usize = @intCast(v8.v8__StackTrace__GetFrameCount(stack));- for (0..frame_count) |i| {- const frame = v8.v8__StackTrace__GetFrame(stack, isolate, @intCast(i)).?;-- var name_buf: [128]u8 = undefined;- const name = utf8(isolate, v8.v8__StackFrame__GetFunctionName(frame), &name_buf);- var script_buf: [256]u8 = undefined;- const script = utf8(isolate, v8.v8__StackFrame__GetScriptNameOrSourceURL(frame), &script_buf);-- try writer.print("{s}{s} ({s}:{d}:{d})", .{- separator,- if (name.len == 0) "<anonymous>" else name,- if (script.len == 0) "<unknown>" else script,- v8.v8__StackFrame__GetLineNumber(frame),- v8.v8__StackFrame__GetColumn(frame),- });- }-}--fn utf8(isolate: *v8.Isolate, str: ?*const v8.String, buf: []u8) []const u8 {- const s = str orelse return "";- const n = v8.v8__String__WriteUtf8(s, isolate, buf.ptr, buf.len, v8.WRITE_REPLACE_INVALID_UTF8, null);- return buf[0..n];-}--fn truncate(s: []const u8, max: usize) []const u8 {- return s[0..@min(s.len, max)];-}-/// Clears a pending termination so V8 calls (e.g. those made during cleanup)/// don't keep tripping over the terminating-state asserts. Safe to call/// unconditionally; a no-op if termination wasn't pending. Also clears thediff --git a/src/browser/js/js.zig b/src/browser/js/js.zigindex 5010598d6..1d249c587 100644--- a/src/browser/js/js.zig+++ b/src/browser/js/js.zig@@ -596,12 +596,26 @@ pub fn writeStackTrace(isolate: *v8.Isolate, stack_handle: *const v8.StackTrace,for (0..@intCast(frame_count)) |i| {const frame_handle = v8.v8__StackTrace__GetFrame(stack_handle, isolate, @intCast(i)).?;- if (v8.v8__StackFrame__GetFunctionName(frame_handle)) |name| {- var buf: [1024]u8 = undefined;- const n = v8.v8__String__WriteUtf8(name, isolate, &buf, buf.len, v8.WRITE_REPLACE_INVALID_UTF8, null);- try writer.print("{s}{s}:{d}", .{ separator, buf[0..n], v8.v8__StackFrame__GetLineNumber(frame_handle) });- } else {- try writer.print("{s}<anonymous>:{d}", .{ separator, v8.v8__StackFrame__GetLineNumber(frame_handle) });++ var name_buf: [512]u8 = undefined;+ var name: []const u8 = "";+ if (v8.v8__StackFrame__GetFunctionName(frame_handle)) |str| {+ const n = v8.v8__String__WriteUtf8(str, isolate, &name_buf, name_buf.len, v8.WRITE_REPLACE_INVALID_UTF8, null);+ name = name_buf[0..n];+ }+ var script_buf: [512]u8 = undefined;+ var script: []const u8 = "";+ if (v8.v8__StackFrame__GetScriptNameOrSourceURL(frame_handle)) |str| {+ const n = v8.v8__String__WriteUtf8(str, isolate, &script_buf, script_buf.len, v8.WRITE_REPLACE_INVALID_UTF8, null);+ script = script_buf[0..n];}++ try writer.print("{s}{s} ({s}:{d}:{d})", .{+ separator,+ if (name.len == 0) "<anonymous>" else name,+ if (script.len == 0) "<unknown>" else script,+ v8.v8__StackFrame__GetLineNumber(frame_handle),+ v8.v8__StackFrame__GetColumn(frame_handle),+ });}}