Skip to content

Commit b573ee0

Browse files
committed
Add support for lua.getField(), lua.setField(), lua.getGlobal(), lua.setGlobal()
1 parent 5798813 commit b573ee0

File tree

3 files changed

+82
-37
lines changed

3 files changed

+82
-37
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ lua.doString(
7878

7979
| API | Support |
8080
|---|---|
81-
| Lua C API (`lua_*`) | 58% available (55/92) | <!-- 59.76% - Add 1.07 per -->
82-
| Auxilary Library (`luaL_*`) | 6% available (3/48) | <!-- Always 2% * n, for n up to 48 -->
81+
| Lua C API (`lua_*`) | 64% available (59/92) |
82+
| Auxilary Library (`luaL_*`) | 6% available (3/48) |
8383
| LuaJIT Extensions | *No plans to implement.* |
8484

8585
## C API Coverage (`lua_`)
@@ -119,8 +119,8 @@ pattern has changed, such as using the Zig `init()` function pattern instead of
119119
| `lua_gc`||
120120
| `lua_getallocf`|`lua.getAllocF()` |
121121
| `lua_getfenv`||
122-
| `lua_getfield`||
123-
| `lua_getglobal`||
122+
| `lua_getfield`| ☑️ `lua.getField()` |
123+
| `lua_getglobal`| ☑️ `lua.getGlobal()` |
124124
| `lua_getmetatable`| ☑️ `lua.getMetatable()` |
125125
| `lua_gettable`| ☑️ `lua.getTable()` |
126126
| `lua_gettop`| ☑️ `lua.getTop()` |
@@ -172,8 +172,8 @@ pattern has changed, such as using the Zig `init()` function pattern instead of
172172
| `lua_resume`||
173173
| `lua_setallocf`|`lua.setAllocF()`|
174174
| `lua_setfenv`||
175-
| `lua_setfield`||
176-
| `lua_setglobal`||
175+
| `lua_setfield`| ☑️ `lua.setField()` |
176+
| `lua_setglobal`| ☑️ `lua.setGlobal()` |
177177
| `lua_setmetatable`| ☑️ `lua.setMetatable()` |
178178
| `lua_settable`| ☑️ `lua.setTable()` |
179179
| `lua_settop`| ☑️ `lua.setTop()` |

src/lua_api.zig

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,6 @@ pub const GcMode = enum(i32) {
4141
/// Stack Behavior: `[-0, +1, -]`
4242
pub fn getfenv(lua: *Lua, index: i32) void;
4343

44-
/// Pushes onto the stack the value t[k], where t is the value at the given valid index.
45-
/// As in Lua, this function may trigger a metamethod for the "index" event (see https://www.lua.org/manual/5.1/manual.html#2.8).
46-
///
47-
/// From: `void lua_getfield(lua_State *L, int index, const char *k);`
48-
/// Refer to: https://www.lua.org/manual/5.1/manual.html#lua_getfield
49-
/// Stack Behavior: `[-0, +1, e]`
50-
pub fn getField(lua: *Lua, index: i32, k: [:0]const u8) LuaType;
51-
52-
/// Pushes onto the stack the value of the global name.
53-
///
54-
/// From: `void lua_getglobal(lua_State *L, const char *name);`
55-
/// Refer to: https://www.lua.org/manual/5.1/manual.html#lua_getglobal
56-
/// Stack Behavior: `[-0, +1, e]`
57-
pub fn getGlobal(lua: *Lua, name: [*:0]const u8) LuaType;
58-
5944
/// The type used by the Lua API to represent integral values.
6045
/// By default it is a signed integral type that the machine handles "comfortably".
6146
///
@@ -248,22 +233,6 @@ pub fn resumeCoroutine(lua: *Lua, narg: i32) i32;
248233
/// Stack Behavior: `[-1, +0, -]`
249234
pub fn setFenv(lua: *Lua, index: i32) bool;
250235

251-
/// Does the equivalent to t[k] = v, where t is the value at the given valid index and v is the value at the
252-
/// top of the stack. This function pops the value from the stack. As in Lua, this function may trigger a
253-
/// metamethod for the "newindex" event (see https://www.lua.org/manual/5.1/manual.html#2.8).
254-
///
255-
/// From: `void lua_setfield(lua_State *L, int index, const char *k);`
256-
/// Refer to: https://www.lua.org/manual/5.1/manual.html#lua_setfield
257-
/// Stack Behavior: `[-1, +0, e]`
258-
pub fn setField(lua: *Lua, index: i32, key: [:0]const u8) void;
259-
260-
/// Pops a value from the stack and sets it as the new value of global `name`.
261-
///
262-
/// From: `void lua_setglobal(lua_State *L, const char *name);`
263-
/// Refer to: https://www.lua.org/manual/5.1/manual.html#lua_setglobal
264-
/// Stack Behavior: `[-1, +0, e]`
265-
pub fn setGlobal(lua: *Lua, name: [:0]const u8) void;
266-
267236
/// Opaque structure that keeps the whole state of a Lua interpreter. The Lua library is fully reentrant:
268237
/// it has no global variables. All information about a state is kept in this structure. A pointer
269238
/// to this state must be passed as the first argument to every function in the library, except

src/root.zig

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,53 @@ pub const Lua = opaque {
851851
return 1 == c.lua_getmetatable(asState(lua), index);
852852
}
853853

854+
/// Pushes onto the stack the value `t[k]`, where `t` is the value at the given valid index. As in Lua, this function
855+
/// may trigger a metamethod for the "index" event (see https://www.lua.org/manual/5.1/manual.html#2.8).
856+
///
857+
/// From: `void lua_getfield(lua_State *L, int index, const char *k);`
858+
/// Refer to: https://www.lua.org/manual/5.1/manual.html#lua_getfield
859+
/// Stack Behavior: `[-0, +1, e]`
860+
pub fn getField(lua: *Lua, index: i32, key: [:0]const u8) Lua.Type {
861+
lua.validateStackIndex(index);
862+
863+
c.lua_getfield(asState(lua), index, @ptrCast(key.ptr));
864+
return lua.typeOf(-1);
865+
}
866+
867+
/// Does the equivalent to `t[k] = v`, where `t` is the value at the given valid index and `v` is the value at the
868+
/// top of the stack. This function pops the value from the stack. As in Lua, this function may trigger a
869+
/// metamethod for the "newindex" event (see https://www.lua.org/manual/5.1/manual.html#2.8).
870+
///
871+
/// From: `void lua_setfield(lua_State *L, int index, const char *k);`
872+
/// Refer to: https://www.lua.org/manual/5.1/manual.html#lua_setfield
873+
/// Stack Behavior: `[-1, +0, e]`
874+
pub fn setField(lua: *Lua, index: i32, key: [:0]const u8) void {
875+
lua.validateStackIndex(index);
876+
877+
return c.lua_setfield(asState(lua), index, @ptrCast(key.ptr));
878+
}
879+
880+
/// Pushes onto the stack the value of the global name.
881+
///
882+
/// From: `void lua_getglobal(lua_State *L, const char *name);`
883+
/// Refer to: https://www.lua.org/manual/5.1/manual.html#lua_getglobal
884+
/// Stack Behavior: `[-0, +1, e]`
885+
pub fn getGlobal(lua: *Lua, name: [:0]const u8) Lua.Type {
886+
c.lua_getglobal(asState(lua), @as([*:0]const u8, @ptrCast(name.ptr)));
887+
return lua.typeOf(-1);
888+
}
889+
890+
/// Pops a value from the stack and sets it as the new value of global `name`.
891+
///
892+
/// From: `void lua_setglobal(lua_State *L, const char *name);`
893+
/// Refer to: https://www.lua.org/manual/5.1/manual.html#lua_setglobal
894+
/// Stack Behavior: `[-1, +0, e]`
895+
pub fn setGlobal(lua: *Lua, name: [:0]const u8) void {
896+
assert(lua.getTop() > 0);
897+
898+
return c.lua_setglobal(asState(lua), @as([*:0]const u8, @ptrCast(name.ptr)));
899+
}
900+
854901
/// Does the equivalent of `t[k] = v`, where `t` is the acceptable index of the table on the stack, `v` is
855902
/// the value at the top of the stack, and `k` is the value just below the top. This function pops both the
856903
/// key and the value from the stack. As in Lua, this function may trigger a metamethod for the "newindex"
@@ -1701,6 +1748,35 @@ test "tables" {
17011748
try std.testing.expectEqual(Lua.Type.string, lua.getTableRaw(-2));
17021749
}
17031750

1751+
test "getfield and setfield" {
1752+
const lua = try Lua.init(std.testing.allocator);
1753+
defer lua.deinit();
1754+
1755+
lua.newTable();
1756+
lua.pushInteger(42);
1757+
lua.setField(-2, "foo");
1758+
1759+
try std.testing.expectEqual(1, lua.getTop());
1760+
1761+
const actual_type = lua.getField(-1, "foo");
1762+
try std.testing.expectEqual(Lua.Type.number, actual_type);
1763+
try std.testing.expectEqual(42, lua.toInteger(-1));
1764+
}
1765+
1766+
test "getglobal and setglobal" {
1767+
const lua = try Lua.init(std.testing.allocator);
1768+
defer lua.deinit();
1769+
1770+
lua.pushInteger(42);
1771+
lua.setGlobal("XXX");
1772+
1773+
try std.testing.expectEqual(0, lua.getTop());
1774+
1775+
const actual_type = lua.getGlobal("XXX");
1776+
try std.testing.expectEqual(Lua.Type.number, actual_type);
1777+
try std.testing.expectEqual(42, lua.toInteger(-1));
1778+
}
1779+
17041780
test "lengthOf" {
17051781
const lua = try Lua.init(std.testing.allocator);
17061782
defer lua.deinit();

0 commit comments

Comments
 (0)