-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathtests.zig
More file actions
3095 lines (2499 loc) · 86.7 KB
/
tests.zig
File metadata and controls
3095 lines (2499 loc) · 86.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const std = @import("std");
const testing = std.testing;
const ArrayList = std.ArrayListUnmanaged; // delete this once 0.14 support is no longer necessary
const zlua = @import("zlua");
const Buffer = zlua.Buffer;
const DebugInfo = zlua.DebugInfo;
const Lua = zlua.Lua;
const expect = testing.expect;
const expectEqual = testing.expectEqual;
const expectEqualStrings = testing.expectEqualStrings;
const expectError = testing.expectError;
fn expectStringContains(actual: []const u8, expected_contains: []const u8) !void {
if (std.mem.indexOf(u8, actual, expected_contains) == null) return;
return error.LuaTestExpectedStringContains;
}
/// Return true if zlua.lang matches any of the given langs
inline fn langIn(langs: anytype) bool {
inline for (langs) |lang| if (zlua.lang == lang) return true;
return false;
}
test "initialization" {
// initialize the Zig wrapper
const lua: *Lua = try .init(testing.allocator);
try expectEqual(zlua.Status.ok, lua.status());
lua.deinit();
// attempt to initialize the Zig wrapper with no memory
try expectError(error.OutOfMemory, Lua.init(testing.failing_allocator));
}
test "Zig allocator access" {
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
const inner = struct {
fn inner(l: *Lua) !i32 {
const allocator = l.allocator();
const num = try l.toInteger(1);
// Use the allocator
const nums = try allocator.alloc(i32, @intCast(num));
defer allocator.free(nums);
// Do something pointless to use the slice
var sum: i32 = 0;
for (nums, 0..) |*n, i| n.* = @intCast(i);
for (nums) |n| sum += n;
l.pushInteger(sum);
return 1;
}
}.inner;
lua.pushFunction(zlua.wrap(inner));
lua.pushInteger(10);
try lua.protectedCall(.{ .args = 1, .results = 1 });
try expectEqual(45, try lua.toInteger(-1));
}
test "standard library loading" {
// open all standard libraries
{
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
lua.openLibs();
}
// open all standard libraries with individual functions
// these functions are only useful if you want to load the standard
// packages into a non-standard table
{
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
lua.openBase();
lua.openString();
lua.openTable();
lua.openMath();
lua.openOS();
lua.openDebug();
if (zlua.lang != .lua51 and zlua.lang != .lua53 and zlua.lang != .lua54) lua.openBit32();
if (zlua.lang != .luau) {
lua.openPackage();
lua.openIO();
}
if (zlua.lang != .lua51 and zlua.lang != .luajit) lua.openCoroutine();
if (zlua.lang != .lua51 and zlua.lang != .lua52 and zlua.lang != .luajit) lua.openUtf8();
}
}
test "number conversion success and failure" {
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
_ = lua.pushString("1234.5678");
try expectEqual(1234.5678, try lua.toNumber(-1));
_ = lua.pushString("1234");
try expectEqual(1234, try lua.toInteger(-1));
lua.pushNil();
try expectError(error.LuaError, lua.toNumber(-1));
try expectError(error.LuaError, lua.toInteger(-1));
_ = lua.pushString("fail");
try expectError(error.LuaError, lua.toNumber(-1));
try expectError(error.LuaError, lua.toInteger(-1));
}
test "arithmetic (lua_arith)" {
if (!langIn(.{ .lua52, .lua53, .lua54 })) return;
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
lua.pushNumber(10);
lua.pushNumber(42);
lua.arith(.add);
try expectEqual(52, try lua.toNumber(1));
lua.pushNumber(12);
lua.arith(.sub);
try expectEqual(40, try lua.toNumber(1));
lua.pushNumber(2);
lua.arith(.mul);
try expectEqual(80, try lua.toNumber(1));
lua.pushNumber(8);
lua.arith(.div);
try expectEqual(10, try lua.toNumber(1));
lua.pushNumber(3);
lua.arith(.mod);
try expectEqual(1, try lua.toNumber(1));
lua.arith(.negate);
try expectEqual(-1, try lua.toNumber(1));
if (zlua.lang == .lua52) return;
lua.arith(.negate);
lua.pushNumber(2);
lua.arith(.shl);
try expectEqual(4, try lua.toInteger(1));
lua.pushNumber(1);
lua.arith(.shr);
try expectEqual(2, try lua.toInteger(1));
lua.pushNumber(4);
lua.arith(.bor);
try expectEqual(6, try lua.toInteger(1));
lua.pushNumber(1);
lua.arith(.band);
try expectEqual(0, try lua.toInteger(1));
lua.pushNumber(1);
lua.arith(.bxor);
try expectEqual(1, try lua.toInteger(1));
lua.arith(.bnot); // 0xFFFFFFFFFFFFFFFE which is -2
try expectEqual(-2, try lua.toInteger(1));
lua.pushNumber(3);
lua.arith(.pow);
try expectEqual(-8, try lua.toInteger(1));
lua.pushNumber(11);
lua.pushNumber(2);
lua.arith(.int_div);
try expectEqual(5, try lua.toNumber(-1));
}
test "compare" {
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
lua.pushNumber(1);
lua.pushNumber(2);
if (langIn(.{ .lua52, .lua53, .lua54 })) {
try expect(!lua.compare(-2, -1, .eq));
try expect(!lua.compare(-1, -2, .le));
try expect(!lua.compare(-1, -2, .lt));
try expect(lua.compare(-2, -1, .le));
try expect(lua.compare(-2, -1, .lt));
try expect(!lua.rawEqual(-1, -2));
lua.pushNumber(2);
try expect(lua.rawEqual(-1, -2));
} else {
try testing.expect(!lua.equal(1, 2));
try testing.expect(lua.lessThan(1, 2));
lua.pushInteger(2);
try testing.expect(lua.equal(2, 3));
}
}
const add = struct {
fn addInner(l: *Lua) i32 {
const a = l.toInteger(1) catch 0;
const b = l.toInteger(2) catch 0;
l.pushInteger(a + b);
return 1;
}
}.addInner;
test "type of and getting values" {
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
lua.pushNil();
try expect(lua.isNil(1));
try expect(lua.isNoneOrNil(1));
try expect(lua.isNoneOrNil(2));
try expect(lua.isNone(2));
try expectEqual(.nil, lua.typeOf(1));
lua.pushBoolean(true);
try expectEqual(.boolean, lua.typeOf(-1));
try expect(lua.isBoolean(-1));
lua.newTable();
try expectEqual(.table, lua.typeOf(-1));
try expect(lua.isTable(-1));
lua.pushInteger(1);
try expectEqual(.number, lua.typeOf(-1));
try expect(lua.isNumber(-1));
try expectEqual(1, try lua.toInteger(-1));
try expectEqualStrings("number", lua.typeNameIndex(-1));
var value: i32 = 0;
lua.pushLightUserdata(&value);
try expectEqual(.light_userdata, lua.typeOf(-1));
try expect(lua.isLightUserdata(-1));
try expect(lua.isUserdata(-1));
lua.pushNumber(0.1);
try expectEqual(.number, lua.typeOf(-1));
try expect(lua.isNumber(-1));
try expectEqual(0.1, try lua.toNumber(-1));
_ = lua.pushThread();
try expectEqual(.thread, lua.typeOf(-1));
try expect(lua.isThread(-1));
try expectEqual(lua, (try lua.toThread(-1)));
try expectEqualStrings("all your codebase are belong to us", lua.pushStringZ("all your codebase are belong to us"));
try expectEqual(.string, lua.typeOf(-1));
try expect(lua.isString(-1));
lua.pushFunction(zlua.wrap(add));
try expectEqual(.function, lua.typeOf(-1));
try expect(lua.isCFunction(-1));
try expect(lua.isFunction(-1));
try expectEqual(zlua.wrap(add), try lua.toCFunction(-1));
try expectEqualStrings("hello world", lua.pushString("hello world"));
try expectEqual(.string, lua.typeOf(-1));
try expect(lua.isString(-1));
_ = lua.pushFString("%s %s %d", .{ "hello", "world", @as(i32, 10) });
try expectEqual(.string, lua.typeOf(-1));
try expect(lua.isString(-1));
try expectEqualStrings("hello world 10", try lua.toString(-1));
lua.pushValue(2);
try expectEqual(.boolean, lua.typeOf(-1));
try expect(lua.isBoolean(-1));
}
test "typenames" {
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
try expectEqualStrings("no value", lua.typeName(.none));
try expectEqualStrings("nil", lua.typeName(.nil));
try expectEqualStrings("boolean", lua.typeName(.boolean));
try expectEqualStrings("userdata", lua.typeName(.light_userdata));
try expectEqualStrings("number", lua.typeName(.number));
try expectEqualStrings("string", lua.typeName(.string));
try expectEqualStrings("table", lua.typeName(.table));
try expectEqualStrings("function", lua.typeName(.function));
try expectEqualStrings("userdata", lua.typeName(.userdata));
try expectEqualStrings("thread", lua.typeName(.thread));
if (zlua.lang == .luau) {
try expectEqualStrings("vector", lua.typeName(.vector));
}
}
test "unsigned" {
if (zlua.lang != .lua52) return;
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
lua.pushUnsigned(123456);
try expectEqual(123456, try lua.toUnsigned(-1));
_ = lua.pushString("hello");
try expectError(error.LuaError, lua.toUnsigned(-1));
}
test "executing string contents" {
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
lua.openLibs();
try lua.loadString("f = function(x) return x + 10 end");
try lua.protectedCall(.{});
try lua.loadString("a = f(2)");
try lua.protectedCall(.{});
try expectEqual(.number, try lua.getGlobal("a"));
try expectEqual(12, try lua.toInteger(1));
try expectError(if (zlua.lang == .luau) error.LuaError else error.LuaSyntax, lua.loadString("bad syntax"));
try lua.loadString("a = g()");
try expectError(error.LuaRuntime, lua.protectedCall(.{}));
}
test "filling and checking the stack" {
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
try expectEqual(0, lua.getTop());
// We want to push 30 values onto the stack
// this should work without fail
try lua.checkStack(30);
var count: i32 = 0;
while (count < 30) : (count += 1) {
lua.pushNil();
}
try expectEqual(30, lua.getTop());
// this should fail (beyond max stack size)
try expectError(error.LuaError, lua.checkStack(1_000_000));
// this is small enough it won't fail (would raise an error if it did)
lua.checkStackErr(40, null);
while (count < 40) : (count += 1) {
lua.pushNil();
}
try expectEqual(40, lua.getTop());
}
test "stack manipulation" {
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
// TODO: combine these more
if (zlua.lang == .lua53 or zlua.lang == .lua54) {
var num: i32 = 1;
while (num <= 10) : (num += 1) {
lua.pushInteger(num);
}
try expectEqual(10, lua.getTop());
lua.setTop(12);
try expectEqual(12, lua.getTop());
try expect(lua.isNil(-1));
// rotate the two nils to the bottom of the stack
lua.rotate(1, 2);
try expect(lua.isNil(1));
try expect(lua.isNil(2));
lua.remove(2);
try expect(lua.isNil(1));
try expect(lua.isInteger(2));
lua.insert(1);
try expect(lua.isInteger(1));
try expect(lua.isNil(2));
lua.replace(2);
try expect(lua.isInteger(2));
try expectEqual(10, lua.getTop());
lua.copy(1, 2);
try expectEqual(10, try lua.toInteger(1));
try expectEqual(10, try lua.toInteger(2));
try expectEqual(1, try lua.toInteger(3));
try expectEqual(8, try lua.toInteger(-1));
lua.setTop(0);
try expectEqual(0, lua.getTop());
} else {
var num: i32 = 1;
while (num <= 10) : (num += 1) {
lua.pushInteger(num);
}
try expectEqual(10, lua.getTop());
lua.setTop(12);
try expectEqual(12, lua.getTop());
try expect(lua.isNil(-1));
lua.remove(1);
try expect(lua.isNil(-1));
lua.insert(1);
try expect(lua.isNil(1));
if (zlua.lang == .lua52) {
lua.copy(1, 2);
try expectEqual(3, try lua.toInteger(3));
try expectEqual(10, try lua.toInteger(-2));
}
lua.setTop(0);
try expectEqual(0, lua.getTop());
}
}
test "calling a function" {
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
lua.register("zigadd", zlua.wrap(add));
_ = try lua.getGlobal("zigadd");
lua.pushInteger(10);
lua.pushInteger(32);
// protectedCall is preferred, but we might as well test call when we know it is safe
lua.call(.{ .args = 2, .results = 1 });
try expectEqual(42, try lua.toInteger(1));
}
test "calling a function with cProtectedCall" {
if (zlua.lang != .lua51) return;
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
var value: i32 = 1234;
const testFn = struct {
fn inner(l: *Lua) !i32 {
const passedValue = try l.toUserdata(i32, 1);
if (passedValue.* != 1234) unreachable;
return 0;
}
}.inner;
// cProtectedCall doesn't return values on the stack, so the test just makes
// sure things work!
try lua.cProtectedCall(zlua.wrap(testFn), &value);
}
test "version" {
if (zlua.lang == .lua51 or zlua.lang == .luau or zlua.lang == .luajit) return;
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
switch (zlua.lang) {
.lua52 => try expectEqual(502, lua.version(false).*),
.lua53 => try expectEqual(503, lua.version(false).*),
.lua54 => try expectEqual(504, lua.version()),
else => unreachable,
}
lua.checkVersion();
}
test "string buffers" {
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
var buffer: Buffer = undefined;
buffer.init(lua);
buffer.addChar('z');
buffer.addStringZ("igl");
var str = buffer.prep();
str[0] = 'u';
str[1] = 'a';
buffer.addSize(2);
buffer.addString(" api ");
lua.pushNumber(5.1);
buffer.addValue();
buffer.pushResult();
try expectEqualStrings("ziglua api 5.1", try lua.toString(-1));
// now test a small buffer
buffer.init(lua);
var b = buffer.prep();
b[0] = 'a';
b[1] = 'b';
b[2] = 'c';
buffer.addSize(3);
b = buffer.prep();
@memcpy(b[0..23], "defghijklmnopqrstuvwxyz");
buffer.addSize(23);
buffer.pushResult();
try expectEqualStrings("abcdefghijklmnopqrstuvwxyz", try lua.toString(-1));
lua.pop(1);
if (zlua.lang == .lua51 or zlua.lang == .luajit) return;
buffer.init(lua);
b = buffer.prep();
@memcpy(b[0..3], "abc");
buffer.pushResultSize(3);
try expectEqualStrings("abc", try lua.toString(-1));
lua.pop(1);
if (zlua.lang == .luau) return;
// TODO: maybe implement this for all langs?
b = buffer.initSize(lua, 20);
@memcpy(b[0..20], "a" ** 20);
buffer.pushResultSize(20);
if (zlua.lang != .lua54) return;
try expectEqual(20, buffer.len());
buffer.sub(10);
try expectEqual(10, buffer.len());
try expectEqualStrings("a" ** 10, buffer.addr());
buffer.addGSub(" append", "append", "appended");
try expectEqualStrings("a" ** 10 ++ " appended", buffer.addr());
}
test "global table" {
if (langIn(.{ .lua51, .luajit, .luau })) return;
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
// open some libs so we can inspect them
lua.openBase();
lua.openMath();
lua.pushGlobalTable();
// find the print function
_ = lua.pushStringZ("print");
try expectEqual(.function, lua.getTable(-2));
// index the global table in the global table
try expectEqual(.table, lua.getField(-2, "_G"));
// find pi in the math table
try expectEqual(.table, lua.getField(-1, "math"));
try expectEqual(.number, lua.getField(-1, "pi"));
// but the string table should be nil
lua.pop(2);
try expectEqual(.nil, lua.getField(-1, "string"));
}
const sub = struct {
fn subInner(l: *Lua) i32 {
const a = l.toInteger(1) catch 0;
const b = l.toInteger(2) catch 0;
l.pushInteger(a - b);
return 1;
}
}.subInner;
test "function registration" {
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
if (langIn(.{ .lua51, .luajit, .luau })) {
// register all functions as part of a table
const funcs = [_]zlua.FnReg{
.{ .name = "add", .func = zlua.wrap(add) },
};
lua.newTable();
lua.registerFns(null, &funcs);
_ = lua.getField(-1, "add");
lua.pushInteger(1);
lua.pushInteger(2);
try lua.protectedCall(.{ .args = 2, .results = 1 });
try expectEqual(3, lua.toInteger(-1));
lua.setTop(0);
// register functions as globals in a library table
lua.registerFns("testlib", &funcs);
// testlib.add(1, 2)
_ = try lua.getGlobal("testlib");
_ = lua.getField(-1, "add");
lua.pushInteger(1);
lua.pushInteger(2);
try lua.protectedCall(.{ .args = 2, .results = 1 });
try expectEqual(3, lua.toInteger(-1));
return;
}
// register all functions as part of a table
const funcs = [_]zlua.FnReg{
.{ .name = "add", .func = zlua.wrap(add) },
.{ .name = "sub", .func = zlua.wrap(sub) },
.{ .name = "placeholder", .func = null },
};
lua.newTable();
lua.setFuncs(&funcs, 0);
_ = lua.getField(-1, "placeholder");
try expectEqual(.boolean, lua.typeOf(-1));
lua.pop(1);
_ = lua.getField(-1, "add");
try expectEqual(.function, lua.typeOf(-1));
lua.pop(1);
_ = lua.getField(-1, "sub");
try expectEqual(.function, lua.typeOf(-1));
// also try calling the sub function sub(42, 40)
lua.pushInteger(42);
lua.pushInteger(40);
try lua.protectedCall(.{ .args = 2, .results = 1 });
try expectEqual(2, try lua.toInteger(-1));
// now test the newlib variation to build a library from functions
// indirectly tests newLibTable
lua.newLib(&funcs);
// add functions to the global table under "funcs"
lua.setGlobal("funcs");
try lua.doString("funcs.add(10, 20)");
try lua.doString("funcs.sub('10', 20)");
try expectError(error.LuaRuntime, lua.doString("funcs.placeholder()"));
}
test "panic fn" {
if (zlua.lang == .luau) return;
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
// just test setting up the panic function
// it uses longjmp so cannot return here to test
const panicFn = zlua.wrap(struct {
fn inner(l: *Lua) i32 {
_ = l;
return 0;
}
}.inner);
try expectEqual(null, lua.atPanic(panicFn));
}
test "warn fn" {
if (zlua.lang != .lua54) return;
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
lua.warning("this message is going to the void", false);
const warnFn = zlua.wrap(struct {
fn inner(data: ?*anyopaque, msg: []const u8, to_cont: bool) void {
_ = data;
_ = to_cont;
if (!std.mem.eql(u8, msg, "this will be caught by the warnFn")) std.debug.panic("test failed", .{});
}
}.inner);
lua.setWarnF(warnFn, null);
lua.warning("this will be caught by the warnFn", false);
}
test "concat" {
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
_ = lua.pushStringZ("hello ");
lua.pushNumber(10);
_ = lua.pushStringZ(" wow!");
lua.concat(3);
if (zlua.lang == .lua53 or zlua.lang == .lua54) {
try expectEqualStrings("hello 10.0 wow!", try lua.toString(-1));
} else {
try expectEqualStrings("hello 10 wow!", try lua.toString(-1));
}
}
test "garbage collector" {
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
// because the garbage collector is an opaque, unmanaged
// thing, it is hard to test, so just run each function
lua.gcStop();
lua.gcCollect();
lua.gcRestart();
_ = lua.gcCount();
_ = lua.gcCountB();
lua.gcStep(10);
if (zlua.lang != .lua51 and zlua.lang != .luajit) _ = lua.gcIsRunning();
if (langIn(.{ .lua51, .lua52, .lua53 })) {
_ = lua.gcSetPause(2);
_ = lua.gcSetStepMul(2);
}
if (zlua.lang == .lua52) {
lua.gcSetGenerational();
lua.gcSetGenerational();
} else if (zlua.lang == .lua54) {
try expect(lua.gcSetGenerational(0, 10));
try expect(lua.gcSetIncremental(0, 0, 0));
try expect(!lua.gcSetIncremental(0, 0, 0));
} else if (zlua.lang == .luau) {
_ = lua.gcSetGoal(10);
_ = lua.gcSetStepMul(2);
_ = lua.gcSetStepSize(1);
}
}
test "extra space" {
if (zlua.lang != .lua53 and zlua.lang != .lua54) return;
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
const space: *align(1) usize = @ptrCast(lua.getExtraSpace().ptr);
space.* = 1024;
// each new thread is initialized with a copy of the extra space from the main thread
var thread = lua.newThread();
try expectEqual(1024, @as(*align(1) usize, @ptrCast(thread.getExtraSpace())).*);
}
test "table access" {
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
try lua.doString("a = { [1] = 'first', key = 'value', ['other one'] = 1234 }");
_ = try lua.getGlobal("a");
if (zlua.lang == .lua53 or zlua.lang == .lua54) {
try expectEqual(.string, lua.rawGetIndex(1, 1));
try expectEqualStrings("first", try lua.toString(-1));
}
try expectEqual(.string, switch (zlua.lang) {
.lua53, .lua54 => lua.getIndex(1, 1),
else => lua.rawGetIndex(1, 1),
});
try expectEqualStrings("first", try lua.toString(-1));
_ = lua.pushStringZ("key");
try expectEqual(.string, lua.getTable(1));
try expectEqualStrings("value", try lua.toString(-1));
_ = lua.pushStringZ("other one");
try expectEqual(.number, lua.rawGetTable(1));
try expectEqual(1234, try lua.toInteger(-1));
// a.name = "ziglua"
_ = lua.pushStringZ("name");
_ = lua.pushStringZ("ziglua");
lua.setTable(1);
// a.lang = "zig"
_ = lua.pushStringZ("lang");
_ = lua.pushStringZ("zig");
lua.rawSetTable(1);
try expectError(error.LuaError, lua.getMetatable(1));
// create a metatable (it isn't a useful one)
lua.newTable();
lua.pushFunction(zlua.wrap(add));
lua.setField(-2, "__len");
lua.setMetatable(1);
try lua.getMetatable(1);
_ = try lua.getMetaField(1, "__len");
try expectError(error.LuaError, lua.getMetaField(1, "__index"));
lua.pushBoolean(true);
lua.setField(1, "bool");
try lua.doString("b = a.bool");
try expectEqual(.boolean, try lua.getGlobal("b"));
try expect(lua.toBoolean(-1));
// create array [1, 2, 3, 4, 5]
lua.createTable(0, 0);
var index: i32 = 1;
while (index <= 5) : (index += 1) {
lua.pushInteger(index);
if (zlua.lang == .lua53 or zlua.lang == .lua54) lua.setIndex(-2, index) else lua.rawSetIndex(-2, index);
}
if (!langIn(.{ .lua51, .luajit, .luau })) {
try expectEqual(5, lua.rawLen(-1));
try expectEqual(5, lua.lenRaiseErr(-1));
}
// add a few more
while (index <= 10) : (index += 1) {
lua.pushInteger(index);
lua.rawSetIndex(-2, index);
}
}
test "conversions" {
if (zlua.lang != .lua53 and zlua.lang != .lua54) return;
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
// number conversion
try expectEqual(3, Lua.numberToInteger(3.14));
try expectError(error.LuaError, Lua.numberToInteger(@as(zlua.Number, @floatFromInt(zlua.max_integer)) + 10));
// string conversion
try lua.stringToNumber("1");
try expect(lua.isInteger(-1));
try expectEqual(1, try lua.toInteger(1));
try lua.stringToNumber(" 1.0 ");
try expect(lua.isNumber(-1));
try expectEqual(1.0, try lua.toNumber(-1));
try expectError(error.LuaError, lua.stringToNumber("a"));
try expectError(error.LuaError, lua.stringToNumber("1.a"));
try expectError(error.LuaError, lua.stringToNumber(""));
}
test "absIndex" {
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
lua.setTop(2);
try expectEqual(@as(i32, 2), lua.absIndex(-1));
try expectEqual(@as(i32, 1), lua.absIndex(-2));
}
test "dump and load" {
if (zlua.lang == .luau) return;
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
// store a function in a global
try lua.doString("f = function(x) return function(n) return n + x end end");
// put the function on the stack
_ = try lua.getGlobal("f");
const writer = struct {
fn inner(l: *Lua, buf: []const u8, data: *anyopaque) bool {
_ = l;
var arr: *ArrayList(u8) = @ptrCast(@alignCast(data));
arr.appendSlice(std.testing.allocator, buf) catch return false;
return true;
}
}.inner;
var buffer: ArrayList(u8) = .empty;
defer buffer.deinit(std.testing.allocator);
// save the function as a binary chunk in the buffer
if (zlua.lang == .lua53 or zlua.lang == .lua54) {
try lua.dump(zlua.wrap(writer), &buffer, false);
} else {
try lua.dump(zlua.wrap(writer), &buffer);
}
// clear the stack
if (zlua.lang == .lua54) {
try lua.closeThread(lua);
} else lua.setTop(0);
const reader = struct {
fn inner(l: *Lua, data: *anyopaque) ?[]const u8 {
_ = l;
const arr: *ArrayList(u8) = @ptrCast(@alignCast(data));
return arr.items;
}
}.inner;
// now load the function back onto the stack
if (zlua.lang == .lua51 or zlua.lang == .luajit) {
try lua.load(zlua.wrap(reader), &buffer, "function");
} else {
try lua.load(zlua.wrap(reader), &buffer, "function", .binary);
}
try expectEqual(.function, lua.typeOf(-1));
// run the function (creating a new function)
lua.pushInteger(5);
try lua.protectedCall(.{ .args = 1, .results = 1 });
// now call the new function (which should return the value + 5)
lua.pushInteger(6);
try lua.protectedCall(.{ .args = 1, .results = 1 });
try expectEqual(11, try lua.toInteger(-1));
}
test "threads" {
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
var new_thread = lua.newThread();
try expectEqual(1, lua.getTop());
try expectEqual(0, new_thread.getTop());
lua.pushInteger(10);
lua.pushNil();
lua.xMove(new_thread, 2);
try expectEqual(2, new_thread.getTop());
}
test "userdata and uservalues" {
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
const Data = struct {
val: i32,
code: [4]u8,
};
// create a Lua-owned pointer to a Data with 2 associated user values
var data = if (zlua.lang == .lua54) lua.newUserdata(Data, 2) else lua.newUserdata(Data);
data.val = 1;
@memcpy(&data.code, "abcd");
try expectEqual(data, try lua.toUserdata(Data, 1));
try expectEqual(@as(*const anyopaque, @ptrCast(data)), try lua.toPointer(1));
if (zlua.lang == .lua52 or zlua.lang == .lua53) {
// assign the associated user value
lua.pushNil();
try lua.setUserValue(1);
_ = lua.getUserValue(1);
try expectEqual(.nil, lua.typeOf(-1));
} else if (zlua.lang == .lua54) {
// assign the user values
lua.pushNumber(1234.56);
try lua.setUserValue(1, 1);
_ = lua.pushStringZ("test string");
try lua.setUserValue(1, 2);
try expectEqual(.number, try lua.getUserValue(1, 1));
try expectEqual(1234.56, try lua.toNumber(-1));
try expectEqual(.string, try lua.getUserValue(1, 2));
try expectEqualStrings("test string", try lua.toString(-1));
try expectError(error.LuaError, lua.setUserValue(1, 3));
try expectError(error.LuaError, lua.getUserValue(1, 3));
}
}
test "upvalues" {
const lua: *Lua = try .init(testing.allocator);
defer lua.deinit();
// counter from PIL
const counter = struct {
fn inner(l: *Lua) !i32 {
var counter = try l.toInteger(Lua.upvalueIndex(1));
counter += 1;
l.pushInteger(counter);
l.pushInteger(counter);
l.replace(Lua.upvalueIndex(1));
return 1;
}
}.inner;
// Initialize the counter at 0
lua.pushInteger(0);
lua.pushClosure(zlua.wrap(counter), 1);