浏览代码

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

Roberto Ierusalimschy 10 年之前
父节点
当前提交
2b83711fba
共有 3 个文件被更改,包括 7 次插入10 次删除
  1. 2 6
      lcode.c
  2. 3 1
      lobject.h
  3. 2 3
      lvm.c

+ 2 - 6
lcode.c

@@ -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
 ** 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
 */
 static int validop (int op, TValue *v1, TValue *v2) {
-  lua_Number a, b;
   lua_Integer i;
-  cast_void(a); cast_void(b);  /* macro may not use its arguments */
-  if (luai_numinvalidop(op, (cast_void(tonumber(v1, &a)), a),
-                            (cast_void(tonumber(v2, &b)), b)))
-    return 0;
+  if (luai_numinvalidop(op, nvalue(v1), nvalue(v2))) return 0;
   switch (op) {
     case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
     case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT:  /* conversion errors */

+ 3 - 1
lobject.h

@@ -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
 ** See Copyright Notice in lua.h
 */
@@ -154,6 +154,8 @@ typedef struct lua_TValue TValue;
 /* Macros to access values */
 #define ivalue(o)	check_exp(ttisinteger(o), val_(o).i)
 #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 pvalue(o)	check_exp(ttislightuserdata(o), val_(o).p)
 #define tsvalue(o)	check_exp(ttisstring(o), gco2ts(val_(o).gc))

+ 2 - 3
lvm.c

@@ -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
 ** 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? */
             luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) {
-    /* convert result of 'luaO_str2num' to a float */
-      *n = (ttisinteger(&v)) ? cast_num(ivalue(&v)) : fltvalue(&v);
+    *n = nvalue(&v);  /* convert result of 'luaO_str2num' to a float */
     return 1;
   }
   else