Skip to content

Commit 5c843b5

Browse files
committed
docs: update readme and docs for Zig 0.10.0
1 parent 6515d4b commit 5c843b5

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed

docs.md

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
# ziglua Documentation
1+
# Ziglua Documentation
22

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.*
44

55
This documentation provides
66

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
88
* Safety considerations
99
* `build.zig` documentation
1010
* Example code
1111

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.
1313

1414
## Moving from the C API to Zig
1515

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:
1717

1818
* Renaming or omitting functions
1919
* Modifying parameters (names and types) and return values
2020
* Additional helper functions have been added
2121

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
2323

2424
### Naming
2525

@@ -35,13 +35,13 @@ Because Zig best practice is to communicate intent precisely, some abbreviations
3535

3636
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.
3737

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.
3939

4040
## Safety
4141

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.
4343

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:
4545

4646
### Errors
4747

@@ -51,17 +51,17 @@ On the other hand, many functions either succeed or return an error. Rather than
5151

5252
### Booleans
5353

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.
5555

5656
### Slices
5757

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.
5959

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.)
6161

6262
### Enums
6363

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.
6565

6666
### Optionals
6767

@@ -83,37 +83,36 @@ Because `error` is a reserved word in Zig, these functions have been renamed to
8383

8484
### `string` vs `lstring`
8585

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.
8787

8888
The "string" variant functions are safe to use when the string is known to be null terminated without inner zeros.
8989

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+
9092
### `lua_pushvfstring`
9193

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.
9695

9796
### `lua_tointegerx` and `lua_tonumberx`
9897

9998
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.
10099

101100
### `lua_pushliteral`
102101

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.
104103

105104
### `pcall`
106105

107106
Both `lua_pcall` and `lua_pcallk` are expanded to `protectedCall` and `protectedCallCont` for readability.
108107

109108
## Build Documentation
110109

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:
112111

113112
1. `@import()` the `build.zig` file
114-
2. `addPackage()` the ziglua api
113+
2. `addPackage()` the Ziglua api
115114

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.
117116

118117
```zig
119118
const ziglua = @import("lib/ziglua/build.zig");
@@ -126,7 +125,7 @@ pub fn build(b: *Builder) void {
126125

127126
This makes the `ziglua` package available in your project. Access with `@import("ziglua")`.
128127

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()`:
130129

131130
* `.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.
132131

@@ -142,7 +141,7 @@ exe.addPackage(ziglua.linkAndPackage(b, exe, .{ .use_apicheck = true, .version =
142141

143142
## Examples
144143

145-
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.
146145

147146
### Simple Lua Interpreter
148147

@@ -186,14 +185,14 @@ pub fn main() anyerror!void {
186185
187186
// Compile a line of Lua code
188187
lua.loadString(buffer[0..len :0]) catch {
189-
try stdout.print("{s}\n", .{lua.toString(-1)});
188+
try stdout.print("{s}\n", .{lua.toString(-1) catch unreachable});
190189
lua.pop(1);
191190
continue;
192191
};
193192
194193
// Execute a line of Lua code
195194
lua.protectedCall(0, 0, 0) catch {
196-
try stdout.print("{s}\n", .{lua.toString(-1)});
195+
try stdout.print("{s}\n", .{lua.toString(-1) catch unreachable});
197196
lua.pop(1);
198197
};
199198
}
@@ -204,6 +203,8 @@ This shows a basic interpreter that reads a string from stdin. That string is pa
204203

205204
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.
206205

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+
207208
### Calling a Zig function
208209

209210
Registering a Zig function to be called from Lua is simple
@@ -236,7 +237,7 @@ pub fn main() anyerror!void {
236237
// assert that this function call will not error
237238
lua.protectedCall(2, 1, 0) catch unreachable;
238239
239-
std.debug.print("the result: {}\n", .{lua.toInteger(1)});
240+
std.debug.print("the result: {}\n", .{lua.toInteger(1) catch unreachable});
240241
}
241242
```
242243

readme.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# Ziglua
22

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.
44

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
77
* **module**: used to create shared Lua modules that can be loaded at runtime in other Lua-based software
88

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+
911
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.
1012

1113
* [Docs](https://github.com/natecraddock/ziglua/blob/master/docs.md)
@@ -19,15 +21,15 @@ In a nutshell, Ziglua is a simple wrapper around the C API you would get by usin
1921
* Null-terminated slices instead of C strings
2022
* Type-checked enums for parameters and return values
2123
* 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`)
2325

2426
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.
2527

2628
If you want something higher-level (but doesn't expose the full API), perhaps try [zoltan](https://github.com/ranciere/zoltan).
2729

2830
## Getting Started
2931

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):
3133

3234
```zig
3335
// use the path to the Ziglua build.zig file
@@ -39,7 +41,7 @@ pub fn build(b: *Builder) void {
3941
}
4042
```
4143

42-
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:
4345

4446
```zig
4547
const std = @import("std");
@@ -56,7 +58,7 @@ pub fn main() anyerror!void {
5658
defer lua.deinit();
5759
5860
lua.pushInteger(42);
59-
std.debug.print("{}\n", .{lua.toInteger(1)});
61+
std.debug.print("{}\n", .{try lua.toInteger(1)});
6062
}
6163
```
6264

@@ -66,7 +68,7 @@ See [docs.md](https://github.com/natecraddock/ziglua/blob/master/docs.md) for do
6668

6769
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`).
6870

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.
7072

7173
## Acknowledgements
7274

0 commit comments

Comments
 (0)