Skip to content

Commit a213c57

Browse files
committed
Simplify pushFunction and pushClosure
This makes the api work for all versions of Lua. If Luau users wish to set the debugname for the function, they can use the pushFunctionNamed and pushClosureNamed functions.
1 parent 4cbe5ef commit a213c57

File tree

2 files changed

+53
-65
lines changed

2 files changed

+53
-65
lines changed

src/lib.zig

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,31 +1443,37 @@ pub const Lua = struct {
14431443
c.lua_pushboolean(lua.state, @intFromBool(b));
14441444
}
14451445

1446-
fn pushClosureLua(lua: *Lua, c_fn: CFn, n: i32) void {
1447-
c.lua_pushcclosure(lua.state, c_fn, n);
1448-
}
1449-
1450-
fn pushClosureLuau(lua: *Lua, c_fn: CFn, name: [:0]const u8, n: i32) void {
1451-
c.lua_pushcclosurek(lua.state, c_fn, name, n, null);
1452-
}
1453-
14541446
/// Pushes a new Closure onto the stack
14551447
/// `n` tells how many upvalues this function will have
1456-
/// See https://www.lua.org/manual/5.1/manual.html#lua_pushcclosure
1457-
pub const pushClosure = if (lang == .luau) pushClosureLuau else pushClosureLua;
1448+
/// See https://www.lua.org/manual/5.4/manual.html#lua_pushcclosure
1449+
pub fn pushClosure(lua: *Lua, c_fn: CFn, n: i32) void {
1450+
switch (lang) {
1451+
.luau => c.lua_pushcclosurek(lua.state, c_fn, "ZigFn", n, null),
1452+
else => c.lua_pushcclosure(lua.state, c_fn, n),
1453+
}
1454+
}
14581455

