diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-12-28 09:52:49 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-12-28 09:52:49 -0200 |
commit | fb602839744a1e4d1c966ca4ab5640231c155cd3 (patch) | |
tree | faa0f49d4840654d401e511dc186642a46eeb806 | |
parent | acdb0b741e31adebfa4f608f8bf23e65fa68d741 (diff) | |
download | lua-fb602839744a1e4d1c966ca4ab5640231c155cd3.tar.gz lua-fb602839744a1e4d1c966ca4ab5640231c155cd3.tar.bz2 lua-fb602839744a1e4d1c966ca4ab5640231c155cd3.zip |
better error messages
-rw-r--r-- | lauxlib.c | 64 | ||||
-rw-r--r-- | liolib.c | 25 |
2 files changed, 48 insertions, 41 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.22 1999/12/20 13:09:45 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.23 1999/12/27 17:33:22 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -30,69 +30,79 @@ int luaL_findstring (const char *name, const char *const list[]) { | |||
30 | return -1; /* name not found */ | 30 | return -1; /* name not found */ |
31 | } | 31 | } |
32 | 32 | ||
33 | void luaL_argerror (lua_State *L, int numarg, const char *extramsg) { | 33 | void luaL_argerror (lua_State *L, int narg, const char *extramsg) { |
34 | lua_Function f = lua_stackedfunction(L, 0); | 34 | lua_Function f = lua_stackedfunction(L, 0); |
35 | const char *funcname; | 35 | const char *funcname; |
36 | lua_getobjname(L, f, &funcname); | 36 | lua_getobjname(L, f, &funcname); |
37 | numarg -= lua_nups(L, f); | 37 | narg -= lua_nups(L, f); |
38 | if (funcname == NULL) | 38 | if (funcname == NULL) |
39 | funcname = "?"; | 39 | funcname = "?"; |
40 | luaL_verror(L, "bad argument #%d to function `%.50s' (%.100s)", | 40 | luaL_verror(L, "bad argument #%d to `%.50s' (%.100s)", |
41 | numarg, funcname, extramsg); | 41 | narg, funcname, extramsg); |
42 | } | 42 | } |
43 | 43 | ||
44 | static const char *checkstr (lua_State *L, lua_Object o, int n, long *len) { | 44 | |
45 | static void type_error (lua_State *L, int narg, const char *typename, | ||
46 | lua_Object o) { | ||
47 | char buff[100]; | ||
48 | const char *otype = (o == LUA_NOOBJECT) ? "no value" : lua_type(L, o); | ||
49 | sprintf(buff, "%.10s expected, got %.10s", typename, otype); | ||
50 | luaL_argerror(L, narg, buff); | ||
51 | } | ||
52 | |||
53 | |||
54 | static const char *checkstr (lua_State *L, lua_Object o, int narg, long *len) { | ||
45 | const char *s = lua_getstring(L, o); | 55 | const char *s = lua_getstring(L, o); |
46 | luaL_arg_check(L, s, n, "string expected"); | 56 | if (!s) type_error(L, narg, "string", o); |
47 | if (len) *len = lua_strlen(L, o); | 57 | if (len) *len = lua_strlen(L, o); |
48 | return s; | 58 | return s; |
49 | } | 59 | } |
50 | 60 | ||
51 | const char *luaL_check_lstr (lua_State *L, int n, long *len) { | 61 | const char *luaL_check_lstr (lua_State *L, int narg, long *len) { |
52 | return checkstr(L, lua_getparam(L, n), n, len); | 62 | return checkstr(L, lua_getparam(L, narg), narg, len); |
53 | } | 63 | } |
54 | 64 | ||
55 | const char *luaL_opt_lstr (lua_State *L, int n, const char *def, long *len) { | 65 | const char *luaL_opt_lstr (lua_State *L, int narg, const char *def, long *len) { |
56 | lua_Object o = lua_getparam(L, n); | 66 | lua_Object o = lua_getparam(L, narg); |
57 | if (o == LUA_NOOBJECT) { | 67 | if (o == LUA_NOOBJECT) { |
58 | if (len) *len = def ? strlen(def) : 0; | 68 | if (len) *len = def ? strlen(def) : 0; |
59 | return def; | 69 | return def; |
60 | } | 70 | } |
61 | else return checkstr(L, o, n, len); | 71 | else return checkstr(L, o, narg, len); |
62 | } | 72 | } |
63 | 73 | ||
64 | double luaL_check_number (lua_State *L, int n) { | 74 | double luaL_check_number (lua_State *L, int narg) { |
65 | lua_Object o = lua_getparam(L, n); | 75 | lua_Object o = lua_getparam(L, narg); |
66 | luaL_arg_check(L, lua_isnumber(L, o), n, "number expected"); | 76 | if (!lua_isnumber(L, o)) type_error(L, narg, "number", o); |
67 | return lua_getnumber(L, o); | 77 | return lua_getnumber(L, o); |
68 | } | 78 | } |
69 | 79 | ||
70 | 80 | ||
71 | double luaL_opt_number (lua_State *L, int n, double def) { | 81 | double luaL_opt_number (lua_State *L, int narg, double def) { |
72 | lua_Object o = lua_getparam(L, n); | 82 | lua_Object o = lua_getparam(L, narg); |
73 | if (o == LUA_NOOBJECT) return def; | 83 | if (o == LUA_NOOBJECT) return def; |
74 | else { | 84 | else { |
75 | luaL_arg_check(L, lua_isnumber(L, o), n, "number expected"); | 85 | if (!lua_isnumber(L, o)) type_error(L, narg, "number", o); |
76 | return lua_getnumber(L, o); | 86 | return lua_getnumber(L, o); |
77 | } | 87 | } |
78 | } | 88 | } |
79 | 89 | ||
80 | 90 | ||
81 | lua_Object luaL_tablearg (lua_State *L, int arg) { | 91 | lua_Object luaL_tablearg (lua_State *L, int narg) { |
82 | lua_Object o = lua_getparam(L, arg); | 92 | lua_Object o = lua_getparam(L, narg); |
83 | luaL_arg_check(L, lua_istable(L, o), arg, "table expected"); | 93 | if (!lua_istable(L, o)) type_error(L, narg, "table", o); |
84 | return o; | 94 | return o; |
85 | } | 95 | } |
86 | 96 | ||
87 | lua_Object luaL_functionarg (lua_State *L, int arg) { | 97 | lua_Object luaL_functionarg (lua_State *L, int narg) { |
88 | lua_Object o = lua_getparam(L, arg); | 98 | lua_Object o = lua_getparam(L, narg); |
89 | luaL_arg_check(L, lua_isfunction(L, o), arg, "function expected"); | 99 | if (!lua_isfunction(L, o)) type_error(L, narg, "function", o); |
90 | return o; | 100 | return o; |
91 | } | 101 | } |
92 | 102 | ||
93 | lua_Object luaL_nonnullarg (lua_State *L, int n) { | 103 | lua_Object luaL_nonnullarg (lua_State *L, int narg) { |
94 | lua_Object o = lua_getparam(L, n); | 104 | lua_Object o = lua_getparam(L, narg); |
95 | luaL_arg_check(L, o != LUA_NOOBJECT, n, "value expected"); | 105 | luaL_arg_check(L, o != LUA_NOOBJECT, narg, "value expected"); |
96 | return o; | 106 | return o; |
97 | } | 107 | } |
98 | 108 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 1.52 1999/11/22 17:39:51 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 1.53 1999/12/27 13:04:53 roberto Exp roberto $ |
3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -395,20 +395,17 @@ static void io_write (lua_State *L) { | |||
395 | FILE *f = getfileparam(L, FOUTPUT, &arg); | 395 | FILE *f = getfileparam(L, FOUTPUT, &arg); |
396 | int status = 1; | 396 | int status = 1; |
397 | lua_Object o; | 397 | lua_Object o; |
398 | while ((o = lua_getparam(L, arg++)) != LUA_NOOBJECT) { | 398 | while ((o = lua_getparam(L, arg)) != LUA_NOOBJECT) { |
399 | switch (lua_type(L, o)[2]) { | 399 | if (lua_type(L, o)[2] == 'm') { /* nuMber? */ /* LUA_NUMBER */ |
400 | case 'r': { /* stRing? */ | 400 | /* optimization: could be done exactly as for strings */ |
401 | long l = lua_strlen(L, o); | 401 | status = status && fprintf(f, "%.16g", lua_getnumber(L, o)) > 0; |
402 | status = status && | 402 | } |
403 | ((long)fwrite(lua_getstring(L, o), sizeof(char), l, f) == l); | 403 | else { |
404 | break; | 404 | long l; |
405 | } | 405 | const char *s = luaL_check_lstr(L, arg, &l); |
406 | case 'm': /* nuMber? */ /* LUA_NUMBER */ | 406 | status = status && ((long)fwrite(s, sizeof(char), l, f) == l); |
407 | /* optimization: could be done exactly as for strings */ | ||
408 | status = status && fprintf(f, "%.16g", lua_getnumber(L, o)) > 0; | ||
409 | break; | ||
410 | default: luaL_argerror(L, arg-1, "string expected"); | ||
411 | } | 407 | } |
408 | arg++; | ||
412 | } | 409 | } |
413 | pushresult(L, status); | 410 | pushresult(L, status); |
414 | } | 411 | } |