Skip to content

Commit 9cb37dc

Browse files
committed
Attempt to add more context to debug logs.
1 - On `unkown global property`, include the stack trace (this might be WAY too verbose) 2 - On script get, include stack trace (when available) 3 - On script get, include referrer 4 - Stack traces will now include the script name/src when available
1 parent 207f065 commit 9cb37dc

File tree

7 files changed

+41
-49
lines changed

7 files changed

+41
-49
lines changed

build.zig.zon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
.fingerprint = 0xda130f3af836cea0,
66
.dependencies = .{
77
.v8 = .{
8-
.url = "https://github.com/lightpanda-io/zig-v8-fork/archive/7a1beb016efcb2bd66c0b46b5fc6bcd04fa72db8.tar.gz",
9-
.hash = "v8-0.0.0-xddH6_PFAwAqwLMWbF7S0rAZzRbN8zmf2GhLH_ThdMSR",
8+
.url = "https://github.com/lightpanda-io/zig-v8-fork/archive/5ce9ade6c6d7f7ab9ab4582a6b1b36fdc8e246e2.tar.gz",
9+
.hash = "v8-0.0.0-xddH6yTGAwA__kfiDozsVXL2_jnycrtDUfR8kIADQFUz",
1010
},
1111
//.v8 = .{ .path = "../zig-v8-fork" }
1212
},

src/browser/ScriptManager.zig

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ fn clearList(_: *const ScriptManager, list: *OrderList) void {
142142
std.debug.assert(list.first == null);
143143
}
144144

