aboutsummaryrefslogtreecommitdiff
path: root/lauxlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2003-03-18 09:25:32 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2003-03-18 09:25:32 -0300
commit40cfb0691e233e164efee27873454cf457255a47 (patch)
treee41e02ce562545e700837f5c64ed0ac3a645ec81 /lauxlib.c
parent9b7af7e45bfe3432be8fea27e70de076b22f805f (diff)
downloadlua-40cfb0691e233e164efee27873454cf457255a47.tar.gz
lua-40cfb0691e233e164efee27873454cf457255a47.tar.bz2
lua-40cfb0691e233e164efee27873454cf457255a47.zip
new auxiliary functions for `type' manipulation
Diffstat (limited to 'lauxlib.c')
-rw-r--r--lauxlib.c54
1 files changed, 40 insertions, 14 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 8fb01bc7..291d232d 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.94 2003/02/11 09:44:38 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.95 2003/02/11 15:32:31 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*/
@@ -103,6 +103,45 @@ LUALIB_API int luaL_findstring (const char *name, const char *const list[]) {
103} 103}
104 104
105 105
106LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
107 lua_pushstring(L, tname);
108 lua_rawget(L, LUA_REGISTRYINDEX); /* get registry.name */
109 if (!lua_isnil(L, -1)) /* name already in use? */
110 return 0; /* leave previous value on top, but return 0 */
111 lua_pop(L, 1);
112 lua_newtable(L); /* create metatable */
113 lua_pushstring(L, tname);
114 lua_pushvalue(L, -2);
115 lua_rawset(L, LUA_REGISTRYINDEX); /* registry.name = metatable */
116 lua_pushvalue(L, -1);
117 lua_pushstring(L, tname);
118 lua_rawset(L, LUA_REGISTRYINDEX); /* registry[metatable] = name */
119 return 1;
120}
121
122
123LUALIB_API void luaL_getmetatable (lua_State *L, const char *tname) {
124 lua_pushstring(L, tname);
125 lua_rawget(L, LUA_REGISTRYINDEX);
126}
127
128
129LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
130 const char *tn;
131 if (!lua_getmetatable(L, ud)) return NULL; /* no metatable? */
132 lua_rawget(L, LUA_REGISTRYINDEX); /* get registry[metatable] */
133 tn = lua_tostring(L, -1);
134 if (tn && (strcmp(tn, tname) == 0)) {
135 lua_pop(L, 1);
136 return lua_touserdata(L, ud);
137 }
138 else {
139 lua_pop(L, 1);
140 return NULL;
141 }
142}
143
144
106LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) { 145LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {
107 if (!lua_checkstack(L, space)) 146 if (!lua_checkstack(L, space))
108 luaL_error(L, "stack overflow (%s)", mes); 147 luaL_error(L, "stack overflow (%s)", mes);
@@ -121,19 +160,6 @@ LUALIB_API void luaL_checkany (lua_State *L, int narg) {
121} 160}
122 161
123 162
124LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
125 if (!lua_getmetatable(L, ud)) return NULL; /* no metatable? */
126 lua_pushstring(L, tname);
127 lua_rawget(L, LUA_REGISTRYINDEX);
128 if (!lua_rawequal(L, -1, -2)) {
129 lua_pop(L, 2);
130 return NULL;
131 }
132 lua_pop(L, 2);
133 return lua_touserdata(L, ud);
134}
135
136
137LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) { 163LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {
138 const char *s = lua_tostring(L, narg); 164 const char *s = lua_tostring(L, narg);
139 if (!s) tag_error(L, narg, LUA_TSTRING); 165 if (!s) tag_error(L, narg, LUA_TSTRING);