@@ -776,7 +776,7 @@ pub const Lua = struct {
776
776
/// Does the equivalent of t[`i`] = v where t is the table at the given `index`
777
777
/// and v is the value at the top of the stack
778
778
/// Pops the value from the stack. Does not use __newindex metavalue
779
- pub fn rawSetI (lua : * Lua , index : i32 , i : Integer ) void {
779
+ pub fn rawSetIndex (lua : * Lua , index : i32 , i : Integer ) void {
780
780
c .lua_rawseti (lua .state , index , i );
781
781
}
782
782
@@ -847,7 +847,7 @@ pub const Lua = struct {
847
847
848
848
/// Does the equivalent to t[`n`] = v where t is the value at the given `index`
849
849
/// and v is the value on the top of the stack. Pops the value from the stack
850
- pub fn setI (lua : * Lua , index : i32 , n : Integer ) void {
850
+ pub fn setIndex (lua : * Lua , index : i32 , n : Integer ) void {
851
851
c .lua_seti (lua .state , index , n );
852
852
}
853
853
@@ -899,11 +899,10 @@ pub const Lua = struct {
899
899
}
900
900
901
901
/// Converts the zero-terminated string `str` to a number, pushes that number onto the stack,
902
- /// and returns the total size of the string (length + 1)
903
- pub fn stringToNumber (lua : * Lua , str : [:0 ]const u8 ) ! usize {
902
+ /// Returns an error if conversion failed
903
+ pub fn stringToNumber (lua : * Lua , str : [:0 ]const u8 ) ! void {
904
904
const size = c .lua_stringtonumber (lua .state , str );
905
905
if (size == 0 ) return Error .Fail ;
906
- return size ;
907
906
}
908
907
909
908
/// Converts the Lua value at the given `index` into a boolean
@@ -913,9 +912,9 @@ pub const Lua = struct {
913
912
}
914
913
915
914
/// Converts a value at the given `index` into a CFn
916
- /// Returns null if the value is not a CFn
917
- pub fn toCFunction (lua : * Lua , index : i32 ) ? CFn {
918
- return c .lua_tocfunction (lua .state , index );
915
+ /// Returns an error if the value is not a CFn
916
+ pub fn toCFunction (lua : * Lua , index : i32 ) ! CFn {
917
+ return c .lua_tocfunction (lua .state , index ) orelse return Error . Fail ;
919
918
}
920
919
921
920
/// Marks the given index in the stack as a to-be-closed slot
@@ -976,10 +975,11 @@ pub const Lua = struct {
976
975
977
976
/// Converts the value at the given `index` to a Lua thread (wrapped with a `Lua` struct)
978
977
/// The thread does _not_ contain an allocator because it is not the main thread and should therefore not be used with `deinit()`
979
- pub fn toThread (lua : * Lua , index : i32 ) ? Lua {
978
+ /// Returns an error if the value is not a thread
979
+ pub fn toThread (lua : * Lua , index : i32 ) ! Lua {
980
980
const thread = c .lua_tothread (lua .state , index );
981
981
if (thread ) | thread_ptr | return Lua { .state = thread_ptr };
982
- return null ;
982
+ return Error . Fail ;
983
983
}
984
984
985
985
/// If the value at the given `index` is a full userdata, returns its memory-block address
@@ -1958,7 +1958,7 @@ test "compare" {
1958
1958
try expect (lua .compare (-2 , -1 , .lt ));
1959
1959
}
1960
1960
1961
- test "type of" {
1961
+ test "type of and getting values " {
1962
1962
var lua = try Lua .init (testing .allocator );
1963
1963
defer lua .deinit ();
1964
1964
@@ -2017,6 +2017,13 @@ test "type of" {
2017
2017
try expect (lua .isBoolean (13 ));
2018
2018
2019
2019
try expectEqualStrings ("hello world 10" , lua .toString (12 ).? );
2020
+
2021
+ // the created thread should equal the main thread (but created thread has no allocator ref)
2022
+ try expectEqual (lua .state , (try lua .toThread (7 )).state );
2023
+ try expectEqual (@as (CFn , wrap (add )), try lua .toCFunction (10 ));
2024
+
2025
+ try expectEqual (@as (Number , 0.1 ), try lua .toNumberX (6 ));
2026
+ try expectEqual (@as (Integer , 1 ), try lua .toIntegerX (3 ));
2020
2027
}
2021
2028
2022
2029
test "typenames" {
@@ -2354,16 +2361,59 @@ test "table access" {
2354
2361
try lua .doString ("b = a.bool" );
2355
2362
try expectEqual (LuaType .boolean , lua .getGlobal ("b" ));
2356
2363
try expect (lua .toBoolean (-1 ));
2364
+
2365
+ // create array [1, 2, 3, 4, 5]
2366
+ lua .createTable (0 , 0 );
2367
+ var index : Integer = 1 ;
2368
+ while (index <= 5 ) : (index += 1 ) {
2369
+ lua .pushInteger (index );
2370
+ lua .setIndex (-2 , index );
2371
+ }
2372
+ try expectEqual (@as (Unsigned , 5 ), lua .rawLen (-1 ));
2373
+ try expectEqual (@as (Integer , 5 ), lua .lenAux (-1 ));
2374
+
2375
+ // add a few more
2376
+ while (index <= 10 ) : (index += 1 ) {
2377
+ lua .pushInteger (index );
2378
+ lua .rawSetIndex (-2 , index );
2379
+ }
2380
+ try expectEqual (@as (Unsigned , 10 ), lua .rawLen (-1 ));
2381
+ }
2382
+
2383
+ test "conversions" {
2384
+ var lua = try Lua .init (testing .allocator );
2385
+ defer lua .deinit ();
2386
+
2387
+ // number conversion
2388
+ var value : Integer = undefined ;
2389
+ try Lua .numberToInteger (3.14 , & value );
2390
+ try expectEqual (@as (Integer , 3 ), value );
2391
+ try expectError (Error .Fail , Lua .numberToInteger (@intToFloat (Number , max_integer ) + 10 , & value ));
2392
+
2393
+ // string conversion
2394
+ try lua .stringToNumber ("1" );
2395
+ try expect (lua .isInteger (-1 ));
2396
+ try expectEqual (@as (Integer , 1 ), lua .toInteger (1 ));
2397
+
2398
+ try lua .stringToNumber (" 1.0 " );
2399
+ try expect (lua .isNumber (-1 ));
2400
+ try expectEqual (@as (Number , 1.0 ), lua .toNumber (-1 ));
2401
+
2402
+ try expectError (Error .Fail , lua .stringToNumber ("a" ));
2403
+ try expectError (Error .Fail , lua .stringToNumber ("1.a" ));
2404
+ try expectError (Error .Fail , lua .stringToNumber ("" ));
2405
+
2406
+ // index conversion
2407
+ try expectEqual (@as (i32 , 2 ), lua .absIndex (-1 ));
2408
+ try expectEqual (@as (i32 , 1 ), lua .absIndex (-2 ));
2357
2409
}
2358
2410
2359
2411
test "refs" {
2360
2412
// temporary test that includes a reference to all functions so
2361
2413
// they will be type-checked
2362
2414
2363
2415
// stdlib
2364
- _ = Lua .absIndex ;
2365
2416
_ = Lua .closeSlot ;
2366
- _ = Lua .createTable ;
2367
2417
_ = Lua .dump ;
2368
2418
_ = Lua .raiseError ;
2369
2419
_ = Lua .getIUserValue ;
@@ -2372,27 +2422,18 @@ test "refs" {
2372
2422
_ = Lua .newThread ;
2373
2423
_ = Lua .newUserdataUV ;
2374
2424
_ = Lua .next ;
2375
- _ = Lua .numberToInteger ;
2376
2425
_ = Lua .rawEqual ;
2377
2426
_ = Lua .rawGetP ;
2378
- _ = Lua .rawLen ;
2379
2427
_ = Lua .rawSet ;
2380
- _ = Lua .rawSetI ;
2381
2428
_ = Lua .rawSetP ;
2382
2429
_ = Lua .resetThread ;
2383
- _ = Lua .setI ;
2384
2430
_ = Lua .setIUserValue ;
2385
2431
_ = Lua .setMetatable ;
2386
2432
_ = Lua .setTable ;
2387
2433
_ = Lua .setWarnF ;
2388
2434
_ = Lua .status ;
2389
- _ = Lua .stringToNumber ;
2390
- _ = Lua .toCFunction ;
2391
2435
_ = Lua .toClose ;
2392
- _ = Lua .toIntegerX ;
2393
- _ = Lua .toNumberX ;
2394
2436
_ = Lua .toPointer ;
2395
- _ = Lua .toThread ;
2396
2437
_ = Lua .toUserdata ;
2397
2438
_ = Lua .upvalueIndex ;
2398
2439
_ = Lua .warning ;
@@ -2436,7 +2477,6 @@ test "refs" {
2436
2477
_ = Lua .getMetatableAux ;
2437
2478
_ = Lua .getSubtable ;
2438
2479
_ = Lua .gSub ;
2439
- _ = Lua .lenAux ;
2440
2480
_ = Lua .loadBuffer ;
2441
2481
_ = Lua .loadBufferX ;
2442
2482
_ = Lua .loadFile ;
0 commit comments