aboutsummaryrefslogtreecommitdiff
path: root/loadlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-03-26 19:25:05 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-03-26 19:25:05 -0300
commit7e9bc41fe7539212165975478e2678e0ec9644c7 (patch)
tree8ad05abf32f4f6c82a9d70f5207f7e361d7da68b /loadlib.c
parentb1e1c15ced1f02becf578efb12dbeea6a9f01eee (diff)
downloadlua-7e9bc41fe7539212165975478e2678e0ec9644c7.tar.gz
lua-7e9bc41fe7539212165975478e2678e0ec9644c7.tar.bz2
lua-7e9bc41fe7539212165975478e2678e0ec9644c7.zip
'loadlib(lib, "*")' loads 'lib' with global names exported to all
Diffstat (limited to 'loadlib.c')
-rw-r--r--loadlib.c33
1 files changed, 20 insertions, 13 deletions
diff --git a/loadlib.c b/loadlib.c
index 6457d53d..a3863de8 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loadlib.c,v 1.60 2008/08/05 19:25:42 roberto Exp roberto $ 2** $Id: loadlib.c,v 1.61 2008/08/06 13:38:32 roberto Exp roberto $
3** Dynamic library loader for Lua 3** Dynamic library loader for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5** 5**
@@ -43,7 +43,7 @@
43 43
44 44
45static void ll_unloadlib (void *lib); 45static void ll_unloadlib (void *lib);
46static void *ll_load (lua_State *L, const char *path); 46static void *ll_load (lua_State *L, const char *path, int seeglb);
47static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym); 47static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym);
48 48
49 49
@@ -65,8 +65,8 @@ static void ll_unloadlib (void *lib) {
65} 65}
66 66
67 67
68static void *ll_load (lua_State *L, const char *path) { 68static void *ll_load (lua_State *L, const char *path, int seeglb) {
69 void *lib = dlopen(path, RTLD_NOW); 69 void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : 0));
70 if (lib == NULL) lua_pushstring(L, dlerror()); 70 if (lib == NULL) lua_pushstring(L, dlerror());
71 return lib; 71 return lib;
72} 72}
@@ -122,8 +122,9 @@ static void ll_unloadlib (void *lib) {
122} 122}
123 123
124 124
125static void *ll_load (lua_State *L, const char *path) { 125static void *ll_load (lua_State *L, const char *path, int seeglb) {
126 HINSTANCE lib = LoadLibrary(path); 126 HINSTANCE lib = LoadLibrary(path);
127 UNUSED(seeglb); /* symbols are 'global' by default? */
127 if (lib == NULL) pusherror(L); 128 if (lib == NULL) pusherror(L);
128 return lib; 129 return lib;
129} 130}
@@ -186,7 +187,7 @@ static void ll_unloadlib (void *lib) {
186} 187}
187 188
188 189
189static void *ll_load (lua_State *L, const char *path) { 190static void *ll_load (lua_State *L, const char *path, int seeglb) {
190 NSObjectFileImage img; 191 NSObjectFileImage img;
191 NSObjectFileImageReturnCode ret; 192 NSObjectFileImageReturnCode ret;
192 /* this would be a rare case, but prevents crashing if it happens */ 193 /* this would be a rare case, but prevents crashing if it happens */
@@ -196,8 +197,10 @@ static void *ll_load (lua_State *L, const char *path) {
196 } 197 }
197 ret = NSCreateObjectFileImageFromFile(path, &img); 198 ret = NSCreateObjectFileImageFromFile(path, &img);
198 if (ret == NSObjectFileImageSuccess) { 199 if (ret == NSObjectFileImageSuccess) {
199 NSModule mod = NSLinkModule(img, path, NSLINKMODULE_OPTION_PRIVATE | 200 NSModule mod = NSLinkModule(img,
200 NSLINKMODULE_OPTION_RETURN_ON_ERROR); 201 path,
202 NSLINKMODULE_OPTION_RETURN_ON_ERROR |
203 (seeglb ? 0 : NSLINKMODULE_OPTION_PRIVATE));
201 NSDestroyObjectFileImage(img); 204 NSDestroyObjectFileImage(img);
202 if (mod == NULL) pusherror(L); 205 if (mod == NULL) pusherror(L);
203 return mod; 206 return mod;
@@ -235,19 +238,19 @@ static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
235 238
236 239
237static void ll_unloadlib (void *lib) { 240static void ll_unloadlib (void *lib) {
238 (void)lib; /* to avoid warnings */ 241 UNUSED(lib); /* to avoid warnings */
239} 242}
240 243
241 244
242static void *ll_load (lua_State *L, const char *path) { 245static void *ll_load (lua_State *L, const char *path, int seeglb) {
243 (void)path; /* to avoid warnings */ 246 UNUSED(path); /* to avoid warnings */
244 lua_pushliteral(L, DLMSG); 247 lua_pushliteral(L, DLMSG);
245 return NULL; 248 return NULL;
246} 249}
247 250
248 251
249static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { 252static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
250 (void)lib; (void)sym; /* to avoid warnings */ 253 UNUSED(lib); UNUSED(sym); /* to avoid warnings */
251 lua_pushliteral(L, DLMSG); 254 lua_pushliteral(L, DLMSG);
252 return NULL; 255 return NULL;
253} 256}
@@ -291,9 +294,13 @@ static int gctm (lua_State *L) {
291 294
292static int ll_loadfunc (lua_State *L, const char *path, const char *sym) { 295static int ll_loadfunc (lua_State *L, const char *path, const char *sym) {
293 void **reg = ll_register(L, path); 296 void **reg = ll_register(L, path);
294 if (*reg == NULL) *reg = ll_load(L, path); 297 if (*reg == NULL) *reg = ll_load(L, path, *sym == '*');
295 if (*reg == NULL) 298 if (*reg == NULL)
296 return ERRLIB; /* unable to load library */ 299 return ERRLIB; /* unable to load library */
300 else if (*sym == '*') { /* loading only library (no function)? */
301 lua_pushboolean(L, 1); /* return 'true' */
302 return 0;
303 }
297 else { 304 else {
298 lua_CFunction f = ll_sym(L, *reg, sym); 305 lua_CFunction f = ll_sym(L, *reg, sym);
299 if (f == NULL) 306 if (f == NULL)