Zig 0.15.1

Depends on https://github.com/lightpanda-io/zig-v8-fork/pull/89
This commit is contained in:
Karl Seguin
2025-08-28 19:42:26 +08:00
parent 94960cc842
commit 1443f38e5f
51 changed files with 508 additions and 525 deletions

View File

@@ -211,11 +211,11 @@ pub const Writer = struct {
exclude_root: bool = false,
};
pub fn jsonStringify(self: *const Writer, w: anytype) !void {
pub fn jsonStringify(self: *const Writer, w: anytype) error{WriteFailed}!void {
if (self.exclude_root) {
_ = self.writeChildren(self.root, 1, w) catch |err| {
log.err(.cdp, "node writeChildren", .{ .err = err });
return error.OutOfMemory;
return error.WriteFailed;
};
} else {
self.toJSON(self.root, 0, w) catch |err| {
@@ -223,7 +223,7 @@ pub const Writer = struct {
// @TypeOf(w).Error. In other words, our code can't return its own
// error, we can only return a writer error. Kinda sucks.
log.err(.cdp, "node toJSON stringify", .{ .err = err });
return error.OutOfMemory;
return error.WriteFailed;
};
}
}
@@ -425,7 +425,7 @@ test "cdp Node: Writer" {
{
const node = try registry.register(doc.asNode());
const json = try std.json.stringifyAlloc(testing.allocator, Writer{
const json = try std.json.Stringify.valueAlloc(testing.allocator, Writer{
.root = node,
.depth = 0,
.exclude_root = false,
@@ -465,7 +465,7 @@ test "cdp Node: Writer" {
{
const node = registry.lookup_by_id.get(1).?;
const json = try std.json.stringifyAlloc(testing.allocator, Writer{
const json = try std.json.Stringify.valueAlloc(testing.allocator, Writer{
.root = node,
.depth = 1,
.exclude_root = false,
@@ -520,7 +520,7 @@ test "cdp Node: Writer" {
{
const node = registry.lookup_by_id.get(1).?;
const json = try std.json.stringifyAlloc(testing.allocator, Writer{
const json = try std.json.Stringify.valueAlloc(testing.allocator, Writer{
.root = node,
.depth = -1,
.exclude_root = true,

View File

@@ -487,58 +487,58 @@ pub fn BrowserContext(comptime CDP_T: type) type {
}
pub fn onPageRemove(ctx: *anyopaque, _: Notification.PageRemove) !void {
const self: *Self = @alignCast(@ptrCast(ctx));
const self: *Self = @ptrCast(@alignCast(ctx));
try @import("domains/page.zig").pageRemove(self);
}
pub fn onPageCreated(ctx: *anyopaque, page: *Page) !void {
const self: *Self = @alignCast(@ptrCast(ctx));
const self: *Self = @ptrCast(@alignCast(ctx));
return @import("domains/page.zig").pageCreated(self, page);
}
pub fn onPageNavigate(ctx: *anyopaque, msg: *const Notification.PageNavigate) !void {
const self: *Self = @alignCast(@ptrCast(ctx));
const self: *Self = @ptrCast(@alignCast(ctx));
defer self.resetNotificationArena();
return @import("domains/page.zig").pageNavigate(self.notification_arena, self, msg);
}
pub fn onPageNavigated(ctx: *anyopaque, msg: *const Notification.PageNavigated) !void {
const self: *Self = @alignCast(@ptrCast(ctx));
const self: *Self = @ptrCast(@alignCast(ctx));
return @import("domains/page.zig").pageNavigated(self, msg);
}
pub fn onHttpRequestStart(ctx: *anyopaque, msg: *const Notification.RequestStart) !void {
const self: *Self = @alignCast(@ptrCast(ctx));
const self: *Self = @ptrCast(@alignCast(ctx));
defer self.resetNotificationArena();
try @import("domains/network.zig").httpRequestStart(self.notification_arena, self, msg);
}
pub fn onHttpRequestIntercept(ctx: *anyopaque, msg: *const Notification.RequestIntercept) !void {
const self: *Self = @alignCast(@ptrCast(ctx));
const self: *Self = @ptrCast(@alignCast(ctx));
defer self.resetNotificationArena();
try @import("domains/fetch.zig").requestIntercept(self.notification_arena, self, msg);
}
pub fn onHttpRequestFail(ctx: *anyopaque, msg: *const Notification.RequestFail) !void {
const self: *Self = @alignCast(@ptrCast(ctx));
const self: *Self = @ptrCast(@alignCast(ctx));
defer self.resetNotificationArena();
return @import("domains/network.zig").httpRequestFail(self.notification_arena, self, msg);
}
pub fn onHttpResponseHeadersDone(ctx: *anyopaque, msg: *const Notification.ResponseHeaderDone) !void {
const self: *Self = @alignCast(@ptrCast(ctx));
const self: *Self = @ptrCast(@alignCast(ctx));
defer self.resetNotificationArena();
return @import("domains/network.zig").httpResponseHeaderDone(self.notification_arena, self, msg);
}
pub fn onHttpRequestDone(ctx: *anyopaque, msg: *const Notification.RequestDone) !void {
const self: *Self = @alignCast(@ptrCast(ctx));
const self: *Self = @ptrCast(@alignCast(ctx));
defer self.resetNotificationArena();
return @import("domains/network.zig").httpRequestDone(self.notification_arena, self, msg);
}
pub fn onHttpResponseData(ctx: *anyopaque, msg: *const Notification.ResponseData) !void {
const self: *Self = @alignCast(@ptrCast(ctx));
const self: *Self = @ptrCast(@alignCast(ctx));
const arena = self.arena;
const id = msg.transfer.id;
@@ -550,7 +550,7 @@ pub fn BrowserContext(comptime CDP_T: type) type {
}
pub fn onHttpRequestAuthRequired(ctx: *anyopaque, data: *const Notification.RequestAuthRequired) !void {
const self: *Self = @alignCast(@ptrCast(ctx));
const self: *Self = @ptrCast(@alignCast(ctx));
defer self.resetNotificationArena();
try @import("domains/fetch.zig").requestAuthRequired(self.notification_arena, self, data);
}
@@ -566,7 +566,7 @@ pub fn BrowserContext(comptime CDP_T: type) type {
}
pub fn onInspectorResponse(ctx: *anyopaque, _: u32, msg: []const u8) void {
sendInspectorMessage(@alignCast(@ptrCast(ctx)), msg) catch |err| {
sendInspectorMessage(@ptrCast(@alignCast(ctx)), msg) catch |err| {
log.err(.cdp, "send inspector response", .{ .err = err });
};
}
@@ -583,7 +583,7 @@ pub fn BrowserContext(comptime CDP_T: type) type {
log.debug(.cdp, "inspector event", .{ .method = method });
}
sendInspectorMessage(@alignCast(@ptrCast(ctx)), msg) catch |err| {
sendInspectorMessage(@ptrCast(@alignCast(ctx)), msg) catch |err| {
log.err(.cdp, "send inspector event", .{ .err = err });
};
}

View File

@@ -372,7 +372,7 @@ fn getNode(arena: Allocator, browser_context: anytype, node_id: ?Node.Id, backen
if (object_id) |object_id_| {
// Retrieve the object from which ever context it is in.
const parser_node = try browser_context.inspector.getNodePtr(arena, object_id_);
return try browser_context.node_registry.register(@alignCast(@ptrCast(parser_node)));
return try browser_context.node_registry.register(@ptrCast(@alignCast(parser_node)));
}
return error.MissingParams;
}

View File

@@ -315,10 +315,10 @@ fn continueWithAuth(cmd: anytype) !void {
// restart the request with the provided credentials.
const arena = transfer.arena.allocator();
transfer.updateCredentials(
try std.fmt.allocPrintZ(arena, "{s}:{s}", .{
try std.fmt.allocPrintSentinel(arena, "{s}:{s}", .{
params.authChallengeResponse.username,
params.authChallengeResponse.password,
}),
}, 0),
);
transfer.reset();

View File

@@ -81,7 +81,7 @@ fn setExtraHTTPHeaders(cmd: anytype) !void {
try extra_headers.ensureTotalCapacity(arena, params.headers.map.count());
var it = params.headers.map.iterator();
while (it.next()) |header| {
const header_string = try std.fmt.allocPrintZ(arena, "{s}: {s}", .{ header.key_ptr.*, header.value_ptr.* });
const header_string = try std.fmt.allocPrintSentinel(arena, "{s}: {s}", .{ header.key_ptr.*, header.value_ptr.* }, 0);
extra_headers.appendAssumeCapacity(header_string);
}
@@ -296,58 +296,61 @@ pub const TransferAsRequestWriter = struct {
};
}
pub fn jsonStringify(self: *const TransferAsRequestWriter, writer: anytype) !void {
const stream = writer.stream;
pub fn jsonStringify(self: *const TransferAsRequestWriter, jws: anytype) !void {
self._jsonStringify(jws) catch return error.WriteFailed;
}
fn _jsonStringify(self: *const TransferAsRequestWriter, jws: anytype) !void {
const writer = jws.writer;
const transfer = self.transfer;
try writer.beginObject();
try jws.beginObject();
{
try writer.objectField("url");
try writer.beginWriteRaw();
try stream.writeByte('\"');
try transfer.uri.writeToStream(.{
try jws.objectField("url");
try jws.beginWriteRaw();
try writer.writeByte('\"');
try transfer.uri.writeToStream(writer, .{
.scheme = true,
.authentication = true,
.authority = true,
.path = true,
.query = true,
}, stream);
try stream.writeByte('\"');
writer.endWriteRaw();
});
try writer.writeByte('\"');
jws.endWriteRaw();
}
{
if (transfer.uri.fragment) |frag| {
try writer.objectField("urlFragment");
try writer.beginWriteRaw();
try stream.writeAll("\"#");
try stream.writeAll(frag.percent_encoded);
try stream.writeByte('\"');
writer.endWriteRaw();
try jws.objectField("urlFragment");
try jws.beginWriteRaw();
try writer.writeAll("\"#");
try writer.writeAll(frag.percent_encoded);
try writer.writeByte('\"');
jws.endWriteRaw();
}
}
{
try writer.objectField("method");
try writer.write(@tagName(transfer.req.method));
try jws.objectField("method");
try jws.write(@tagName(transfer.req.method));
}
{
try writer.objectField("hasPostData");
try writer.write(transfer.req.body != null);
try jws.objectField("hasPostData");
try jws.write(transfer.req.body != null);
}
{
try writer.objectField("headers");
try writer.beginObject();
try jws.objectField("headers");
try jws.beginObject();
var it = transfer.req.headers.iterator();
while (it.next()) |hdr| {
try writer.objectField(hdr.name);
try writer.write(hdr.value);
try jws.objectField(hdr.name);
try jws.write(hdr.value);
}
try writer.endObject();
try jws.endObject();
}
try writer.endObject();
try jws.endObject();
}
};
@@ -362,35 +365,39 @@ const TransferAsResponseWriter = struct {
};
}
pub fn jsonStringify(self: *const TransferAsResponseWriter, writer: anytype) !void {
const stream = writer.stream;
pub fn jsonStringify(self: *const TransferAsResponseWriter, jws: anytype) !void {
self._jsonStringify(jws) catch return error.WriteFailed;
}
fn _jsonStringify(self: *const TransferAsResponseWriter, jws: anytype) !void {
const writer = jws.writer;
const transfer = self.transfer;
try writer.beginObject();
try jws.beginObject();
{
try writer.objectField("url");
try writer.beginWriteRaw();
try stream.writeByte('\"');
try transfer.uri.writeToStream(.{
try jws.objectField("url");
try jws.beginWriteRaw();
try writer.writeByte('\"');
try transfer.uri.writeToStream(writer, .{
.scheme = true,
.authentication = true,
.authority = true,
.path = true,
.query = true,
}, stream);
try stream.writeByte('\"');
writer.endWriteRaw();
});
try writer.writeByte('\"');
jws.endWriteRaw();
}
if (transfer.response_header) |*rh| {
// it should not be possible for this to be false, but I'm not
// feeling brave today.
const status = rh.status;
try writer.objectField("status");
try writer.write(status);
try jws.objectField("status");
try jws.write(status);
try writer.objectField("statusText");
try writer.write(@as(std.http.Status, @enumFromInt(status)).phrase() orelse "Unknown");
try jws.objectField("statusText");
try jws.write(@as(std.http.Status, @enumFromInt(status)).phrase() orelse "Unknown");
}
{
@@ -410,10 +417,10 @@ const TransferAsResponseWriter = struct {
}
}
try writer.objectField("headers");
try writer.write(std.json.ArrayHashMap([]const u8){ .map = map });
try jws.objectField("headers");
try jws.write(std.json.ArrayHashMap([]const u8){ .map = map });
}
try writer.endObject();
try jws.endObject();
}
};
@@ -426,20 +433,23 @@ const DocumentUrlWriter = struct {
};
}
pub fn jsonStringify(self: *const DocumentUrlWriter, writer: anytype) !void {
const stream = writer.stream;
pub fn jsonStringify(self: *const DocumentUrlWriter, jws: anytype) !void {
self._jsonStringify(jws) catch return error.WriteFailed;
}
fn _jsonStringify(self: *const DocumentUrlWriter, jws: anytype) !void {
const writer = jws.writer;
try writer.beginWriteRaw();
try stream.writeByte('\"');
try self.uri.writeToStream(.{
try jws.beginWriteRaw();
try writer.writeByte('\"');
try self.uri.writeToStream(writer, .{
.scheme = true,
.authentication = true,
.authority = true,
.path = true,
.query = true,
}, stream);
try stream.writeByte('\"');
writer.endWriteRaw();
});
try writer.writeByte('\"');
jws.endWriteRaw();
}
};

View File

@@ -170,7 +170,7 @@ pub const CookieWriter = struct {
self.writeCookies(w) catch |err| {
// The only error our jsonStringify method can return is @TypeOf(w).Error.
log.err(.cdp, "json stringify", .{ .err = err });
return error.OutOfMemory;
return error.WriteFailed;
};
}

View File

@@ -304,19 +304,17 @@ fn sendMessageToTarget(cmd: anytype) !void {
}
const Capture = struct {
allocator: std.mem.Allocator,
buf: std.ArrayListUnmanaged(u8),
aw: std.Io.Writer.Allocating,
pub fn sendJSON(self: *@This(), message: anytype) !void {
return std.json.stringify(message, .{
return std.json.Stringify.value(message, .{
.emit_null_optional_fields = false,
}, self.buf.writer(self.allocator));
}, &self.aw.writer);
}
};
var capture = Capture{
.buf = .{},
.allocator = cmd.arena,
.aw = .init(cmd.arena),
};
cmd.cdp.dispatch(cmd.arena, &capture, params.message) catch |err| {
@@ -325,7 +323,7 @@ fn sendMessageToTarget(cmd: anytype) !void {
};
try cmd.sendEvent("Target.receivedMessageFromTarget", .{
.message = capture.buf.items,
.message = capture.aw.written(),
.sessionId = params.sessionId,
}, .{});
}

View File

@@ -50,10 +50,10 @@ const Client = struct {
};
}
pub fn sendJSON(self: *Client, message: anytype, opts: json.StringifyOptions) !void {
pub fn sendJSON(self: *Client, message: anytype, opts: json.Stringify.Options) !void {
var opts_copy = opts;
opts_copy.whitespace = .indent_2;
const serialized = try json.stringifyAlloc(self.allocator, message, opts_copy);
const serialized = try json.Stringify.valueAlloc(self.allocator, message, opts_copy);
try self.serialized.append(self.allocator, serialized);
const value = try json.parseFromSliceLeaky(json.Value, self.allocator, serialized, .{});
@@ -131,7 +131,7 @@ const TestContext = struct {
pub fn processMessage(self: *TestContext, msg: anytype) !void {
var json_message: []const u8 = undefined;
if (@typeInfo(@TypeOf(msg)) != .pointer) {
json_message = try std.json.stringifyAlloc(self.arena.allocator(), msg, .{});
json_message = try std.json.Stringify.valueAlloc(self.arena.allocator(), msg, .{});
} else {
// assume this is a string we want to send as-is, if it isn't, we'll
// get a compile error, so no big deal.
@@ -189,7 +189,7 @@ const TestContext = struct {
index: ?usize = null,
};
pub fn expectSent(self: *TestContext, expected: anytype, opts: SentOpts) !void {
const serialized = try json.stringifyAlloc(self.arena.allocator(), expected, .{
const serialized = try json.Stringify.valueAlloc(self.arena.allocator(), expected, .{
.whitespace = .indent_2,
.emit_null_optional_fields = false,
});