Skip to content

Commit 1cb1f1c

Browse files
committed
Simplify stack.push to take State pointer, remove unused createFunc parameter
1 parent e67798a commit 1cb1f1c

File tree

4 files changed

+118
-117
lines changed

4 files changed

+118
-117
lines changed

src/Debug.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ pub fn getLocal(self: Self, level: i32, n: i32, comptime T: type) ?struct { name
587587
/// Returns: Name of the variable that was set, or null if not available
588588
pub fn setLocal(self: Self, level: i32, n: i32, comptime T: type, value: T) ?[:0]const u8 {
589589
// Push the value onto the stack first
590-
stack.push(Lua{ .state = self.state.* }, value);
590+
stack.push(self.state, value);
591591

592592
const name_ptr = self.state.setLocal(level, n);
593593
if (name_ptr == null) {
@@ -632,7 +632,7 @@ pub fn setLocal(self: Self, level: i32, n: i32, comptime T: type, value: T) ?[:0
632632
/// Returns: Struct with name and converted value, or null if not available
633633
pub fn getUpvalue(self: Self, func: Lua.Function, n: i32, comptime T: type) ?struct { name: [:0]const u8, value: T } {
634634
// Push function onto the stack to get its index
635-
stack.push(func.ref.lua, func.ref);
635+
stack.push(func.state(), func.ref);
636636
defer self.state.pop(1);
637637

638638
const name_ptr = self.state.getUpvalue(-1, n);
@@ -684,10 +684,10 @@ pub fn getUpvalue(self: Self, func: Lua.Function, n: i32, comptime T: type) ?str
684684
/// Returns: Name of the upvalue that was set, or null if not available
685685
pub fn setUpvalue(self: Self, func: Lua.Function, n: i32, comptime T: type, value: T) ?[:0]const u8 {
686686
// Push function onto the stack first to get its index
687-
stack.push(func.ref.lua, func.ref);
687+
stack.push(func.state(), func.ref);
688688

689689
// Push the value onto the stack (lua_setupvalue expects value on top)
690-
stack.push(func.ref.lua, value);
690+
stack.push(func.state(), value);
691691

692692
const name_ptr = self.state.setUpvalue(-2, n); // Function is now at -2, value at -1
693693

src/Lua.zig

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,8 @@ pub const Table = struct {
546546
ref: Ref,
547547

548548
/// Returns the underlying Lua state for direct state operations.
549-
inline fn state(self: Table) State {
550-
return self.ref.lua.state;
549+
pub inline fn state(self: Table) *const State {
550+
return &self.ref.lua.state;
551551
}
552552

553553
/// Releases the table reference, allowing the table to be garbage collected.
@@ -571,8 +571,8 @@ pub const Table = struct {
571571
pub fn setRaw(self: Table, index: i32, value: anytype) !void {
572572
try self.ref.lua.checkStack(2);
573573

574-
stack.push(self.ref.lua, self.ref); // Push table ref
575-
stack.push(self.ref.lua, value); // Push value
574+
stack.push(self.state(), self.ref); // Push table ref
575+
stack.push(self.state(), value); // Push value
576576
self.state().rawSetI(-2, index); // Set table and pop value
577577
self.state().pop(1); // Pop table
578578
}
@@ -596,7 +596,7 @@ pub const Table = struct {
596596
pub fn getRaw(self: Table, index: i32, comptime T: type) !?T {
597597
try self.ref.lua.checkStack(2);
598598

599-
stack.push(self.ref.lua, self.ref); // Push table ref
599+
stack.push(self.state(), self.ref); // Push table ref
600600
_ = self.state().rawGetI(-1, index); // Push value of t[i] onto stack.
601601

602602
defer self.state().pop(1); // Pop table
@@ -643,9 +643,9 @@ pub const Table = struct {
643643
pub fn set(self: Table, key: anytype, value: anytype) !void {
644644
try self.ref.lua.checkStack(3);
645645

646-
stack.push(self.ref.lua, self.ref); // Push table ref
647-
stack.push(self.ref.lua, key); // Push key
648-
stack.push(self.ref.lua, value); // Push value
646+
stack.push(self.state(), self.ref); // Push table ref
647+
stack.push(self.state(), key); // Push key
648+
stack.push(self.state(), value); // Push value
649649

650650
self.state().setTable(-3); // Set table[key] = value and pop key and value
651651
self.state().pop(1); // Pop table
@@ -712,20 +712,20 @@ pub const Table = struct {
712712

713713
try self.ref.lua.checkStack(@intCast(3 + upvalue_count));
714714

715-
stack.push(self.ref.lua, self.ref); // Push table ref
716-
stack.push(self.ref.lua, key); // Push key
715+
stack.push(self.state(), self.ref); // Push table ref
716+
stack.push(self.state(), key); // Push key
717717

718718
// Push upvalues onto the stack
719719
if (upvalues_info == .@"struct" and upvalues_info.@"struct".is_tuple) {
720720
inline for (0..upvalues_info.@"struct".fields.len) |i| {
721-
stack.push(self.ref.lua, upvalues[i]);
721+
stack.push(self.state(), upvalues[i]);
722722
}
723723
} else {
724-
stack.push(self.ref.lua, upvalues);
724+
stack.push(self.state(), upvalues);
725725
}
726726

727727
// Create the closure with upvalues
728-
const trampoline: State.CFunction = stack.createFunc(self.ref.lua, func);
728+
const trampoline: State.CFunction = stack.createFunc(func);
729729
self.state().pushCClosureK(trampoline, @typeName(@TypeOf(func)), @intCast(upvalue_count), null);
730730

731731
self.state().setTable(-3); // Set table[key] = closure and pop key and value
@@ -780,8 +780,8 @@ pub const Table = struct {
780780
pub fn get(self: Table, key: anytype, comptime T: type) !?T {
781781
try self.ref.lua.checkStack(2);
782782

783-
stack.push(self.ref.lua, self.ref); // Push table ref
784-
stack.push(self.ref.lua, key); // Push key
783+
stack.push(self.state(), self.ref); // Push table ref
784+
stack.push(self.state(), key); // Push key
785785

786786
_ = self.state().getTable(-2); // Pop key and push "table[key]" onto stack
787787
defer self.state().pop(1); // Pop table
@@ -810,8 +810,8 @@ pub const Table = struct {
810810
pub fn call(self: Table, key: anytype, args: anytype, comptime R: type) !Result(R) {
811811
try self.ref.lua.checkStack(3);
812812

813-
stack.push(self.ref.lua, self.ref); // Push table ref
814-
stack.push(self.ref.lua, key); // Push key
813+
stack.push(self.state(), self.ref); // Push table ref
814+
stack.push(self.state(), key); // Push key
815815
_ = self.state().getTable(-2); // Get function from table, pop key
816816

817817
defer self.state().pop(-1); // Pop table in the end.
@@ -850,8 +850,8 @@ pub const Table = struct {
850850
///
851851
/// Errors: `Error.InvalidType` if the named field is not a function
852852
pub fn compile(self: Table, name: []const u8) !void {
853-
stack.push(self.ref.lua, self.ref); // Push table ref
854-
stack.push(self.ref.lua, name); // Push func name
853+
stack.push(self.state(), self.ref); // Push table ref
854+
stack.push(self.state(), name); // Push func name
855855
_ = self.state().getTable(-2); // Get function from table, pop key
856856

857857
if (!self.state().isFunction(-1)) {
@@ -901,7 +901,7 @@ pub const Table = struct {
901901
pub fn len(self: Table) !i32 {
902902
try self.ref.lua.checkStack(1);
903903

904-
stack.push(self.ref.lua, self.ref); // Push table ref
904+
stack.push(self.state(), self.ref); // Push table ref
905905
defer self.state().pop(1); // Pop table
906906

907907
return self.state().objLen(-1);
@@ -957,7 +957,7 @@ pub const Table = struct {
957957
pub fn setReadonly(self: Table, readonly: bool) !void {
958958
try self.ref.lua.checkStack(1);
959959

960-
stack.push(self.ref.lua, self.ref); // Push table ref
960+
stack.push(self.state(), self.ref); // Push table ref
961961
defer self.state().pop(1); // Pop table
962962

963963
self.state().setReadonly(-1, readonly);
@@ -982,7 +982,7 @@ pub const Table = struct {
982982
pub fn isReadonly(self: Table) !bool {
983983
try self.ref.lua.checkStack(1);
984984

985-
stack.push(self.ref.lua, self.ref); // Push table ref
985+
stack.push(self.state(), self.ref); // Push table ref
986986
defer self.state().pop(1); // Pop table
987987

988988
return self.state().getReadonly(-1);
@@ -1011,7 +1011,7 @@ pub const Table = struct {
10111011
pub fn setSafeEnv(self: Table, safe: bool) !void {
10121012
try self.ref.lua.checkStack(1);
10131013

1014-
stack.push(self.ref.lua, self.ref); // Push table ref
1014+
stack.push(self.state(), self.ref); // Push table ref
10151015
defer self.state().pop(1); // Pop table
10161016

10171017
self.state().setSafeEnv(-1, safe);
@@ -1043,8 +1043,8 @@ pub const Table = struct {
10431043
pub fn setMetaTable(self: Table, metatable: Table) !void {
10441044
try self.ref.lua.checkStack(2);
10451045

1046-
stack.push(self.ref.lua, self.ref); // Push table ref
1047-
stack.push(self.ref.lua, metatable.ref); // Push metatable ref
1046+
stack.push(self.state(), self.ref); // Push table ref
1047+
stack.push(self.state(), metatable.ref); // Push metatable ref
10481048

10491049
_ = self.state().setMetatable(-2); // Set metatable and pop it
10501050
self.state().pop(1); // Pop table
@@ -1080,7 +1080,7 @@ pub const Table = struct {
10801080
pub fn getMetaTable(self: Table) !?Table {
10811081
try self.ref.lua.checkStack(1);
10821082

1083-
stack.push(self.ref.lua, self.ref); // Push table ref
1083+
stack.push(self.state(), self.ref); // Push table ref
10841084
defer self.state().pop(1); // Pop table
10851085

10861086
if (self.state().getMetatable(-1)) {
@@ -1128,7 +1128,7 @@ pub const Table = struct {
11281128
pub fn clear(self: Table) !void {
11291129
try self.ref.lua.checkStack(1);
11301130

1131-
stack.push(self.ref.lua, self.ref); // Push table ref
1131+
stack.push(self.state(), self.ref); // Push table ref
11321132
self.state().clearTable(-1); // Clear table
11331133
self.state().pop(1); // Pop table
11341134
}
@@ -1162,7 +1162,7 @@ pub const Table = struct {
11621162
pub fn clone(self: Table) !Table {
11631163
try self.ref.lua.checkStack(2);
11641164

1165-
stack.push(self.ref.lua, self.ref); // Push table ref
1165+
stack.push(self.state(), self.ref); // Push table ref
11661166
self.state().cloneTable(-1); // Clone table, pushes clone on stack
11671167

11681168
// Create a reference to the cloned table on the stack
@@ -1218,12 +1218,12 @@ pub const Table = struct {
12181218
try self.table.ref.lua.checkStack(3);
12191219

12201220
// Push table onto stack
1221-
stack.push(self.table.ref.lua, self.table.ref);
1221+
stack.push(self.table.state(), self.table.ref);
12221222
defer self.table.state().pop(1);
12231223

12241224
// Push key for lua_next (nil if null)
12251225
if (self.current_entry) |entry| {
1226-
stack.push(self.table.ref.lua, entry.key);
1226+
stack.push(self.table.state(), entry.key);
12271227
entry.deinit();
12281228
} else {
12291229
self.table.state().pushNil();
@@ -1594,8 +1594,8 @@ pub const Function = struct {
15941594
ref: Ref,
15951595

15961596
/// Returns the underlying Lua state for direct state operations.
1597-
inline fn state(self: Function) State {
1598-
return self.ref.lua.state;
1597+
pub inline fn state(self: Function) *const State {
1598+
return &self.ref.lua.state;
15991599
}
16001600

16011601
pub fn deinit(self: Function) void {
@@ -1636,7 +1636,7 @@ pub const Function = struct {
16361636
pub fn call(self: @This(), args: anytype, comptime R: type) !Result(R) {
16371637
try self.ref.lua.checkStack(2);
16381638

1639-
stack.push(self.ref.lua, self.ref); // Push function ref
1639+
stack.push(self.state(), self.ref); // Push function ref
16401640

16411641
// Use resume semantics if in a thread, call semantics if in main state
16421642
return self.ref.lua.call(args, R, self.ref.lua.isThread());
@@ -1675,7 +1675,7 @@ pub const Function = struct {
16751675
/// }
16761676
/// ```
16771677
pub fn compile(self: @This()) void {
1678-
stack.push(self.ref.lua, self.ref); // Push function ref
1678+
stack.push(self.state(), self.ref); // Push function ref
16791679
defer self.state().pop(1); // Remove from stack
16801680

16811681
self.state().codegenCompile(-1);
@@ -1701,7 +1701,7 @@ pub const Function = struct {
17011701
pub fn clone(self: Function) !Function {
17021702
try self.ref.lua.checkStack(2);
17031703

1704-
stack.push(self.ref.lua, self.ref); // Push function ref
1704+
stack.push(self.state(), self.ref); // Push function ref
17051705
self.state().cloneFunction(-1); // Clone function, pushes clone on stack
17061706

17071707
// Create a reference to the cloned function on the stack
@@ -1747,7 +1747,7 @@ pub const Function = struct {
17471747
/// // actual_line will be 3 if line 3 has executable code
17481748
/// ```
17491749
pub fn setBreakpoint(self: Function, line: i32, enabled: bool) !i32 {
1750-
stack.push(self.ref.lua, self.ref); // Push function ref
1750+
stack.push(self.state(), self.ref); // Push function ref
17511751
defer self.state().pop(1); // Remove from stack
17521752

17531753
const result = self.state().breakpoint(-1, line, enabled);
@@ -1884,7 +1884,7 @@ pub const StrBuf = struct {
18841884
/// ```
18851885
pub fn add(self: *StrBuf, value: anytype) !void {
18861886
try self.lua.checkStack(1);
1887-
stack.push(self.lua.*, value);
1887+
stack.push(&self.lua.state, value);
18881888
State.addValueAny(&self.buf, -1);
18891889
self.lua.state.pop(1);
18901890
}
@@ -2122,16 +2122,16 @@ fn call(self: Self, args: anytype, comptime R: type, is_resume: bool) !Result(R)
21222122
if (info.is_tuple) {
21232123
// Push tuple elements in order
21242124
inline for (args) |arg| {
2125-
stack.push(self, arg);
2125+
stack.push(&self.state, arg);
21262126
}
21272127
break :blk @as(u32, @intCast(info.fields.len));
21282128
} else {
2129-
stack.push(self, args);
2129+
stack.push(&self.state, args);
21302130
break :blk 1;
21312131
}
21322132
},
21332133
else => {
2134-
stack.push(self, args);
2134+
stack.push(&self.state, args);
21352135
break :blk 1;
21362136
},
21372137
}
@@ -2450,7 +2450,7 @@ test "ref types" {
24502450
const lua = try init(&std.testing.allocator);
24512451
defer lua.deinit();
24522452

2453-
stack.push(lua, testAdd);
2453+
stack.push(&lua.state, testAdd);
24542454
try expect(lua.state.isFunction(-1));
24552455
try expect(lua.state.isCFunction(-1)); // Zig functions are wrapped as C functions
24562456

@@ -2477,10 +2477,10 @@ test "dump stack" {
24772477
try expect(std.mem.indexOf(u8, empty_dump, "Lua stack is empty") != null);
24782478

24792479
// Test stack with values
2480-
stack.push(lua, @as(f64, 42.5));
2481-
stack.push(lua, true);
2482-
stack.push(lua, "hello");
2483-
stack.push(lua, @as(?i32, null));
2480+
stack.push(&lua.state, @as(f64, 42.5));
2481+
stack.push(&lua.state, true);
2482+
stack.push(&lua.state, "hello");
2483+
stack.push(&lua.state, @as(?i32, null));
24842484

24852485
const stack_size_before = lua.top();
24862486

@@ -2526,12 +2526,12 @@ test "table ops" {
25262526

25272527
// Test pushing table to stack
25282528
try table.set("test", 42);
2529-
stack.push(lua, table);
2529+
stack.push(&lua.state, table);
25302530
try expectEq(lua.top(), 1);
25312531
try expect(lua.state.isTable(-1));
25322532

25332533
// Verify we can access the table value through the pushed table
2534-
stack.push(lua, "test");
2534+
stack.push(&lua.state, "test");
25352535
_ = lua.state.getTable(-2);
25362536
try expectEq(stack.pop(lua, i32), 42);
25372537

0 commit comments

Comments
 (0)