Skip to content

Commit 88bf2f8

Browse files
committed
new function 'tointeger' + 'luaV_arith' replaced by 'luaT_trybinTM'
1 parent 8fff05f commit 88bf2f8

4 files changed

Lines changed: 70 additions & 100 deletions

File tree

lapi.c

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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 $
33
** Lua API
44
** See Copyright Notice in lua.h
55
*/
@@ -311,10 +311,9 @@ LUA_API void lua_arith (lua_State *L, int op) {
311311
o1 = L->top - 2;
312312
o2 = L->top - 1;
313313
if (tonumber(o1, &n1) && tonumber(o2, &n2)) {
314-
setnvalue(o1, luaO_arith(op, n1, n2));
314+
setnvalue(o1, luaO_numarith(op, n1, n2));
315315
}
316-
else
317-
luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD));
316+
else luaT_trybinTM(L, o1, o2, o1, cast(TMS, op - LUA_OPADD + TM_ADD));
318317
L->top--;
319318
lua_unlock(L);
320319
}
@@ -339,53 +338,30 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
339338
}
340339

341340

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) {
343342
lua_Number n;
344343
const TValue *o = index2addr(L, idx);
345-
if (tonumber(o, &n)) {
346-
if (isnum) *isnum = 1;
347-
return n;
348-
}
349-
else {
350-
if (isnum) *isnum = 0;
351-
return 0;
352-
}
344+
int isnum = tonumber(o, &n);
345+
if (!isnum)
346+
n = 0; /* call to 'tonumber' may change 'n' even if it fails */
347+
if (pisnum) *pisnum = isnum;
348+
return n;
353349
}
354350

355351

356-
LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) {
357-
lua_Number n;
352+
LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
353+
lua_Integer res;
358354
const TValue *o = index2addr(L, idx);
359-
if (ttisinteger(o)) {
360-
if (isnum) *isnum = 1;
361-
return ivalue(o);
362-
}
363-
else if (tonumber(o, &n)) {
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-
}
355+
int isnum = tointeger(o, &res);
356+
if (!isnum)
357+
res = 0; /* call to 'tointeger' may change 'n' even if it fails */
358+
if (pisnum) *pisnum = isnum;
359+
return res;
373360
}
374361

375362

376-
LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *isnum) {
377-
lua_Number n;
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-
}
363+
LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *pisnum) {
364+
return lua_tointegerx(L, idx, pisnum); /* at least for now... <<<< */
389365
}
390366

391367

@@ -491,17 +467,15 @@ LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
491467

492468
LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
493469
lua_lock(L);
494-
setivalue(L->top, cast_num(n));
470+
setivalue(L->top, n);
495471
api_incr_top(L);
496472
lua_unlock(L);
497473
}
498474

499475

500476
LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) {
501-
lua_Number n;
502477
lua_lock(L);
503-
n = lua_unsigned2number(u);
504-
setnvalue(L->top, n);
478+
setivalue(L->top, cast_integer(u));
505479
api_incr_top(L);
506480
lua_unlock(L);
507481
}

ltable.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: ltable.c,v 2.73 2013/04/15 15:44:46 roberto Exp roberto $
2+
** $Id: ltable.c,v 2.74 2013/04/26 15:39:25 roberto Exp roberto $
33
** Lua tables (hash)
44
** See Copyright Notice in lua.h
55
*/
@@ -123,17 +123,6 @@ static Node *mainposition (const Table *t, const TValue *key) {
123123
}
124124

125125

126-
static int numisint (lua_Number n, lua_Integer *p) {
127-
lua_Integer k;
128-
lua_number2integer(k, n);
129-
if (luai_numeq(cast_num(k), n)) { /* 'k' is int? */
130-
*p = k;
131-
return 1;
132-
}
133-
return 0;
134-
}
135-
136-
137126
/*
138127
** returns the index for `key' if `key' is an appropriate key to live in
139128
** the array part of the table, -1 otherwise.
@@ -423,7 +412,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
423412
lua_Integer k;
424413
if (luai_numisnan(L, n))
425414
luaG_runerror(L, "table index is NaN");
426-
if (numisint(n, &k)) { /* index is int? */
415+
if (luaV_numtointeger(n, &k)) { /* index is int? */
427416
setivalue(&aux, k);
428417
key = &aux; /* insert it as an integer */
429418
}
@@ -505,7 +494,7 @@ const TValue *luaH_get (Table *t, const TValue *key) {
505494
case LUA_TNIL: return luaO_nilobject;
506495
case LUA_TNUMFLT: {
507496
lua_Integer k;
508-
if (numisint(fltvalue(key), &k)) /* index is int? */
497+
if (luaV_numtointeger(fltvalue(key), &k)) /* index is int? */
509498
return luaH_getint(t, k); /* use specialized version */
510499
/* else go through */
511500
}

lvm.c

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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+
5981
void 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,

lvm.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lvm.h,v 2.20 2013/04/25 19:12:41 roberto Exp roberto $
2+
** $Id: lvm.h,v 2.21 2013/04/26 16:03:50 roberto Exp roberto $
33
** Lua virtual machine
44
** See Copyright Notice in lua.h
55
*/
@@ -18,6 +18,9 @@
1818
#define tonumber(o,n) \
1919
(ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n))
2020

21+
#define tointeger(o,i) \
22+
(ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger_(o,i))
23+
2124

2225
#define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2)
2326

@@ -26,6 +29,8 @@ LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2);
2629
LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
2730
LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
2831
LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n);
32+
LUAI_FUNC int luaV_tointeger_ (const TValue *obj, lua_Integer *p);
33+
LUAI_FUNC int luaV_numtointeger (lua_Number n, lua_Integer *p);
2934
LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj);
3035
LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key,
3136
StkId val);
@@ -37,8 +42,6 @@ LUAI_FUNC void luaV_concat (lua_State *L, int total);
3742
LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y);
3843
LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y);
3944
LUAI_FUNC lua_Integer luaV_pow (lua_Integer x, lua_Integer y);
40-
LUAI_FUNC void luaV_arith (lua_State *L, StkId ra, const TValue *rb,
41-
const TValue *rc, TMS op);
4245
LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb);
4346

4447
#endif

0 commit comments

Comments
 (0)