You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are currently two additional options that can be passed to `b.dependency()`:
51
+
52
+
*`.version`: Set the Lua version to build and embed. Defaults to `.lua_54`. Possible values are `.lua_51`, `.lua_52`, `.lua_53`, and `.lua_54`.
53
+
*`.shared`: Defaults to `false` for embedding in a Zig program. Set to `true` to dynamically link the Lua source code (useful for creating shared modules).
54
+
55
+
For example, here is a `b.dependency()` call that and links against a shared Lua 5.2 library:
56
+
57
+
```zig
58
+
const ziglua = b.dependency("ziglua", .{
59
+
.target = target,
60
+
.optimize = optimize,
61
+
.version = .lua52,
62
+
.shared = true,
63
+
});
64
+
```
65
+
13
66
## Moving from the C API to Zig
14
67
15
68
While efforts have been made to keep the Ziglua API similar to the C API, many changes have been made including:
@@ -24,17 +77,17 @@ With this in mind, here are some general guidelines to help when moving from the
24
77
25
78
In general, most functions are named similarly to the original C functions. The `lua_` and `luaL_` prefixes have been removed, because all functions are in the `Lua` struct namespace. Additionally, all functions are in `camelCase` to match Zig naming style.
26
79
27
-
In the few cases when the [auxiliary library](https://www.lua.org/manual/5.4/manual.html#5) functions have the same name as a normal C API function, the suffix `Aux` is added to the function name to distinguish from the normal function.
80
+
In the few cases when the [auxiliary library](https://www.lua.org/manual/5.4/manual.html#5) functions have the same name as a normal C API function, the auxlib function is given a more descriptive name.
28
81
29
-
For example, the functions `lua_newstate` and `luaL_newstate` are translated to `Lua.newState` and `Lua.newStateAux` respectively.
82
+
For example, the functions `lua_newstate` and `luaL_newstate` are translated to `Lua.newState` and `Lua.newStateLibc` respectively.
30
83
31
84
Because Zig best practice is to communicate intent precisely, some abbreviations are expanded to make names more clear, like renaming `pcall` to `protectedCall`.
32
85
33
86
### Lua Initialization
34
87
35
88
In the C API, there are two functions provided to initialize the main Lua state: `lua_newstate` and `luaL_newstate`. The former requires passing an allocator function to be used by Lua for all memory allocations, while the latter uses the default libc allocator.
36
89
37
-
Ziglua provides a third option with the `Lua.init(Allocator)` function, which accepts a Zig allocator. All three functions are available depending on your needs, but most likely you will want to use the `Lua.init(Allocator)` function. If you have special requirements for allocation, then `Lua.newState` would be useful. `Lua.newStateAux` is available if you wish to use the default libc allocator.
90
+
Ziglua provides a third option with the `Lua.init(Allocator)` function, which accepts a Zig allocator. All three functions are available depending on your needs, but most likely you will want to use the `Lua.init(Allocator)` function. If you have special requirements for allocation, then `Lua.newState` would be useful. `Lua.newStateLibc` is available if you wish to use the default libc allocator.
38
91
39
92
## Safety
40
93
@@ -44,7 +97,7 @@ Here is a list of the features Ziglua uses for greater safety:
44
97
45
98
### Errors
46
99
47
-
Many functions now return Zig errors rather than an integer code. The compiler will then ensure that the error is handled, or ignored. There are specific error types like `ziglua.Error.Runtime` for errors that have a specific meaning.
100
+
Many functions now return Zig errors rather than an integer code. The Zig compiler will then ensure that the error is handled, or ignored. There are specific error types like `ziglua.Error.Runtime` for errors that have a specific meaning.
48
101
49
102
On the other hand, many functions either succeed or return an error. Rather than returning a boolean success code, these functions return the generic `ziglua.Error.Fail` to indicate failure. The type of failure can be determined in the context of the function called.
50
103
@@ -103,163 +156,3 @@ This is a macro for `lua_pushstring`, so use `Lua.pushString()` instead.
103
156
### `pcall`
104
157
105
158
Both `lua_pcall` and `lua_pcallk` are expanded to `protectedCall` and `protectedCallCont` for readability.
106
-
107
-
## Build Documentation
108
-
109
-
Example `build.zig.zon` file. The hash in the url is the hash of the commit you are using.
There are currently two additional options that can be passed to `b.dependency()`:
145
-
146
-
*`.version`: Set the Lua version to build and embed. Defaults to `.lua_54`. Possible values are `.lua_51`, `.lua_52`, `.lua_53`, and `.lua_54`.
147
-
148
-
*`.shared`: Defaults to `false` for embedding in a Zig program. Set to `true` to dynamically link the Lua source code (useful for creating shared modules).
149
-
150
-
For example, here is a `b.dependency()` call that and links against a shared Lua 5.2 library:
151
-
152
-
```zig
153
-
const ziglua = b.dependency("ziglua", .{
154
-
.target = target,
155
-
.optimize = optimize,
156
-
.version = .lua52,
157
-
.shared = true,
158
-
});
159
-
```
160
-
161
-
## Examples
162
-
163
-
Here are more thorough examples that show off the Ziglua bindings in context. All examples use the previously documented [`build.zig`](#build-documentation) setup.
164
-
165
-
### Simple Lua Interpreter
166
-
167
-
This is a modified program from _Programming In Lua 4th Edition_
168
-
169
-
```zig
170
-
const std = @import("std");
171
-
const ziglua = @import("ziglua");
172
-
173
-
const Lua = ziglua.Lua;
174
-
175
-
pub fn main() anyerror!void {
176
-
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
177
-
const allocator = gpa.allocator();
178
-
defer _ = gpa.deinit();
179
-
180
-
// Initialize The Lua vm and get a reference to the main thread
181
-
var lua = try Lua.init(allocator);
182
-
defer lua.deinit();
183
-
184
-
// Open the standard libraries
185
-
lua.openLibs();
186
-
187
-
var stdin = std.io.getStdIn().reader();
188
-
var stdout = std.io.getStdOut().writer();
189
-
190
-
var buffer: [256]u8 = undefined;
191
-
while (true) {
192
-
_ = try stdout.write("> ");
193
-
194
-
// Read a line of input
195
-
const len = try stdin.read(&buffer);
196
-
if (len == 0) break; // EOF
197
-
if (len >= buffer.len - 1) {
198
-
try stdout.print("error: line too long!\n", .{});
199
-
continue;
200
-
}
201
-
202
-
// Ensure the buffer is null-terminated so the Lua API can read the length
This shows a basic interpreter that reads a string from stdin. That string is parsed and compiled as Lua code and then executed.
222
-
223
-
Notice that the functions `lua.loadString()` and `lua.protectedCall()` return errors that must be handled, here printing the error message that was placed on the stack.
224
-
225
-
The `lua.toString()` calls are both followed with `catch unreachable` in this example. This function can fail if the value at the given index is not a string. The stack should contain a Lua error string, so in this example we assert that it will not fail. We also could have passed a generic error string with `catch "Error"`
226
-
227
-
### Calling a Zig function
228
-
229
-
Registering a Zig function to be called from Lua is simple
230
-
231
-
```zig
232
-
const std = @import("std");
233
-
const ziglua = @import("ziglua");
234
-
235
-
const Lua = ziglua.Lua;
236
-
237
-
fn adder(lua: *Lua) i32 {
238
-
const a = lua.toInteger(1) catch 0;
239
-
const b = lua.toInteger(2) catch 0;
240
-
lua.pushInteger(a + b);
241
-
return 1;
242
-
}
243
-
244
-
pub fn main() anyerror!void {
245
-
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
Notice the use of `ziglua.wrap`. This is because the function `fn adder(*Lua) i32` is a `ziglua.ZigFn`, when the `lua.pushFunction` call expects a `ziglua.CFn` type.
264
-
265
-
The `ziglua.wrap` function generates a new function at compile time that wraps the Zig function in a function compatible with the Lua C API. This could be done automatically by `lua.pushFunction`, but that would require the parameter to be comptime-known. The call to `ziglua.wrap` is slightly more verbose, but has the benefit of being more flexible.
0 commit comments