11/*
2- ** $Id: lvm.c,v 2.165 2013/04/26 16:06:53 roberto Exp roberto $
2+ ** $Id: lvm.c,v 2.166 2013/04/26 19:51:17 roberto Exp roberto $
33** Lua virtual machine
44** See Copyright Notice in lua.h
55*/
@@ -56,6 +56,28 @@ int luaV_tostring (lua_State *L, StkId obj) {
5656}
5757
5858
59+ int luaV_numtointeger (lua_Number n , lua_Integer * p ) {
60+ lua_Integer k ;
61+ lua_number2integer (k , n );
62+ if (luai_numeq (cast_num (k ), n )) { /* 'k' is int? */
63+ * p = k ;
64+ return 1 ;
65+ }
66+ return 0 ;
67+ }
68+
69+
70+ int luaV_tointeger_ (const TValue * obj , lua_Integer * p ) {
71+ lua_Number n ;
72+ lua_assert (!ttisinteger (obj ));
73+ if (tonumber (obj , & n )) {
74+ n = l_mathop (floor )(n );
75+ return luaV_numtointeger (n , p );
76+ }
77+ else return 0 ;
78+ }
79+
80+
5981void luaV_gettable (lua_State * L , const TValue * t , TValue * key , StkId val ) {
6082 int loop ;
6183 for (loop = 0 ; loop < MAXTAGLOOP ; loop ++ ) {
@@ -225,10 +247,8 @@ void luaV_concat (lua_State *L, int total) {
225247 do {
226248 StkId top = L -> top ;
227249 int n = 2 ; /* number of elements handled in this pass (at least 2) */
228- if (!(ttisstring (top - 2 ) || ttisnumber (top - 2 )) || !tostring (L , top - 1 )) {
229- if (!luaT_callbinTM (L , top - 2 , top - 1 , top - 2 , TM_CONCAT ))
230- luaG_concaterror (L , top - 2 , top - 1 );
231- }
250+ if (!(ttisstring (top - 2 ) || ttisnumber (top - 2 )) || !tostring (L , top - 1 ))
251+ luaT_trybinTM (L , top - 2 , top - 1 , top - 2 , TM_CONCAT );
232252 else if (tsvalue (top - 1 )-> len == 0 ) /* second operand is empty? */
233253 (void )tostring (L , top - 2 ); /* result is first operand */
234254 else if (ttisstring (top - 2 ) && tsvalue (top - 2 )-> len == 0 ) {
@@ -335,18 +355,6 @@ lua_Integer luaV_pow (lua_Integer x, lua_Integer y) {
335355}
336356
337357
338- void luaV_arith (lua_State * L , StkId ra , const TValue * rb ,
339- const TValue * rc , TMS op ) {
340- lua_Number b , c ;
341- if (tonumber (rb , & b ) && tonumber (rc , & c )) {
342- lua_Number res = luaO_arith (op - TM_ADD + LUA_OPADD , b , c );
343- setnvalue (ra , res );
344- }
345- else if (!luaT_callbinTM (L , rb , rc , ra , op ))
346- luaG_aritherror (L , rb , rc );
347- }
348-
349-
350358/*
351359** check whether cached closure in prototype 'p' may be reused, that is,
352360** whether there is a cached closure with the same upvalues needed by
@@ -422,7 +430,7 @@ void luaV_finishOp (lua_State *L) {
422430 break ;
423431 }
424432 case OP_CONCAT : {
425- StkId top = L -> top - 1 ; /* top when 'luaT_callbinTM ' was called */
433+ StkId top = L -> top - 1 ; /* top when 'luaT_trybinTM ' was called */
426434 int b = GETARG_B (inst ); /* first element to concatenate */
427435 int total = cast_int (top - 1 - (base + b )); /* yet to concatenate */
428436 setobj2s (L , top - 2 , top ); /* put TM result in proper position */
@@ -586,38 +594,38 @@ void luaV_execute (lua_State *L) {
586594 lua_Number nb ; lua_Number nc ;
587595 if (ttisinteger (rb ) && ttisinteger (rc )) {
588596 lua_Integer ib = ivalue (rb ); lua_Integer ic = ivalue (rc );
589- setivalue (ra , ib + ic );
597+ setivalue (ra , cast_integer ( cast_unsigned ( ib ) + cast_unsigned ( ic )) );
590598 }
591599 else if (tonumber (rb , & nb ) && tonumber (rc , & nc )) {
592600 setnvalue (ra , luai_numadd (L , nb , nc ));
593601 }
594- else { Protect (luaV_arith (L , ra , rb , rc , TM_ADD )); }
602+ else { Protect (luaT_trybinTM (L , rb , rc , ra , TM_ADD )); }
595603 )
596604 vmcase (OP_SUB ,
597605 TValue * rb = RKB (i );
598606 TValue * rc = RKC (i );
599607 lua_Number nb ; lua_Number nc ;
600608 if (ttisinteger (rb ) && ttisinteger (rc )) {
601609 lua_Integer ib = ivalue (rb ); lua_Integer ic = ivalue (rc );
602- setivalue (ra , ib - ic );
610+ setivalue (ra , cast_integer ( cast_unsigned ( ib ) - cast_unsigned ( ic )) );
603611 }
604612 else if (tonumber (rb , & nb ) && tonumber (rc , & nc )) {
605613 setnvalue (ra , luai_numsub (L , nb , nc ));
606614 }
607- else { Protect (luaV_arith (L , ra , rb , rc , TM_SUB )); }
615+ else { Protect (luaT_trybinTM (L , rb , rc , ra , TM_SUB )); }
608616 )
609617 vmcase (OP_MUL ,
610618 TValue * rb = RKB (i );
611619 TValue * rc = RKC (i );
612620 lua_Number nb ; lua_Number nc ;
613621 if (ttisinteger (rb ) && ttisinteger (rc )) {
614622 lua_Integer ib = ivalue (rb ); lua_Integer ic = ivalue (rc );
615- setivalue (ra , ib * ic );
623+ setivalue (ra , cast_integer ( cast_unsigned ( ib ) * cast_unsigned ( ic )) );
616624 }
617625 else if (tonumber (rb , & nb ) && tonumber (rc , & nc )) {
618626 setnvalue (ra , luai_nummul (L , nb , nc ));
619627 }
620- else { Protect (luaV_arith (L , ra , rb , rc , TM_MUL )); }
628+ else { Protect (luaT_trybinTM (L , rb , rc , ra , TM_MUL )); }
621629 )
622630 vmcase (OP_DIV , /* float division (always with floats) */
623631 TValue * rb = RKB (i );
@@ -626,20 +634,16 @@ void luaV_execute (lua_State *L) {
626634 if (tonumber (rb , & nb ) && tonumber (rc , & nc )) {
627635 setnvalue (ra , luai_numdiv (L , nb , nc ));
628636 }
629- else { Protect (luaV_arith (L , ra , rb , rc , TM_DIV )); }
637+ else { Protect (luaT_trybinTM (L , rb , rc , ra , TM_DIV )); }
630638 )
631639 vmcase (OP_IDIV , /* integer division */
632640 TValue * rb = RKB (i );
633641 TValue * rc = RKC (i );
634- lua_Number nb ; lua_Number nc ;
635- if (ttisinteger (rb ) && ttisinteger (rc )) {
636- lua_Integer ib = ivalue (rb ); lua_Integer ic = ivalue (rc );
642+ lua_Integer ib ; lua_Integer ic ;
643+ if (tointeger (rb , & ib ) && tointeger (rc , & ic )) {
637644 setivalue (ra , luaV_div (L , ib , ic ));
638645 }
639- else if (tonumber (rb , & nb ) && tonumber (rc , & nc )) {
640- setnvalue (ra , luai_numidiv (L , nb , nc ));
641- }
642- else { Protect (luaV_arith (L , ra , rb , rc , TM_IDIV )); }
646+ else { Protect (luaT_trybinTM (L , rb , rc , ra , TM_IDIV )); }
643647 )
644648 vmcase (OP_MOD ,
645649 TValue * rb = RKB (i );
@@ -652,7 +656,7 @@ void luaV_execute (lua_State *L) {
652656 else if (tonumber (rb , & nb ) && tonumber (rc , & nc )) {
653657 setnvalue (ra , luai_nummod (L , nb , nc ));
654658 }
655- else { Protect (luaV_arith (L , ra , rb , rc , TM_MOD )); }
659+ else { Protect (luaT_trybinTM (L , rb , rc , ra , TM_MOD )); }
656660 )
657661 vmcase (OP_POW ,
658662 TValue * rb = RKB (i );
@@ -667,20 +671,20 @@ void luaV_execute (lua_State *L) {
667671 else if (tonumber (rb , & nb ) && tonumber (rc , & nc )) {
668672 setnvalue (ra , luai_numpow (L , nb , nc ));
669673 }
670- else { Protect (luaV_arith (L , ra , rb , rc , TM_POW )); }
674+ else { Protect (luaT_trybinTM (L , rb , rc , ra , TM_POW )); }
671675 )
672676 vmcase (OP_UNM ,
673677 TValue * rb = RB (i );
678+ lua_Number nb ;
674679 if (ttisinteger (rb )) {
675680 lua_Integer ib = ivalue (rb );
676681 setivalue (ra , - ib );
677682 }
678- else if (ttisfloat (rb )) {
679- lua_Number nb = fltvalue (rb );
683+ else if (tonumber (rb , & nb )) {
680684 setnvalue (ra , luai_numunm (L , nb ));
681685 }
682686 else {
683- Protect (luaV_arith (L , ra , rb , rb , TM_UNM ));
687+ Protect (luaT_trybinTM (L , rb , rb , ra , TM_UNM ));
684688 }
685689 )
686690 vmcase (OP_NOT ,
0 commit comments