Skip to content

Commit 606a0b2

Browse files
committed
Compact README documentation
Simplified code examples by removing redundant imports and verbose patterns. Converted multi-line functions to one-liners and streamlined result unwrapping for better readability.
1 parent 34f0abf commit 606a0b2

File tree

1 file changed

+50
-120
lines changed

1 file changed

+50
-120
lines changed

README.md

Lines changed: 50 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -59,35 +59,22 @@ The documentation is automatically updated on every change to the `main` branch.
5959
6060
### Basic Usage
6161

62-
The following example demonstrates some basic use cases.
63-
64-
- `Lua` takes optional Zig allocator.
65-
- Global table `_G` is available via `globals()` table.
66-
- `eval` is a helper function that compiles Lua code to Luau bytecode and executes it.
67-
- `set` and `get` are used to pass and retrieve data.
68-
6962
```zig
70-
const std = @import("std");
71-
const luaz = @import("luaz");
72-
const assert = std.debug.assert;
73-
7463
pub fn main() !void {
7564
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
7665
defer _ = gpa.deinit();
7766
78-
var lua = try Lua.init(&gpa.allocator); // Create Lua state with custom allocator
79-
defer lua.deinit(); // Clean up Lua state
67+
var lua = try Lua.init(&gpa.allocator); // Custom allocator (or null for default)
68+
defer lua.deinit();
8069
81-
// Set a global variable
70+
// Set and get globals
8271
try lua.globals().set("greeting", "Hello from Zig!");
83-
84-
// Get and verify the global variable
8572
const value = try lua.globals().get("greeting", []const u8);
86-
assert(std.mem.eql(u8, value.?, "Hello from Zig!"));
73+
std.debug.assert(std.mem.eql(u8, value.?, "Hello from Zig!"));
8774
88-
// Evaluate Lua code and get result
75+
// Evaluate Lua code
8976
const result = try lua.eval("return 2 + 3 * 4", .{}, i32);
90-
assert(result.ok.? == 14);
77+
std.debug.assert(result.ok.? == 14);
9178
}
9279
```
9380

