Skip to content

Commit edf6386

Browse files
committedFeb 16, 2023
feat: update toUserdata interface
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.
1 parent a8e6f28 commit edf6386

File tree

8 files changed

+20
-24
lines changed

8 files changed

+20
-24
lines changed
 

‎src/ziglua-5.1/lib.zig

+4-5
Original file line numberDiff line numberDiff line change
@@ -874,11 +874,10 @@ pub const Lua = struct {
874874
return error.Fail;
875875
}
876876

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

‎src/ziglua-5.1/tests.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ test "userdata and uservalues" {
553553
data.val = 1;
554554
std.mem.copy(u8, &data.code, "abcd");
555555

556-
try expectEqual(data, ziglua.opaqueCast(Data, try lua.toUserdata(1)));
556+
try expectEqual(data, try lua.toUserdata(Data, 1));
557557
try expectEqual(@ptrCast(*const anyopaque, data), @alignCast(@alignOf(Data), try lua.toPointer(1)));
558558
}
559559

‎src/ziglua-5.2/lib.zig

+4-5
Original file line numberDiff line numberDiff line change
@@ -1054,11 +1054,10 @@ pub const Lua = struct {
10541054
return result;
10551055
}
10561056

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

‎src/ziglua-5.2/tests.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ test "userdata and uservalues" {
731731
lua.getUserValue(1);
732732
try expectEqual(LuaType.nil, lua.typeOf(-1));
733733

734-
try expectEqual(data, ziglua.opaqueCast(Data, try lua.toUserdata(1)));
734+
try expectEqual(data, try lua.toUserdata(Data, 1));
735735
try expectEqual(@ptrCast(*const anyopaque, data), @alignCast(@alignOf(Data), try lua.toPointer(1)));
736736
}
737737

‎src/ziglua-5.3/lib.zig

+4-5
Original file line numberDiff line numberDiff line change
@@ -1115,11 +1115,10 @@ pub const Lua = struct {
11151115
return error.Fail;
11161116
}
11171117

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

‎src/ziglua-5.3/tests.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ test "userdata and uservalues" {
774774
try expectEqual(LuaType.number, lua.getUserValue(1));
775775
try expectEqual(@as(Number, 1234.56), try lua.toNumber(-1));
776776

777-
try expectEqual(data, ziglua.opaqueCast(Data, try lua.toUserdata(1)));
777+
try expectEqual(data, try lua.toUserdata(Data, 1));
778778
try expectEqual(@ptrCast(*const anyopaque, data), @alignCast(@alignOf(Data), try lua.toPointer(1)));
779779
}
780780

‎src/ziglua-5.4/lib.zig

+4-5
Original file line numberDiff line numberDiff line change
@@ -1143,11 +1143,10 @@ pub const Lua = struct {
11431143
return error.Fail;
11441144
}
11451145

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

‎src/ziglua-5.4/tests.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ test "userdata and uservalues" {
819819
try expectError(error.Fail, lua.setIndexUserValue(1, 3));
820820
try expectError(error.Fail, lua.getIndexUserValue(1, 3));
821821

822-
try expectEqual(data, ziglua.opaqueCast(Data, try lua.toUserdata(1)));
822+
try expectEqual(data, try lua.toUserdata(Data, 1));
823823
try expectEqual(@ptrCast(*const anyopaque, data), @alignCast(@alignOf(Data), try lua.toPointer(1)));
824824
}
825825

0 commit comments

Comments
 (0)
Please sign in to comment.