new macro 'nvalue' (to convert an object to a float when we know

object is a number)
This commit is contained in:
Roberto Ierusalimschy 2014-12-19 11:36:32 -02:00
parent 3ae21a352c
commit 2b83711fba
3 changed files with 7 additions and 10 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lcode.c,v 2.96 2014/11/21 12:15:57 roberto Exp roberto $ ** $Id: lcode.c,v 2.97 2014/11/24 14:59:22 roberto Exp roberto $
** Code generator for Lua ** Code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -761,12 +761,8 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
** return false if folding can raise an error ** return false if folding can raise an error
*/ */
static int validop (int op, TValue *v1, TValue *v2) { static int validop (int op, TValue *v1, TValue *v2) {
lua_Number a, b;
lua_Integer i; lua_Integer i;
cast_void(a); cast_void(b); /* macro may not use its arguments */ if (luai_numinvalidop(op, nvalue(v1), nvalue(v2))) return 0;
if (luai_numinvalidop(op, (cast_void(tonumber(v1, &a)), a),
(cast_void(tonumber(v2, &b)), b)))
return 0;
switch (op) { switch (op) {
case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: /* conversion errors */ case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: /* conversion errors */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.h,v 2.103 2014/10/01 11:52:33 roberto Exp roberto $ ** $Id: lobject.h,v 2.104 2014/10/25 11:50:46 roberto Exp roberto $
** Type definitions for Lua objects ** Type definitions for Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -154,6 +154,8 @@ typedef struct lua_TValue TValue;
/* Macros to access values */ /* Macros to access values */
#define ivalue(o) check_exp(ttisinteger(o), val_(o).i) #define ivalue(o) check_exp(ttisinteger(o), val_(o).i)
#define fltvalue(o) check_exp(ttisfloat(o), val_(o).n) #define fltvalue(o) check_exp(ttisfloat(o), val_(o).n)
#define nvalue(o) check_exp(ttisnumber(o), \
(ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o)))
#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc) #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p) #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
#define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc)) #define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc))

5
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 2.229 2014/11/19 15:05:15 roberto Exp roberto $ ** $Id: lvm.c,v 2.230 2014/11/21 12:15:00 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -74,8 +74,7 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
} }
else if (cvt2num(obj) && /* string convertible to number? */ else if (cvt2num(obj) && /* string convertible to number? */
luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) { luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) {
/* convert result of 'luaO_str2num' to a float */ *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */
*n = (ttisinteger(&v)) ? cast_num(ivalue(&v)) : fltvalue(&v);
return 1; return 1;
} }
else else