@@ -18,36 +18,12 @@ fn expectStringContains(actual: []const u8, expected_contains: []const u8) !void
18
18
return error .TestExpectedStringContains ;
19
19
}
20
20
21
- // Helper functions
22
- //
23
- // For the most part, it is easy to test each Lua version simulataneously. Although each
24
- // version offers a different API, there are more similarities than differences. Using
25
- // ziglua.lang is usually enough to handle the differences. Some common functions like
26
- // toInteger differ enough to require these helper functions to handle the differences
27
- // to keep the test code more readable
28
-
29
- // TODO: implement as much of this as is reasonable in the shared lib.zig and remove these
30
-
31
21
/// Return true if ziglua.lang matches any of the given langs
32
22
inline fn langIn (langs : anytype ) bool {
33
23
inline for (langs ) | lang | if (ziglua .lang == lang ) return true ;
34
24
return false ;
35
25
}
36
26
37
- /// toInteger that always returns an error union
38
- inline fn toInteger (lua : * Lua , index : i32 ) ! ziglua.Integer {
39
- if (ziglua .lang == .lua51 or ziglua .lang == .luajit ) {
40
- return lua .toInteger (index );
41
- } else return try lua .toInteger (index );
42
- }
43
-
44
- /// toNumber that always returns an error union
45
- inline fn toNumber (lua : * Lua , index : i32 ) ! ziglua.Number {
46
- if (ziglua .lang == .lua51 or ziglua .lang == .luajit ) {
47
- return lua .toNumber (index );
48
- } else return try lua .toNumber (index );
49
- }
50
-
51
27
fn alloc (data : ? * anyopaque , ptr : ? * anyopaque , osize : usize , nsize : usize ) callconv (.C ) ? * anyopaque {
52
28
_ = data ;
53
29
@@ -123,7 +99,7 @@ test "Zig allocator access" {
123
99
fn inner (l : * Lua ) i32 {
124
100
const allocator = l .allocator ();
125
101
126
- const num = toInteger (l , 1 ) catch unreachable ;
102
+ const num = l . toInteger (1 ) catch unreachable ;
127
103
128
104
// Use the allocator
129
105
const nums = allocator .alloc (i32 , @intCast (num )) catch unreachable ;
@@ -143,7 +119,7 @@ test "Zig allocator access" {
143
119
lua .pushInteger (10 );
144
120
try lua .protectedCall (1 , 1 , 0 );
145
121
146
- try expectEqual (45 , try toInteger (lua , -1 ));
122
+ try expectEqual (45 , try lua . toInteger (-1 ));
147
123
}
148
124
149
125
test "standard library loading" {
@@ -272,8 +248,8 @@ test "compare" {
272
248
273
249
const add = struct {
274
250
fn addInner (l : * Lua ) i32 {
275
- const a = toInteger (l , 1 ) catch 0 ;
276
- const b = toInteger (l , 2 ) catch 0 ;
251
+ const a = l . toInteger (1 ) catch 0 ;
252
+ const b = l . toInteger (2 ) catch 0 ;
277
253
l .pushInteger (a + b );
278
254
return 1 ;
279
255
}
@@ -301,7 +277,7 @@ test "type of and getting values" {
301
277
lua .pushInteger (1 );
302
278
try expectEqual (.number , lua .typeOf (-1 ));
303
279
try expect (lua .isNumber (-1 ));
304
- try expectEqual (1 , try toInteger (lua , -1 ));
280
+ try expectEqual (1 , try lua . toInteger (-1 ));
305
281
try expectEqualStrings ("number" , lua .typeNameIndex (-1 ));
306
282
307
283
var value : i32 = 0 ;
@@ -313,7 +289,7 @@ test "type of and getting values" {
313
289
lua .pushNumber (0.1 );
314
290
try expectEqual (.number , lua .typeOf (-1 ));
315
291
try expect (lua .isNumber (-1 ));
316
- try expectEqual (0.1 , try toNumber (lua , -1 ));
292
+ try expectEqual (0.1 , try lua . toNumber (-1 ));
317
293
318
294
_ = lua .pushThread ();
319
295
try expectEqual (.thread , lua .typeOf (-1 ));
@@ -388,7 +364,7 @@ test "executing string contents" {
388
364
try lua .protectedCall (0 , 0 , 0 );
389
365
390
366
try expectEqual (.number , try lua .getGlobal ("a" ));
391
- try expectEqual (12 , try toInteger (lua , 1 ));
367
+ try expectEqual (12 , try lua . toInteger (1 ));
392
368
393
369
try expectError (if (ziglua .lang == .luau ) error .Fail else error .Syntax , lua .loadString ("bad syntax" ));
394
370
try lua .loadString ("a = g()" );
@@ -505,7 +481,7 @@ test "calling a function" {
505
481
506
482
// protectedCall is preferred, but we might as well test call when we know it is safe
507
483
lua .call (2 , 1 );
508
- try expectEqual (42 , try toInteger (lua , 1 ));
484
+ try expectEqual (42 , try lua . toInteger (1 ));
509
485
}
510
486
511
487
test "calling a function with cProtectedCall" {
@@ -836,7 +812,7 @@ test "table access" {
836
812
837
813
_ = lua .pushStringZ ("other one" );
838
814
try expectEqual (.number , lua .rawGetTable (1 ));
839
- try expectEqual (1234 , try toInteger (lua , -1 ));
815
+ try expectEqual (1234 , try lua . toInteger (-1 ));
840
816
841
817
// a.name = "ziglua"
842
818
_ = lua .pushStringZ ("name" );
@@ -984,7 +960,7 @@ test "dump and load" {
984
960
// now call the new function (which should return the value + 5)
985
961
lua .pushInteger (6 );
986
962
try lua .protectedCall (1 , 1 , 0 );
987
- try expectEqual (11 , try toInteger (lua , -1 ));
963
+ try expectEqual (11 , try lua . toInteger (-1 ));
988
964
}
989
965
990
966
test "threads" {
@@ -1051,7 +1027,7 @@ test "upvalues" {
1051
1027
// counter from PIL
1052
1028
const counter = struct {
1053
1029
fn inner (l : * Lua ) i32 {
1054
- var counter = toInteger (l , Lua .upvalueIndex (1 )) catch 0 ;
1030
+ var counter = l . toInteger (Lua .upvalueIndex (1 )) catch 0 ;
1055
1031
counter += 1 ;
1056
1032
l .pushInteger (counter );
1057
1033
l .pushInteger (counter );
@@ -1070,7 +1046,7 @@ test "upvalues" {
1070
1046
while (expected <= 10 ) : (expected += 1 ) {
1071
1047
_ = try lua .getGlobal ("counter" );
1072
1048
lua .call (0 , 1 );
1073
- try expectEqual (expected , try toInteger (lua , -1 ));
1049
+ try expectEqual (expected , try lua . toInteger (-1 ));
1074
1050
lua .pop (1 );
1075
1051
}
1076
1052
}
@@ -1096,7 +1072,7 @@ test "table traversal" {
1096
1072
},
1097
1073
.number = > {
1098
1074
try expectEqualStrings ("third" , try lua .toString (-2 ));
1099
- try expectEqual (1 , try toInteger (lua , -1 ));
1075
+ try expectEqual (1 , try lua . toInteger (-1 ));
1100
1076
},
1101
1077
else = > unreachable ,
1102
1078
}
@@ -1456,25 +1432,25 @@ test "checkOption" {
1456
1432
lua .pushFunction (function );
1457
1433
_ = lua .pushStringZ ("one" );
1458
1434
try lua .protectedCall (1 , 1 , 0 );
1459
- try expectEqual (1 , try toInteger (lua , -1 ));
1435
+ try expectEqual (1 , try lua . toInteger (-1 ));
1460
1436
lua .pop (1 );
1461
1437
1462
1438
lua .pushFunction (function );
1463
1439
_ = lua .pushStringZ ("two" );
1464
1440
try lua .protectedCall (1 , 1 , 0 );
1465
- try expectEqual (2 , try toInteger (lua , -1 ));
1441
+ try expectEqual (2 , try lua . toInteger (-1 ));
1466
1442
lua .pop (1 );
1467
1443
1468
1444
lua .pushFunction (function );
1469
1445
_ = lua .pushStringZ ("three" );
1470
1446
try lua .protectedCall (1 , 1 , 0 );
1471
- try expectEqual (3 , try toInteger (lua , -1 ));
1447
+ try expectEqual (3 , try lua . toInteger (-1 ));
1472
1448
lua .pop (1 );
1473
1449
1474
1450
// try the default now
1475
1451
lua .pushFunction (function );
1476
1452
try lua .protectedCall (0 , 1 , 0 );
1477
- try expectEqual (1 , try toInteger (lua , -1 ));
1453
+ try expectEqual (1 , try lua . toInteger (-1 ));
1478
1454
lua .pop (1 );
1479
1455
1480
1456
// check the raised error
@@ -1515,7 +1491,7 @@ test "loadBuffer" {
1515
1491
1516
1492
try lua .protectedCall (0 , ziglua .mult_return , 0 );
1517
1493
_ = try lua .getGlobal ("global" );
1518
- try expectEqual (10 , try toInteger (lua , -1 ));
1494
+ try expectEqual (10 , try lua . toInteger (-1 ));
1519
1495
}
1520
1496
1521
1497
test "where" {
@@ -1608,7 +1584,7 @@ test "metatables" {
1608
1584
}
1609
1585
1610
1586
try lua .callMeta (-1 , "__len" );
1611
- try expectEqual (10 , try toNumber (lua , -1 ));
1587
+ try expectEqual (10 , try lua . toNumber (-1 ));
1612
1588
}
1613
1589
1614
1590
test "args and errors" {
@@ -1987,7 +1963,7 @@ test "debug interface Lua 5.1 and Luau" {
1987
1963
l .getInfo (.{ .l = true }, i );
1988
1964
if (i .current_line .? != 2 ) std .debug .panic ("Expected line to be 2" , .{});
1989
1965
_ = l .getLocal (i , 1 ) catch unreachable ;
1990
- if ((l .toNumber (-1 )) != 3 ) std .debug .panic ("Expected x to equal 3" , .{});
1966
+ if ((l .toNumber (-1 ) catch unreachable ) != 3 ) std .debug .panic ("Expected x to equal 3" , .{});
1991
1967
},
1992
1968
.line = > if (i .current_line .? == 4 ) {
1993
1969
// modify the value of y to be 0 right before returning
@@ -1998,7 +1974,7 @@ test "debug interface Lua 5.1 and Luau" {
1998
1974
l .getInfo (.{ .l = true }, i );
1999
1975
if (i .current_line .? != 4 ) std .debug .panic ("Expected line to be 4" , .{});
2000
1976
_ = l .getLocal (i , 1 ) catch unreachable ;
2001
- if ((l .toNumber (-1 )) != 3 ) std .debug .panic ("Expected result to equal 3" , .{});
1977
+ if ((l .toNumber (-1 ) catch unreachable ) != 3 ) std .debug .panic ("Expected result to equal 3" , .{});
2002
1978
},
2003
1979
else = > unreachable ,
2004
1980
}
@@ -2038,7 +2014,7 @@ test "debug upvalues" {
2038
2014
2039
2015
// inspect the upvalue (should be x)
2040
2016
try expectEqualStrings (if (ziglua .lang == .luau ) "" else "x" , try lua .getUpvalue (-1 , 1 ));
2041
- try expectEqual (1 , try toNumber (lua , -1 ));
2017
+ try expectEqual (1 , try lua . toNumber (-1 ));
2042
2018
lua .pop (1 );
2043
2019
2044
2020
// now make the function an "add five" function
@@ -2051,7 +2027,7 @@ test "debug upvalues" {
2051
2027
// call the new function (should return 7)
2052
2028
lua .pushNumber (2 );
2053
2029
try lua .protectedCall (1 , 1 , 0 );
2054
- try expectEqual (7 , try toNumber (lua , -1 ));
2030
+ try expectEqual (7 , try lua . toNumber (-1 ));
2055
2031
2056
2032
if (langIn (.{ .lua51 , .luajit , .luau })) return ;
2057
2033
@@ -2565,12 +2541,12 @@ test "pushAny" {
2565
2541
2566
2542
//int
2567
2543
try lua .pushAny (1 );
2568
- const my_int = try toInteger (lua , -1 );
2544
+ const my_int = try lua . toInteger (-1 );
2569
2545
try testing .expect (my_int == 1 );
2570
2546
2571
2547
//float
2572
2548
try lua .pushAny (1.0 );
2573
- const my_float = try toNumber (lua , -1 );
2549
+ const my_float = try lua . toNumber (-1 );
2574
2550
try testing .expect (my_float == 1.0 );
2575
2551
2576
2552
//bool
0 commit comments