@@ -698,6 +698,7 @@ pub const Lua = struct {
698
698
/// This function creates and pushes a new full userdata onto the stack
699
699
/// with `num_uvalue` associated Lua values, plus an associated block of raw memory with `size` bytes
700
700
/// Returns the address of the block of memory
701
+ /// TODO: rename to newUserdata?
701
702
pub fn newUserdataUV (lua : * Lua , comptime T : type , new_uvalue : i32 ) * T {
702
703
// safe to .? because this function throws a Lua error on out of memory
703
704
// so the returned pointer should never be null
@@ -721,6 +722,7 @@ pub const Lua = struct {
721
722
}
722
723
723
724
/// Calls a function (or callable object) in protected mode
725
+ /// NOTE: it might be good to make the args named struct params?
724
726
pub fn protectedCall (lua : * Lua , num_args : i32 , num_results : i32 , msg_handler : i32 ) ! void {
725
727
// The translate-c version of lua_pcall does not type-check so we must rewrite it
726
728
// (macros don't always translate well with translate-c)
@@ -1095,6 +1097,7 @@ pub const Lua = struct {
1095
1097
}
1096
1098
1097
1099
/// Returns the name of the given `LuaType` as a null-terminated slice
1100
+ /// TODO: return a spanned string
1098
1101
pub fn typeName (lua : * Lua , t : LuaType ) [* :0 ]const u8 {
1099
1102
return c .lua_typename (lua .state , @enumToInt (t ));
1100
1103
}
@@ -1297,7 +1300,7 @@ pub const Lua = struct {
1297
1300
/// Possibly never returns
1298
1301
pub fn argCheck (lua : * Lua , cond : bool , arg : i32 , extra_msg : [:0 ]const u8 ) void {
1299
1302
// translate-c failed
1300
- if (cond ) lua .typeError (arg , extra_msg );
1303
+ if (cond ) lua .argError (arg , extra_msg );
1301
1304
}
1302
1305
1303
1306
/// Raises an error reporting a problem with argument `arg` of the C function that called it
@@ -1383,7 +1386,9 @@ pub const Lua = struct {
1383
1386
1384
1387
/// Checks whether the function argument `arg` is a userdata of the type `type_name`
1385
1388
/// Returns the userdata's memory-block address
1389
+ /// TODO: accept type as param?
1386
1390
pub fn checkUserdata (lua : * Lua , arg : i32 , type_name : [:0 ]const u8 ) * anyopaque {
1391
+ // the returned pointer will not be null
1387
1392
return c .luaL_checkudata (lua .state , arg , type_name ).? ;
1388
1393
}
1389
1394
@@ -1409,11 +1414,12 @@ pub const Lua = struct {
1409
1414
1410
1415
/// Raises an error
1411
1416
pub fn raiseErrorAux (lua : * Lua , fmt : [:0 ]const u8 , args : anytype ) noreturn {
1412
- @call (.{}, c .luaL_error , .{ lua .state , fmt } ++ args );
1417
+ _ = @call (.{}, c .luaL_error , .{ lua .state , fmt } ++ args );
1418
+ unreachable ;
1413
1419
}
1414
1420
1415
1421
/// This function produces the return values for process-related functions in the standard library
1416
- pub fn exeResult (lua : * Lua , stat : i32 ) i32 {
1422
+ pub fn execResult (lua : * Lua , stat : i32 ) i32 {
1417
1423
return c .luaL_execresult (lua .state , stat );
1418
1424
}
1419
1425
@@ -1439,8 +1445,8 @@ pub const Lua = struct {
1439
1445
}
1440
1446
1441
1447
/// Ensures that the value t[`field`], where t is the value at `index`, is a table, and pushes that table onto the stack.
1442
- pub fn getSubtable (lua : * Lua , index : i32 , field : [:0 ]const u8 ) bool {
1443
- return c .luaL_getsubtable (lua .state , index , field ) != 0 ;
1448
+ pub fn getSubtable (lua : * Lua , index : i32 , field : [:0 ]const u8 ) ! void {
1449
+ if ( c .luaL_getsubtable (lua .state , index , field ) == 0 ) return error . Fail ;
1444
1450
}
1445
1451
1446
1452
/// Creates a copy of string `str`, replacing any occurrence of the string `pat` with the string `rep`
@@ -1455,7 +1461,7 @@ pub const Lua = struct {
1455
1461
return c .luaL_len (lua .state , index );
1456
1462
}
1457
1463
1458
- /// The same as `Lua.loadBufferX` with `mode` set to null
1464
+ /// The same as `Lua.loadBufferX` with `mode` set to binary+text
1459
1465
pub fn loadBuffer (lua : * Lua , buf : []const u8 , name : [:0 ]const u8 ) ! void {
1460
1466
try lua .loadBufferX (buf , name , .binary_text );
1461
1467
}
@@ -1476,21 +1482,17 @@ pub const Lua = struct {
1476
1482
}
1477
1483
}
1478
1484
1479
- /// Equivalent to `Lua.loadFileX()` with mode equal to null
1485
+ /// Equivalent to `Lua.loadFileX()` with mode equal to binary+text
1480
1486
pub fn loadFile (lua : * Lua , file_name : [:0 ]const u8 ) ! void {
1481
- return loadFileX (lua , file_name , null );
1487
+ try lua . loadFileX (file_name , .binary_text );
1482
1488
}
1483
1489
1484
1490
/// Loads a file as a Lua chunk
1485
- pub fn loadFileX (lua : * Lua , file_name : [:0 ]const u8 , mode : ? Mode ) ! void {
1486
- const mode_str = blk : {
1487
- if (mode == null ) break :blk "bt" ;
1488
-
1489
- break :blk switch (mode .? ) {
1490
- .binary = > "b" ,
1491
- .text = > "t" ,
1492
- .binary_text = > "bt" ,
1493
- };
1491
+ pub fn loadFileX (lua : * Lua , file_name : [:0 ]const u8 , mode : Mode ) ! void {
1492
+ const mode_str = switch (mode ) {
1493
+ .binary = > "b" ,
1494
+ .text = > "t" ,
1495
+ .binary_text = > "bt" ,
1494
1496
};
1495
1497
const ret = c .luaL_loadfilex (lua .state , file_name , mode_str );
1496
1498
switch (ret ) {
@@ -1581,9 +1583,9 @@ pub const Lua = struct {
1581
1583
1582
1584
/// Creates and returns a reference in the table at index `index` for the object on the top of the stack
1583
1585
/// Pops the object
1584
- pub fn ref (lua : * Lua , index : i32 ) ? i32 {
1586
+ pub fn ref (lua : * Lua , index : i32 ) ! i32 {
1585
1587
const ret = c .luaL_ref (lua .state , index );
1586
- return if (ret == ref_nil ) null else ret ;
1588
+ return if (ret == ref_nil ) error . Fail else ret ;
1587
1589
}
1588
1590
1589
1591
/// If package.loaded[`mod_name`] is not true, calls the function `open_fn` with `mod_name`
@@ -1614,9 +1616,11 @@ pub const Lua = struct {
1614
1616
c .luaL_setmetatable (lua .state , table_name );
1615
1617
}
1616
1618
1617
- /// This function works like `Lua.checkUserdata()` except it returns null instead of raising an error on fail
1618
- pub fn testUserdata (lua : * Lua , arg : i32 , type_name : [:0 ]const u8 ) ? * anyopaque {
1619
- return c .luaL_testudata (lua .state , arg , type_name );
1619
+ /// This function works like `Lua.checkUserdata()` except it returns a Zig error instead of raising a Lua error on fail
1620
+ pub fn testUserdata (lua : * Lua , arg : i32 , type_name : [:0 ]const u8 ) ! * anyopaque {
1621
+ if (c .luaL_testudata (lua .state , arg , type_name )) | ptr | {
1622
+ return ptr ;
1623
+ } else return error .Fail ;
1620
1624
}
1621
1625
1622
1626
/// Converts any Lua value at the given index into a string in a reasonable format
@@ -1627,7 +1631,7 @@ pub const Lua = struct {
1627
1631
}
1628
1632
1629
1633
/// Creates and pushes a traceback of the stack of `other`
1630
- pub fn traceback (lua : * Lua , other : Lua , msg : [:0 ]const u8 , level : i32 ) void {
1634
+ pub fn traceback (lua : * Lua , other : * Lua , msg : [:0 ]const u8 , level : i32 ) void {
1631
1635
c .luaL_traceback (lua .state , other .state , msg , level );
1632
1636
}
1633
1637
@@ -1638,8 +1642,7 @@ pub const Lua = struct {
1638
1642
}
1639
1643
1640
1644
/// Returns the name of the type of the value at the given `index`
1641
- /// TODO: maybe typeNameIndex?
1642
- pub fn typeNameAux (lua : * Lua , index : i32 ) [:0 ]const u8 {
1645
+ pub fn typeNameIndex (lua : * Lua , index : i32 ) [:0 ]const u8 {
1643
1646
return std .mem .span (c .luaL_typename (lua .state , index ));
1644
1647
}
1645
1648
0 commit comments