@@ -2382,6 +2382,91 @@ test "toAny struct" {
2382
2382
));
2383
2383
}
2384
2384
2385
+ test "toAny tuple from vararg" {
2386
+ var lua = try Lua .init (testing .allocator );
2387
+ defer lua .deinit ();
2388
+
2389
+ const Tuple = std .meta .Tuple (&.{ i32 , bool , i32 });
2390
+
2391
+ lua .pushInteger (100 );
2392
+ lua .pushBoolean (true );
2393
+ lua .pushInteger (300 );
2394
+
2395
+ const result = try lua .toAny (Tuple , 1 );
2396
+ try testing .expect (std .meta .eql (result , .{ 100 , true , 300 }));
2397
+
2398
+ const result_reverse = try lua .toAny (Tuple , -1 );
2399
+ try testing .expect (std .meta .eql (result_reverse , .{ 300 , true , 100 }));
2400
+
2401
+ const result_error = lua .toAny (Tuple , 2 );
2402
+ try testing .expectError (error .NotInRange , result_error );
2403
+
2404
+ const result_reverse_error = lua .toAny (Tuple , -2 );
2405
+ try testing .expectError (error .NotInRange , result_reverse_error );
2406
+ }
2407
+
2408
+ test "toAny tuple from struct" {
2409
+ var lua = try Lua .init (testing .allocator );
2410
+ defer lua .deinit ();
2411
+
2412
+ const MyType = struct {
2413
+ foo : i32 ,
2414
+ bar : bool ,
2415
+ tuple : std .meta .Tuple (&.{ i32 , bool , struct { foo : bool } }),
2416
+ };
2417
+
2418
+ try lua .doString (
2419
+ \\ value = {
2420
+ \\ ["foo"] = 10,
2421
+ \\ ["bar"] = false,
2422
+ \\ ["tuple"] = {100, false, {["foo"] = true}}
2423
+ \\ }
2424
+ );
2425
+
2426
+ const lua_type = try lua .getGlobal ("value" );
2427
+ try testing .expect (lua_type == .table );
2428
+ const my_struct = try lua .toAny (MyType , 1 );
2429
+ try testing .expect (std .meta .eql (
2430
+ my_struct ,
2431
+ MyType { .foo = 10 , .bar = false , .tuple = .{ 100 , false , .{ .foo = true } } },
2432
+ ));
2433
+ }
2434
+
2435
+ test "toAny from struct with custom toAny" {
2436
+ var lua = try Lua .init (testing .allocator );
2437
+ defer lua .deinit ();
2438
+
2439
+ const MyType = struct {
2440
+ foo : bool ,
2441
+ bar : struct {
2442
+ const Self = @This ();
2443
+ foo : i32 ,
2444
+
2445
+ pub fn ziglua_toAny (l : * Lua , a : ? std.mem.Allocator , comptime aa : bool , i : i32 ) ! Self {
2446
+ return try ziglua .Internals .toStruct (l , Self , a , aa , i );
2447
+ }
2448
+ },
2449
+ };
2450
+
2451
+ try lua .doString (
2452
+ \\ value = {
2453
+ \\ ["foo"] = true,
2454
+ \\ ["bar"] = {
2455
+ \\ ["foo"] = 12
2456
+ \\ }
2457
+ \\ }
2458
+ );
2459
+
2460
+ const lua_type = try lua .getGlobal ("value" );
2461
+ try testing .expect (lua_type == .table );
2462
+ const my_struct = try lua .toAny (MyType , 1 );
2463
+ lua .pop (-1 );
2464
+ try testing .expect (std .meta .eql (
2465
+ my_struct ,
2466
+ MyType { .foo = true , .bar = .{ .foo = 12 } },
2467
+ ));
2468
+ }
2469
+
2385
2470
test "toAny mutable string" {
2386
2471
var lua = try Lua .init (testing .allocator );
2387
2472
defer lua .deinit ();
@@ -2591,18 +2676,47 @@ test "pushAny struct" {
2591
2676
try testing .expect (value .bar == (MyType {}).bar );
2592
2677
}
2593
2678
2594
- test "pushAny anon struct" {
2679
+ test "pushAny tuple" {
2680
+ var lua = try Lua .init (testing .allocator );
2681
+ defer lua .deinit ();
2682
+
2683
+ const Tuple = std .meta .Tuple (&.{ i32 , bool , i32 });
2684
+ const value : Tuple = .{ 500 , false , 600 };
2685
+
2686
+ try lua .pushAny (value );
2687
+
2688
+ const result = try lua .toAny (Tuple , 1 );
2689
+ try testing .expect (std .meta .eql (result , .{ 500 , false , 600 }));
2690
+ }
2691
+
2692
+ test "pushAny from struct with custom pushAny" {
2595
2693
var lua = try Lua .init (testing .allocator );
2596
2694
defer lua .deinit ();
2597
2695
2598
2696
const MyType = struct {
2599
- x : i32 ,
2600
- enable : bool ,
2697
+ const Self = @This ();
2698
+ foo : i32 ,
2699
+ tuple : std .meta .Tuple (&.{ i32 , i32 }),
2700
+
2701
+ pub fn ziglua_pushAny (self : * const Self , l : * Lua ) ! void {
2702
+ l .newTable ();
2703
+
2704
+ inline for (@typeInfo (Self ).@"struct" .fields ) | f | {
2705
+ try l .pushAny (f .name );
2706
+ try l .pushAny (@field (self , f .name ));
2707
+ l .setTable (-3 );
2708
+ }
2709
+ }
2601
2710
};
2602
- try lua .pushAny (.{ .x = @as (i32 , 13 ), .enable = true });
2603
- const value = try lua .toAny (MyType , -1 );
2604
- try testing .expect (value .x == 13 );
2605
- try testing .expect (value .enable == true );
2711
+
2712
+ const value : MyType = .{ .foo = 15 , .tuple = .{ 1 , 2 } };
2713
+
2714
+ try lua .pushAny (value );
2715
+ const my_struct = try lua .toAny (MyType , 1 );
2716
+ try testing .expect (std .meta .eql (
2717
+ my_struct ,
2718
+ MyType { .foo = 15 , .tuple = .{ 1 , 2 } },
2719
+ ));
2606
2720
}
2607
2721
2608
2722
test "pushAny tagged union" {
0 commit comments