diff options
Diffstat (limited to '')
| -rw-r--r-- | src/lua/lapi.c (renamed from src/lua-5.3/lapi.c) | 656 |
1 files changed, 384 insertions, 272 deletions
diff --git a/src/lua-5.3/lapi.c b/src/lua/lapi.c index 711895b..3e24781 100644 --- a/src/lua-5.3/lapi.c +++ b/src/lua/lapi.c | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 2.259.1.2 2017/12/06 18:35:12 roberto Exp $ | 2 | ** $Id: lapi.c $ |
| 3 | ** Lua API | 3 | ** Lua API |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -10,6 +10,7 @@ | |||
| 10 | #include "lprefix.h" | 10 | #include "lprefix.h" |
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | #include <limits.h> | ||
| 13 | #include <stdarg.h> | 14 | #include <stdarg.h> |
| 14 | #include <string.h> | 15 | #include <string.h> |
| 15 | 16 | ||
| @@ -36,11 +37,14 @@ const char lua_ident[] = | |||
| 36 | "$LuaAuthors: " LUA_AUTHORS " $"; | 37 | "$LuaAuthors: " LUA_AUTHORS " $"; |
| 37 | 38 | ||
| 38 | 39 | ||
| 39 | /* value at a non-valid index */ | ||
| 40 | #define NONVALIDVALUE cast(TValue *, luaO_nilobject) | ||
| 41 | 40 | ||
| 42 | /* corresponding test */ | 41 | /* |
| 43 | #define isvalid(o) ((o) != luaO_nilobject) | 42 | ** Test for a valid index. |
| 43 | ** '!ttisnil(o)' implies 'o != &G(L)->nilvalue', so it is not needed. | ||
| 44 | ** However, it covers the most common cases in a faster way. | ||
| 45 | */ | ||
| 46 | #define isvalid(L, o) (!ttisnil(o) || o != &G(L)->nilvalue) | ||
| 47 | |||
| 44 | 48 | ||
| 45 | /* test for pseudo index */ | 49 | /* test for pseudo index */ |
| 46 | #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX) | 50 | #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX) |
| @@ -48,49 +52,46 @@ const char lua_ident[] = | |||
| 48 | /* test for upvalue */ | 52 | /* test for upvalue */ |
| 49 | #define isupvalue(i) ((i) < LUA_REGISTRYINDEX) | 53 | #define isupvalue(i) ((i) < LUA_REGISTRYINDEX) |
| 50 | 54 | ||
| 51 | /* test for valid but not pseudo index */ | ||
| 52 | #define isstackindex(i, o) (isvalid(o) && !ispseudo(i)) | ||
| 53 | |||
| 54 | #define api_checkvalidindex(l,o) api_check(l, isvalid(o), "invalid index") | ||
| 55 | |||
| 56 | #define api_checkstackindex(l, i, o) \ | ||
| 57 | api_check(l, isstackindex(i, o), "index not in the stack") | ||
| 58 | 55 | ||
| 59 | 56 | static TValue *index2value (lua_State *L, int idx) { | |
| 60 | static TValue *index2addr (lua_State *L, int idx) { | ||
| 61 | CallInfo *ci = L->ci; | 57 | CallInfo *ci = L->ci; |
| 62 | if (idx > 0) { | 58 | if (idx > 0) { |
| 63 | TValue *o = ci->func + idx; | 59 | StkId o = ci->func + idx; |
| 64 | api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index"); | 60 | api_check(L, idx <= L->ci->top - (ci->func + 1), "unacceptable index"); |
| 65 | if (o >= L->top) return NONVALIDVALUE; | 61 | if (o >= L->top) return &G(L)->nilvalue; |
| 66 | else return o; | 62 | else return s2v(o); |
| 67 | } | 63 | } |
| 68 | else if (!ispseudo(idx)) { /* negative index */ | 64 | else if (!ispseudo(idx)) { /* negative index */ |
| 69 | api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); | 65 | api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); |
| 70 | return L->top + idx; | 66 | return s2v(L->top + idx); |
| 71 | } | 67 | } |
| 72 | else if (idx == LUA_REGISTRYINDEX) | 68 | else if (idx == LUA_REGISTRYINDEX) |
| 73 | return &G(L)->l_registry; | 69 | return &G(L)->l_registry; |
| 74 | else { /* upvalues */ | 70 | else { /* upvalues */ |
| 75 | idx = LUA_REGISTRYINDEX - idx; | 71 | idx = LUA_REGISTRYINDEX - idx; |
| 76 | api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); | 72 | api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); |
| 77 | if (ttislcf(ci->func)) /* light C function? */ | 73 | if (ttislcf(s2v(ci->func))) /* light C function? */ |
| 78 | return NONVALIDVALUE; /* it has no upvalues */ | 74 | return &G(L)->nilvalue; /* it has no upvalues */ |
| 79 | else { | 75 | else { |
| 80 | CClosure *func = clCvalue(ci->func); | 76 | CClosure *func = clCvalue(s2v(ci->func)); |
| 81 | return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE; | 77 | return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : &G(L)->nilvalue; |
| 82 | } | 78 | } |
| 83 | } | 79 | } |
| 84 | } | 80 | } |
| 85 | 81 | ||
| 86 | 82 | ||
| 87 | /* | 83 | static StkId index2stack (lua_State *L, int idx) { |
| 88 | ** to be called by 'lua_checkstack' in protected mode, to grow stack | 84 | CallInfo *ci = L->ci; |
| 89 | ** capturing memory errors | 85 | if (idx > 0) { |
| 90 | */ | 86 | StkId o = ci->func + idx; |
| 91 | static void growstack (lua_State *L, void *ud) { | 87 | api_check(L, o < L->top, "unacceptable index"); |
| 92 | int size = *(int *)ud; | 88 | return o; |
| 93 | luaD_growstack(L, size); | 89 | } |
| 90 | else { /* non-positive index */ | ||
| 91 | api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); | ||
| 92 | api_check(L, !ispseudo(idx), "invalid index"); | ||
| 93 | return L->top + idx; | ||
| 94 | } | ||
| 94 | } | 95 | } |
| 95 | 96 | ||
| 96 | 97 | ||
| @@ -106,7 +107,7 @@ LUA_API int lua_checkstack (lua_State *L, int n) { | |||
| 106 | if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */ | 107 | if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */ |
| 107 | res = 0; /* no */ | 108 | res = 0; /* no */ |
| 108 | else /* try to grow stack */ | 109 | else /* try to grow stack */ |
| 109 | res = (luaD_rawrunprotected(L, &growstack, &n) == LUA_OK); | 110 | res = luaD_growstack(L, n, 0); |
| 110 | } | 111 | } |
| 111 | if (res && ci->top < L->top + n) | 112 | if (res && ci->top < L->top + n) |
| 112 | ci->top = L->top + n; /* adjust frame top */ | 113 | ci->top = L->top + n; /* adjust frame top */ |
| @@ -124,7 +125,7 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { | |||
| 124 | api_check(from, to->ci->top - to->top >= n, "stack overflow"); | 125 | api_check(from, to->ci->top - to->top >= n, "stack overflow"); |
| 125 | from->top -= n; | 126 | from->top -= n; |
| 126 | for (i = 0; i < n; i++) { | 127 | for (i = 0; i < n; i++) { |
| 127 | setobj2s(to, to->top, from->top + i); | 128 | setobjs2s(to, to->top, from->top + i); |
| 128 | to->top++; /* stack already checked by previous 'api_check' */ | 129 | to->top++; /* stack already checked by previous 'api_check' */ |
| 129 | } | 130 | } |
| 130 | lua_unlock(to); | 131 | lua_unlock(to); |
| @@ -141,10 +142,9 @@ LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) { | |||
| 141 | } | 142 | } |
| 142 | 143 | ||
| 143 | 144 | ||
| 144 | LUA_API const lua_Number *lua_version (lua_State *L) { | 145 | LUA_API lua_Number lua_version (lua_State *L) { |
| 145 | static const lua_Number version = LUA_VERSION_NUM; | 146 | UNUSED(L); |
| 146 | if (L == NULL) return &version; | 147 | return LUA_VERSION_NUM; |
| 147 | else return G(L)->version; | ||
| 148 | } | 148 | } |
| 149 | 149 | ||
| 150 | 150 | ||
| @@ -170,18 +170,23 @@ LUA_API int lua_gettop (lua_State *L) { | |||
| 170 | 170 | ||
| 171 | 171 | ||
| 172 | LUA_API void lua_settop (lua_State *L, int idx) { | 172 | LUA_API void lua_settop (lua_State *L, int idx) { |
| 173 | StkId func = L->ci->func; | 173 | CallInfo *ci = L->ci; |
| 174 | StkId func = ci->func; | ||
| 175 | ptrdiff_t diff; /* difference for new top */ | ||
| 174 | lua_lock(L); | 176 | lua_lock(L); |
| 175 | if (idx >= 0) { | 177 | if (idx >= 0) { |
| 176 | api_check(L, idx <= L->stack_last - (func + 1), "new top too large"); | 178 | api_check(L, idx <= ci->top - (func + 1), "new top too large"); |
| 177 | while (L->top < (func + 1) + idx) | 179 | diff = ((func + 1) + idx) - L->top; |
| 178 | setnilvalue(L->top++); | 180 | for (; diff > 0; diff--) |
| 179 | L->top = (func + 1) + idx; | 181 | setnilvalue(s2v(L->top++)); /* clear new slots */ |
| 180 | } | 182 | } |
| 181 | else { | 183 | else { |
| 182 | api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); | 184 | api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); |
| 183 | L->top += idx+1; /* 'subtract' index (index is negative) */ | 185 | diff = idx + 1; /* will "subtract" index (as it is negative) */ |
| 184 | } | 186 | } |
| 187 | if (diff < 0 && hastocloseCfunc(ci->nresults)) | ||
| 188 | luaF_close(L, L->top + diff, LUA_OK); | ||
| 189 | L->top += diff; /* correct top only after closing any upvalue */ | ||
| 185 | lua_unlock(L); | 190 | lua_unlock(L); |
| 186 | } | 191 | } |
| 187 | 192 | ||
| @@ -189,11 +194,13 @@ LUA_API void lua_settop (lua_State *L, int idx) { | |||
| 189 | /* | 194 | /* |
| 190 | ** Reverse the stack segment from 'from' to 'to' | 195 | ** Reverse the stack segment from 'from' to 'to' |
| 191 | ** (auxiliary to 'lua_rotate') | 196 | ** (auxiliary to 'lua_rotate') |
| 197 | ** Note that we move(copy) only the value inside the stack. | ||
| 198 | ** (We do not move additional fields that may exist.) | ||
| 192 | */ | 199 | */ |
| 193 | static void reverse (lua_State *L, StkId from, StkId to) { | 200 | static void reverse (lua_State *L, StkId from, StkId to) { |
| 194 | for (; from < to; from++, to--) { | 201 | for (; from < to; from++, to--) { |
| 195 | TValue temp; | 202 | TValue temp; |
| 196 | setobj(L, &temp, from); | 203 | setobj(L, &temp, s2v(from)); |
| 197 | setobjs2s(L, from, to); | 204 | setobjs2s(L, from, to); |
| 198 | setobj2s(L, to, &temp); | 205 | setobj2s(L, to, &temp); |
| 199 | } | 206 | } |
| @@ -208,8 +215,7 @@ LUA_API void lua_rotate (lua_State *L, int idx, int n) { | |||
| 208 | StkId p, t, m; | 215 | StkId p, t, m; |
| 209 | lua_lock(L); | 216 | lua_lock(L); |
| 210 | t = L->top - 1; /* end of stack segment being rotated */ | 217 | t = L->top - 1; /* end of stack segment being rotated */ |
| 211 | p = index2addr(L, idx); /* start of segment */ | 218 | p = index2stack(L, idx); /* start of segment */ |
| 212 | api_checkstackindex(L, idx, p); | ||
| 213 | api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'"); | 219 | api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'"); |
| 214 | m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */ | 220 | m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */ |
| 215 | reverse(L, p, m); /* reverse the prefix with length 'n' */ | 221 | reverse(L, p, m); /* reverse the prefix with length 'n' */ |
| @@ -222,12 +228,12 @@ LUA_API void lua_rotate (lua_State *L, int idx, int n) { | |||
| 222 | LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { | 228 | LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { |
| 223 | TValue *fr, *to; | 229 | TValue *fr, *to; |
| 224 | lua_lock(L); | 230 | lua_lock(L); |
| 225 | fr = index2addr(L, fromidx); | 231 | fr = index2value(L, fromidx); |
| 226 | to = index2addr(L, toidx); | 232 | to = index2value(L, toidx); |
| 227 | api_checkvalidindex(L, to); | 233 | api_check(L, isvalid(L, to), "invalid index"); |
| 228 | setobj(L, to, fr); | 234 | setobj(L, to, fr); |
| 229 | if (isupvalue(toidx)) /* function upvalue? */ | 235 | if (isupvalue(toidx)) /* function upvalue? */ |
| 230 | luaC_barrier(L, clCvalue(L->ci->func), fr); | 236 | luaC_barrier(L, clCvalue(s2v(L->ci->func)), fr); |
| 231 | /* LUA_REGISTRYINDEX does not need gc barrier | 237 | /* LUA_REGISTRYINDEX does not need gc barrier |
| 232 | (collector revisits it before finishing collection) */ | 238 | (collector revisits it before finishing collection) */ |
| 233 | lua_unlock(L); | 239 | lua_unlock(L); |
| @@ -236,7 +242,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { | |||
| 236 | 242 | ||
| 237 | LUA_API void lua_pushvalue (lua_State *L, int idx) { | 243 | LUA_API void lua_pushvalue (lua_State *L, int idx) { |
| 238 | lua_lock(L); | 244 | lua_lock(L); |
| 239 | setobj2s(L, L->top, index2addr(L, idx)); | 245 | setobj2s(L, L->top, index2value(L, idx)); |
| 240 | api_incr_top(L); | 246 | api_incr_top(L); |
| 241 | lua_unlock(L); | 247 | lua_unlock(L); |
| 242 | } | 248 | } |
| @@ -249,53 +255,53 @@ LUA_API void lua_pushvalue (lua_State *L, int idx) { | |||
| 249 | 255 | ||
| 250 | 256 | ||
| 251 | LUA_API int lua_type (lua_State *L, int idx) { | 257 | LUA_API int lua_type (lua_State *L, int idx) { |
| 252 | StkId o = index2addr(L, idx); | 258 | const TValue *o = index2value(L, idx); |
| 253 | return (isvalid(o) ? ttnov(o) : LUA_TNONE); | 259 | return (isvalid(L, o) ? ttype(o) : LUA_TNONE); |
| 254 | } | 260 | } |
| 255 | 261 | ||
| 256 | 262 | ||
| 257 | LUA_API const char *lua_typename (lua_State *L, int t) { | 263 | LUA_API const char *lua_typename (lua_State *L, int t) { |
| 258 | UNUSED(L); | 264 | UNUSED(L); |
| 259 | api_check(L, LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag"); | 265 | api_check(L, LUA_TNONE <= t && t < LUA_NUMTYPES, "invalid type"); |
| 260 | return ttypename(t); | 266 | return ttypename(t); |
| 261 | } | 267 | } |
| 262 | 268 | ||
| 263 | 269 | ||
| 264 | LUA_API int lua_iscfunction (lua_State *L, int idx) { | 270 | LUA_API int lua_iscfunction (lua_State *L, int idx) { |
| 265 | StkId o = index2addr(L, idx); | 271 | const TValue *o = index2value(L, idx); |
| 266 | return (ttislcf(o) || (ttisCclosure(o))); | 272 | return (ttislcf(o) || (ttisCclosure(o))); |
| 267 | } | 273 | } |
| 268 | 274 | ||
| 269 | 275 | ||
| 270 | LUA_API int lua_isinteger (lua_State *L, int idx) { | 276 | LUA_API int lua_isinteger (lua_State *L, int idx) { |
| 271 | StkId o = index2addr(L, idx); | 277 | const TValue *o = index2value(L, idx); |
| 272 | return ttisinteger(o); | 278 | return ttisinteger(o); |
| 273 | } | 279 | } |
| 274 | 280 | ||
| 275 | 281 | ||
| 276 | LUA_API int lua_isnumber (lua_State *L, int idx) { | 282 | LUA_API int lua_isnumber (lua_State *L, int idx) { |
| 277 | lua_Number n; | 283 | lua_Number n; |
| 278 | const TValue *o = index2addr(L, idx); | 284 | const TValue *o = index2value(L, idx); |
| 279 | return tonumber(o, &n); | 285 | return tonumber(o, &n); |
| 280 | } | 286 | } |
| 281 | 287 | ||
| 282 | 288 | ||
| 283 | LUA_API int lua_isstring (lua_State *L, int idx) { | 289 | LUA_API int lua_isstring (lua_State *L, int idx) { |
| 284 | const TValue *o = index2addr(L, idx); | 290 | const TValue *o = index2value(L, idx); |
| 285 | return (ttisstring(o) || cvt2str(o)); | 291 | return (ttisstring(o) || cvt2str(o)); |
| 286 | } | 292 | } |
| 287 | 293 | ||
| 288 | 294 | ||
| 289 | LUA_API int lua_isuserdata (lua_State *L, int idx) { | 295 | LUA_API int lua_isuserdata (lua_State *L, int idx) { |
| 290 | const TValue *o = index2addr(L, idx); | 296 | const TValue *o = index2value(L, idx); |
| 291 | return (ttisfulluserdata(o) || ttislightuserdata(o)); | 297 | return (ttisfulluserdata(o) || ttislightuserdata(o)); |
| 292 | } | 298 | } |
| 293 | 299 | ||
| 294 | 300 | ||
| 295 | LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { | 301 | LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { |
| 296 | StkId o1 = index2addr(L, index1); | 302 | const TValue *o1 = index2value(L, index1); |
| 297 | StkId o2 = index2addr(L, index2); | 303 | const TValue *o2 = index2value(L, index2); |
| 298 | return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0; | 304 | return (isvalid(L, o1) && isvalid(L, o2)) ? luaV_rawequalobj(o1, o2) : 0; |
| 299 | } | 305 | } |
| 300 | 306 | ||
| 301 | 307 | ||
| @@ -309,19 +315,20 @@ LUA_API void lua_arith (lua_State *L, int op) { | |||
| 309 | api_incr_top(L); | 315 | api_incr_top(L); |
| 310 | } | 316 | } |
| 311 | /* first operand at top - 2, second at top - 1; result go to top - 2 */ | 317 | /* first operand at top - 2, second at top - 1; result go to top - 2 */ |
| 312 | luaO_arith(L, op, L->top - 2, L->top - 1, L->top - 2); | 318 | luaO_arith(L, op, s2v(L->top - 2), s2v(L->top - 1), L->top - 2); |
| 313 | L->top--; /* remove second operand */ | 319 | L->top--; /* remove second operand */ |
| 314 | lua_unlock(L); | 320 | lua_unlock(L); |
| 315 | } | 321 | } |
| 316 | 322 | ||
| 317 | 323 | ||
| 318 | LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { | 324 | LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { |
| 319 | StkId o1, o2; | 325 | const TValue *o1; |
| 326 | const TValue *o2; | ||
| 320 | int i = 0; | 327 | int i = 0; |
| 321 | lua_lock(L); /* may call tag method */ | 328 | lua_lock(L); /* may call tag method */ |
| 322 | o1 = index2addr(L, index1); | 329 | o1 = index2value(L, index1); |
| 323 | o2 = index2addr(L, index2); | 330 | o2 = index2value(L, index2); |
| 324 | if (isvalid(o1) && isvalid(o2)) { | 331 | if (isvalid(L, o1) && isvalid(L, o2)) { |
| 325 | switch (op) { | 332 | switch (op) { |
| 326 | case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break; | 333 | case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break; |
| 327 | case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break; | 334 | case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break; |
| @@ -335,7 +342,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { | |||
| 335 | 342 | ||
| 336 | 343 | ||
| 337 | LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) { | 344 | LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) { |
| 338 | size_t sz = luaO_str2num(s, L->top); | 345 | size_t sz = luaO_str2num(s, s2v(L->top)); |
| 339 | if (sz != 0) | 346 | if (sz != 0) |
| 340 | api_incr_top(L); | 347 | api_incr_top(L); |
| 341 | return sz; | 348 | return sz; |
| @@ -343,35 +350,33 @@ LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) { | |||
| 343 | 350 | ||
| 344 | 351 | ||
| 345 | LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) { | 352 | LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) { |
| 346 | lua_Number n; | 353 | lua_Number n = 0; |
| 347 | const TValue *o = index2addr(L, idx); | 354 | const TValue *o = index2value(L, idx); |
| 348 | int isnum = tonumber(o, &n); | 355 | int isnum = tonumber(o, &n); |
| 349 | if (!isnum) | 356 | if (pisnum) |
| 350 | n = 0; /* call to 'tonumber' may change 'n' even if it fails */ | 357 | *pisnum = isnum; |
| 351 | if (pisnum) *pisnum = isnum; | ||
| 352 | return n; | 358 | return n; |
| 353 | } | 359 | } |
| 354 | 360 | ||
| 355 | 361 | ||
| 356 | LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) { | 362 | LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) { |
| 357 | lua_Integer res; | 363 | lua_Integer res = 0; |
| 358 | const TValue *o = index2addr(L, idx); | 364 | const TValue *o = index2value(L, idx); |
| 359 | int isnum = tointeger(o, &res); | 365 | int isnum = tointeger(o, &res); |
| 360 | if (!isnum) | 366 | if (pisnum) |
| 361 | res = 0; /* call to 'tointeger' may change 'n' even if it fails */ | 367 | *pisnum = isnum; |
| 362 | if (pisnum) *pisnum = isnum; | ||
| 363 | return res; | 368 | return res; |
| 364 | } | 369 | } |
| 365 | 370 | ||
| 366 | 371 | ||
| 367 | LUA_API int lua_toboolean (lua_State *L, int idx) { | 372 | LUA_API int lua_toboolean (lua_State *L, int idx) { |
| 368 | const TValue *o = index2addr(L, idx); | 373 | const TValue *o = index2value(L, idx); |
| 369 | return !l_isfalse(o); | 374 | return !l_isfalse(o); |
| 370 | } | 375 | } |
| 371 | 376 | ||
| 372 | 377 | ||
| 373 | LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { | 378 | LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { |
| 374 | StkId o = index2addr(L, idx); | 379 | TValue *o = index2value(L, idx); |
| 375 | if (!ttisstring(o)) { | 380 | if (!ttisstring(o)) { |
| 376 | if (!cvt2str(o)) { /* not convertible? */ | 381 | if (!cvt2str(o)) { /* not convertible? */ |
| 377 | if (len != NULL) *len = 0; | 382 | if (len != NULL) *len = 0; |
| @@ -380,7 +385,7 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { | |||
| 380 | lua_lock(L); /* 'luaO_tostring' may create a new string */ | 385 | lua_lock(L); /* 'luaO_tostring' may create a new string */ |
| 381 | luaO_tostring(L, o); | 386 | luaO_tostring(L, o); |
| 382 | luaC_checkGC(L); | 387 | luaC_checkGC(L); |
| 383 | o = index2addr(L, idx); /* previous call may reallocate the stack */ | 388 | o = index2value(L, idx); /* previous call may reallocate the stack */ |
| 384 | lua_unlock(L); | 389 | lua_unlock(L); |
| 385 | } | 390 | } |
| 386 | if (len != NULL) | 391 | if (len != NULL) |
| @@ -389,20 +394,20 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { | |||
| 389 | } | 394 | } |
| 390 | 395 | ||
| 391 | 396 | ||
| 392 | LUA_API size_t lua_rawlen (lua_State *L, int idx) { | 397 | LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) { |
| 393 | StkId o = index2addr(L, idx); | 398 | const TValue *o = index2value(L, idx); |
| 394 | switch (ttype(o)) { | 399 | switch (ttypetag(o)) { |
| 395 | case LUA_TSHRSTR: return tsvalue(o)->shrlen; | 400 | case LUA_VSHRSTR: return tsvalue(o)->shrlen; |
| 396 | case LUA_TLNGSTR: return tsvalue(o)->u.lnglen; | 401 | case LUA_VLNGSTR: return tsvalue(o)->u.lnglen; |
| 397 | case LUA_TUSERDATA: return uvalue(o)->len; | 402 | case LUA_VUSERDATA: return uvalue(o)->len; |
| 398 | case LUA_TTABLE: return luaH_getn(hvalue(o)); | 403 | case LUA_VTABLE: return luaH_getn(hvalue(o)); |
| 399 | default: return 0; | 404 | default: return 0; |
| 400 | } | 405 | } |
| 401 | } | 406 | } |
| 402 | 407 | ||
| 403 | 408 | ||
| 404 | LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { | 409 | LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { |
| 405 | StkId o = index2addr(L, idx); | 410 | const TValue *o = index2value(L, idx); |
| 406 | if (ttislcf(o)) return fvalue(o); | 411 | if (ttislcf(o)) return fvalue(o); |
| 407 | else if (ttisCclosure(o)) | 412 | else if (ttisCclosure(o)) |
| 408 | return clCvalue(o)->f; | 413 | return clCvalue(o)->f; |
| @@ -410,9 +415,8 @@ LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { | |||
| 410 | } | 415 | } |
| 411 | 416 | ||
| 412 | 417 | ||
| 413 | LUA_API void *lua_touserdata (lua_State *L, int idx) { | 418 | static void *touserdata (const TValue *o) { |
| 414 | StkId o = index2addr(L, idx); | 419 | switch (ttype(o)) { |
| 415 | switch (ttnov(o)) { | ||
| 416 | case LUA_TUSERDATA: return getudatamem(uvalue(o)); | 420 | case LUA_TUSERDATA: return getudatamem(uvalue(o)); |
| 417 | case LUA_TLIGHTUSERDATA: return pvalue(o); | 421 | case LUA_TLIGHTUSERDATA: return pvalue(o); |
| 418 | default: return NULL; | 422 | default: return NULL; |
| @@ -420,23 +424,37 @@ LUA_API void *lua_touserdata (lua_State *L, int idx) { | |||
| 420 | } | 424 | } |
| 421 | 425 | ||
| 422 | 426 | ||
| 427 | LUA_API void *lua_touserdata (lua_State *L, int idx) { | ||
| 428 | const TValue *o = index2value(L, idx); | ||
| 429 | return touserdata(o); | ||
| 430 | } | ||
| 431 | |||
| 432 | |||
| 423 | LUA_API lua_State *lua_tothread (lua_State *L, int idx) { | 433 | LUA_API lua_State *lua_tothread (lua_State *L, int idx) { |
| 424 | StkId o = index2addr(L, idx); | 434 | const TValue *o = index2value(L, idx); |
| 425 | return (!ttisthread(o)) ? NULL : thvalue(o); | 435 | return (!ttisthread(o)) ? NULL : thvalue(o); |
| 426 | } | 436 | } |
| 427 | 437 | ||
| 428 | 438 | ||
| 439 | /* | ||
| 440 | ** Returns a pointer to the internal representation of an object. | ||
| 441 | ** Note that ANSI C does not allow the conversion of a pointer to | ||
| 442 | ** function to a 'void*', so the conversion here goes through | ||
| 443 | ** a 'size_t'. (As the returned pointer is only informative, this | ||
| 444 | ** conversion should not be a problem.) | ||
| 445 | */ | ||
| 429 | LUA_API const void *lua_topointer (lua_State *L, int idx) { | 446 | LUA_API const void *lua_topointer (lua_State *L, int idx) { |
| 430 | StkId o = index2addr(L, idx); | 447 | const TValue *o = index2value(L, idx); |
| 431 | switch (ttype(o)) { | 448 | switch (ttypetag(o)) { |
| 432 | case LUA_TTABLE: return hvalue(o); | 449 | case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o))); |
| 433 | case LUA_TLCL: return clLvalue(o); | 450 | case LUA_VUSERDATA: case LUA_VLIGHTUSERDATA: |
| 434 | case LUA_TCCL: return clCvalue(o); | 451 | return touserdata(o); |
| 435 | case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o))); | 452 | default: { |
| 436 | case LUA_TTHREAD: return thvalue(o); | 453 | if (iscollectable(o)) |
| 437 | case LUA_TUSERDATA: return getudatamem(uvalue(o)); | 454 | return gcvalue(o); |
| 438 | case LUA_TLIGHTUSERDATA: return pvalue(o); | 455 | else |
| 439 | default: return NULL; | 456 | return NULL; |
| 457 | } | ||
| 440 | } | 458 | } |
| 441 | } | 459 | } |
| 442 | 460 | ||
| @@ -449,7 +467,7 @@ LUA_API const void *lua_topointer (lua_State *L, int idx) { | |||
| 449 | 467 | ||
| 450 | LUA_API void lua_pushnil (lua_State *L) { | 468 | LUA_API void lua_pushnil (lua_State *L) { |
| 451 | lua_lock(L); | 469 | lua_lock(L); |
| 452 | setnilvalue(L->top); | 470 | setnilvalue(s2v(L->top)); |
| 453 | api_incr_top(L); | 471 | api_incr_top(L); |
| 454 | lua_unlock(L); | 472 | lua_unlock(L); |
| 455 | } | 473 | } |
| @@ -457,7 +475,7 @@ LUA_API void lua_pushnil (lua_State *L) { | |||
| 457 | 475 | ||
| 458 | LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { | 476 | LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { |
| 459 | lua_lock(L); | 477 | lua_lock(L); |
| 460 | setfltvalue(L->top, n); | 478 | setfltvalue(s2v(L->top), n); |
| 461 | api_incr_top(L); | 479 | api_incr_top(L); |
| 462 | lua_unlock(L); | 480 | lua_unlock(L); |
| 463 | } | 481 | } |
| @@ -465,7 +483,7 @@ LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { | |||
| 465 | 483 | ||
| 466 | LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { | 484 | LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { |
| 467 | lua_lock(L); | 485 | lua_lock(L); |
| 468 | setivalue(L->top, n); | 486 | setivalue(s2v(L->top), n); |
| 469 | api_incr_top(L); | 487 | api_incr_top(L); |
| 470 | lua_unlock(L); | 488 | lua_unlock(L); |
| 471 | } | 489 | } |
| @@ -491,7 +509,7 @@ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { | |||
| 491 | LUA_API const char *lua_pushstring (lua_State *L, const char *s) { | 509 | LUA_API const char *lua_pushstring (lua_State *L, const char *s) { |
| 492 | lua_lock(L); | 510 | lua_lock(L); |
| 493 | if (s == NULL) | 511 | if (s == NULL) |
| 494 | setnilvalue(L->top); | 512 | setnilvalue(s2v(L->top)); |
| 495 | else { | 513 | else { |
| 496 | TString *ts; | 514 | TString *ts; |
| 497 | ts = luaS_new(L, s); | 515 | ts = luaS_new(L, s); |
| @@ -532,7 +550,7 @@ LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { | |||
| 532 | LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { | 550 | LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { |
| 533 | lua_lock(L); | 551 | lua_lock(L); |
| 534 | if (n == 0) { | 552 | if (n == 0) { |
| 535 | setfvalue(L->top, fn); | 553 | setfvalue(s2v(L->top), fn); |
| 536 | api_incr_top(L); | 554 | api_incr_top(L); |
| 537 | } | 555 | } |
| 538 | else { | 556 | else { |
| @@ -543,10 +561,10 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { | |||
| 543 | cl->f = fn; | 561 | cl->f = fn; |
| 544 | L->top -= n; | 562 | L->top -= n; |
| 545 | while (n--) { | 563 | while (n--) { |
| 546 | setobj2n(L, &cl->upvalue[n], L->top + n); | 564 | setobj2n(L, &cl->upvalue[n], s2v(L->top + n)); |
| 547 | /* does not need barrier because closure is white */ | 565 | /* does not need barrier because closure is white */ |
| 548 | } | 566 | } |
| 549 | setclCvalue(L, L->top, cl); | 567 | setclCvalue(L, s2v(L->top), cl); |
| 550 | api_incr_top(L); | 568 | api_incr_top(L); |
| 551 | luaC_checkGC(L); | 569 | luaC_checkGC(L); |
| 552 | } | 570 | } |
| @@ -556,7 +574,10 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { | |||
| 556 | 574 | ||
| 557 | LUA_API void lua_pushboolean (lua_State *L, int b) { | 575 | LUA_API void lua_pushboolean (lua_State *L, int b) { |
| 558 | lua_lock(L); | 576 | lua_lock(L); |
| 559 | setbvalue(L->top, (b != 0)); /* ensure that true is 1 */ | 577 | if (b) |
| 578 | setbtvalue(s2v(L->top)); | ||
| 579 | else | ||
| 580 | setbfvalue(s2v(L->top)); | ||
| 560 | api_incr_top(L); | 581 | api_incr_top(L); |
| 561 | lua_unlock(L); | 582 | lua_unlock(L); |
| 562 | } | 583 | } |
| @@ -564,7 +585,7 @@ LUA_API void lua_pushboolean (lua_State *L, int b) { | |||
| 564 | 585 | ||
| 565 | LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { | 586 | LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { |
| 566 | lua_lock(L); | 587 | lua_lock(L); |
| 567 | setpvalue(L->top, p); | 588 | setpvalue(s2v(L->top), p); |
| 568 | api_incr_top(L); | 589 | api_incr_top(L); |
| 569 | lua_unlock(L); | 590 | lua_unlock(L); |
| 570 | } | 591 | } |
| @@ -572,7 +593,7 @@ LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { | |||
| 572 | 593 | ||
| 573 | LUA_API int lua_pushthread (lua_State *L) { | 594 | LUA_API int lua_pushthread (lua_State *L) { |
| 574 | lua_lock(L); | 595 | lua_lock(L); |
| 575 | setthvalue(L, L->top, L); | 596 | setthvalue(L, s2v(L->top), L); |
| 576 | api_incr_top(L); | 597 | api_incr_top(L); |
| 577 | lua_unlock(L); | 598 | lua_unlock(L); |
| 578 | return (G(L)->mainthread == L); | 599 | return (G(L)->mainthread == L); |
| @@ -595,10 +616,10 @@ static int auxgetstr (lua_State *L, const TValue *t, const char *k) { | |||
| 595 | else { | 616 | else { |
| 596 | setsvalue2s(L, L->top, str); | 617 | setsvalue2s(L, L->top, str); |
| 597 | api_incr_top(L); | 618 | api_incr_top(L); |
| 598 | luaV_finishget(L, t, L->top - 1, L->top - 1, slot); | 619 | luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot); |
| 599 | } | 620 | } |
| 600 | lua_unlock(L); | 621 | lua_unlock(L); |
| 601 | return ttnov(L->top - 1); | 622 | return ttype(s2v(L->top - 1)); |
| 602 | } | 623 | } |
| 603 | 624 | ||
| 604 | 625 | ||
| @@ -610,74 +631,90 @@ LUA_API int lua_getglobal (lua_State *L, const char *name) { | |||
| 610 | 631 | ||
| 611 | 632 | ||
| 612 | LUA_API int lua_gettable (lua_State *L, int idx) { | 633 | LUA_API int lua_gettable (lua_State *L, int idx) { |
| 613 | StkId t; | 634 | const TValue *slot; |
| 635 | TValue *t; | ||
| 614 | lua_lock(L); | 636 | lua_lock(L); |
| 615 | t = index2addr(L, idx); | 637 | t = index2value(L, idx); |
| 616 | luaV_gettable(L, t, L->top - 1, L->top - 1); | 638 | if (luaV_fastget(L, t, s2v(L->top - 1), slot, luaH_get)) { |
| 639 | setobj2s(L, L->top - 1, slot); | ||
| 640 | } | ||
| 641 | else | ||
| 642 | luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot); | ||
| 617 | lua_unlock(L); | 643 | lua_unlock(L); |
| 618 | return ttnov(L->top - 1); | 644 | return ttype(s2v(L->top - 1)); |
| 619 | } | 645 | } |
| 620 | 646 | ||
| 621 | 647 | ||
| 622 | LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { | 648 | LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { |
| 623 | lua_lock(L); | 649 | lua_lock(L); |
| 624 | return auxgetstr(L, index2addr(L, idx), k); | 650 | return auxgetstr(L, index2value(L, idx), k); |
| 625 | } | 651 | } |
| 626 | 652 | ||
| 627 | 653 | ||
| 628 | LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { | 654 | LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { |
| 629 | StkId t; | 655 | TValue *t; |
| 630 | const TValue *slot; | 656 | const TValue *slot; |
| 631 | lua_lock(L); | 657 | lua_lock(L); |
| 632 | t = index2addr(L, idx); | 658 | t = index2value(L, idx); |
| 633 | if (luaV_fastget(L, t, n, slot, luaH_getint)) { | 659 | if (luaV_fastgeti(L, t, n, slot)) { |
| 634 | setobj2s(L, L->top, slot); | 660 | setobj2s(L, L->top, slot); |
| 635 | api_incr_top(L); | ||
| 636 | } | 661 | } |
| 637 | else { | 662 | else { |
| 638 | setivalue(L->top, n); | 663 | TValue aux; |
| 639 | api_incr_top(L); | 664 | setivalue(&aux, n); |
| 640 | luaV_finishget(L, t, L->top - 1, L->top - 1, slot); | 665 | luaV_finishget(L, t, &aux, L->top, slot); |
| 641 | } | 666 | } |
| 667 | api_incr_top(L); | ||
| 642 | lua_unlock(L); | 668 | lua_unlock(L); |
| 643 | return ttnov(L->top - 1); | 669 | return ttype(s2v(L->top - 1)); |
| 670 | } | ||
| 671 | |||
| 672 | |||
| 673 | static int finishrawget (lua_State *L, const TValue *val) { | ||
| 674 | if (isempty(val)) /* avoid copying empty items to the stack */ | ||
| 675 | setnilvalue(s2v(L->top)); | ||
| 676 | else | ||
| 677 | setobj2s(L, L->top, val); | ||
| 678 | api_incr_top(L); | ||
| 679 | lua_unlock(L); | ||
| 680 | return ttype(s2v(L->top - 1)); | ||
| 681 | } | ||
| 682 | |||
| 683 | |||
| 684 | static Table *gettable (lua_State *L, int idx) { | ||
| 685 | TValue *t = index2value(L, idx); | ||
| 686 | api_check(L, ttistable(t), "table expected"); | ||
| 687 | return hvalue(t); | ||
| 644 | } | 688 | } |
| 645 | 689 | ||
| 646 | 690 | ||
| 647 | LUA_API int lua_rawget (lua_State *L, int idx) { | 691 | LUA_API int lua_rawget (lua_State *L, int idx) { |
| 648 | StkId t; | 692 | Table *t; |
| 693 | const TValue *val; | ||
| 649 | lua_lock(L); | 694 | lua_lock(L); |
| 650 | t = index2addr(L, idx); | 695 | api_checknelems(L, 1); |
| 651 | api_check(L, ttistable(t), "table expected"); | 696 | t = gettable(L, idx); |
| 652 | setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1)); | 697 | val = luaH_get(t, s2v(L->top - 1)); |
| 653 | lua_unlock(L); | 698 | L->top--; /* remove key */ |
| 654 | return ttnov(L->top - 1); | 699 | return finishrawget(L, val); |
| 655 | } | 700 | } |
| 656 | 701 | ||
| 657 | 702 | ||
| 658 | LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { | 703 | LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { |
| 659 | StkId t; | 704 | Table *t; |
| 660 | lua_lock(L); | 705 | lua_lock(L); |
| 661 | t = index2addr(L, idx); | 706 | t = gettable(L, idx); |
| 662 | api_check(L, ttistable(t), "table expected"); | 707 | return finishrawget(L, luaH_getint(t, n)); |
| 663 | setobj2s(L, L->top, luaH_getint(hvalue(t), n)); | ||
| 664 | api_incr_top(L); | ||
| 665 | lua_unlock(L); | ||
| 666 | return ttnov(L->top - 1); | ||
| 667 | } | 708 | } |
| 668 | 709 | ||
| 669 | 710 | ||
| 670 | LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { | 711 | LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { |
| 671 | StkId t; | 712 | Table *t; |
| 672 | TValue k; | 713 | TValue k; |
| 673 | lua_lock(L); | 714 | lua_lock(L); |
| 674 | t = index2addr(L, idx); | 715 | t = gettable(L, idx); |
| 675 | api_check(L, ttistable(t), "table expected"); | 716 | setpvalue(&k, cast_voidp(p)); |
| 676 | setpvalue(&k, cast(void *, p)); | 717 | return finishrawget(L, luaH_get(t, &k)); |
| 677 | setobj2s(L, L->top, luaH_get(hvalue(t), &k)); | ||
| 678 | api_incr_top(L); | ||
| 679 | lua_unlock(L); | ||
| 680 | return ttnov(L->top - 1); | ||
| 681 | } | 718 | } |
| 682 | 719 | ||
| 683 | 720 | ||
| @@ -685,7 +722,7 @@ LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { | |||
| 685 | Table *t; | 722 | Table *t; |
| 686 | lua_lock(L); | 723 | lua_lock(L); |
| 687 | t = luaH_new(L); | 724 | t = luaH_new(L); |
| 688 | sethvalue(L, L->top, t); | 725 | sethvalue2s(L, L->top, t); |
| 689 | api_incr_top(L); | 726 | api_incr_top(L); |
| 690 | if (narray > 0 || nrec > 0) | 727 | if (narray > 0 || nrec > 0) |
| 691 | luaH_resize(L, t, narray, nrec); | 728 | luaH_resize(L, t, narray, nrec); |
| @@ -699,8 +736,8 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { | |||
| 699 | Table *mt; | 736 | Table *mt; |
| 700 | int res = 0; | 737 | int res = 0; |
| 701 | lua_lock(L); | 738 | lua_lock(L); |
| 702 | obj = index2addr(L, objindex); | 739 | obj = index2value(L, objindex); |
| 703 | switch (ttnov(obj)) { | 740 | switch (ttype(obj)) { |
| 704 | case LUA_TTABLE: | 741 | case LUA_TTABLE: |
| 705 | mt = hvalue(obj)->metatable; | 742 | mt = hvalue(obj)->metatable; |
| 706 | break; | 743 | break; |
| @@ -708,11 +745,11 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { | |||
| 708 | mt = uvalue(obj)->metatable; | 745 | mt = uvalue(obj)->metatable; |
| 709 | break; | 746 | break; |
| 710 | default: | 747 | default: |
| 711 | mt = G(L)->mt[ttnov(obj)]; | 748 | mt = G(L)->mt[ttype(obj)]; |
| 712 | break; | 749 | break; |
| 713 | } | 750 | } |
| 714 | if (mt != NULL) { | 751 | if (mt != NULL) { |
| 715 | sethvalue(L, L->top, mt); | 752 | sethvalue2s(L, L->top, mt); |
| 716 | api_incr_top(L); | 753 | api_incr_top(L); |
| 717 | res = 1; | 754 | res = 1; |
| 718 | } | 755 | } |
| @@ -721,15 +758,23 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { | |||
| 721 | } | 758 | } |
| 722 | 759 | ||
| 723 | 760 | ||
| 724 | LUA_API int lua_getuservalue (lua_State *L, int idx) { | 761 | LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) { |
| 725 | StkId o; | 762 | TValue *o; |
| 763 | int t; | ||
| 726 | lua_lock(L); | 764 | lua_lock(L); |
| 727 | o = index2addr(L, idx); | 765 | o = index2value(L, idx); |
| 728 | api_check(L, ttisfulluserdata(o), "full userdata expected"); | 766 | api_check(L, ttisfulluserdata(o), "full userdata expected"); |
| 729 | getuservalue(L, uvalue(o), L->top); | 767 | if (n <= 0 || n > uvalue(o)->nuvalue) { |
| 768 | setnilvalue(s2v(L->top)); | ||
| 769 | t = LUA_TNONE; | ||
| 770 | } | ||
| 771 | else { | ||
| 772 | setobj2s(L, L->top, &uvalue(o)->uv[n - 1].uv); | ||
| 773 | t = ttype(s2v(L->top)); | ||
| 774 | } | ||
| 730 | api_incr_top(L); | 775 | api_incr_top(L); |
| 731 | lua_unlock(L); | 776 | lua_unlock(L); |
| 732 | return ttnov(L->top - 1); | 777 | return t; |
| 733 | } | 778 | } |
| 734 | 779 | ||
| 735 | 780 | ||
| @@ -744,12 +789,14 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) { | |||
| 744 | const TValue *slot; | 789 | const TValue *slot; |
| 745 | TString *str = luaS_new(L, k); | 790 | TString *str = luaS_new(L, k); |
| 746 | api_checknelems(L, 1); | 791 | api_checknelems(L, 1); |
| 747 | if (luaV_fastset(L, t, str, slot, luaH_getstr, L->top - 1)) | 792 | if (luaV_fastget(L, t, str, slot, luaH_getstr)) { |
| 793 | luaV_finishfastset(L, t, slot, s2v(L->top - 1)); | ||
| 748 | L->top--; /* pop value */ | 794 | L->top--; /* pop value */ |
| 795 | } | ||
| 749 | else { | 796 | else { |
| 750 | setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */ | 797 | setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */ |
| 751 | api_incr_top(L); | 798 | api_incr_top(L); |
| 752 | luaV_finishset(L, t, L->top - 1, L->top - 2, slot); | 799 | luaV_finishset(L, t, s2v(L->top - 1), s2v(L->top - 2), slot); |
| 753 | L->top -= 2; /* pop value and key */ | 800 | L->top -= 2; /* pop value and key */ |
| 754 | } | 801 | } |
| 755 | lua_unlock(L); /* lock done by caller */ | 802 | lua_unlock(L); /* lock done by caller */ |
| @@ -764,11 +811,16 @@ LUA_API void lua_setglobal (lua_State *L, const char *name) { | |||
| 764 | 811 | ||
| 765 | 812 | ||
| 766 | LUA_API void lua_settable (lua_State *L, int idx) { | 813 | LUA_API void lua_settable (lua_State *L, int idx) { |
| 767 | StkId t; | 814 | TValue *t; |
| 815 | const TValue *slot; | ||
| 768 | lua_lock(L); | 816 | lua_lock(L); |
| 769 | api_checknelems(L, 2); | 817 | api_checknelems(L, 2); |
| 770 | t = index2addr(L, idx); | 818 | t = index2value(L, idx); |
| 771 | luaV_settable(L, t, L->top - 2, L->top - 1); | 819 | if (luaV_fastget(L, t, s2v(L->top - 2), slot, luaH_get)) { |
| 820 | luaV_finishfastset(L, t, slot, s2v(L->top - 1)); | ||
| 821 | } | ||
| 822 | else | ||
| 823 | luaV_finishset(L, t, s2v(L->top - 2), s2v(L->top - 1), slot); | ||
| 772 | L->top -= 2; /* pop index and value */ | 824 | L->top -= 2; /* pop index and value */ |
| 773 | lua_unlock(L); | 825 | lua_unlock(L); |
| 774 | } | 826 | } |
| @@ -776,68 +828,63 @@ LUA_API void lua_settable (lua_State *L, int idx) { | |||
| 776 | 828 | ||
| 777 | LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { | 829 | LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { |
| 778 | lua_lock(L); /* unlock done in 'auxsetstr' */ | 830 | lua_lock(L); /* unlock done in 'auxsetstr' */ |
| 779 | auxsetstr(L, index2addr(L, idx), k); | 831 | auxsetstr(L, index2value(L, idx), k); |
| 780 | } | 832 | } |
| 781 | 833 | ||
| 782 | 834 | ||
| 783 | LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { | 835 | LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { |
| 784 | StkId t; | 836 | TValue *t; |
| 785 | const TValue *slot; | 837 | const TValue *slot; |
| 786 | lua_lock(L); | 838 | lua_lock(L); |
| 787 | api_checknelems(L, 1); | 839 | api_checknelems(L, 1); |
| 788 | t = index2addr(L, idx); | 840 | t = index2value(L, idx); |
| 789 | if (luaV_fastset(L, t, n, slot, luaH_getint, L->top - 1)) | 841 | if (luaV_fastgeti(L, t, n, slot)) { |
| 790 | L->top--; /* pop value */ | 842 | luaV_finishfastset(L, t, slot, s2v(L->top - 1)); |
| 843 | } | ||
| 791 | else { | 844 | else { |
| 792 | setivalue(L->top, n); | 845 | TValue aux; |
| 793 | api_incr_top(L); | 846 | setivalue(&aux, n); |
| 794 | luaV_finishset(L, t, L->top - 1, L->top - 2, slot); | 847 | luaV_finishset(L, t, &aux, s2v(L->top - 1), slot); |
| 795 | L->top -= 2; /* pop value and key */ | ||
| 796 | } | 848 | } |
| 849 | L->top--; /* pop value */ | ||
| 797 | lua_unlock(L); | 850 | lua_unlock(L); |
| 798 | } | 851 | } |
| 799 | 852 | ||
| 800 | 853 | ||
| 801 | LUA_API void lua_rawset (lua_State *L, int idx) { | 854 | static void aux_rawset (lua_State *L, int idx, TValue *key, int n) { |
| 802 | StkId o; | 855 | Table *t; |
| 803 | TValue *slot; | 856 | TValue *slot; |
| 804 | lua_lock(L); | 857 | lua_lock(L); |
| 805 | api_checknelems(L, 2); | 858 | api_checknelems(L, n); |
| 806 | o = index2addr(L, idx); | 859 | t = gettable(L, idx); |
| 807 | api_check(L, ttistable(o), "table expected"); | 860 | slot = luaH_set(L, t, key); |
| 808 | slot = luaH_set(L, hvalue(o), L->top - 2); | 861 | setobj2t(L, slot, s2v(L->top - 1)); |
| 809 | setobj2t(L, slot, L->top - 1); | 862 | invalidateTMcache(t); |
| 810 | invalidateTMcache(hvalue(o)); | 863 | luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); |
| 811 | luaC_barrierback(L, hvalue(o), L->top-1); | 864 | L->top -= n; |
| 812 | L->top -= 2; | ||
| 813 | lua_unlock(L); | 865 | lua_unlock(L); |
| 814 | } | 866 | } |
| 815 | 867 | ||
| 816 | 868 | ||
| 817 | LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { | 869 | LUA_API void lua_rawset (lua_State *L, int idx) { |
| 818 | StkId o; | 870 | aux_rawset(L, idx, s2v(L->top - 2), 2); |
| 819 | lua_lock(L); | ||
| 820 | api_checknelems(L, 1); | ||
| 821 | o = index2addr(L, idx); | ||
| 822 | api_check(L, ttistable(o), "table expected"); | ||
| 823 | luaH_setint(L, hvalue(o), n, L->top - 1); | ||
| 824 | luaC_barrierback(L, hvalue(o), L->top-1); | ||
| 825 | L->top--; | ||
| 826 | lua_unlock(L); | ||
| 827 | } | 871 | } |
| 828 | 872 | ||
| 829 | 873 | ||
| 830 | LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { | 874 | LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { |
| 831 | StkId o; | 875 | TValue k; |
| 832 | TValue k, *slot; | 876 | setpvalue(&k, cast_voidp(p)); |
| 877 | aux_rawset(L, idx, &k, 1); | ||
| 878 | } | ||
| 879 | |||
| 880 | |||
| 881 | LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { | ||
| 882 | Table *t; | ||
| 833 | lua_lock(L); | 883 | lua_lock(L); |
| 834 | api_checknelems(L, 1); | 884 | api_checknelems(L, 1); |
| 835 | o = index2addr(L, idx); | 885 | t = gettable(L, idx); |
| 836 | api_check(L, ttistable(o), "table expected"); | 886 | luaH_setint(L, t, n, s2v(L->top - 1)); |
| 837 | setpvalue(&k, cast(void *, p)); | 887 | luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); |
| 838 | slot = luaH_set(L, hvalue(o), &k); | ||
| 839 | setobj2t(L, slot, L->top - 1); | ||
| 840 | luaC_barrierback(L, hvalue(o), L->top - 1); | ||
| 841 | L->top--; | 888 | L->top--; |
| 842 | lua_unlock(L); | 889 | lua_unlock(L); |
| 843 | } | 890 | } |
| @@ -848,14 +895,14 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { | |||
| 848 | Table *mt; | 895 | Table *mt; |
| 849 | lua_lock(L); | 896 | lua_lock(L); |
| 850 | api_checknelems(L, 1); | 897 | api_checknelems(L, 1); |
| 851 | obj = index2addr(L, objindex); | 898 | obj = index2value(L, objindex); |
| 852 | if (ttisnil(L->top - 1)) | 899 | if (ttisnil(s2v(L->top - 1))) |
| 853 | mt = NULL; | 900 | mt = NULL; |
| 854 | else { | 901 | else { |
| 855 | api_check(L, ttistable(L->top - 1), "table expected"); | 902 | api_check(L, ttistable(s2v(L->top - 1)), "table expected"); |
| 856 | mt = hvalue(L->top - 1); | 903 | mt = hvalue(s2v(L->top - 1)); |
| 857 | } | 904 | } |
| 858 | switch (ttnov(obj)) { | 905 | switch (ttype(obj)) { |
| 859 | case LUA_TTABLE: { | 906 | case LUA_TTABLE: { |
| 860 | hvalue(obj)->metatable = mt; | 907 | hvalue(obj)->metatable = mt; |
| 861 | if (mt) { | 908 | if (mt) { |
| @@ -873,7 +920,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { | |||
| 873 | break; | 920 | break; |
| 874 | } | 921 | } |
| 875 | default: { | 922 | default: { |
| 876 | G(L)->mt[ttnov(obj)] = mt; | 923 | G(L)->mt[ttype(obj)] = mt; |
| 877 | break; | 924 | break; |
| 878 | } | 925 | } |
| 879 | } | 926 | } |
| @@ -883,16 +930,23 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { | |||
| 883 | } | 930 | } |
| 884 | 931 | ||
| 885 | 932 | ||
| 886 | LUA_API void lua_setuservalue (lua_State *L, int idx) { | 933 | LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) { |
| 887 | StkId o; | 934 | TValue *o; |
| 935 | int res; | ||
| 888 | lua_lock(L); | 936 | lua_lock(L); |
| 889 | api_checknelems(L, 1); | 937 | api_checknelems(L, 1); |
| 890 | o = index2addr(L, idx); | 938 | o = index2value(L, idx); |
| 891 | api_check(L, ttisfulluserdata(o), "full userdata expected"); | 939 | api_check(L, ttisfulluserdata(o), "full userdata expected"); |
| 892 | setuservalue(L, uvalue(o), L->top - 1); | 940 | if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue))) |
| 893 | luaC_barrier(L, gcvalue(o), L->top - 1); | 941 | res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */ |
| 942 | else { | ||
| 943 | setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top - 1)); | ||
| 944 | luaC_barrierback(L, gcvalue(o), s2v(L->top - 1)); | ||
| 945 | res = 1; | ||
| 946 | } | ||
| 894 | L->top--; | 947 | L->top--; |
| 895 | lua_unlock(L); | 948 | lua_unlock(L); |
| 949 | return res; | ||
| 896 | } | 950 | } |
| 897 | 951 | ||
| 898 | 952 | ||
| @@ -916,7 +970,7 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults, | |||
| 916 | api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); | 970 | api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); |
| 917 | checkresults(L, nargs, nresults); | 971 | checkresults(L, nargs, nresults); |
| 918 | func = L->top - (nargs+1); | 972 | func = L->top - (nargs+1); |
| 919 | if (k != NULL && L->nny == 0) { /* need to prepare continuation? */ | 973 | if (k != NULL && yieldable(L)) { /* need to prepare continuation? */ |
| 920 | L->ci->u.c.k = k; /* save continuation */ | 974 | L->ci->u.c.k = k; /* save continuation */ |
| 921 | L->ci->u.c.ctx = ctx; /* save context */ | 975 | L->ci->u.c.ctx = ctx; /* save context */ |
| 922 | luaD_call(L, func, nresults); /* do the call */ | 976 | luaD_call(L, func, nresults); /* do the call */ |
| @@ -959,12 +1013,12 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, | |||
| 959 | if (errfunc == 0) | 1013 | if (errfunc == 0) |
| 960 | func = 0; | 1014 | func = 0; |
| 961 | else { | 1015 | else { |
| 962 | StkId o = index2addr(L, errfunc); | 1016 | StkId o = index2stack(L, errfunc); |
| 963 | api_checkstackindex(L, errfunc, o); | 1017 | api_check(L, ttisfunction(s2v(o)), "error handler must be a function"); |
| 964 | func = savestack(L, o); | 1018 | func = savestack(L, o); |
| 965 | } | 1019 | } |
| 966 | c.func = L->top - (nargs+1); /* function to be called */ | 1020 | c.func = L->top - (nargs+1); /* function to be called */ |
| 967 | if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */ | 1021 | if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */ |
| 968 | c.nresults = nresults; /* do a 'conventional' protected call */ | 1022 | c.nresults = nresults; /* do a 'conventional' protected call */ |
| 969 | status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); | 1023 | status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); |
| 970 | } | 1024 | } |
| @@ -973,7 +1027,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, | |||
| 973 | ci->u.c.k = k; /* save continuation */ | 1027 | ci->u.c.k = k; /* save continuation */ |
| 974 | ci->u.c.ctx = ctx; /* save context */ | 1028 | ci->u.c.ctx = ctx; /* save context */ |
| 975 | /* save information for error recovery */ | 1029 | /* save information for error recovery */ |
| 976 | ci->extra = savestack(L, c.func); | 1030 | ci->u2.funcidx = cast_int(savestack(L, c.func)); |
| 977 | ci->u.c.old_errfunc = L->errfunc; | 1031 | ci->u.c.old_errfunc = L->errfunc; |
| 978 | L->errfunc = func; | 1032 | L->errfunc = func; |
| 979 | setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */ | 1033 | setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */ |
| @@ -998,14 +1052,14 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, | |||
| 998 | luaZ_init(L, &z, reader, data); | 1052 | luaZ_init(L, &z, reader, data); |
| 999 | status = luaD_protectedparser(L, &z, chunkname, mode); | 1053 | status = luaD_protectedparser(L, &z, chunkname, mode); |
| 1000 | if (status == LUA_OK) { /* no errors? */ | 1054 | if (status == LUA_OK) { /* no errors? */ |
| 1001 | LClosure *f = clLvalue(L->top - 1); /* get newly created function */ | 1055 | LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */ |
| 1002 | if (f->nupvalues >= 1) { /* does it have an upvalue? */ | 1056 | if (f->nupvalues >= 1) { /* does it have an upvalue? */ |
| 1003 | /* get global table from registry */ | 1057 | /* get global table from registry */ |
| 1004 | Table *reg = hvalue(&G(L)->l_registry); | 1058 | Table *reg = hvalue(&G(L)->l_registry); |
| 1005 | const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS); | 1059 | const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS); |
| 1006 | /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ | 1060 | /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ |
| 1007 | setobj(L, f->upvals[0]->v, gt); | 1061 | setobj(L, f->upvals[0]->v, gt); |
| 1008 | luaC_upvalbarrier(L, f->upvals[0]); | 1062 | luaC_barrier(L, f->upvals[0], gt); |
| 1009 | } | 1063 | } |
| 1010 | } | 1064 | } |
| 1011 | lua_unlock(L); | 1065 | lua_unlock(L); |
| @@ -1018,7 +1072,7 @@ LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { | |||
| 1018 | TValue *o; | 1072 | TValue *o; |
| 1019 | lua_lock(L); | 1073 | lua_lock(L); |
| 1020 | api_checknelems(L, 1); | 1074 | api_checknelems(L, 1); |
| 1021 | o = L->top - 1; | 1075 | o = s2v(L->top - 1); |
| 1022 | if (isLfunction(o)) | 1076 | if (isLfunction(o)) |
| 1023 | status = luaU_dump(L, getproto(o), writer, data, strip); | 1077 | status = luaU_dump(L, getproto(o), writer, data, strip); |
| 1024 | else | 1078 | else |
| @@ -1036,12 +1090,12 @@ LUA_API int lua_status (lua_State *L) { | |||
| 1036 | /* | 1090 | /* |
| 1037 | ** Garbage-collection function | 1091 | ** Garbage-collection function |
| 1038 | */ | 1092 | */ |
| 1039 | 1093 | LUA_API int lua_gc (lua_State *L, int what, ...) { | |
| 1040 | LUA_API int lua_gc (lua_State *L, int what, int data) { | 1094 | va_list argp; |
| 1041 | int res = 0; | 1095 | int res = 0; |
| 1042 | global_State *g; | 1096 | global_State *g = G(L); |
| 1043 | lua_lock(L); | 1097 | lua_lock(L); |
| 1044 | g = G(L); | 1098 | va_start(argp, what); |
| 1045 | switch (what) { | 1099 | switch (what) { |
| 1046 | case LUA_GCSTOP: { | 1100 | case LUA_GCSTOP: { |
| 1047 | g->gcrunning = 0; | 1101 | g->gcrunning = 0; |
| @@ -1066,11 +1120,12 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { | |||
| 1066 | break; | 1120 | break; |
| 1067 | } | 1121 | } |
| 1068 | case LUA_GCSTEP: { | 1122 | case LUA_GCSTEP: { |
| 1123 | int data = va_arg(argp, int); | ||
| 1069 | l_mem debt = 1; /* =1 to signal that it did an actual step */ | 1124 | l_mem debt = 1; /* =1 to signal that it did an actual step */ |
| 1070 | lu_byte oldrunning = g->gcrunning; | 1125 | lu_byte oldrunning = g->gcrunning; |
| 1071 | g->gcrunning = 1; /* allow GC to run */ | 1126 | g->gcrunning = 1; /* allow GC to run */ |
| 1072 | if (data == 0) { | 1127 | if (data == 0) { |
| 1073 | luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */ | 1128 | luaE_setdebt(g, 0); /* do a basic step */ |
| 1074 | luaC_step(L); | 1129 | luaC_step(L); |
| 1075 | } | 1130 | } |
| 1076 | else { /* add 'data' to total debt */ | 1131 | else { /* add 'data' to total debt */ |
| @@ -1084,22 +1139,49 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { | |||
| 1084 | break; | 1139 | break; |
| 1085 | } | 1140 | } |
| 1086 | case LUA_GCSETPAUSE: { | 1141 | case LUA_GCSETPAUSE: { |
| 1087 | res = g->gcpause; | 1142 | int data = va_arg(argp, int); |
| 1088 | g->gcpause = data; | 1143 | res = getgcparam(g->gcpause); |
| 1144 | setgcparam(g->gcpause, data); | ||
| 1089 | break; | 1145 | break; |
| 1090 | } | 1146 | } |
| 1091 | case LUA_GCSETSTEPMUL: { | 1147 | case LUA_GCSETSTEPMUL: { |
| 1092 | res = g->gcstepmul; | 1148 | int data = va_arg(argp, int); |
| 1093 | if (data < 40) data = 40; /* avoid ridiculous low values (and 0) */ | 1149 | res = getgcparam(g->gcstepmul); |
| 1094 | g->gcstepmul = data; | 1150 | setgcparam(g->gcstepmul, data); |
| 1095 | break; | 1151 | break; |
| 1096 | } | 1152 | } |
| 1097 | case LUA_GCISRUNNING: { | 1153 | case LUA_GCISRUNNING: { |
| 1098 | res = g->gcrunning; | 1154 | res = g->gcrunning; |
| 1099 | break; | 1155 | break; |
| 1100 | } | 1156 | } |
| 1157 | case LUA_GCGEN: { | ||
| 1158 | int minormul = va_arg(argp, int); | ||
| 1159 | int majormul = va_arg(argp, int); | ||
| 1160 | res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC; | ||
| 1161 | if (minormul != 0) | ||
| 1162 | g->genminormul = minormul; | ||
| 1163 | if (majormul != 0) | ||
| 1164 | setgcparam(g->genmajormul, majormul); | ||
| 1165 | luaC_changemode(L, KGC_GEN); | ||
| 1166 | break; | ||
| 1167 | } | ||
| 1168 | case LUA_GCINC: { | ||
| 1169 | int pause = va_arg(argp, int); | ||
| 1170 | int stepmul = va_arg(argp, int); | ||
| 1171 | int stepsize = va_arg(argp, int); | ||
| 1172 | res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC; | ||
| 1173 | if (pause != 0) | ||
| 1174 | setgcparam(g->gcpause, pause); | ||
| 1175 | if (stepmul != 0) | ||
| 1176 | setgcparam(g->gcstepmul, stepmul); | ||
| 1177 | if (stepsize != 0) | ||
| 1178 | g->gcstepsize = stepsize; | ||
| 1179 | luaC_changemode(L, KGC_INC); | ||
| 1180 | break; | ||
| 1181 | } | ||
| 1101 | default: res = -1; /* invalid option */ | 1182 | default: res = -1; /* invalid option */ |
| 1102 | } | 1183 | } |
| 1184 | va_end(argp); | ||
| 1103 | lua_unlock(L); | 1185 | lua_unlock(L); |
| 1104 | return res; | 1186 | return res; |
| 1105 | } | 1187 | } |
| @@ -1121,12 +1203,12 @@ LUA_API int lua_error (lua_State *L) { | |||
| 1121 | 1203 | ||
| 1122 | 1204 | ||
| 1123 | LUA_API int lua_next (lua_State *L, int idx) { | 1205 | LUA_API int lua_next (lua_State *L, int idx) { |
| 1124 | StkId t; | 1206 | Table *t; |
| 1125 | int more; | 1207 | int more; |
| 1126 | lua_lock(L); | 1208 | lua_lock(L); |
| 1127 | t = index2addr(L, idx); | 1209 | api_checknelems(L, 1); |
| 1128 | api_check(L, ttistable(t), "table expected"); | 1210 | t = gettable(L, idx); |
| 1129 | more = luaH_next(L, hvalue(t), L->top - 1); | 1211 | more = luaH_next(L, t, L->top - 1); |
| 1130 | if (more) { | 1212 | if (more) { |
| 1131 | api_incr_top(L); | 1213 | api_incr_top(L); |
| 1132 | } | 1214 | } |
| @@ -1137,6 +1219,22 @@ LUA_API int lua_next (lua_State *L, int idx) { | |||
| 1137 | } | 1219 | } |
| 1138 | 1220 | ||
| 1139 | 1221 | ||
| 1222 | LUA_API void lua_toclose (lua_State *L, int idx) { | ||
| 1223 | int nresults; | ||
| 1224 | StkId o; | ||
| 1225 | lua_lock(L); | ||
| 1226 | o = index2stack(L, idx); | ||
| 1227 | nresults = L->ci->nresults; | ||
| 1228 | api_check(L, L->openupval == NULL || uplevel(L->openupval) <= o, | ||
| 1229 | "marked index below or equal new one"); | ||
| 1230 | luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */ | ||
| 1231 | if (!hastocloseCfunc(nresults)) /* function not marked yet? */ | ||
| 1232 | L->ci->nresults = codeNresults(nresults); /* mark it */ | ||
| 1233 | lua_assert(hastocloseCfunc(L->ci->nresults)); | ||
| 1234 | lua_unlock(L); | ||
| 1235 | } | ||
| 1236 | |||
| 1237 | |||
| 1140 | LUA_API void lua_concat (lua_State *L, int n) { | 1238 | LUA_API void lua_concat (lua_State *L, int n) { |
| 1141 | lua_lock(L); | 1239 | lua_lock(L); |
| 1142 | api_checknelems(L, n); | 1240 | api_checknelems(L, n); |
| @@ -1154,9 +1252,9 @@ LUA_API void lua_concat (lua_State *L, int n) { | |||
| 1154 | 1252 | ||
| 1155 | 1253 | ||
| 1156 | LUA_API void lua_len (lua_State *L, int idx) { | 1254 | LUA_API void lua_len (lua_State *L, int idx) { |
| 1157 | StkId t; | 1255 | TValue *t; |
| 1158 | lua_lock(L); | 1256 | lua_lock(L); |
| 1159 | t = index2addr(L, idx); | 1257 | t = index2value(L, idx); |
| 1160 | luaV_objlen(L, L->top, t); | 1258 | luaV_objlen(L, L->top, t); |
| 1161 | api_incr_top(L); | 1259 | api_incr_top(L); |
| 1162 | lua_unlock(L); | 1260 | lua_unlock(L); |
| @@ -1181,11 +1279,28 @@ LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) { | |||
| 1181 | } | 1279 | } |
| 1182 | 1280 | ||
| 1183 | 1281 | ||
| 1184 | LUA_API void *lua_newuserdata (lua_State *L, size_t size) { | 1282 | void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) { |
| 1283 | lua_lock(L); | ||
| 1284 | G(L)->ud_warn = ud; | ||
| 1285 | G(L)->warnf = f; | ||
| 1286 | lua_unlock(L); | ||
| 1287 | } | ||
| 1288 | |||
| 1289 | |||
| 1290 | void lua_warning (lua_State *L, const char *msg, int tocont) { | ||
| 1291 | lua_lock(L); | ||
| 1292 | luaE_warning(L, msg, tocont); | ||
| 1293 | lua_unlock(L); | ||
| 1294 | } | ||
| 1295 | |||
| 1296 | |||
| 1297 | |||
| 1298 | LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) { | ||
| 1185 | Udata *u; | 1299 | Udata *u; |
| 1186 | lua_lock(L); | 1300 | lua_lock(L); |
| 1187 | u = luaS_newudata(L, size); | 1301 | api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value"); |
| 1188 | setuvalue(L, L->top, u); | 1302 | u = luaS_newudata(L, size, nuvalue); |
| 1303 | setuvalue(L, s2v(L->top), u); | ||
| 1189 | api_incr_top(L); | 1304 | api_incr_top(L); |
| 1190 | luaC_checkGC(L); | 1305 | luaC_checkGC(L); |
| 1191 | lua_unlock(L); | 1306 | lua_unlock(L); |
| @@ -1194,25 +1309,27 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size) { | |||
| 1194 | 1309 | ||
| 1195 | 1310 | ||
| 1196 | 1311 | ||
| 1197 | static const char *aux_upvalue (StkId fi, int n, TValue **val, | 1312 | static const char *aux_upvalue (TValue *fi, int n, TValue **val, |
| 1198 | CClosure **owner, UpVal **uv) { | 1313 | GCObject **owner) { |
| 1199 | switch (ttype(fi)) { | 1314 | switch (ttypetag(fi)) { |
| 1200 | case LUA_TCCL: { /* C closure */ | 1315 | case LUA_VCCL: { /* C closure */ |
| 1201 | CClosure *f = clCvalue(fi); | 1316 | CClosure *f = clCvalue(fi); |
| 1202 | if (!(1 <= n && n <= f->nupvalues)) return NULL; | 1317 | if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues))) |
| 1318 | return NULL; /* 'n' not in [1, f->nupvalues] */ | ||
| 1203 | *val = &f->upvalue[n-1]; | 1319 | *val = &f->upvalue[n-1]; |
| 1204 | if (owner) *owner = f; | 1320 | if (owner) *owner = obj2gco(f); |
| 1205 | return ""; | 1321 | return ""; |
| 1206 | } | 1322 | } |
| 1207 | case LUA_TLCL: { /* Lua closure */ | 1323 | case LUA_VLCL: { /* Lua closure */ |
| 1208 | LClosure *f = clLvalue(fi); | 1324 | LClosure *f = clLvalue(fi); |
| 1209 | TString *name; | 1325 | TString *name; |
| 1210 | Proto *p = f->p; | 1326 | Proto *p = f->p; |
| 1211 | if (!(1 <= n && n <= p->sizeupvalues)) return NULL; | 1327 | if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues))) |
| 1328 | return NULL; /* 'n' not in [1, p->sizeupvalues] */ | ||
| 1212 | *val = f->upvals[n-1]->v; | 1329 | *val = f->upvals[n-1]->v; |
| 1213 | if (uv) *uv = f->upvals[n - 1]; | 1330 | if (owner) *owner = obj2gco(f->upvals[n - 1]); |
| 1214 | name = p->upvalues[n-1].name; | 1331 | name = p->upvalues[n-1].name; |
| 1215 | return (name == NULL) ? "(*no name)" : getstr(name); | 1332 | return (name == NULL) ? "(no name)" : getstr(name); |
| 1216 | } | 1333 | } |
| 1217 | default: return NULL; /* not a closure */ | 1334 | default: return NULL; /* not a closure */ |
| 1218 | } | 1335 | } |
| @@ -1223,7 +1340,7 @@ LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { | |||
| 1223 | const char *name; | 1340 | const char *name; |
| 1224 | TValue *val = NULL; /* to avoid warnings */ | 1341 | TValue *val = NULL; /* to avoid warnings */ |
| 1225 | lua_lock(L); | 1342 | lua_lock(L); |
| 1226 | name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL, NULL); | 1343 | name = aux_upvalue(index2value(L, funcindex), n, &val, NULL); |
| 1227 | if (name) { | 1344 | if (name) { |
| 1228 | setobj2s(L, L->top, val); | 1345 | setobj2s(L, L->top, val); |
| 1229 | api_incr_top(L); | 1346 | api_incr_top(L); |
| @@ -1236,41 +1353,40 @@ LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { | |||
| 1236 | LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { | 1353 | LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { |
| 1237 | const char *name; | 1354 | const char *name; |
| 1238 | TValue *val = NULL; /* to avoid warnings */ | 1355 | TValue *val = NULL; /* to avoid warnings */ |
| 1239 | CClosure *owner = NULL; | 1356 | GCObject *owner = NULL; /* to avoid warnings */ |
| 1240 | UpVal *uv = NULL; | 1357 | TValue *fi; |
| 1241 | StkId fi; | ||
| 1242 | lua_lock(L); | 1358 | lua_lock(L); |
| 1243 | fi = index2addr(L, funcindex); | 1359 | fi = index2value(L, funcindex); |
| 1244 | api_checknelems(L, 1); | 1360 | api_checknelems(L, 1); |
| 1245 | name = aux_upvalue(fi, n, &val, &owner, &uv); | 1361 | name = aux_upvalue(fi, n, &val, &owner); |
| 1246 | if (name) { | 1362 | if (name) { |
| 1247 | L->top--; | 1363 | L->top--; |
| 1248 | setobj(L, val, L->top); | 1364 | setobj(L, val, s2v(L->top)); |
| 1249 | if (owner) { luaC_barrier(L, owner, L->top); } | 1365 | luaC_barrier(L, owner, val); |
| 1250 | else if (uv) { luaC_upvalbarrier(L, uv); } | ||
| 1251 | } | 1366 | } |
| 1252 | lua_unlock(L); | 1367 | lua_unlock(L); |
| 1253 | return name; | 1368 | return name; |
| 1254 | } | 1369 | } |
| 1255 | 1370 | ||
| 1256 | 1371 | ||
| 1257 | static UpVal **getupvalref (lua_State *L, int fidx, int n) { | 1372 | static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { |
| 1258 | LClosure *f; | 1373 | LClosure *f; |
| 1259 | StkId fi = index2addr(L, fidx); | 1374 | TValue *fi = index2value(L, fidx); |
| 1260 | api_check(L, ttisLclosure(fi), "Lua function expected"); | 1375 | api_check(L, ttisLclosure(fi), "Lua function expected"); |
| 1261 | f = clLvalue(fi); | 1376 | f = clLvalue(fi); |
| 1262 | api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index"); | 1377 | api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index"); |
| 1378 | if (pf) *pf = f; | ||
| 1263 | return &f->upvals[n - 1]; /* get its upvalue pointer */ | 1379 | return &f->upvals[n - 1]; /* get its upvalue pointer */ |
| 1264 | } | 1380 | } |
| 1265 | 1381 | ||
| 1266 | 1382 | ||
| 1267 | LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { | 1383 | LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { |
| 1268 | StkId fi = index2addr(L, fidx); | 1384 | TValue *fi = index2value(L, fidx); |
| 1269 | switch (ttype(fi)) { | 1385 | switch (ttypetag(fi)) { |
| 1270 | case LUA_TLCL: { /* lua closure */ | 1386 | case LUA_VLCL: { /* lua closure */ |
| 1271 | return *getupvalref(L, fidx, n); | 1387 | return *getupvalref(L, fidx, n, NULL); |
| 1272 | } | 1388 | } |
| 1273 | case LUA_TCCL: { /* C closure */ | 1389 | case LUA_VCCL: { /* C closure */ |
| 1274 | CClosure *f = clCvalue(fi); | 1390 | CClosure *f = clCvalue(fi); |
| 1275 | api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index"); | 1391 | api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index"); |
| 1276 | return &f->upvalue[n - 1]; | 1392 | return &f->upvalue[n - 1]; |
| @@ -1285,15 +1401,11 @@ LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { | |||
| 1285 | 1401 | ||
| 1286 | LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1, | 1402 | LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1, |
| 1287 | int fidx2, int n2) { | 1403 | int fidx2, int n2) { |
| 1288 | UpVal **up1 = getupvalref(L, fidx1, n1); | 1404 | LClosure *f1; |
| 1289 | UpVal **up2 = getupvalref(L, fidx2, n2); | 1405 | UpVal **up1 = getupvalref(L, fidx1, n1, &f1); |
| 1290 | if (*up1 == *up2) | 1406 | UpVal **up2 = getupvalref(L, fidx2, n2, NULL); |
| 1291 | return; | ||
| 1292 | luaC_upvdeccount(L, *up1); | ||
| 1293 | *up1 = *up2; | 1407 | *up1 = *up2; |
| 1294 | (*up1)->refcount++; | 1408 | luaC_objbarrier(L, f1, *up1); |
| 1295 | if (upisopen(*up1)) (*up1)->u.open.touched = 1; | ||
| 1296 | luaC_upvalbarrier(L, *up1); | ||
| 1297 | } | 1409 | } |
| 1298 | 1410 | ||
| 1299 | 1411 | ||
