diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-04-29 14:12:50 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-04-29 14:12:50 -0300 |
commit | 88bf2f83c035e612db103d7138a8312c455e4c88 (patch) | |
tree | e11d49feacbe6c78d5f07d621878852590813915 /lapi.c | |
parent | 8fff05f6d00802fa47614d681f1c7a17694856e8 (diff) | |
download | lua-88bf2f83c035e612db103d7138a8312c455e4c88.tar.gz lua-88bf2f83c035e612db103d7138a8312c455e4c88.tar.bz2 lua-88bf2f83c035e612db103d7138a8312c455e4c88.zip |
new function 'tointeger' + 'luaV_arith' replaced by 'luaT_trybinTM'
Diffstat (limited to 'lapi.c')
-rw-r--r-- | lapi.c | 66 |
1 files changed, 20 insertions, 46 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 2.176 2013/04/26 16:03:50 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.177 2013/04/26 19:51:17 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 | */ |
@@ -311,10 +311,9 @@ LUA_API void lua_arith (lua_State *L, int op) { | |||
311 | o1 = L->top - 2; | 311 | o1 = L->top - 2; |
312 | o2 = L->top - 1; | 312 | o2 = L->top - 1; |
313 | if (tonumber(o1, &n1) && tonumber(o2, &n2)) { | 313 | if (tonumber(o1, &n1) && tonumber(o2, &n2)) { |
314 | setnvalue(o1, luaO_arith(op, n1, n2)); | 314 | setnvalue(o1, luaO_numarith(op, n1, n2)); |
315 | } | 315 | } |
316 | else | 316 | else luaT_trybinTM(L, o1, o2, o1, cast(TMS, op - LUA_OPADD + TM_ADD)); |
317 | luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD)); | ||
318 | L->top--; | 317 | L->top--; |
319 | lua_unlock(L); | 318 | lua_unlock(L); |
320 | } | 319 | } |
@@ -339,53 +338,30 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { | |||
339 | } | 338 | } |
340 | 339 | ||
341 | 340 | ||
342 | 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 *pisnum) { |
343 | lua_Number n; | 342 | lua_Number n; |
344 | const TValue *o = index2addr(L, idx); | 343 | const TValue *o = index2addr(L, idx); |
345 | if (tonumber(o, &n)) { | 344 | int isnum = tonumber(o, &n); |
346 | if (isnum) *isnum = 1; | 345 | if (!isnum) |
347 | return n; | 346 | n = 0; /* call to 'tonumber' may change 'n' even if it fails */ |
348 | } | 347 | if (pisnum) *pisnum = isnum; |
349 | else { | 348 | return n; |
350 | if (isnum) *isnum = 0; | ||
351 | return 0; | ||
352 | } | ||
353 | } | 349 | } |
354 | 350 | ||
355 | 351 | ||
356 | LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) { | 352 | LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) { |
357 | lua_Number n; | 353 | lua_Integer res; |
358 | const TValue *o = index2addr(L, idx); | 354 | const TValue *o = index2addr(L, idx); |
359 | if (ttisinteger(o)) { | 355 | int isnum = tointeger(o, &res); |
360 | if (isnum) *isnum = 1; | 356 | if (!isnum) |
361 | return ivalue(o); | 357 | res = 0; /* call to 'tointeger' may change 'n' even if it fails */ |
362 | } | 358 | if (pisnum) *pisnum = isnum; |
363 | else if (tonumber(o, &n)) { | 359 | return res; |
364 | lua_Integer res; | ||
365 | lua_number2integer(res, n); | ||
366 | if (isnum) *isnum = 1; | ||
367 | return res; | ||
368 | } | ||
369 | else { | ||
370 | if (isnum) *isnum = 0; | ||
371 | return 0; | ||
372 | } | ||
373 | } | 360 | } |
374 | 361 | ||
375 | 362 | ||
376 | LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *isnum) { | 363 | LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *pisnum) { |
377 | lua_Number n; | 364 | return lua_tointegerx(L, idx, pisnum); /* at least for now... <<<< */ |
378 | const TValue *o = index2addr(L, idx); | ||
379 | if (tonumber(o, &n)) { | ||
380 | lua_Unsigned res; | ||
381 | lua_number2unsigned(res, n); | ||
382 | if (isnum) *isnum = 1; | ||
383 | return res; | ||
384 | } | ||
385 | else { | ||
386 | if (isnum) *isnum = 0; | ||
387 | return 0; | ||
388 | } | ||
389 | } | 365 | } |
390 | 366 | ||
391 | 367 | ||
@@ -491,17 +467,15 @@ LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { | |||
491 | 467 | ||
492 | LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { | 468 | LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { |
493 | lua_lock(L); | 469 | lua_lock(L); |
494 | setivalue(L->top, cast_num(n)); | 470 | setivalue(L->top, n); |
495 | api_incr_top(L); | 471 | api_incr_top(L); |
496 | lua_unlock(L); | 472 | lua_unlock(L); |
497 | } | 473 | } |
498 | 474 | ||
499 | 475 | ||
500 | LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) { | 476 | LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) { |
501 | lua_Number n; | ||
502 | lua_lock(L); | 477 | lua_lock(L); |
503 | n = lua_unsigned2number(u); | 478 | setivalue(L->top, cast_integer(u)); |
504 | setnvalue(L->top, n); | ||
505 | api_incr_top(L); | 479 | api_incr_top(L); |
506 | lua_unlock(L); | 480 | lua_unlock(L); |
507 | } | 481 | } |