Skip to content

Commit 1839b34

Browse files
Merge pull request #792 from lightpanda-io/fix_current_script_scope
Fixes the scoping of page.current_script
2 parents c1ffe7f + d056199 commit 1839b34

File tree

2 files changed

+65
-11
lines changed

2 files changed

+65
-11
lines changed

src/browser/page.zig

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,29 @@ pub const Page = struct {
151151
const self: *Page = @ptrCast(@alignCast(ctx));
152152
const base = if (self.current_script) |s| s.src else null;
153153

154-
const file_src = blk: {
154+
const src = blk: {
155155
if (base) |_base| {
156156
break :blk try URL.stitch(self.arena, specifier, _base, .{});
157157
} else break :blk specifier;
158158
};
159159

160-
if (self.module_map.get(file_src)) |module| return module;
160+
if (self.module_map.get(src)) |module| {
161+
log.debug(.http, "fetching module", .{
162+
.src = src,
163+
.cached = true,
164+
});
165+
return module;
166+
}
167+
168+
log.debug(.http, "fetching module", .{
169+
.src = src,
170+
.base = base,
171+
.cached = false,
172+
.specifier = specifier,
173+
});
161174

162175
const module = try self.fetchData(specifier, base);
163-
if (module) |_module| try self.module_map.putNoClobber(self.arena, file_src, _module);
176+
if (module) |_module| try self.module_map.putNoClobber(self.arena, src, _module);
164177
return module;
165178
}
166179

@@ -461,9 +474,9 @@ pub const Page = struct {
461474
};
462475

463476
var script_source: ?[]const u8 = null;
477+
defer self.current_script = null;
464478
if (script.src) |src| {
465479
self.current_script = script;
466-
defer self.current_script = null;
467480

468481
// https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-classic-script
469482
script_source = (try self.fetchData(src, null)) orelse {
@@ -507,8 +520,18 @@ pub const Page = struct {
507520
var origin_url = &self.url;
508521
const url = try origin_url.resolve(arena, res_src);
509522

510-
log.debug(.http, "fetching script", .{ .url = url });
511-
errdefer |err| log.err(.http, "fetch error", .{ .err = err, .url = url });
523+
var status_code: u16 = 0;
524+
log.debug(.http, "fetching script", .{
525+
.url = url,
526+
.src = src,
527+
.base = base,
528+
});
529+
530+
errdefer |err| log.err(.http, "fetch error", .{
531+
.err = err,
532+
.url = url,
533+
.status = status_code,
534+
});
512535

513536
var request = try self.newHTTPRequest(.GET, &url, .{
514537
.origin_uri = &origin_url.uri,
@@ -521,7 +544,8 @@ pub const Page = struct {
521544
var header = response.header;
522545
try self.session.cookie_jar.populateFromResponse(&url.uri, &header);
523546

524-
if (header.status < 200 or header.status > 299) {
547+
status_code = header.status;
548+
if (status_code < 200 or status_code > 299) {
525549
return error.BadStatusCode;
526550
}
527551

@@ -539,7 +563,7 @@ pub const Page = struct {
539563

540564
log.info(.http, "fetch complete", .{
541565
.url = url,
542-
.status = header.status,
566+
.status = status_code,
543567
.content_length = arr.items.len,
544568
});
545569
return arr.items;
@@ -1002,6 +1026,9 @@ const Script = struct {
10021026
defer try_catch.deinit();
10031027

10041028
const src = self.src orelse "inline";
1029+
1030+
log.debug(.browser, "executing script", .{ .src = src, .kind = self.kind });
1031+
10051032
_ = switch (self.kind) {
10061033
.javascript => page.main_context.exec(body, src),
10071034
.module => blk: {

src/runtime/js.zig

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
11581158
}
11591159

11601160
if (!js_value.isArray()) {
1161-
return .{.invalid = {}};
1161+
return .{ .invalid = {} };
11621162
}
11631163

11641164
// This can get tricky.
@@ -1227,12 +1227,25 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
12271227
// const referrer_module = if (referrer) |ref| v8.Module{ .handle = ref } else null;
12281228
const module_loader = self.module_loader;
12291229
const source = module_loader.func(module_loader.ptr, specifier) catch |err| {
1230-
log.err(.js, "resolve module fetch error", .{ .specifier = specifier, .err = err });
1230+
log.err(.js, "resolve module fetch", .{
1231+
.err = err,
1232+
.specifier = specifier,
1233+
});
12311234
return null;
12321235
} orelse return null;
12331236

1237+
var try_catch: TryCatch = undefined;
1238+
try_catch.init(self);
1239+
defer try_catch.deinit();
1240+
12341241
const m = compileModule(self.isolate, source, specifier) catch |err| {
1235-
log.err(.js, "resolve module compile error", .{ .specifier = specifier, .err = err });
1242+
log.err(.js, "resolve module compile", .{
1243+
.specifier = specifier,
1244+
.stack = try_catch.stack(self.context_arena) catch null,
1245+
.src = try_catch.sourceLine(self.context_arena) catch "err",
1246+
.line = try_catch.sourceLineNumber() orelse 0,
1247+
.exception = (try_catch.exception(self.context_arena) catch @errorName(err)) orelse @errorName(err),
1248+
});
12361249
return null;
12371250
};
12381251
return m.handle;
@@ -1547,6 +1560,20 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
15471560
return try valueToString(allocator, s, js_context.isolate, js_context.v8_context);
15481561
}
15491562

1563+
// the caller needs to deinit the string returned
1564+
pub fn sourceLine(self: TryCatch, allocator: Allocator) !?[]const u8 {
1565+
const js_context = self.js_context;
1566+
const msg = self.inner.getMessage() orelse return null;
1567+
const sl = msg.getSourceLine(js_context.v8_context) orelse return null;
1568+
return try jsStringToZig(allocator, sl, js_context.isolate);
1569+
}
1570+
1571+
pub fn sourceLineNumber(self: TryCatch) ?u32 {
1572+
const js_context = self.js_context;
1573+
const msg = self.inner.getMessage() orelse return null;
1574+
return msg.getLineNumber(js_context.v8_context);
1575+
}
1576+
15501577
// a shorthand method to return either the entire stack message
15511578
// or just the exception message
15521579
// - in Debug mode return the stack if available

0 commit comments

Comments
 (0)