@@ -15,16 +15,15 @@ ModUtil = {
1515 IndexArray = { },
1616 Entangled = { },
1717 Metatables = { }
18-
1918}
2019
2120-- Extended Global Utilities (assuming lua 5.2)
2221
2322local error , pcall , xpcall = error , pcall , xpcall
2423
25- local debug , type , table = debug , type , table
24+ local debug , type , table , unpack = debug , type , table , table.unpack
2625local function getname ( )
27- return debug.getinfo ( 2 , " n" ).name
26+ return debug.getinfo ( 2 , " n" ).name or " ? "
2827end
2928
3029-- doesn't invoke __index
@@ -33,14 +32,14 @@ local rawnext = rawnext
3332
3433local function pusherror ( f , ... )
3534 local ret = table.pack ( pcall ( f , ... ) )
36- if ret [ 1 ] then return table . rawunpack ( ret , 2 , ret .n ) end
35+ if ret [ 1 ] then return unpack ( ret , 2 , ret .n ) end
3736 error ( ret [ 2 ], 3 )
3837end
3938
4039-- invokes __next
4140function next ( t , k )
4241 local m = debug.getmetatable ( t )
43- local f = m and m . __next or rawnext
42+ local f = m and rawget ( m , ' __next' ) or rawnext
4443 return pusherror ( f , t , k )
4544end
4645
@@ -85,7 +84,7 @@ local rawinext = rawinext
8584-- invokes __inext
8685function inext ( t , i )
8786 local m = debug.getmetatable ( t )
88- local f = m and m . __inext or rawinext
87+ local f = m and rawget ( m , ' __inext' ) or rawinext
8988 return pusherror ( f , t , i )
9089end
9190
@@ -189,7 +188,7 @@ function table.remove( list, pos )
189188 if pos == nil then
190189 pos = last
191190 end
192- if pos < 1 or pos > last then
191+ if pos < 0 or pos > last + 1 then
193192 error ( " bad argument #2 to '" .. getname ( ) .. " ' (position out of bounds)" , 2 )
194193 end
195194 local value = list [ pos ]
@@ -203,16 +202,16 @@ function table.remove( list, pos )
203202 return value
204203end
205204
206- table .rawunpack = table. unpack
205+ table .rawunpack = unpack
207206-- table.unpack that respects metamethods
208207do
209- local function unpack ( t , m , i , ... )
208+ local function _unpack ( t , m , i , ... )
210209 if i < m then return ... end
211- return unpack ( t , m , i - 1 , t [ i ], ... )
210+ return _unpack ( t , m , i - 1 , t [ i ], ... )
212211 end
213212
214213 function table .unpack ( list , i , j )
215- return unpack ( list , i or 1 , j or list .n or # list or 1 )
214+ return _unpack ( list , i or 1 , j or list .n or # list or 1 )
216215 end
217216end
218217
0 commit comments