Browse Source

Update Zig binding generation (#2765)

Jared Miller 3 years ago
parent
commit
c084dd2616
2 changed files with 615 additions and 636 deletions
  1. 563 575
      bindings/zig/bgfx.zig
  2. 52 61
      scripts/bindings-zig.lua

File diff suppressed because it is too large
+ 563 - 575
bindings/zig/bgfx.zig


+ 52 - 61
scripts/bindings-zig.lua

@@ -13,6 +13,8 @@ local zig_template = [[
 
 const std = @import("std");
 
+pub const ViewId = u16;
+
 $types
 
 $funcs
@@ -31,8 +33,17 @@ local function hasSuffix(str, suffix)
 	return suffix == "" or str:sub(- #suffix) == suffix
 end
 
-local function convert_type_0(arg)
+local enum = { }
 
+local function convert_array(member)
+	if string.find(member.array, "::") then
+		return string.format("[%s]", enum[member.array])
+	else
+		return member.array
+	end
+end
+
+local function convert_type_0(arg)
 	if hasPrefix(arg.ctype, "uint64_t") then
 		return arg.ctype:gsub("uint64_t", "u64")
 	elseif hasPrefix(arg.ctype, "int64_t") then
@@ -43,18 +54,14 @@ local function convert_type_0(arg)
 		return arg.ctype:gsub("int32_t", "i32")
 	elseif hasPrefix(arg.ctype, "uint16_t") then
 		return arg.ctype:gsub("uint16_t", "u16")
-	elseif hasPrefix(arg.ctype, "bgfx_view_id_t") then
-		return arg.ctype:gsub("bgfx_view_id_t", "u16")
 	elseif hasPrefix(arg.ctype, "uint8_t") then
 		return arg.ctype:gsub("uint8_t", "u8")
 	elseif hasPrefix(arg.ctype, "uintptr_t") then
 		return arg.ctype:gsub("uintptr_t", "usize")
 	elseif hasPrefix(arg.ctype, "float") then
 		return arg.ctype:gsub("float", "f32")
-		-- elseif arg.ctype == "bgfx_caps_gpu_t" then
-		--     return arg.ctype:gsub("bgfx_caps_gpu_t", "u32")
 	elseif arg.ctype == "const char*" then
-	   return "[*c]const u8"
+		return "[*c]const u8"
 	elseif hasPrefix(arg.ctype, "char") then
 		return arg.ctype:gsub("char", "u8")
 	elseif hasSuffix(arg.fulltype, "Handle") then
@@ -66,8 +73,6 @@ local function convert_type_0(arg)
 		or arg.fulltype == "CallbackI*"
 		or arg.fulltype == "ReleaseFn" then
 		return "?*anyopaque"
-	elseif arg.fulltype == "const ViewId*" then
-		return "[*c]c_ushort"
 	end
 
 	return arg.fulltype
@@ -80,24 +85,25 @@ local function convert_type(arg)
 	ctype = ctype:gsub("&", "*")
 	ctype = ctype:gsub("char", "u8")
 	ctype = ctype:gsub("float", "f32")
-	if hasPrefix(ctype, "const ") then
-		ctype = ctype:gsub("const ", "")
-	end
+	ctype = ctype:gsub("const void%*", "?*const anyopaque")
+	ctype = ctype:gsub("Encoder%*", "?*Encoder")
+
 	if hasSuffix(ctype, "void*") then
 		ctype = ctype:gsub("void%*", "?*anyopaque");
 	elseif hasSuffix(ctype, "*") then
 		ctype = "[*c]" .. ctype:gsub("*", "")
 	end
 
+	if arg.array ~= nil then
+		ctype = ctype:gsub("const ", "")
+		ctype = convert_array(arg) .. ctype
+	end
+
 	return ctype
 end
 
 local function convert_struct_type(arg)
-	local ctype = convert_type(arg)
-	if hasPrefix(arg.ctype, "bool") then
-		ctype = ctype:gsub("bool", "bool")
-	end
-	return ctype
+	return convert_type(arg)
 end
 
 local function convert_ret_type(arg)
@@ -106,10 +112,10 @@ end
 
 local converter = {}
 local yield = coroutine.yield
-local indent = ""
-
 local gen = {}
 
+local indent = ""
+
 function gen.gen()
 	local r = zig_template:gsub("$(%l+)", function(what)
 		local tmp = {}
@@ -134,10 +140,6 @@ function gen.gen()
 	return r
 end
 
--- function gen.gen_dllname()
--- 	return csharp_dllname_template
--- end
-
 local combined = { "State", "Stencil", "Buffer", "Texture", "Sampler", "Reset" }
 
 for _, v in ipairs(combined) do
@@ -145,39 +147,40 @@ for _, v in ipairs(combined) do
 end
 
 local lastCombinedFlag
-
 local function FlagBlock(typ)
 	local format = "0x%08x"
-	local enumType = "c_uint"
+	local enumType = "u32"
 	if typ.bits == 64 then
 		format = "0x%016x"
-		enumType = "c_ulong"
+		enumType = "u64"
 	elseif typ.bits == 16 then
 		format = "0x%04x"
-		enumType = "c_ushort"
+		enumType = "u16"
 	end
 
-	yield("pub const " .. typ.name .. "Flags = enum(" .. enumType .. ") {")
+	local name = typ.name .. "Flags"
+	yield("pub const " .. name .. " = " .. enumType .. ";")
 
 	for idx, flag in ipairs(typ.flag) do
-
 		if flag.comment ~= nil then
 			if idx ~= 1 then
 				yield("")
 			end
 
 			for _, comment in ipairs(flag.comment) do
-				yield("    /// " .. comment)
+				yield("/// " .. comment)
 			end
 		end
 
 		local flagName = flag.name:gsub("_", "")
-		yield("    "
+		yield("pub const " .. name .. "_"
 			.. flagName
+			.. ": "
+			.. name
 			.. string.rep(" ", 22 - #(flagName))
 			.. " = "
 			.. string.format(flag.format or format, flag.value)
-			.. ","
+			.. ";"
 		)
 	end
 
@@ -199,8 +202,6 @@ local function FlagBlock(typ)
 			.. string.format(format, flag.mask)
 		)
 	end
-
-	yield("};")
 end
 
 local function lastCombinedFlagBlock()
@@ -214,22 +215,8 @@ local function lastCombinedFlagBlock()
 	end
 end
 
-local enum = {}
-
-local function convert_array(member)
-	if string.find(member.array, "::") then
-		return string.format("[%d]", enum[member.array])
-	else
-		return member.array
-	end
-end
-
 local function convert_struct_member(member)
-	if member.array then
-		return member.name .. ": " .. convert_array(member) .. convert_struct_type(member)
-	else
-		return member.name .. ": " .. convert_struct_type(member)
-	end
+	return member.name .. ": " .. convert_struct_type(member)
 end
 
 local namespace = ""
@@ -319,13 +306,11 @@ function converter.types(typ)
 			FlagBlock(typ)
 		end
 	elseif typ.struct ~= nil then
-
 		local skip = false
 
 		if typ.namespace ~= nil then
 			if namespace ~= typ.namespace then
 				yield("pub const " .. typ.namespace .. " = extern struct {")
-				-- yield("{")
 				namespace = typ.namespace
 				indent = "    "
 			end
@@ -336,14 +321,15 @@ function converter.types(typ)
 		end
 
 		if not skip then
-			yield(indent .. "pub const " .. typ.name .. " = extern struct {")
-			-- yield(indent .. "{")
+			if typ.name ~= "Encoder" then
+				yield(indent .. "pub const " .. typ.name .. " = extern struct {")
+			else
+				yield(indent .. "pub const " .. typ.name .. " = opaque {")
+			end
 		end
 
 		for _, member in ipairs(typ.struct) do
-			yield(
-			indent .. indent .. convert_struct_member(member) .. ","
-			)
+			yield(indent .. indent .. convert_struct_member(member) .. ",")
 		end
 
 		yield(indent .. "};")
@@ -381,7 +367,15 @@ function converter.funcs(func)
 
 	local args = {}
 	if func.this ~= nil then
-		args[1] = "self: [*c]" .. func.this_type.type
+		local ptr = "[*c]"
+		if func.const ~= nil then
+			ptr = ptr .. "const "
+		end
+
+		if func.this_type.type == "Encoder" then
+			ptr = "?*"
+		end
+		args[1] = "self: " .. ptr .. func.this_type.type
 	end
 	for _, arg in ipairs(func.args) do
 		local argName = arg.name:gsub("_", "")
@@ -392,12 +386,9 @@ function converter.funcs(func)
 			table.insert(args, convert_type(arg))
 		end
 	end
-	yield("pub extern fn bgfx_" .. func.cname .. "(" .. table.concat(args, ", ") .. ") callconv(.C) " .. convert_ret_type(func.ret) .. ";")
+	yield("pub extern fn bgfx_" .. func.cname .. "(" .. table.concat(args, ", ") .. ") " .. convert_ret_type(func.ret) .. ";")
 end
 
--- printtable("idl types", idl.types)
--- printtable("idl funcs", idl.funcs)
-
 function gen.write(codes, outputfile)
 	local out = assert(io.open(outputfile, "wb"))
 	out:write(codes)
@@ -406,7 +397,7 @@ function gen.write(codes, outputfile)
 end
 
 if (...) == nil then
-	-- run `lua bindings-cs.lua` in command line
+	-- run `lua bindings-zig.lua` in command line
 	print(gen.gen())
 end
 

Some files were not shown because too many files changed in this diff