diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-10-10 09:57:55 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-10-10 09:57:55 -0300 |
commit | 533737f26e3f8036d7978e09427ea5ff75aec9df (patch) | |
tree | 003004a423cf20bc4e1493918760494b0d694f93 /lauxlib.c | |
parent | a41d60e1d1f3a954648884d4ab7fb7e9ccdd52d6 (diff) | |
download | lua-533737f26e3f8036d7978e09427ea5ff75aec9df.tar.gz lua-533737f26e3f8036d7978e09427ea5ff75aec9df.tar.bz2 lua-533737f26e3f8036d7978e09427ea5ff75aec9df.zip |
new functions `lua_getfield' and `lua_setfield'
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 43 |
1 files changed, 17 insertions, 26 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.104 2003/10/02 20:31:17 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.105 2003/10/07 20:13:41 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 | */ |
@@ -108,15 +108,13 @@ LUALIB_API int luaL_findstring (const char *name, const char *const list[]) { | |||
108 | 108 | ||
109 | 109 | ||
110 | LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { | 110 | LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { |
111 | lua_pushstring(L, tname); | 111 | lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get registry.name */ |
112 | lua_rawget(L, LUA_REGISTRYINDEX); /* get registry.name */ | ||
113 | if (!lua_isnil(L, -1)) /* name already in use? */ | 112 | if (!lua_isnil(L, -1)) /* name already in use? */ |
114 | return 0; /* leave previous value on top, but return 0 */ | 113 | return 0; /* leave previous value on top, but return 0 */ |
115 | lua_pop(L, 1); | 114 | lua_pop(L, 1); |
116 | lua_newtable(L); /* create metatable */ | 115 | lua_newtable(L); /* create metatable */ |
117 | lua_pushstring(L, tname); | 116 | lua_pushvalue(L, -1); |
118 | lua_pushvalue(L, -2); | 117 | lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ |
119 | lua_rawset(L, LUA_REGISTRYINDEX); /* registry.name = metatable */ | ||
120 | lua_pushvalue(L, -1); | 118 | lua_pushvalue(L, -1); |
121 | lua_pushstring(L, tname); | 119 | lua_pushstring(L, tname); |
122 | lua_rawset(L, LUA_REGISTRYINDEX); /* registry[metatable] = name */ | 120 | lua_rawset(L, LUA_REGISTRYINDEX); /* registry[metatable] = name */ |
@@ -125,8 +123,7 @@ LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { | |||
125 | 123 | ||
126 | 124 | ||
127 | LUALIB_API void luaL_getmetatable (lua_State *L, const char *tname) { | 125 | LUALIB_API void luaL_getmetatable (lua_State *L, const char *tname) { |
128 | lua_pushstring(L, tname); | 126 | lua_getfield(L, LUA_REGISTRYINDEX, tname); |
129 | lua_rawget(L, LUA_REGISTRYINDEX); | ||
130 | } | 127 | } |
131 | 128 | ||
132 | 129 | ||
@@ -215,8 +212,7 @@ LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg, | |||
215 | LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { | 212 | LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { |
216 | if (!lua_getmetatable(L, obj)) /* no metatable? */ | 213 | if (!lua_getmetatable(L, obj)) /* no metatable? */ |
217 | return 0; | 214 | return 0; |
218 | lua_pushstring(L, event); | 215 | lua_getfield(L, -1, event); |
219 | lua_rawget(L, -2); | ||
220 | if (lua_isnil(L, -1)) { | 216 | if (lua_isnil(L, -1)) { |
221 | lua_pop(L, 2); /* remove metatable and metafield */ | 217 | lua_pop(L, 2); /* remove metatable and metafield */ |
222 | return 0; | 218 | return 0; |
@@ -241,24 +237,23 @@ LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { | |||
241 | LUALIB_API void luaL_openlib (lua_State *L, const char *libname, | 237 | LUALIB_API void luaL_openlib (lua_State *L, const char *libname, |
242 | const luaL_reg *l, int nup) { | 238 | const luaL_reg *l, int nup) { |
243 | if (libname) { | 239 | if (libname) { |
244 | lua_pushstring(L, libname); | 240 | /* check whether lib already exists */ |
245 | lua_gettable(L, LUA_GLOBALSINDEX); /* check whether lib already exists */ | 241 | lua_getfield(L, LUA_GLOBALSINDEX, libname); |
246 | if (lua_isnil(L, -1)) { /* no? */ | 242 | if (lua_isnil(L, -1)) { /* no? */ |
247 | lua_pop(L, 1); | 243 | lua_pop(L, 1); |
248 | lua_newtable(L); /* create it */ | 244 | lua_newtable(L); /* create it */ |
249 | lua_pushstring(L, libname); | 245 | lua_pushvalue(L, -1); |
250 | lua_pushvalue(L, -2); | 246 | /* register it with given name */ |
251 | lua_settable(L, LUA_GLOBALSINDEX); /* register it with given name */ | 247 | lua_setfield(L, LUA_GLOBALSINDEX, libname); |
252 | } | 248 | } |
253 | lua_insert(L, -(nup+1)); /* move library table to below upvalues */ | 249 | lua_insert(L, -(nup+1)); /* move library table to below upvalues */ |
254 | } | 250 | } |
255 | for (; l->name; l++) { | 251 | for (; l->name; l++) { |
256 | int i; | 252 | int i; |
257 | lua_pushstring(L, l->name); | ||
258 | for (i=0; i<nup; i++) /* copy upvalues to the top */ | 253 | for (i=0; i<nup; i++) /* copy upvalues to the top */ |
259 | lua_pushvalue(L, -(nup+1)); | 254 | lua_pushvalue(L, -nup); |
260 | lua_pushcclosure(L, l->func, nup); | 255 | lua_pushcclosure(L, l->func, nup); |
261 | lua_settable(L, -(nup+3)); | 256 | lua_setfield(L, -(nup+2), l->name); |
262 | } | 257 | } |
263 | lua_pop(L, nup); /* remove upvalues */ | 258 | lua_pop(L, nup); /* remove upvalues */ |
264 | } | 259 | } |
@@ -286,9 +281,8 @@ static void getsizes (lua_State *L) { | |||
286 | lua_newtable(L); /* create it */ | 281 | lua_newtable(L); /* create it */ |
287 | lua_pushvalue(L, -1); /* `size' will be its own metatable */ | 282 | lua_pushvalue(L, -1); /* `size' will be its own metatable */ |
288 | lua_setmetatable(L, -2); | 283 | lua_setmetatable(L, -2); |
289 | lua_pushliteral(L, "__mode"); | ||
290 | lua_pushliteral(L, "kv"); | 284 | lua_pushliteral(L, "kv"); |
291 | lua_rawset(L, -3); /* metatable(N).__mode = "kv" */ | 285 | lua_setfield(L, -2, "__mode"); /* metatable(N).__mode = "kv" */ |
292 | lua_pushvalue(L, -1); | 286 | lua_pushvalue(L, -1); |
293 | lua_rawseti(L, LUA_REGISTRYINDEX, ARRAYSIZE_REF); /* store in register */ | 287 | lua_rawseti(L, LUA_REGISTRYINDEX, ARRAYSIZE_REF); /* store in register */ |
294 | } | 288 | } |
@@ -297,12 +291,10 @@ static void getsizes (lua_State *L) { | |||
297 | 291 | ||
298 | void luaL_setn (lua_State *L, int t, int n) { | 292 | void luaL_setn (lua_State *L, int t, int n) { |
299 | t = abs_index(L, t); | 293 | t = abs_index(L, t); |
300 | lua_pushliteral(L, "n"); | 294 | lua_getfield(L, t, "n"); |
301 | lua_rawget(L, t); | ||
302 | if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */ | 295 | if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */ |
303 | lua_pushliteral(L, "n"); /* use it */ | ||
304 | lua_pushinteger(L, n); | 296 | lua_pushinteger(L, n); |
305 | lua_rawset(L, t); | 297 | lua_setfield(L, t, "n"); |
306 | } | 298 | } |
307 | else { /* use `sizes' */ | 299 | else { /* use `sizes' */ |
308 | getsizes(L); | 300 | getsizes(L); |
@@ -317,8 +309,7 @@ void luaL_setn (lua_State *L, int t, int n) { | |||
317 | int luaL_getn (lua_State *L, int t) { | 309 | int luaL_getn (lua_State *L, int t) { |
318 | int n; | 310 | int n; |
319 | t = abs_index(L, t); | 311 | t = abs_index(L, t); |
320 | lua_pushliteral(L, "n"); /* try t.n */ | 312 | lua_getfield(L, t, "n"); /* try t.n */ |
321 | lua_rawget(L, t); | ||
322 | if ((n = checkint(L, 1)) >= 0) return n; | 313 | if ((n = checkint(L, 1)) >= 0) return n; |
323 | getsizes(L); /* else try sizes[t] */ | 314 | getsizes(L); /* else try sizes[t] */ |
324 | lua_pushvalue(L, t); | 315 | lua_pushvalue(L, t); |