@@ -96,160 +83,106 @@ pub fn main() !void {
9683
9784
### Struct and Array Tables
9885

99-
Zig structs and arrays are automatically converted to Lua tables:
86+
Zig structs and arrays automatically convert to Lua tables:
10087

10188
```zig
102-
const std = @import("std");
103-
const luaz = @import("luaz");
104-
10589
const Point = struct { x: f64, y: f64 };
10690
10791
pub fn main() !void {
10892
var lua = try Lua.init(null);
10993
defer lua.deinit();
11094
111-
// Struct becomes a Lua table with field names as keys
112-
const point = Point{ .x = 10.5, .y = 20.3 };
113-
try lua.globals().set("point", point);
114-
115-
// Array becomes a Lua table with 1-based integer indices
116-
const numbers = [_]i32{ 1, 2, 3, 4, 5 };
117-
try lua.globals().set("numbers", numbers);
95+
// Struct → table with field names as keys
96+
try lua.globals().set("point", Point{ .x = 10.5, .y = 20.3 });
97+
98+
// Array → table with 1-based indices
99+
try lua.globals().set("numbers", [_]i32{ 1, 2, 3, 4, 5 });
118100
119101
// Access from Lua
120-
const x_result = try lua.eval("return point.x", .{}, f64);
121-
const x = x_result.ok.?; // 10.5
122-
const first_result = try lua.eval("return numbers[1]", .{}, i32);
123-
const first = first_result.ok.?; // 1
124-
const length_result = try lua.eval("return #numbers", .{}, i32);
125-
const length = length_result.ok.?; // 5
102+
const x = (try lua.eval("return point.x", .{}, f64)).ok.?; // 10.5
103+
const first = (try lua.eval("return numbers[1]", .{}, i32)).ok.?; // 1
104+
const length = (try lua.eval("return #numbers", .{}, i32)).ok.?; // 5
126105
}
127106
```
128107

129108
### Function Calls
130109

131-
Both Lua functions can be called from Zig and Zig functions from Lua with automatic type conversion and argument
132-
handling.
110+
Seamless bidirectional function calls with automatic type conversion:
133111

134112
```zig
135-
const std = @import("std");
136-
const luaz = @import("luaz");
137-
const assert = std.debug.assert;
138-
139-
fn sum(a: i32, b: i32) i32 {
140-
return a + b;
141-
}
113+
fn sum(a: i32, b: i32) i32 { return a + b; }
142114
143115
pub fn main() !void {
144-
var lua = try Lua.init(null); // Use default allocator
116+
var lua = try Lua.init(null);
145117
defer lua.deinit();
146118
147-
// Register Zig function in Lua
119+
// Register Zig function → call from Lua
148120
try lua.globals().set("sum", sum);
121+
std.debug.assert((try lua.eval("return sum(10, 20)", .{}, i32)).ok.? == 30);
149122
150-
// Call Zig function from Lua
151-
const result1 = try lua.eval("return sum(10, 20)", .{}, i32);
152-
assert(result1.ok.? == 30);
153-
154-
// Define Lua function
123+
// Define Lua function → call from Zig
155124
_ = try lua.eval("function multiply(x, y) return x * y end", .{}, void);
125+
std.debug.assert((try lua.globals().call("multiply", .{6, 7}, i32)).ok.? == 42);
156126
157-
// Call Lua function from Zig
158-
const result2 = try lua.globals().call("multiply", .{6, 7}, i32);
159-
assert(result2.ok.? == 42);
160-
161-
// Closures with captured values
127+
// Closures with upvalues
162128
const table = lua.createTable(.{});
163129
defer table.deinit();
164130
165131
fn getGlobal(upv: Lua.Upvalues(*Lua), key: []const u8) !i32 {
166132
return try upv.value.globals().get(key, i32) orelse 0;
167133
}
168-
const lua_ptr = @constCast(&lua);
169-
try table.setClosure("getGlobal", lua_ptr, getGlobal);
134+
try table.setClosure("getGlobal", @constCast(&lua), getGlobal);
170135
try lua.globals().set("funcs", table);
171136
try lua.globals().set("myValue", @as(i32, 123));
172-
173-
const result3 = try lua.eval("return funcs.getGlobal('myValue')", .{}, i32);
174-
assert(result3.ok.? == 123);
137+
std.debug.assert((try lua.eval("return funcs.getGlobal('myValue')", .{}, i32)).ok.? == 123);
175138
}
176139
```
177140

178141
### UserData Integration
179142

180-
luaz has automatic compile-time binding generation for user data. It supports constructors, static and instance
181-
methods. If a struct has `deinit`, it'll be automatically invoked on garbage collection.
143+
Automatic compile-time binding generation with metamethod support:
182144

183145
```zig
184-
const std = @import("std");
185-
const luaz = @import("luaz");
186-
const assert = std.debug.assert;
187-
188146
const Counter = struct {
189147
value: i32,
190148
191-
pub fn init(initial: i32) Counter {
192-
return Counter{ .value = initial };
193-
}
194-
195-
pub fn deinit(self: *Counter) void {
196-
std.log.info("Counter with value {} being destroyed", .{self.value});
197-
}
198-
199-
pub fn getMaxValue() i32 {
200-
return std.math.maxInt(i32);
201-
}
202-
203-
pub fn increment(self: *Counter, amount: i32) i32 {
204-
self.value += amount;
205-
return self.value;
206-
}
207-
208-
pub fn getValue(self: *const Counter) i32 {
209-
return self.value;
149+
pub fn init(initial: i32) Counter { return .{ .value = initial }; }
150+
pub fn deinit(self: *Counter) void { _ = self; } // Auto-called on GC
151+
pub fn getMaxValue() i32 { return std.math.maxInt(i32); } // Static method
152+
pub fn increment(self: *Counter, amount: i32) i32 {
153+
self.value += amount;
154+
return self.value;
210155
}
211-
212-
// Metamethods for arithmetic operations
213-
pub fn __add(self: Counter, other: i32) Counter {
214-
return Counter{ .value = self.value + other };
215-
}
216-
217-
pub fn __tostring(self: Counter) []const u8 {
156+
pub fn getValue(self: *const Counter) i32 { return self.value; }
157+
158+
// Metamethods
159+
pub fn __add(self: Counter, other: i32) Counter { return .{ .value = self.value + other }; }
160+
pub fn __len(self: Counter) i32 { return self.value; }
161+
pub fn __tostring(self: Counter) []const u8 {
218162
return std.fmt.allocPrint(std.heap.page_allocator, "Counter({})", .{self.value}) catch "Counter";
219163
}
220-
221-
pub fn __len(self: Counter) i32 {
222-
return self.value;
223-
}
224164
};
225165
226166
pub fn main() !void {
227-
var lua = try luaz.Lua.init(null);
167+
var lua = try Lua.init(null);
228168
defer lua.deinit();
229169
230-
// Register Counter type with Lua
231170
try lua.registerUserData(Counter);
232171
233172
_ = try lua.eval(
234-
\\local counter = Counter.new(10) -- Use constructor
235-
\\assert(counter:increment(5) == 15) -- Call instance method
236-
\\assert(counter:getValue() == 15) -- Get current value
237-
\\
238-
\\local max = Counter.getMaxValue() -- Call static method
239-
\\assert(max == 2147483647) -- Max i32 value
240-
\\
241-
\\-- Metamethods in action
242-
\\local new_counter = counter + 5 -- Uses __add metamethod
173+
\\local counter = Counter.new(10) -- Constructor
174+
\\assert(counter:increment(5) == 15) -- Instance method
175+
\\assert(Counter.getMaxValue() == 2147483647) -- Static method
176+
\\assert(#counter == 15) -- __len metamethod
177+
\\local new_counter = counter + 5 -- __add metamethod
243178
\\assert(new_counter:getValue() == 20)
244-
\\assert(#counter == 15) -- Uses __len metamethod
245-
\\print(tostring(counter)) -- Uses __tostring metamethod
246179
, .{}, void);
247180
}
248181
```
249182

250183
### String Buffer (StrBuf)
251184

252-
Efficient string building using Luau's StrBuf API with automatic memory management.
185+
Efficient string building using Luau's StrBuf API:
253186

254187
```zig
255188
fn buildGreeting(upv: Lua.Upvalues(*Lua), name: []const u8, age: i32) !Lua.StrBuf {
@@ -265,19 +198,16 @@ fn buildGreeting(upv: Lua.Upvalues(*Lua), name: []const u8, age: i32) !Lua.StrBu
265198
266199
// Register and call from Lua
267200
try lua.globals().setClosure("buildGreeting", &lua, buildGreeting);
268-
const result = try lua.eval("return buildGreeting('Alice', 25)", .{}, []const u8);
269-
const greeting = result.ok.?;
201+
const greeting = (try lua.eval("return buildGreeting('Alice', 25)", .{}, []const u8)).ok.?;
270202
```
271203

272-
> [!WARNING]
273-
> This library is still evolving and the API is not stable. Backward incompatible changes may be introduced up until the 1.0 release. Consider pinning to a specific commit or tag if you need stability.
274-
275204
## 🔧 Build Configuration
276205

277-
### Zig Version Policy
278-
Until Zig reaches 1.0 (stable release), luaz targets the latest released version of Zig. When updating to a new breaking Zig version, we create a branch for the previous version (e.g., `zig-0.14`) that can be checked out if the older version is required.
206+
> [!NOTE]
207+
> Until Zig reaches 1.0 (stable release), luaz targets the latest released version of Zig. When updating to a new breaking Zig version, we create a branch for the previous version (e.g., `zig-0.14`) that can be checked out if the older version is required.
279208
280-
Current supported Zig version: **0.15.1**
209+
> [!WARNING]
210+
> This library is still evolving and the API is not stable. Backward incompatible changes may be introduced up until the 1.0 release. Consider pinning to a specific commit or tag if you need stability.
281211
282212
### Vector Size
283213
By default, luaz is built with 4-component vectors. To customize:

0 commit comments

Comments
 (0)