1459-
fn pushFunctionLua(lua: *Lua, c_fn: CFn) void {
1460-
lua.pushClosure(c_fn, 0);
1456+
/// Pushes a new Closure onto the stack with a debugname
1457+
/// `n` tells how many upvalues this function will have
1458+
/// See https://www.lua.org/manual/5.4/manual.html#lua_pushcclosure
1459+
pub fn pushClosureNamed(lua: *Lua, c_fn: CFn, debugname: [:0]const u8, n: i32) void {
1460+
c.lua_pushcclosurek(lua.state, c_fn, debugname, n, null);
14611461
}
14621462

1463-
fn pushFunctionLuau(lua: *Lua, c_fn: CFn, name: [:0]const u8) void {
1464-
lua.pushClosure(c_fn, name, 0);
1463+
/// Pushes a new function onto the stack
1464+
/// See https://www.lua.org/manual/5.4/manual.html#lua_pushcfunction
1465+
pub fn pushFunction(lua: *Lua, c_fn: CFn) void {
1466+
switch (lang) {
1467+
.luau => c.lua_pushcclosurek(lua.state, c_fn, "ZigFn", 0, null),
1468+
else => c.lua_pushcfunction(lua.state, c_fn),
1469+
}
14651470
}
14661471

1467-
/// Pushes a new Closure onto the stack
1468-
/// `n` tells how many upvalues this function will have
1469-
/// See https://www.lua.org/manual/5.4/manual.html#lua_pushcclosure
1470-
pub const pushFunction = if (lang == .luau) pushFunctionLuau else pushFunctionLua;
1472+
/// Pushes a new function onto the stack with a debugname
1473+
/// See https://www.lua.org/manual/5.4/manual.html#lua_pushcfunction
1474+
pub fn pushFunctionNamed(lua: *Lua, c_fn: CFn, debugname: [:0]const u8) void {
1475+
c.lua_pushcclosurek(lua.state, c_fn, debugname, 0, null);
1476+
}
14711477

14721478
/// Push a formatted string onto the stack and return a pointer to the string
14731479
/// See https://www.lua.org/manual/5.4/manual.html#lua_pushfstring
@@ -1672,7 +1678,7 @@ pub const Lua = struct {
16721678
pub fn register(lua: *Lua, name: [:0]const u8, c_fn: CFn) void {
16731679
switch (lang) {
16741680
.luau => {
1675-
lua.pushFunction(c_fn, name);
1681+
lua.pushFunction(c_fn);
16761682
lua.setGlobal(name);
16771683
},
16781684
else => c.lua_register(lua.state, name.ptr, c_fn),
@@ -2815,10 +2821,7 @@ pub const Lua = struct {
28152821
}
28162822
for (funcs) |f| {
28172823
// TODO: handle null functions
2818-
switch (lang) {
2819-
.luau => lua.pushFunction(f.func.?, f.name),
2820-
else => lua.pushFunction(f.func.?),
2821-
}
2824+
lua.pushFunction(f.func.?);
28222825
lua.setField(-2, f.name);
28232826
}
28242827
}
@@ -2829,12 +2832,7 @@ pub const Lua = struct {
28292832
pub fn requireF(lua: *Lua, mod_name: [:0]const u8, open_fn: CFn, global: bool) void {
28302833
switch (lang) {
28312834
.lua51, .luajit, .luau => {
2832-
if (lang == .luau) {
2833-
lua.pushFunction(open_fn, mod_name);
2834-
} else {
2835-
lua.pushFunction(open_fn);
2836-
}
2837-
2835+
lua.pushFunction(open_fn);
28382836
_ = lua.pushString(mod_name);
28392837
lua.call(1, 0);
28402838
},

src/tests.zig

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,6 @@ inline fn toNumber(lua: *Lua, index: i32) !ziglua.Number {
4848
} else return try lua.toNumber(index);
4949
}
5050

51-
/// pushFunction that sets the name for Luau
52-
inline fn pushFunction(lua: *Lua, c_fn: ziglua.CFn) void {
53-
if (ziglua.lang == .luau) return lua.pushFunction(c_fn, "");
54-
lua.pushFunction(c_fn);
55-
}
56-
5751
fn alloc(data: ?*anyopaque, ptr: ?*anyopaque, osize: usize, nsize: usize) callconv(.C) ?*anyopaque {
5852
_ = data;
5953

@@ -145,7 +139,7 @@ test "Zig allocator access" {
145139
}
146140
}.inner;
147141

148-
pushFunction(&lua, ziglua.wrap(inner));
142+
lua.pushFunction(ziglua.wrap(inner));
149143
lua.pushInteger(10);
150144
try lua.protectedCall(1, 1, 0);
151145

@@ -347,7 +341,7 @@ test "type of and getting values" {
347341
try expectEqual(.string, lua.typeOf(-1));
348342
try expect(lua.isString(-1));
349343

350-
if (ziglua.lang == .luau) lua.pushFunction(ziglua.wrap(add), "add") else lua.pushFunction(ziglua.wrap(add));
344+
lua.pushFunction(ziglua.wrap(add));
351345
try expectEqual(.function, lua.typeOf(-1));
352346
try expect(lua.isCFunction(-1));
353347
try expect(lua.isFunction(-1));
@@ -875,10 +869,7 @@ test "table access" {
875869
// create a metatable (it isn't a useful one)
876870
lua.newTable();
877871

878-
if (ziglua.lang == .luau)
879-
lua.pushFunction(ziglua.wrap(add), "add")
880-
else
881-
lua.pushFunction(ziglua.wrap(add));
872+
lua.pushFunction(ziglua.wrap(add));
882873
lua.setField(-2, "__len");
883874
lua.setMetatable(1);
884875

@@ -1087,7 +1078,7 @@ test "upvalues" {
10871078

10881079
// Initialize the counter at 0
10891080
lua.pushInteger(0);
1090-
if (ziglua.lang == .luau) lua.pushClosure(ziglua.wrap(counter), "counter", 1) else lua.pushClosure(ziglua.wrap(counter), 1);
1081+
lua.pushClosure(ziglua.wrap(counter), 1);
10911082
lua.setGlobal("counter");
10921083

10931084
// call the function repeatedly, each time ensuring the result increases by one
@@ -1191,7 +1182,7 @@ test "raise error" {
11911182
}
11921183
}.inner;
11931184

1194-
if (ziglua.lang == .luau) lua.pushFunction(ziglua.wrap(makeError), "makeError") else lua.pushFunction(ziglua.wrap(makeError));
1185+
lua.pushFunction(ziglua.wrap(makeError));
11951186
try expectError(error.Runtime, lua.protectedCall(0, 0, 0));
11961187
try expectEqualStrings("makeError made an error", try lua.toBytes(-1));
11971188
}
@@ -1307,11 +1298,10 @@ test "yielding no continuation" {
13071298
return l.yield(1);
13081299
}
13091300
}.inner);
1301+
thread.pushFunction(func);
13101302
if (ziglua.lang == .luau) {
1311-
thread.pushFunction(func, "yieldfn");
13121303
_ = try thread.resumeThread(null, 0);
13131304
} else {
1314-
thread.pushFunction(func);
13151305
_ = try thread.resumeThread(0);
13161306
}
13171307

@@ -1367,28 +1357,28 @@ test "aux check functions" {
13671357
}
13681358
}.inner);
13691359

1370-
pushFunction(&lua, function);
1360+
lua.pushFunction(function);
13711361
lua.protectedCall(0, 0, 0) catch {
13721362
try expectStringContains("argument #1", try lua.toBytes(-1));
13731363
lua.pop(-1);
13741364
};
13751365

1376-
pushFunction(&lua, function);
1366+
lua.pushFunction(function);
13771367
lua.pushNil();
13781368
lua.protectedCall(1, 0, 0) catch {
13791369
try expectStringContains("number expected", try lua.toBytes(-1));
13801370
lua.pop(-1);
13811371
};
13821372

1383-
pushFunction(&lua, function);
1373+
lua.pushFunction(function);
13841374
lua.pushNil();
13851375
lua.pushInteger(3);
13861376
lua.protectedCall(2, 0, 0) catch {
13871377
try expectStringContains("string expected", try lua.toBytes(-1));
13881378
lua.pop(-1);
13891379
};
13901380

1391-
pushFunction(&lua, function);
1381+
lua.pushFunction(function);
13921382
lua.pushNil();
13931383
lua.pushInteger(3);
13941384
_ = lua.pushBytes("hello world");
@@ -1397,7 +1387,7 @@ test "aux check functions" {
13971387
lua.pop(-1);
13981388
};
13991389

1400-
pushFunction(&lua, function);
1390+
lua.pushFunction(function);
14011391
lua.pushNil();
14021392
lua.pushInteger(3);
14031393
_ = lua.pushBytes("hello world");
@@ -1407,7 +1397,7 @@ test "aux check functions" {
14071397
lua.pop(-1);
14081398
};
14091399

1410-
pushFunction(&lua, function);
1400+
lua.pushFunction(function);
14111401
lua.pushNil();
14121402
lua.pushInteger(3);
14131403
_ = lua.pushBytes("hello world");
@@ -1432,7 +1422,7 @@ test "aux check functions" {
14321422
};
14331423
}
14341424

1435-
pushFunction(&lua, function);
1425+
lua.pushFunction(function);
14361426
// test pushFail here (currently acts the same as pushNil)
14371427
if (ziglua.lang == .lua54) lua.pushFail() else lua.pushNil();
14381428
lua.pushInteger(3);
@@ -1460,10 +1450,10 @@ test "aux opt functions" {
14601450
}
14611451
}.inner);
14621452

