Skip to content

Commit b3cc713

Browse files
committed
Add support for buffer.addString(), buffer.addLString(), buffer.addValue()
1 parent 5fa3635 commit b3cc713

File tree

3 files changed

+96
-26
lines changed

3 files changed

+96
-26
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ you.
8686
| API | Support |
8787
|-----------------------------|---------------------------------------|
8888
| Lua C API (`lua_*`) | 🎉 100% coverage<sup>†</sup> (92/92) |
89-
| Auxilary Library (`luaL_*`) | 🤩 84% coverage (42/48) |
89+
| Auxilary Library (`luaL_*`) | 🤩 96% coverage (46/48) |
9090
| LuaJIT Extensions | *No plans to implement.* |
9191

9292
*†: Coroutine yield/resume is not yet part of the public `zig-luajit` Zig API, see [#6][ISSUE-6].*
@@ -224,10 +224,10 @@ The `zig-luajit` project has not yet reached the 1.0 release, the API is subject
224224
| C API Symbol | Available in `zig-luajit` |
225225
|----------------------------|-------------------------------------|
226226
| `luaL_addchar` | ☑️ `buffer.addChar()`|
227-
| `luaL_addlstring` ||
228227
| `luaL_addsize` | ☑️ `buffer.addSize()`|
229-
| `luaL_addstring` ||
230-
| `luaL_addvalue` ||
228+
| `luaL_addlstring` | ☑️ `buffer.addLString()`|
229+
| `luaL_addstring` | ☑️ `buffer.addString()`|
230+
| `luaL_addvalue` | ☑️ `buffer.addValue()`|
231231
| `luaL_argcheck` | ☑️📢 `lua.checkArgument()` |
232232
| `luaL_argerror` | ☑️📢 `lua.raiseErrorArgument()` |
233233
| `luaL_buffinit` | ☑️📢 `lua.initBuffer()`|

src/lual_api.zig

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,7 @@
33

44
// This file contains brainstorming and draft translations of the C API to Lua.
55

6-
/// Adds the string pointed to by `s` with length `l` to the buffer `B`. The string may contain embedded zeros.
7-
///
8-
/// From: `void luaL_addlstring(luaL_Buffer *B, const char *s, size_t l);`
9-
/// Refer to: https://www.lua.org/manual/5.1/manual.html#luaL_addlstring
10-
/// Stack Behavior: `[-0, +0, m]`
11-
pub fn addLString(buffer: *Buffer, s: [*]const u8, l: usize) void;
12-
13-
/// Adds the zero-terminated string pointed to by s to the buffer B.
14-
/// The string may not contain embedded zeros.
15-
///
16-
/// From: `void luaL_addstring(luaL_Buffer *B, const char *s);`
17-
/// Refer to: https://www.lua.org/manual/5.1/manual.html#luaL_addstring
18-
/// Stack Behavior: `[-0, +0, m]`
19-
pub fn addString(buffer: *Buffer, s: [*:0]const u8) void;
206

21-
/// Adds the value at the top of the stack to the buffer B (see https://www.lua.org/manual/5.1/manual.html#luaL_Buffer).
22-
/// Pops the value. This is the only function on string buffers that can (and must) be called with an extra
23-
/// element on the stack, which is the value to be added to the buffer.
24-
///
25-
/// From: `void luaL_addvalue(luaL_Buffer *B);`
26-
/// Refer to: https://www.lua.org/manual/5.1/manual.html#luaL_addvalue
27-
/// Stack Behavior: `[-1, +0, m]`
28-
pub fn addValue(buffer: *Buffer) void;
297

308
/// Creates a new Lua state using the standard C realloc function for memory allocation and sets a default
319
/// panic function that prints an error message to the standard error output in case of fatal errors.

src/root.zig

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2497,6 +2497,44 @@ pub const Lua = opaque {
24972497
}
24982498
}
24992499

