Skip to content

Commit 730590b

Browse files
committed
- Rework common translate-c and cImport logic into Compilation.translateC
- Add std.zig.Server.allocErrorBundle, replace duplicates
1 parent eae3204 commit 730590b

File tree

7 files changed

+206
-258
lines changed

7 files changed

+206
-258
lines changed

lib/compiler/std-docs.zig

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -345,20 +345,7 @@ fn buildWasmBinary(
345345
}
346346
},
347347
.error_bundle => {
348-
const EbHdr = std.zig.Server.Message.ErrorBundle;
349-
const eb_hdr = @as(*align(1) const EbHdr, @ptrCast(body));
350-
const extra_bytes =
351-
body[@sizeOf(EbHdr)..][0 .. @sizeOf(u32) * eb_hdr.extra_len];
352-
const string_bytes =
353-
body[@sizeOf(EbHdr) + extra_bytes.len ..][0..eb_hdr.string_bytes_len];
354-
// TODO: use @ptrCast when the compiler supports it
355-
const unaligned_extra = std.mem.bytesAsSlice(u32, extra_bytes);
356-
const extra_array = try arena.alloc(u32, unaligned_extra.len);
357-
@memcpy(extra_array, unaligned_extra);
358-
result_error_bundle = .{
359-
.string_bytes = try arena.dupe(u8, string_bytes),
360-
.extra = extra_array,
361-
};
348+
result_error_bundle = try std.zig.Server.allocErrorBundle(arena, body);
362349
},
363350
.emit_digest => {
364351
const EmitDigest = std.zig.Server.Message.EmitDigest;

lib/std/Build/Step.zig

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -524,22 +524,7 @@ fn zigProcessUpdate(s: *Step, zp: *ZigProcess, watch: bool, web_server: ?*Build.
524524
}
525525
},
526526
.error_bundle => {
527-
const EbHdr = std.zig.Server.Message.ErrorBundle;
528-
const eb_hdr = @as(*align(1) const EbHdr, @ptrCast(body));
529-
const extra_bytes =
530-
body[@sizeOf(EbHdr)..][0 .. @sizeOf(u32) * eb_hdr.extra_len];
531-
const string_bytes =
532-
body[@sizeOf(EbHdr) + extra_bytes.len ..][0..eb_hdr.string_bytes_len];
533-
// TODO: use @ptrCast when the compiler supports it
534-
const unaligned_extra = std.mem.bytesAsSlice(u32, extra_bytes);
535-
{
536-
s.result_error_bundle = .{ .string_bytes = &.{}, .extra = &.{} };
537-
errdefer s.result_error_bundle.deinit(gpa);
538-
s.result_error_bundle.string_bytes = try gpa.dupe(u8, string_bytes);
539-
const extra = try gpa.alloc(u32, unaligned_extra.len);
540-
@memcpy(extra, unaligned_extra);
541-
s.result_error_bundle.extra = extra;
542-
}
527+
s.result_error_bundle = try std.zig.Server.allocErrorBundle(gpa, body);
543528
// This message indicates the end of the update.
544529
if (watch) break :poll;
545530
},

lib/std/Build/WebServer.zig

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -595,19 +595,7 @@ fn buildClientWasm(ws: *WebServer, arena: Allocator, optimize: std.builtin.Optim
595595
}
596596
},
597597
.error_bundle => {
598-
const EbHdr = std.zig.Server.Message.ErrorBundle;
599-
const eb_hdr = @as(*align(1) const EbHdr, @ptrCast(body));
600-
const extra_bytes =
601-
body[@sizeOf(EbHdr)..][0 .. @sizeOf(u32) * eb_hdr.extra_len];
602-
const string_bytes =
603-
body[@sizeOf(EbHdr) + extra_bytes.len ..][0..eb_hdr.string_bytes_len];
604-
const unaligned_extra: []align(1) const u32 = @ptrCast(extra_bytes);
605-
const extra_array = try arena.alloc(u32, unaligned_extra.len);
606-
@memcpy(extra_array, unaligned_extra);
607-
result_error_bundle = .{
608-
.string_bytes = try arena.dupe(u8, string_bytes),
609-
.extra = extra_array,
610-
};
598+
result_error_bundle = try std.zig.Server.allocErrorBundle(arena, body);
611599
},
612600
.emit_digest => {
613601
const EmitDigest = std.zig.Server.Message.EmitDigest;

lib/std/zig/Server.zig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,28 @@ pub fn serveErrorBundle(s: *Server, error_bundle: std.zig.ErrorBundle) !void {
231231
try s.out.flush();
232232
}
233233

234+
pub fn allocErrorBundle(allocator: std.mem.Allocator, body: []const u8) !std.zig.ErrorBundle {
235+
const eb_hdr = @as(*align(1) const OutMessage.ErrorBundle, @ptrCast(body));
236+
const extra_bytes =
237+
body[@sizeOf(OutMessage.ErrorBundle)..][0 .. @sizeOf(u32) * eb_hdr.extra_len];
238+
const string_bytes =
239+
body[@sizeOf(OutMessage.ErrorBundle) + extra_bytes.len ..][0..eb_hdr.string_bytes_len];
240+
const unaligned_extra: []align(1) const u32 = @ptrCast(extra_bytes);
241+
242+
var error_bundle: std.zig.ErrorBundle = .{
243+
.string_bytes = &.{},
244+
.extra = &.{},
245+
};
246+
errdefer error_bundle.deinit(allocator);
247+
248+
error_bundle.string_bytes = try allocator.dupe(u8, string_bytes);
249+
const extra = try allocator.alloc(u32, unaligned_extra.len);
250+
@memcpy(extra, unaligned_extra);
251+
error_bundle.extra = extra;
252+
253+
return error_bundle;
254+
}
255+
234256
pub const TestMetadata = struct {
235257
names: []const u32,
236258
expected_panic_msgs: []const u32,

0 commit comments

Comments
 (0)