1463-
pushFunction(&lua, function);
1453+
lua.pushFunction(function);
14641454
try lua.protectedCall(0, 0, 0);
14651455

1466-
pushFunction(&lua, function);
1456+
lua.pushFunction(function);
14671457
lua.pushInteger(10);
14681458
_ = lua.pushBytes("zig");
14691459
lua.pushNumber(1.23);
@@ -1493,32 +1483,32 @@ test "checkOption" {
14931483
}
14941484
}.inner);
14951485

1496-
pushFunction(&lua, function);
1486+
lua.pushFunction(function);
14971487
_ = lua.pushString("one");
14981488
try lua.protectedCall(1, 1, 0);
14991489
try expectEqual(1, try toInteger(&lua, -1));
15001490
lua.pop(1);
15011491

1502-
pushFunction(&lua, function);
1492+
lua.pushFunction(function);
15031493
_ = lua.pushString("two");
15041494
try lua.protectedCall(1, 1, 0);
15051495
try expectEqual(2, try toInteger(&lua, -1));
15061496
lua.pop(1);
15071497

1508-
pushFunction(&lua, function);
1498+
lua.pushFunction(function);
15091499
_ = lua.pushString("three");
15101500
try lua.protectedCall(1, 1, 0);
15111501
try expectEqual(3, try toInteger(&lua, -1));
15121502
lua.pop(1);
15131503

