diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-01-25 14:45:36 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-01-25 14:45:36 -0200 |
commit | a53d9b66ca6247818acaf41e28cdf123082a272b (patch) | |
tree | 8e909200d4d925fc7394e6adf83cc5941456c588 /lauxlib.c | |
parent | c8559e3c8d12e052783e2952db1372c68cc16059 (diff) | |
download | lua-a53d9b66ca6247818acaf41e28cdf123082a272b.tar.gz lua-a53d9b66ca6247818acaf41e28cdf123082a272b.tar.bz2 lua-a53d9b66ca6247818acaf41e28cdf123082a272b.zip |
first implementation for type names
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 28 |
1 files changed, 20 insertions, 8 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.43 2000/10/30 13:07:48 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.44 2000/12/04 18:33:40 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,14 +40,18 @@ LUALIB_API 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, int t) { | 43 | static void type_error (lua_State *L, int narg, const char *tname) { |
44 | char buff[50]; | 44 | char buff[80]; |
45 | sprintf(buff, "%.8s expected, got %.8s", lua_typename(L, t), | 45 | sprintf(buff, "%.25s expected, got %.25s", tname, lua_xtype(L, narg)); |
46 | lua_typename(L, lua_type(L, narg))); | ||
47 | luaL_argerror(L, narg, buff); | 46 | luaL_argerror(L, narg, buff); |
48 | } | 47 | } |
49 | 48 | ||
50 | 49 | ||
50 | static void tag_error (lua_State *L, int narg, int tag) { | ||
51 | type_error(L, narg, lua_typename(L, tag)); | ||
52 | } | ||
53 | |||
54 | |||
51 | LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) { | 55 | LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) { |
52 | if (space > lua_stackspace(L)) | 56 | if (space > lua_stackspace(L)) |
53 | luaL_verror(L, "stack overflow (%.30s)", mes); | 57 | luaL_verror(L, "stack overflow (%.30s)", mes); |
@@ -56,7 +60,7 @@ LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) { | |||
56 | 60 | ||
57 | LUALIB_API void luaL_checktype(lua_State *L, int narg, int t) { | 61 | LUALIB_API void luaL_checktype(lua_State *L, int narg, int t) { |
58 | if (lua_type(L, narg) != t) | 62 | if (lua_type(L, narg) != t) |
59 | type_error(L, narg, t); | 63 | tag_error(L, narg, t); |
60 | } | 64 | } |
61 | 65 | ||
62 | 66 | ||
@@ -66,9 +70,17 @@ LUALIB_API void luaL_checkany (lua_State *L, int narg) { | |||
66 | } | 70 | } |
67 | 71 | ||
68 | 72 | ||
73 | LUALIB_API void *luaL_check_userdata (lua_State *L, int narg, | ||
74 | const char *name) { | ||
75 | if (strcmp(lua_xtype(L, narg), name) != 0) | ||
76 | type_error(L, narg, name); | ||
77 | return lua_touserdata(L, narg); | ||
78 | } | ||
79 | |||
80 | |||
69 | LUALIB_API const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) { | 81 | LUALIB_API const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) { |
70 | const char *s = lua_tostring(L, narg); | 82 | const char *s = lua_tostring(L, narg); |
71 | if (!s) type_error(L, narg, LUA_TSTRING); | 83 | if (!s) tag_error(L, narg, LUA_TSTRING); |
72 | if (len) *len = lua_strlen(L, narg); | 84 | if (len) *len = lua_strlen(L, narg); |
73 | return s; | 85 | return s; |
74 | } | 86 | } |
@@ -87,7 +99,7 @@ LUALIB_API const char *luaL_opt_lstr (lua_State *L, int narg, const char *def, s | |||
87 | LUALIB_API lua_Number luaL_check_number (lua_State *L, int narg) { | 99 | LUALIB_API lua_Number luaL_check_number (lua_State *L, int narg) { |
88 | lua_Number d = lua_tonumber(L, narg); | 100 | lua_Number d = lua_tonumber(L, narg); |
89 | if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ | 101 | if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ |
90 | type_error(L, narg, LUA_TNUMBER); | 102 | tag_error(L, narg, LUA_TNUMBER); |
91 | return d; | 103 | return d; |
92 | } | 104 | } |
93 | 105 | ||