From 74cb4f86cd8008b1ee7a7c8fa2a29da58de7f1fa Mon Sep 17 00:00:00 2001 From: Nathan Craddock Date: Mon, 27 Apr 2026 01:46:38 -0600 Subject: [PATCH] Add more functions to Luau The following API functions are now available in Luau: * cProtectedCall * rawGetPtr * rawSetPtr * traceback The following new functions were added for Luau: * rawGetPtrTagged * rawSetPtrTagged There are more Luau-specific API functions that are not part of zlua. zlua.c can be used to access these functions until wrapper functions are added. --- src/lib.zig | 48 ++++++++++++++++++++++++++++++++++++++++++------ src/tests.zig | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/src/lib.zig b/src/lib.zig index f4a5c40..dd6ace5 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -952,7 +952,7 @@ pub const Lua = opaque { /// containing `userdata`. In case of errors, `Lua.cProtectedCall()` returns the same error codes as `Lua.protectedCall()`, plus the error object on the /// top of the stack; otherwise, it returns zero, and does not change the stack. All values returned by `c_fn` are discarded. /// - /// Only implemented in Lua 5.1 + /// Only implemented in Lua 5.1 and Luau. /// /// * Pops from Stack: `0` /// * Pushes to Stack: `(0|1)` @@ -2260,7 +2260,7 @@ pub const Lua = opaque { /// Pushes onto the stack the value `t[k]`, where `t` is the table at the given `index` and `k` is the pointer `p` represented as a /// light userdata. /// - /// Not available in Lua 5.1, LuaJIT, or Luau + /// Not available in Lua 5.1 or LuaJIT /// /// * Pops from Stack: `0` /// * Pushes to Stack: `1` @@ -2270,6 +2270,7 @@ pub const Lua = opaque { pub fn rawGetPtr(lua: *Lua, index: i32, p: *const anyopaque) LuaType { switch (lang) { .lua53, .lua54, .lua55 => return @enumFromInt(c.lua_rawgetp(@ptrCast(lua), index, p)), + .luau => return @enumFromInt(c.lua_rawgetp(@as(*LuaState, @ptrCast(lua)), index, @constCast(p))), else => { c.lua_rawgetp(@ptrCast(lua), index, p); return lua.typeOf(-1); @@ -2277,6 +2278,20 @@ pub const Lua = opaque { } } + /// Pushes onto the stack the value `t[k]`, where `t` is the table at the given `index` and `k` is the pointer `p` represented as a + /// light userdata with the given `tag`. + /// + /// Only implemented in Luau. + /// + /// * Pops from Stack: `0` + /// * Pushes to Stack: `1` + /// * Lua Runtime Errors: `none` + /// + /// See https://sleitnick.github.io/luau-api/reference.html#lua_rawgetptagged + pub fn rawGetPtrTagged(lua: *Lua, index: i32, p: *const anyopaque, tag: i32) LuaType { + return @enumFromInt(c.lua_rawgetptagged(@as(*LuaState, @ptrCast(lua)), index, @constCast(p), tag)); + } + /// Returns the raw "length" of the value at the given index: /// * for strings, this is the string length /// * for tables, this is the result of the length operator ('#') with no metamethods @@ -2330,7 +2345,7 @@ pub const Lua = opaque { /// /// This function pops the value from the stack. The assignment is raw, that is, it does not use the `__newindex` metavalue. /// - /// Not available in Lua 5.1, LuaJIT or Luau + /// Not available in Lua 5.1 or LuaJIT /// /// * Pops from Stack: `1` /// * Pushes to Stack: `0` @@ -2338,7 +2353,27 @@ pub const Lua = opaque { /// /// See https://www.lua.org/manual/5.4/manual.html#lua_rawsetp pub fn rawSetPtr(lua: *Lua, index: i32, p: *const anyopaque) void { - c.lua_rawsetp(@ptrCast(lua), index, p); + switch (lang) { + .luau => c.lua_rawsetp(@as(*LuaState, @ptrCast(lua)), index, @constCast(p)), + else => c.lua_rawsetp(@ptrCast(lua), index, p), + } + } + + /// Does the equivalent of `t[p] = v`, where `t` is the table at the given + /// `index`, `p` is encoded as a light userdata with the given `tag`, and + /// `v` is the value on the top of the stack. + /// + /// This function pops the value from the stack. The assignment is raw, that is, it does not use the `__newindex` metavalue. + /// + /// Only implemented in Luau. + /// + /// * Pops from Stack: `1` + /// * Pushes to Stack: `0` + /// * Lua Runtime Errors: `memory` + /// + /// See https://sleitnick.github.io/luau-api/reference.html#lua_rawsetptagged + pub fn rawSetPtrTagged(lua: *Lua, index: i32, p: *const anyopaque, tag: i32) void { + c.lua_rawsetptagged(@as(*LuaState, @ptrCast(lua)), index, @constCast(p), tag); } /// Sets the C function `f` as the new value of global `name` @@ -2872,9 +2907,10 @@ pub const Lua = opaque { /// Only available in Luau pub fn toStringAtom(lua: *Lua, index: i32) error{ExpectedString}!struct { i32, [:0]const u8 } { if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau."); + var length: usize = undefined; var atom: c_int = undefined; - if (c.lua_tostringatom(@ptrCast(lua), index, &atom)) |ptr| { - return .{ atom, std.mem.span(ptr) }; + if (c.lua_tolstringatom(@ptrCast(lua), index, &length, &atom)) |ptr| { + return .{ atom, ptr[0..length :0] }; } return error.ExpectedString; } diff --git a/src/tests.zig b/src/tests.zig index 5b11f2b..0328ea2 100644 --- a/src/tests.zig +++ b/src/tests.zig @@ -449,7 +449,7 @@ test "calling a function" { } test "calling a function with cProtectedCall" { - if (zlua.lang != .lua51) return; + if (zlua.lang != .lua51 and zlua.lang != .luau) return; const lua: *Lua = try .init(testing.allocator); defer lua.deinit(); @@ -1046,7 +1046,7 @@ test "table traversal" { } test "registry" { - if (langIn(.{ .lua51, .luajit, .luau })) return; + if (langIn(.{ .lua51, .luajit })) return; const lua: *Lua = try .init(testing.allocator); defer lua.deinit(); @@ -1602,7 +1602,7 @@ test "args and errors" { } test "traceback" { - if (langIn(.{ .lua51, .luajit, .luau })) return; + if (langIn(.{ .lua51, .luajit })) return; const lua: *Lua = try .init(testing.allocator); defer lua.deinit(); @@ -1619,7 +1619,11 @@ test "traceback" { try lua.doString("res = tracebackFn()"); _ = try lua.getGlobal("res"); - try expectEqualStrings("\nstack traceback:\n\t[string \"res = tracebackFn()\"]:1: in main chunk", try lua.toString(-1)); + if (zlua.lang == .luau) { + try expectEqualStrings("\n[string \"...\"]:1\n", try lua.toString(-1)); + } else { + try expectEqualStrings("\nstack traceback:\n\t[string \"res = tracebackFn()\"]:1: in main chunk", try lua.toString(-1)); + } } test "getSubtable" { @@ -2144,6 +2148,28 @@ test "tagged userdata" { try expectError(error.ExpectedTaggedUserdata, lua.getUserdataTag(-1)); } +test "tagged raw get and set" { + if (zlua.lang != .luau) return; + + var lua: *Lua = try .init(testing.allocator); + defer lua.deinit(); + + lua.newTable(); + + var value: usize = 42; + const tag = 5; + + _ = lua.pushString("Some string"); + lua.rawSetPtrTagged(1, &value, tag); + + const lua_type = lua.rawGetPtrTagged(1, &value, tag); + try expectEqual(.string, lua_type); + + // Wrong tag will return nil. + const fail = lua.rawGetPtrTagged(1, &value, 6); + try expectEqual(.nil, fail); +} + fn vectorCtor(l: *Lua) !i32 { const x = try l.toNumber(1); const y = try l.toNumber(2); -- knotmirror 0.0.1