summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Pall <mike>2012-10-12 14:42:40 +0200
committerMike Pall <mike>2012-10-12 14:42:40 +0200
commitb50375a11ca0366091a7d43b9e842dc9fb6d0875 (patch)
treeef6bc3ec7a460b0be5889643ae5c701a0104cafe
parentfff12979fec8a8483d17e6a8ae05cd2e881f4b99 (diff)
downloadluajit-b50375a11ca0366091a7d43b9e842dc9fb6d0875.tar.gz
luajit-b50375a11ca0366091a7d43b9e842dc9fb6d0875.tar.bz2
luajit-b50375a11ca0366091a7d43b9e842dc9fb6d0875.zip
From Lua 5.2: Add package.loadlib(libname, "*").
-rw-r--r--src/lib_package.c28
1 files changed, 16 insertions, 12 deletions
diff --git a/src/lib_package.c b/src/lib_package.c
index ca5a5a16..1794a3d4 100644
--- a/src/lib_package.c
+++ b/src/lib_package.c
@@ -3,7 +3,7 @@
3** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h 3** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
4** 4**
5** Major portions taken verbatim or adapted from the Lua interpreter. 5** Major portions taken verbatim or adapted from the Lua interpreter.
6** Copyright (C) 1994-2011 Lua.org, PUC-Rio. See Copyright Notice in lua.h 6** Copyright (C) 1994-2012 Lua.org, PUC-Rio. See Copyright Notice in lua.h
7*/ 7*/
8 8
9#define lib_package_c 9#define lib_package_c
@@ -41,9 +41,9 @@ static void ll_unloadlib(void *lib)
41 dlclose(lib); 41 dlclose(lib);
42} 42}
43 43
44static void *ll_load(lua_State *L, const char *path) 44static void *ll_load(lua_State *L, const char *path, int gl)
45{ 45{
46 void *lib = dlopen(path, RTLD_NOW); 46 void *lib = dlopen(path, RTLD_NOW | (gl ? RTLD_GLOBAL : RTLD_LOCAL));
47 if (lib == NULL) lua_pushstring(L, dlerror()); 47 if (lib == NULL) lua_pushstring(L, dlerror());
48 return lib; 48 return lib;
49} 49}
@@ -112,10 +112,11 @@ static void ll_unloadlib(void *lib)
112 FreeLibrary((HINSTANCE)lib); 112 FreeLibrary((HINSTANCE)lib);
113} 113}
114 114
115static void *ll_load(lua_State *L, const char *path) 115static void *ll_load(lua_State *L, const char *path, int gl)
116{ 116{
117 HINSTANCE lib = LoadLibraryA(path); 117 HINSTANCE lib = LoadLibraryA(path);
118 if (lib == NULL) pusherror(L); 118 if (lib == NULL) pusherror(L);
119 UNUSED(gl);
119 return lib; 120 return lib;
120} 121}
121 122
@@ -149,26 +150,26 @@ static const char *ll_bcsym(void *lib, const char *sym)
149 150
150static void ll_unloadlib(void *lib) 151static void ll_unloadlib(void *lib)
151{ 152{
152 (void)lib; 153 UNUSED(lib);
153} 154}
154 155
155static void *ll_load(lua_State *L, const char *path) 156static void *ll_load(lua_State *L, const char *path, int gl)
156{ 157{
157 (void)path; 158 UNUSED(path); UNUSED(gl);
158 lua_pushliteral(L, DLMSG); 159 lua_pushliteral(L, DLMSG);
159 return NULL; 160 return NULL;
160} 161}
161 162
162static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym) 163static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym)
163{ 164{
164 (void)lib; (void)sym; 165 UNUSED(lib); UNUSED(sym);
165 lua_pushliteral(L, DLMSG); 166 lua_pushliteral(L, DLMSG);
166 return NULL; 167 return NULL;
167} 168}
168 169
169static const char *ll_bcsym(void *lib, const char *sym) 170static const char *ll_bcsym(void *lib, const char *sym)
170{ 171{
171 (void)lib; (void)sym; 172 UNUSED(lib); UNUSED(sym);
172 return NULL; 173 return NULL;
173} 174}
174 175
@@ -211,9 +212,12 @@ static const char *mksymname(lua_State *L, const char *modname,
211static int ll_loadfunc(lua_State *L, const char *path, const char *name, int r) 212static int ll_loadfunc(lua_State *L, const char *path, const char *name, int r)
212{ 213{
213 void **reg = ll_register(L, path); 214 void **reg = ll_register(L, path);
214 if (*reg == NULL) *reg = ll_load(L, path); 215 if (*reg == NULL) *reg = ll_load(L, path, (*name == '*'));
215 if (*reg == NULL) { 216 if (*reg == NULL) {
216 return PACKAGE_ERR_LIB; /* unable to load library */ 217 return PACKAGE_ERR_LIB; /* Unable to load library. */
218 } else if (*name == '*') { /* Only load library into global namespace. */
219 lua_pushboolean(L, 1);
220 return 0;
217 } else { 221 } else {
218 const char *sym = r ? name : mksymname(L, name, SYMPREFIX_CF); 222 const char *sym = r ? name : mksymname(L, name, SYMPREFIX_CF);
219 lua_CFunction f = ll_sym(L, *reg, sym); 223 lua_CFunction f = ll_sym(L, *reg, sym);
@@ -230,7 +234,7 @@ static int ll_loadfunc(lua_State *L, const char *path, const char *name, int r)
230 return 0; 234 return 0;
231 } 235 }
232 } 236 }
233 return PACKAGE_ERR_FUNC; /* unable to find function */ 237 return PACKAGE_ERR_FUNC; /* Unable to find function. */
234 } 238 }
235} 239}
236 240