1
1
//! Registering a Zig function to be called from Lua
2
2
3
3
const std = @import ("std" );
4
- const lua_wrapper = @import ("lua_wrapper " );
4
+ const zlua = @import ("zlua " );
5
5
6
6
// It can be convenient to store a short reference to the Lua struct when
7
7
// it is used multiple times throughout a file.
8
- const Lua = lua_wrapper .Lua ;
8
+ const Lua = zlua .Lua ;
9
9
10
10
// A Zig function called by Lua must accept a single *Lua parameter and must return an i32 (an error union is allowed)
11
11
// This is the Zig equivalent of the lua_CFunction typedef int (*lua_CFunction) (lua_State *L) in the C API
@@ -26,10 +26,10 @@ pub fn main() anyerror!void {
26
26
defer lua .deinit ();
27
27
28
28
// Push the adder function to the Lua stack.
29
- // Here we use lua_wrapper .wrap() to convert from a Zig function to the lua_CFunction required by Lua.
29
+ // Here we use zlua .wrap() to convert from a Zig function to the lua_CFunction required by Lua.
30
30
// This could be done automatically by pushFunction(), but that would require the parameter to be comptime-known.
31
- // The call to lua_wrapper .wrap() is slightly more verbose, but has the benefit of being more flexible.
32
- lua .pushFunction (lua_wrapper .wrap (adder ));
31
+ // The call to zlua .wrap() is slightly more verbose, but has the benefit of being more flexible.
32
+ lua .pushFunction (zlua .wrap (adder ));
33
33
34
34
// Push the arguments onto the stack
35
35
lua .pushInteger (10 );
@@ -47,7 +47,7 @@ pub fn main() anyerror!void {
47
47
std .debug .print ("the result: {}\n " , .{lua .toInteger (-1 ) catch unreachable });
48
48
49
49
// We can also register the function to a global and run from a Lua "program"
50
- lua .pushFunction (lua_wrapper .wrap (adder ));
50
+ lua .pushFunction (zlua .wrap (adder ));
51
51
lua .setGlobal ("add" );
52
52
53
53
// We need to open the base library so the global print() is available
0 commit comments