Skip to content

Commit

Permalink
Use new destructuring syntax for atom return values
Browse files Browse the repository at this point in the history
Also use "[:0]const u8" return instead of [*:0]const u8 as
the former form is handier in general.  Keeping zero termination
as that's harder to put back once you've removed it.
  • Loading branch information
nurpax committed Jan 16, 2024
1 parent 03202f0 commit 114ba5f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
14 changes: 8 additions & 6 deletions src/libluau.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
49 changes: 22 additions & 27 deletions src/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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,
}
}
};
Expand Down

0 comments on commit 114ba5f

Please sign in to comment.