Browse Source

correct error message for conversion errors from float to int

Roberto Ierusalimschy 12 years ago
parent
commit
f2043b7a58
3 changed files with 26 additions and 12 deletions
  1. 20 10
      ldebug.c
  2. 3 1
      ldebug.h
  3. 3 1
      ltm.c

+ 20 - 10
ldebug.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: ldebug.c,v 2.93 2013/04/26 16:03:50 roberto Exp roberto $
+** $Id: ldebug.c,v 2.94 2013/04/29 16:58:10 roberto Exp roberto $
 ** Debug Interface
 ** Debug Interface
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -498,10 +498,9 @@ static const char *getupvalname (CallInfo *ci, const TValue *o,
 }
 }
 
 
 
 
-l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
+static const char *varinfo (lua_State *L, const TValue *o) {
+  const char *name;
   CallInfo *ci = L->ci;
   CallInfo *ci = L->ci;
-  const char *name = NULL;
-  const char *t = objtypename(o);
   const char *kind = NULL;
   const char *kind = NULL;
   if (isLua(ci)) {
   if (isLua(ci)) {
     kind = getupvalname(ci, o, &name);  /* check whether 'o' is an upvalue */
     kind = getupvalname(ci, o, &name);  /* check whether 'o' is an upvalue */
@@ -509,17 +508,19 @@ l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
       kind = getobjname(ci_func(ci)->p, currentpc(ci),
       kind = getobjname(ci_func(ci)->p, currentpc(ci),
                         cast_int(o - ci->u.l.base), &name);
                         cast_int(o - ci->u.l.base), &name);
   }
   }
-  if (kind)
-    luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)",
-                op, kind, name, t);
-  else
-    luaG_runerror(L, "attempt to %s a %s value", op, t);
+  return (kind) ? luaO_pushfstring(L, " (%s " LUA_QS ")", kind, name) : "";
+}
+
+
+l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) {
+  const char *t = objtypename(o);
+  luaG_runerror(L, "attempt to %s a %s value%s", op, t, varinfo(L, o));
 }
 }
 
 
 
 
 l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
 l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
   if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
   if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;
-  lua_assert(!ttisstring(p1) && !ttisnumber(p2));
+  lua_assert(!ttisstring(p1) && !ttisnumber(p1));
   luaG_typeerror(L, p1, "concatenate");
   luaG_typeerror(L, p1, "concatenate");
 }
 }
 
 
@@ -532,6 +533,15 @@ l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
 }
 }
 
 
 
 
+l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
+  lua_Integer temp;
+  if (!tointeger(p1, &temp))
+    p2 = p1;
+  luaG_runerror(L, "attempt to convert an out of range float%s to an integer",
+                   varinfo(L, p2));
+}
+
+
 l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
 l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
   const char *t1 = objtypename(p1);
   const char *t1 = objtypename(p1);
   const char *t2 = objtypename(p2);
   const char *t2 = objtypename(p2);

+ 3 - 1
ldebug.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: ldebug.h,v 2.8 2013/04/25 15:59:42 roberto Exp roberto $
+** $Id: ldebug.h,v 2.9 2013/04/29 16:58:10 roberto Exp roberto $
 ** Auxiliary functions from Debug Interface module
 ** Auxiliary functions from Debug Interface module
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -27,6 +27,8 @@ LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1,
                                                   const TValue *p2);
                                                   const TValue *p2);
 LUAI_FUNC l_noret luaG_aritherror (lua_State *L, const TValue *p1,
 LUAI_FUNC l_noret luaG_aritherror (lua_State *L, const TValue *p1,
                                                  const TValue *p2);
                                                  const TValue *p2);
+LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1,
+                                                 const TValue *p2);
 LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1,
 LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1,
                                                  const TValue *p2);
                                                  const TValue *p2);
 LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...);
 LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...);

+ 3 - 1
ltm.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: ltm.c,v 2.18 2013/04/26 13:07:53 roberto Exp roberto $
+** $Id: ltm.c,v 2.19 2013/04/29 16:56:50 roberto Exp roberto $
 ** Tag methods
 ** Tag methods
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -112,6 +112,8 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
   if (!luaT_callbinTM(L, p1, p2, res, event)) {
   if (!luaT_callbinTM(L, p1, p2, res, event)) {
     if (event == TM_CONCAT)
     if (event == TM_CONCAT)
       luaG_concaterror(L, p1, p2);
       luaG_concaterror(L, p1, p2);
+    else if (event == TM_IDIV && ttisnumber(p1) && ttisnumber(p2))
+      luaG_tointerror(L, p1, p2);
     else
     else
       luaG_aritherror(L, p1, p2);
       luaG_aritherror(L, p1, p2);
   }
   }