Skip to content

Commit e151c01

Browse files
committed
Treat userdata as Ref
Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
1 parent 25e8083 commit e151c01

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

src/lua.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ pub const Lua = struct {
629629
string: []const u8,
630630
table: Table,
631631
function: Function,
632-
userdata: *anyopaque,
632+
userdata: Ref,
633633
lightuserdata: *anyopaque,
634634

635635
/// Releases any resources held by this Value.
@@ -647,6 +647,7 @@ pub const Lua = struct {
647647
switch (self) {
648648
.table => |t| t.deinit(),
649649
.function => |f| f.deinit(),
650+
.userdata => |u| u.deinit(),
650651
else => {}, // No cleanup needed for primitive types
651652
}
652653
}

src/stack.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub fn push(lua: Lua, value: anytype) void {
123123
.string => |s| lua.state.pushLString(s),
124124
.table => |t| push(lua, t),
125125
.function => |f| push(lua, f),
126-
.userdata => |u| lua.state.pushLightUserdata(u), // Note: full userdata push would need more complex handling
126+
.userdata => |u| push(lua, u), // Push userdata reference
127127
.lightuserdata => |u| lua.state.pushLightUserdata(u),
128128
}
129129
return;
@@ -568,8 +568,8 @@ pub fn toValue(lua: Lua, comptime T: type, index: i32) ?T {
568568
Lua.Value{ .function = func }
569569
else
570570
null,
571-
.userdata => if (lua.state.toUserdata(index)) |data|
572-
Lua.Value{ .userdata = data }
571+
.userdata => if (lua.state.isUserdata(index))
572+
Lua.Value{ .userdata = Lua.Ref{ .lua = lua, .ref = lua.state.ref(index) } }
573573
else
574574
null,
575575
.lightuserdata => if (lua.state.toLightUserdata(index)) |data|

src/tests.zig

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -817,13 +817,21 @@ test "Value enum all cases" {
817817
try expect(std.meta.activeTag(back.?) == std.meta.activeTag(case.value));
818818
}
819819

820-
// Test userdata (both types push as lightuserdata, so they retrieve as lightuserdata)
820+
// Test lightuserdata
821821
try globals.set("light", Lua.Value{ .lightuserdata = ptr });
822-
try globals.set("user", Lua.Value{ .userdata = ptr });
823822
const light_back = try globals.get("light", Lua.Value);
824-
const user_back = try globals.get("user", Lua.Value);
825823
try expect(light_back.? == .lightuserdata);
826-
try expect(user_back.? == .lightuserdata); // Both become lightuserdata when retrieved
824+
825+
// Test userdata (create proper userdata with reference)
826+
_ = lua.state.newUserdata(@sizeOf(i32));
827+
const userdata_ref = Lua.Ref.init(lua, -1);
828+
lua.state.pop(1); // Remove from stack since we have a reference
829+
defer userdata_ref.deinit();
830+
831+
try globals.set("user", Lua.Value{ .userdata = userdata_ref });
832+
const user_back = try globals.get("user", Lua.Value);
833+
try expect(user_back.? == .userdata);
834+
defer user_back.?.deinit();
827835

828836
// Test table
829837
const table = lua.createTable(.{});

0 commit comments

Comments
 (0)