浏览代码

better error messages

Roberto Ierusalimschy 23 年之前
父节点
当前提交
a102221a0b
共有 2 个文件被更改,包括 20 次插入8 次删除
  1. 9 2
      lauxlib.c
  2. 11 6
      liolib.c

+ 9 - 2
lauxlib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lauxlib.c,v 1.71 2002/05/16 18:39:46 roberto Exp roberto $
+** $Id: lauxlib.c,v 1.72 2002/06/03 20:11:41 roberto Exp roberto $
 ** Auxiliary functions for building Lua libraries
 ** See Copyright Notice in lua.h
 */
@@ -37,7 +37,13 @@ LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {
   lua_Debug ar;
   lua_getstack(L, 0, &ar);
   lua_getinfo(L, "n", &ar);
-  if (strcmp(ar.namewhat, "method") == 0) narg--;  /* do not count `self' */
+  if (strcmp(ar.namewhat, "method") == 0) {
+    narg--;  /* do not count `self' */
+    if (narg == 0)  /* error is in the self argument itself? */
+      return luaL_verror(L,
+        "calling %s on bad self (perhaps using `:' instead of `.')",
+        ar.name);
+  }
   if (ar.name == NULL)
     ar.name = "?";
   return luaL_verror(L, "bad argument #%d to `%s' (%s)",
@@ -314,6 +320,7 @@ typedef struct LoadF {
 
 static const char *getF (void *ud, size_t *size) {
   LoadF *lf = (LoadF *)ud;
+  if (feof(lf->f)) return NULL;
   *size = fread(lf->buff, 1, LUAL_BUFFERSIZE, lf->f);
   return (*size > 0) ? lf->buff : NULL;
 }

+ 11 - 6
liolib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: liolib.c,v 2.4 2002/05/02 17:12:27 roberto Exp roberto $
+** $Id: liolib.c,v 2.5 2002/05/06 19:05:10 roberto Exp roberto $
 ** Standard I/O (and system) library
 ** See Copyright Notice in lua.h
 */
@@ -60,8 +60,9 @@ static FILE *tofile (lua_State *L, int findex) {
     lua_pop(L, 1);
     return *f;
   }
-  luaL_argerror(L, findex, "bad file");
-  return NULL;  /* to avoid warnings */
+  if (findex > 0)
+    luaL_argerror(L, findex, "bad file");
+  return NULL;
 }
 
 
@@ -133,14 +134,18 @@ static int io_tmpfile (lua_State *L) {
 
 
 static FILE *getiofile (lua_State *L, const char *name) {
+  FILE *f;
   lua_pushstring(L, name);
   lua_rawget(L, lua_upvalueindex(1));
-  return tofile(L, -1);
+  f = tofile(L, -1);
+  if (f == NULL)
+    luaL_verror(L, "%s is closed", name);
+  return f;
 }
 
 
 static int g_iofile (lua_State *L, const char *name, const char *mode) {
-  if (lua_isnone(L, 1)) {
+  if (lua_isnoneornil(L, 1)) {
     lua_pushstring(L, name);
     lua_rawget(L, lua_upvalueindex(1));
     return 1;
@@ -154,8 +159,8 @@ static int g_iofile (lua_State *L, const char *name, const char *mode) {
       newfile(L, f);
     }
     else {
+      tofile(L, 1);  /* check that it's a valid file handle */
       lua_pushvalue(L, 1);
-      tofile(L, -1);  /* check that it's a valid file handle */
     }
     lua_rawset(L, lua_upvalueindex(1));
     return 0;