diff options
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 90 |
1 files changed, 5 insertions, 85 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.166 2007/04/19 20:21:53 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.167 2007/05/15 18:46:12 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 | */ |
@@ -257,12 +257,6 @@ LUALIB_API const char *luaL_tostring (lua_State *L, int idx) { | |||
257 | } | 257 | } |
258 | 258 | ||
259 | 259 | ||
260 | LUALIB_API void luaL_register (lua_State *L, const char *libname, | ||
261 | const luaL_Reg *l) { | ||
262 | luaI_openlib(L, libname, l, 0); | ||
263 | } | ||
264 | |||
265 | |||
266 | static int libsize (const luaL_Reg *l) { | 260 | static int libsize (const luaL_Reg *l) { |
267 | int size = 0; | 261 | int size = 0; |
268 | for (; l->name; l++) size++; | 262 | for (; l->name; l++) size++; |
@@ -270,8 +264,8 @@ static int libsize (const luaL_Reg *l) { | |||
270 | } | 264 | } |
271 | 265 | ||
272 | 266 | ||
273 | LUALIB_API void luaI_openlib (lua_State *L, const char *libname, | 267 | LUALIB_API void luaL_register (lua_State *L, const char *libname, |
274 | const luaL_Reg *l, int nup) { | 268 | const luaL_Reg *l) { |
275 | if (libname) { | 269 | if (libname) { |
276 | int size = libsize(l); | 270 | int size = libsize(l); |
277 | /* check whether lib already exists */ | 271 | /* check whether lib already exists */ |
@@ -286,88 +280,14 @@ LUALIB_API void luaI_openlib (lua_State *L, const char *libname, | |||
286 | lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ | 280 | lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ |
287 | } | 281 | } |
288 | lua_remove(L, -2); /* remove _LOADED table */ | 282 | lua_remove(L, -2); /* remove _LOADED table */ |
289 | lua_insert(L, -(nup+1)); /* move library table to below upvalues */ | ||
290 | } | 283 | } |
291 | for (; l->name; l++) { | 284 | for (; l->name; l++) { |
292 | int i; | 285 | lua_pushcfunction(L, l->func); |
293 | for (i=0; i<nup; i++) /* copy upvalues to the top */ | 286 | lua_setfield(L, -2, l->name); |
294 | lua_pushvalue(L, -nup); | ||
295 | lua_pushcclosure(L, l->func, nup); | ||
296 | lua_setfield(L, -(nup+2), l->name); | ||
297 | } | ||
298 | lua_pop(L, nup); /* remove upvalues */ | ||
299 | } | ||
300 | |||
301 | |||
302 | |||
303 | /* | ||
304 | ** {====================================================== | ||
305 | ** getn-setn: size for arrays | ||
306 | ** ======================================================= | ||
307 | */ | ||
308 | |||
309 | #if defined(LUA_COMPAT_GETN) | ||
310 | |||
311 | static int checkint (lua_State *L, int topop) { | ||
312 | int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1; | ||
313 | lua_pop(L, topop); | ||
314 | return n; | ||
315 | } | ||
316 | |||
317 | |||
318 | static void getsizes (lua_State *L) { | ||
319 | lua_getfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); | ||
320 | if (lua_isnil(L, -1)) { /* no `size' table? */ | ||
321 | lua_pop(L, 1); /* remove nil */ | ||
322 | lua_newtable(L); /* create it */ | ||
323 | lua_pushvalue(L, -1); /* `size' will be its own metatable */ | ||
324 | lua_setmetatable(L, -2); | ||
325 | lua_pushliteral(L, "kv"); | ||
326 | lua_setfield(L, -2, "__mode"); /* metatable(N).__mode = "kv" */ | ||
327 | lua_pushvalue(L, -1); | ||
328 | lua_setfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); /* store in register */ | ||
329 | } | ||
330 | } | ||
331 | |||
332 | |||
333 | LUALIB_API void luaL_setn (lua_State *L, int t, int n) { | ||
334 | t = abs_index(L, t); | ||
335 | lua_pushliteral(L, "n"); | ||
336 | lua_rawget(L, t); | ||
337 | if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */ | ||
338 | lua_pushliteral(L, "n"); /* use it */ | ||
339 | lua_pushinteger(L, n); | ||
340 | lua_rawset(L, t); | ||
341 | } | ||
342 | else { /* use `sizes' */ | ||
343 | getsizes(L); | ||
344 | lua_pushvalue(L, t); | ||
345 | lua_pushinteger(L, n); | ||
346 | lua_rawset(L, -3); /* sizes[t] = n */ | ||
347 | lua_pop(L, 1); /* remove `sizes' */ | ||
348 | } | 287 | } |
349 | } | 288 | } |
350 | 289 | ||
351 | 290 | ||
352 | LUALIB_API int luaL_getn (lua_State *L, int t) { | ||
353 | int n; | ||
354 | t = abs_index(L, t); | ||
355 | lua_pushliteral(L, "n"); /* try t.n */ | ||
356 | lua_rawget(L, t); | ||
357 | if ((n = checkint(L, 1)) >= 0) return n; | ||
358 | getsizes(L); /* else try sizes[t] */ | ||
359 | lua_pushvalue(L, t); | ||
360 | lua_rawget(L, -2); | ||
361 | if ((n = checkint(L, 2)) >= 0) return n; | ||
362 | return (int)lua_objlen(L, t); | ||
363 | } | ||
364 | |||
365 | #endif | ||
366 | |||
367 | /* }====================================================== */ | ||
368 | |||
369 | |||
370 | |||
371 | LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, | 291 | LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, |
372 | const char *r) { | 292 | const char *r) { |
373 | const char *wild; | 293 | const char *wild; |