Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5408,3 +5408,39 @@ pub fn exportFn(comptime name: []const u8, comptime func: anytype) CFn {
}
}.luaopen;
}

/// Generates a list of Lua function registrations (`FnReg`) from all
/// pub declarations in a struct type `T`.
///
/// Example:
///
/// ```zig
/// const MyLib = struct {
/// fn someHelper(...) { ... } // non-pub funcs skipped
/// pub fn foo(l: *Lua) void { ... }
/// pub fn bar(l: *Lua) void { ... }
///
/// };
///
/// const funcs = fnRegsFromType(MyLib);
/// lua.newLib(funcs);
/// lua.setGlobal("mylib"); // mylib.foo, mylib.bar now visible
/// ```
pub fn fnRegsFromType(comptime T: type) []const FnReg {
const decls = switch (@typeInfo(T)) {
inline .@"struct", .@"enum", .@"union", .@"opaque" => |info| info.decls,
else => @compileError("Type " ++ @typeName(T) ++ "does not allow declarations"),
};
comptime var funcs: []const FnReg = &.{};
inline for (decls) |d| {
if (@typeInfo(@TypeOf(@field(T, d.name))) == .@"fn") {
const reg: []const FnReg = &.{.{
.name = d.name,
.func = comptime wrap(@field(T, d.name)),
}};
funcs = funcs ++ reg;
}
}
const final = funcs;
return final;
}
33 changes: 33 additions & 0 deletions src/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3093,3 +3093,36 @@ test "checkNumeric and toNumeric" {
_ = std.mem.indexOf(u8, string, error_msg) orelse return error.BadErrorMessage;
}
}

test "function registration with fnRegsFromType" {
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();

const MyLib = struct {
pub fn add(l: *Lua) i32 {
const a = l.toInteger(1) catch 0;
const b = l.toInteger(2) catch 0;
l.pushInteger(a + b);
return 1;
}
pub fn neg(l: *Lua) i32 {
const a = l.toInteger(1) catch 0;
l.pushInteger(-a);
return 1;
}
};

// Construct function registration table at comptime from
// public decls on MyLib.

if (zlua.lang == .lua51 or zlua.lang == .luau or zlua.lang == .luajit) {
const funcs = comptime zlua.fnRegsFromType(MyLib);
lua.newTable();
lua.registerFns("fnregs", funcs);
} else {
lua.newLib(comptime zlua.fnRegsFromType(MyLib));
Copy link
Contributor Author

@nurpax nurpax Sep 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Is moving the comptime keyword to caller site the more idiomatic way in Zig? I guess I'm used to doing it the way I did it in the original version.

For example, I see comptime used in the library, not at call time, like in the initCompTime function here:

https://ziglang.org/documentation/master/std/#src/std/static_string_map.zig

I don't have a super strong opinion about, but I guess I have a preference for version that doesn't require the callsite to mark the call comptime.

BTW, I intentionally had this working only for structs because I didn't think this sort of "get decls from a namespace" makes so much sense e.g. in enums or unions. Or it may at least be that some public funcs in an enum likely aren't zlua wrappable. In this case, requiring a struct and giving an error may guide the user of this library to the right path. OTOH, if you had some cool uses in mind, then being overly restrictive is not that great idea either!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, i think what you did was right! the reason for comptime here is actually to move it to a testable part of the API contract. sorry for also unrelatedly fiddling with the implementation a little—I liked proving to myself that my code was equivalent to yours even though it doesn't have an explicitly comptime block.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be willing to accept a PR that moves comptime back to ziglua so that users of this API don't need to explicitly add comptime? That seems to be the way it's usually done in std library at least.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would, if you insist, but I claim that you are misunderstanding what I am saying.

  • the use of the comptime keyword in the caller here makes no difference and does not need to be done in consumer code.
  • the purpose of the comptime keyword in the test is so that in the future if the function body is altered to include runtime code, CI will fail
  • the code in the body of the function is executed entirely at compile time currently, even though there is no explicit comptime block. please read the function carefully: the main body of work that it does constructs a []const FnReg by modifying a comptime var. All of the code up to const final = funcs; therefore must be run at compile time.

Copy link
Contributor Author

@nurpax nurpax Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice! Thanks for clarifying and sorry for the confusion. I had indeed misunderstood what you said before. In this case there's no need to follow up with a PR. :)

lua.setGlobal("fnregs");
}
try lua.doString("res = fnregs.add(100, fnregs.neg(25))");
try expectEqual(75, lua.get(i32, "res"));
}
Loading