diff --git a/src/libluau.zig b/src/libluau.zig index fa443f2..56deb07 100644 --- a/src/libluau.zig +++ b/src/libluau.zig @@ -974,18 +974,20 @@ pub const Lua = struct { /// Converts the Lua string at the given `index` to a string atom. /// The Lua value must be a string. - pub fn toStringAtom(lua: *Lua, index: i32, atom: *c_int) ![*:0]const u8 { - if (c.lua_tostringatom(lua.state, index, atom)) |ptr| { - return ptr; + pub fn toStringAtom(lua: *Lua, index: i32) !struct { i32, [:0]const u8 } { + var atom: c_int = -1; + if (c.lua_tostringatom(lua.state, index, &atom)) |ptr| { + return .{ atom, std.mem.span(ptr) }; } return error.Fail; } /// Retrieve the user atom index and name for the method being /// invoked in a namecall. - pub fn namecallAtom(lua: *Lua, atom: *c_int) ![*:0]const u8 { - if (c.lua_namecallatom(lua.state, atom)) |ptr| { - return ptr; + pub fn namecallAtom(lua: *Lua) !struct { i32, [:0]const u8 } { + var atom: c_int = -1; + if (c.lua_namecallatom(lua.state, &atom)) |ptr| { + return .{ atom, std.mem.span(ptr) }; } return error.Fail; } diff --git a/src/tests.zig b/src/tests.zig index d0ab9aa..faed081 100644 --- a/src/tests.zig +++ b/src/tests.zig @@ -2340,24 +2340,20 @@ test "useratom" { _ = lua.pushString("unknownatom"); _ = lua.pushString("method_one"); _ = lua.pushString("another_method"); - var atom_idx0: c_int = 0; - var atom_idx1: c_int = 0; - var atom_idx2: c_int = 0; - const str0 = try lua.toStringAtom(-2, &atom_idx0); - const str1 = try lua.toStringAtom(-1, &atom_idx1); - const str2 = try lua.toStringAtom(-3, &atom_idx2); - try testing.expect(std.mem.eql(u8, std.mem.span(str0), "method_one")); - try testing.expect(std.mem.eql(u8, std.mem.span(str1), "another_method")); - try testing.expect(std.mem.eql(u8, std.mem.span(str2), "unknownatom")); // should work, but returns -1 for atom idx + const atom_idx0, const str0 = try lua.toStringAtom(-2); + const atom_idx1, const str1 = try lua.toStringAtom(-1); + const atom_idx2, const str2 = try lua.toStringAtom(-3); + try testing.expect(std.mem.eql(u8, str0, "method_one")); + try testing.expect(std.mem.eql(u8, str1, "another_method")); + try testing.expect(std.mem.eql(u8, str2, "unknownatom")); // should work, but returns -1 for atom idx try expectEqual(0, atom_idx0); try expectEqual(1, atom_idx1); try expectEqual(-1, atom_idx2); lua.pushInteger(13); - var dummy: c_int = 0; - try expectError(error.Fail, lua.toStringAtom(-1, &dummy)); + try expectError(error.Fail, lua.toStringAtom(-1)); } test "namecall" { @@ -2381,23 +2377,22 @@ test "namecall" { } pub fn vectorNamecall(l: *Lua) i32 { - var atom_idx: c_int = -1; - if (l.namecallAtom(&atom_idx)) |_| { - switch (atom_idx) { - dot_idx => { - const a = l.checkVector(1); - const b = l.checkVector(2); - l.pushNumber(a[0] * b[0] + a[1] * b[1] + a[2] * b[2]); // vec3 dot - }, - sum_idx => { - const a = l.checkVector(1); - l.pushNumber(a[0] + a[1] + a[2]); - }, - else => unreachable, - } - return 1; - } else |_| { + const atom_idx, _ = l.namecallAtom() catch { l.raiseErrorStr("%s is not a valid vector method", .{l.checkString(1)}); + }; + switch (atom_idx) { + dot_idx => { + const a = l.checkVector(1); + const b = l.checkVector(2); + l.pushNumber(a[0] * b[0] + a[1] * b[1] + a[2] * b[2]); // vec3 dot + return 1; + }, + sum_idx => { + const a = l.checkVector(1); + l.pushNumber(a[0] + a[1] + a[2]); + return 1; + }, + else => unreachable, } } };