diff options
| author | kcbanner <kcbanner@gmail.com> | 2025-10-08 03:49:13 -0400 |
|---|---|---|
| committer | kcbanner <kcbanner@gmail.com> | 2025-10-09 01:06:09 -0400 |
| commit | 8b6cdc3d8224cf1e97a970f78281db2fcd14b7a2 (patch) | |
| tree | 2b7c53bbec33d5c1d9edde483a51fc196ed602e6 /lib/std | |
| parent | 447280d0d98b540aed4df83db7cf95a417d81611 (diff) | |
| download | zig-8b6cdc3d8224cf1e97a970f78281db2fcd14b7a2.tar.gz zig-8b6cdc3d8224cf1e97a970f78281db2fcd14b7a2.zip | |
- Rework common translate-c and cImport logic into `Compilation.translateC`
- Add std.zig.Server.allocErrorBundle, replace duplicates
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/Build/Step.zig | 17 | ||||
| -rw-r--r-- | lib/std/Build/WebServer.zig | 14 | ||||
| -rw-r--r-- | lib/std/zig/Server.zig | 22 |
3 files changed, 24 insertions, 29 deletions
diff --git a/lib/std/Build/Step.zig b/lib/std/Build/Step.zig index 8e9e12248a..6e7e9c4702 100644 --- a/lib/std/Build/Step.zig +++ b/lib/std/Build/Step.zig @@ -524,22 +524,7 @@ fn zigProcessUpdate(s: *Step, zp: *ZigProcess, watch: bool, web_server: ?*Build. } }, .error_bundle => { - const EbHdr = std.zig.Server.Message.ErrorBundle; - const eb_hdr = @as(*align(1) const EbHdr, @ptrCast(body)); - const extra_bytes = - body[@sizeOf(EbHdr)..][0 .. @sizeOf(u32) * eb_hdr.extra_len]; - const string_bytes = - body[@sizeOf(EbHdr) + extra_bytes.len ..][0..eb_hdr.string_bytes_len]; - // TODO: use @ptrCast when the compiler supports it - const unaligned_extra = std.mem.bytesAsSlice(u32, extra_bytes); - { - s.result_error_bundle = .{ .string_bytes = &.{}, .extra = &.{} }; - errdefer s.result_error_bundle.deinit(gpa); - s.result_error_bundle.string_bytes = try gpa.dupe(u8, string_bytes); - const extra = try gpa.alloc(u32, unaligned_extra.len); - @memcpy(extra, unaligned_extra); - s.result_error_bundle.extra = extra; - } + s.result_error_bundle = try std.zig.Server.allocErrorBundle(gpa, body); // This message indicates the end of the update. if (watch) break :poll; }, diff --git a/lib/std/Build/WebServer.zig b/lib/std/Build/WebServer.zig index 8f91a8580d..f05d8490d3 100644 --- a/lib/std/Build/WebServer.zig +++ b/lib/std/Build/WebServer.zig @@ -595,19 +595,7 @@ fn buildClientWasm(ws: *WebServer, arena: Allocator, optimize: std.builtin.Optim } }, .error_bundle => { - const EbHdr = std.zig.Server.Message.ErrorBundle; - const eb_hdr = @as(*align(1) const EbHdr, @ptrCast(body)); - const extra_bytes = - body[@sizeOf(EbHdr)..][0 .. @sizeOf(u32) * eb_hdr.extra_len]; - const string_bytes = - body[@sizeOf(EbHdr) + extra_bytes.len ..][0..eb_hdr.string_bytes_len]; - const unaligned_extra: []align(1) const u32 = @ptrCast(extra_bytes); - const extra_array = try arena.alloc(u32, unaligned_extra.len); - @memcpy(extra_array, unaligned_extra); - result_error_bundle = .{ - .string_bytes = try arena.dupe(u8, string_bytes), - .extra = extra_array, - }; + result_error_bundle = try std.zig.Server.allocErrorBundle(arena, body); }, .emit_digest => { const EmitDigest = std.zig.Server.Message.EmitDigest; diff --git a/lib/std/zig/Server.zig b/lib/std/zig/Server.zig index c035cbdec2..06b9afae91 100644 --- a/lib/std/zig/Server.zig +++ b/lib/std/zig/Server.zig @@ -231,6 +231,28 @@ pub fn serveErrorBundle(s: *Server, error_bundle: std.zig.ErrorBundle) !void { try s.out.flush(); } +pub fn allocErrorBundle(allocator: std.mem.Allocator, body: []const u8) !std.zig.ErrorBundle { + const eb_hdr = @as(*align(1) const OutMessage.ErrorBundle, @ptrCast(body)); + const extra_bytes = + body[@sizeOf(OutMessage.ErrorBundle)..][0 .. @sizeOf(u32) * eb_hdr.extra_len]; + const string_bytes = + body[@sizeOf(OutMessage.ErrorBundle) + extra_bytes.len ..][0..eb_hdr.string_bytes_len]; + const unaligned_extra: []align(1) const u32 = @ptrCast(extra_bytes); + + var error_bundle: std.zig.ErrorBundle = .{ + .string_bytes = &.{}, + .extra = &.{}, + }; + errdefer error_bundle.deinit(allocator); + + error_bundle.string_bytes = try allocator.dupe(u8, string_bytes); + const extra = try allocator.alloc(u32, unaligned_extra.len); + @memcpy(extra, unaligned_extra); + error_bundle.extra = extra; + + return error_bundle; +} + pub const TestMetadata = struct { names: []const u32, expected_panic_msgs: []const u32, |