15141504
// try the default now
1515-
pushFunction(&lua, function);
1505+
lua.pushFunction(function);
15161506
try lua.protectedCall(0, 1, 0);
15171507
try expectEqual(1, try toInteger(&lua, -1));
15181508
lua.pop(1);
15191509

15201510
// check the raised error
1521-
pushFunction(&lua, function);
1511+
lua.pushFunction(function);
15221512
_ = lua.pushString("unknown");
15231513
try expectError(error.Runtime, lua.protectedCall(1, 1, 0));
15241514
try expectStringContains("(invalid option 'unknown')", try lua.toBytes(-1));
@@ -1569,7 +1559,7 @@ test "where" {
15691559
}
15701560
}.inner);
15711561

1572-
pushFunction(&lua, whereFn);
1562+
lua.pushFunction(whereFn);
15731563
lua.setGlobal("whereFn");
15741564

15751565
try lua.doString(
@@ -1662,7 +1652,7 @@ test "args and errors" {
16621652
}
16631653
}.inner);
16641654

1665-
pushFunction(&lua, argCheck);
1655+
lua.pushFunction(argCheck);
16661656
try expectError(error.Runtime, lua.protectedCall(0, 0, 0));
16671657

16681658
const raisesError = ziglua.wrap(struct {
@@ -1672,7 +1662,7 @@ test "args and errors" {
16721662
}
16731663
}.inner);
16741664

1675-
pushFunction(&lua, raisesError);
1665+
lua.pushFunction(raisesError);
16761666
try expectError(error.Runtime, lua.protectedCall(0, 0, 0));
16771667
try expectEqualStrings("some error zig!", try lua.toBytes(-1));
16781668

@@ -1755,7 +1745,7 @@ test "userdata" {
17551745
}
17561746
}.inner);
17571747

1758-
pushFunction(&lua, checkUdata);
1748+
lua.pushFunction(checkUdata);
17591749

17601750
{
17611751
var t = if (ziglua.lang == .lua54) lua.newUserdata(Type, 0) else lua.newUserdata(Type);
@@ -1840,7 +1830,7 @@ test "userdata slices" {
18401830
}
18411831
}.inner;
18421832

1843-
pushFunction(&lua, ziglua.wrap(udataFn));
1833+
lua.pushFunction(ziglua.wrap(udataFn));
18441834
lua.pushValue(2);
18451835

18461836
try lua.protectedCall(1, 0, 0);
@@ -2387,7 +2377,7 @@ test "namecall" {
23872377

23882378
try lua.newMetatable("vector");
23892379
lua.pushString("__namecall");
2390-
lua.pushFunction(ziglua.wrap(funcs.vectorNamecall), "vector_namecall");
2380+
lua.pushFunctionNamed(ziglua.wrap(funcs.vectorNamecall), "vector_namecall");
23912381
lua.setTable(-3);
23922382

23932383
lua.setReadonly(-1, true);

0 commit comments

Comments
 (0)