diff options
Diffstat (limited to 'lvm.c')
-rw-r--r-- | lvm.c | 42 |
1 files changed, 19 insertions, 23 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 2.162 2013/04/25 19:50:02 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 2.163 2013/04/26 13:07:53 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 | */ |
@@ -32,19 +32,14 @@ | |||
32 | #define MAXTAGLOOP 100 | 32 | #define MAXTAGLOOP 100 |
33 | 33 | ||
34 | 34 | ||
35 | const TValue *luaV_tonumber (const TValue *obj, TValue *n) { | 35 | int luaV_tonumber_ (const TValue *obj, lua_Number *n) { |
36 | lua_Number num; | 36 | lua_assert(!ttisfloat(obj)); |
37 | if (ttisfloat(obj)) return obj; | ||
38 | if (ttisinteger(obj)) { | 37 | if (ttisinteger(obj)) { |
39 | setnvalue(n, cast_num(ivalue(obj))); | 38 | *n = cast_num(ivalue(obj)); |
40 | return n; | 39 | return 1; |
41 | } | ||
42 | if (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, &num)) { | ||
43 | setnvalue(n, num); | ||
44 | return n; | ||
45 | } | 40 | } |
46 | else | 41 | else |
47 | return NULL; | 42 | return (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, n)); |
48 | } | 43 | } |
49 | 44 | ||
50 | 45 | ||
@@ -337,11 +332,9 @@ lua_Integer luaV_pow (lua_Integer x, lua_Integer y) { | |||
337 | 332 | ||
338 | void luaV_arith (lua_State *L, StkId ra, const TValue *rb, | 333 | void luaV_arith (lua_State *L, StkId ra, const TValue *rb, |
339 | const TValue *rc, TMS op) { | 334 | const TValue *rc, TMS op) { |
340 | TValue tempb, tempc; | 335 | lua_Number b, c; |
341 | const TValue *b, *c; | 336 | if (tonumber(rb, &b) && tonumber(rc, &c)) { |
342 | if ((b = luaV_tonumber(rb, &tempb)) != NULL && | 337 | lua_Number res = luaO_arith(op - TM_ADD + LUA_OPADD, b, c); |
343 | (c = luaV_tonumber(rc, &tempc)) != NULL) { | ||
344 | lua_Number res = luaO_arith(op - TM_ADD + LUA_OPADD, nvalue(b), nvalue(c)); | ||
345 | setnvalue(ra, res); | 338 | setnvalue(ra, res); |
346 | } | 339 | } |
347 | else if (!luaT_callbinTM(L, rb, rc, ra, op)) | 340 | else if (!luaT_callbinTM(L, rb, rc, ra, op)) |
@@ -841,20 +834,23 @@ void luaV_execute (lua_State *L) { | |||
841 | } | 834 | } |
842 | ) | 835 | ) |
843 | vmcase(OP_FORPREP, | 836 | vmcase(OP_FORPREP, |
844 | const TValue *init = ra; | 837 | TValue *init = ra; |
845 | const TValue *plimit = ra+1; | 838 | TValue *plimit = ra + 1; |
846 | const TValue *pstep = ra+2; | 839 | TValue *pstep = ra + 2; |
847 | if (ttisinteger(ra) && ttisinteger(ra + 1) && ttisinteger(ra + 2)) { | 840 | if (ttisinteger(ra) && ttisinteger(ra + 1) && ttisinteger(ra + 2)) { |
848 | setivalue(ra, ivalue(ra) - ivalue(pstep)); | 841 | setivalue(ra, ivalue(ra) - ivalue(pstep)); |
849 | } | 842 | } |
850 | else { /* try with floats */ | 843 | else { /* try with floats */ |
851 | if (!tonumber(init, ra)) | 844 | lua_Number ninit; lua_Number nlimit; lua_Number nstep; |
845 | if (!tonumber(init, &ninit)) | ||
852 | luaG_runerror(L, LUA_QL("for") " initial value must be a number"); | 846 | luaG_runerror(L, LUA_QL("for") " initial value must be a number"); |
853 | else if (!tonumber(plimit, ra+1)) | 847 | if (!tonumber(plimit, &nlimit)) |
854 | luaG_runerror(L, LUA_QL("for") " limit must be a number"); | 848 | luaG_runerror(L, LUA_QL("for") " limit must be a number"); |
855 | else if (!tonumber(pstep, ra+2)) | 849 | setnvalue(plimit, nlimit); |
850 | if (!tonumber(pstep, &nstep)) | ||
856 | luaG_runerror(L, LUA_QL("for") " step must be a number"); | 851 | luaG_runerror(L, LUA_QL("for") " step must be a number"); |
857 | setnvalue(ra, luai_numsub(L, fltvalue(ra), fltvalue(pstep))); | 852 | setnvalue(pstep, nstep); |
853 | setnvalue(ra, luai_numsub(L, ninit, nstep)); | ||
858 | } | 854 | } |
859 | ci->u.l.savedpc += GETARG_sBx(i); | 855 | ci->u.l.savedpc += GETARG_sBx(i); |
860 | ) | 856 | ) |