diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-10-02 17:10:55 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-10-02 17:10:55 -0300 |
| commit | f6834f4393eaa1055c2bbde82ebb33cc58be8371 (patch) | |
| tree | 3583008ef181106d0fc7e130300f12adc70a5854 /lauxlib.c | |
| parent | 78bc8e553d4190fc3b90be5b621fc0f3507586ef (diff) | |
| download | lua-f6834f4393eaa1055c2bbde82ebb33cc58be8371.tar.gz lua-f6834f4393eaa1055c2bbde82ebb33cc58be8371.tar.bz2 lua-f6834f4393eaa1055c2bbde82ebb33cc58be8371.zip | |
new API function `lua_type' + new type lua_Type
Diffstat (limited to 'lauxlib.c')
| -rw-r--r-- | lauxlib.c | 29 |
1 files changed, 15 insertions, 14 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.c,v 1.36 2000/09/12 13:48:22 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.37 2000/09/29 12:40:56 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 | */ |
| @@ -40,11 +40,11 @@ void luaL_argerror (lua_State *L, int narg, const char *extramsg) { | |||
| 40 | } | 40 | } |
| 41 | 41 | ||
| 42 | 42 | ||
| 43 | static void type_error (lua_State *L, int narg, const char *type_name) { | 43 | static void type_error (lua_State *L, int narg, lua_Type t) { |
| 44 | char buff[100]; | 44 | char buff[100]; |
| 45 | const char *rt = lua_type(L, narg); | 45 | const char *rt = lua_typename(L, lua_type(L, narg)); |
| 46 | if (*rt == 'N') rt = "no value"; | 46 | if (*rt == 'N') rt = "no value"; |
| 47 | sprintf(buff, "%.10s expected, got %.10s", type_name, rt); | 47 | sprintf(buff, "%.10s expected, got %.10s", lua_typename(L, t), rt); |
| 48 | luaL_argerror(L, narg, buff); | 48 | luaL_argerror(L, narg, buff); |
| 49 | } | 49 | } |
| 50 | 50 | ||
| @@ -55,20 +55,21 @@ void luaL_checkstack (lua_State *L, int space, const char *mes) { | |||
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | 57 | ||
| 58 | /* | 58 | void luaL_checktype(lua_State *L, int narg, lua_Type t) { |
| 59 | ** use the 3rd letter of type names for testing: | 59 | if (lua_type(L, narg) != t) |
| 60 | ** nuMber, niL, stRing, fuNction, usErdata, taBle, anY | 60 | type_error(L, narg, t); |
| 61 | */ | 61 | } |
| 62 | void luaL_checktype(lua_State *L, int narg, const char *tname) { | 62 | |
| 63 | const char *rt = lua_type(L, narg); | 63 | |
| 64 | if (!(*rt != 'N' && (tname[2] == 'y' || tname[2] == rt[2]))) | 64 | void luaL_checkany (lua_State *L, int narg) { |
| 65 | type_error(L, narg, tname); | 65 | if (lua_type(L, narg) == LUA_NOVALUE) |
| 66 | luaL_argerror(L, narg, "value expected"); | ||
| 66 | } | 67 | } |
| 67 | 68 | ||
| 68 | 69 | ||
| 69 | const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) { | 70 | const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) { |
| 70 | const char *s = lua_tostring(L, narg); | 71 | const char *s = lua_tostring(L, narg); |
| 71 | if (!s) type_error(L, narg, "string"); | 72 | if (!s) type_error(L, narg, LUA_TSTRING); |
| 72 | if (len) *len = lua_strlen(L, narg); | 73 | if (len) *len = lua_strlen(L, narg); |
| 73 | return s; | 74 | return s; |
| 74 | } | 75 | } |
| @@ -88,7 +89,7 @@ const char *luaL_opt_lstr (lua_State *L, int narg, const char *def, | |||
| 88 | double luaL_check_number (lua_State *L, int narg) { | 89 | double luaL_check_number (lua_State *L, int narg) { |
| 89 | double d = lua_tonumber(L, narg); | 90 | double d = lua_tonumber(L, narg); |
| 90 | if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ | 91 | if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ |
| 91 | type_error(L, narg, "number"); | 92 | type_error(L, narg, LUA_TNUMBER); |
| 92 | return d; | 93 | return d; |
| 93 | } | 94 | } |
| 94 | 95 | ||
