aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2013-04-26 16:51:17 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2013-04-26 16:51:17 -0300
commit360587f31959b9a112b13000b3c7ee8ae51cf146 (patch)
tree800c578cf9a945c336cd8a251aa61c792c76ce0c
parenteee51492e26bc295d2b67b91aa7bdb97a21e3623 (diff)
downloadlua-360587f31959b9a112b13000b3c7ee8ae51cf146.tar.gz
lua-360587f31959b9a112b13000b3c7ee8ae51cf146.tar.bz2
lua-360587f31959b9a112b13000b3c7ee8ae51cf146.zip
favoring 'tonumber' over 'nvalue'
-rw-r--r--lapi.c7
-rw-r--r--lvm.c57
2 files changed, 30 insertions, 34 deletions
diff --git a/lapi.c b/lapi.c
index 5edb9fcf..7ad8817d 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 2.175 2013/04/26 15:39:25 roberto Exp roberto $ 2** $Id: lapi.c,v 2.176 2013/04/26 16:03:50 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*/
@@ -299,6 +299,7 @@ LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
299LUA_API void lua_arith (lua_State *L, int op) { 299LUA_API void lua_arith (lua_State *L, int op) {
300 StkId o1; /* 1st operand */ 300 StkId o1; /* 1st operand */
301 StkId o2; /* 2nd operand */ 301 StkId o2; /* 2nd operand */
302 lua_Number n1, n2;
302 lua_lock(L); 303 lua_lock(L);
303 if (op != LUA_OPUNM) /* all other operations expect two operands */ 304 if (op != LUA_OPUNM) /* all other operations expect two operands */
304 api_checknelems(L, 2); 305 api_checknelems(L, 2);
@@ -309,8 +310,8 @@ LUA_API void lua_arith (lua_State *L, int op) {
309 } 310 }
310 o1 = L->top - 2; 311 o1 = L->top - 2;
311 o2 = L->top - 1; 312 o2 = L->top - 1;
312 if (ttisnumber(o1) && ttisnumber(o2)) { 313 if (tonumber(o1, &n1) && tonumber(o2, &n2)) {
313 setnvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2))); 314 setnvalue(o1, luaO_arith(op, n1, n2));
314 } 315 }
315 else 316 else
316 luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD)); 317 luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD));
diff --git a/lvm.c b/lvm.c
index 8e316627..815c864a 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 2.164 2013/04/26 16:03:50 roberto Exp roberto $ 2** $Id: lvm.c,v 2.165 2013/04/26 16:06: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*/
@@ -145,10 +145,11 @@ static int l_strcmp (const TString *ls, const TString *rs) {
145 145
146int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { 146int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
147 int res; 147 int res;
148 lua_Number nl, nr;
148 if (ttisinteger(l) && ttisinteger(r)) 149 if (ttisinteger(l) && ttisinteger(r))
149 return (ivalue(l) < ivalue(r)); 150 return (ivalue(l) < ivalue(r));
150 else if (ttisnumber(l) && ttisnumber(r)) 151 else if (tonumber(l, &nl) && tonumber(r, &nr))
151 return luai_numlt(L, nvalue(l), nvalue(r)); 152 return luai_numlt(L, nl, nr);
152 else if (ttisstring(l) && ttisstring(r)) 153 else if (ttisstring(l) && ttisstring(r))
153 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0; 154 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
154 else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0) 155 else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0)
@@ -159,10 +160,11 @@ int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
159 160
160int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { 161int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
161 int res; 162 int res;
163 lua_Number nl, nr;
162 if (ttisinteger(l) && ttisinteger(r)) 164 if (ttisinteger(l) && ttisinteger(r))
163 return (ivalue(l) <= ivalue(r)); 165 return (ivalue(l) <= ivalue(r));
164 else if (ttisnumber(l) && ttisnumber(r)) 166 else if (tonumber(l, &nl) && tonumber(r, &nr))
165 return luai_numle(L, nvalue(l), nvalue(r)); 167 return luai_numle(L, nl, nr);
166 else if (ttisstring(l) && ttisstring(r)) 168 else if (ttisstring(l) && ttisstring(r))
167 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0; 169 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
168 else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* first try `le' */ 170 else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* first try `le' */
@@ -181,8 +183,11 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
181 if (ttype(t1) != ttype(t2)) { 183 if (ttype(t1) != ttype(t2)) {
182 if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER) 184 if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER)
183 return 0; /* only numbers can be equal with different variants */ 185 return 0; /* only numbers can be equal with different variants */
184 else /* two numbers with different variants */ 186 else { /* two numbers with different variants */
185 return luai_numeq(nvalue(t1), nvalue(t2)); 187 lua_Number n1, n2;
188 tonumber(t1, &n1); tonumber(t2, &n2);
189 return luai_numeq(n1, n2);
190 }
186 } 191 }
187 /* values have same type and same variant */ 192 /* values have same type and same variant */
188 switch (ttype(t1)) { 193 switch (ttype(t1)) {
@@ -488,16 +493,6 @@ void luaV_finishOp (lua_State *L) {
488 luai_threadyield(L); ) 493 luai_threadyield(L); )
489 494
490 495
491#define arith_op(op,tm) { \
492 TValue *rb = RKB(i); \
493 TValue *rc = RKC(i); \
494 if (ttisnumber(rb) && ttisnumber(rc)) { \
495 lua_Number nb = nvalue(rb), nc = nvalue(rc); \
496 setnvalue(ra, op(L, nb, nc)); \
497 } \
498 else { Protect(luaV_arith(L, ra, rb, rc, tm)); } }
499
500
501#define vmdispatch(o) switch(o) 496#define vmdispatch(o) switch(o)
502#define vmcase(l,b) case l: {b} break; 497#define vmcase(l,b) case l: {b} break;
503#define vmcasenb(l,b) case l: {b} /* nb = no break */ 498#define vmcasenb(l,b) case l: {b} /* nb = no break */
@@ -588,12 +583,12 @@ void luaV_execute (lua_State *L) {
588 vmcase(OP_ADD, 583 vmcase(OP_ADD,
589 TValue *rb = RKB(i); 584 TValue *rb = RKB(i);
590 TValue *rc = RKC(i); 585 TValue *rc = RKC(i);
586 lua_Number nb; lua_Number nc;
591 if (ttisinteger(rb) && ttisinteger(rc)) { 587 if (ttisinteger(rb) && ttisinteger(rc)) {
592 lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); 588 lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
593 setivalue(ra, ib + ic); 589 setivalue(ra, ib + ic);
594 } 590 }
595 else if (ttisnumber(rb) && ttisnumber(rc)) { 591 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
596 lua_Number nb = nvalue(rb); lua_Number nc = nvalue(rc);
597 setnvalue(ra, luai_numadd(L, nb, nc)); 592 setnvalue(ra, luai_numadd(L, nb, nc));
598 } 593 }
599 else { Protect(luaV_arith(L, ra, rb, rc, TM_ADD)); } 594 else { Protect(luaV_arith(L, ra, rb, rc, TM_ADD)); }
@@ -601,12 +596,12 @@ void luaV_execute (lua_State *L) {
601 vmcase(OP_SUB, 596 vmcase(OP_SUB,
602 TValue *rb = RKB(i); 597 TValue *rb = RKB(i);
603 TValue *rc = RKC(i); 598 TValue *rc = RKC(i);
599 lua_Number nb; lua_Number nc;
604 if (ttisinteger(rb) && ttisinteger(rc)) { 600 if (ttisinteger(rb) && ttisinteger(rc)) {
605 lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); 601 lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
606 setivalue(ra, ib - ic); 602 setivalue(ra, ib - ic);
607 } 603 }
608 else if (ttisnumber(rb) && ttisnumber(rc)) { 604 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
609 lua_Number nb = nvalue(rb); lua_Number nc = nvalue(rc);
610 setnvalue(ra, luai_numsub(L, nb, nc)); 605 setnvalue(ra, luai_numsub(L, nb, nc));
611 } 606 }
612 else { Protect(luaV_arith(L, ra, rb, rc, TM_SUB)); } 607 else { Protect(luaV_arith(L, ra, rb, rc, TM_SUB)); }
@@ -614,12 +609,12 @@ void luaV_execute (lua_State *L) {
614 vmcase(OP_MUL, 609 vmcase(OP_MUL,
615 TValue *rb = RKB(i); 610 TValue *rb = RKB(i);
616 TValue *rc = RKC(i); 611 TValue *rc = RKC(i);
612 lua_Number nb; lua_Number nc;
617 if (ttisinteger(rb) && ttisinteger(rc)) { 613 if (ttisinteger(rb) && ttisinteger(rc)) {
618 lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); 614 lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
619 setivalue(ra, ib * ic); 615 setivalue(ra, ib * ic);
620 } 616 }
621 else if (ttisnumber(rb) && ttisnumber(rc)) { 617 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
622 lua_Number nb = nvalue(rb); lua_Number nc = nvalue(rc);
623 setnvalue(ra, luai_nummul(L, nb, nc)); 618 setnvalue(ra, luai_nummul(L, nb, nc));
624 } 619 }
625 else { Protect(luaV_arith(L, ra, rb, rc, TM_MUL)); } 620 else { Protect(luaV_arith(L, ra, rb, rc, TM_MUL)); }
@@ -627,8 +622,8 @@ void luaV_execute (lua_State *L) {
627 vmcase(OP_DIV, /* float division (always with floats) */ 622 vmcase(OP_DIV, /* float division (always with floats) */
628 TValue *rb = RKB(i); 623 TValue *rb = RKB(i);
629 TValue *rc = RKC(i); 624 TValue *rc = RKC(i);
630 if (ttisnumber(rb) && ttisnumber(rc)) { 625 lua_Number nb; lua_Number nc;
631 lua_Number nb = nvalue(rb); lua_Number nc = nvalue(rc); 626 if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
632 setnvalue(ra, luai_numdiv(L, nb, nc)); 627 setnvalue(ra, luai_numdiv(L, nb, nc));
633 } 628 }
634 else { Protect(luaV_arith(L, ra, rb, rc, TM_DIV)); } 629 else { Protect(luaV_arith(L, ra, rb, rc, TM_DIV)); }
@@ -636,12 +631,12 @@ void luaV_execute (lua_State *L) {
636 vmcase(OP_IDIV, /* integer division */ 631 vmcase(OP_IDIV, /* integer division */
637 TValue *rb = RKB(i); 632 TValue *rb = RKB(i);
638 TValue *rc = RKC(i); 633 TValue *rc = RKC(i);
634 lua_Number nb; lua_Number nc;
639 if (ttisinteger(rb) && ttisinteger(rc)) { 635 if (ttisinteger(rb) && ttisinteger(rc)) {
640 lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); 636 lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
641 setivalue(ra, luaV_div(L, ib, ic)); 637 setivalue(ra, luaV_div(L, ib, ic));
642 } 638 }
643 else if (ttisnumber(rb) && ttisnumber(rc)) { 639 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
644 lua_Number nb = nvalue(rb); lua_Number nc = nvalue(rc);
645 setnvalue(ra, luai_numidiv(L, nb, nc)); 640 setnvalue(ra, luai_numidiv(L, nb, nc));
646 } 641 }
647 else { Protect(luaV_arith(L, ra, rb, rc, TM_IDIV)); } 642 else { Protect(luaV_arith(L, ra, rb, rc, TM_IDIV)); }
@@ -649,12 +644,12 @@ void luaV_execute (lua_State *L) {
649 vmcase(OP_MOD, 644 vmcase(OP_MOD,
650 TValue *rb = RKB(i); 645 TValue *rb = RKB(i);
651 TValue *rc = RKC(i); 646 TValue *rc = RKC(i);
647 lua_Number nb; lua_Number nc;
652 if (ttisinteger(rb) && ttisinteger(rc)) { 648 if (ttisinteger(rb) && ttisinteger(rc)) {
653 lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); 649 lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
654 setivalue(ra, luaV_mod(L, ib, ic)); 650 setivalue(ra, luaV_mod(L, ib, ic));
655 } 651 }
656 else if (ttisnumber(rb) && ttisnumber(rc)) { 652 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
657 lua_Number nb = nvalue(rb); lua_Number nc = nvalue(rc);
658 setnvalue(ra, luai_nummod(L, nb, nc)); 653 setnvalue(ra, luai_nummod(L, nb, nc));
659 } 654 }
660 else { Protect(luaV_arith(L, ra, rb, rc, TM_MOD)); } 655 else { Protect(luaV_arith(L, ra, rb, rc, TM_MOD)); }
@@ -662,14 +657,14 @@ void luaV_execute (lua_State *L) {
662 vmcase(OP_POW, 657 vmcase(OP_POW,
663 TValue *rb = RKB(i); 658 TValue *rb = RKB(i);
664 TValue *rc = RKC(i); 659 TValue *rc = RKC(i);
660 lua_Number nb; lua_Number nc;
665 lua_Integer ic; 661 lua_Integer ic;
666 if (ttisinteger(rb) && ttisinteger(rc) && 662 if (ttisinteger(rb) && ttisinteger(rc) &&
667 (ic = ivalue(rc)) >= 0) { 663 (ic = ivalue(rc)) >= 0) {
668 lua_Integer ib = ivalue(rb); 664 lua_Integer ib = ivalue(rb);
669 setivalue(ra, luaV_pow(ib, ic)); 665 setivalue(ra, luaV_pow(ib, ic));
670 } 666 }
671 else if (ttisnumber(rb) && ttisnumber(rc)) { 667 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
672 lua_Number nb = nvalue(rb); lua_Number nc = nvalue(rc);
673 setnvalue(ra, luai_numpow(L, nb, nc)); 668 setnvalue(ra, luai_numpow(L, nb, nc));
674 } 669 }
675 else { Protect(luaV_arith(L, ra, rb, rc, TM_POW)); } 670 else { Protect(luaV_arith(L, ra, rb, rc, TM_POW)); }