| ... | ... | @@ -238,26 +238,34 @@ pub fn serveErrorBundle(s: *Server, error_bundle: std.zig.ErrorBundle) !void { |
| 238 | 238 | try s.out.flush(); |
| 239 | 239 | } |
| 240 | 240 | |
| 241 | | pub fn allocErrorBundle(allocator: std.mem.Allocator, body: []const u8) !std.zig.ErrorBundle { |
| 242 | | const eb_hdr = @as(*align(1) const OutMessage.ErrorBundle, @ptrCast(body)); |
| 243 | | const extra_bytes = |
| 244 | | body[@sizeOf(OutMessage.ErrorBundle)..][0 .. @sizeOf(u32) * eb_hdr.extra_len]; |
| 245 | | const string_bytes = |
| 246 | | body[@sizeOf(OutMessage.ErrorBundle) + extra_bytes.len ..][0..eb_hdr.string_bytes_len]; |
| 247 | | const unaligned_extra: []align(1) const u32 = @ptrCast(extra_bytes); |
| 248 | | |
| 249 | | var error_bundle: std.zig.ErrorBundle = .{ |
| 241 | pub fn allocErrorBundle(gpa: std.mem.Allocator, body: []const u8) error{ OutOfMemory, EndOfStream }!std.zig.ErrorBundle { |
| 242 | var r: Reader = .fixed(body); |
| 243 | const hdr = r.takeStruct(OutMessage.ErrorBundle, .little) catch |err| switch (err) { |
| 244 | error.EndOfStream => |e| return e, |
| 245 | error.ReadFailed => unreachable, |
| 246 | }; |
| 247 | |
| 248 | var eb: std.zig.ErrorBundle = .{ |
| 250 | 249 | .string_bytes = &.{}, |
| 251 | 250 | .extra = &.{}, |
| 252 | 251 | }; |
| 253 | | errdefer error_bundle.deinit(allocator); |
| 252 | errdefer eb.deinit(gpa); |
| 253 | |
| 254 | const extra = try gpa.alloc(u32, hdr.extra_len); |
| 255 | eb.extra = extra; |
| 256 | const string_bytes = try gpa.alloc(u8, hdr.string_bytes_len); |
| 257 | eb.string_bytes = string_bytes; |
| 254 | 258 | |
| 255 | | error_bundle.string_bytes = try allocator.dupe(u8, string_bytes); |
| 256 | | const extra = try allocator.alloc(u32, unaligned_extra.len); |
| 257 | | @memcpy(extra, unaligned_extra); |
| 258 | | error_bundle.extra = extra; |
| 259 | r.readSliceEndian(u32, extra, .little) catch |err| switch (err) { |
| 260 | error.EndOfStream => |e| return e, |
| 261 | error.ReadFailed => unreachable, |
| 262 | }; |
| 263 | r.readSliceAll(string_bytes) catch |err| switch (err) { |
| 264 | error.EndOfStream => |e| return e, |
| 265 | error.ReadFailed => unreachable, |
| 266 | }; |
| 259 | 267 | |
| 260 | | return error_bundle; |
| 268 | return eb; |
| 261 | 269 | } |
| 262 | 270 | |
| 263 | 271 | pub const TestMetadata = struct { |