aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-12-22 15:43:27 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-12-22 15:43:27 -0200
commit96727c61b87d199b51a3ebb707255e79d5ed1b84 (patch)
treec99da1452411280bb649910908ac58a32df93197
parentd5ebc3ff6de4b5f7019bb887e917e00cb3a0a899 (diff)
downloadlua-96727c61b87d199b51a3ebb707255e79d5ed1b84.tar.gz
lua-96727c61b87d199b51a3ebb707255e79d5ed1b84.tar.bz2
lua-96727c61b87d199b51a3ebb707255e79d5ed1b84.zip
several improvements/corrections
-rw-r--r--loadlib.c173
-rw-r--r--luaconf.h67
2 files changed, 136 insertions, 104 deletions
diff --git a/loadlib.c b/loadlib.c
index cb70baab..ff946429 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loadlib.c,v 1.11 2004/11/19 15:52:12 roberto Exp roberto $ 2** $Id: loadlib.c,v 1.12 2004/12/13 12:14:21 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*
@@ -58,26 +58,23 @@ static void registerlib (lua_State *L, const void *lib);
58 58
59#define freelib dlclose 59#define freelib dlclose
60 60
61static int loadlib(lua_State *L, const char *path, const char *init) 61static int loadlib(lua_State *L, const char *path, const char *init) {
62{ 62 void *lib=dlopen(path,RTLD_NOW);
63 void *lib=dlopen(path,RTLD_NOW); 63 if (lib != NULL) {
64 if (lib!=NULL) 64 lua_CFunction f=(lua_CFunction) dlsym(lib,init);
65 { 65 if (f != NULL) {
66 lua_CFunction f=(lua_CFunction) dlsym(lib,init); 66 registerlib(L, lib);
67 if (f!=NULL) 67 lua_pushcfunction(L,f);
68 { 68 return 0;
69 registerlib(L, lib); 69 }
70 lua_pushcfunction(L,f); 70 }
71 return 0; 71 /* else return appropriate error message */
72 } 72 lua_pushstring(L,dlerror());
73 } 73 if (lib != NULL) {
74 /* else return appropriate error message */ 74 dlclose(lib);
75 lua_pushstring(L,dlerror()); 75 return ERR_FUNCTION; /* error loading function */
76 if (lib!=NULL) { 76 }
77 dlclose(lib); 77 else return ERR_OPEN; /* error loading library */
78 return ERR_FUNCTION; /* error loading function */
79 }
80 else return ERR_OPEN; /* error loading library */
81} 78}
82 79
83 80
@@ -91,36 +88,34 @@ static int loadlib(lua_State *L, const char *path, const char *init)
91 88
92#define freelib(lib) FreeLibrary((HINSTANCE)lib) 89#define freelib(lib) FreeLibrary((HINSTANCE)lib)
93 90
94static void pusherror(lua_State *L) 91
95{ 92static void pusherror(lua_State *L) {
96 int error=GetLastError(); 93 int error = GetLastError();
97 char buffer[128]; 94 char buffer[128];
98 if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, 95 if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
99 0, error, 0, buffer, sizeof(buffer), 0)) 96 0, error, 0, buffer, sizeof(buffer), 0))
100 lua_pushstring(L,buffer); 97 lua_pushstring(L,buffer);
101 else 98 else
102 lua_pushfstring(L,"system error %d\n",error); 99 lua_pushfstring(L, "system error %d\n", error);
103} 100}
104 101
105static int loadlib(lua_State *L, const char *path, const char *init) 102
106{ 103static int loadlib (lua_State *L, const char *path, const char *init) {
107 HINSTANCE lib=LoadLibrary(path); 104 HINSTANCE lib = LoadLibrary(path);
108 if (lib!=NULL) 105 if (lib != NULL) {
109 { 106 lua_CFunction f = (lua_CFunction)GetProcAddress(lib, init);
110 lua_CFunction f=(lua_CFunction) GetProcAddress(lib,init); 107 if (f != NULL) {
111 if (f!=NULL) 108 registerlib(L, lib);
112 { 109 lua_pushcfunction(L, f);
113 registerlib(L, lib); 110 return 0;
114 lua_pushcfunction(L,f); 111 }
115 return 1;
116 } 112 }
117 } 113 pusherror(L);
118 pusherror(L); 114 if (lib != NULL) {
119 if (lib!=NULL) { 115 FreeLibrary(lib);
120 FreeLibrary(lib); 116 return ERR_OPEN;
121 return ERR_OPEN; 117 }
122 } 118 else return ERR_FUNCTION;
123 else return ERR_FUNCTION;
124} 119}
125 120
126 121
@@ -134,21 +129,22 @@ static int loadlib(lua_State *L, const char *path, const char *init)
134/* Mach cannot unload images (?) */ 129/* Mach cannot unload images (?) */
135#define freelib(lib) ((void)lib) 130#define freelib(lib) ((void)lib)
136 131
137static void pusherror (lua_State *L) 132
138{ 133static void pusherror (lua_State *L) {
139 const char *err_str; 134 const char *err_str;
140 const char *err_file; 135 const char *err_file;
141 NSLinkEditErrors err; 136 NSLinkEditErrors err;
142 int err_num; 137 int err_num;
143 NSLinkEditError(&err, &err_num, &err_file, &err_str); 138 NSLinkEditError(&err, &err_num, &err_file, &err_str);
144 lua_pushstring(L, err_str); 139 lua_pushstring(L, err_str);
145} 140}
146 141
142
147static int loadlib (lua_State *L, const char *path, const char *init) { 143static int loadlib (lua_State *L, const char *path, const char *init) {
148 const struct mach_header *lib; 144 const struct mach_header *lib;
149 /* this would be a rare case, but prevents crashing if it happens */ 145 /* this would be a rare case, but prevents crashing if it happens */
150 if(!_dyld_present()) { 146 if(!_dyld_present()) {
151 lua_pushstring(L,"dyld not present."); 147 lua_pushstring(L, "dyld not present.");
152 return ERR_OPEN; 148 return ERR_OPEN;
153 } 149 }
154 lib = NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR); 150 lib = NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
@@ -159,7 +155,7 @@ static int loadlib (lua_State *L, const char *path, const char *init) {
159 if(init_sym != NULL) { 155 if(init_sym != NULL) {
160 lua_CFunction f = (lua_CFunction)NSAddressOfSymbol(init_sym); 156 lua_CFunction f = (lua_CFunction)NSAddressOfSymbol(init_sym);
161 registerlib(L, lib); 157 registerlib(L, lib);
162 lua_pushcfunction(L,f); 158 lua_pushcfunction(L, f);
163 return 0; 159 return 0;
164 } 160 }
165 } 161 }
@@ -177,11 +173,10 @@ static int loadlib (lua_State *L, const char *path, const char *init) {
177 173
178#define freelib(lib) ((void)lib) 174#define freelib(lib) ((void)lib)
179 175
180static int loadlib(lua_State *L, const char *path, const char *init) 176static int loadlib (lua_State *L, const char *path, const char *init) {
181{ 177 (void)path; (void)init; (void)&registerlib; /* to avoid warnings */
182 (void)path; (void)init; (void)&registerlib; /* to avoid warnings */ 178 lua_pushliteral(L,"`loadlib' not supported");
183 lua_pushliteral(L,"`loadlib' not supported"); 179 return ERR_ABSENT;
184 return ERR_ABSENT;
185} 180}
186 181
187#endif 182#endif
@@ -192,41 +187,39 @@ static int loadlib(lua_State *L, const char *path, const char *init)
192** so that, when Lua closes its state, the library's __gc metamethod 187** so that, when Lua closes its state, the library's __gc metamethod
193** will be called to unload the library. 188** will be called to unload the library.
194*/ 189*/
195static void registerlib (lua_State *L, const void *lib) 190static void registerlib (lua_State *L, const void *lib) {
196{ 191 const void **plib = (const void **)lua_newuserdata(L, sizeof(const void *));
197 const void **plib = (const void **)lua_newuserdata(L, sizeof(const void *)); 192 *plib = lib;
198 *plib = lib; 193 luaL_getmetatable(L, "_LOADLIB");
199 luaL_getmetatable(L, "_LOADLIB"); 194 lua_setmetatable(L, -2);
200 lua_setmetatable(L, -2); 195 lua_pushboolean(L, 1);
201 lua_pushboolean(L, 1); 196 lua_settable(L, LUA_REGISTRYINDEX); /* registry[lib] = true */
202 lua_settable(L, LUA_REGISTRYINDEX); /* registry[lib] = true */
203} 197}
204 198
205/* 199/*
206** __gc tag method: calls library's `freelib' function with the lib 200** __gc tag method: calls library's `freelib' function with the lib
207** handle 201** handle
208*/ 202*/
209static int gctm (lua_State *L) 203static int gctm (lua_State *L) {
210{ 204 void *lib = *(void **)luaL_checkudata(L, 1, "_LOADLIB");
211 void *lib = *(void **)luaL_checkudata(L, 1, "_LOADLIB"); 205 freelib(lib);
212 freelib(lib); 206 return 0;
213 return 0;
214} 207}
215 208
216 209
217static int ll_loadlib (lua_State *L) { 210static int ll_loadlib (lua_State *L) {
218 static const char *const errcodes[] = {NULL, "open", "init", "absent"}; 211 static const char *const errcodes[] = {NULL, "open", "init", "absent"};
219 const char *path=luaL_checkstring(L,1); 212 const char *path = luaL_checkstring(L, 1);
220 const char *init=luaL_checkstring(L,2); 213 const char *init = luaL_checkstring(L, 2);
221 int res = loadlib(L,path,init); 214 int res = loadlib(L,path,init);
222 if (res == 0) /* no error? */ 215 if (res == 0) /* no error? */
223 return 1; /* function is at stack top */ 216 return 1; /* function is at stack top */
224 else { /* error */ 217 else { /* error */
225 lua_pushnil(L); 218 lua_pushnil(L);
226 lua_insert(L,-2); /* insert nil before error message */ 219 lua_insert(L, -2); /* insert nil before error message */
227 lua_pushstring(L, errcodes[res]); 220 lua_pushstring(L, errcodes[res]);
228 return 3; 221 return 3;
229 } 222 }
230} 223}
231 224
232 225
@@ -264,7 +257,7 @@ static const char *loadC (lua_State *L, const char *fname, const char *name) {
264 luaL_getfield(L, LUA_GLOBALSINDEX, "package.cpath"); 257 luaL_getfield(L, LUA_GLOBALSINDEX, "package.cpath");
265 path = lua_tostring(L, -1); 258 path = lua_tostring(L, -1);
266 if (path == NULL) 259 if (path == NULL)
267 luaL_error(L, "global _CPATH must be a string"); 260 luaL_error(L, "`package.cpath' must be a string");
268 fname = luaL_searchpath(L, fname, path); 261 fname = luaL_searchpath(L, fname, path);
269 if (fname == NULL) return path; /* library not found in this path */ 262 if (fname == NULL) return path; /* library not found in this path */
270 funcname = luaL_gsub(L, name, ".", LUA_OFSEP); 263 funcname = luaL_gsub(L, name, ".", LUA_OFSEP);
@@ -346,6 +339,8 @@ static int ll_module (lua_State *L) {
346 lua_pushvalue(L, LUA_GLOBALSINDEX); 339 lua_pushvalue(L, LUA_GLOBALSINDEX);
347 lua_setfield(L, -2, "__index"); /* mt.__index = _G */ 340 lua_setfield(L, -2, "__index"); /* mt.__index = _G */
348 lua_setmetatable(L, -2); 341 lua_setmetatable(L, -2);
342 lua_pushvalue(L, -1);
343 lua_setfield(L, -2, "_M"); /* module._M = module */
349 lua_pushstring(L, modname); 344 lua_pushstring(L, modname);
350 lua_setfield(L, -2, "_NAME"); 345 lua_setfield(L, -2, "_NAME");
351 dot = strrchr(modname, '.'); /* look for last dot in module name */ 346 dot = strrchr(modname, '.'); /* look for last dot in module name */
diff --git a/luaconf.h b/luaconf.h
index c7c2ee98..aab35b6f 100644
--- a/luaconf.h
+++ b/luaconf.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: luaconf.h,v 1.19 2004/12/01 15:52:54 roberto Exp roberto $ 2** $Id: luaconf.h,v 1.21 2004/12/13 12:08:34 roberto Exp $
3** Configuration file for Lua 3** Configuration file for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -31,10 +31,22 @@
31*/ 31*/
32 32
33/* default path */ 33/* default path */
34#define LUA_PATH_DEFAULT \ 34#if defined(_WIN32)
35 "./?.lua;/usr/local/share/lua/5.0/?.lua;/usr/local/share/lua/5.0/?/init.lua" 35#define LUA_ROOT "C:\\Program Files\\Lua51"
36#define LUA_CPATH_DEFAULT \ 36#define LUA_LDIR LUA_ROOT "\\lua"
37 "./?.so;/usr/local/lib/lua/5.0/?.so;/usr/local/lib/lua/5.0/lib?.so" 37#define LUA_CDIR LUA_ROOT "\\dll"
38#define LUA_PATH_DEFAULT \
39 "?.lua;" LUA_LDIR "\\?.lua;" LUA_LDIR "\\?\\init.lua"
40#define LUA_CPATH_DEFAULT "?.dll;" LUA_CDIR "\\?.dll"
41
42#else
43#define LUA_ROOT "/usr/local"
44#define LUA_LDIR LUA_ROOT "/share/lua/5.1"
45#define LUA_CDIR LUA_ROOT "/lib/lua/5.1"
46#define LUA_PATH_DEFAULT \
47 "./?.lua;" LUA_LDIR "/?.lua;" LUA_LDIR "/?/init.lua"
48#define LUA_CPATH_DEFAULT "./?.so;" LUA_CDIR "/?.so"
49#endif
38 50
39 51
40 52
@@ -57,10 +69,10 @@
57#define LUA_API extern 69#define LUA_API extern
58 70
59/* mark for auxlib functions */ 71/* mark for auxlib functions */
60#define LUALIB_API extern 72#define LUALIB_API extern
61 73
62/* buffer size used by lauxlib buffer system */ 74/* buffer size used by lauxlib buffer system */
63#define LUAL_BUFFERSIZE BUFSIZ 75#define LUAL_BUFFERSIZE BUFSIZ
64 76
65 77
66/* assertions in Lua (mainly for internal debugging) */ 78/* assertions in Lua (mainly for internal debugging) */
@@ -82,6 +94,10 @@
82#ifdef _POSIX_C_SOURCE 94#ifdef _POSIX_C_SOURCE
83#include <unistd.h> 95#include <unistd.h>
84#define stdin_is_tty() isatty(0) 96#define stdin_is_tty() isatty(0)
97#elif defined(_WIN32)
98#include <io.h>
99#include <fcntl.h>
100#define stdin_is_tty() _isatty(_fileno(stdin))
85#else 101#else
86#define stdin_is_tty() 1 /* assume stdin is a tty */ 102#define stdin_is_tty() 1 /* assume stdin is a tty */
87#endif 103#endif
@@ -200,14 +216,31 @@
200/* function to convert a lua_Number to int (with any rounding method) */ 216/* function to convert a lua_Number to int (with any rounding method) */
201#if defined(__GNUC__) && defined(__i386) 217#if defined(__GNUC__) && defined(__i386)
202#define lua_number2int(i,d) __asm__ ("fistpl %0":"=m"(i):"t"(d):"st") 218#define lua_number2int(i,d) __asm__ ("fistpl %0":"=m"(i):"t"(d):"st")
219
220#elif defined(_WIN32) && defined(_M_IX86)
221#include <math.h>
222#pragma warning(disable: 4514)
223__inline int l_lrint (double flt)
224{ int i;
225 _asm {
226 fld flt
227 fistp i
228 };
229 return i;
230}
231#define lua_number2int(i,d) ((i)=l_lrint((d)))
232
203#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199900L) 233#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199900L)
204/* on machines compliant with C99, you can try `lrint' */ 234/* on machines compliant with C99, you can try `lrint' */
205#include <math.h> 235#include <math.h>
206#define lua_number2int(i,d) ((i)=lrint(d)) 236#define lua_number2int(i,d) ((i)=lrint(d))
237
207#else 238#else
208#define lua_number2int(i,d) ((i)=(int)(d)) 239#define lua_number2int(i,d) ((i)=(int)(d))
240
209#endif 241#endif
210 242
243
211/* function to convert a lua_Number to lua_Integer (with any rounding method) */ 244/* function to convert a lua_Number to lua_Integer (with any rounding method) */
212#define lua_number2integer(i,n) lua_number2int((i), (n)) 245#define lua_number2integer(i,n) lua_number2int((i), (n))
213 246
@@ -258,8 +291,8 @@
258** or when reading immutable fields from global objects 291** or when reading immutable fields from global objects
259** (such as string values and udata values). 292** (such as string values and udata values).
260*/ 293*/
261#define lua_lock(L) ((void) 0) 294#define lua_lock(L) ((void) 0)
262#define lua_unlock(L) ((void) 0) 295#define lua_unlock(L) ((void) 0)
263 296
264/* 297/*
265** this macro allows a thread switch in appropriate places in the Lua 298** this macro allows a thread switch in appropriate places in the Lua
@@ -273,9 +306,6 @@
273#define lua_userstateopen(L) /* empty */ 306#define lua_userstateopen(L) /* empty */
274 307
275 308
276/* initial GC parameters */
277#define STEPMUL 4
278
279#endif 309#endif
280 310
281/* }====================================================== */ 311/* }====================================================== */
@@ -305,8 +335,15 @@
305#define LUA_POF "luaopen_" 335#define LUA_POF "luaopen_"
306#endif 336#endif
307 337
338/* separator for open functions in C libraries */
339#define LUA_OFSEP ""
340
308/* directory separator (for submodules) */ 341/* directory separator (for submodules) */
342#if defined(_WIN32)
343#define LUA_DIRSEP "\\"
344#else
309#define LUA_DIRSEP "/" 345#define LUA_DIRSEP "/"
346#endif
310 347
311/* separator of templates in a path */ 348/* separator of templates in a path */
312#define LUA_PATH_SEP ';' 349#define LUA_PATH_SEP ';'
@@ -332,12 +369,12 @@
332/* 369/*
333** Configuration for loadlib 370** Configuration for loadlib
334*/ 371*/
335#if defined(__linux) || defined(sun) || defined(sgi) || defined(BSD) 372#if defined(_WIN32)
336#define USE_DLOPEN
337#elif defined(_WIN32)
338#define USE_DLL 373#define USE_DLL
339#elif defined(__APPLE__) && defined(__MACH__) 374#elif defined(__APPLE__) && defined(__MACH__)
340#define USE_DYLD 375#define USE_DYLD
376#elif defined(__linux) || defined(sun) || defined(sgi) || defined(BSD)
377#define USE_DLOPEN
341#endif 378#endif
342 379
343 380