Move more asserts to custom asserter.

Deciding what should be an lp.assert, vs an std.debug.assert, vs a debug-only
assert is a little arbitrary.

debug-only asserts, guarded with an `if (comptime IS_DEBUG)` obviously avoid the
check in release and thus have a performance advantage. We also use them at
library boundaries. If libcurl says it will always emit a header line with a
trailing \r\n, is that really a check we need to do in production? I don't think
so. First, that code path is checked _a lot_ in debug. Second, it feels a bit
like we're testing libcurl (in production!)..why? A debug-only assertion should
be good enough to catch any changes in libcurl.
This commit is contained in:
Karl Seguin
2026-01-19 09:12:16 +08:00
parent 9b000a002e
commit a6e7ecd9e5
31 changed files with 204 additions and 110 deletions

View File

@@ -17,6 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const lp = @import("lightpanda");
const Allocator = std.mem.Allocator;
const CdpStorage = @import("storage.zig");
@@ -215,7 +216,7 @@ pub fn httpRequestFail(arena: Allocator, bc: anytype, msg: *const Notification.R
// Isn't possible to do a network request within a Browser (which our
// notification is tied to), without a page.
std.debug.assert(bc.session.page != null);
lp.assert(bc.session.page != null, "CDP.network.httpRequestFail null page", .{});
// We're missing a bunch of fields, but, for now, this seems like enough
try bc.cdp.sendEvent("Network.loadingFailed", .{

View File

@@ -17,13 +17,15 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const lp = @import("lightpanda");
const log = @import("../../log.zig");
const js = @import("../../browser/js/js.zig");
const Page = @import("../../browser/Page.zig");
const timestampF = @import("../../datetime.zig").timestamp;
const Notification = @import("../../Notification.zig");
const log = @import("../../log.zig");
const js = @import("../../browser/js/js.zig");
const v8 = js.v8;
const v8 = js.v8;
const Allocator = std.mem.Allocator;
pub fn processMessage(cmd: anytype) !void {
@@ -142,7 +144,7 @@ fn close(cmd: anytype) !void {
const target_id = bc.target_id orelse return error.TargetNotLoaded;
// can't be null if we have a target_id
std.debug.assert(bc.session.page != null);
lp.assert(bc.session.page != null, "CDP.page.close null page", .{});
try cmd.sendResult(.{}, .{});

View File

@@ -17,6 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const lp = @import("lightpanda");
const log = @import("../../log.zig");
// TODO: hard coded IDs
@@ -164,10 +165,10 @@ fn createTarget(cmd: anytype) !void {
}
// if target_id is null, we should never have a page
std.debug.assert(bc.session.page == null);
lp.assert(bc.session.page == null, "CDP.target.createTarget not null page", .{});
// if target_id is null, we should never have a session_id
std.debug.assert(bc.session_id == null);
lp.assert(bc.session_id == null, "CDP.target.createTarget not null session_id", .{});
const target_id = cmd.cdp.target_id_gen.next();
@@ -255,7 +256,7 @@ fn closeTarget(cmd: anytype) !void {
}
// can't be null if we have a target_id
std.debug.assert(bc.session.page != null);
lp.assert(bc.session.page != null, "CDP.target.closeTarget null page", .{});
try cmd.sendResult(.{ .success = true }, .{ .include_session_id = false });
@@ -332,7 +333,7 @@ fn sendMessageToTarget(cmd: anytype) !void {
return error.TargetNotLoaded;
}
std.debug.assert(bc.session_id != null);
lp.assert(bc.session_id != null, "CDP.target.sendMessageToTarget null session_id", .{});
if (std.mem.eql(u8, bc.session_id.?, params.sessionId) == false) {
// Is this right? Is the params.sessionId meant to be the active
// sessionId? What else could it be? We have no other session_id.
@@ -440,7 +441,7 @@ fn setAutoAttach(cmd: anytype) !void {
fn doAttachtoTarget(cmd: anytype, target_id: []const u8) !void {
const bc = cmd.browser_context.?;
std.debug.assert(bc.session_id == null);
lp.assert(bc.session_id == null, "CDP.target.doAttachtoTarget not null session_id", .{});
const session_id = cmd.cdp.session_id_gen.next();
// extra_headers should not be kept on a new page or tab,