From 7b90a331bf1caecfa1ba1e2f3e7f87fa01c389b9 Mon Sep 17 00:00:00 2001 From: moteus Date: Thu, 26 Dec 2013 13:08:45 +0400 Subject: Fix. load library on Lua 5.2 --- src/l52util.c | 5 +++++ src/l52util.h | 5 +++++ src/llthread.c | 24 ++++++++++++------------ 3 files changed, 22 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/l52util.c b/src/l52util.c index 592c1d1..0b40c3e 100644 --- a/src/l52util.c +++ b/src/l52util.c @@ -43,6 +43,11 @@ void lua_rawsetp (lua_State *L, int index, const void *p){ lua_rawset(L, index); } +void lutil_require(lua_State *L, const char* name, lua_CFunction fn, int glb) { + // @fixme generate error if we can not load module + lua_cpcall(L, fn, NULL); +} + #endif int lutil_newmetatablep (lua_State *L, const void *p) { diff --git a/src/l52util.h b/src/l52util.h index f4f0497..94f1bfa 100644 --- a/src/l52util.h +++ b/src/l52util.h @@ -18,6 +18,9 @@ int luaL_typerror (lua_State *L, int narg, const char *tname); void luaL_register (lua_State *L, const char *libname, const luaL_Reg *l); + +#define lutil_require luaL_requiref + #else // lua 5.1 // functions form lua 5.2 @@ -29,6 +32,8 @@ void lua_rawgetp (lua_State *L, int index, const void *p); void lua_rawsetp (lua_State *L, int index, const void *p); void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup); +void lutil_require(lua_State *L, const char* name, lua_CFunction fn, int glb); + #endif int lutil_newmetatablep (lua_State *L, const void *p); diff --git a/src/llthread.c b/src/llthread.c index 6cb437b..0449ea9 100644 --- a/src/llthread.c +++ b/src/llthread.c @@ -308,26 +308,26 @@ typedef struct llthread_t { static void open_thread_libs(lua_State *L){ #ifdef LLTHREAD_REGISTER_STD_LIBRARY -# define L_REGLIB(L, name) lua_pushcfunction(L, luaopen_##name); lua_setfield(L, -2, #name) +# define L_REGLIB(L, name, G) lua_pushcfunction(L, luaopen_##name); lua_setfield(L, -2, #name) #else -# define L_REGLIB(L, name) lua_cpcall(L, luaopen_##name, 0) +# define L_REGLIB(L, name, G) lutil_require(L, #name, luaopen_##name, G) #endif int top = lua_gettop(L); - lua_cpcall(L, luaopen_base, 0); - lua_cpcall(L, luaopen_package, 0); + lutil_require(L, "_G", luaopen_base, 1); + lutil_require(L, "package", luaopen_package, 1); lua_settop(L, top); /* get package.preload */ lua_getglobal(L, "package"); lua_getfield(L, -1, "preload"); lua_remove(L, -2); - L_REGLIB(L, io ); - L_REGLIB(L, os ); - L_REGLIB(L, math ); - L_REGLIB(L, table ); - L_REGLIB(L, debug ); - L_REGLIB(L, string ); - L_REGLIB(L, llthreads ); + L_REGLIB(L, io, 1); + L_REGLIB(L, os, 1); + L_REGLIB(L, math, 1); + L_REGLIB(L, table, 1); + L_REGLIB(L, debug, 1); + L_REGLIB(L, string, 1); + L_REGLIB(L, llthreads, 0); lua_settop(L, top); #undef L_REGLIB @@ -446,7 +446,7 @@ static int llthread_start(llthread_t *this, int start_detached) { #ifndef USE_PTHREAD this->thread = (HANDLE)_beginthreadex(NULL, 0, llthread_child_thread_run, child, 0, NULL); if(INVALID_THREAD == this->thread){ - rc = -1 + rc = -1; } #else rc = pthread_create(&(this->thread), NULL, llthread_child_thread_run, child); -- cgit v1.2.3-55-g6feb