lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

>I'm wondering if a library that uses such general global names as "close"
>would conflict too easily with other Lua programs.
>A limited solution would be to make lua_socklibopen() take an optional table
>name to insert the functions into instead of the global scope.  But this
>breaks down as soon as some other library depends on socklib and expects the
>names to be in a certain place (global).

In 4.0, you can do this:		(untested!)

 lua_getglobals(L);			/* save old table of globals */
 lua_newtable(L);			/* create new table for library */
 lua_setglobal(L,"socket");		/* stored it in library name */
 lua_setglobals(L);			/* use new table as globals */
 lua_socklibopen(L):			/* open library "inside" new table */
 lua_setglobals(L);			/* restore old table of globals */

Now, you can access "close" as "socket.close".

Perhaps a function like the one above could be added to lauxlib.
Something like
 void luaL_openmodule(lua_State* L, const char* name, void (*f)(lua_State*))