Description
I think it would be nice if this library could provide some functionality that would automate some of the tedious work writing lua bindings.
For example,
theoretically, it should be possible to provide an api something like this
fn autoCall(lua: *Lua, comptime ReturnType: type, function_name: []const u8, args: anytype) !ReturnType;
const result = try lua.call(i32, "add", .{1, 2});
It should be possible to iterate over the args at comptime to make sure only types that can be represented in lua are passed. Then an inline for loop can be used to iterate over the args and call the appropriate lua_push
function.
Another example would be get and set functions for lua globals
fn get(lua: *Lua, comptime T: type, name: []const u8) !T;
fn set(lua: *Lua, name: []const u8, value: anytype) !void;
It might also be possible to automatically write the bindings for a zig function to be called by lua at comptime?
I'm not entirely sure how this would work. Here's some sort of pseudo code of what I was thinking about. But some more thought about how to make this work within the current limits of zigs comptime would still be needed. You would probably need to then generate a second interface function that has the proper pop
functions present so that it can properly call the zig function.
Not really sure but these are just some ideas
fn autoSetFunction(lua: *Lua, name: []const u8, comptime function: anytype) !void {
const type_info = @typeInfo(@TypeOf(function));
if(type_info != .Fn) @compileError("you must pass a function pointer");
//create an interface function based off of the parameter types
inline for(type_info.function.params) |parameter| {
switch(parameter.type) {
i32, u32 => //etc
}
//then pass the interface function to lua
lua.pushFunction(...);
}