Skip to content

Commit

Permalink
feat: update toUserdata interface
Browse files Browse the repository at this point in the history
Now the function accepts a type so the return value is a typed pointer
rather than an *anyopaque. This makes the function easier to use because
the call site would always be a cast anyway.
  • Loading branch information
natecraddock committed Feb 16, 2023
1 parent a8e6f28 commit edf6386
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 24 deletions.
9 changes: 4 additions & 5 deletions src/ziglua-5.1/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -874,11 +874,10 @@ pub const Lua = struct {
return error.Fail;
}

/// If the value at the given `index` is a full userdata, returns its memory-block address
/// If the value is a light userdata, returns its value (a pointer)
/// Otherwise returns an error
pub fn toUserdata(lua: *Lua, index: i32) !*anyopaque {
if (c.lua_touserdata(lua.state, index)) |ptr| return ptr;
/// Returns a pointer of the given type to the userdata at the given index.
/// Works for both full and light userdata. Otherwise returns an error.
pub fn toUserdata(lua: *Lua, comptime T: type, index: i32) !*T {
if (c.lua_touserdata(lua.state, index)) |ptr| return opaqueCast(T, ptr);
return error.Fail;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ziglua-5.1/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ test "userdata and uservalues" {
data.val = 1;
std.mem.copy(u8, &data.code, "abcd");

try expectEqual(data, ziglua.opaqueCast(Data, try lua.toUserdata(1)));
try expectEqual(data, try lua.toUserdata(Data, 1));
try expectEqual(@ptrCast(*const anyopaque, data), @alignCast(@alignOf(Data), try lua.toPointer(1)));
}

Expand Down
9 changes: 4 additions & 5 deletions src/ziglua-5.2/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1054,11 +1054,10 @@ pub const Lua = struct {
return result;
}

/// If the value at the given `index` is a full userdata, returns its memory-block address
/// If the value is a light userdata, returns its value (a pointer)
/// Otherwise returns an error
pub fn toUserdata(lua: *Lua, index: i32) !*anyopaque {
if (c.lua_touserdata(lua.state, index)) |ptr| return ptr;
/// Returns a pointer of the given type to the userdata at the given index.
/// Works for both full and light userdata. Otherwise returns an error.
pub fn toUserdata(lua: *Lua, comptime T: type, index: i32) !*T {
if (c.lua_touserdata(lua.state, index)) |ptr| return opaqueCast(T, ptr);
return error.Fail;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ziglua-5.2/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ test "userdata and uservalues" {
lua.getUserValue(1);
try expectEqual(LuaType.nil, lua.typeOf(-1));

try expectEqual(data, ziglua.opaqueCast(Data, try lua.toUserdata(1)));
try expectEqual(data, try lua.toUserdata(Data, 1));
try expectEqual(@ptrCast(*const anyopaque, data), @alignCast(@alignOf(Data), try lua.toPointer(1)));
}

Expand Down
9 changes: 4 additions & 5 deletions src/ziglua-5.3/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1115,11 +1115,10 @@ pub const Lua = struct {
return error.Fail;
}

/// If the value at the given `index` is a full userdata, returns its memory-block address
/// If the value is a light userdata, returns its value (a pointer)
/// Otherwise returns an error
pub fn toUserdata(lua: *Lua, index: i32) !*anyopaque {
if (c.lua_touserdata(lua.state, index)) |ptr| return ptr;
/// Returns a pointer of the given type to the userdata at the given index.
/// Works for both full and light userdata. Otherwise returns an error.
pub fn toUserdata(lua: *Lua, comptime T: type, index: i32) !*T {
if (c.lua_touserdata(lua.state, index)) |ptr| return opaqueCast(T, ptr);
return error.Fail;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ziglua-5.3/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ test "userdata and uservalues" {
try expectEqual(LuaType.number, lua.getUserValue(1));
try expectEqual(@as(Number, 1234.56), try lua.toNumber(-1));

try expectEqual(data, ziglua.opaqueCast(Data, try lua.toUserdata(1)));
try expectEqual(data, try lua.toUserdata(Data, 1));
try expectEqual(@ptrCast(*const anyopaque, data), @alignCast(@alignOf(Data), try lua.toPointer(1)));
}

Expand Down
9 changes: 4 additions & 5 deletions src/ziglua-5.4/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1143,11 +1143,10 @@ pub const Lua = struct {
return error.Fail;
}

/// If the value at the given `index` is a full userdata, returns its memory-block address
/// If the value is a light userdata, returns its value (a pointer)
/// Otherwise returns an error
pub fn toUserdata(lua: *Lua, index: i32) !*anyopaque {
if (c.lua_touserdata(lua.state, index)) |ptr| return ptr;
/// Returns a pointer of the given type to the userdata at the given index.
/// Works for both full and light userdata. Otherwise returns an error.
pub fn toUserdata(lua: *Lua, comptime T: type, index: i32) !*T {
if (c.lua_touserdata(lua.state, index)) |ptr| return opaqueCast(T, ptr);
return error.Fail;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ziglua-5.4/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ test "userdata and uservalues" {
try expectError(error.Fail, lua.setIndexUserValue(1, 3));
try expectError(error.Fail, lua.getIndexUserValue(1, 3));

try expectEqual(data, ziglua.opaqueCast(Data, try lua.toUserdata(1)));
try expectEqual(data, try lua.toUserdata(Data, 1));
try expectEqual(@ptrCast(*const anyopaque, data), @alignCast(@alignOf(Data), try lua.toPointer(1)));
}

Expand Down

0 comments on commit edf6386

Please sign in to comment.