diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-04-25 16:50:02 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-04-25 16:50:02 -0300 |
| commit | bb1851ce98e80ef9fadd9d02510c3e8f0a6569f6 (patch) | |
| tree | 2b9b61b1e1c88b5e91281952e1c1bc203358d539 | |
| parent | 5951c79ae12b31556f8a64886b1d7fd0801d7889 (diff) | |
| download | lua-bb1851ce98e80ef9fadd9d02510c3e8f0a6569f6.tar.gz lua-bb1851ce98e80ef9fadd9d02510c3e8f0a6569f6.tar.bz2 lua-bb1851ce98e80ef9fadd9d02510c3e8f0a6569f6.zip | |
'for' loop uses integers when possible
| -rw-r--r-- | lvm.c | 49 |
1 files changed, 33 insertions, 16 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 2.160 2013/04/25 16:07:52 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 2.161 2013/04/25 19:12:41 roberto Exp roberto $ |
| 3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -805,27 +805,44 @@ void luaV_execute (lua_State *L) { | |||
| 805 | } | 805 | } |
| 806 | ) | 806 | ) |
| 807 | vmcase(OP_FORLOOP, | 807 | vmcase(OP_FORLOOP, |
| 808 | lua_Number step = nvalue(ra+2); | 808 | if (ttisinteger(ra)) { /* integer count? */ |
| 809 | lua_Number idx = luai_numadd(L, nvalue(ra), step); /* increment index */ | 809 | lua_Integer step = ivalue(ra + 2); |
| 810 | lua_Number limit = nvalue(ra+1); | 810 | lua_Integer idx = ivalue(ra) + step; /* increment index */ |
| 811 | if (luai_numlt(L, 0, step) ? luai_numle(L, idx, limit) | 811 | lua_Integer limit = ivalue(ra + 1); |
| 812 | : luai_numle(L, limit, idx)) { | 812 | if ((0 < step) ? (idx <= limit) : (limit <= idx)) { |
| 813 | ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ | 813 | ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ |
| 814 | setnvalue(ra, idx); /* update internal index... */ | 814 | setivalue(ra, idx); /* update internal index... */ |
| 815 | setnvalue(ra+3, idx); /* ...and external index */ | 815 | setivalue(ra + 3, idx); /* ...and external index */ |
| 816 | } | ||
| 817 | } | ||
| 818 | else { /* floating count */ | ||
| 819 | lua_Number step = fltvalue(ra + 2); | ||
| 820 | lua_Number idx = luai_numadd(L, fltvalue(ra), step); /* inc. index */ | ||
| 821 | lua_Number limit = fltvalue(ra + 1); | ||
| 822 | if (luai_numlt(L, 0, step) ? luai_numle(L, idx, limit) | ||
| 823 | : luai_numle(L, limit, idx)) { | ||
| 824 | ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ | ||
| 825 | setnvalue(ra, idx); /* update internal index... */ | ||
| 826 | setnvalue(ra + 3, idx); /* ...and external index */ | ||
| 827 | } | ||
| 816 | } | 828 | } |
| 817 | ) | 829 | ) |
| 818 | vmcase(OP_FORPREP, | 830 | vmcase(OP_FORPREP, |
| 819 | const TValue *init = ra; | 831 | const TValue *init = ra; |
| 820 | const TValue *plimit = ra+1; | 832 | const TValue *plimit = ra+1; |
| 821 | const TValue *pstep = ra+2; | 833 | const TValue *pstep = ra+2; |
| 822 | if (!tonumber(init, ra)) | 834 | if (ttisinteger(ra) && ttisinteger(ra + 1) && ttisinteger(ra + 2)) { |
| 823 | luaG_runerror(L, LUA_QL("for") " initial value must be a number"); | 835 | setivalue(ra, ivalue(ra) - ivalue(pstep)); |
| 824 | else if (!tonumber(plimit, ra+1)) | 836 | } |
| 825 | luaG_runerror(L, LUA_QL("for") " limit must be a number"); | 837 | else { /* try with floats */ |
| 826 | else if (!tonumber(pstep, ra+2)) | 838 | if (!tonumber(init, ra)) |
| 827 | luaG_runerror(L, LUA_QL("for") " step must be a number"); | 839 | luaG_runerror(L, LUA_QL("for") " initial value must be a number"); |
| 828 | setnvalue(ra, luai_numsub(L, nvalue(ra), nvalue(pstep))); | 840 | else if (!tonumber(plimit, ra+1)) |
| 841 | luaG_runerror(L, LUA_QL("for") " limit must be a number"); | ||
| 842 | else if (!tonumber(pstep, ra+2)) | ||
| 843 | luaG_runerror(L, LUA_QL("for") " step must be a number"); | ||
| 844 | setnvalue(ra, luai_numsub(L, fltvalue(ra), fltvalue(pstep))); | ||
| 845 | } | ||
| 829 | ci->u.l.savedpc += GETARG_sBx(i); | 846 | ci->u.l.savedpc += GETARG_sBx(i); |
| 830 | ) | 847 | ) |
| 831 | vmcasenb(OP_TFORCALL, | 848 | vmcasenb(OP_TFORCALL, |
