aboutsummaryrefslogtreecommitdiff
path: root/lvm.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--lvm.c107
1 files changed, 60 insertions, 47 deletions
diff --git a/lvm.c b/lvm.c
index 4d71cfff..0a6d6270 100644
--- a/lvm.c
+++ b/lvm.c
@@ -93,7 +93,9 @@ static int l_strton (const TValue *obj, TValue *result) {
93 return 0; 93 return 0;
94 else { 94 else {
95 TString *st = tsvalue(obj); 95 TString *st = tsvalue(obj);
96 return (luaO_str2num(getstr(st), result) == tsslen(st) + 1); 96 size_t stlen;
97 const char *s = getlstr(st, stlen);
98 return (luaO_str2num(s, result) == stlen + 1);
97 } 99 }
98} 100}
99 101
@@ -198,12 +200,15 @@ static int forlimit (lua_State *L, lua_Integer init, const TValue *lim,
198 200
199/* 201/*
200** Prepare a numerical for loop (opcode OP_FORPREP). 202** Prepare a numerical for loop (opcode OP_FORPREP).
203** Before execution, stack is as follows:
204** ra : initial value
205** ra + 1 : limit
206** ra + 2 : step
201** Return true to skip the loop. Otherwise, 207** Return true to skip the loop. Otherwise,
202** after preparation, stack will be as follows: 208** after preparation, stack will be as follows:
203** ra : internal index (safe copy of the control variable) 209** ra : loop counter (integer loops) or limit (float loops)
204** ra + 1 : loop counter (integer loops) or limit (float loops) 210** ra + 1 : step
205** ra + 2 : step 211** ra + 2 : control variable
206** ra + 3 : control variable
207*/ 212*/
208static int forprep (lua_State *L, StkId ra) { 213static int forprep (lua_State *L, StkId ra) {
209 TValue *pinit = s2v(ra); 214 TValue *pinit = s2v(ra);
@@ -215,7 +220,6 @@ static int forprep (lua_State *L, StkId ra) {
215 lua_Integer limit; 220 lua_Integer limit;
216 if (step == 0) 221 if (step == 0)
217 luaG_runerror(L, "'for' step is zero"); 222 luaG_runerror(L, "'for' step is zero");
218 setivalue(s2v(ra + 3), init); /* control variable */
219 if (forlimit(L, init, plimit, &limit, step)) 223 if (forlimit(L, init, plimit, &limit, step))
220 return 1; /* skip the loop */ 224 return 1; /* skip the loop */
221 else { /* prepare loop counter */ 225 else { /* prepare loop counter */
@@ -230,9 +234,10 @@ static int forprep (lua_State *L, StkId ra) {
230 /* 'step+1' avoids negating 'mininteger' */ 234 /* 'step+1' avoids negating 'mininteger' */
231 count /= l_castS2U(-(step + 1)) + 1u; 235 count /= l_castS2U(-(step + 1)) + 1u;
232 } 236 }
233 /* store the counter in place of the limit (which won't be 237 /* use 'chgivalue' for places that for sure had integers */
234 needed anymore) */ 238 chgivalue(s2v(ra), l_castU2S(count)); /* change init to count */
235 setivalue(plimit, l_castU2S(count)); 239 setivalue(s2v(ra + 1), step); /* change limit to step */
240 chgivalue(s2v(ra + 2), init); /* change step to init */
236 } 241 }
237 } 242 }
238 else { /* try making all values floats */ 243 else { /* try making all values floats */
@@ -249,11 +254,10 @@ static int forprep (lua_State *L, StkId ra) {
249 : luai_numlt(init, limit)) 254 : luai_numlt(init, limit))
250 return 1; /* skip the loop */ 255 return 1; /* skip the loop */
251 else { 256 else {
252 /* make sure internal values are all floats */ 257 /* make sure all values are floats */
253 setfltvalue(plimit, limit); 258 setfltvalue(s2v(ra), limit);
254 setfltvalue(pstep, step); 259 setfltvalue(s2v(ra + 1), step);
255 setfltvalue(s2v(ra), init); /* internal index */ 260 setfltvalue(s2v(ra + 2), init); /* control variable */
256 setfltvalue(s2v(ra + 3), init); /* control variable */
257 } 261 }
258 } 262 }
259 return 0; 263 return 0;
@@ -266,14 +270,13 @@ static int forprep (lua_State *L, StkId ra) {
266** written online with opcode OP_FORLOOP, for performance.) 270** written online with opcode OP_FORLOOP, for performance.)
267*/ 271*/
268static int floatforloop (StkId ra) { 272static int floatforloop (StkId ra) {
269 lua_Number step = fltvalue(s2v(ra + 2)); 273 lua_Number step = fltvalue(s2v(ra + 1));
270 lua_Number limit = fltvalue(s2v(ra + 1)); 274 lua_Number limit = fltvalue(s2v(ra));
271 lua_Number idx = fltvalue(s2v(ra)); /* internal index */ 275 lua_Number idx = fltvalue(s2v(ra + 2)); /* control variable */
272 idx = luai_numadd(L, idx, step); /* increment index */ 276 idx = luai_numadd(L, idx, step); /* increment index */
273 if (luai_numlt(0, step) ? luai_numle(idx, limit) 277 if (luai_numlt(0, step) ? luai_numle(idx, limit)
274 : luai_numle(limit, idx)) { 278 : luai_numle(limit, idx)) {
275 chgfltvalue(s2v(ra), idx); /* update internal index */ 279 chgfltvalue(s2v(ra + 2), idx); /* update control variable */
276 setfltvalue(s2v(ra + 3), idx); /* and control variable */
277 return 1; /* jump back */ 280 return 1; /* jump back */
278 } 281 }
279 else 282 else
@@ -376,10 +379,10 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
376** have different lengths. 379** have different lengths.
377*/ 380*/
378static int l_strcmp (const TString *ts1, const TString *ts2) { 381static int l_strcmp (const TString *ts1, const TString *ts2) {
379 const char *s1 = getstr(ts1); 382 size_t rl1; /* real length */
380 size_t rl1 = tsslen(ts1); /* real length */ 383 const char *s1 = getlstr(ts1, rl1);
381 const char *s2 = getstr(ts2); 384 size_t rl2;
382 size_t rl2 = tsslen(ts2); 385 const char *s2 = getlstr(ts2, rl2);
383 for (;;) { /* for each segment */ 386 for (;;) { /* for each segment */
384 int temp = strcoll(s1, s2); 387 int temp = strcoll(s1, s2);
385 if (temp != 0) /* not equal? */ 388 if (temp != 0) /* not equal? */
@@ -629,8 +632,9 @@ static void copy2buff (StkId top, int n, char *buff) {
629 size_t tl = 0; /* size already copied */ 632 size_t tl = 0; /* size already copied */
630 do { 633 do {
631 TString *st = tsvalue(s2v(top - n)); 634 TString *st = tsvalue(s2v(top - n));
632 size_t l = tsslen(st); /* length of string being copied */ 635 size_t l; /* length of string being copied */
633 memcpy(buff + tl, getstr(st), l * sizeof(char)); 636 const char *s = getlstr(st, l);
637 memcpy(buff + tl, s, l * sizeof(char));
634 tl += l; 638 tl += l;
635 } while (--n > 0); 639 } while (--n > 0);
636} 640}
@@ -1783,15 +1787,14 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1783 } 1787 }
1784 vmcase(OP_FORLOOP) { 1788 vmcase(OP_FORLOOP) {
1785 StkId ra = RA(i); 1789 StkId ra = RA(i);
1786 if (ttisinteger(s2v(ra + 2))) { /* integer loop? */ 1790 if (ttisinteger(s2v(ra + 1))) { /* integer loop? */
1787 lua_Unsigned count = l_castS2U(ivalue(s2v(ra + 1))); 1791 lua_Unsigned count = l_castS2U(ivalue(s2v(ra)));
1788 if (count > 0) { /* still more iterations? */ 1792 if (count > 0) { /* still more iterations? */
1789 lua_Integer step = ivalue(s2v(ra + 2)); 1793 lua_Integer step = ivalue(s2v(ra + 1));
1790 lua_Integer idx = ivalue(s2v(ra)); /* internal index */ 1794 lua_Integer idx = ivalue(s2v(ra + 2)); /* control variable */
1791 chgivalue(s2v(ra + 1), count - 1); /* update counter */ 1795 chgivalue(s2v(ra), count - 1); /* update counter */
1792 idx = intop(+, idx, step); /* add step to index */ 1796 idx = intop(+, idx, step); /* add step to index */
1793 chgivalue(s2v(ra), idx); /* update internal index */ 1797 chgivalue(s2v(ra + 2), idx); /* update control variable */
1794 setivalue(s2v(ra + 3), idx); /* and control variable */
1795 pc -= GETARG_Bx(i); /* jump back */ 1798 pc -= GETARG_Bx(i); /* jump back */
1796 } 1799 }
1797 } 1800 }
@@ -1808,26 +1811,38 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1808 vmbreak; 1811 vmbreak;
1809 } 1812 }
1810 vmcase(OP_TFORPREP) { 1813 vmcase(OP_TFORPREP) {
1814 /* before: 'ra' has the iterator function, 'ra + 1' has the state,
1815 'ra + 2' has the initial value for the control variable, and
1816 'ra + 3' has the closing variable. This opcode then swaps the
1817 control and the closing variables and marks the closing variable
1818 as to-be-closed.
1819 */
1811 StkId ra = RA(i); 1820 StkId ra = RA(i);
1812 /* create to-be-closed upvalue (if needed) */ 1821 TValue temp; /* to swap control and closing variables */
1813 halfProtect(luaF_newtbcupval(L, ra + 3)); 1822 setobj(L, &temp, s2v(ra + 3));
1814 pc += GETARG_Bx(i); 1823 setobjs2s(L, ra + 3, ra + 2);
1815 i = *(pc++); /* go to next instruction */ 1824 setobj2s(L, ra + 2, &temp);
1825 /* create to-be-closed upvalue (if closing var. is not nil) */
1826 halfProtect(luaF_newtbcupval(L, ra + 2));
1827 pc += GETARG_Bx(i); /* go to end of the loop */
1828 i = *(pc++); /* fetch next instruction */
1816 lua_assert(GET_OPCODE(i) == OP_TFORCALL && ra == RA(i)); 1829 lua_assert(GET_OPCODE(i) == OP_TFORCALL && ra == RA(i));
1817 goto l_tforcall; 1830 goto l_tforcall;
1818 } 1831 }
1819 vmcase(OP_TFORCALL) { 1832 vmcase(OP_TFORCALL) {
1820 l_tforcall: { 1833 l_tforcall: {
1821 StkId ra = RA(i);
1822 /* 'ra' has the iterator function, 'ra + 1' has the state, 1834 /* 'ra' has the iterator function, 'ra + 1' has the state,
1823 'ra + 2' has the control variable, and 'ra + 3' has the 1835 'ra + 2' has the closing variable, and 'ra + 3' has the control
1824 to-be-closed variable. The call will use the stack after 1836 variable. The call will use the stack starting at 'ra + 3',
1825 these values (starting at 'ra + 4') 1837 so that it preserves the first three values, and the first
1838 return will be the new value for the control variable.
1826 */ 1839 */
1827 /* push function, state, and control variable */ 1840 StkId ra = RA(i);
1828 memcpy(ra + 4, ra, 3 * sizeof(*ra)); 1841 setobjs2s(L, ra + 5, ra + 3); /* copy the control variable */
1829 L->top.p = ra + 4 + 3; 1842 setobjs2s(L, ra + 4, ra + 1); /* copy state */
1830 ProtectNT(luaD_call(L, ra + 4, GETARG_C(i))); /* do the call */ 1843 setobjs2s(L, ra + 3, ra); /* copy function */
1844 L->top.p = ra + 3 + 3;
1845 ProtectNT(luaD_call(L, ra + 3, GETARG_C(i))); /* do the call */
1831 updatestack(ci); /* stack may have changed */ 1846 updatestack(ci); /* stack may have changed */
1832 i = *(pc++); /* go to next instruction */ 1847 i = *(pc++); /* go to next instruction */
1833 lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i)); 1848 lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i));
@@ -1836,10 +1851,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
1836 vmcase(OP_TFORLOOP) { 1851 vmcase(OP_TFORLOOP) {
1837 l_tforloop: { 1852 l_tforloop: {
1838 StkId ra = RA(i); 1853 StkId ra = RA(i);
1839 if (!ttisnil(s2v(ra + 4))) { /* continue loop? */ 1854 if (!ttisnil(s2v(ra + 3))) /* continue loop? */
1840 setobjs2s(L, ra + 2, ra + 4); /* save control variable */
1841 pc -= GETARG_Bx(i); /* jump back */ 1855 pc -= GETARG_Bx(i); /* jump back */
1842 }
1843 vmbreak; 1856 vmbreak;
1844 }} 1857 }}
1845 vmcase(OP_SETLIST) { 1858 vmcase(OP_SETLIST) {