145-
pub fn addFromElement(self: *ScriptManager, element: *parser.Element) !void {
145+
pub fn addFromElement(self: *ScriptManager, element: *parser.Element, comptime ctx: []const u8) !void {
146146
if (try parser.elementGetAttribute(element, "nomodule") != null) {
147147
// these scripts should only be loaded if we don't support modules
148148
// but since we do support modules, we can just skip them.
@@ -230,7 +230,11 @@ pub fn addFromElement(self: *ScriptManager, element: *parser.Element) !void {
230230
self.scripts.append(&pending_script.node);
231231
return;
232232
} else {
233-
log.debug(.http, "script queue", .{ .url = remote_url.? });
233+
log.debug(.http, "script queue", .{
234+
.ctx = ctx,
235+
.url = remote_url.?,
236+
.stack = page.js.stackTrace() catch "???",
237+
});
234238
}
235239

236240
pending_script.getList().append(&pending_script.node);
@@ -255,40 +259,7 @@ pub fn addFromElement(self: *ScriptManager, element: *parser.Element) !void {
255259
});
256260
}
257261

258-
// @TODO: Improving this would have the simplest biggest performance improvement
259-
// for most sites.
260-
//
261-
// For JS imports (both static and dynamic), we currently block to get the
262-
// result (the content of the file).
263-
//
264-
// For static imports, this is necessary, since v8 is expecting the compiled module
265-
// as part of the function return. (we should try to pre-load the JavaScript
266-
// source via module.GetModuleRequests(), but that's for a later time).
267-
//
268-
// For dynamic dynamic imports, this is not strictly necessary since the v8
269-
// call returns a Promise; we could make this a normal get call, associated with
270-
// the promise, and when done, resolve the promise.
271-
//
272-
// In both cases, for now at least, we just issue a "blocking" request. We block
273-
// by ticking the http client until the script is complete.
274-
//
275-
// This uses the client.blockingRequest call which has a dedicated handle for
276-
// these blocking requests. Because they are blocking, we're guaranteed to have
277-
// only 1 at a time, thus the 1 reserved handle.
278-
//
279-
// You almost don't need the http client's blocking handle. In most cases, you
280-
// should always have 1 free handle whenever you get here, because we always
281-
// release the handle before executing the doneCallback. So, if a module does:
282-
// import * as x from 'blah'
283-
// And we need to load 'blah', there should always be 1 free handle - the handle
284-
// of the http GET we just completed before executing the module.
285-
// The exception to this, and the reason we need a special blocking handle, is
286-
// for inline modules within the HTML page itself:
287-
// <script type=module>import ....</script>
288-
// Unlike external modules which can only ever be executed after releasing an
289-
// http handle, these are executed without there necessarily being a free handle.
290-
// Thus, Http/Client.zig maintains a dedicated handle for these calls.
291-
pub fn getModule(self: *ScriptManager, url: [:0]const u8) !void {
262+
pub fn getModule(self: *ScriptManager, url: [:0]const u8, referrer: []const u8) !void {
292263
const gop = try self.sync_modules.getOrPut(self.allocator, url);
293264
if (gop.found_existing) {
294265
// already requested
@@ -305,6 +276,13 @@ pub fn getModule(self: *ScriptManager, url: [:0]const u8) !void {
305276
var headers = try self.client.newHeaders();
306277
try self.page.requestCookie(.{}).headersForRequest(self.page.arena, url, &headers);
307278

279+
log.debug(.http, "script queue", .{
280+
.url = url,
281+
.ctx = "module",
282+
.referrer = referrer,
283+
.stack = self.page.js.stackTrace() catch "???",
284+
});
285+
308286
try self.client.request(.{
309287
.url = url,
310288
.ctx = sync,
@@ -355,7 +333,7 @@ pub fn waitForModule(self: *ScriptManager, url: [:0]const u8) !GetResult {
355333
}
356334
}
357335

358-
pub fn getAsyncModule(self: *ScriptManager, url: [:0]const u8, cb: AsyncModule.Callback, cb_data: *anyopaque) !void {
336+
pub fn getAsyncModule(self: *ScriptManager, url: [:0]const u8, cb: AsyncModule.Callback, cb_data: *anyopaque, referrer: []const u8) !void {
359337
const async = try self.async_module_pool.create();
360338
errdefer self.async_module_pool.destroy(async);
361339

@@ -368,6 +346,13 @@ pub fn getAsyncModule(self: *ScriptManager, url: [:0]const u8, cb: AsyncModule.C
368346
var headers = try self.client.newHeaders();
369347
try self.page.requestCookie(.{}).headersForRequest(self.page.arena, url, &headers);
370348

349+
log.debug(.http, "script queue", .{
350+
.url = url,
351+
.ctx = "dynamic module",
352+
.referrer = referrer,
353+
.stack = self.page.js.stackTrace() catch "???",
354+
});
355+
371356
try self.client.request(.{
372357
.url = url,
373358
.method = .GET,

src/browser/html/elements.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ pub const HTMLScriptElement = struct {
887887
// s.src = '...';
888888
// This should load the script.
889889
// addFromElement protects against double execution.
890-
try page.script_manager.addFromElement(@ptrCast(@alignCast(self)));
890+
try page.script_manager.addFromElement(@ptrCast(@alignCast(self)), "dynamic");
891891
}
892892
}
893893

src/browser/js/Context.zig

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ pub fn module(self: *Context, comptime want_result: bool, src: []const u8, url:
263263
const owned_specifier = try self.context_arena.dupeZ(u8, normalized_specifier);
264264
gop.key_ptr.* = owned_specifier;
265265
gop.value_ptr.* = .{};
266-
try self.script_manager.?.getModule(owned_specifier);
266+
try self.script_manager.?.getModule(owned_specifier, src);
267267
}
268268
}
269269
}
@@ -996,7 +996,10 @@ fn debugValueToString(self: *const Context, js_obj: v8.Object) ![]u8 {
996996
}
997997

998998
pub fn stackTrace(self: *const Context) !?[]const u8 {
999-
std.debug.assert(@import("builtin").mode == .Debug);
999+
if (comptime @import("builtin").mode != .Debug) {
1000+
return "not available";
1001+
}
1002+
10001003
const isolate = self.isolate;
10011004
const separator = log.separator();
10021005

@@ -1006,6 +1009,10 @@ pub fn stackTrace(self: *const Context) !?[]const u8 {
10061009
const stack_trace = v8.StackTrace.getCurrentStackTrace(isolate, 30);
10071010
const frame_count = stack_trace.getFrameCount();
10081011

1012+
if (v8.StackTrace.getCurrentScriptNameOrSourceUrl(isolate)) |script| {
1013+
try writer.print("{s}<{s}>", .{ separator, try self.jsStringToZig(script, .{}) });
1014+
}
1015+
10091016
for (0..frame_count) |i| {
10101017
const frame = stack_trace.getFrame(isolate, @intCast(i));
10111018
if (frame.getScriptName()) |name| {
@@ -1131,7 +1138,7 @@ pub fn dynamicModuleCallback(
11311138
return @constCast(self.rejectPromise("Out of memory").handle);
11321139
};
11331140

1134-
const promise = self._dynamicModuleCallback(normalized_specifier) catch |err| blk: {
1141+
const promise = self._dynamicModuleCallback(normalized_specifier, resource) catch |err| blk: {
11351142
log.err(.js, "dynamic module callback", .{
11361143
.err = err,
11371144
});
@@ -1191,7 +1198,7 @@ fn _resolveModuleCallback(self: *Context, referrer: v8.Module, specifier: []cons
11911198
// harm in handling this case.
11921199
@branchHint(.unlikely);
11931200
gop.value_ptr.* = .{};
1194-
try self.script_manager.?.getModule(normalized_specifier);
1201+
try self.script_manager.?.getModule(normalized_specifier, referrer_path);
11951202
}
11961203

11971204
var fetch_result = try self.script_manager.?.waitForModule(normalized_specifier);
@@ -1227,7 +1234,7 @@ const DynamicModuleResolveState = struct {
12271234
resolver: v8.Persistent(v8.PromiseResolver),
12281235
};
12291236

1230-
fn _dynamicModuleCallback(self: *Context, specifier: [:0]const u8) !v8.Promise {
1237+
fn _dynamicModuleCallback(self: *Context, specifier: [:0]const u8, referrer: []const u8) !v8.Promise {
12311238
const isolate = self.isolate;
12321239
const gop = try self.module_cache.getOrPut(self.context_arena, specifier);
12331240
if (gop.found_existing and gop.value_ptr.resolver_promise != null) {
@@ -1267,7 +1274,7 @@ fn _dynamicModuleCallback(self: *Context, specifier: [:0]const u8) !v8.Promise {
12671274
};
12681275

12691276
// Next, we need to actually load it.
1270-
self.script_manager.?.getAsyncModule(specifier, dynamicModuleSourceCallback, state) catch |err| {
1277+
self.script_manager.?.getAsyncModule(specifier, dynamicModuleSourceCallback, state, referrer) catch |err| {
12711278
const error_msg = v8.String.initUtf8(isolate, @errorName(err));
12721279
_ = resolver.reject(self.v8_context, error_msg.toValue());
12731280
};

src/browser/page.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ pub const Page = struct {
785785
// ignore non-js script.
786786
continue;
787787
}
788-
try self.script_manager.addFromElement(@ptrCast(node));
788+
try self.script_manager.addFromElement(@ptrCast(node), "page");
789789
}
790790

791791
self.script_manager.staticScriptsDone();
@@ -1250,7 +1250,7 @@ pub export fn scriptAddedCallback(ctx: ?*anyopaque, element: ?*parser.Element) c
12501250
// here, else the script_manager will flag it as already-processed.
12511251
_ = parser.elementGetAttribute(element.?, "src") catch return orelse return;
12521252

1253-
self.script_manager.addFromElement(element.?) catch |err| {
1253+
self.script_manager.addFromElement(element.?, "dynamic") catch |err| {
12541254
log.warn(.browser, "dynamic script", .{ .err = err });
12551255
};
12561256
}

src/browser/polyfill/polyfill.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ pub const Loader = struct {
6969
if (comptime builtin.mode == .Debug) {
7070
log.debug(.unknown_prop, "unkown global property", .{
7171
.info = "but the property can exist in pure JS",
72+
.stack = js_context.stackTrace() catch "???",
7273
.property = name,
7374
});
7475
}

src/http/Http.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,6 @@ const LineWriter = struct {
443443
}
444444
};
445445

446-
447446
pub fn debugCallback(_: *c.CURL, msg_type: c.curl_infotype, raw: [*c]u8, len: usize, _: *anyopaque) callconv(.c) void {
448447
const data = raw[0..len];
449448
switch (msg_type) {

0 commit comments

Comments
 (0)