lua-users home
lua-l archive

Re: Standard Lua modules in 5.2.1

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


The following works for me in both 5.1 and 5.2.
John
static const luaL_Reg lualibs[] = {
#if LUA_VERSION_NUM < 502
 {"", luaopen_base},
#else
 {"_G", luaopen_base},
#endif
 {LUA_TABLIBNAME, luaopen_table},
 {LUA_STRLIBNAME, luaopen_string},
 {NULL, NULL}
};
lua_State *
dl_open(void)
{
 lua_State *L = luaL_newstate();
 const luaL_Reg *lib = lualibs; /* Load libraries used by the */
 for (; lib->func; lib++) {	 /* Lua datalog program. */
#if LUA_VERSION_NUM < 502
 lua_pushcfunction(L, lib->func);
 lua_pushstring(L, lib->name);
 lua_call(L, 1, 0);
#else
 luaL_requiref(L, lib->name, lib->func, 1);
 lua_pop(L, 1);		/* remove lib */
#endif
 }
 if (dl_lua(L))		/* Load the Lua program. */
 return NULL;
 else
 return L;
}
On Thu, Jul 5, 2012 at 5:48 PM, Luiz Henrique de Figueiredo
<lhf@tecgraf.puc-rio.br> wrote:
>> The following minimal code does not set the modules.( OOLUA::run_chunk
>> just runs the chunk checking for error etc it is not important.)
>
> In Lua 5.2, libraries no longer create globals; they just return
> the library table.
>
> luaL_openlibs calls luaL_requiref instead of calling luaopen_*,
> and luaL_requiref sets the corresponding globals.
>

AltStyle によって変換されたページ (->オリジナル) /