diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2024-09-19 00:43:31 +0200 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2024-09-19 00:43:31 +0200 |
| commit | 402b97fbdf4857adc5aa80f85fa5e59148e126b7 (patch) | |
| tree | 666e08bf605b3e09064cb965c3596684e3c0da5e /zig-lua/lua-5.4.7/lvm.c | |
| parent | 2e2da1fd114edb932954a108ddb6f7359dbb868a (diff) | |
| download | probe-402b97fbdf4857adc5aa80f85fa5e59148e126b7.tar.gz | |
Added debuggers to Nix shell
Diffstat (limited to 'zig-lua/lua-5.4.7/lvm.c')
| -rw-r--r-- | zig-lua/lua-5.4.7/lvm.c | 1899 |
1 files changed, 1899 insertions, 0 deletions
diff --git a/zig-lua/lua-5.4.7/lvm.c b/zig-lua/lua-5.4.7/lvm.c new file mode 100644 index 0000000..fcd24e1 --- /dev/null +++ b/zig-lua/lua-5.4.7/lvm.c | |||
| @@ -0,0 +1,1899 @@ | |||
| 1 | /* | ||
| 2 | ** $Id: lvm.c $ | ||
| 3 | ** Lua virtual machine | ||
| 4 | ** See Copyright Notice in lua.h | ||
| 5 | */ | ||
| 6 | |||
| 7 | #define lvm_c | ||
| 8 | #define LUA_CORE | ||
| 9 | |||
| 10 | #include "lprefix.h" | ||
| 11 | |||
| 12 | #include <float.h> | ||
| 13 | #include <limits.h> | ||
| 14 | #include <math.h> | ||
| 15 | #include <stdio.h> | ||
| 16 | #include <stdlib.h> | ||
| 17 | #include <string.h> | ||
| 18 | |||
| 19 | #include "lua.h" | ||
| 20 | |||
| 21 | #include "ldebug.h" | ||
| 22 | #include "ldo.h" | ||
| 23 | #include "lfunc.h" | ||
| 24 | #include "lgc.h" | ||
| 25 | #include "lobject.h" | ||
| 26 | #include "lopcodes.h" | ||
| 27 | #include "lstate.h" | ||
| 28 | #include "lstring.h" | ||
| 29 | #include "ltable.h" | ||
| 30 | #include "ltm.h" | ||
| 31 | #include "lvm.h" | ||
| 32 | |||
| 33 | |||
| 34 | /* | ||
| 35 | ** By default, use jump tables in the main interpreter loop on gcc | ||
| 36 | ** and compatible compilers. | ||
| 37 | */ | ||
| 38 | #if !defined(LUA_USE_JUMPTABLE) | ||
| 39 | #if defined(__GNUC__) | ||
| 40 | #define LUA_USE_JUMPTABLE 1 | ||
| 41 | #else | ||
| 42 | #define LUA_USE_JUMPTABLE 0 | ||
| 43 | #endif | ||
| 44 | #endif | ||
| 45 | |||
| 46 | |||
| 47 | |||
| 48 | /* limit for table tag-method chains (to avoid infinite loops) */ | ||
| 49 | #define MAXTAGLOOP 2000 | ||
| 50 | |||
| 51 | |||
| 52 | /* | ||
| 53 | ** 'l_intfitsf' checks whether a given integer is in the range that | ||
| 54 | ** can be converted to a float without rounding. Used in comparisons. | ||
| 55 | */ | ||
| 56 | |||
| 57 | /* number of bits in the mantissa of a float */ | ||
| 58 | #define NBM (l_floatatt(MANT_DIG)) | ||
| 59 | |||
| 60 | /* | ||
| 61 | ** Check whether some integers may not fit in a float, testing whether | ||
| 62 | ** (maxinteger >> NBM) > 0. (That implies (1 << NBM) <= maxinteger.) | ||
| 63 | ** (The shifts are done in parts, to avoid shifting by more than the size | ||
| 64 | ** of an integer. In a worst case, NBM == 113 for long double and | ||
| 65 | ** sizeof(long) == 32.) | ||
| 66 | */ | ||
| 67 | #if ((((LUA_MAXINTEGER >> (NBM / 4)) >> (NBM / 4)) >> (NBM / 4)) \ | ||
| 68 | >> (NBM - (3 * (NBM / 4)))) > 0 | ||
| 69 | |||
| 70 | /* limit for integers that fit in a float */ | ||
| 71 | #define MAXINTFITSF ((lua_Unsigned)1 << NBM) | ||
| 72 | |||
| 73 | /* check whether 'i' is in the interval [-MAXINTFITSF, MAXINTFITSF] */ | ||
| 74 | #define l_intfitsf(i) ((MAXINTFITSF + l_castS2U(i)) <= (2 * MAXINTFITSF)) | ||
| 75 | |||
| 76 | #else /* all integers fit in a float precisely */ | ||
| 77 | |||
| 78 | #define l_intfitsf(i) 1 | ||
| 79 | |||
| 80 | #endif | ||
| 81 | |||
| 82 | |||
| 83 | /* | ||
| 84 | ** Try to convert a value from string to a number value. | ||
| 85 | ** If the value is not a string or is a string not representing | ||
| 86 | ** a valid numeral (or if coercions from strings to numbers | ||
| 87 | ** are disabled via macro 'cvt2num'), do not modify 'result' | ||
| 88 | ** and return 0. | ||
| 89 | */ | ||
| 90 | static int l_strton (const TValue *obj, TValue *result) { | ||
| 91 | lua_assert(obj != result); | ||
| 92 | if (!cvt2num(obj)) /* is object not a string? */ | ||
| 93 | return 0; | ||
| 94 | else { | ||
| 95 | TString *st = tsvalue(obj); | ||
| 96 | return (luaO_str2num(getstr(st), result) == tsslen(st) + 1); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | |||
| 101 | /* | ||
| 102 | ** Try to convert a value to a float. The float case is already handled | ||
| 103 | ** by the macro 'tonumber'. | ||
| 104 | */ | ||
| 105 | int luaV_tonumber_ (const TValue *obj, lua_Number *n) { | ||
| 106 | TValue v; | ||
| 107 | if (ttisinteger(obj)) { | ||
| 108 | *n = cast_num(ivalue(obj)); | ||
| 109 | return 1; | ||
| 110 | } | ||
| 111 | else if (l_strton(obj, &v)) { /* string coercible to number? */ | ||
| 112 | *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */ | ||
| 113 | return 1; | ||
| 114 | } | ||
| 115 | else | ||
| 116 | return 0; /* conversion failed */ | ||
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | /* | ||
| 121 | ** try to convert a float to an integer, rounding according to 'mode'. | ||
| 122 | */ | ||
| 123 | int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode) { | ||
| 124 | lua_Number f = l_floor(n); | ||
| 125 | if (n != f) { /* not an integral value? */ | ||
| 126 | if (mode == F2Ieq) return 0; /* fails if mode demands integral value */ | ||
| 127 | else if (mode == F2Iceil) /* needs ceil? */ | ||
| 128 | f += 1; /* convert floor to ceil (remember: n != f) */ | ||
| 129 | } | ||
| 130 | return lua_numbertointeger(f, p); | ||
| 131 | } | ||
| 132 | |||
| 133 | |||
| 134 | /* | ||
| 135 | ** try to convert a value to an integer, rounding according to 'mode', | ||
| 136 | ** without string coercion. | ||
| 137 | ** ("Fast track" handled by macro 'tointegerns'.) | ||
| 138 | */ | ||
| 139 | int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode) { | ||
| 140 | if (ttisfloat(obj)) | ||
| 141 | return luaV_flttointeger(fltvalue(obj), p, mode); | ||
| 142 | else if (ttisinteger(obj)) { | ||
| 143 | *p = ivalue(obj); | ||
| 144 | return 1; | ||
| 145 | } | ||
| 146 | else | ||
| 147 | return 0; | ||
| 148 | } | ||
| 149 | |||
| 150 | |||
| 151 | /* | ||
| 152 | ** try to convert a value to an integer. | ||
| 153 | */ | ||
| 154 | int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode) { | ||
| 155 | TValue v; | ||
| 156 | if (l_strton(obj, &v)) /* does 'obj' point to a numerical string? */ | ||
| 157 | obj = &v; /* change it to point to its corresponding number */ | ||
| 158 | return luaV_tointegerns(obj, p, mode); | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | /* | ||
| 163 | ** Try to convert a 'for' limit to an integer, preserving the semantics | ||
| 164 | ** of the loop. Return true if the loop must not run; otherwise, '*p' | ||
| 165 | ** gets the integer limit. | ||
| 166 | ** (The following explanation assumes a positive step; it is valid for | ||
| 167 | ** negative steps mutatis mutandis.) | ||
| 168 | ** If the limit is an integer or can be converted to an integer, | ||
| 169 | ** rounding down, that is the limit. | ||
| 170 | ** Otherwise, check whether the limit can be converted to a float. If | ||
| 171 | ** the float is too large, clip it to LUA_MAXINTEGER. If the float | ||
| 172 | ** is too negative, the loop should not run, because any initial | ||
| 173 | ** integer value is greater than such limit; so, the function returns | ||
| 174 | ** true to signal that. (For this latter case, no integer limit would be | ||
| 175 | ** correct; even a limit of LUA_MININTEGER would run the loop once for | ||
| 176 | ** an initial value equal to LUA_MININTEGER.) | ||
| 177 | */ | ||
| 178 | static int forlimit (lua_State *L, lua_Integer init, const TValue *lim, | ||
| 179 | lua_Integer *p, lua_Integer step) { | ||
| 180 | if (!luaV_tointeger(lim, p, (step < 0 ? F2Iceil : F2Ifloor))) { | ||
| 181 | /* not coercible to in integer */ | ||
| 182 | lua_Number flim; /* try to convert to float */ | ||
| 183 | if (!tonumber(lim, &flim)) /* cannot convert to float? */ | ||
| 184 | luaG_forerror(L, lim, "limit"); | ||
| 185 | /* else 'flim' is a float out of integer bounds */ | ||
| 186 | if (luai_numlt(0, flim)) { /* if it is positive, it is too large */ | ||
| 187 | if (step < 0) return 1; /* initial value must be less than it */ | ||
| 188 | *p = LUA_MAXINTEGER; /* truncate */ | ||
| 189 | } | ||
| 190 | else { /* it is less than min integer */ | ||
| 191 | if (step > 0) return 1; /* initial value must be greater than it */ | ||
| 192 | *p = LUA_MININTEGER; /* truncate */ | ||
| 193 | } | ||
| 194 | } | ||
| 195 | return (step > 0 ? init > *p : init < *p); /* not to run? */ | ||
| 196 | } | ||
| 197 | |||
| 198 | |||
| 199 | /* | ||
| 200 | ** Prepare a numerical for loop (opcode OP_FORPREP). | ||
| 201 | ** Return true to skip the loop. Otherwise, | ||
| 202 | ** after preparation, stack will be as follows: | ||
| 203 | ** ra : internal index (safe copy of the control variable) | ||
| 204 | ** ra + 1 : loop counter (integer loops) or limit (float loops) | ||
| 205 | ** ra + 2 : step | ||
| 206 | ** ra + 3 : control variable | ||
| 207 | */ | ||
| 208 | static int forprep (lua_State *L, StkId ra) { | ||
| 209 | TValue *pinit = s2v(ra); | ||
| 210 | TValue *plimit = s2v(ra + 1); | ||
| 211 | TValue *pstep = s2v(ra + 2); | ||
| 212 | if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */ | ||
| 213 | lua_Integer init = ivalue(pinit); | ||
| 214 | lua_Integer step = ivalue(pstep); | ||
| 215 | lua_Integer limit; | ||
| 216 | if (step == 0) | ||
| 217 | luaG_runerror(L, "'for' step is zero"); | ||
| 218 | setivalue(s2v(ra + 3), init); /* control variable */ | ||
| 219 | if (forlimit(L, init, plimit, &limit, step)) | ||
| 220 | return 1; /* skip the loop */ | ||
| 221 | else { /* prepare loop counter */ | ||
| 222 | lua_Unsigned count; | ||
| 223 | if (step > 0) { /* ascending loop? */ | ||
| 224 | count = l_castS2U(limit) - l_castS2U(init); | ||
| 225 | if (step != 1) /* avoid division in the too common case */ | ||
| 226 | count /= l_castS2U(step); | ||
| 227 | } | ||
| 228 | else { /* step < 0; descending loop */ | ||
| 229 | count = l_castS2U(init) - l_castS2U(limit); | ||
| 230 | /* 'step+1' avoids negating 'mininteger' */ | ||
| 231 | count /= l_castS2U(-(step + 1)) + 1u; | ||
| 232 | } | ||
| 233 | /* store the counter in place of the limit (which won't be | ||
| 234 | needed anymore) */ | ||
| 235 | setivalue(plimit, l_castU2S(count)); | ||
| 236 | } | ||
| 237 | } | ||
| 238 | else { /* try making all values floats */ | ||
| 239 | lua_Number init; lua_Number limit; lua_Number step; | ||
| 240 | if (l_unlikely(!tonumber(plimit, &limit))) | ||
| 241 | luaG_forerror(L, plimit, "limit"); | ||
| 242 | if (l_unlikely(!tonumber(pstep, &step))) | ||
| 243 | luaG_forerror(L, pstep, "step"); | ||
| 244 | if (l_unlikely(!tonumber(pinit, &init))) | ||
| 245 | luaG_forerror(L, pinit, "initial value"); | ||
| 246 | if (step == 0) | ||
| 247 | luaG_runerror(L, "'for' step is zero"); | ||
| 248 | if (luai_numlt(0, step) ? luai_numlt(limit, init) | ||
| 249 | : luai_numlt(init, limit)) | ||
| 250 | return 1; /* skip the loop */ | ||
| 251 | else { | ||
| 252 | /* make sure internal values are all floats */ | ||
| 253 | setfltvalue(plimit, limit); | ||
| 254 | setfltvalue(pstep, step); | ||
| 255 | setfltvalue(s2v(ra), init); /* internal index */ | ||
| 256 | setfltvalue(s2v(ra + 3), init); /* control variable */ | ||
| 257 | } | ||
| 258 | } | ||
| 259 | return 0; | ||
| 260 | } | ||
| 261 | |||
| 262 | |||
| 263 | /* | ||
| 264 | ** Execute a step of a float numerical for loop, returning | ||
| 265 | ** true iff the loop must continue. (The integer case is | ||
| 266 | ** written online with opcode OP_FORLOOP, for performance.) | ||
| 267 | */ | ||
| 268 | static int floatforloop (StkId ra) { | ||
| 269 | lua_Number step = fltvalue(s2v(ra + 2)); | ||
| 270 | lua_Number limit = fltvalue(s2v(ra + 1)); | ||
| 271 | lua_Number idx = fltvalue(s2v(ra)); /* internal index */ | ||
| 272 | idx = luai_numadd(L, idx, step); /* increment index */ | ||
| 273 | if (luai_numlt(0, step) ? luai_numle(idx, limit) | ||
| 274 | : luai_numle(limit, idx)) { | ||
| 275 | chgfltvalue(s2v(ra), idx); /* update internal index */ | ||
| 276 | setfltvalue(s2v(ra + 3), idx); /* and control variable */ | ||
| 277 | return 1; /* jump back */ | ||
| 278 | } | ||
| 279 | else | ||
| 280 | return 0; /* finish the loop */ | ||
| 281 | } | ||
| 282 | |||
| 283 | |||
| 284 | /* | ||
| 285 | ** Finish the table access 'val = t[key]'. | ||
| 286 | ** if 'slot' is NULL, 't' is not a table; otherwise, 'slot' points to | ||
| 287 | ** t[k] entry (which must be empty). | ||
| 288 | */ | ||
| 289 | void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, | ||
| 290 | const TValue *slot) { | ||
| 291 | int loop; /* counter to avoid infinite loops */ | ||
| 292 | const TValue *tm; /* metamethod */ | ||
| 293 | for (loop = 0; loop < MAXTAGLOOP; loop++) { | ||
| 294 | if (slot == NULL) { /* 't' is not a table? */ | ||
| 295 | lua_assert(!ttistable(t)); | ||
| 296 | tm = luaT_gettmbyobj(L, t, TM_INDEX); | ||
| 297 | if (l_unlikely(notm(tm))) | ||
| 298 | luaG_typeerror(L, t, "index"); /* no metamethod */ | ||
| 299 | /* else will try the metamethod */ | ||
| 300 | } | ||
| 301 | else { /* 't' is a table */ | ||
| 302 | lua_assert(isempty(slot)); | ||
| 303 | tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */ | ||
| 304 | if (tm == NULL) { /* no metamethod? */ | ||
| 305 | setnilvalue(s2v(val)); /* result is nil */ | ||
| 306 | return; | ||
| 307 | } | ||
| 308 | /* else will try the metamethod */ | ||
| 309 | } | ||
| 310 | if (ttisfunction(tm)) { /* is metamethod a function? */ | ||
| 311 | luaT_callTMres(L, tm, t, key, val); /* call it */ | ||
| 312 | return; | ||
| 313 | } | ||
| 314 | t = tm; /* else try to access 'tm[key]' */ | ||
| 315 | if (luaV_fastget(L, t, key, slot, luaH_get)) { /* fast track? */ | ||
| 316 | setobj2s(L, val, slot); /* done */ | ||
| 317 | return; | ||
| 318 | } | ||
| 319 | /* else repeat (tail call 'luaV_finishget') */ | ||
| 320 | } | ||
| 321 | luaG_runerror(L, "'__index' chain too long; possible loop"); | ||
| 322 | } | ||
| 323 | |||
| 324 | |||
| 325 | /* | ||
| 326 | ** Finish a table assignment 't[key] = val'. | ||
| 327 | ** If 'slot' is NULL, 't' is not a table. Otherwise, 'slot' points | ||
| 328 | ** to the entry 't[key]', or to a value with an absent key if there | ||
| 329 | ** is no such entry. (The value at 'slot' must be empty, otherwise | ||
| 330 | ** 'luaV_fastget' would have done the job.) | ||
| 331 | */ | ||
| 332 | void luaV_finishset (lua_State *L, const TValue *t, TValue *key, | ||
| 333 | TValue *val, const TValue *slot) { | ||
| 334 | int loop; /* counter to avoid infinite loops */ | ||
| 335 | for (loop = 0; loop < MAXTAGLOOP; loop++) { | ||
| 336 | const TValue *tm; /* '__newindex' metamethod */ | ||
| 337 | if (slot != NULL) { /* is 't' a table? */ | ||
| 338 | Table *h = hvalue(t); /* save 't' table */ | ||
| 339 | lua_assert(isempty(slot)); /* slot must be empty */ | ||
| 340 | tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ | ||
| 341 | if (tm == NULL) { /* no metamethod? */ | ||
| 342 | luaH_finishset(L, h, key, slot, val); /* set new value */ | ||
| 343 | invalidateTMcache(h); | ||
| 344 | luaC_barrierback(L, obj2gco(h), val); | ||
| 345 | return; | ||
| 346 | } | ||
| 347 | /* else will try the metamethod */ | ||
| 348 | } | ||
| 349 | else { /* not a table; check metamethod */ | ||
| 350 | tm = luaT_gettmbyobj(L, t, TM_NEWINDEX); | ||
| 351 | if (l_unlikely(notm(tm))) | ||
| 352 | luaG_typeerror(L, t, "index"); | ||
| 353 | } | ||
| 354 | /* try the metamethod */ | ||
| 355 | if (ttisfunction(tm)) { | ||
| 356 | luaT_callTM(L, tm, t, key, val); | ||
| 357 | return; | ||
| 358 | } | ||
| 359 | t = tm; /* else repeat assignment over 'tm' */ | ||
| 360 | if (luaV_fastget(L, t, key, slot, luaH_get)) { | ||
| 361 | luaV_finishfastset(L, t, slot, val); | ||
| 362 | return; /* done */ | ||
| 363 | } | ||
| 364 | /* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */ | ||
| 365 | } | ||
| 366 | luaG_runerror(L, "'__newindex' chain too long; possible loop"); | ||
| 367 | } | ||
| 368 | |||
| 369 | |||
| 370 | /* | ||
| 371 | ** Compare two strings 'ts1' x 'ts2', returning an integer less-equal- | ||
| 372 | ** -greater than zero if 'ts1' is less-equal-greater than 'ts2'. | ||
| 373 | ** The code is a little tricky because it allows '\0' in the strings | ||
| 374 | ** and it uses 'strcoll' (to respect locales) for each segment | ||
| 375 | ** of the strings. Note that segments can compare equal but still | ||
| 376 | ** have different lengths. | ||
| 377 | */ | ||
| 378 | static int l_strcmp (const TString *ts1, const TString *ts2) { | ||
| 379 | const char *s1 = getstr(ts1); | ||
| 380 | size_t rl1 = tsslen(ts1); /* real length */ | ||
| 381 | const char *s2 = getstr(ts2); | ||
| 382 | size_t rl2 = tsslen(ts2); | ||
| 383 | for (;;) { /* for each segment */ | ||
| 384 | int temp = strcoll(s1, s2); | ||
| 385 | if (temp != 0) /* not equal? */ | ||
| 386 | return temp; /* done */ | ||
| 387 | else { /* strings are equal up to a '\0' */ | ||
| 388 | size_t zl1 = strlen(s1); /* index of first '\0' in 's1' */ | ||
| 389 | size_t zl2 = strlen(s2); /* index of first '\0' in 's2' */ | ||
| 390 | if (zl2 == rl2) /* 's2' is finished? */ | ||
| 391 | return (zl1 == rl1) ? 0 : 1; /* check 's1' */ | ||
| 392 | else if (zl1 == rl1) /* 's1' is finished? */ | ||
| 393 | return -1; /* 's1' is less than 's2' ('s2' is not finished) */ | ||
| 394 | /* both strings longer than 'zl'; go on comparing after the '\0' */ | ||
| 395 | zl1++; zl2++; | ||
| 396 | s1 += zl1; rl1 -= zl1; s2 += zl2; rl2 -= zl2; | ||
| 397 | } | ||
| 398 | } | ||
| 399 | } | ||
| 400 | |||
| 401 | |||
| 402 | /* | ||
| 403 | ** Check whether integer 'i' is less than float 'f'. If 'i' has an | ||
| 404 | ** exact representation as a float ('l_intfitsf'), compare numbers as | ||
| 405 | ** floats. Otherwise, use the equivalence 'i < f <=> i < ceil(f)'. | ||
| 406 | ** If 'ceil(f)' is out of integer range, either 'f' is greater than | ||
| 407 | ** all integers or less than all integers. | ||
| 408 | ** (The test with 'l_intfitsf' is only for performance; the else | ||
| 409 | ** case is correct for all values, but it is slow due to the conversion | ||
| 410 | ** from float to int.) | ||
| 411 | ** When 'f' is NaN, comparisons must result in false. | ||
| 412 | */ | ||
| 413 | l_sinline int LTintfloat (lua_Integer i, lua_Number f) { | ||
| 414 | if (l_intfitsf(i)) | ||
| 415 | return luai_numlt(cast_num(i), f); /* compare them as floats */ | ||
| 416 | else { /* i < f <=> i < ceil(f) */ | ||
| 417 | lua_Integer fi; | ||
| 418 | if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */ | ||
| 419 | return i < fi; /* compare them as integers */ | ||
| 420 | else /* 'f' is either greater or less than all integers */ | ||
| 421 | return f > 0; /* greater? */ | ||
| 422 | } | ||
| 423 | } | ||
| 424 | |||
| 425 | |||
| 426 | /* | ||
| 427 | ** Check whether integer 'i' is less than or equal to float 'f'. | ||
| 428 | ** See comments on previous function. | ||
| 429 | */ | ||
| 430 | l_sinline int LEintfloat (lua_Integer i, lua_Number f) { | ||
| 431 | if (l_intfitsf(i)) | ||
| 432 | return luai_numle(cast_num(i), f); /* compare them as floats */ | ||
| 433 | else { /* i <= f <=> i <= floor(f) */ | ||
| 434 | lua_Integer fi; | ||
| 435 | if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */ | ||
| 436 | return i <= fi; /* compare them as integers */ | ||
| 437 | else /* 'f' is either greater or less than all integers */ | ||
| 438 | return f > 0; /* greater? */ | ||
| 439 | } | ||
| 440 | } | ||
| 441 | |||
| 442 | |||
| 443 | /* | ||
| 444 | ** Check whether float 'f' is less than integer 'i'. | ||
| 445 | ** See comments on previous function. | ||
| 446 | */ | ||
| 447 | l_sinline int LTfloatint (lua_Number f, lua_Integer i) { | ||
| 448 | if (l_intfitsf(i)) | ||
| 449 | return luai_numlt(f, cast_num(i)); /* compare them as floats */ | ||
| 450 | else { /* f < i <=> floor(f) < i */ | ||
| 451 | lua_Integer fi; | ||
| 452 | if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */ | ||
| 453 | return fi < i; /* compare them as integers */ | ||
| 454 | else /* 'f' is either greater or less than all integers */ | ||
| 455 | return f < 0; /* less? */ | ||
| 456 | } | ||
| 457 | } | ||
| 458 | |||
| 459 | |||
| 460 | /* | ||
| 461 | ** Check whether float 'f' is less than or equal to integer 'i'. | ||
| 462 | ** See comments on previous function. | ||
| 463 | */ | ||
| 464 | l_sinline int LEfloatint (lua_Number f, lua_Integer i) { | ||
| 465 | if (l_intfitsf(i)) | ||
| 466 | return luai_numle(f, cast_num(i)); /* compare them as floats */ | ||
| 467 | else { /* f <= i <=> ceil(f) <= i */ | ||
| 468 | lua_Integer fi; | ||
| 469 | if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */ | ||
| 470 | return fi <= i; /* compare them as integers */ | ||
| 471 | else /* 'f' is either greater or less than all integers */ | ||
| 472 | return f < 0; /* less? */ | ||
| 473 | } | ||
| 474 | } | ||
| 475 | |||
| 476 | |||
| 477 | /* | ||
| 478 | ** Return 'l < r', for numbers. | ||
| 479 | */ | ||
| 480 | l_sinline int LTnum (const TValue *l, const TValue *r) { | ||
| 481 | lua_assert(ttisnumber(l) && ttisnumber(r)); | ||
| 482 | if (ttisinteger(l)) { | ||
| 483 | lua_Integer li = ivalue(l); | ||
| 484 | if (ttisinteger(r)) | ||
| 485 | return li < ivalue(r); /* both are integers */ | ||
| 486 | else /* 'l' is int and 'r' is float */ | ||
| 487 | return LTintfloat(li, fltvalue(r)); /* l < r ? */ | ||
| 488 | } | ||
| 489 | else { | ||
| 490 | lua_Number lf = fltvalue(l); /* 'l' must be float */ | ||
| 491 | if (ttisfloat(r)) | ||
| 492 | return luai_numlt(lf, fltvalue(r)); /* both are float */ | ||
| 493 | else /* 'l' is float and 'r' is int */ | ||
| 494 | return LTfloatint(lf, ivalue(r)); | ||
| 495 | } | ||
| 496 | } | ||
| 497 | |||
| 498 | |||
| 499 | /* | ||
| 500 | ** Return 'l <= r', for numbers. | ||
| 501 | */ | ||
| 502 | l_sinline int LEnum (const TValue *l, const TValue *r) { | ||
| 503 | lua_assert(ttisnumber(l) && ttisnumber(r)); | ||
| 504 | if (ttisinteger(l)) { | ||
| 505 | lua_Integer li = ivalue(l); | ||
| 506 | if (ttisinteger(r)) | ||
| 507 | return li <= ivalue(r); /* both are integers */ | ||
| 508 | else /* 'l' is int and 'r' is float */ | ||
| 509 | return LEintfloat(li, fltvalue(r)); /* l <= r ? */ | ||
| 510 | } | ||
| 511 | else { | ||
| 512 | lua_Number lf = fltvalue(l); /* 'l' must be float */ | ||
| 513 | if (ttisfloat(r)) | ||
| 514 | return luai_numle(lf, fltvalue(r)); /* both are float */ | ||
| 515 | else /* 'l' is float and 'r' is int */ | ||
| 516 | return LEfloatint(lf, ivalue(r)); | ||
| 517 | } | ||
| 518 | } | ||
| 519 | |||
| 520 | |||
| 521 | /* | ||
| 522 | ** return 'l < r' for non-numbers. | ||
| 523 | */ | ||
| 524 | static int lessthanothers (lua_State *L, const TValue *l, const TValue *r) { | ||
| 525 | lua_assert(!ttisnumber(l) || !ttisnumber(r)); | ||
| 526 | if (ttisstring(l) && ttisstring(r)) /* both are strings? */ | ||
| 527 | return l_strcmp(tsvalue(l), tsvalue(r)) < 0; | ||
| 528 | else | ||
| 529 | return luaT_callorderTM(L, l, r, TM_LT); | ||
| 530 | } | ||
| 531 | |||
| 532 | |||
| 533 | /* | ||
| 534 | ** Main operation less than; return 'l < r'. | ||
| 535 | */ | ||
| 536 | int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { | ||
| 537 | if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ | ||
| 538 | return LTnum(l, r); | ||
| 539 | else return lessthanothers(L, l, r); | ||
| 540 | } | ||
| 541 | |||
| 542 | |||
| 543 | /* | ||
| 544 | ** return 'l <= r' for non-numbers. | ||
| 545 | */ | ||
| 546 | static int lessequalothers (lua_State *L, const TValue *l, const TValue *r) { | ||
| 547 | lua_assert(!ttisnumber(l) || !ttisnumber(r)); | ||
| 548 | if (ttisstring(l) && ttisstring(r)) /* both are strings? */ | ||
| 549 | return l_strcmp(tsvalue(l), tsvalue(r)) <= 0; | ||
| 550 | else | ||
| 551 | return luaT_callorderTM(L, l, r, TM_LE); | ||
| 552 | } | ||
| 553 | |||
| 554 | |||
| 555 | /* | ||
| 556 | ** Main operation less than or equal to; return 'l <= r'. | ||
| 557 | */ | ||
| 558 | int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { | ||
| 559 | if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ | ||
| 560 | return LEnum(l, r); | ||
| 561 | else return lessequalothers(L, l, r); | ||
| 562 | } | ||
| 563 | |||
| 564 | |||
| 565 | /* | ||
| 566 | ** Main operation for equality of Lua values; return 't1 == t2'. | ||
| 567 | ** L == NULL means raw equality (no metamethods) | ||
| 568 | */ | ||
| 569 | int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { | ||
| 570 | const TValue *tm; | ||
| 571 | if (ttypetag(t1) != ttypetag(t2)) { /* not the same variant? */ | ||
| 572 | if (ttype(t1) != ttype(t2) || ttype(t1) != LUA_TNUMBER) | ||
| 573 | return 0; /* only numbers can be equal with different variants */ | ||
| 574 | else { /* two numbers with different variants */ | ||
| 575 | /* One of them is an integer. If the other does not have an | ||
| 576 | integer value, they cannot be equal; otherwise, compare their | ||
| 577 | integer values. */ | ||
| 578 | lua_Integer i1, i2; | ||
| 579 | return (luaV_tointegerns(t1, &i1, F2Ieq) && | ||
| 580 | luaV_tointegerns(t2, &i2, F2Ieq) && | ||
| 581 | i1 == i2); | ||
| 582 | } | ||
| 583 | } | ||
| 584 | /* values have same type and same variant */ | ||
| 585 | switch (ttypetag(t1)) { | ||
| 586 | case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: return 1; | ||
| 587 | case LUA_VNUMINT: return (ivalue(t1) == ivalue(t2)); | ||
| 588 | case LUA_VNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2)); | ||
| 589 | case LUA_VLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); | ||
| 590 | case LUA_VLCF: return fvalue(t1) == fvalue(t2); | ||
| 591 | case LUA_VSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2)); | ||
| 592 | case LUA_VLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2)); | ||
| 593 | case LUA_VUSERDATA: { | ||
| 594 | if (uvalue(t1) == uvalue(t2)) return 1; | ||
| 595 | else if (L == NULL) return 0; | ||
| 596 | tm = fasttm(L, uvalue(t1)->metatable, TM_EQ); | ||
| 597 | if (tm == NULL) | ||
| 598 | tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); | ||
| 599 | break; /* will try TM */ | ||
| 600 | } | ||
| 601 | case LUA_VTABLE: { | ||
| 602 | if (hvalue(t1) == hvalue(t2)) return 1; | ||
| 603 | else if (L == NULL) return 0; | ||
| 604 | tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); | ||
| 605 | if (tm == NULL) | ||
| 606 | tm = fasttm(L, hvalue(t2)->metatable, TM_EQ); | ||
| 607 | break; /* will try TM */ | ||
| 608 | } | ||
| 609 | default: | ||
| 610 | return gcvalue(t1) == gcvalue(t2); | ||
| 611 | } | ||
| 612 | if (tm == NULL) /* no TM? */ | ||
| 613 | return 0; /* objects are different */ | ||
| 614 | else { | ||
| 615 | luaT_callTMres(L, tm, t1, t2, L->top.p); /* call TM */ | ||
| 616 | return !l_isfalse(s2v(L->top.p)); | ||
| 617 | } | ||
| 618 | } | ||
| 619 | |||
| 620 | |||
| 621 | /* macro used by 'luaV_concat' to ensure that element at 'o' is a string */ | ||
| 622 | #define tostring(L,o) \ | ||
| 623 | (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1))) | ||
| 624 | |||
| 625 | #define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0) | ||
| 626 | |||
| 627 | /* copy strings in stack from top - n up to top - 1 to buffer */ | ||
| 628 | static void copy2buff (StkId top, int n, char *buff) { | ||
| 629 | size_t tl = 0; /* size already copied */ | ||
| 630 | do { | ||
| 631 | TString *st = tsvalue(s2v(top - n)); | ||
| 632 | size_t l = tsslen(st); /* length of string being copied */ | ||
| 633 | memcpy(buff + tl, getstr(st), l * sizeof(char)); | ||
| 634 | tl += l; | ||
| 635 | } while (--n > 0); | ||
| 636 | } | ||
| 637 | |||
| 638 | |||
| 639 | /* | ||
| 640 | ** Main operation for concatenation: concat 'total' values in the stack, | ||
| 641 | ** from 'L->top.p - total' up to 'L->top.p - 1'. | ||
| 642 | */ | ||
| 643 | void luaV_concat (lua_State *L, int total) { | ||
| 644 | if (total == 1) | ||
| 645 | return; /* "all" values already concatenated */ | ||
| 646 | do { | ||
| 647 | StkId top = L->top.p; | ||
| 648 | int n = 2; /* number of elements handled in this pass (at least 2) */ | ||
| 649 | if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) || | ||
| 650 | !tostring(L, s2v(top - 1))) | ||
| 651 | luaT_tryconcatTM(L); /* may invalidate 'top' */ | ||
| 652 | else if (isemptystr(s2v(top - 1))) /* second operand is empty? */ | ||
| 653 | cast_void(tostring(L, s2v(top - 2))); /* result is first operand */ | ||
| 654 | else if (isemptystr(s2v(top - 2))) { /* first operand is empty string? */ | ||
| 655 | setobjs2s(L, top - 2, top - 1); /* result is second op. */ | ||
| 656 | } | ||
| 657 | else { | ||
| 658 | /* at least two non-empty string values; get as many as possible */ | ||
| 659 | size_t tl = tsslen(tsvalue(s2v(top - 1))); | ||
| 660 | TString *ts; | ||
| 661 | /* collect total length and number of strings */ | ||
| 662 | for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { | ||
| 663 | size_t l = tsslen(tsvalue(s2v(top - n - 1))); | ||
| 664 | if (l_unlikely(l >= MAX_SIZE - sizeof(TString) - tl)) { | ||
| 665 | L->top.p = top - total; /* pop strings to avoid wasting stack */ | ||
| 666 | luaG_runerror(L, "string length overflow"); | ||
| 667 | } | ||
| 668 | tl += l; | ||
| 669 | } | ||
| 670 | if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */ | ||
| 671 | char buff[LUAI_MAXSHORTLEN]; | ||
| 672 | copy2buff(top, n, buff); /* copy strings to buffer */ | ||
| 673 | ts = luaS_newlstr(L, buff, tl); | ||
| 674 | } | ||
| 675 | else { /* long string; copy strings directly to final result */ | ||
| 676 | ts = luaS_createlngstrobj(L, tl); | ||
| 677 | copy2buff(top, n, getlngstr(ts)); | ||
| 678 | } | ||
| 679 | setsvalue2s(L, top - n, ts); /* create result */ | ||
| 680 | } | ||
| 681 | total -= n - 1; /* got 'n' strings to create one new */ | ||
| 682 | L->top.p -= n - 1; /* popped 'n' strings and pushed one */ | ||
| 683 | } while (total > 1); /* repeat until only 1 result left */ | ||
| 684 | } | ||
| 685 | |||
| 686 | |||
| 687 | /* | ||
| 688 | ** Main operation 'ra = #rb'. | ||
| 689 | */ | ||
| 690 | void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { | ||
| 691 | const TValue *tm; | ||
| 692 | switch (ttypetag(rb)) { | ||
| 693 | case LUA_VTABLE: { | ||
| 694 | Table *h = hvalue(rb); | ||
| 695 | tm = fasttm(L, h->metatable, TM_LEN); | ||
| 696 | if (tm) break; /* metamethod? break switch to call it */ | ||
| 697 | setivalue(s2v(ra), luaH_getn(h)); /* else primitive len */ | ||
| 698 | return; | ||
| 699 | } | ||
| 700 | case LUA_VSHRSTR: { | ||
| 701 | setivalue(s2v(ra), tsvalue(rb)->shrlen); | ||
| 702 | return; | ||
| 703 | } | ||
| 704 | case LUA_VLNGSTR: { | ||
| 705 | setivalue(s2v(ra), tsvalue(rb)->u.lnglen); | ||
| 706 | return; | ||
| 707 | } | ||
| 708 | default: { /* try metamethod */ | ||
| 709 | tm = luaT_gettmbyobj(L, rb, TM_LEN); | ||
| 710 | if (l_unlikely(notm(tm))) /* no metamethod? */ | ||
| 711 | luaG_typeerror(L, rb, "get length of"); | ||
| 712 | break; | ||
| 713 | } | ||
| 714 | } | ||
| 715 | luaT_callTMres(L, tm, rb, rb, ra); | ||
| 716 | } | ||
| 717 | |||
| 718 | |||
| 719 | /* | ||
| 720 | ** Integer division; return 'm // n', that is, floor(m/n). | ||
| 721 | ** C division truncates its result (rounds towards zero). | ||
| 722 | ** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer, | ||
| 723 | ** otherwise 'floor(q) == trunc(q) - 1'. | ||
| 724 | */ | ||
| 725 | lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) { | ||
| 726 | if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ | ||
| 727 | if (n == 0) | ||
| 728 | luaG_runerror(L, "attempt to divide by zero"); | ||
| 729 | return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */ | ||
| 730 | } | ||
| 731 | else { | ||
| 732 | lua_Integer q = m / n; /* perform C division */ | ||
| 733 | if ((m ^ n) < 0 && m % n != 0) /* 'm/n' would be negative non-integer? */ | ||
| 734 | q -= 1; /* correct result for different rounding */ | ||
| 735 | return q; | ||
| 736 | } | ||
| 737 | } | ||
| 738 | |||
| 739 | |||
| 740 | /* | ||
| 741 | ** Integer modulus; return 'm % n'. (Assume that C '%' with | ||
| 742 | ** negative operands follows C99 behavior. See previous comment | ||
| 743 | ** about luaV_idiv.) | ||
| 744 | */ | ||
| 745 | lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) { | ||
| 746 | if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ | ||
| 747 | if (n == 0) | ||
| 748 | luaG_runerror(L, "attempt to perform 'n%%0'"); | ||
| 749 | return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */ | ||
| 750 | } | ||
| 751 | else { | ||
| 752 | lua_Integer r = m % n; | ||
| 753 | if (r != 0 && (r ^ n) < 0) /* 'm/n' would be non-integer negative? */ | ||
| 754 | r += n; /* correct result for different rounding */ | ||
| 755 | return r; | ||
| 756 | } | ||
| 757 | } | ||
| 758 | |||
| 759 | |||
| 760 | /* | ||
| 761 | ** Float modulus | ||
| 762 | */ | ||
| 763 | lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) { | ||
| 764 | lua_Number r; | ||
| 765 | luai_nummod(L, m, n, r); | ||
| 766 | return r; | ||
| 767 | } | ||
| 768 | |||
| 769 | |||
| 770 | /* number of bits in an integer */ | ||
| 771 | #define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT) | ||
| 772 | |||
| 773 | |||
| 774 | /* | ||
| 775 | ** Shift left operation. (Shift right just negates 'y'.) | ||
| 776 | */ | ||
| 777 | lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { | ||
| 778 | if (y < 0) { /* shift right? */ | ||
| 779 | if (y <= -NBITS) return 0; | ||
| 780 | else return intop(>>, x, -y); | ||
| 781 | } | ||
| 782 | else { /* shift left */ | ||
| 783 | if (y >= NBITS) return 0; | ||
| 784 | else return intop(<<, x, y); | ||
| 785 | } | ||
| 786 | } | ||
| 787 | |||
| 788 | |||
| 789 | /* | ||
| 790 | ** create a new Lua closure, push it in the stack, and initialize | ||
| 791 | ** its upvalues. | ||
| 792 | */ | ||
| 793 | static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, | ||
| 794 | StkId ra) { | ||
| 795 | int nup = p->sizeupvalues; | ||
| 796 | Upvaldesc *uv = p->upvalues; | ||
| 797 | int i; | ||
| 798 | LClosure *ncl = luaF_newLclosure(L, nup); | ||
| 799 | ncl->p = p; | ||
| 800 | setclLvalue2s(L, ra, ncl); /* anchor new closure in stack */ | ||
| 801 | for (i = 0; i < nup; i++) { /* fill in its upvalues */ | ||
| 802 | if (uv[i].instack) /* upvalue refers to local variable? */ | ||
| 803 | ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx); | ||
| 804 | else /* get upvalue from enclosing function */ | ||
| 805 | ncl->upvals[i] = encup[uv[i].idx]; | ||
| 806 | luaC_objbarrier(L, ncl, ncl->upvals[i]); | ||
| 807 | } | ||
| 808 | } | ||
| 809 | |||
| 810 | |||
| 811 | /* | ||
| 812 | ** finish execution of an opcode interrupted by a yield | ||
| 813 | */ | ||
| 814 | void luaV_finishOp (lua_State *L) { | ||
| 815 | CallInfo *ci = L->ci; | ||
| 816 | StkId base = ci->func.p + 1; | ||
| 817 | Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ | ||
| 818 | OpCode op = GET_OPCODE(inst); | ||
| 819 | switch (op) { /* finish its execution */ | ||
| 820 | case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: { | ||
| 821 | setobjs2s(L, base + GETARG_A(*(ci->u.l.savedpc - 2)), --L->top.p); | ||
| 822 | break; | ||
| 823 | } | ||
| 824 | case OP_UNM: case OP_BNOT: case OP_LEN: | ||
| 825 | case OP_GETTABUP: case OP_GETTABLE: case OP_GETI: | ||
| 826 | case OP_GETFIELD: case OP_SELF: { | ||
| 827 | setobjs2s(L, base + GETARG_A(inst), --L->top.p); | ||
| 828 | break; | ||
| 829 | } | ||
| 830 | case OP_LT: case OP_LE: | ||
| 831 | case OP_LTI: case OP_LEI: | ||
| 832 | case OP_GTI: case OP_GEI: | ||
| 833 | case OP_EQ: { /* note that 'OP_EQI'/'OP_EQK' cannot yield */ | ||
| 834 | int res = !l_isfalse(s2v(L->top.p - 1)); | ||
| 835 | L->top.p--; | ||
| 836 | #if defined(LUA_COMPAT_LT_LE) | ||
| 837 | if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ | ||
| 838 | ci->callstatus ^= CIST_LEQ; /* clear mark */ | ||
| 839 | res = !res; /* negate result */ | ||
| 840 | } | ||
| 841 | #endif | ||
| 842 | lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); | ||
| 843 | if (res != GETARG_k(inst)) /* condition failed? */ | ||
| 844 | ci->u.l.savedpc++; /* skip jump instruction */ | ||
| 845 | break; | ||
| 846 | } | ||
| 847 | case OP_CONCAT: { | ||
| 848 | StkId top = L->top.p - 1; /* top when 'luaT_tryconcatTM' was called */ | ||
| 849 | int a = GETARG_A(inst); /* first element to concatenate */ | ||
| 850 | int total = cast_int(top - 1 - (base + a)); /* yet to concatenate */ | ||
| 851 | setobjs2s(L, top - 2, top); /* put TM result in proper position */ | ||
| 852 | L->top.p = top - 1; /* top is one after last element (at top-2) */ | ||
| 853 | luaV_concat(L, total); /* concat them (may yield again) */ | ||
| 854 | break; | ||
| 855 | } | ||
| 856 | case OP_CLOSE: { /* yielded closing variables */ | ||
| 857 | ci->u.l.savedpc--; /* repeat instruction to close other vars. */ | ||
| 858 | break; | ||
| 859 | } | ||
| 860 | case OP_RETURN: { /* yielded closing variables */ | ||
| 861 | StkId ra = base + GETARG_A(inst); | ||
| 862 | /* adjust top to signal correct number of returns, in case the | ||
| 863 | return is "up to top" ('isIT') */ | ||
| 864 | L->top.p = ra + ci->u2.nres; | ||
| 865 | /* repeat instruction to close other vars. and complete the return */ | ||
| 866 | ci->u.l.savedpc--; | ||
| 867 | break; | ||
| 868 | } | ||
| 869 | default: { | ||
| 870 | /* only these other opcodes can yield */ | ||
| 871 | lua_assert(op == OP_TFORCALL || op == OP_CALL || | ||
| 872 | op == OP_TAILCALL || op == OP_SETTABUP || op == OP_SETTABLE || | ||
| 873 | op == OP_SETI || op == OP_SETFIELD); | ||
| 874 | break; | ||
| 875 | } | ||
| 876 | } | ||
| 877 | } | ||
| 878 | |||
| 879 | |||
| 880 | |||
| 881 | |||
| 882 | /* | ||
| 883 | ** {================================================================== | ||
| 884 | ** Macros for arithmetic/bitwise/comparison opcodes in 'luaV_execute' | ||
| 885 | ** =================================================================== | ||
| 886 | */ | ||
| 887 | |||
| 888 | #define l_addi(L,a,b) intop(+, a, b) | ||
| 889 | #define l_subi(L,a,b) intop(-, a, b) | ||
| 890 | #define l_muli(L,a,b) intop(*, a, b) | ||
| 891 | #define l_band(a,b) intop(&, a, b) | ||
| 892 | #define l_bor(a,b) intop(|, a, b) | ||
| 893 | #define l_bxor(a,b) intop(^, a, b) | ||
| 894 | |||
| 895 | #define l_lti(a,b) (a < b) | ||
| 896 | #define l_lei(a,b) (a <= b) | ||
| 897 | #define l_gti(a,b) (a > b) | ||
| 898 | #define l_gei(a,b) (a >= b) | ||
| 899 | |||
| 900 | |||
| 901 | /* | ||
| 902 | ** Arithmetic operations with immediate operands. 'iop' is the integer | ||
| 903 | ** operation, 'fop' is the float operation. | ||
| 904 | */ | ||
| 905 | #define op_arithI(L,iop,fop) { \ | ||
| 906 | StkId ra = RA(i); \ | ||
| 907 | TValue *v1 = vRB(i); \ | ||
| 908 | int imm = GETARG_sC(i); \ | ||
| 909 | if (ttisinteger(v1)) { \ | ||
| 910 | lua_Integer iv1 = ivalue(v1); \ | ||
| 911 | pc++; setivalue(s2v(ra), iop(L, iv1, imm)); \ | ||
| 912 | } \ | ||
| 913 | else if (ttisfloat(v1)) { \ | ||
| 914 | lua_Number nb = fltvalue(v1); \ | ||
| 915 | lua_Number fimm = cast_num(imm); \ | ||
| 916 | pc++; setfltvalue(s2v(ra), fop(L, nb, fimm)); \ | ||
| 917 | }} | ||
| 918 | |||
| 919 | |||
| 920 | /* | ||
| 921 | ** Auxiliary function for arithmetic operations over floats and others | ||
| 922 | ** with two register operands. | ||
| 923 | */ | ||
| 924 | #define op_arithf_aux(L,v1,v2,fop) { \ | ||
| 925 | lua_Number n1; lua_Number n2; \ | ||
| 926 | if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ | ||
| 927 | pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \ | ||
| 928 | }} | ||
| 929 | |||
| 930 | |||
| 931 | /* | ||
| 932 | ** Arithmetic operations over floats and others with register operands. | ||
| 933 | */ | ||
| 934 | #define op_arithf(L,fop) { \ | ||
| 935 | StkId ra = RA(i); \ | ||
| 936 | TValue *v1 = vRB(i); \ | ||
| 937 | TValue *v2 = vRC(i); \ | ||
| 938 | op_arithf_aux(L, v1, v2, fop); } | ||
| 939 | |||
| 940 | |||
| 941 | /* | ||
| 942 | ** Arithmetic operations with K operands for floats. | ||
| 943 | */ | ||
| 944 | #define op_arithfK(L,fop) { \ | ||
| 945 | StkId ra = RA(i); \ | ||
| 946 | TValue *v1 = vRB(i); \ | ||
| 947 | TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \ | ||
| 948 | op_arithf_aux(L, v1, v2, fop); } | ||
| 949 | |||
| 950 | |||
| 951 | /* | ||
| 952 | ** Arithmetic operations over integers and floats. | ||
| 953 | */ | ||
| 954 | #define op_arith_aux(L,v1,v2,iop,fop) { \ | ||
| 955 | StkId ra = RA(i); \ | ||
| 956 | if (ttisinteger(v1) && ttisinteger(v2)) { \ | ||
| 957 | lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ | ||
| 958 | pc++; setivalue(s2v(ra), iop(L, i1, i2)); \ | ||
| 959 | } \ | ||
| 960 | else op_arithf_aux(L, v1, v2, fop); } | ||
| 961 | |||
| 962 | |||
| 963 | /* | ||
| 964 | ** Arithmetic operations with register operands. | ||
| 965 | */ | ||
| 966 | #define op_arith(L,iop,fop) { \ | ||
| 967 | TValue *v1 = vRB(i); \ | ||
| 968 | TValue *v2 = vRC(i); \ | ||
| 969 | op_arith_aux(L, v1, v2, iop, fop); } | ||
| 970 | |||
| 971 | |||
| 972 | /* | ||
| 973 | ** Arithmetic operations with K operands. | ||
| 974 | */ | ||
| 975 | #define op_arithK(L,iop,fop) { \ | ||
| 976 | TValue *v1 = vRB(i); \ | ||
| 977 | TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \ | ||
| 978 | op_arith_aux(L, v1, v2, iop, fop); } | ||
| 979 | |||
| 980 | |||
| 981 | /* | ||
| 982 | ** Bitwise operations with constant operand. | ||
| 983 | */ | ||
| 984 | #define op_bitwiseK(L,op) { \ | ||
| 985 | StkId ra = RA(i); \ | ||
| 986 | TValue *v1 = vRB(i); \ | ||
| 987 | TValue *v2 = KC(i); \ | ||
| 988 | lua_Integer i1; \ | ||
| 989 | lua_Integer i2 = ivalue(v2); \ | ||
| 990 | if (tointegerns(v1, &i1)) { \ | ||
| 991 | pc++; setivalue(s2v(ra), op(i1, i2)); \ | ||
| 992 | }} | ||
| 993 | |||
| 994 | |||
| 995 | /* | ||
| 996 | ** Bitwise operations with register operands. | ||
| 997 | */ | ||
| 998 | #define op_bitwise(L,op) { \ | ||
| 999 | StkId ra = RA(i); \ | ||
| 1000 | TValue *v1 = vRB(i); \ | ||
| 1001 | TValue *v2 = vRC(i); \ | ||
| 1002 | lua_Integer i1; lua_Integer i2; \ | ||
| 1003 | if (tointegerns(v1, &i1) && tointegerns(v2, &i2)) { \ | ||
| 1004 | pc++; setivalue(s2v(ra), op(i1, i2)); \ | ||
| 1005 | }} | ||
| 1006 | |||
| 1007 | |||
| 1008 | /* | ||
| 1009 | ** Order operations with register operands. 'opn' actually works | ||
| 1010 | ** for all numbers, but the fast track improves performance for | ||
| 1011 | ** integers. | ||
| 1012 | */ | ||
| 1013 | #define op_order(L,opi,opn,other) { \ | ||
| 1014 | StkId ra = RA(i); \ | ||
| 1015 | int cond; \ | ||
| 1016 | TValue *rb = vRB(i); \ | ||
| 1017 | if (ttisinteger(s2v(ra)) && ttisinteger(rb)) { \ | ||
| 1018 | lua_Integer ia = ivalue(s2v(ra)); \ | ||
| 1019 | lua_Integer ib = ivalue(rb); \ | ||
| 1020 | cond = opi(ia, ib); \ | ||
| 1021 | } \ | ||
| 1022 | else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \ | ||
| 1023 | cond = opn(s2v(ra), rb); \ | ||
| 1024 | else \ | ||
| 1025 | Protect(cond = other(L, s2v(ra), rb)); \ | ||
| 1026 | docondjump(); } | ||
| 1027 | |||
| 1028 | |||
| 1029 | /* | ||
| 1030 | ** Order operations with immediate operand. (Immediate operand is | ||
| 1031 | ** always small enough to have an exact representation as a float.) | ||
| 1032 | */ | ||
| 1033 | #define op_orderI(L,opi,opf,inv,tm) { \ | ||
| 1034 | StkId ra = RA(i); \ | ||
| 1035 | int cond; \ | ||
| 1036 | int im = GETARG_sB(i); \ | ||
| 1037 | if (ttisinteger(s2v(ra))) \ | ||
| 1038 | cond = opi(ivalue(s2v(ra)), im); \ | ||
| 1039 | else if (ttisfloat(s2v(ra))) { \ | ||
| 1040 | lua_Number fa = fltvalue(s2v(ra)); \ | ||
| 1041 | lua_Number fim = cast_num(im); \ | ||
| 1042 | cond = opf(fa, fim); \ | ||
| 1043 | } \ | ||
| 1044 | else { \ | ||
| 1045 | int isf = GETARG_C(i); \ | ||
| 1046 | Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \ | ||
| 1047 | } \ | ||
| 1048 | docondjump(); } | ||
| 1049 | |||
| 1050 | /* }================================================================== */ | ||
| 1051 | |||
| 1052 | |||
| 1053 | /* | ||
| 1054 | ** {================================================================== | ||
| 1055 | ** Function 'luaV_execute': main interpreter loop | ||
| 1056 | ** =================================================================== | ||
| 1057 | */ | ||
| 1058 | |||
| 1059 | /* | ||
| 1060 | ** some macros for common tasks in 'luaV_execute' | ||
| 1061 | */ | ||
| 1062 | |||
| 1063 | |||
| 1064 | #define RA(i) (base+GETARG_A(i)) | ||
| 1065 | #define RB(i) (base+GETARG_B(i)) | ||
| 1066 | #define vRB(i) s2v(RB(i)) | ||
| 1067 | #define KB(i) (k+GETARG_B(i)) | ||
| 1068 | #define RC(i) (base+GETARG_C(i)) | ||
| 1069 | #define vRC(i) s2v(RC(i)) | ||
| 1070 | #define KC(i) (k+GETARG_C(i)) | ||
| 1071 | #define RKC(i) ((TESTARG_k(i)) ? k + GETARG_C(i) : s2v(base + GETARG_C(i))) | ||
| 1072 | |||
| 1073 | |||
| 1074 | |||
| 1075 | #define updatetrap(ci) (trap = ci->u.l.trap) | ||
| 1076 | |||
| 1077 | #define updatebase(ci) (base = ci->func.p + 1) | ||
| 1078 | |||
| 1079 | |||
| 1080 | #define updatestack(ci) \ | ||
| 1081 | { if (l_unlikely(trap)) { updatebase(ci); ra = RA(i); } } | ||
| 1082 | |||
| 1083 | |||
| 1084 | /* | ||
| 1085 | ** Execute a jump instruction. The 'updatetrap' allows signals to stop | ||
| 1086 | ** tight loops. (Without it, the local copy of 'trap' could never change.) | ||
| 1087 | */ | ||
| 1088 | #define dojump(ci,i,e) { pc += GETARG_sJ(i) + e; updatetrap(ci); } | ||
| 1089 | |||
| 1090 | |||
| 1091 | /* for test instructions, execute the jump instruction that follows it */ | ||
| 1092 | #define donextjump(ci) { Instruction ni = *pc; dojump(ci, ni, 1); } | ||
| 1093 | |||
| 1094 | /* | ||
| 1095 | ** do a conditional jump: skip next instruction if 'cond' is not what | ||
| 1096 | ** was expected (parameter 'k'), else do next instruction, which must | ||
| 1097 | ** be a jump. | ||
| 1098 | */ | ||
| 1099 | #define docondjump() if (cond != GETARG_k(i)) pc++; else donextjump(ci); | ||
| 1100 | |||
| 1101 | |||
| 1102 | /* | ||
| 1103 | ** Correct global 'pc'. | ||
| 1104 | */ | ||
| 1105 | #define savepc(L) (ci->u.l.savedpc = pc) | ||
| 1106 | |||
| 1107 | |||
| 1108 | /* | ||
| 1109 | ** Whenever code can raise errors, the global 'pc' and the global | ||
| 1110 | ** 'top' must be correct to report occasional errors. | ||
| 1111 | */ | ||
| 1112 | #define savestate(L,ci) (savepc(L), L->top.p = ci->top.p) | ||
| 1113 | |||
| 1114 | |||
| 1115 | /* | ||
| 1116 | ** Protect code that, in general, can raise errors, reallocate the | ||
| 1117 | ** stack, and change the hooks. | ||
| 1118 | */ | ||
| 1119 | #define Protect(exp) (savestate(L,ci), (exp), updatetrap(ci)) | ||
| 1120 | |||
| 1121 | /* special version that does not change the top */ | ||
| 1122 | #define ProtectNT(exp) (savepc(L), (exp), updatetrap(ci)) | ||
| 1123 | |||
| 1124 | /* | ||
| 1125 | ** Protect code that can only raise errors. (That is, it cannot change | ||
| 1126 | ** the stack or hooks.) | ||
| 1127 | */ | ||
| 1128 | #define halfProtect(exp) (savestate(L,ci), (exp)) | ||
| 1129 | |||
| 1130 | /* 'c' is the limit of live values in the stack */ | ||
| 1131 | #define checkGC(L,c) \ | ||
| 1132 | { luaC_condGC(L, (savepc(L), L->top.p = (c)), \ | ||
| 1133 | updatetrap(ci)); \ | ||
| 1134 | luai_threadyield(L); } | ||
| 1135 | |||
| 1136 | |||
| 1137 | /* fetch an instruction and prepare its execution */ | ||
| 1138 | #define vmfetch() { \ | ||
| 1139 | if (l_unlikely(trap)) { /* stack reallocation or hooks? */ \ | ||
| 1140 | trap = luaG_traceexec(L, pc); /* handle hooks */ \ | ||
| 1141 | updatebase(ci); /* correct stack */ \ | ||
| 1142 | } \ | ||
| 1143 | i = *(pc++); \ | ||
| 1144 | } | ||
| 1145 | |||
| 1146 | #define vmdispatch(o) switch(o) | ||
| 1147 | #define vmcase(l) case l: | ||
| 1148 | #define vmbreak break | ||
| 1149 | |||
| 1150 | |||
| 1151 | void luaV_execute (lua_State *L, CallInfo *ci) { | ||
| 1152 | LClosure *cl; | ||
| 1153 | TValue *k; | ||
| 1154 | StkId base; | ||
| 1155 | const Instruction *pc; | ||
| 1156 | int trap; | ||
| 1157 | #if LUA_USE_JUMPTABLE | ||
| 1158 | #include "ljumptab.h" | ||
| 1159 | #endif | ||
| 1160 | startfunc: | ||
| 1161 | trap = L->hookmask; | ||
| 1162 | returning: /* trap already set */ | ||
| 1163 | cl = ci_func(ci); | ||
| 1164 | k = cl->p->k; | ||
| 1165 | pc = ci->u.l.savedpc; | ||
| 1166 | if (l_unlikely(trap)) | ||
| 1167 | trap = luaG_tracecall(L); | ||
| 1168 | base = ci->func.p + 1; | ||
| 1169 | /* main loop of interpreter */ | ||
| 1170 | for (;;) { | ||
| 1171 | Instruction i; /* instruction being executed */ | ||
| 1172 | vmfetch(); | ||
| 1173 | #if 0 | ||
| 1174 | /* low-level line tracing for debugging Lua */ | ||
| 1175 | printf("line: %d\n", luaG_getfuncline(cl->p, pcRel(pc, cl->p))); | ||
| 1176 | #endif | ||
| 1177 | lua_assert(base == ci->func.p + 1); | ||
| 1178 | lua_assert(base <= L->top.p && L->top.p <= L->stack_last.p); | ||
| 1179 | /* invalidate top for instructions not expecting it */ | ||
| 1180 | lua_assert(isIT(i) || (cast_void(L->top.p = base), 1)); | ||
| 1181 | vmdispatch (GET_OPCODE(i)) { | ||
| 1182 | vmcase(OP_MOVE) { | ||
| 1183 | StkId ra = RA(i); | ||
| 1184 | setobjs2s(L, ra, RB(i)); | ||
| 1185 | vmbreak; | ||
| 1186 | } | ||
| 1187 | vmcase(OP_LOADI) { | ||
| 1188 | StkId ra = RA(i); | ||
| 1189 | lua_Integer b = GETARG_sBx(i); | ||
| 1190 | setivalue(s2v(ra), b); | ||
| 1191 | vmbreak; | ||
| 1192 | } | ||
| 1193 | vmcase(OP_LOADF) { | ||
| 1194 | StkId ra = RA(i); | ||
| 1195 | int b = GETARG_sBx(i); | ||
| 1196 | setfltvalue(s2v(ra), cast_num(b)); | ||
| 1197 | vmbreak; | ||
| 1198 | } | ||
| 1199 | vmcase(OP_LOADK) { | ||
| 1200 | StkId ra = RA(i); | ||
| 1201 | TValue *rb = k + GETARG_Bx(i); | ||
| 1202 | setobj2s(L, ra, rb); | ||
| 1203 | vmbreak; | ||
| 1204 | } | ||
| 1205 | vmcase(OP_LOADKX) { | ||
| 1206 | StkId ra = RA(i); | ||
| 1207 | TValue *rb; | ||
| 1208 | rb = k + GETARG_Ax(*pc); pc++; | ||
| 1209 | setobj2s(L, ra, rb); | ||
| 1210 | vmbreak; | ||
| 1211 | } | ||
| 1212 | vmcase(OP_LOADFALSE) { | ||
| 1213 | StkId ra = RA(i); | ||
| 1214 | setbfvalue(s2v(ra)); | ||
| 1215 | vmbreak; | ||
| 1216 | } | ||
| 1217 | vmcase(OP_LFALSESKIP) { | ||
| 1218 | StkId ra = RA(i); | ||
| 1219 | setbfvalue(s2v(ra)); | ||
| 1220 | pc++; /* skip next instruction */ | ||
| 1221 | vmbreak; | ||
| 1222 | } | ||
| 1223 | vmcase(OP_LOADTRUE) { | ||
| 1224 | StkId ra = RA(i); | ||
| 1225 | setbtvalue(s2v(ra)); | ||
| 1226 | vmbreak; | ||
| 1227 | } | ||
| 1228 | vmcase(OP_LOADNIL) { | ||
| 1229 | StkId ra = RA(i); | ||
| 1230 | int b = GETARG_B(i); | ||
| 1231 | do { | ||
| 1232 | setnilvalue(s2v(ra++)); | ||
| 1233 | } while (b--); | ||
| 1234 | vmbreak; | ||
| 1235 | } | ||
| 1236 | vmcase(OP_GETUPVAL) { | ||
| 1237 | StkId ra = RA(i); | ||
| 1238 | int b = GETARG_B(i); | ||
| 1239 | setobj2s(L, ra, cl->upvals[b]->v.p); | ||
| 1240 | vmbreak; | ||
| 1241 | } | ||
| 1242 | vmcase(OP_SETUPVAL) { | ||
| 1243 | StkId ra = RA(i); | ||
| 1244 | UpVal *uv = cl->upvals[GETARG_B(i)]; | ||
| 1245 | setobj(L, uv->v.p, s2v(ra)); | ||
| 1246 | luaC_barrier(L, uv, s2v(ra)); | ||
| 1247 | vmbreak; | ||
| 1248 | } | ||
| 1249 | vmcase(OP_GETTABUP) { | ||
| 1250 | StkId ra = RA(i); | ||
| 1251 | const TValue *slot; | ||
| 1252 | TValue *upval = cl->upvals[GETARG_B(i)]->v.p; | ||
| 1253 | TValue *rc = KC(i); | ||
| 1254 | TString *key = tsvalue(rc); /* key must be a short string */ | ||
| 1255 | if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) { | ||
| 1256 | setobj2s(L, ra, slot); | ||
| 1257 | } | ||
| 1258 | else | ||
| 1259 | Protect(luaV_finishget(L, upval, rc, ra, slot)); | ||
| 1260 | vmbreak; | ||
| 1261 | } | ||
| 1262 | vmcase(OP_GETTABLE) { | ||
| 1263 | StkId ra = RA(i); | ||
| 1264 | const TValue *slot; | ||
| 1265 | TValue *rb = vRB(i); | ||
| 1266 | TValue *rc = vRC(i); | ||
| 1267 | lua_Unsigned n; | ||
| 1268 | if (ttisinteger(rc) /* fast track for integers? */ | ||
| 1269 | ? (cast_void(n = ivalue(rc)), luaV_fastgeti(L, rb, n, slot)) | ||
| 1270 | : luaV_fastget(L, rb, rc, slot, luaH_get)) { | ||
| 1271 | setobj2s(L, ra, slot); | ||
| 1272 | } | ||
| 1273 | else | ||
| 1274 | Protect(luaV_finishget(L, rb, rc, ra, slot)); | ||
| 1275 | vmbreak; | ||
| 1276 | } | ||
| 1277 | vmcase(OP_GETI) { | ||
| 1278 | StkId ra = RA(i); | ||
| 1279 | const TValue *slot; | ||
| 1280 | TValue *rb = vRB(i); | ||
| 1281 | int c = GETARG_C(i); | ||
| 1282 | if (luaV_fastgeti(L, rb, c, slot)) { | ||
| 1283 | setobj2s(L, ra, slot); | ||
| 1284 | } | ||
| 1285 | else { | ||
| 1286 | TValue key; | ||
| 1287 | setivalue(&key, c); | ||
| 1288 | Protect(luaV_finishget(L, rb, &key, ra, slot)); | ||
| 1289 | } | ||
| 1290 | vmbreak; | ||
| 1291 | } | ||
| 1292 | vmcase(OP_GETFIELD) { | ||
| 1293 | StkId ra = RA(i); | ||
| 1294 | const TValue *slot; | ||
| 1295 | TValue *rb = vRB(i); | ||
| 1296 | TValue *rc = KC(i); | ||
| 1297 | TString *key = tsvalue(rc); /* key must be a short string */ | ||
| 1298 | if (luaV_fastget(L, rb, key, slot, luaH_getshortstr)) { | ||
| 1299 | setobj2s(L, ra, slot); | ||
| 1300 | } | ||
| 1301 | else | ||
| 1302 | Protect(luaV_finishget(L, rb, rc, ra, slot)); | ||
| 1303 | vmbreak; | ||
| 1304 | } | ||
| 1305 | vmcase(OP_SETTABUP) { | ||
| 1306 | const TValue *slot; | ||
| 1307 | TValue *upval = cl->upvals[GETARG_A(i)]->v.p; | ||
| 1308 | TValue *rb = KB(i); | ||
| 1309 | TValue *rc = RKC(i); | ||
| 1310 | TString *key = tsvalue(rb); /* key must be a short string */ | ||
| 1311 | if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) { | ||
| 1312 | luaV_finishfastset(L, upval, slot, rc); | ||
| 1313 | } | ||
| 1314 | else | ||
| 1315 | Protect(luaV_finishset(L, upval, rb, rc, slot)); | ||
| 1316 | vmbreak; | ||
| 1317 | } | ||
| 1318 | vmcase(OP_SETTABLE) { | ||
| 1319 | StkId ra = RA(i); | ||
| 1320 | const TValue *slot; | ||
| 1321 | TValue *rb = vRB(i); /* key (table is in 'ra') */ | ||
| 1322 | TValue *rc = RKC(i); /* value */ | ||
| 1323 | lua_Unsigned n; | ||
| 1324 | if (ttisinteger(rb) /* fast track for integers? */ | ||
| 1325 | ? (cast_void(n = ivalue(rb)), luaV_fastgeti(L, s2v(ra), n, slot)) | ||
| 1326 | : luaV_fastget(L, s2v(ra), rb, slot, luaH_get)) { | ||
| 1327 | luaV_finishfastset(L, s2v(ra), slot, rc); | ||
| 1328 | } | ||
| 1329 | else | ||
| 1330 | Protect(luaV_finishset(L, s2v(ra), rb, rc, slot)); | ||
| 1331 | vmbreak; | ||
| 1332 | } | ||
| 1333 | vmcase(OP_SETI) { | ||
| 1334 | StkId ra = RA(i); | ||
| 1335 | const TValue *slot; | ||
| 1336 | int c = GETARG_B(i); | ||
| 1337 | TValue *rc = RKC(i); | ||
| 1338 | if (luaV_fastgeti(L, s2v(ra), c, slot)) { | ||
| 1339 | luaV_finishfastset(L, s2v(ra), slot, rc); | ||
| 1340 | } | ||
| 1341 | else { | ||
| 1342 | TValue key; | ||
| 1343 | setivalue(&key, c); | ||
| 1344 | Protect(luaV_finishset(L, s2v(ra), &key, rc, slot)); | ||
| 1345 | } | ||
| 1346 | vmbreak; | ||
| 1347 | } | ||
| 1348 | vmcase(OP_SETFIELD) { | ||
| 1349 | StkId ra = RA(i); | ||
| 1350 | const TValue *slot; | ||
| 1351 | TValue *rb = KB(i); | ||
| 1352 | TValue *rc = RKC(i); | ||
| 1353 | TString *key = tsvalue(rb); /* key must be a short string */ | ||
| 1354 | if (luaV_fastget(L, s2v(ra), key, slot, luaH_getshortstr)) { | ||
| 1355 | luaV_finishfastset(L, s2v(ra), slot, rc); | ||
| 1356 | } | ||
| 1357 | else | ||
| 1358 | Protect(luaV_finishset(L, s2v(ra), rb, rc, slot)); | ||
| 1359 | vmbreak; | ||
| 1360 | } | ||
| 1361 | vmcase(OP_NEWTABLE) { | ||
| 1362 | StkId ra = RA(i); | ||
| 1363 | int b = GETARG_B(i); /* log2(hash size) + 1 */ | ||
| 1364 | int c = GETARG_C(i); /* array size */ | ||
| 1365 | Table *t; | ||
| 1366 | if (b > 0) | ||
| 1367 | b = 1 << (b - 1); /* size is 2^(b - 1) */ | ||
| 1368 | lua_assert((!TESTARG_k(i)) == (GETARG_Ax(*pc) == 0)); | ||
| 1369 | if (TESTARG_k(i)) /* non-zero extra argument? */ | ||
| 1370 | c += GETARG_Ax(*pc) * (MAXARG_C + 1); /* add it to size */ | ||
| 1371 | pc++; /* skip extra argument */ | ||
| 1372 | L->top.p = ra + 1; /* correct top in case of emergency GC */ | ||
| 1373 | t = luaH_new(L); /* memory allocation */ | ||
| 1374 | sethvalue2s(L, ra, t); | ||
| 1375 | if (b != 0 || c != 0) | ||
| 1376 | luaH_resize(L, t, c, b); /* idem */ | ||
| 1377 | checkGC(L, ra + 1); | ||
| 1378 | vmbreak; | ||
| 1379 | } | ||
| 1380 | vmcase(OP_SELF) { | ||
| 1381 | StkId ra = RA(i); | ||
| 1382 | const TValue *slot; | ||
| 1383 | TValue *rb = vRB(i); | ||
| 1384 | TValue *rc = RKC(i); | ||
| 1385 | TString *key = tsvalue(rc); /* key must be a string */ | ||
| 1386 | setobj2s(L, ra + 1, rb); | ||
| 1387 | if (luaV_fastget(L, rb, key, slot, luaH_getstr)) { | ||
| 1388 | setobj2s(L, ra, slot); | ||
| 1389 | } | ||
| 1390 | else | ||
| 1391 | Protect(luaV_finishget(L, rb, rc, ra, slot)); | ||
| 1392 | vmbreak; | ||
| 1393 | } | ||
| 1394 | vmcase(OP_ADDI) { | ||
| 1395 | op_arithI(L, l_addi, luai_numadd); | ||
| 1396 | vmbreak; | ||
| 1397 | } | ||
| 1398 | vmcase(OP_ADDK) { | ||
| 1399 | op_arithK(L, l_addi, luai_numadd); | ||
| 1400 | vmbreak; | ||
| 1401 | } | ||
| 1402 | vmcase(OP_SUBK) { | ||
| 1403 | op_arithK(L, l_subi, luai_numsub); | ||
| 1404 | vmbreak; | ||
| 1405 | } | ||
| 1406 | vmcase(OP_MULK) { | ||
| 1407 | op_arithK(L, l_muli, luai_nummul); | ||
| 1408 | vmbreak; | ||
| 1409 | } | ||
| 1410 | vmcase(OP_MODK) { | ||
| 1411 | savestate(L, ci); /* in case of division by 0 */ | ||
| 1412 | op_arithK(L, luaV_mod, luaV_modf); | ||
| 1413 | vmbreak; | ||
| 1414 | } | ||
| 1415 | vmcase(OP_POWK) { | ||
| 1416 | op_arithfK(L, luai_numpow); | ||
| 1417 | vmbreak; | ||
| 1418 | } | ||
| 1419 | vmcase(OP_DIVK) { | ||
| 1420 | op_arithfK(L, luai_numdiv); | ||
| 1421 | vmbreak; | ||
| 1422 | } | ||
| 1423 | vmcase(OP_IDIVK) { | ||
| 1424 | savestate(L, ci); /* in case of division by 0 */ | ||
| 1425 | op_arithK(L, luaV_idiv, luai_numidiv); | ||
| 1426 | vmbreak; | ||
| 1427 | } | ||
| 1428 | vmcase(OP_BANDK) { | ||
| 1429 | op_bitwiseK(L, l_band); | ||
| 1430 | vmbreak; | ||
| 1431 | } | ||
| 1432 | vmcase(OP_BORK) { | ||
| 1433 | op_bitwiseK(L, l_bor); | ||
| 1434 | vmbreak; | ||
| 1435 | } | ||
| 1436 | vmcase(OP_BXORK) { | ||
| 1437 | op_bitwiseK(L, l_bxor); | ||
| 1438 | vmbreak; | ||
| 1439 | } | ||
| 1440 | vmcase(OP_SHRI) { | ||
| 1441 | StkId ra = RA(i); | ||
| 1442 | TValue *rb = vRB(i); | ||
| 1443 | int ic = GETARG_sC(i); | ||
| 1444 | lua_Integer ib; | ||
| 1445 | if (tointegerns(rb, &ib)) { | ||
| 1446 | pc++; setivalue(s2v(ra), luaV_shiftl(ib, -ic)); | ||
| 1447 | } | ||
| 1448 | vmbreak; | ||
| 1449 | } | ||
| 1450 | vmcase(OP_SHLI) { | ||
| 1451 | StkId ra = RA(i); | ||
| 1452 | TValue *rb = vRB(i); | ||
| 1453 | int ic = GETARG_sC(i); | ||
| 1454 | lua_Integer ib; | ||
| 1455 | if (tointegerns(rb, &ib)) { | ||
| 1456 | pc++; setivalue(s2v(ra), luaV_shiftl(ic, ib)); | ||
| 1457 | } | ||
| 1458 | vmbreak; | ||
| 1459 | } | ||
| 1460 | vmcase(OP_ADD) { | ||
| 1461 | op_arith(L, l_addi, luai_numadd); | ||
| 1462 | vmbreak; | ||
| 1463 | } | ||
| 1464 | vmcase(OP_SUB) { | ||
| 1465 | op_arith(L, l_subi, luai_numsub); | ||
| 1466 | vmbreak; | ||
| 1467 | } | ||
| 1468 | vmcase(OP_MUL) { | ||
| 1469 | op_arith(L, l_muli, luai_nummul); | ||
| 1470 | vmbreak; | ||
| 1471 | } | ||
| 1472 | vmcase(OP_MOD) { | ||
| 1473 | savestate(L, ci); /* in case of division by 0 */ | ||
| 1474 | op_arith(L, luaV_mod, luaV_modf); | ||
| 1475 | vmbreak; | ||
| 1476 | } | ||
| 1477 | vmcase(OP_POW) { | ||
| 1478 | op_arithf(L, luai_numpow); | ||
| 1479 | vmbreak; | ||
| 1480 | } | ||
| 1481 | vmcase(OP_DIV) { /* float division (always with floats) */ | ||
| 1482 | op_arithf(L, luai_numdiv); | ||
| 1483 | vmbreak; | ||
| 1484 | } | ||
| 1485 | vmcase(OP_IDIV) { /* floor division */ | ||
| 1486 | savestate(L, ci); /* in case of division by 0 */ | ||
| 1487 | op_arith(L, luaV_idiv, luai_numidiv); | ||
| 1488 | vmbreak; | ||
| 1489 | } | ||
| 1490 | vmcase(OP_BAND) { | ||
| 1491 | op_bitwise(L, l_band); | ||
| 1492 | vmbreak; | ||
| 1493 | } | ||
| 1494 | vmcase(OP_BOR) { | ||
| 1495 | op_bitwise(L, l_bor); | ||
| 1496 | vmbreak; | ||
| 1497 | } | ||
| 1498 | vmcase(OP_BXOR) { | ||
| 1499 | op_bitwise(L, l_bxor); | ||
| 1500 | vmbreak; | ||
| 1501 | } | ||
| 1502 | vmcase(OP_SHR) { | ||
| 1503 | op_bitwise(L, luaV_shiftr); | ||
| 1504 | vmbreak; | ||
| 1505 | } | ||
| 1506 | vmcase(OP_SHL) { | ||
| 1507 | op_bitwise(L, luaV_shiftl); | ||
| 1508 | vmbreak; | ||
| 1509 | } | ||
| 1510 | vmcase(OP_MMBIN) { | ||
| 1511 | StkId ra = RA(i); | ||
| 1512 | Instruction pi = *(pc - 2); /* original arith. expression */ | ||
| 1513 | TValue *rb = vRB(i); | ||
| 1514 | TMS tm = (TMS)GETARG_C(i); | ||
| 1515 | StkId result = RA(pi); | ||
| 1516 | lua_assert(OP_ADD <= GET_OPCODE(pi) && GET_OPCODE(pi) <= OP_SHR); | ||
| 1517 | Protect(luaT_trybinTM(L, s2v(ra), rb, result, tm)); | ||
| 1518 | vmbreak; | ||
| 1519 | } | ||
| 1520 | vmcase(OP_MMBINI) { | ||
| 1521 | StkId ra = RA(i); | ||
| 1522 | Instruction pi = *(pc - 2); /* original arith. expression */ | ||
| 1523 | int imm = GETARG_sB(i); | ||
| 1524 | TMS tm = (TMS)GETARG_C(i); | ||
| 1525 | int flip = GETARG_k(i); | ||
| 1526 | StkId result = RA(pi); | ||
| 1527 | Protect(luaT_trybiniTM(L, s2v(ra), imm, flip, result, tm)); | ||
| 1528 | vmbreak; | ||
| 1529 | } | ||
| 1530 | vmcase(OP_MMBINK) { | ||
| 1531 | StkId ra = RA(i); | ||
| 1532 | Instruction pi = *(pc - 2); /* original arith. expression */ | ||
| 1533 | TValue *imm = KB(i); | ||
| 1534 | TMS tm = (TMS)GETARG_C(i); | ||
| 1535 | int flip = GETARG_k(i); | ||
| 1536 | StkId result = RA(pi); | ||
| 1537 | Protect(luaT_trybinassocTM(L, s2v(ra), imm, flip, result, tm)); | ||
| 1538 | vmbreak; | ||
| 1539 | } | ||
| 1540 | vmcase(OP_UNM) { | ||
| 1541 | StkId ra = RA(i); | ||
| 1542 | TValue *rb = vRB(i); | ||
| 1543 | lua_Number nb; | ||
| 1544 | if (ttisinteger(rb)) { | ||
| 1545 | lua_Integer ib = ivalue(rb); | ||
| 1546 | setivalue(s2v(ra), intop(-, 0, ib)); | ||
| 1547 | } | ||
| 1548 | else if (tonumberns(rb, nb)) { | ||
| 1549 | setfltvalue(s2v(ra), luai_numunm(L, nb)); | ||
| 1550 | } | ||
| 1551 | else | ||
| 1552 | Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM)); | ||
| 1553 | vmbreak; | ||
| 1554 | } | ||
| 1555 | vmcase(OP_BNOT) { | ||
| 1556 | StkId ra = RA(i); | ||
| 1557 | TValue *rb = vRB(i); | ||
| 1558 | lua_Integer ib; | ||
| 1559 | if (tointegerns(rb, &ib)) { | ||
| 1560 | setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib)); | ||
| 1561 | } | ||
| 1562 | else | ||
| 1563 | Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); | ||
| 1564 | vmbreak; | ||
| 1565 | } | ||
| 1566 | vmcase(OP_NOT) { | ||
| 1567 | StkId ra = RA(i); | ||
| 1568 | TValue *rb = vRB(i); | ||
| 1569 | if (l_isfalse(rb)) | ||
| 1570 | setbtvalue(s2v(ra)); | ||
| 1571 | else | ||
| 1572 | setbfvalue(s2v(ra)); | ||
| 1573 | vmbreak; | ||
| 1574 | } | ||
| 1575 | vmcase(OP_LEN) { | ||
| 1576 | StkId ra = RA(i); | ||
| 1577 | Protect(luaV_objlen(L, ra, vRB(i))); | ||
| 1578 | vmbreak; | ||
| 1579 | } | ||
| 1580 | vmcase(OP_CONCAT) { | ||
| 1581 | StkId ra = RA(i); | ||
| 1582 | int n = GETARG_B(i); /* number of elements to concatenate */ | ||
| 1583 | L->top.p = ra + n; /* mark the end of concat operands */ | ||
| 1584 | ProtectNT(luaV_concat(L, n)); | ||
| 1585 | checkGC(L, L->top.p); /* 'luaV_concat' ensures correct top */ | ||
| 1586 | vmbreak; | ||
| 1587 | } | ||
| 1588 | vmcase(OP_CLOSE) { | ||
| 1589 | StkId ra = RA(i); | ||
| 1590 | Protect(luaF_close(L, ra, LUA_OK, 1)); | ||
| 1591 | vmbreak; | ||
| 1592 | } | ||
| 1593 | vmcase(OP_TBC) { | ||
| 1594 | StkId ra = RA(i); | ||
| 1595 | /* create new to-be-closed upvalue */ | ||
| 1596 | halfProtect(luaF_newtbcupval(L, ra)); | ||
| 1597 | vmbreak; | ||
| 1598 | } | ||
| 1599 | vmcase(OP_JMP) { | ||
| 1600 | dojump(ci, i, 0); | ||
| 1601 | vmbreak; | ||
| 1602 | } | ||
| 1603 | vmcase(OP_EQ) { | ||
| 1604 | StkId ra = RA(i); | ||
| 1605 | int cond; | ||
| 1606 | TValue *rb = vRB(i); | ||
| 1607 | Protect(cond = luaV_equalobj(L, s2v(ra), rb)); | ||
| 1608 | docondjump(); | ||
| 1609 | vmbreak; | ||
| 1610 | } | ||
| 1611 | vmcase(OP_LT) { | ||
| 1612 | op_order(L, l_lti, LTnum, lessthanothers); | ||
| 1613 | vmbreak; | ||
| 1614 | } | ||
| 1615 | vmcase(OP_LE) { | ||
| 1616 | op_order(L, l_lei, LEnum, lessequalothers); | ||
| 1617 | vmbreak; | ||
| 1618 | } | ||
| 1619 | vmcase(OP_EQK) { | ||
| 1620 | StkId ra = RA(i); | ||
| 1621 | TValue *rb = KB(i); | ||
| 1622 | /* basic types do not use '__eq'; we can use raw equality */ | ||
| 1623 | int cond = luaV_rawequalobj(s2v(ra), rb); | ||
| 1624 | docondjump(); | ||
| 1625 | vmbreak; | ||
| 1626 | } | ||
| 1627 | vmcase(OP_EQI) { | ||
| 1628 | StkId ra = RA(i); | ||
| 1629 | int cond; | ||
| 1630 | int im = GETARG_sB(i); | ||
| 1631 | if (ttisinteger(s2v(ra))) | ||
| 1632 | cond = (ivalue(s2v(ra)) == im); | ||
| 1633 | else if (ttisfloat(s2v(ra))) | ||
| 1634 | cond = luai_numeq(fltvalue(s2v(ra)), cast_num(im)); | ||
| 1635 | else | ||
| 1636 | cond = 0; /* other types cannot be equal to a number */ | ||
| 1637 | docondjump(); | ||
| 1638 | vmbreak; | ||
| 1639 | } | ||
| 1640 | vmcase(OP_LTI) { | ||
| 1641 | op_orderI(L, l_lti, luai_numlt, 0, TM_LT); | ||
| 1642 | vmbreak; | ||
| 1643 | } | ||
| 1644 | vmcase(OP_LEI) { | ||
| 1645 | op_orderI(L, l_lei, luai_numle, 0, TM_LE); | ||
| 1646 | vmbreak; | ||
| 1647 | } | ||
| 1648 | vmcase(OP_GTI) { | ||
| 1649 | op_orderI(L, l_gti, luai_numgt, 1, TM_LT); | ||
| 1650 | vmbreak; | ||
| 1651 | } | ||
| 1652 | vmcase(OP_GEI) { | ||
| 1653 | op_orderI(L, l_gei, luai_numge, 1, TM_LE); | ||
| 1654 | vmbreak; | ||
| 1655 | } | ||
| 1656 | vmcase(OP_TEST) { | ||
| 1657 | StkId ra = RA(i); | ||
| 1658 | int cond = !l_isfalse(s2v(ra)); | ||
| 1659 | docondjump(); | ||
| 1660 | vmbreak; | ||
| 1661 | } | ||
| 1662 | vmcase(OP_TESTSET) { | ||
| 1663 | StkId ra = RA(i); | ||
| 1664 | TValue *rb = vRB(i); | ||
| 1665 | if (l_isfalse(rb) == GETARG_k(i)) | ||
| 1666 | pc++; | ||
| 1667 | else { | ||
| 1668 | setobj2s(L, ra, rb); | ||
| 1669 | donextjump(ci); | ||
| 1670 | } | ||
| 1671 | vmbreak; | ||
| 1672 | } | ||
| 1673 | vmcase(OP_CALL) { | ||
| 1674 | StkId ra = RA(i); | ||
| 1675 | CallInfo *newci; | ||
| 1676 | int b = GETARG_B(i); | ||
| 1677 | int nresults = GETARG_C(i) - 1; | ||
| 1678 | if (b != 0) /* fixed number of arguments? */ | ||
| 1679 | L->top.p = ra + b; /* top signals number of arguments */ | ||
| 1680 | /* else previous instruction set top */ | ||
| 1681 | savepc(L); /* in case of errors */ | ||
| 1682 | if ((newci = luaD_precall(L, ra, nresults)) == NULL) | ||
| 1683 | updatetrap(ci); /* C call; nothing else to be done */ | ||
| 1684 | else { /* Lua call: run function in this same C frame */ | ||
| 1685 | ci = newci; | ||
| 1686 | goto startfunc; | ||
| 1687 | } | ||
| 1688 | vmbreak; | ||
| 1689 | } | ||
| 1690 | vmcase(OP_TAILCALL) { | ||
| 1691 | StkId ra = RA(i); | ||
| 1692 | int b = GETARG_B(i); /* number of arguments + 1 (function) */ | ||
| 1693 | int n; /* number of results when calling a C function */ | ||
| 1694 | int nparams1 = GETARG_C(i); | ||
| 1695 | /* delta is virtual 'func' - real 'func' (vararg functions) */ | ||
| 1696 | int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0; | ||
| 1697 | if (b != 0) | ||
| 1698 | L->top.p = ra + b; | ||
| 1699 | else /* previous instruction set top */ | ||
| 1700 | b = cast_int(L->top.p - ra); | ||
| 1701 | savepc(ci); /* several calls here can raise errors */ | ||
| 1702 | if (TESTARG_k(i)) { | ||
| 1703 | luaF_closeupval(L, base); /* close upvalues from current call */ | ||
| 1704 | lua_assert(L->tbclist.p < base); /* no pending tbc variables */ | ||
| 1705 | lua_assert(base == ci->func.p + 1); | ||
| 1706 | } | ||
| 1707 | if ((n = luaD_pretailcall(L, ci, ra, b, delta)) < 0) /* Lua function? */ | ||
| 1708 | goto startfunc; /* execute the callee */ | ||
| 1709 | else { /* C function? */ | ||
| 1710 | ci->func.p -= delta; /* restore 'func' (if vararg) */ | ||
| 1711 | luaD_poscall(L, ci, n); /* finish caller */ | ||
| 1712 | updatetrap(ci); /* 'luaD_poscall' can change hooks */ | ||
| 1713 | goto ret; /* caller returns after the tail call */ | ||
| 1714 | } | ||
| 1715 | } | ||
| 1716 | vmcase(OP_RETURN) { | ||
| 1717 | StkId ra = RA(i); | ||
| 1718 | int n = GETARG_B(i) - 1; /* number of results */ | ||
| 1719 | int nparams1 = GETARG_C(i); | ||
| 1720 | if (n < 0) /* not fixed? */ | ||
| 1721 | n = cast_int(L->top.p - ra); /* get what is available */ | ||
| 1722 | savepc(ci); | ||
| 1723 | if (TESTARG_k(i)) { /* may there be open upvalues? */ | ||
| 1724 | ci->u2.nres = n; /* save number of returns */ | ||
| 1725 | if (L->top.p < ci->top.p) | ||
| 1726 | L->top.p = ci->top.p; | ||
| 1727 | luaF_close(L, base, CLOSEKTOP, 1); | ||
| 1728 | updatetrap(ci); | ||
| 1729 | updatestack(ci); | ||
| 1730 | } | ||
| 1731 | if (nparams1) /* vararg function? */ | ||
| 1732 | ci->func.p -= ci->u.l.nextraargs + nparams1; | ||
| 1733 | L->top.p = ra + n; /* set call for 'luaD_poscall' */ | ||
| 1734 | luaD_poscall(L, ci, n); | ||
| 1735 | updatetrap(ci); /* 'luaD_poscall' can change hooks */ | ||
| 1736 | goto ret; | ||
| 1737 | } | ||
| 1738 | vmcase(OP_RETURN0) { | ||
| 1739 | if (l_unlikely(L->hookmask)) { | ||
| 1740 | StkId ra = RA(i); | ||
| 1741 | L->top.p = ra; | ||
| 1742 | savepc(ci); | ||
| 1743 | luaD_poscall(L, ci, 0); /* no hurry... */ | ||
| 1744 | trap = 1; | ||
| 1745 | } | ||
| 1746 | else { /* do the 'poscall' here */ | ||
| 1747 | int nres; | ||
| 1748 | L->ci = ci->previous; /* back to caller */ | ||
| 1749 | L->top.p = base - 1; | ||
| 1750 | for (nres = ci->nresults; l_unlikely(nres > 0); nres--) | ||
| 1751 | setnilvalue(s2v(L->top.p++)); /* all results are nil */ | ||
| 1752 | } | ||
| 1753 | goto ret; | ||
| 1754 | } | ||
| 1755 | vmcase(OP_RETURN1) { | ||
| 1756 | if (l_unlikely(L->hookmask)) { | ||
| 1757 | StkId ra = RA(i); | ||
| 1758 | L->top.p = ra + 1; | ||
| 1759 | savepc(ci); | ||
| 1760 | luaD_poscall(L, ci, 1); /* no hurry... */ | ||
| 1761 | trap = 1; | ||
| 1762 | } | ||
| 1763 | else { /* do the 'poscall' here */ | ||
| 1764 | int nres = ci->nresults; | ||
| 1765 | L->ci = ci->previous; /* back to caller */ | ||
| 1766 | if (nres == 0) | ||
| 1767 | L->top.p = base - 1; /* asked for no results */ | ||
| 1768 | else { | ||
| 1769 | StkId ra = RA(i); | ||
| 1770 | setobjs2s(L, base - 1, ra); /* at least this result */ | ||
| 1771 | L->top.p = base; | ||
| 1772 | for (; l_unlikely(nres > 1); nres--) | ||
| 1773 | setnilvalue(s2v(L->top.p++)); /* complete missing results */ | ||
| 1774 | } | ||
| 1775 | } | ||
| 1776 | ret: /* return from a Lua function */ | ||
| 1777 | if (ci->callstatus & CIST_FRESH) | ||
| 1778 | return; /* end this frame */ | ||
| 1779 | else { | ||
| 1780 | ci = ci->previous; | ||
| 1781 | goto returning; /* continue running caller in this frame */ | ||
| 1782 | } | ||
| 1783 | } | ||
| 1784 | vmcase(OP_FORLOOP) { | ||
| 1785 | StkId ra = RA(i); | ||
| 1786 | if (ttisinteger(s2v(ra + 2))) { /* integer loop? */ | ||
| 1787 | lua_Unsigned count = l_castS2U(ivalue(s2v(ra + 1))); | ||
| 1788 | if (count > 0) { /* still more iterations? */ | ||
| 1789 | lua_Integer step = ivalue(s2v(ra + 2)); | ||
| 1790 | lua_Integer idx = ivalue(s2v(ra)); /* internal index */ | ||
| 1791 | chgivalue(s2v(ra + 1), count - 1); /* update counter */ | ||
| 1792 | idx = intop(+, idx, step); /* add step to index */ | ||
| 1793 | chgivalue(s2v(ra), idx); /* update internal index */ | ||
| 1794 | setivalue(s2v(ra + 3), idx); /* and control variable */ | ||
| 1795 | pc -= GETARG_Bx(i); /* jump back */ | ||
| 1796 | } | ||
| 1797 | } | ||
| 1798 | else if (floatforloop(ra)) /* float loop */ | ||
| 1799 | pc -= GETARG_Bx(i); /* jump back */ | ||
| 1800 | updatetrap(ci); /* allows a signal to break the loop */ | ||
| 1801 | vmbreak; | ||
| 1802 | } | ||
| 1803 | vmcase(OP_FORPREP) { | ||
| 1804 | StkId ra = RA(i); | ||
| 1805 | savestate(L, ci); /* in case of errors */ | ||
| 1806 | if (forprep(L, ra)) | ||
| 1807 | pc += GETARG_Bx(i) + 1; /* skip the loop */ | ||
| 1808 | vmbreak; | ||
| 1809 | } | ||
| 1810 | vmcase(OP_TFORPREP) { | ||
| 1811 | StkId ra = RA(i); | ||
| 1812 | /* create to-be-closed upvalue (if needed) */ | ||
| 1813 | halfProtect(luaF_newtbcupval(L, ra + 3)); | ||
| 1814 | pc += GETARG_Bx(i); | ||
| 1815 | i = *(pc++); /* go to next instruction */ | ||
| 1816 | lua_assert(GET_OPCODE(i) == OP_TFORCALL && ra == RA(i)); | ||
| 1817 | goto l_tforcall; | ||
| 1818 | } | ||
| 1819 | vmcase(OP_TFORCALL) { | ||
| 1820 | l_tforcall: { | ||
| 1821 | StkId ra = RA(i); | ||
| 1822 | /* 'ra' has the iterator function, 'ra + 1' has the state, | ||
| 1823 | 'ra + 2' has the control variable, and 'ra + 3' has the | ||
| 1824 | to-be-closed variable. The call will use the stack after | ||
| 1825 | these values (starting at 'ra + 4') | ||
| 1826 | */ | ||
| 1827 | /* push function, state, and control variable */ | ||
| 1828 | memcpy(ra + 4, ra, 3 * sizeof(*ra)); | ||
| 1829 | L->top.p = ra + 4 + 3; | ||
| 1830 | ProtectNT(luaD_call(L, ra + 4, GETARG_C(i))); /* do the call */ | ||
| 1831 | updatestack(ci); /* stack may have changed */ | ||
| 1832 | i = *(pc++); /* go to next instruction */ | ||
| 1833 | lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i)); | ||
| 1834 | goto l_tforloop; | ||
| 1835 | }} | ||
| 1836 | vmcase(OP_TFORLOOP) { | ||
| 1837 | l_tforloop: { | ||
| 1838 | StkId ra = RA(i); | ||
| 1839 | if (!ttisnil(s2v(ra + 4))) { /* continue loop? */ | ||
| 1840 | setobjs2s(L, ra + 2, ra + 4); /* save control variable */ | ||
| 1841 | pc -= GETARG_Bx(i); /* jump back */ | ||
| 1842 | } | ||
| 1843 | vmbreak; | ||
| 1844 | }} | ||
| 1845 | vmcase(OP_SETLIST) { | ||
| 1846 | StkId ra = RA(i); | ||
| 1847 | int n = GETARG_B(i); | ||
| 1848 | unsigned int last = GETARG_C(i); | ||
| 1849 | Table *h = hvalue(s2v(ra)); | ||
| 1850 | if (n == 0) | ||
| 1851 | n = cast_int(L->top.p - ra) - 1; /* get up to the top */ | ||
| 1852 | else | ||
| 1853 | L->top.p = ci->top.p; /* correct top in case of emergency GC */ | ||
| 1854 | last += n; | ||
| 1855 | if (TESTARG_k(i)) { | ||
| 1856 | last += GETARG_Ax(*pc) * (MAXARG_C + 1); | ||
| 1857 | pc++; | ||
| 1858 | } | ||
| 1859 | if (last > luaH_realasize(h)) /* needs more space? */ | ||
| 1860 | luaH_resizearray(L, h, last); /* preallocate it at once */ | ||
| 1861 | for (; n > 0; n--) { | ||
| 1862 | TValue *val = s2v(ra + n); | ||
| 1863 | setobj2t(L, &h->array[last - 1], val); | ||
| 1864 | last--; | ||
| 1865 | luaC_barrierback(L, obj2gco(h), val); | ||
| 1866 | } | ||
| 1867 | vmbreak; | ||
| 1868 | } | ||
| 1869 | vmcase(OP_CLOSURE) { | ||
| 1870 | StkId ra = RA(i); | ||
| 1871 | Proto *p = cl->p->p[GETARG_Bx(i)]; | ||
| 1872 | halfProtect(pushclosure(L, p, cl->upvals, base, ra)); | ||
| 1873 | checkGC(L, ra + 1); | ||
| 1874 | vmbreak; | ||
| 1875 | } | ||
| 1876 | vmcase(OP_VARARG) { | ||
| 1877 | StkId ra = RA(i); | ||
| 1878 | int n = GETARG_C(i) - 1; /* required results */ | ||
| 1879 | Protect(luaT_getvarargs(L, ci, ra, n)); | ||
| 1880 | vmbreak; | ||
| 1881 | } | ||
| 1882 | vmcase(OP_VARARGPREP) { | ||
| 1883 | ProtectNT(luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p)); | ||
| 1884 | if (l_unlikely(trap)) { /* previous "Protect" updated trap */ | ||
| 1885 | luaD_hookcall(L, ci); | ||
| 1886 | L->oldpc = 1; /* next opcode will be seen as a "new" line */ | ||
| 1887 | } | ||
| 1888 | updatebase(ci); /* function has new base after adjustment */ | ||
| 1889 | vmbreak; | ||
| 1890 | } | ||
| 1891 | vmcase(OP_EXTRAARG) { | ||
| 1892 | lua_assert(0); | ||
| 1893 | vmbreak; | ||
| 1894 | } | ||
| 1895 | } | ||
| 1896 | } | ||
| 1897 | } | ||
| 1898 | |||
| 1899 | /* }================================================================== */ | ||
