1
0
mirror of https://github.com/lua/lua synced 2025-05-01 09:48:05 +03:00

several improvements/corrections

This commit is contained in:
Roberto Ierusalimschy 2004-12-22 15:43:27 -02:00
parent d5ebc3ff6d
commit 96727c61b8
2 changed files with 136 additions and 104 deletions

@ -1,5 +1,5 @@
/* /*
** $Id: loadlib.c,v 1.11 2004/11/19 15:52:12 roberto Exp roberto $ ** $Id: loadlib.c,v 1.12 2004/12/13 12:14:21 roberto Exp roberto $
** Dynamic library loader for Lua ** Dynamic library loader for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
* *
@ -58,14 +58,11 @@ static void registerlib (lua_State *L, const void *lib);
#define freelib dlclose #define freelib dlclose
static int loadlib(lua_State *L, const char *path, const char *init) static int loadlib(lua_State *L, const char *path, const char *init) {
{
void *lib=dlopen(path,RTLD_NOW); void *lib=dlopen(path,RTLD_NOW);
if (lib!=NULL) if (lib != NULL) {
{
lua_CFunction f=(lua_CFunction) dlsym(lib,init); lua_CFunction f=(lua_CFunction) dlsym(lib,init);
if (f!=NULL) if (f != NULL) {
{
registerlib(L, lib); registerlib(L, lib);
lua_pushcfunction(L,f); lua_pushcfunction(L,f);
return 0; return 0;
@ -91,8 +88,8 @@ static int loadlib(lua_State *L, const char *path, const char *init)
#define freelib(lib) FreeLibrary((HINSTANCE)lib) #define freelib(lib) FreeLibrary((HINSTANCE)lib)
static void pusherror(lua_State *L)
{ static void pusherror(lua_State *L) {
int error = GetLastError(); int error = GetLastError();
char buffer[128]; char buffer[128];
if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
@ -102,17 +99,15 @@ static void pusherror(lua_State *L)
lua_pushfstring(L, "system error %d\n", error); lua_pushfstring(L, "system error %d\n", error);
} }
static int loadlib(lua_State *L, const char *path, const char *init)
{ static int loadlib (lua_State *L, const char *path, const char *init) {
HINSTANCE lib = LoadLibrary(path); HINSTANCE lib = LoadLibrary(path);
if (lib!=NULL) if (lib != NULL) {
{
lua_CFunction f = (lua_CFunction)GetProcAddress(lib, init); lua_CFunction f = (lua_CFunction)GetProcAddress(lib, init);
if (f!=NULL) if (f != NULL) {
{
registerlib(L, lib); registerlib(L, lib);
lua_pushcfunction(L, f); lua_pushcfunction(L, f);
return 1; return 0;
} }
} }
pusherror(L); pusherror(L);
@ -134,8 +129,8 @@ static int loadlib(lua_State *L, const char *path, const char *init)
/* Mach cannot unload images (?) */ /* Mach cannot unload images (?) */
#define freelib(lib) ((void)lib) #define freelib(lib) ((void)lib)
static void pusherror (lua_State *L)
{ static void pusherror (lua_State *L) {
const char *err_str; const char *err_str;
const char *err_file; const char *err_file;
NSLinkEditErrors err; NSLinkEditErrors err;
@ -144,6 +139,7 @@ static void pusherror (lua_State *L)
lua_pushstring(L, err_str); lua_pushstring(L, err_str);
} }
static int loadlib (lua_State *L, const char *path, const char *init) { static int loadlib (lua_State *L, const char *path, const char *init) {
const struct mach_header *lib; const struct mach_header *lib;
/* this would be a rare case, but prevents crashing if it happens */ /* this would be a rare case, but prevents crashing if it happens */
@ -177,8 +173,7 @@ static int loadlib (lua_State *L, const char *path, const char *init) {
#define freelib(lib) ((void)lib) #define freelib(lib) ((void)lib)
static int loadlib(lua_State *L, const char *path, const char *init) static int loadlib (lua_State *L, const char *path, const char *init) {
{
(void)path; (void)init; (void)&registerlib; /* to avoid warnings */ (void)path; (void)init; (void)&registerlib; /* to avoid warnings */
lua_pushliteral(L,"`loadlib' not supported"); lua_pushliteral(L,"`loadlib' not supported");
return ERR_ABSENT; return ERR_ABSENT;
@ -192,8 +187,7 @@ static int loadlib(lua_State *L, const char *path, const char *init)
** so that, when Lua closes its state, the library's __gc metamethod ** so that, when Lua closes its state, the library's __gc metamethod
** will be called to unload the library. ** will be called to unload the library.
*/ */
static void registerlib (lua_State *L, const void *lib) static void registerlib (lua_State *L, const void *lib) {
{
const void **plib = (const void **)lua_newuserdata(L, sizeof(const void *)); const void **plib = (const void **)lua_newuserdata(L, sizeof(const void *));
*plib = lib; *plib = lib;
luaL_getmetatable(L, "_LOADLIB"); luaL_getmetatable(L, "_LOADLIB");
@ -206,8 +200,7 @@ static void registerlib (lua_State *L, const void *lib)
** __gc tag method: calls library's `freelib' function with the lib ** __gc tag method: calls library's `freelib' function with the lib
** handle ** handle
*/ */
static int gctm (lua_State *L) static int gctm (lua_State *L) {
{
void *lib = *(void **)luaL_checkudata(L, 1, "_LOADLIB"); void *lib = *(void **)luaL_checkudata(L, 1, "_LOADLIB");
freelib(lib); freelib(lib);
return 0; return 0;
@ -264,7 +257,7 @@ static const char *loadC (lua_State *L, const char *fname, const char *name) {
luaL_getfield(L, LUA_GLOBALSINDEX, "package.cpath"); luaL_getfield(L, LUA_GLOBALSINDEX, "package.cpath");
path = lua_tostring(L, -1); path = lua_tostring(L, -1);
if (path == NULL) if (path == NULL)
luaL_error(L, "global _CPATH must be a string"); luaL_error(L, "`package.cpath' must be a string");
fname = luaL_searchpath(L, fname, path); fname = luaL_searchpath(L, fname, path);
if (fname == NULL) return path; /* library not found in this path */ if (fname == NULL) return path; /* library not found in this path */
funcname = luaL_gsub(L, name, ".", LUA_OFSEP); funcname = luaL_gsub(L, name, ".", LUA_OFSEP);
@ -346,6 +339,8 @@ static int ll_module (lua_State *L) {
lua_pushvalue(L, LUA_GLOBALSINDEX); lua_pushvalue(L, LUA_GLOBALSINDEX);
lua_setfield(L, -2, "__index"); /* mt.__index = _G */ lua_setfield(L, -2, "__index"); /* mt.__index = _G */
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "_M"); /* module._M = module */
lua_pushstring(L, modname); lua_pushstring(L, modname);
lua_setfield(L, -2, "_NAME"); lua_setfield(L, -2, "_NAME");
dot = strrchr(modname, '.'); /* look for last dot in module name */ dot = strrchr(modname, '.'); /* look for last dot in module name */

@ -1,5 +1,5 @@
/* /*
** $Id: luaconf.h,v 1.19 2004/12/01 15:52:54 roberto Exp roberto $ ** $Id: luaconf.h,v 1.21 2004/12/13 12:08:34 roberto Exp $
** Configuration file for Lua ** Configuration file for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -31,10 +31,22 @@
*/ */
/* default path */ /* default path */
#if defined(_WIN32)
#define LUA_ROOT "C:\\Program Files\\Lua51"
#define LUA_LDIR LUA_ROOT "\\lua"
#define LUA_CDIR LUA_ROOT "\\dll"
#define LUA_PATH_DEFAULT \ #define LUA_PATH_DEFAULT \
"./?.lua;/usr/local/share/lua/5.0/?.lua;/usr/local/share/lua/5.0/?/init.lua" "?.lua;" LUA_LDIR "\\?.lua;" LUA_LDIR "\\?\\init.lua"
#define LUA_CPATH_DEFAULT \ #define LUA_CPATH_DEFAULT "?.dll;" LUA_CDIR "\\?.dll"
"./?.so;/usr/local/lib/lua/5.0/?.so;/usr/local/lib/lua/5.0/lib?.so"
#else
#define LUA_ROOT "/usr/local"
#define LUA_LDIR LUA_ROOT "/share/lua/5.1"
#define LUA_CDIR LUA_ROOT "/lib/lua/5.1"
#define LUA_PATH_DEFAULT \
"./?.lua;" LUA_LDIR "/?.lua;" LUA_LDIR "/?/init.lua"
#define LUA_CPATH_DEFAULT "./?.so;" LUA_CDIR "/?.so"
#endif
@ -82,6 +94,10 @@
#ifdef _POSIX_C_SOURCE #ifdef _POSIX_C_SOURCE
#include <unistd.h> #include <unistd.h>
#define stdin_is_tty() isatty(0) #define stdin_is_tty() isatty(0)
#elif defined(_WIN32)
#include <io.h>
#include <fcntl.h>
#define stdin_is_tty() _isatty(_fileno(stdin))
#else #else
#define stdin_is_tty() 1 /* assume stdin is a tty */ #define stdin_is_tty() 1 /* assume stdin is a tty */
#endif #endif
@ -200,14 +216,31 @@
/* function to convert a lua_Number to int (with any rounding method) */ /* function to convert a lua_Number to int (with any rounding method) */
#if defined(__GNUC__) && defined(__i386) #if defined(__GNUC__) && defined(__i386)
#define lua_number2int(i,d) __asm__ ("fistpl %0":"=m"(i):"t"(d):"st") #define lua_number2int(i,d) __asm__ ("fistpl %0":"=m"(i):"t"(d):"st")
#elif defined(_WIN32) && defined(_M_IX86)
#include <math.h>
#pragma warning(disable: 4514)
__inline int l_lrint (double flt)
{ int i;
_asm {
fld flt
fistp i
};
return i;
}
#define lua_number2int(i,d) ((i)=l_lrint((d)))
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199900L) #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199900L)
/* on machines compliant with C99, you can try `lrint' */ /* on machines compliant with C99, you can try `lrint' */
#include <math.h> #include <math.h>
#define lua_number2int(i,d) ((i)=lrint(d)) #define lua_number2int(i,d) ((i)=lrint(d))
#else #else
#define lua_number2int(i,d) ((i)=(int)(d)) #define lua_number2int(i,d) ((i)=(int)(d))
#endif #endif
/* function to convert a lua_Number to lua_Integer (with any rounding method) */ /* function to convert a lua_Number to lua_Integer (with any rounding method) */
#define lua_number2integer(i,n) lua_number2int((i), (n)) #define lua_number2integer(i,n) lua_number2int((i), (n))
@ -273,9 +306,6 @@
#define lua_userstateopen(L) /* empty */ #define lua_userstateopen(L) /* empty */
/* initial GC parameters */
#define STEPMUL 4
#endif #endif
/* }====================================================== */ /* }====================================================== */
@ -305,8 +335,15 @@
#define LUA_POF "luaopen_" #define LUA_POF "luaopen_"
#endif #endif
/* separator for open functions in C libraries */
#define LUA_OFSEP ""
/* directory separator (for submodules) */ /* directory separator (for submodules) */
#if defined(_WIN32)
#define LUA_DIRSEP "\\"
#else
#define LUA_DIRSEP "/" #define LUA_DIRSEP "/"
#endif
/* separator of templates in a path */ /* separator of templates in a path */
#define LUA_PATH_SEP ';' #define LUA_PATH_SEP ';'
@ -332,12 +369,12 @@
/* /*
** Configuration for loadlib ** Configuration for loadlib
*/ */
#if defined(__linux) || defined(sun) || defined(sgi) || defined(BSD) #if defined(_WIN32)
#define USE_DLOPEN
#elif defined(_WIN32)
#define USE_DLL #define USE_DLL
#elif defined(__APPLE__) && defined(__MACH__) #elif defined(__APPLE__) && defined(__MACH__)
#define USE_DYLD #define USE_DYLD
#elif defined(__linux) || defined(sun) || defined(sgi) || defined(BSD)
#define USE_DLOPEN
#endif #endif