2500+
/// Adds the zero-terminated string to the buffer.
2501+
/// The string may not contain embedded zeros.
2502+
///
2503+
/// From: `void luaL_addstring(luaL_Buffer *B, const char *s);`
2504+
/// Refer to: https://www.lua.org/manual/5.1/manual.html#luaL_addstring
2505+
/// Stack Behavior: `[-0, +0, m]`
2506+
pub fn addString(buffer: *Buffer, string: [*:0]const u8) void {
2507+
assert(buffer.p != null and buffer.L != null); // You must use `Lua.initBuffer(&Lua.Buffer)` before calling `Lua.Buffer.addString()`.
2508+
2509+
return c.luaL_addstring(@ptrCast(buffer), string);
2510+
}
2511+
2512+
/// Adds the string to the buffer.
2513+
/// The string may contain embedded zeros.
2514+
///
2515+
/// From: `void luaL_addlstring(luaL_Buffer *B, const char *s, size_t l);`
2516+
/// Refer to: https://www.lua.org/manual/5.1/manual.html#luaL_addlstring
2517+
/// Stack Behavior: `[-0, +0, m]`
2518+
pub fn addLString(buffer: *Buffer, string: [:0]const u8) void {
2519+
assert(buffer.p != null and buffer.L != null); // You must use `Lua.initBuffer(&Lua.Buffer)` before calling `Lua.Buffer.addLString()`.
2520+
2521+
return c.luaL_addlstring(@ptrCast(buffer), string.ptr, string.len);
2522+
}
2523+
2524+
/// Adds the value at the top of the stack to the buffer (see https://www.lua.org/manual/5.1/manual.html#luaL_Buffer).
2525+
/// Pops the value. This is the only function on string buffers that can (and must) be called with an extra
2526+
/// element on the stack, which is the value to be added to the buffer.
2527+
///
2528+
/// From: `void luaL_addvalue(luaL_Buffer *B);`
2529+
/// Refer to: https://www.lua.org/manual/5.1/manual.html#luaL_addvalue
2530+
/// Stack Behavior: `[-1, +0, m]`
2531+
pub fn addValue(buffer: *Buffer) void {
2532+
assert(buffer.p != null and buffer.L != null); // You must use `Lua.initBuffer(&Lua.Buffer)` before calling `Lua.Buffer.addLString()`.
2533+
assert(buffer.L.?.getTop() > 0);
2534+
2535+
return c.luaL_addvalue(@ptrCast(buffer));
2536+
}
2537+
25002538
/// Prepares writable space in the buffer.
25012539
///
25022540
/// Used to provide direct writing of data. The caller **MUST** call `buffer.addSize()` after writing content
@@ -4854,3 +4892,57 @@ test "Buffer can be created by direct writes to the buffer" {
48544892
try std.testing.expect(lua.isString(-1));
48554893
try std.testing.expectEqualStrings("Hello", try lua.toLString(-1));
48564894
}
4895+
4896+
test "Buffer can be created by adding strings to the buffer" {
4897+
const lua = try Lua.init(std.testing.allocator);
4898+
defer lua.deinit();
4899+
4900+
var b: Lua.Buffer = .{};
4901+
lua.initBuffer(&b);
4902+
b.addString("Hello, world!");
4903+
b.pushResult();
4904+
4905+
try std.testing.expectEqual(1, lua.getTop());
4906+
try std.testing.expect(lua.isString(-1));
4907+
try std.testing.expectEqualStrings("Hello, world!", try lua.toLString(-1));
4908+
}
4909+
4910+
test "Buffer can be created by adding literal strings to the buffer" {
4911+
const lua = try Lua.init(std.testing.allocator);
4912+
defer lua.deinit();
4913+
4914+
var b: Lua.Buffer = .{};
4915+
lua.initBuffer(&b);
4916+
b.addLString("Hello, world!");
4917+
b.pushResult();
4918+
4919+
try std.testing.expectEqual(1, lua.getTop());
4920+
try std.testing.expect(lua.isString(-1));
4921+
try std.testing.expectEqualStrings("Hello, world!", try lua.toLString(-1));
4922+
}
4923+
4924+
test "Buffer can be created by adding values on the stack to the buffer" {
4925+
const lua = try Lua.init(std.testing.allocator);
4926+
defer lua.deinit();
4927+
4928+
var b: Lua.Buffer = .{};
4929+
lua.initBuffer(&b);
4930+
4931+
lua.pushInteger(42);
4932+
b.addValue();
4933+
try std.testing.expectEqual(0, lua.getTop());
4934+
4935+
lua.pushLString("AAA");
4936+
b.addValue();
4937+
try std.testing.expectEqual(0, lua.getTop());
4938+
4939+
lua.pushNumber(99.2);
4940+
b.addValue();
4941+
try std.testing.expectEqual(0, lua.getTop());
4942+
4943+
b.pushResult();
4944+
4945+
try std.testing.expectEqual(1, lua.getTop());
4946+
try std.testing.expect(lua.isString(-1));
4947+
try std.testing.expectEqualStrings("42AAA99.2", try lua.toLString(-1));
4948+
}

0 commit comments

Comments
 (0)