瀏覽代碼

better error messages

Roberto Ierusalimschy 25 年之前
父節點
當前提交
fb60283974
共有 2 個文件被更改,包括 48 次插入41 次删除
  1. 37 27
      lauxlib.c
  2. 11 14
      liolib.c

+ 37 - 27
lauxlib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lauxlib.c,v 1.22 1999/12/20 13:09:45 roberto Exp roberto $
+** $Id: lauxlib.c,v 1.23 1999/12/27 17:33:22 roberto Exp roberto $
 ** Auxiliary functions for building Lua libraries
 ** See Copyright Notice in lua.h
 */
@@ -30,69 +30,79 @@ int luaL_findstring (const char *name, const char *const list[]) {
   return -1;  /* name not found */
 }
 
-void luaL_argerror (lua_State *L, int numarg, const char *extramsg) {
+void luaL_argerror (lua_State *L, int narg, const char *extramsg) {
   lua_Function f = lua_stackedfunction(L, 0);
   const char *funcname;
   lua_getobjname(L, f, &funcname);
-  numarg -= lua_nups(L, f);
+  narg -= lua_nups(L, f);
   if (funcname == NULL)
     funcname = "?";
-  luaL_verror(L, "bad argument #%d to function `%.50s' (%.100s)",
-              numarg, funcname, extramsg);
+  luaL_verror(L, "bad argument #%d to `%.50s' (%.100s)",
+              narg, funcname, extramsg);
 }
 
-static const char *checkstr (lua_State *L, lua_Object o, int n, long *len) {
+
+static void type_error (lua_State *L, int narg, const char *typename,
+                        lua_Object o) {
+  char buff[100];
+  const char *otype = (o == LUA_NOOBJECT) ? "no value" : lua_type(L, o);
+  sprintf(buff, "%.10s expected, got %.10s", typename, otype);
+  luaL_argerror(L, narg, buff);
+}
+
+
+static const char *checkstr (lua_State *L, lua_Object o, int narg, long *len) {
   const char *s = lua_getstring(L, o);
-  luaL_arg_check(L, s, n, "string expected");
+  if (!s) type_error(L, narg, "string", o);
   if (len) *len = lua_strlen(L, o);
   return s;
 }
 
-const char *luaL_check_lstr (lua_State *L, int n, long *len) {
-  return checkstr(L, lua_getparam(L, n), n, len);
+const char *luaL_check_lstr (lua_State *L, int narg, long *len) {
+  return checkstr(L, lua_getparam(L, narg), narg, len);
 }
 
-const char *luaL_opt_lstr (lua_State *L, int n, const char *def, long *len) {
-  lua_Object o = lua_getparam(L, n);
+const char *luaL_opt_lstr (lua_State *L, int narg, const char *def, long *len) {
+  lua_Object o = lua_getparam(L, narg);
   if (o == LUA_NOOBJECT) {
     if (len) *len = def ? strlen(def) : 0;
     return def;
   }
-  else return checkstr(L, o, n, len);
+  else return checkstr(L, o, narg, len);
 }
 
-double luaL_check_number (lua_State *L, int n) {
-  lua_Object o = lua_getparam(L, n);
-  luaL_arg_check(L, lua_isnumber(L, o), n, "number expected");
+double luaL_check_number (lua_State *L, int narg) {
+  lua_Object o = lua_getparam(L, narg);
+  if (!lua_isnumber(L, o)) type_error(L, narg, "number", o);
   return lua_getnumber(L, o);
 }
 
 
-double luaL_opt_number (lua_State *L, int n, double def) {
-  lua_Object o = lua_getparam(L, n);
+double luaL_opt_number (lua_State *L, int narg, double def) {
+  lua_Object o = lua_getparam(L, narg);
   if (o == LUA_NOOBJECT) return def;
   else {
-    luaL_arg_check(L, lua_isnumber(L, o), n, "number expected");
+    if (!lua_isnumber(L, o)) type_error(L, narg, "number", o);
     return lua_getnumber(L, o);
   }
 }
 
 
-lua_Object luaL_tablearg (lua_State *L, int arg) {
-  lua_Object o = lua_getparam(L, arg);
-  luaL_arg_check(L, lua_istable(L, o), arg, "table expected");
+lua_Object luaL_tablearg (lua_State *L, int narg) {
+  lua_Object o = lua_getparam(L, narg);
+  if (!lua_istable(L, o)) type_error(L, narg, "table", o);
   return o;
 }
 
-lua_Object luaL_functionarg (lua_State *L, int arg) {
-  lua_Object o = lua_getparam(L, arg);
-  luaL_arg_check(L, lua_isfunction(L, o), arg, "function expected");
+lua_Object luaL_functionarg (lua_State *L, int narg) {
+  lua_Object o = lua_getparam(L, narg);
+  if (!lua_isfunction(L, o)) type_error(L, narg, "function", o);
   return o;
 }
 
-lua_Object luaL_nonnullarg (lua_State *L, int n) {
-  lua_Object o = lua_getparam(L, n);
-  luaL_arg_check(L, o != LUA_NOOBJECT, n, "value expected");
+lua_Object luaL_nonnullarg (lua_State *L, int narg) {
+  lua_Object o = lua_getparam(L, narg);
+  luaL_arg_check(L, o != LUA_NOOBJECT, narg, "value expected");
   return o;
 }
 

+ 11 - 14
liolib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: liolib.c,v 1.52 1999/11/22 17:39:51 roberto Exp roberto $
+** $Id: liolib.c,v 1.53 1999/12/27 13:04:53 roberto Exp roberto $
 ** Standard I/O (and system) library
 ** See Copyright Notice in lua.h
 */
@@ -395,20 +395,17 @@ static void io_write (lua_State *L) {
   FILE *f = getfileparam(L, FOUTPUT, &arg);
   int status = 1;
   lua_Object o;
-  while ((o = lua_getparam(L, arg++)) != LUA_NOOBJECT) {
-    switch (lua_type(L, o)[2]) {
-      case 'r': {  /* stRing? */
-        long l = lua_strlen(L, o);
-        status = status &&
-                 ((long)fwrite(lua_getstring(L, o), sizeof(char), l, f) == l);
-        break;
-      }
-      case 'm': /* nuMber? */  /* LUA_NUMBER */
-        /* optimization: could be done exactly as for strings */
-        status = status && fprintf(f, "%.16g", lua_getnumber(L, o)) > 0;
-        break;
-      default: luaL_argerror(L, arg-1, "string expected");
+  while ((o = lua_getparam(L, arg)) != LUA_NOOBJECT) {
+    if (lua_type(L, o)[2] == 'm') {  /* nuMber? */  /* LUA_NUMBER */
+      /* optimization: could be done exactly as for strings */
+      status = status && fprintf(f, "%.16g", lua_getnumber(L, o)) > 0;
+    }
+    else {
+      long l;
+      const char *s = luaL_check_lstr(L, arg, &l);
+      status = status && ((long)fwrite(s, sizeof(char), l, f) == l);
     }
+    arg++;
   }
   pushresult(L, status);
 }