aboutsummaryrefslogtreecommitdiff
path: root/ltablib.c
diff options
context:
space:
mode:
Diffstat (limited to 'ltablib.c')
-rw-r--r--ltablib.c11
1 files changed, 7 insertions, 4 deletions
diff --git a/ltablib.c b/ltablib.c
index 46ecb5e0..15c3c09f 100644
--- a/ltablib.c
+++ b/ltablib.c
@@ -42,15 +42,17 @@ static int checkfield (lua_State *L, const char *key, int n) {
42 42
43/* 43/*
44** Check that 'arg' either is a table or can behave like one (that is, 44** Check that 'arg' either is a table or can behave like one (that is,
45** has a metatable with the required metamethods) 45** has a metatable with the required metamethods).
46*/ 46*/
47static void checktab (lua_State *L, int arg, int what) { 47static void checktab (lua_State *L, int arg, int what) {
48 if (lua_type(L, arg) != LUA_TTABLE) { /* is it not a table? */ 48 int tp = lua_type(L, arg);
49 if (tp != LUA_TTABLE) { /* is it not a table? */
49 int n = 1; /* number of elements to pop */ 50 int n = 1; /* number of elements to pop */
50 if (lua_getmetatable(L, arg) && /* must have metatable */ 51 if (lua_getmetatable(L, arg) && /* must have metatable */
51 (!(what & TAB_R) || checkfield(L, "__index", ++n)) && 52 (!(what & TAB_R) || checkfield(L, "__index", ++n)) &&
52 (!(what & TAB_W) || checkfield(L, "__newindex", ++n)) && 53 (!(what & TAB_W) || checkfield(L, "__newindex", ++n)) &&
53 (!(what & TAB_L) || checkfield(L, "__len", ++n))) { 54 (!(what & TAB_L) || /* strings don't need '__len' to have a length */
55 tp == LUA_TSTRING || checkfield(L, "__len", ++n))) {
54 lua_pop(L, n); /* pop metatable and tested metamethods */ 56 lua_pop(L, n); /* pop metatable and tested metamethods */
55 } 57 }
56 else 58 else
@@ -204,8 +206,9 @@ static int tpack (lua_State *L) {
204 206
205static int tunpack (lua_State *L) { 207static int tunpack (lua_State *L) {
206 lua_Unsigned n; 208 lua_Unsigned n;
209 lua_Integer len = aux_getn(L, 1, TAB_R);
207 lua_Integer i = luaL_optinteger(L, 2, 1); 210 lua_Integer i = luaL_optinteger(L, 2, 1);
208 lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); 211 lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, len);
209 if (i > e) return 0; /* empty range */ 212 if (i > e) return 0; /* empty range */
210 n = l_castS2U(e) - l_castS2U(i); /* number of elements minus 1 */ 213 n = l_castS2U(e) - l_castS2U(i); /* number of elements minus 1 */
211 if (l_unlikely(n >= (unsigned int)INT_MAX || 214 if (l_unlikely(n >= (unsigned int)INT_MAX ||