zig-webgpu-gen

git clone git://git.electrosoup.com/zig-webgpu-gen
Log | Files | Refs

commit 47b753d85b8e470cdb8d4e9e076751efa7921368
parent afa0b976ab44e75e949913d694be891b3eb92191
Author: Christian Ermann <christianermann@gmail.com>
Date:   Sat,  9 May 2026 15:42:32 -0700

Update to zig 0.16.0

Diffstat:
Mbuild.zig.zon | 2+-
Msrc/main.zig | 118+++++++++++++++++++++++++++++++++++++++++++------------------------------------
2 files changed, 66 insertions(+), 54 deletions(-)

diff --git a/build.zig.zon b/build.zig.zon @@ -28,7 +28,7 @@ // Tracks the earliest Zig version that the package considers to be a // supported use case. - .minimum_zig_version = "0.15.0-dev.515+833d4c9ce", + .minimum_zig_version = "0.16.0", // This field is optional. // Each dependency must either provide a `url` and `hash`, or a `path`. diff --git a/src/main.zig b/src/main.zig @@ -23,7 +23,7 @@ const Constant64 = enum { uint64_max, nan, - pub fn render(self: Constant64, writer: anytype) !void { + pub fn render(self: Constant64, writer: *std.Io.Writer) !void { switch (self) { .usize_max => try writer.writeAll("std.math.maxInt(usize)"), .uint32_max => try writer.writeAll("std.math.maxInt(u32)"), @@ -38,7 +38,7 @@ const Value64 = union(enum) { float: f64, constant: Constant64, - pub fn render(self: Value64, writer: anytype) !void { + pub fn render(self: Value64, writer: *std.Io.Writer) !void { switch (self) { .int => |val| try writer.print("{d}", .{ val }), .float => |val| try writer.print("{d}", .{ val }), @@ -145,7 +145,7 @@ const PrimitiveType = enum { pub fn renderDefn( self: PrimitiveType, - writer: anytype, + writer: *std.Io.Writer, name: []const u8, optional: bool, pointer: ?Pointer, @@ -179,7 +179,7 @@ const PrimitiveType = enum { pub fn renderExternDefn( self: PrimitiveType, - writer: anytype, + writer: *std.Io.Writer, name: []const u8, optional: bool, pointer: ?Pointer, @@ -224,7 +224,7 @@ const PrimitiveType = enum { pub fn renderExternCall( self: PrimitiveType, - writer: anytype, + writer: *std.Io.Writer, name: []const u8, indent: u32, ) !void { @@ -244,7 +244,7 @@ const PrimitiveType = enum { fn renderReturn( self: PrimitiveType, - writer: anytype, + writer: *std.Io.Writer, optional: bool, pointer: ?Pointer, ) !void { @@ -291,7 +291,7 @@ const ComplexType = union(enum) { pub fn renderDefn( self: ComplexType, - writer: anytype, + writer: *std.Io.Writer, name: []const u8, optional: bool, pointer: ?Pointer, @@ -331,7 +331,7 @@ const ComplexType = union(enum) { pub fn renderExternDefn( self: ComplexType, - writer: anytype, + writer: *std.Io.Writer, name: []const u8, optional: bool, pointer: ?Pointer, @@ -381,7 +381,7 @@ const ComplexType = union(enum) { pub fn renderExternCall( self: ComplexType, - writer: anytype, + writer: *std.Io.Writer, name: []const u8, indent: u32, ) !void { @@ -404,7 +404,7 @@ const ComplexType = union(enum) { fn renderReturn( self: ComplexType, - writer: anytype, + writer: *std.Io.Writer, optional: bool, pointer: ?Pointer, ) !void { @@ -478,7 +478,7 @@ const CallbackType = struct { pub fn renderDefn( self: CallbackType, - writer: anytype, + writer: *std.Io.Writer, name: []const u8, optional: bool, pointer: ?Pointer, @@ -502,7 +502,7 @@ const CallbackType = struct { pub fn renderExternDefn( self: CallbackType, - writer: anytype, + writer: *std.Io.Writer, name: []const u8, optional: bool, pointer: ?Pointer, @@ -526,7 +526,7 @@ const CallbackType = struct { pub fn renderExternCall( self: CallbackType, - writer: anytype, + writer: *std.Io.Writer, name: []const u8, indent: u32, ) !void { @@ -581,7 +581,7 @@ const Type = union(enum) { pub fn renderDefn( self: Type, - writer: anytype, + writer: *std.Io.Writer, name: []const u8, optional: bool, pointer: ?Pointer, @@ -600,7 +600,7 @@ const Type = union(enum) { pub fn renderExternDefn( self: Type, - writer: anytype, + writer: *std.Io.Writer, name: []const u8, optional: bool, pointer: ?Pointer, @@ -619,7 +619,7 @@ const Type = union(enum) { pub fn renderExternCall( self: Type, - writer: anytype, + writer: *std.Io.Writer, name: []const u8, indent: u32, ) !void { @@ -634,7 +634,7 @@ const Type = union(enum) { fn renderReturn( self: Type, - writer: anytype, + writer: *std.Io.Writer, optional: bool, pointer: ?Pointer, ) !void { @@ -731,7 +731,7 @@ const ParameterType = struct { fn renderDefn( self: ParameterType, - writer: anytype, + writer: *std.Io.Writer, indent: u32, ) !void { try renderDoc(self, writer, indent); @@ -746,7 +746,7 @@ const ParameterType = struct { fn renderExternDefn( self: ParameterType, - writer: anytype, + writer: *std.Io.Writer, indent: u32, ) !void { try renderDoc(self, writer, indent); @@ -761,7 +761,7 @@ const ParameterType = struct { fn renderExternCall( self: ParameterType, - writer: anytype, + writer: *std.Io.Writer, indent: u32, ) !void { return self.type.renderExternCall( @@ -785,7 +785,7 @@ const Constant = struct { value: Value64, doc: []const u8, - pub fn render(self: Constant, writer: anytype) !void { + pub fn render(self: Constant, writer: *std.Io.Writer) !void { try writer.print("pub const {s} = ", .{ self.name }); try self.value.render(writer); try writer.writeAll(";\n"); @@ -799,7 +799,7 @@ const Enum = struct { extended: ?bool = null, entries: []const ?EnumEntry, - pub fn render(self: Enum, writer: anytype) !void { + pub fn render(self: Enum, writer: *std.Io.Writer) !void { if (self.extended) |extended| { _ = extended; return error.Unimplemented; @@ -826,7 +826,7 @@ const EnumEntry = struct { doc: []const u8, value: ?u16 = null, - pub fn render(self: EnumEntry, writer: anytype) !void { + pub fn render(self: EnumEntry, writer: *std.Io.Writer) !void { if (self.value) |value| { _ = value; return error.Unimplemented; @@ -844,7 +844,7 @@ const BitFlag = struct { extended: ?bool = null, entries: []const ?BitFlagEntry, - pub fn render(self: BitFlag, writer: anytype, indent: u32) !void { + pub fn render(self: BitFlag, writer: *std.Io.Writer, indent: u32) !void { try renderDoc(self, writer, indent); try renderIndent(indent, writer); try writer.writeAll("pub const "); @@ -918,7 +918,7 @@ const Struct = struct { free_members: ?bool = null, members: ?[]ParameterType = null, - fn render(self: Struct, writer: anytype, indent: u32) !void { + fn render(self: Struct, writer: *std.Io.Writer, indent: u32) !void { try renderDoc(self, writer, indent); try renderIndent(indent, writer); try writer.writeAll("pub const "); @@ -959,7 +959,7 @@ const Struct = struct { try writer.writeAll("};\n"); } - fn renderInitMethod(self: Struct, writer: anytype, indent: u32) !void { + fn renderInitMethod(self: Struct, writer: *std.Io.Writer, indent: u32) !void { try renderIndent(indent, writer); try writer.writeAll("pub fn init(\n"); if (self.members) |members| { @@ -992,7 +992,7 @@ const Callback = struct { style: CallbackStyle, args: ?[]ParameterType = null, - pub fn render(self: Callback, writer: anytype) !void { + pub fn render(self: Callback, writer: *std.Io.Writer) !void { try writer.writeAll("pub const "); try snakeToPascal(self.name, writer); try writer.writeAll("Callback = fn (\n"); @@ -1012,7 +1012,7 @@ const ReturnType = struct { passed_with_ownership: ?bool = null, pointer: ?Pointer = null, - pub fn render(self: ReturnType, writer: anytype) !void { + pub fn render(self: ReturnType, writer: *std.Io.Writer) !void { try self.type.renderReturn( writer, self.optional, @@ -1031,7 +1031,7 @@ const Function = struct { fn renderArgsExternDefn( self: Function, - writer: anytype, + writer: *std.Io.Writer, object: ?[]const u8, indent: u32 ) !void { @@ -1050,7 +1050,7 @@ const Function = struct { fn renderArgsExternCall( self: Function, - writer: anytype, + writer: *std.Io.Writer, object: ?[]const u8, indent: u32 ) !void { @@ -1067,7 +1067,7 @@ const Function = struct { fn renderArgsDefn( self: Function, - writer: anytype, + writer: *std.Io.Writer, object: ?[]const u8, indent: u32 ) !void { @@ -1086,7 +1086,7 @@ const Function = struct { fn renderExternDefn( self: Function, - writer: anytype, + writer: *std.Io.Writer, object: ?[]const u8, indent: u32, ) !void { @@ -1111,7 +1111,7 @@ const Function = struct { fn renderExternCall( self: Function, - writer: anytype, + writer: *std.Io.Writer, object: ?[]const u8, indent: u32, ) !void { @@ -1128,7 +1128,7 @@ const Function = struct { fn renderDefn( self: Function, - writer: anytype, + writer: *std.Io.Writer, object: ?[]const u8, indent: u32, ) !void { @@ -1163,7 +1163,7 @@ const Object = struct { extended: ?bool = null, methods: []Function, - pub fn render(self: Object, writer: anytype, indent: u32) !void { + pub fn render(self: Object, writer: *std.Io.Writer, indent: u32) !void { for (self.methods) |method| { try method.renderExternDefn(writer, self.name, indent); try writer.writeAll("\n"); @@ -1183,7 +1183,7 @@ const Object = struct { } }; -fn snakeToPascal(str: []const u8, writer: anytype) !void { +fn snakeToPascal(str: []const u8, writer: *std.Io.Writer) !void { var it = std.mem.splitScalar(u8, str, '_'); while (it.next()) |token| { try writer.writeByte(std.ascii.toUpper(token[0])); @@ -1191,7 +1191,7 @@ fn snakeToPascal(str: []const u8, writer: anytype) !void { } } -fn snakeToCamel(str: []const u8, writer: anytype) !void { +fn snakeToCamel(str: []const u8, writer: *std.Io.Writer) !void { var it = std.mem.splitScalar(u8, str, '_'); if (it.next()) |token| { try writer.writeAll(token); @@ -1202,7 +1202,7 @@ fn snakeToCamel(str: []const u8, writer: anytype) !void { } } -fn toLowercase(str: []const u8, writer: anytype) !void { +fn toLowercase(str: []const u8, writer: *std.Io.Writer) !void { for (str) |anycase| { const lowercase = std.ascii.toLower(anycase); try writer.writeByte(lowercase); @@ -1211,7 +1211,7 @@ fn toLowercase(str: []const u8, writer: anytype) !void { fn renderDoc( self: anytype, - writer: anytype, + writer: *std.Io.Writer, indent: u32, ) !void { if (std.mem.startsWith(u8, self.doc, "TODO")) { @@ -1230,13 +1230,13 @@ fn renderDoc( } } -fn renderIndent(indent: u32, writer: anytype) !void { +fn renderIndent(indent: u32, writer: *std.Io.Writer) !void { for (0..indent) |_| { try writer.writeAll(" "); } } -fn renderHeader(writer: anytype) !void { +fn renderHeader(writer: *std.Io.Writer) !void { const header = \\// auto-generated zig binding for webgpu \\ @@ -1277,19 +1277,28 @@ fn renderHeader(writer: anytype) !void { try writer.writeAll("\n"); } -pub fn main() !void { - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; - const allocator = gpa.allocator(); +pub fn main(init: std.process.Init) !void { + const allocator = init.gpa; + const io = init.io; - const args = try std.process.argsAlloc(allocator); - const out_path = args[1]; - const out_file = try std.fs.cwd().createFile(out_path, .{}); - defer out_file.close(); - - const file = try std.fs.cwd().openFile("webgpu.json", .{}); - defer file.close(); + const args = try init.minimal.args.toSlice(init.arena.allocator()); - const json = try file.readToEndAlloc(allocator, 10 * 2024 * 1024); + const out_path = args[1]; + const out_file = try std.Io.Dir.createFile( + std.Io.Dir.cwd(), + io, + out_path, + .{}, + ); + defer out_file.close(io); + + const json = try std.Io.Dir.readFileAlloc( + std.Io.Dir.cwd(), + io, + "webgpu.json", + allocator, + .unlimited, + ); defer allocator.free(json); const parsed = try std.json.parseFromSlice( @@ -1302,7 +1311,9 @@ pub fn main() !void { ); defer parsed.deinit(); - const stdout = out_file.writer(); + var buf: [1024]u8 = undefined; + var out_writer = out_file.writer(io, &buf); + var stdout = &out_writer.interface; try renderHeader(stdout); try stdout.writeAll("// constants\n"); @@ -1339,5 +1350,6 @@ pub fn main() !void { try object.render(stdout, 0); try stdout.writeAll("\n"); } + try stdout.flush(); }