11/*
2- ** $Id: loadlib.c,v 1.120 2014/11/02 19:19:04 roberto Exp roberto $
2+ ** $Id: loadlib.c,v 1.121 2014/11/03 15:11:10 roberto Exp roberto $
33** Dynamic library loader for Lua
44** See Copyright Notice in lua.h
55**
8585#define LUA_OFSEP "_"
8686
8787
88- /* table (in the registry) that keeps handles for all loaded C libraries */
89- #define CLIBS "_CLIBS"
88+ /*
89+ ** unique key for table in the registry that keeps handles
90+ ** for all loaded C libraries
91+ */
92+ static const int CLIBS = 0 ;
9093
9194#define LIB_FAIL "open"
9295
@@ -261,7 +264,7 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
261264*/
262265static void * checkclib (lua_State * L , const char * path ) {
263266 void * plib ;
264- lua_getfield (L , LUA_REGISTRYINDEX , CLIBS );
267+ lua_rawgetp (L , LUA_REGISTRYINDEX , & CLIBS );
265268 lua_getfield (L , -1 , path );
266269 plib = lua_touserdata (L , -1 ); /* plib = CLIBS[path] */
267270 lua_pop (L , 2 ); /* pop CLIBS table and 'plib' */
@@ -274,7 +277,7 @@ static void *checkclib (lua_State *L, const char *path) {
274277** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries
275278*/
276279static void addtoclib (lua_State * L , const char * path , void * plib ) {
277- lua_getfield (L , LUA_REGISTRYINDEX , CLIBS );
280+ lua_rawgetp (L , LUA_REGISTRYINDEX , & CLIBS );
278281 lua_pushlightuserdata (L , plib );
279282 lua_pushvalue (L , -1 );
280283 lua_setfield (L , -3 , path ); /* CLIBS[path] = plib */
@@ -735,11 +738,12 @@ static void createsearcherstable (lua_State *L) {
735738** setting a finalizer to close all libraries when closing state.
736739*/
737740static void createclibstable (lua_State * L ) {
738- luaL_getsubtable ( L , LUA_REGISTRYINDEX , CLIBS ); /* create CLIBS table */
741+ lua_newtable ( L ); /* create CLIBS table */
739742 lua_createtable (L , 0 , 1 ); /* create metatable for CLIBS */
740743 lua_pushcfunction (L , gctm );
741744 lua_setfield (L , -2 , "__gc" ); /* set finalizer for CLIBS table */
742745 lua_setmetatable (L , -2 );
746+ lua_rawsetp (L , LUA_REGISTRYINDEX , & CLIBS ); /* set CLIBS table in registry */
743747}
744748
745749
0 commit comments