]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* |
2 | ** $Id: lvm.c,v 2.245 2015/06/09 15:53:35 roberto Exp $ | |
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 | /* limit for table tag-method chains (to avoid loops) */ | |
35 | #define MAXTAGLOOP 2000 | |
36 | ||
37 | ||
38 | ||
39 | /* | |
40 | ** 'l_intfitsf' checks whether a given integer can be converted to a | |
41 | ** float without rounding. Used in comparisons. Left undefined if | |
42 | ** all integers fit in a float precisely. | |
43 | */ | |
44 | #if !defined(l_intfitsf) | |
45 | ||
46 | /* number of bits in the mantissa of a float */ | |
47 | #define NBM (l_mathlim(MANT_DIG)) | |
48 | ||
49 | /* | |
50 | ** Check whether some integers may not fit in a float, that is, whether | |
51 | ** (maxinteger >> NBM) > 0 (that implies (1 << NBM) <= maxinteger). | |
52 | ** (The shifts are done in parts to avoid shifting by more than the size | |
53 | ** of an integer. In a worst case, NBM == 113 for long double and | |
54 | ** sizeof(integer) == 32.) | |
55 | */ | |
56 | #if ((((LUA_MAXINTEGER >> (NBM / 4)) >> (NBM / 4)) >> (NBM / 4)) \ | |
57 | >> (NBM - (3 * (NBM / 4)))) > 0 | |
58 | ||
59 | #define l_intfitsf(i) \ | |
60 | (-((lua_Integer)1 << NBM) <= (i) && (i) <= ((lua_Integer)1 << NBM)) | |
61 | ||
62 | #endif | |
63 | ||
64 | #endif | |
65 | ||
66 | ||
67 | ||
68 | /* | |
69 | ** Try to convert a value to a float. The float case is already handled | |
70 | ** by the macro 'tonumber'. | |
71 | */ | |
72 | int luaV_tonumber_ (const TValue *obj, lua_Number *n) { | |
73 | TValue v; | |
74 | if (ttisinteger(obj)) { | |
75 | *n = cast_num(ivalue(obj)); | |
76 | return 1; | |
77 | } | |
78 | else if (cvt2num(obj) && /* string convertible to number? */ | |
79 | luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) { | |
80 | *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */ | |
81 | return 1; | |
82 | } | |
83 | else | |
84 | return 0; /* conversion failed */ | |
85 | } | |
86 | ||
87 | ||
88 | /* | |
89 | ** try to convert a value to an integer, rounding according to 'mode': | |
90 | ** mode == 0: accepts only integral values | |
91 | ** mode == 1: takes the floor of the number | |
92 | ** mode == 2: takes the ceil of the number | |
93 | */ | |
94 | int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { | |
95 | TValue v; | |
96 | again: | |
97 | if (ttisfloat(obj)) { | |
98 | lua_Number n = fltvalue(obj); | |
99 | lua_Number f = l_floor(n); | |
100 | if (n != f) { /* not an integral value? */ | |
101 | if (mode == 0) return 0; /* fails if mode demands integral value */ | |
102 | else if (mode > 1) /* needs ceil? */ | |
103 | f += 1; /* convert floor to ceil (remember: n != f) */ | |
104 | } | |
105 | return lua_numbertointeger(f, p); | |
106 | } | |
107 | else if (ttisinteger(obj)) { | |
108 | *p = ivalue(obj); | |
109 | return 1; | |
110 | } | |
111 | else if (cvt2num(obj) && | |
112 | luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) { | |
113 | obj = &v; | |
114 | goto again; /* convert result from 'luaO_str2num' to an integer */ | |
115 | } | |
116 | return 0; /* conversion failed */ | |
117 | } | |
118 | ||
119 | ||
120 | /* | |
121 | ** Try to convert a 'for' limit to an integer, preserving the | |
122 | ** semantics of the loop. | |
123 | ** (The following explanation assumes a non-negative step; it is valid | |
124 | ** for negative steps mutatis mutandis.) | |
125 | ** If the limit can be converted to an integer, rounding down, that is | |
126 | ** it. | |
127 | ** Otherwise, check whether the limit can be converted to a number. If | |
128 | ** the number is too large, it is OK to set the limit as LUA_MAXINTEGER, | |
129 | ** which means no limit. If the number is too negative, the loop | |
130 | ** should not run, because any initial integer value is larger than the | |
131 | ** limit. So, it sets the limit to LUA_MININTEGER. 'stopnow' corrects | |
132 | ** the extreme case when the initial value is LUA_MININTEGER, in which | |
133 | ** case the LUA_MININTEGER limit would still run the loop once. | |
134 | */ | |
135 | static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, | |
136 | int *stopnow) { | |
137 | *stopnow = 0; /* usually, let loops run */ | |
138 | if (!luaV_tointeger(obj, p, (step < 0 ? 2 : 1))) { /* not fit in integer? */ | |
139 | lua_Number n; /* try to convert to float */ | |
140 | if (!tonumber(obj, &n)) /* cannot convert to float? */ | |
141 | return 0; /* not a number */ | |
142 | if (luai_numlt(0, n)) { /* if true, float is larger than max integer */ | |
143 | *p = LUA_MAXINTEGER; | |
144 | if (step < 0) *stopnow = 1; | |
145 | } | |
146 | else { /* float is smaller than min integer */ | |
147 | *p = LUA_MININTEGER; | |
148 | if (step >= 0) *stopnow = 1; | |
149 | } | |
150 | } | |
151 | return 1; | |
152 | } | |
153 | ||
154 | ||
155 | /* | |
156 | ** Main function for table access (invoking metamethods if needed). | |
157 | ** Compute 'val = t[key]' | |
158 | */ | |
159 | void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) { | |
160 | int loop; /* counter to avoid infinite loops */ | |
161 | for (loop = 0; loop < MAXTAGLOOP; loop++) { | |
162 | const TValue *tm; | |
163 | if (ttistable(t)) { /* 't' is a table? */ | |
164 | Table *h = hvalue(t); | |
165 | const TValue *res = luaH_get(h, key); /* do a primitive get */ | |
166 | if (!ttisnil(res) || /* result is not nil? */ | |
167 | (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */ | |
168 | setobj2s(L, val, res); /* result is the raw get */ | |
169 | return; | |
170 | } | |
171 | /* else will try metamethod */ | |
172 | } | |
173 | else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX))) | |
174 | luaG_typeerror(L, t, "index"); /* no metamethod */ | |
175 | if (ttisfunction(tm)) { /* metamethod is a function */ | |
176 | luaT_callTM(L, tm, t, key, val, 1); | |
177 | return; | |
178 | } | |
179 | t = tm; /* else repeat access over 'tm' */ | |
180 | } | |
181 | luaG_runerror(L, "gettable chain too long; possible loop"); | |
182 | } | |
183 | ||
184 | ||
185 | /* | |
186 | ** Main function for table assignment (invoking metamethods if needed). | |
187 | ** Compute 't[key] = val' | |
188 | */ | |
189 | void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { | |
190 | int loop; /* counter to avoid infinite loops */ | |
191 | for (loop = 0; loop < MAXTAGLOOP; loop++) { | |
192 | const TValue *tm; | |
193 | if (ttistable(t)) { /* 't' is a table? */ | |
194 | Table *h = hvalue(t); | |
195 | TValue *oldval = cast(TValue *, luaH_get(h, key)); | |
196 | /* if previous value is not nil, there must be a previous entry | |
197 | in the table; a metamethod has no relevance */ | |
198 | if (!ttisnil(oldval) || | |
199 | /* previous value is nil; must check the metamethod */ | |
200 | ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL && | |
201 | /* no metamethod; is there a previous entry in the table? */ | |
202 | (oldval != luaO_nilobject || | |
203 | /* no previous entry; must create one. (The next test is | |
204 | always true; we only need the assignment.) */ | |
205 | (oldval = luaH_newkey(L, h, key), 1)))) { | |
206 | /* no metamethod and (now) there is an entry with given key */ | |
207 | setobj2t(L, oldval, val); /* assign new value to that entry */ | |
208 | invalidateTMcache(h); | |
209 | luaC_barrierback(L, h, val); | |
210 | return; | |
211 | } | |
212 | /* else will try the metamethod */ | |
213 | } | |
214 | else /* not a table; check metamethod */ | |
215 | if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) | |
216 | luaG_typeerror(L, t, "index"); | |
217 | /* try the metamethod */ | |
218 | if (ttisfunction(tm)) { | |
219 | luaT_callTM(L, tm, t, key, val, 0); | |
220 | return; | |
221 | } | |
222 | t = tm; /* else repeat assignment over 'tm' */ | |
223 | } | |
224 | luaG_runerror(L, "settable chain too long; possible loop"); | |
225 | } | |
226 | ||
227 | ||
228 | /* | |
229 | ** Compare two strings 'ls' x 'rs', returning an integer smaller-equal- | |
230 | ** -larger than zero if 'ls' is smaller-equal-larger than 'rs'. | |
231 | ** The code is a little tricky because it allows '\0' in the strings | |
232 | ** and it uses 'strcoll' (to respect locales) for each segments | |
233 | ** of the strings. | |
234 | */ | |
235 | static int l_strcmp (const TString *ls, const TString *rs) { | |
236 | const char *l = getstr(ls); | |
237 | size_t ll = tsslen(ls); | |
238 | const char *r = getstr(rs); | |
239 | size_t lr = tsslen(rs); | |
240 | for (;;) { /* for each segment */ | |
241 | int temp = strcoll(l, r); | |
242 | if (temp != 0) /* not equal? */ | |
243 | return temp; /* done */ | |
244 | else { /* strings are equal up to a '\0' */ | |
245 | size_t len = strlen(l); /* index of first '\0' in both strings */ | |
246 | if (len == lr) /* 'rs' is finished? */ | |
247 | return (len == ll) ? 0 : 1; /* check 'ls' */ | |
248 | else if (len == ll) /* 'ls' is finished? */ | |
249 | return -1; /* 'ls' is smaller than 'rs' ('rs' is not finished) */ | |
250 | /* both strings longer than 'len'; go on comparing after the '\0' */ | |
251 | len++; | |
252 | l += len; ll -= len; r += len; lr -= len; | |
253 | } | |
254 | } | |
255 | } | |
256 | ||
257 | ||
258 | /* | |
259 | ** Check whether integer 'i' is less than float 'f'. If 'i' has an | |
260 | ** exact representation as a float ('l_intfitsf'), compare numbers as | |
261 | ** floats. Otherwise, if 'f' is outside the range for integers, result | |
262 | ** is trivial. Otherwise, compare them as integers. (When 'i' has no | |
263 | ** float representation, either 'f' is "far away" from 'i' or 'f' has | |
264 | ** no precision left for a fractional part; either way, how 'f' is | |
265 | ** truncated is irrelevant.) When 'f' is NaN, comparisons must result | |
266 | ** in false. | |
267 | */ | |
268 | static int LTintfloat (lua_Integer i, lua_Number f) { | |
269 | #if defined(l_intfitsf) | |
270 | if (!l_intfitsf(i)) { | |
271 | if (f >= -cast_num(LUA_MININTEGER)) /* -minint == maxint + 1 */ | |
272 | return 1; /* f >= maxint + 1 > i */ | |
273 | else if (f > cast_num(LUA_MININTEGER)) /* minint < f <= maxint ? */ | |
274 | return (i < cast(lua_Integer, f)); /* compare them as integers */ | |
275 | else /* f <= minint <= i (or 'f' is NaN) --> not(i < f) */ | |
276 | return 0; | |
277 | } | |
278 | #endif | |
279 | return luai_numlt(cast_num(i), f); /* compare them as floats */ | |
280 | } | |
281 | ||
282 | ||
283 | /* | |
284 | ** Check whether integer 'i' is less than or equal to float 'f'. | |
285 | ** See comments on previous function. | |
286 | */ | |
287 | static int LEintfloat (lua_Integer i, lua_Number f) { | |
288 | #if defined(l_intfitsf) | |
289 | if (!l_intfitsf(i)) { | |
290 | if (f >= -cast_num(LUA_MININTEGER)) /* -minint == maxint + 1 */ | |
291 | return 1; /* f >= maxint + 1 > i */ | |
292 | else if (f >= cast_num(LUA_MININTEGER)) /* minint <= f <= maxint ? */ | |
293 | return (i <= cast(lua_Integer, f)); /* compare them as integers */ | |
294 | else /* f < minint <= i (or 'f' is NaN) --> not(i <= f) */ | |
295 | return 0; | |
296 | } | |
297 | #endif | |
298 | return luai_numle(cast_num(i), f); /* compare them as floats */ | |
299 | } | |
300 | ||
301 | ||
302 | /* | |
303 | ** Return 'l < r', for numbers. | |
304 | */ | |
305 | static int LTnum (const TValue *l, const TValue *r) { | |
306 | if (ttisinteger(l)) { | |
307 | lua_Integer li = ivalue(l); | |
308 | if (ttisinteger(r)) | |
309 | return li < ivalue(r); /* both are integers */ | |
310 | else /* 'l' is int and 'r' is float */ | |
311 | return LTintfloat(li, fltvalue(r)); /* l < r ? */ | |
312 | } | |
313 | else { | |
314 | lua_Number lf = fltvalue(l); /* 'l' must be float */ | |
315 | if (ttisfloat(r)) | |
316 | return luai_numlt(lf, fltvalue(r)); /* both are float */ | |
317 | else if (luai_numisnan(lf)) /* 'r' is int and 'l' is float */ | |
318 | return 0; /* NaN < i is always false */ | |
319 | else /* without NaN, (l < r) <--> not(r <= l) */ | |
320 | return !LEintfloat(ivalue(r), lf); /* not (r <= l) ? */ | |
321 | } | |
322 | } | |
323 | ||
324 | ||
325 | /* | |
326 | ** Return 'l <= r', for numbers. | |
327 | */ | |
328 | static int LEnum (const TValue *l, const TValue *r) { | |
329 | if (ttisinteger(l)) { | |
330 | lua_Integer li = ivalue(l); | |
331 | if (ttisinteger(r)) | |
332 | return li <= ivalue(r); /* both are integers */ | |
333 | else /* 'l' is int and 'r' is float */ | |
334 | return LEintfloat(li, fltvalue(r)); /* l <= r ? */ | |
335 | } | |
336 | else { | |
337 | lua_Number lf = fltvalue(l); /* 'l' must be float */ | |
338 | if (ttisfloat(r)) | |
339 | return luai_numle(lf, fltvalue(r)); /* both are float */ | |
340 | else if (luai_numisnan(lf)) /* 'r' is int and 'l' is float */ | |
341 | return 0; /* NaN <= i is always false */ | |
342 | else /* without NaN, (l <= r) <--> not(r < l) */ | |
343 | return !LTintfloat(ivalue(r), lf); /* not (r < l) ? */ | |
344 | } | |
345 | } | |
346 | ||
347 | ||
348 | /* | |
349 | ** Main operation less than; return 'l < r'. | |
350 | */ | |
351 | int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { | |
352 | int res; | |
353 | if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ | |
354 | return LTnum(l, r); | |
355 | else if (ttisstring(l) && ttisstring(r)) /* both are strings? */ | |
356 | return l_strcmp(tsvalue(l), tsvalue(r)) < 0; | |
357 | else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0) /* no metamethod? */ | |
358 | luaG_ordererror(L, l, r); /* error */ | |
359 | return res; | |
360 | } | |
361 | ||
362 | ||
363 | /* | |
364 | ** Main operation less than or equal to; return 'l <= r'. If it needs | |
365 | ** a metamethod and there is no '__le', try '__lt', based on | |
366 | ** l <= r iff !(r < l) (assuming a total order). If the metamethod | |
367 | ** yields during this substitution, the continuation has to know | |
368 | ** about it (to negate the result of r<l); bit CIST_LEQ in the call | |
369 | ** status keeps that information. | |
370 | */ | |
371 | int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { | |
372 | int res; | |
373 | if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ | |
374 | return LEnum(l, r); | |
375 | else if (ttisstring(l) && ttisstring(r)) /* both are strings? */ | |
376 | return l_strcmp(tsvalue(l), tsvalue(r)) <= 0; | |
377 | else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* try 'le' */ | |
378 | return res; | |
379 | else { /* try 'lt': */ | |
380 | L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ | |
381 | res = luaT_callorderTM(L, r, l, TM_LT); | |
382 | L->ci->callstatus ^= CIST_LEQ; /* clear mark */ | |
383 | if (res < 0) | |
384 | luaG_ordererror(L, l, r); | |
385 | return !res; /* result is negated */ | |
386 | } | |
387 | } | |
388 | ||
389 | ||
390 | /* | |
391 | ** Main operation for equality of Lua values; return 't1 == t2'. | |
392 | ** L == NULL means raw equality (no metamethods) | |
393 | */ | |
394 | int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { | |
395 | const TValue *tm; | |
396 | if (ttype(t1) != ttype(t2)) { /* not the same variant? */ | |
397 | if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER) | |
398 | return 0; /* only numbers can be equal with different variants */ | |
399 | else { /* two numbers with different variants */ | |
400 | lua_Integer i1, i2; /* compare them as integers */ | |
401 | return (tointeger(t1, &i1) && tointeger(t2, &i2) && i1 == i2); | |
402 | } | |
403 | } | |
404 | /* values have same type and same variant */ | |
405 | switch (ttype(t1)) { | |
406 | case LUA_TNIL: return 1; | |
407 | case LUA_TNUMINT: return (ivalue(t1) == ivalue(t2)); | |
408 | case LUA_TNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2)); | |
409 | case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ | |
410 | case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); | |
411 | case LUA_TLCF: return fvalue(t1) == fvalue(t2); | |
412 | case LUA_TSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2)); | |
413 | case LUA_TLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2)); | |
414 | case LUA_TUSERDATA: { | |
415 | if (uvalue(t1) == uvalue(t2)) return 1; | |
416 | else if (L == NULL) return 0; | |
417 | tm = fasttm(L, uvalue(t1)->metatable, TM_EQ); | |
418 | if (tm == NULL) | |
419 | tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); | |
420 | break; /* will try TM */ | |
421 | } | |
422 | case LUA_TTABLE: { | |
423 | if (hvalue(t1) == hvalue(t2)) return 1; | |
424 | else if (L == NULL) return 0; | |
425 | tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); | |
426 | if (tm == NULL) | |
427 | tm = fasttm(L, hvalue(t2)->metatable, TM_EQ); | |
428 | break; /* will try TM */ | |
429 | } | |
430 | default: | |
431 | return gcvalue(t1) == gcvalue(t2); | |
432 | } | |
433 | if (tm == NULL) /* no TM? */ | |
434 | return 0; /* objects are different */ | |
435 | luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */ | |
436 | return !l_isfalse(L->top); | |
437 | } | |
438 | ||
439 | ||
440 | /* macro used by 'luaV_concat' to ensure that element at 'o' is a string */ | |
441 | #define tostring(L,o) \ | |
442 | (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1))) | |
443 | ||
444 | #define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0) | |
445 | ||
446 | /* | |
447 | ** Main operation for concatenation: concat 'total' values in the stack, | |
448 | ** from 'L->top - total' up to 'L->top - 1'. | |
449 | */ | |
450 | void luaV_concat (lua_State *L, int total) { | |
451 | lua_assert(total >= 2); | |
452 | do { | |
453 | StkId top = L->top; | |
454 | int n = 2; /* number of elements handled in this pass (at least 2) */ | |
455 | if (!(ttisstring(top-2) || cvt2str(top-2)) || !tostring(L, top-1)) | |
456 | luaT_trybinTM(L, top-2, top-1, top-2, TM_CONCAT); | |
457 | else if (isemptystr(top - 1)) /* second operand is empty? */ | |
458 | cast_void(tostring(L, top - 2)); /* result is first operand */ | |
459 | else if (isemptystr(top - 2)) { /* first operand is an empty string? */ | |
460 | setobjs2s(L, top - 2, top - 1); /* result is second op. */ | |
461 | } | |
462 | else { | |
463 | /* at least two non-empty string values; get as many as possible */ | |
464 | size_t tl = vslen(top - 1); | |
465 | char *buffer; | |
466 | int i; | |
467 | /* collect total length */ | |
468 | for (i = 1; i < total && tostring(L, top-i-1); i++) { | |
469 | size_t l = vslen(top - i - 1); | |
470 | if (l >= (MAX_SIZE/sizeof(char)) - tl) | |
471 | luaG_runerror(L, "string length overflow"); | |
472 | tl += l; | |
473 | } | |
474 | buffer = luaZ_openspace(L, &G(L)->buff, tl); | |
475 | tl = 0; | |
476 | n = i; | |
477 | do { /* copy all strings to buffer */ | |
478 | size_t l = vslen(top - i); | |
479 | memcpy(buffer+tl, svalue(top-i), l * sizeof(char)); | |
480 | tl += l; | |
481 | } while (--i > 0); | |
482 | setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl)); /* create result */ | |
483 | } | |
484 | total -= n-1; /* got 'n' strings to create 1 new */ | |
485 | L->top -= n-1; /* popped 'n' strings and pushed one */ | |
486 | } while (total > 1); /* repeat until only 1 result left */ | |
487 | } | |
488 | ||
489 | ||
490 | /* | |
491 | ** Main operation 'ra' = #rb'. | |
492 | */ | |
493 | void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { | |
494 | const TValue *tm; | |
495 | switch (ttype(rb)) { | |
496 | case LUA_TTABLE: { | |
497 | Table *h = hvalue(rb); | |
498 | tm = fasttm(L, h->metatable, TM_LEN); | |
499 | if (tm) break; /* metamethod? break switch to call it */ | |
500 | setivalue(ra, luaH_getn(h)); /* else primitive len */ | |
501 | return; | |
502 | } | |
503 | case LUA_TSHRSTR: { | |
504 | setivalue(ra, tsvalue(rb)->shrlen); | |
505 | return; | |
506 | } | |
507 | case LUA_TLNGSTR: { | |
508 | setivalue(ra, tsvalue(rb)->u.lnglen); | |
509 | return; | |
510 | } | |
511 | default: { /* try metamethod */ | |
512 | tm = luaT_gettmbyobj(L, rb, TM_LEN); | |
513 | if (ttisnil(tm)) /* no metamethod? */ | |
514 | luaG_typeerror(L, rb, "get length of"); | |
515 | break; | |
516 | } | |
517 | } | |
518 | luaT_callTM(L, tm, rb, rb, ra, 1); | |
519 | } | |
520 | ||
521 | ||
522 | /* | |
523 | ** Integer division; return 'm // n', that is, floor(m/n). | |
524 | ** C division truncates its result (rounds towards zero). | |
525 | ** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer, | |
526 | ** otherwise 'floor(q) == trunc(q) - 1'. | |
527 | */ | |
528 | lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) { | |
529 | if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */ | |
530 | if (n == 0) | |
531 | luaG_runerror(L, "attempt to divide by zero"); | |
532 | return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */ | |
533 | } | |
534 | else { | |
535 | lua_Integer q = m / n; /* perform C division */ | |
536 | if ((m ^ n) < 0 && m % n != 0) /* 'm/n' would be negative non-integer? */ | |
537 | q -= 1; /* correct result for different rounding */ | |
538 | return q; | |
539 | } | |
540 | } | |
541 | ||
542 | ||
543 | /* | |
544 | ** Integer modulus; return 'm % n'. (Assume that C '%' with | |
545 | ** negative operands follows C99 behavior. See previous comment | |
546 | ** about luaV_div.) | |
547 | */ | |
548 | lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) { | |
549 | if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */ | |
550 | if (n == 0) | |
551 | luaG_runerror(L, "attempt to perform 'n%%0'"); | |
552 | return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */ | |
553 | } | |
554 | else { | |
555 | lua_Integer r = m % n; | |
556 | if (r != 0 && (m ^ n) < 0) /* 'm/n' would be non-integer negative? */ | |
557 | r += n; /* correct result for different rounding */ | |
558 | return r; | |
559 | } | |
560 | } | |
561 | ||
562 | ||
563 | /* number of bits in an integer */ | |
564 | #define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT) | |
565 | ||
566 | /* | |
567 | ** Shift left operation. (Shift right just negates 'y'.) | |
568 | */ | |
569 | lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { | |
570 | if (y < 0) { /* shift right? */ | |
571 | if (y <= -NBITS) return 0; | |
572 | else return intop(>>, x, -y); | |
573 | } | |
574 | else { /* shift left */ | |
575 | if (y >= NBITS) return 0; | |
576 | else return intop(<<, x, y); | |
577 | } | |
578 | } | |
579 | ||
580 | ||
581 | /* | |
582 | ** check whether cached closure in prototype 'p' may be reused, that is, | |
583 | ** whether there is a cached closure with the same upvalues needed by | |
584 | ** new closure to be created. | |
585 | */ | |
586 | static LClosure *getcached (Proto *p, UpVal **encup, StkId base) { | |
587 | LClosure *c = p->cache; | |
588 | if (c != NULL) { /* is there a cached closure? */ | |
589 | int nup = p->sizeupvalues; | |
590 | Upvaldesc *uv = p->upvalues; | |
591 | int i; | |
592 | for (i = 0; i < nup; i++) { /* check whether it has right upvalues */ | |
593 | TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v; | |
594 | if (c->upvals[i]->v != v) | |
595 | return NULL; /* wrong upvalue; cannot reuse closure */ | |
596 | } | |
597 | } | |
598 | return c; /* return cached closure (or NULL if no cached closure) */ | |
599 | } | |
600 | ||
601 | ||
602 | /* | |
603 | ** create a new Lua closure, push it in the stack, and initialize | |
604 | ** its upvalues. Note that the closure is not cached if prototype is | |
605 | ** already black (which means that 'cache' was already cleared by the | |
606 | ** GC). | |
607 | */ | |
608 | static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, | |
609 | StkId ra) { | |
610 | int nup = p->sizeupvalues; | |
611 | Upvaldesc *uv = p->upvalues; | |
612 | int i; | |
613 | LClosure *ncl = luaF_newLclosure(L, nup); | |
614 | ncl->p = p; | |
615 | setclLvalue(L, ra, ncl); /* anchor new closure in stack */ | |
616 | for (i = 0; i < nup; i++) { /* fill in its upvalues */ | |
617 | if (uv[i].instack) /* upvalue refers to local variable? */ | |
618 | ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx); | |
619 | else /* get upvalue from enclosing function */ | |
620 | ncl->upvals[i] = encup[uv[i].idx]; | |
621 | ncl->upvals[i]->refcount++; | |
622 | /* new closure is white, so we do not need a barrier here */ | |
623 | } | |
624 | if (!isblack(p)) /* cache will not break GC invariant? */ | |
625 | p->cache = ncl; /* save it on cache for reuse */ | |
626 | } | |
627 | ||
628 | ||
629 | /* | |
630 | ** finish execution of an opcode interrupted by an yield | |
631 | */ | |
632 | void luaV_finishOp (lua_State *L) { | |
633 | CallInfo *ci = L->ci; | |
634 | StkId base = ci->u.l.base; | |
635 | Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ | |
636 | OpCode op = GET_OPCODE(inst); | |
637 | switch (op) { /* finish its execution */ | |
638 | case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_IDIV: | |
639 | case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: | |
640 | case OP_MOD: case OP_POW: | |
641 | case OP_UNM: case OP_BNOT: case OP_LEN: | |
642 | case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: { | |
643 | setobjs2s(L, base + GETARG_A(inst), --L->top); | |
644 | break; | |
645 | } | |
646 | case OP_LE: case OP_LT: case OP_EQ: { | |
647 | int res = !l_isfalse(L->top - 1); | |
648 | L->top--; | |
649 | if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ | |
650 | lua_assert(op == OP_LE); | |
651 | ci->callstatus ^= CIST_LEQ; /* clear mark */ | |
652 | res = !res; /* negate result */ | |
653 | } | |
654 | lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); | |
655 | if (res != GETARG_A(inst)) /* condition failed? */ | |
656 | ci->u.l.savedpc++; /* skip jump instruction */ | |
657 | break; | |
658 | } | |
659 | case OP_CONCAT: { | |
660 | StkId top = L->top - 1; /* top when 'luaT_trybinTM' was called */ | |
661 | int b = GETARG_B(inst); /* first element to concatenate */ | |
662 | int total = cast_int(top - 1 - (base + b)); /* yet to concatenate */ | |
663 | setobj2s(L, top - 2, top); /* put TM result in proper position */ | |
664 | if (total > 1) { /* are there elements to concat? */ | |
665 | L->top = top - 1; /* top is one after last element (at top-2) */ | |
666 | luaV_concat(L, total); /* concat them (may yield again) */ | |
667 | } | |
668 | /* move final result to final position */ | |
669 | setobj2s(L, ci->u.l.base + GETARG_A(inst), L->top - 1); | |
670 | L->top = ci->top; /* restore top */ | |
671 | break; | |
672 | } | |
673 | case OP_TFORCALL: { | |
674 | lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP); | |
675 | L->top = ci->top; /* correct top */ | |
676 | break; | |
677 | } | |
678 | case OP_CALL: { | |
679 | if (GETARG_C(inst) - 1 >= 0) /* nresults >= 0? */ | |
680 | L->top = ci->top; /* adjust results */ | |
681 | break; | |
682 | } | |
683 | case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE: | |
684 | break; | |
685 | default: lua_assert(0); | |
686 | } | |
687 | } | |
688 | ||
689 | ||
690 | ||
691 | ||
692 | /* | |
693 | ** {================================================================== | |
694 | ** Function 'luaV_execute': main interpreter loop | |
695 | ** =================================================================== | |
696 | */ | |
697 | ||
698 | ||
699 | /* | |
700 | ** some macros for common tasks in 'luaV_execute' | |
701 | */ | |
702 | ||
703 | #if !defined(luai_runtimecheck) | |
704 | #define luai_runtimecheck(L, c) /* void */ | |
705 | #endif | |
706 | ||
707 | ||
708 | #define RA(i) (base+GETARG_A(i)) | |
709 | /* to be used after possible stack reallocation */ | |
710 | #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i)) | |
711 | #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i)) | |
712 | #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \ | |
713 | ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i)) | |
714 | #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ | |
715 | ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i)) | |
716 | #define KBx(i) \ | |
717 | (k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++))) | |
718 | ||
719 | ||
720 | /* execute a jump instruction */ | |
721 | #define dojump(ci,i,e) \ | |
722 | { int a = GETARG_A(i); \ | |
723 | if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \ | |
724 | ci->u.l.savedpc += GETARG_sBx(i) + e; } | |
725 | ||
726 | /* for test instructions, execute the jump instruction that follows it */ | |
727 | #define donextjump(ci) { i = *ci->u.l.savedpc; dojump(ci, i, 1); } | |
728 | ||
729 | ||
730 | #define Protect(x) { {x;}; base = ci->u.l.base; } | |
731 | ||
732 | #define checkGC(L,c) \ | |
733 | Protect( luaC_condGC(L,{L->top = (c); /* limit of live values */ \ | |
734 | luaC_step(L); \ | |
735 | L->top = ci->top;}) /* restore top */ \ | |
736 | luai_threadyield(L); ) | |
737 | ||
738 | ||
739 | #define vmdispatch(o) switch(o) | |
740 | #define vmcase(l) case l: | |
741 | #define vmbreak break | |
742 | ||
743 | void luaV_execute (lua_State *L) { | |
744 | CallInfo *ci = L->ci; | |
745 | LClosure *cl; | |
746 | TValue *k; | |
747 | StkId base; | |
748 | newframe: /* reentry point when frame changes (call/return) */ | |
749 | lua_assert(ci == L->ci); | |
750 | cl = clLvalue(ci->func); | |
751 | k = cl->p->k; | |
752 | base = ci->u.l.base; | |
753 | /* main loop of interpreter */ | |
754 | for (;;) { | |
755 | Instruction i = *(ci->u.l.savedpc++); | |
756 | StkId ra; | |
757 | if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) && | |
758 | (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) { | |
759 | Protect(luaG_traceexec(L)); | |
760 | } | |
761 | /* WARNING: several calls may realloc the stack and invalidate 'ra' */ | |
762 | ra = RA(i); | |
763 | lua_assert(base == ci->u.l.base); | |
764 | lua_assert(base <= L->top && L->top < L->stack + L->stacksize); | |
765 | vmdispatch (GET_OPCODE(i)) { | |
766 | vmcase(OP_MOVE) { | |
767 | setobjs2s(L, ra, RB(i)); | |
768 | vmbreak; | |
769 | } | |
770 | vmcase(OP_LOADK) { | |
771 | TValue *rb = k + GETARG_Bx(i); | |
772 | setobj2s(L, ra, rb); | |
773 | vmbreak; | |
774 | } | |
775 | vmcase(OP_LOADKX) { | |
776 | TValue *rb; | |
777 | lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG); | |
778 | rb = k + GETARG_Ax(*ci->u.l.savedpc++); | |
779 | setobj2s(L, ra, rb); | |
780 | vmbreak; | |
781 | } | |
782 | vmcase(OP_LOADBOOL) { | |
783 | setbvalue(ra, GETARG_B(i)); | |
784 | if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */ | |
785 | vmbreak; | |
786 | } | |
787 | vmcase(OP_LOADNIL) { | |
788 | int b = GETARG_B(i); | |
789 | do { | |
790 | setnilvalue(ra++); | |
791 | } while (b--); | |
792 | vmbreak; | |
793 | } | |
794 | vmcase(OP_GETUPVAL) { | |
795 | int b = GETARG_B(i); | |
796 | setobj2s(L, ra, cl->upvals[b]->v); | |
797 | vmbreak; | |
798 | } | |
799 | vmcase(OP_GETTABUP) { | |
800 | int b = GETARG_B(i); | |
801 | Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra)); | |
802 | vmbreak; | |
803 | } | |
804 | vmcase(OP_GETTABLE) { | |
805 | Protect(luaV_gettable(L, RB(i), RKC(i), ra)); | |
806 | vmbreak; | |
807 | } | |
808 | vmcase(OP_SETTABUP) { | |
809 | int a = GETARG_A(i); | |
810 | Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i))); | |
811 | vmbreak; | |
812 | } | |
813 | vmcase(OP_SETUPVAL) { | |
814 | UpVal *uv = cl->upvals[GETARG_B(i)]; | |
815 | setobj(L, uv->v, ra); | |
816 | luaC_upvalbarrier(L, uv); | |
817 | vmbreak; | |
818 | } | |
819 | vmcase(OP_SETTABLE) { | |
820 | Protect(luaV_settable(L, ra, RKB(i), RKC(i))); | |
821 | vmbreak; | |
822 | } | |
823 | vmcase(OP_NEWTABLE) { | |
824 | int b = GETARG_B(i); | |
825 | int c = GETARG_C(i); | |
826 | Table *t = luaH_new(L); | |
827 | sethvalue(L, ra, t); | |
828 | if (b != 0 || c != 0) | |
829 | luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c)); | |
830 | checkGC(L, ra + 1); | |
831 | vmbreak; | |
832 | } | |
833 | vmcase(OP_SELF) { | |
834 | StkId rb = RB(i); | |
835 | setobjs2s(L, ra+1, rb); | |
836 | Protect(luaV_gettable(L, rb, RKC(i), ra)); | |
837 | vmbreak; | |
838 | } | |
839 | vmcase(OP_ADD) { | |
840 | TValue *rb = RKB(i); | |
841 | TValue *rc = RKC(i); | |
842 | lua_Number nb; lua_Number nc; | |
843 | if (ttisinteger(rb) && ttisinteger(rc)) { | |
844 | lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); | |
845 | setivalue(ra, intop(+, ib, ic)); | |
846 | } | |
847 | else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { | |
848 | setfltvalue(ra, luai_numadd(L, nb, nc)); | |
849 | } | |
850 | else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); } | |
851 | vmbreak; | |
852 | } | |
853 | vmcase(OP_SUB) { | |
854 | TValue *rb = RKB(i); | |
855 | TValue *rc = RKC(i); | |
856 | lua_Number nb; lua_Number nc; | |
857 | if (ttisinteger(rb) && ttisinteger(rc)) { | |
858 | lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); | |
859 | setivalue(ra, intop(-, ib, ic)); | |
860 | } | |
861 | else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { | |
862 | setfltvalue(ra, luai_numsub(L, nb, nc)); | |
863 | } | |
864 | else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); } | |
865 | vmbreak; | |
866 | } | |
867 | vmcase(OP_MUL) { | |
868 | TValue *rb = RKB(i); | |
869 | TValue *rc = RKC(i); | |
870 | lua_Number nb; lua_Number nc; | |
871 | if (ttisinteger(rb) && ttisinteger(rc)) { | |
872 | lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); | |
873 | setivalue(ra, intop(*, ib, ic)); | |
874 | } | |
875 | else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { | |
876 | setfltvalue(ra, luai_nummul(L, nb, nc)); | |
877 | } | |
878 | else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); } | |
879 | vmbreak; | |
880 | } | |
881 | vmcase(OP_DIV) { /* float division (always with floats) */ | |
882 | TValue *rb = RKB(i); | |
883 | TValue *rc = RKC(i); | |
884 | lua_Number nb; lua_Number nc; | |
885 | if (tonumber(rb, &nb) && tonumber(rc, &nc)) { | |
886 | setfltvalue(ra, luai_numdiv(L, nb, nc)); | |
887 | } | |
888 | else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); } | |
889 | vmbreak; | |
890 | } | |
891 | vmcase(OP_BAND) { | |
892 | TValue *rb = RKB(i); | |
893 | TValue *rc = RKC(i); | |
894 | lua_Integer ib; lua_Integer ic; | |
895 | if (tointeger(rb, &ib) && tointeger(rc, &ic)) { | |
896 | setivalue(ra, intop(&, ib, ic)); | |
897 | } | |
898 | else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); } | |
899 | vmbreak; | |
900 | } | |
901 | vmcase(OP_BOR) { | |
902 | TValue *rb = RKB(i); | |
903 | TValue *rc = RKC(i); | |
904 | lua_Integer ib; lua_Integer ic; | |
905 | if (tointeger(rb, &ib) && tointeger(rc, &ic)) { | |
906 | setivalue(ra, intop(|, ib, ic)); | |
907 | } | |
908 | else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); } | |
909 | vmbreak; | |
910 | } | |
911 | vmcase(OP_BXOR) { | |
912 | TValue *rb = RKB(i); | |
913 | TValue *rc = RKC(i); | |
914 | lua_Integer ib; lua_Integer ic; | |
915 | if (tointeger(rb, &ib) && tointeger(rc, &ic)) { | |
916 | setivalue(ra, intop(^, ib, ic)); | |
917 | } | |
918 | else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); } | |
919 | vmbreak; | |
920 | } | |
921 | vmcase(OP_SHL) { | |
922 | TValue *rb = RKB(i); | |
923 | TValue *rc = RKC(i); | |
924 | lua_Integer ib; lua_Integer ic; | |
925 | if (tointeger(rb, &ib) && tointeger(rc, &ic)) { | |
926 | setivalue(ra, luaV_shiftl(ib, ic)); | |
927 | } | |
928 | else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); } | |
929 | vmbreak; | |
930 | } | |
931 | vmcase(OP_SHR) { | |
932 | TValue *rb = RKB(i); | |
933 | TValue *rc = RKC(i); | |
934 | lua_Integer ib; lua_Integer ic; | |
935 | if (tointeger(rb, &ib) && tointeger(rc, &ic)) { | |
936 | setivalue(ra, luaV_shiftl(ib, -ic)); | |
937 | } | |
938 | else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); } | |
939 | vmbreak; | |
940 | } | |
941 | vmcase(OP_MOD) { | |
942 | TValue *rb = RKB(i); | |
943 | TValue *rc = RKC(i); | |
944 | lua_Number nb; lua_Number nc; | |
945 | if (ttisinteger(rb) && ttisinteger(rc)) { | |
946 | lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); | |
947 | setivalue(ra, luaV_mod(L, ib, ic)); | |
948 | } | |
949 | else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { | |
950 | lua_Number m; | |
951 | luai_nummod(L, nb, nc, m); | |
952 | setfltvalue(ra, m); | |
953 | } | |
954 | else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); } | |
955 | vmbreak; | |
956 | } | |
957 | vmcase(OP_IDIV) { /* floor division */ | |
958 | TValue *rb = RKB(i); | |
959 | TValue *rc = RKC(i); | |
960 | lua_Number nb; lua_Number nc; | |
961 | if (ttisinteger(rb) && ttisinteger(rc)) { | |
962 | lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); | |
963 | setivalue(ra, luaV_div(L, ib, ic)); | |
964 | } | |
965 | else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { | |
966 | setfltvalue(ra, luai_numidiv(L, nb, nc)); | |
967 | } | |
968 | else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); } | |
969 | vmbreak; | |
970 | } | |
971 | vmcase(OP_POW) { | |
972 | TValue *rb = RKB(i); | |
973 | TValue *rc = RKC(i); | |
974 | lua_Number nb; lua_Number nc; | |
975 | if (tonumber(rb, &nb) && tonumber(rc, &nc)) { | |
976 | setfltvalue(ra, luai_numpow(L, nb, nc)); | |
977 | } | |
978 | else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); } | |
979 | vmbreak; | |
980 | } | |
981 | vmcase(OP_UNM) { | |
982 | TValue *rb = RB(i); | |
983 | lua_Number nb; | |
984 | if (ttisinteger(rb)) { | |
985 | lua_Integer ib = ivalue(rb); | |
986 | setivalue(ra, intop(-, 0, ib)); | |
987 | } | |
988 | else if (tonumber(rb, &nb)) { | |
989 | setfltvalue(ra, luai_numunm(L, nb)); | |
990 | } | |
991 | else { | |
992 | Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM)); | |
993 | } | |
994 | vmbreak; | |
995 | } | |
996 | vmcase(OP_BNOT) { | |
997 | TValue *rb = RB(i); | |
998 | lua_Integer ib; | |
999 | if (tointeger(rb, &ib)) { | |
1000 | setivalue(ra, intop(^, ~l_castS2U(0), ib)); | |
1001 | } | |
1002 | else { | |
1003 | Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); | |
1004 | } | |
1005 | vmbreak; | |
1006 | } | |
1007 | vmcase(OP_NOT) { | |
1008 | TValue *rb = RB(i); | |
1009 | int res = l_isfalse(rb); /* next assignment may change this value */ | |
1010 | setbvalue(ra, res); | |
1011 | vmbreak; | |
1012 | } | |
1013 | vmcase(OP_LEN) { | |
1014 | Protect(luaV_objlen(L, ra, RB(i))); | |
1015 | vmbreak; | |
1016 | } | |
1017 | vmcase(OP_CONCAT) { | |
1018 | int b = GETARG_B(i); | |
1019 | int c = GETARG_C(i); | |
1020 | StkId rb; | |
1021 | L->top = base + c + 1; /* mark the end of concat operands */ | |
1022 | Protect(luaV_concat(L, c - b + 1)); | |
1023 | ra = RA(i); /* 'luav_concat' may invoke TMs and move the stack */ | |
1024 | rb = base + b; | |
1025 | setobjs2s(L, ra, rb); | |
1026 | checkGC(L, (ra >= rb ? ra + 1 : rb)); | |
1027 | L->top = ci->top; /* restore top */ | |
1028 | vmbreak; | |
1029 | } | |
1030 | vmcase(OP_JMP) { | |
1031 | dojump(ci, i, 0); | |
1032 | vmbreak; | |
1033 | } | |
1034 | vmcase(OP_EQ) { | |
1035 | TValue *rb = RKB(i); | |
1036 | TValue *rc = RKC(i); | |
1037 | Protect( | |
1038 | if (cast_int(luaV_equalobj(L, rb, rc)) != GETARG_A(i)) | |
1039 | ci->u.l.savedpc++; | |
1040 | else | |
1041 | donextjump(ci); | |
1042 | ) | |
1043 | vmbreak; | |
1044 | } | |
1045 | vmcase(OP_LT) { | |
1046 | Protect( | |
1047 | if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i)) | |
1048 | ci->u.l.savedpc++; | |
1049 | else | |
1050 | donextjump(ci); | |
1051 | ) | |
1052 | vmbreak; | |
1053 | } | |
1054 | vmcase(OP_LE) { | |
1055 | Protect( | |
1056 | if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i)) | |
1057 | ci->u.l.savedpc++; | |
1058 | else | |
1059 | donextjump(ci); | |
1060 | ) | |
1061 | vmbreak; | |
1062 | } | |
1063 | vmcase(OP_TEST) { | |
1064 | if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra)) | |
1065 | ci->u.l.savedpc++; | |
1066 | else | |
1067 | donextjump(ci); | |
1068 | vmbreak; | |
1069 | } | |
1070 | vmcase(OP_TESTSET) { | |
1071 | TValue *rb = RB(i); | |
1072 | if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb)) | |
1073 | ci->u.l.savedpc++; | |
1074 | else { | |
1075 | setobjs2s(L, ra, rb); | |
1076 | donextjump(ci); | |
1077 | } | |
1078 | vmbreak; | |
1079 | } | |
1080 | vmcase(OP_CALL) { | |
1081 | int b = GETARG_B(i); | |
1082 | int nresults = GETARG_C(i) - 1; | |
1083 | if (b != 0) L->top = ra+b; /* else previous instruction set top */ | |
1084 | if (luaD_precall(L, ra, nresults)) { /* C function? */ | |
1085 | if (nresults >= 0) L->top = ci->top; /* adjust results */ | |
1086 | base = ci->u.l.base; | |
1087 | } | |
1088 | else { /* Lua function */ | |
1089 | ci = L->ci; | |
1090 | ci->callstatus |= CIST_REENTRY; | |
1091 | goto newframe; /* restart luaV_execute over new Lua function */ | |
1092 | } | |
1093 | vmbreak; | |
1094 | } | |
1095 | vmcase(OP_TAILCALL) { | |
1096 | int b = GETARG_B(i); | |
1097 | if (b != 0) L->top = ra+b; /* else previous instruction set top */ | |
1098 | lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); | |
1099 | if (luaD_precall(L, ra, LUA_MULTRET)) /* C function? */ | |
1100 | base = ci->u.l.base; | |
1101 | else { | |
1102 | /* tail call: put called frame (n) in place of caller one (o) */ | |
1103 | CallInfo *nci = L->ci; /* called frame */ | |
1104 | CallInfo *oci = nci->previous; /* caller frame */ | |
1105 | StkId nfunc = nci->func; /* called function */ | |
1106 | StkId ofunc = oci->func; /* caller function */ | |
1107 | /* last stack slot filled by 'precall' */ | |
1108 | StkId lim = nci->u.l.base + getproto(nfunc)->numparams; | |
1109 | int aux; | |
1110 | /* close all upvalues from previous call */ | |
1111 | if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base); | |
1112 | /* move new frame into old one */ | |
1113 | for (aux = 0; nfunc + aux < lim; aux++) | |
1114 | setobjs2s(L, ofunc + aux, nfunc + aux); | |
1115 | oci->u.l.base = ofunc + (nci->u.l.base - nfunc); /* correct base */ | |
1116 | oci->top = L->top = ofunc + (L->top - nfunc); /* correct top */ | |
1117 | oci->u.l.savedpc = nci->u.l.savedpc; | |
1118 | oci->callstatus |= CIST_TAIL; /* function was tail called */ | |
1119 | ci = L->ci = oci; /* remove new frame */ | |
1120 | lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize); | |
1121 | goto newframe; /* restart luaV_execute over new Lua function */ | |
1122 | } | |
1123 | vmbreak; | |
1124 | } | |
1125 | vmcase(OP_RETURN) { | |
1126 | int b = GETARG_B(i); | |
1127 | if (cl->p->sizep > 0) luaF_close(L, base); | |
1128 | b = luaD_poscall(L, ra, (b != 0 ? b - 1 : L->top - ra)); | |
1129 | if (!(ci->callstatus & CIST_REENTRY)) /* 'ci' still the called one */ | |
1130 | return; /* external invocation: return */ | |
1131 | else { /* invocation via reentry: continue execution */ | |
1132 | ci = L->ci; | |
1133 | if (b) L->top = ci->top; | |
1134 | lua_assert(isLua(ci)); | |
1135 | lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL); | |
1136 | goto newframe; /* restart luaV_execute over new Lua function */ | |
1137 | } | |
1138 | } | |
1139 | vmcase(OP_FORLOOP) { | |
1140 | if (ttisinteger(ra)) { /* integer loop? */ | |
1141 | lua_Integer step = ivalue(ra + 2); | |
1142 | lua_Integer idx = ivalue(ra) + step; /* increment index */ | |
1143 | lua_Integer limit = ivalue(ra + 1); | |
1144 | if ((0 < step) ? (idx <= limit) : (limit <= idx)) { | |
1145 | ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ | |
1146 | chgivalue(ra, idx); /* update internal index... */ | |
1147 | setivalue(ra + 3, idx); /* ...and external index */ | |
1148 | } | |
1149 | } | |
1150 | else { /* floating loop */ | |
1151 | lua_Number step = fltvalue(ra + 2); | |
1152 | lua_Number idx = luai_numadd(L, fltvalue(ra), step); /* inc. index */ | |
1153 | lua_Number limit = fltvalue(ra + 1); | |
1154 | if (luai_numlt(0, step) ? luai_numle(idx, limit) | |
1155 | : luai_numle(limit, idx)) { | |
1156 | ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ | |
1157 | chgfltvalue(ra, idx); /* update internal index... */ | |
1158 | setfltvalue(ra + 3, idx); /* ...and external index */ | |
1159 | } | |
1160 | } | |
1161 | vmbreak; | |
1162 | } | |
1163 | vmcase(OP_FORPREP) { | |
1164 | TValue *init = ra; | |
1165 | TValue *plimit = ra + 1; | |
1166 | TValue *pstep = ra + 2; | |
1167 | lua_Integer ilimit; | |
1168 | int stopnow; | |
1169 | if (ttisinteger(init) && ttisinteger(pstep) && | |
1170 | forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) { | |
1171 | /* all values are integer */ | |
1172 | lua_Integer initv = (stopnow ? 0 : ivalue(init)); | |
1173 | setivalue(plimit, ilimit); | |
1174 | setivalue(init, initv - ivalue(pstep)); | |
1175 | } | |
1176 | else { /* try making all values floats */ | |
1177 | lua_Number ninit; lua_Number nlimit; lua_Number nstep; | |
1178 | if (!tonumber(plimit, &nlimit)) | |
1179 | luaG_runerror(L, "'for' limit must be a number"); | |
1180 | setfltvalue(plimit, nlimit); | |
1181 | if (!tonumber(pstep, &nstep)) | |
1182 | luaG_runerror(L, "'for' step must be a number"); | |
1183 | setfltvalue(pstep, nstep); | |
1184 | if (!tonumber(init, &ninit)) | |
1185 | luaG_runerror(L, "'for' initial value must be a number"); | |
1186 | setfltvalue(init, luai_numsub(L, ninit, nstep)); | |
1187 | } | |
1188 | ci->u.l.savedpc += GETARG_sBx(i); | |
1189 | vmbreak; | |
1190 | } | |
1191 | vmcase(OP_TFORCALL) { | |
1192 | StkId cb = ra + 3; /* call base */ | |
1193 | setobjs2s(L, cb+2, ra+2); | |
1194 | setobjs2s(L, cb+1, ra+1); | |
1195 | setobjs2s(L, cb, ra); | |
1196 | L->top = cb + 3; /* func. + 2 args (state and index) */ | |
1197 | Protect(luaD_call(L, cb, GETARG_C(i), 1)); | |
1198 | L->top = ci->top; | |
1199 | i = *(ci->u.l.savedpc++); /* go to next instruction */ | |
1200 | ra = RA(i); | |
1201 | lua_assert(GET_OPCODE(i) == OP_TFORLOOP); | |
1202 | goto l_tforloop; | |
1203 | } | |
1204 | vmcase(OP_TFORLOOP) { | |
1205 | l_tforloop: | |
1206 | if (!ttisnil(ra + 1)) { /* continue loop? */ | |
1207 | setobjs2s(L, ra, ra + 1); /* save control variable */ | |
1208 | ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ | |
1209 | } | |
1210 | vmbreak; | |
1211 | } | |
1212 | vmcase(OP_SETLIST) { | |
1213 | int n = GETARG_B(i); | |
1214 | int c = GETARG_C(i); | |
1215 | unsigned int last; | |
1216 | Table *h; | |
1217 | if (n == 0) n = cast_int(L->top - ra) - 1; | |
1218 | if (c == 0) { | |
1219 | lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG); | |
1220 | c = GETARG_Ax(*ci->u.l.savedpc++); | |
1221 | } | |
1222 | luai_runtimecheck(L, ttistable(ra)); | |
1223 | h = hvalue(ra); | |
1224 | last = ((c-1)*LFIELDS_PER_FLUSH) + n; | |
1225 | if (last > h->sizearray) /* needs more space? */ | |
1226 | luaH_resizearray(L, h, last); /* pre-allocate it at once */ | |
1227 | for (; n > 0; n--) { | |
1228 | TValue *val = ra+n; | |
1229 | luaH_setint(L, h, last--, val); | |
1230 | luaC_barrierback(L, h, val); | |
1231 | } | |
1232 | L->top = ci->top; /* correct top (in case of previous open call) */ | |
1233 | vmbreak; | |
1234 | } | |
1235 | vmcase(OP_CLOSURE) { | |
1236 | Proto *p = cl->p->p[GETARG_Bx(i)]; | |
1237 | LClosure *ncl = getcached(p, cl->upvals, base); /* cached closure */ | |
1238 | if (ncl == NULL) /* no match? */ | |
1239 | pushclosure(L, p, cl->upvals, base, ra); /* create a new one */ | |
1240 | else | |
1241 | setclLvalue(L, ra, ncl); /* push cashed closure */ | |
1242 | checkGC(L, ra + 1); | |
1243 | vmbreak; | |
1244 | } | |
1245 | vmcase(OP_VARARG) { | |
1246 | int b = GETARG_B(i) - 1; | |
1247 | int j; | |
1248 | int n = cast_int(base - ci->func) - cl->p->numparams - 1; | |
1249 | if (b < 0) { /* B == 0? */ | |
1250 | b = n; /* get all var. arguments */ | |
1251 | Protect(luaD_checkstack(L, n)); | |
1252 | ra = RA(i); /* previous call may change the stack */ | |
1253 | L->top = ra + n; | |
1254 | } | |
1255 | for (j = 0; j < b; j++) { | |
1256 | if (j < n) { | |
1257 | setobjs2s(L, ra + j, base - n + j); | |
1258 | } | |
1259 | else { | |
1260 | setnilvalue(ra + j); | |
1261 | } | |
1262 | } | |
1263 | vmbreak; | |
1264 | } | |
1265 | vmcase(OP_EXTRAARG) { | |
1266 | lua_assert(0); | |
1267 | vmbreak; | |
1268 | } | |
1269 | } | |
1270 | } | |
1271 | } | |
1272 | ||
1273 | /* }================================================================== */ | |
1274 |