diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-09-29 18:03:14 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-09-29 18:03:14 -0300 |
commit | 36541dec9b22ecf1f85be3e64532a325ecb6f1a0 (patch) | |
tree | f447c6e256c0484d83f9e5a0a97a5588094c8577 /lauxlib.c | |
parent | cf2a194edc8b1d3e6b342a31d042e1b5fd0d2e8a (diff) | |
download | lua-36541dec9b22ecf1f85be3e64532a325ecb6f1a0.tar.gz lua-36541dec9b22ecf1f85be3e64532a325ecb6f1a0.tar.bz2 lua-36541dec9b22ecf1f85be3e64532a325ecb6f1a0.zip |
towards new package system
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 49 |
1 files changed, 32 insertions, 17 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.124 2004/09/03 13:17:14 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.125 2004/09/21 16:54:32 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 | */ |
@@ -239,16 +239,22 @@ LUALIB_API void luaL_openlib (lua_State *L, const char *libname, | |||
239 | const luaL_reg *l, int nup) { | 239 | const luaL_reg *l, int nup) { |
240 | if (libname) { | 240 | if (libname) { |
241 | /* check whether lib already exists */ | 241 | /* check whether lib already exists */ |
242 | lua_getglobal(L, libname); | 242 | luaL_getfield(L, LUA_GLOBALSINDEX, libname); |
243 | if (lua_isnil(L, -1)) { /* no? */ | 243 | if (lua_isnil(L, -1)) { /* not found? */ |
244 | lua_pop(L, 1); | 244 | lua_pop(L, 1); /* remove previous result */ |
245 | lua_newtable(L); /* create it */ | 245 | lua_newtable(L); /* create it */ |
246 | if (lua_getmetatable(L, LUA_GLOBALSINDEX)) | 246 | if (lua_getmetatable(L, LUA_GLOBALSINDEX)) |
247 | lua_setmetatable(L, -2); /* share metatable with global table */ | 247 | lua_setmetatable(L, -2); /* share metatable with global table */ |
248 | lua_pushvalue(L, -1); | ||
249 | /* register it with given name */ | 248 | /* register it with given name */ |
250 | lua_setglobal(L, libname); | 249 | lua_pushvalue(L, -1); |
250 | luaL_setfield(L, LUA_GLOBALSINDEX, libname); | ||
251 | } | 251 | } |
252 | else if (!lua_istable(L, -1)) | ||
253 | luaL_error(L, "name conflict for library `%s'", libname); | ||
254 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); | ||
255 | lua_pushvalue(L, -2); | ||
256 | lua_setfield(L, -2, libname); /* _LOADED[modname] = new table */ | ||
257 | lua_pop(L, 1); /* remove _LOADED table */ | ||
252 | lua_insert(L, -(nup+1)); /* move library table to below upvalues */ | 258 | lua_insert(L, -(nup+1)); /* move library table to below upvalues */ |
253 | } | 259 | } |
254 | for (; l->name; l++) { | 260 | for (; l->name; l++) { |
@@ -381,37 +387,45 @@ LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name, | |||
381 | } | 387 | } |
382 | fname = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); | 388 | fname = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); |
383 | lua_remove(L, -2); /* remove path template */ | 389 | lua_remove(L, -2); /* remove path template */ |
384 | f = fopen(fname, "r"); /* try to read it */ | 390 | f = fopen(fname, "r"); /* try to open it */ |
385 | if (f) { | 391 | if (f) { |
392 | int err; | ||
393 | getc(f); /* try to read file */ | ||
394 | err = ferror(f); | ||
386 | fclose(f); | 395 | fclose(f); |
387 | return fname; | 396 | if (err == 0) /* open and read sucessful? */ |
397 | return fname; /* return that file name */ | ||
388 | } | 398 | } |
389 | lua_pop(L, 1); /* remove file name */ | 399 | lua_pop(L, 1); /* remove file name */ |
390 | } | 400 | } |
391 | } | 401 | } |
392 | 402 | ||
393 | 403 | ||
394 | LUALIB_API const char *luaL_getfield (lua_State *L, const char *fname) { | 404 | LUALIB_API const char *luaL_getfield (lua_State *L, int idx, |
405 | const char *fname) { | ||
395 | const char *e; | 406 | const char *e; |
407 | lua_pushvalue(L, idx); | ||
396 | while ((e = strchr(fname, '.')) != NULL) { | 408 | while ((e = strchr(fname, '.')) != NULL) { |
397 | lua_pushlstring(L, fname, e - fname); | 409 | lua_pushlstring(L, fname, e - fname); |
398 | lua_gettable(L, -2); | 410 | lua_rawget(L, -2); |
399 | lua_remove(L, -2); /* remove previous table */ | 411 | lua_remove(L, -2); /* remove previous table */ |
400 | fname = e + 1; | 412 | fname = e + 1; |
401 | if (!lua_istable(L, -1)) return fname; | 413 | if (!lua_istable(L, -1)) return fname; |
402 | } | 414 | } |
403 | lua_getfield(L, -1, fname); /* get last field */ | 415 | lua_pushstring(L, fname); |
416 | lua_rawget(L, -2); /* get last field */ | ||
404 | lua_remove(L, -2); /* remove previous table */ | 417 | lua_remove(L, -2); /* remove previous table */ |
405 | return NULL; | 418 | return NULL; |
406 | } | 419 | } |
407 | 420 | ||
408 | 421 | ||
409 | LUALIB_API const char *luaL_setfield (lua_State *L, const char *fname) { | 422 | LUALIB_API const char *luaL_setfield (lua_State *L, int idx, |
423 | const char *fname) { | ||
410 | const char *e; | 424 | const char *e; |
411 | lua_insert(L, -2); /* move value to below table */ | 425 | lua_pushvalue(L, idx); |
412 | while ((e = strchr(fname, '.')) != NULL) { | 426 | while ((e = strchr(fname, '.')) != NULL) { |
413 | lua_pushlstring(L, fname, e - fname); | 427 | lua_pushlstring(L, fname, e - fname); |
414 | lua_gettable(L, -2); | 428 | lua_rawget(L, -2); |
415 | if (lua_isnil(L, -1)) { /* no such field? */ | 429 | if (lua_isnil(L, -1)) { /* no such field? */ |
416 | lua_pop(L, 1); /* remove this nil */ | 430 | lua_pop(L, 1); /* remove this nil */ |
417 | lua_newtable(L); /* create a new table for field */ | 431 | lua_newtable(L); /* create a new table for field */ |
@@ -426,9 +440,10 @@ LUALIB_API const char *luaL_setfield (lua_State *L, const char *fname) { | |||
426 | return fname; | 440 | return fname; |
427 | } | 441 | } |
428 | } | 442 | } |
429 | lua_insert(L, -2); /* move table to below value */ | 443 | lua_pushstring(L, fname); |
430 | lua_setfield(L, -2, fname); /* set last field */ | 444 | lua_pushvalue(L, -3); /* move value to the top */ |
431 | lua_remove(L, -2); /* remove table */ | 445 | lua_rawset(L, -3); /* set last field */ |
446 | lua_pop(L, 2); /* remove value and table */ | ||
432 | return NULL; | 447 | return NULL; |
433 | } | 448 | } |
434 | 449 | ||