@@ -711,6 +711,7 @@ pub const Lua = struct {
711
711
}
712
712
713
713
/// This function allocates a new userdata of the given type
714
+ /// Returns a pointer to the Lua-owned data
714
715
/// See https://www.lua.org/manual/5.2/manual.html#lua_newuserdata
715
716
pub fn newUserdata (lua : * Lua , comptime T : type ) * T {
716
717
// safe to .? because this function throws a Lua error on out of memory
@@ -719,6 +720,15 @@ pub const Lua = struct {
719
720
return opaqueCast (T , ptr );
720
721
}
721
722
723
+ /// This function creates and pushes a slice of full userdata onto the stack.
724
+ /// Returns a slice to the Lua-owned data.
725
+ /// See https://www.lua.org/manual/5.2/manual.html#lua_newuserdata
726
+ pub fn newUserdataSlice (lua : * Lua , comptime T : type , size : usize ) []T {
727
+ // safe to .? because this function throws a Lua error on out of memory
728
+ const ptr = c .lua_newuserdata (lua .state , @sizeOf (T ) * size ).? ;
729
+ return @ptrCast ([* ]T , @alignCast (@alignOf ([* ]T ), ptr ))[0.. size ];
730
+ }
731
+
722
732
/// Pops a key from the stack, and pushes a key-value pair from the table at the given index
723
733
/// See https://www.lua.org/manual/5.2/manual.html#lua_next
724
734
pub fn next (lua : * Lua , index : i32 ) bool {
@@ -1084,14 +1094,26 @@ pub const Lua = struct {
1084
1094
return result ;
1085
1095
}
1086
1096
1087
- /// Returns a pointer of the given type to the userdata at the given index.
1088
- /// Works for both full and light userdata. Otherwise returns an error.
1097
+ /// Returns a Lua-owned userdata pointer of the given type at the given index.
1098
+ /// Works for both light and full userdata.
1099
+ /// Returns an error if the value is not a userdata.
1089
1100
/// See https://www.lua.org/manual/5.2/manual.html#lua_touserdata
1090
1101
pub fn toUserdata (lua : * Lua , comptime T : type , index : i32 ) ! * T {
1091
1102
if (c .lua_touserdata (lua .state , index )) | ptr | return opaqueCast (T , ptr );
1092
1103
return error .Fail ;
1093
1104
}
1094
1105
1106
+ /// Returns a Lua-owned userdata slice of the given type at the given index.
1107
+ /// Returns an error if the value is not a userdata.
1108
+ /// See https://www.lua.org/manual/5.2/manual.html#lua_touserdata
1109
+ pub fn toUserdataSlice (lua : * Lua , comptime T : type , index : i32 ) ! []T {
1110
+ if (c .lua_touserdata (lua .state , index )) | ptr | {
1111
+ const size = lua .rawLen (index ) / @sizeOf (T );
1112
+ return @ptrCast ([* ]T , @alignCast (@alignOf ([* ]T ), ptr ))[0.. size ];
1113
+ }
1114
+ return error .Fail ;
1115
+ }
1116
+
1095
1117
/// Returns the `LuaType` of the value at the given index
1096
1118
/// Note that this is equivalent to lua_type but because type is a Zig primitive it is renamed to `typeOf`
1097
1119
/// See https://www.lua.org/manual/5.2/manual.html#lua_type
@@ -1389,12 +1411,22 @@ pub const Lua = struct {
1389
1411
c .luaL_checktype (lua .state , arg , @enumToInt (t ));
1390
1412
}
1391
1413
1392
- /// Checks whether the function argument `arg` is a userdata of the type `type_name `
1414
+ /// Checks whether the function argument `arg` is a userdata of the type `name `
1393
1415
/// Returns the userdata's memory-block address
1394
1416
/// See https://www.lua.org/manual/5.2/manual.html#luaL_checkudata
1395
- pub fn checkUserdata (lua : * Lua , comptime T : type , arg : i32 ) * T {
1417
+ pub fn checkUserdata (lua : * Lua , comptime T : type , arg : i32 , name : [: 0 ] const u8 ) * T {
1396
1418
// the returned pointer will not be null
1397
- return opaqueCast (T , c .luaL_checkudata (lua .state , arg , @typeName (T )).? );
1419
+ return opaqueCast (T , c .luaL_checkudata (lua .state , arg , name .ptr ).? );
1420
+ }
1421
+
1422
+ /// Checks whether the function argument `arg` is a userdata of the type `name`
1423
+ /// Returns a Lua-owned userdata slice
1424
+ /// See https://www.lua.org/manual/5.2/manual.html#luaL_checkudata
1425
+ pub fn checkUserdataSlice (lua : * Lua , comptime T : type , arg : i32 , name : [:0 ]const u8 ) []T {
1426
+ // the returned pointer will not be null
1427
+ const ptr = c .luaL_checkudata (lua .state , arg , name .ptr ).? ;
1428
+ const size = lua .rawLen (arg ) / @sizeOf (T );
1429
+ return @ptrCast ([* ]T , @alignCast (@alignOf ([* ]T ), ptr ))[0.. size ];
1398
1430
}
1399
1431
1400
1432
/// Checks whether the function argument arg is a number and returns this number cast to an unsigned
@@ -1652,12 +1684,21 @@ pub const Lua = struct {
1652
1684
1653
1685
/// This function works like `Lua.checkUserdata()` except it returns a Zig error instead of raising a Lua error on fail
1654
1686
/// See https://www.lua.org/manual/5.2/manual.html#luaL_testudata
1655
- pub fn testUserdata (lua : * Lua , comptime T : type , arg : i32 ) ! * T {
1656
- if (c .luaL_testudata (lua .state , arg , @typeName ( T ) )) | ptr | {
1687
+ pub fn testUserdata (lua : * Lua , comptime T : type , arg : i32 , name : [: 0 ] const u8 ) ! * T {
1688
+ if (c .luaL_testudata (lua .state , arg , name . ptr )) | ptr | {
1657
1689
return opaqueCast (T , ptr );
1658
1690
} else return error .Fail ;
1659
1691
}
1660
1692
1693
+ /// This function works like `Lua.checkUserdataSlice()` except it returns a Zig error instead of raising a Lua error on fail
1694
+ /// See https://www.lua.org/manual/5.2/manual.html#luaL_checkudata
1695
+ pub fn testUserdataSlice (lua : * Lua , comptime T : type , arg : i32 , name : [:0 ]const u8 ) ! []T {
1696
+ if (c .luaL_testudata (lua .state , arg , name .ptr )) | ptr | {
1697
+ const size = lua .rawLen (arg ) / @sizeOf (T );
1698
+ return @ptrCast ([* ]T , @alignCast (@alignOf ([* ]T ), ptr ))[0.. size ];
1699
+ } else return error .Fail ;
1700
+ }
1701
+
1661
1702
/// Converts any Lua value at the given index into a string in a reasonable format
1662
1703
/// See https://www.lua.org/manual/5.2/manual.html#luaL_tolstring
1663
1704
pub fn toBytesFmt (lua : * Lua , index : i32 ) [:0 ]const u8 {
0 commit comments