diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-10-17 19:12:57 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-10-17 19:12:57 -0200 |
commit | 1e81da51bab87148981486a84b846399050f4ef2 (patch) | |
tree | d2c94326ca096e032d1ae3fa75a5d0605f494cc6 /lbaselib.c | |
parent | 7cd37142f404462634a5049a840f572e85c5762b (diff) | |
download | lua-1e81da51bab87148981486a84b846399050f4ef2.tar.gz lua-1e81da51bab87148981486a84b846399050f4ef2.tar.bz2 lua-1e81da51bab87148981486a84b846399050f4ef2.zip |
new API for registry and C upvalues + new implementation for references
Diffstat (limited to 'lbaselib.c')
-rw-r--r-- | lbaselib.c | 25 |
1 files changed, 9 insertions, 16 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.41 2001/08/31 19:46:07 roberto Exp $ | 2 | ** $Id: lbaselib.c,v 1.43 2001/10/11 21:41:21 roberto Exp $ |
3 | ** Basic library | 3 | ** Basic library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -363,18 +363,8 @@ static int luaB_require (lua_State *L) { | |||
363 | lua_pushvalue(L, -1); /* duplicate to leave a copy on stack */ | 363 | lua_pushvalue(L, -1); /* duplicate to leave a copy on stack */ |
364 | lua_setglobal(L, LUA_PATH); | 364 | lua_setglobal(L, LUA_PATH); |
365 | } | 365 | } |
366 | lua_getregistry(L); | 366 | lua_pushvalue(L, 1); /* check package's name in book-keeping table */ |
367 | lua_pushliteral(L, LUA_PATH); | 367 | lua_gettable(L, lua_upvalueindex(1)); |
368 | lua_gettable(L, 3); /* get book-keeping table */ | ||
369 | if (lua_isnil(L, 4)) { /* no book-keeping table? */ | ||
370 | lua_pop(L, 1); /* pop the `nil' */ | ||
371 | lua_newtable(L); /* create book-keeping table */ | ||
372 | lua_pushliteral(L, LUA_PATH); | ||
373 | lua_pushvalue(L, -2); /* duplicate table to leave a copy on stack */ | ||
374 | lua_settable(L, 3); /* store book-keeping table in registry */ | ||
375 | } | ||
376 | lua_pushvalue(L, 1); | ||
377 | lua_gettable(L, 4); /* check package's name in book-keeping table */ | ||
378 | if (!lua_isnil(L, -1)) /* is it there? */ | 368 | if (!lua_isnil(L, -1)) /* is it there? */ |
379 | return 0; /* package is already loaded */ | 369 | return 0; /* package is already loaded */ |
380 | else { /* must load it */ | 370 | else { /* must load it */ |
@@ -385,7 +375,7 @@ static int luaB_require (lua_State *L) { | |||
385 | lua_pushvalue(L, 1); /* package name */ | 375 | lua_pushvalue(L, 1); /* package name */ |
386 | lua_concat(L, 2); /* concat directory with package name */ | 376 | lua_concat(L, 2); /* concat directory with package name */ |
387 | res = lua_dofile(L, lua_tostring(L, -1)); /* try to load it */ | 377 | res = lua_dofile(L, lua_tostring(L, -1)); /* try to load it */ |
388 | lua_settop(L, 4); /* pop string and eventual results from dofile */ | 378 | lua_settop(L, 2); /* pop string and eventual results from dofile */ |
389 | if (res == 0) break; /* ok; file done */ | 379 | if (res == 0) break; /* ok; file done */ |
390 | else if (res != LUA_ERRFILE) | 380 | else if (res != LUA_ERRFILE) |
391 | lua_error(L, NULL); /* error running package; propagate it */ | 381 | lua_error(L, NULL); /* error running package; propagate it */ |
@@ -397,7 +387,7 @@ static int luaB_require (lua_State *L) { | |||
397 | } | 387 | } |
398 | lua_pushvalue(L, 1); | 388 | lua_pushvalue(L, 1); |
399 | lua_pushnumber(L, 1); | 389 | lua_pushnumber(L, 1); |
400 | lua_settable(L, 4); /* mark it as loaded */ | 390 | lua_settable(L, lua_upvalueindex(1)); /* mark it as loaded */ |
401 | return 0; | 391 | return 0; |
402 | } | 392 | } |
403 | 393 | ||
@@ -713,7 +703,6 @@ static const luaL_reg base_funcs[] = { | |||
713 | {l_s("rawgettable"), luaB_rawget}, /* for compatibility 3.2 */ | 703 | {l_s("rawgettable"), luaB_rawget}, /* for compatibility 3.2 */ |
714 | {l_s("rawsettable"), luaB_rawset}, /* for compatibility 3.2 */ | 704 | {l_s("rawsettable"), luaB_rawset}, /* for compatibility 3.2 */ |
715 | {l_s("rawtype"), luaB_rawtype}, | 705 | {l_s("rawtype"), luaB_rawtype}, |
716 | {l_s("require"), luaB_require}, | ||
717 | {l_s("setglobal"), luaB_setglobal}, | 706 | {l_s("setglobal"), luaB_setglobal}, |
718 | {l_s("settag"), luaB_settype}, /* for compatibility 4.0 */ | 707 | {l_s("settag"), luaB_settype}, /* for compatibility 4.0 */ |
719 | {l_s("settype"), luaB_settype}, | 708 | {l_s("settype"), luaB_settype}, |
@@ -737,6 +726,10 @@ LUALIB_API int lua_baselibopen (lua_State *L) { | |||
737 | luaL_openl(L, base_funcs); | 726 | luaL_openl(L, base_funcs); |
738 | lua_pushliteral(L, l_s(LUA_VERSION)); | 727 | lua_pushliteral(L, l_s(LUA_VERSION)); |
739 | lua_setglobal(L, l_s("_VERSION")); | 728 | lua_setglobal(L, l_s("_VERSION")); |
729 | /* `require' needs an empty table as upvalue */ | ||
730 | lua_newtable(L); | ||
731 | lua_pushcclosure(L, luaB_require, 1); | ||
732 | lua_setglobal(L, l_s("require")); | ||
740 | return 0; | 733 | return 0; |
741 | } | 734 | } |
742 | 735 | ||