diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-04-26 13:03:50 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-04-26 13:03:50 -0300 |
commit | 4abe99dc341b2ec0cb59b8fa95006e9781938eed (patch) | |
tree | 93f9f9314d5fe985d79edd36741be97e7bf2b162 | |
parent | d4e6b750983febf3af0f09235169be9a9e9250d8 (diff) | |
download | lua-4abe99dc341b2ec0cb59b8fa95006e9781938eed.tar.gz lua-4abe99dc341b2ec0cb59b8fa95006e9781938eed.tar.bz2 lua-4abe99dc341b2ec0cb59b8fa95006e9781938eed.zip |
new interface for 'tonumber'
-rw-r--r-- | lapi.c | 18 | ||||
-rw-r--r-- | ldebug.c | 6 | ||||
-rw-r--r-- | lvm.c | 42 | ||||
-rw-r--r-- | lvm.h | 7 |
4 files changed, 34 insertions, 39 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 2.174 2013/04/25 13:52:49 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.175 2013/04/26 15:39:25 roberto Exp roberto $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -271,7 +271,7 @@ LUA_API int lua_isinteger (lua_State *L, int idx) { | |||
271 | 271 | ||
272 | 272 | ||
273 | LUA_API int lua_isnumber (lua_State *L, int idx) { | 273 | LUA_API int lua_isnumber (lua_State *L, int idx) { |
274 | TValue n; | 274 | lua_Number n; |
275 | const TValue *o = index2addr(L, idx); | 275 | const TValue *o = index2addr(L, idx); |
276 | return tonumber(o, &n); | 276 | return tonumber(o, &n); |
277 | } | 277 | } |
@@ -339,11 +339,11 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { | |||
339 | 339 | ||
340 | 340 | ||
341 | LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum) { | 341 | LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum) { |
342 | TValue n; | 342 | lua_Number n; |
343 | const TValue *o = index2addr(L, idx); | 343 | const TValue *o = index2addr(L, idx); |
344 | if (tonumber(o, &n)) { | 344 | if (tonumber(o, &n)) { |
345 | if (isnum) *isnum = 1; | 345 | if (isnum) *isnum = 1; |
346 | return nvalue(o); | 346 | return n; |
347 | } | 347 | } |
348 | else { | 348 | else { |
349 | if (isnum) *isnum = 0; | 349 | if (isnum) *isnum = 0; |
@@ -353,7 +353,7 @@ LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum) { | |||
353 | 353 | ||
354 | 354 | ||
355 | LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) { | 355 | LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) { |
356 | TValue n; | 356 | lua_Number n; |
357 | const TValue *o = index2addr(L, idx); | 357 | const TValue *o = index2addr(L, idx); |
358 | if (ttisinteger(o)) { | 358 | if (ttisinteger(o)) { |
359 | if (isnum) *isnum = 1; | 359 | if (isnum) *isnum = 1; |
@@ -361,8 +361,7 @@ LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) { | |||
361 | } | 361 | } |
362 | else if (tonumber(o, &n)) { | 362 | else if (tonumber(o, &n)) { |
363 | lua_Integer res; | 363 | lua_Integer res; |
364 | lua_Number num = nvalue(o); | 364 | lua_number2integer(res, n); |
365 | lua_number2integer(res, num); | ||
366 | if (isnum) *isnum = 1; | 365 | if (isnum) *isnum = 1; |
367 | return res; | 366 | return res; |
368 | } | 367 | } |
@@ -374,12 +373,11 @@ LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) { | |||
374 | 373 | ||
375 | 374 | ||
376 | LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *isnum) { | 375 | LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *isnum) { |
377 | TValue n; | 376 | lua_Number n; |
378 | const TValue *o = index2addr(L, idx); | 377 | const TValue *o = index2addr(L, idx); |
379 | if (tonumber(o, &n)) { | 378 | if (tonumber(o, &n)) { |
380 | lua_Unsigned res; | 379 | lua_Unsigned res; |
381 | lua_Number num = nvalue(o); | 380 | lua_number2unsigned(res, n); |
382 | lua_number2unsigned(res, num); | ||
383 | if (isnum) *isnum = 1; | 381 | if (isnum) *isnum = 1; |
384 | return res; | 382 | return res; |
385 | } | 383 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldebug.c,v 2.91 2013/04/25 15:59:42 roberto Exp roberto $ | 2 | ** $Id: ldebug.c,v 2.92 2013/04/26 13:07:53 roberto Exp roberto $ |
3 | ** Debug Interface | 3 | ** Debug Interface |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -525,8 +525,8 @@ l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2) { | |||
525 | 525 | ||
526 | 526 | ||
527 | l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) { | 527 | l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) { |
528 | TValue temp; | 528 | lua_Number temp; |
529 | if (luaV_tonumber(p1, &temp) == NULL) | 529 | if (!tonumber(p1, &temp)) |
530 | p2 = p1; /* first operand is wrong */ | 530 | p2 = p1; /* first operand is wrong */ |
531 | luaG_typeerror(L, p2, "perform arithmetic on"); | 531 | luaG_typeerror(L, p2, "perform arithmetic on"); |
532 | } | 532 | } |
@@ -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 | ) |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.h,v 2.19 2013/04/15 15:44:46 roberto Exp roberto $ | 2 | ** $Id: lvm.h,v 2.20 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 | */ |
@@ -15,7 +15,8 @@ | |||
15 | 15 | ||
16 | #define tostring(L,o) (ttisstring(o) || (luaV_tostring(L, o))) | 16 | #define tostring(L,o) (ttisstring(o) || (luaV_tostring(L, o))) |
17 | 17 | ||
18 | #define tonumber(o,n) (ttisfloat(o) || (((o) = luaV_tonumber(o,n)) != NULL)) | 18 | #define tonumber(o,n) \ |
19 | (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) | ||
19 | 20 | ||
20 | 21 | ||
21 | #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) | 22 | #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) |
@@ -24,7 +25,7 @@ | |||
24 | LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); | 25 | LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); |
25 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); | 26 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); |
26 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); | 27 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); |
27 | LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); | 28 | LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); |
28 | LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); | 29 | LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); |
29 | LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, | 30 | LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, |
30 | StkId val); | 31 | StkId val); |