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
Copy file name to clipboardExpand all lines: docs.md
+28-27Lines changed: 28 additions & 27 deletions
Original file line number
Diff line number
Diff line change
@@ -1,25 +1,25 @@
1
-
# ziglua Documentation
1
+
# Ziglua Documentation
2
2
3
-
*To avoid a duplication of efforts, ziglua does not contain full documentation on the Lua C API. Please refer to [the Lua C API Documentation](https://www.lua.org/manual/5.4/manual.html#4) for full details.*
3
+
*To avoid a duplication of efforts, Ziglua does not contain full documentation on the Lua C API. Please refer to the Lua C API Documentation for full details.*
4
4
5
5
This documentation provides
6
6
7
-
* An overview of ziglua's structure and changes from the C API
7
+
* An overview of Ziglua's structure and changes from the C API
8
8
* Safety considerations
9
9
*`build.zig` documentation
10
10
* Example code
11
11
12
-
Documentation on each individual function is found in the [ziglua.zig](https://github.com/natecraddock/ziglua/blob/master/src/ziglua.zig)source code.
12
+
Documentation on each individual function is found in the source code.
13
13
14
14
## Moving from the C API to Zig
15
15
16
-
While efforts have been made to keep the ziglua API similar to the C API, many changes have been made including:
16
+
While efforts have been made to keep the Ziglua API similar to the C API, many changes have been made including:
17
17
18
18
* Renaming or omitting functions
19
19
* Modifying parameters (names and types) and return values
20
20
* Additional helper functions have been added
21
21
22
-
With this in mind, here are some general guidelines to help guide when moving from the C to Zig APIs
22
+
With this in mind, here are some general guidelines to help when moving from the C to Zig APIs
23
23
24
24
### Naming
25
25
@@ -35,13 +35,13 @@ Because Zig best practice is to communicate intent precisely, some abbreviations
35
35
36
36
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.
37
37
38
-
Ziglua provides a third option with the `Lua.init(Allocator)` function, which accepts a traditional 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.
38
+
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.
39
39
40
40
## Safety
41
41
42
-
The ziglua API aims to be safer than the traditional C API. That said, the way that Lua operates means that Zig cannot protect you from all errors due to the use of `longjmp` in C.
42
+
The Ziglua API aims to be safer than the traditional C API. That said, the way that Lua operates means that Zig cannot protect you from all errors due to the use of `longjmp` in C.
43
43
44
-
Here is a list of the types of features ziglua uses to ensure greater safety:
44
+
Here is a list of the features Ziglua uses for greater safety:
45
45
46
46
### Errors
47
47
@@ -51,17 +51,17 @@ On the other hand, many functions either succeed or return an error. Rather than
51
51
52
52
### Booleans
53
53
54
-
Functions that return or accept C boolean integers now use the Zig `bool` type to increase type safety.
54
+
Functions that return or accept C boolean integers now use the Zig `bool` type.
55
55
56
56
### Slices
57
57
58
-
In cases where C functions use separate pointers and ints to keep track of strings, ziglua uses a Zig slice to keep the data together.
58
+
In cases where C functions use separate pointers and ints to keep track of strings, Ziglua uses a Zig slice to keep the data together.
59
59
60
-
The slices are typed to indicate the contents (zero-terminated, raw bytes, etc)
60
+
The slices are typed to indicate the contents (zero-terminated, raw bytes, etc.)
61
61
62
62
### Enums
63
63
64
-
ziglua uses enums instead of integer codes or strings to prevent passing an invalid value to a function.
64
+
Ziglua uses enums instead of integer codes or strings to prevent passing an invalid value to a function.
65
65
66
66
### Optionals
67
67
@@ -83,37 +83,36 @@ Because `error` is a reserved word in Zig, these functions have been renamed to
83
83
84
84
### `string` vs `lstring`
85
85
86
-
The "string" variant functions vs the "lstring" functions only differ by returning the length of the string. In ziglua, the lstring functions are all named "bytes" instead. For example, `lua_tolstring` is `Lua.toBytes`. This is because these functions are typically used in cases when the string _might_ contain zeros before the null-terminating zero.
86
+
The "string" variant functions vs the "lstring" functions only differ by returning the length of the string. In Ziglua, the lstring functions are all named "bytes" instead. For example, `lua_tolstring` is `Lua.toBytes`. This is because these functions are typically used in cases when the string _might_ contain zeros before the null-terminating zero.
87
87
88
88
The "string" variant functions are safe to use when the string is known to be null terminated without inner zeros.
89
89
90
+
The length of the returned string is almost always needed, so `Lua.toString() returns a zero-terminated Zig slice of the bytes with the correct length.
91
+
90
92
### `lua_pushvfstring`
91
93
92
-
This function has been omitted because Zig does not have a va_list type, and `Lua.pushFString` works well
93
-
enough for string formatting if variadic args are really needed.
94
-
95
-
The length of the returned string is almost always needed, so `Lua.toString() returns a zero-terminated Zig slice of the bytes with the correct length.
94
+
This function has been omitted because Zig does not have a va_list type, and `Lua.pushFString` works well enough for string formatting if variadic args are really needed.
96
95
97
96
### `lua_tointegerx` and `lua_tonumberx`
98
97
99
98
Both of these functions accept an `isnum` return parameter to indicate if the conversion to number was successful. In the Zig version, both functions return either the number, or an error indicating the conversion was unsuccessful, and the `isnum` parameter is omitted.
100
99
101
100
### `lua_pushliteral`
102
101
103
-
This is just a macro for `lua_pushstring`, so just use `Lua.pushString()` instead.
102
+
This is a macro for `lua_pushstring`, so use `Lua.pushString()` instead.
104
103
105
104
### `pcall`
106
105
107
106
Both `lua_pcall` and `lua_pcallk` are expanded to `protectedCall` and `protectedCallCont` for readability.
108
107
109
108
## Build Documentation
110
109
111
-
When integrating ziglua into your projects, the following two statements are required:
110
+
When integrating Ziglua into your projects, the following two statements are required:
112
111
113
112
1.`@import()` the `build.zig` file
114
-
2.`addPackage()` the ziglua api
113
+
2.`addPackage()` the Ziglua api
115
114
116
-
Note that this _must_ be done after setting the target and build mode, otherwise ziglua will not know that information.
115
+
Note that this _must_ be done after setting the target and build mode, otherwise Ziglua will not know that information.
This makes the `ziglua` package available in your project. Access with `@import("ziglua")`.
128
127
129
-
There are currently three options that can be passed in the third argument to `ziglua.link()`:
128
+
There are currently three options that can be passed in the third argument to `ziglua.linkAndPackage()`:
130
129
131
130
*`.use_apicheck`: defaults to **false**. When **true** defines the macro `LUA_USE_APICHECK` in debug builds. See [The C API docs](https://www.lua.org/manual/5.4/manual.html#4) for more information on this macro.
Here are more thorough examples that show off the ziglua bindings in context. All examples use previously documented [`build.zig`](#build-documentation) setup.
144
+
Here are more thorough examples that show off the Ziglua bindings in context. All examples use the previously documented [`build.zig`](#build-documentation) setup.
@@ -204,6 +203,8 @@ This shows a basic interpreter that reads a string from stdin. That string is pa
204
203
205
204
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.
206
205
206
+
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"`
207
+
207
208
### Calling a Zig function
208
209
209
210
Registering a Zig function to be called from Lua is simple
Copy file name to clipboardExpand all lines: readme.md
+10-8Lines changed: 10 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,13 @@
1
1
# Ziglua
2
2
3
-
A Zig library that provides a complete yet lightweight wrapper around the [Lua C API](https://www.lua.org/manual/5.4/manual.html#4). Ziglua currently supports the latest releases of Lua 5.1, 5.2, 5.3, and 5.4.
3
+
A Zig library that provides a complete and lightweight wrapper around the [Lua C API](https://www.lua.org/manual/5.4/manual.html#4). Ziglua currently supports the latest releases of Lua 5.1, 5.2, 5.3, and 5.4.
4
4
5
-
Ziglua offers two approaches as a library:
6
-
***embedded**: used to embed the Lua VM in a Zig program
5
+
The Ziglua library offers two use cases:
6
+
***embedded**: used to statically embed the Lua VM in a Zig program
7
7
***module**: used to create shared Lua modules that can be loaded at runtime in other Lua-based software
8
8
9
+
In both cases, Ziglua will compile Lua from source and link against your Zig code making it easy to create software that integrates with Lua without requiring Lua libraries installed on your system.
10
+
9
11
Like the Lua C API, the Ziglua API "emphasizes flexibility and simplicity... common tasks may involve several API calls. This may be boring, but it gives us full control over all the details" (_Programming In Lua 4th Edition_). However, Ziglua takes advantage of Zig's features to make it easier and safer to interact with the Lua API.
@@ -19,15 +21,15 @@ In a nutshell, Ziglua is a simple wrapper around the C API you would get by usin
19
21
* Null-terminated slices instead of C strings
20
22
* Type-checked enums for parameters and return values
21
23
* Compiler-enforced checking of optional pointers
22
-
*More precise types (e.g. `bool` instead of `int`)
24
+
*Better types in many cases (e.g. `bool` instead of `int`)
23
25
24
26
While there are some helper functions added to complement the C API, Ziglua aims to remain low-level. This allows full access to the Lua API through a layer of Zig's improvements over C.
25
27
26
28
If you want something higher-level (but doesn't expose the full API), perhaps try [zoltan](https://github.com/ranciere/zoltan).
27
29
28
30
## Getting Started
29
31
30
-
Adding Ziglua to your project is easy. First add this repo as a git submodule, or copy the source into your repo. Then add the following to your `build.zig` file (assuming cloned/copied into a `lib/` subdirectory):
32
+
Adding Ziglua to your project takes only a couple of steps. First add this repo as a git submodule, or copy the source into your project (one day the Zig package manager will make this easier). Then add the following to your `build.zig` file (assuming cloned/copied into a `lib/` subdirectory):
This will compile the Lua C sources and statically link with your project. Then simply import the `ziglua` package into your code! Here is a simple example that pushes and inspects an integer on the Lua stack:
44
+
This will compile the Lua C sources and statically link with your project. Then simply import the `ziglua` package into your code. Here is a simple example that pushes and inspects an integer on the Lua stack:
43
45
44
46
```zig
45
47
const std = @import("std");
@@ -56,7 +58,7 @@ pub fn main() anyerror!void {
56
58
defer lua.deinit();
57
59
58
60
lua.pushInteger(42);
59
-
std.debug.print("{}\n", .{lua.toInteger(1)});
61
+
std.debug.print("{}\n", .{try lua.toInteger(1)});
60
62
}
61
63
```
62
64
@@ -66,7 +68,7 @@ See [docs.md](https://github.com/natecraddock/ziglua/blob/master/docs.md) for do
66
68
67
69
Nearly all functions, types, and constants in the C API have been wrapped in Ziglua. Only a few exceptions have been made when the function doesn't make sense in Zig (like functions using `va_list`).
68
70
69
-
All functions have been type checked, but only the standard C API has been tested fully. Ziglua should be relatively stable and safe to use now, but is still new and changing frequently.
71
+
Nearly all functions have associated Zig tests. Ziglua should be relatively stable and safe to use now, but I am still polishing things and function signatures may change from time to time.
0 commit comments