-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update wrap functions to handle error unions #47
Comments
My implementation of autoPushFunction automatically does this. if (@typeInfo(info.Fn.return_type.?) == .ErrorUnion) {
const result = @call(.auto, function, parameters) catch |err| {
lua.raiseErrorStr(@errorName(err), .{});
};
lua.pushAny(result);
} else {
const result = @call(.auto, function, parameters);
lua.pushAny(result);
} |
Thanks for the example! Really helpful. I got this to work with const inner = struct {
fn inner(l: *Lua) anyerror!i32 {
const allocator = l.allocator();
const num = try l.toInteger(1);
// Use the allocator
const nums = try allocator.alloc(i32, @intCast(num));
defer allocator.free(nums);
// Do something pointless to use the slice
var sum: i32 = 0;
for (nums, 0..) |*n, i| n.* = @intCast(i);
for (nums) |n| sum += n;
l.pushInteger(sum);
return 1;
}
}.inner;
lua.pushFunction(ziglua.wrap(inner)); Probably not the end of the world, but I would prefer it if the function could be left as |
Solved it 🚀 |
natecraddock
added a commit
that referenced
this issue
Oct 12, 2024
Any ziglua.wrap supported function that has a *Lua parameter now allows using error unions in the return type. Any Zig errors that are returned will be raised as a Lua error. Closes #47
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would sometimes be useful to use
try
in a wrapped zig function and have that propagate as a Lua error. This should be pretty easy to do with comptimeThe text was updated successfully, but these errors were encountered: