mirror of
https://github.com/lua/lua
synced 2025-04-26 15:33:02 +03:00
several improvements/corrections
This commit is contained in:
parent
d5ebc3ff6d
commit
96727c61b8
173
loadlib.c
173
loadlib.c
@ -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,26 +58,23 @@ 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);
|
||||||
{
|
if (f != NULL) {
|
||||||
lua_CFunction f=(lua_CFunction) dlsym(lib,init);
|
registerlib(L, lib);
|
||||||
if (f!=NULL)
|
lua_pushcfunction(L,f);
|
||||||
{
|
return 0;
|
||||||
registerlib(L, lib);
|
}
|
||||||
lua_pushcfunction(L,f);
|
}
|
||||||
return 0;
|
/* else return appropriate error message */
|
||||||
}
|
lua_pushstring(L,dlerror());
|
||||||
}
|
if (lib != NULL) {
|
||||||
/* else return appropriate error message */
|
dlclose(lib);
|
||||||
lua_pushstring(L,dlerror());
|
return ERR_FUNCTION; /* error loading function */
|
||||||
if (lib!=NULL) {
|
}
|
||||||
dlclose(lib);
|
else return ERR_OPEN; /* error loading library */
|
||||||
return ERR_FUNCTION; /* error loading function */
|
|
||||||
}
|
|
||||||
else return ERR_OPEN; /* error loading library */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -91,36 +88,34 @@ 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,
|
||||||
0, error, 0, buffer, sizeof(buffer), 0))
|
0, error, 0, buffer, sizeof(buffer), 0))
|
||||||
lua_pushstring(L,buffer);
|
lua_pushstring(L,buffer);
|
||||||
else
|
else
|
||||||
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);
|
||||||
{
|
lua_pushcfunction(L, f);
|
||||||
registerlib(L, lib);
|
return 0;
|
||||||
lua_pushcfunction(L,f);
|
}
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
}
|
pusherror(L);
|
||||||
pusherror(L);
|
if (lib != NULL) {
|
||||||
if (lib!=NULL) {
|
FreeLibrary(lib);
|
||||||
FreeLibrary(lib);
|
return ERR_OPEN;
|
||||||
return ERR_OPEN;
|
}
|
||||||
}
|
else return ERR_FUNCTION;
|
||||||
else return ERR_FUNCTION;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -134,21 +129,22 @@ 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;
|
||||||
int err_num;
|
int err_num;
|
||||||
NSLinkEditError(&err, &err_num, &err_file, &err_str);
|
NSLinkEditError(&err, &err_num, &err_file, &err_str);
|
||||||
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 */
|
||||||
if(!_dyld_present()) {
|
if(!_dyld_present()) {
|
||||||
lua_pushstring(L,"dyld not present.");
|
lua_pushstring(L, "dyld not present.");
|
||||||
return ERR_OPEN;
|
return ERR_OPEN;
|
||||||
}
|
}
|
||||||
lib = NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
|
lib = NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
|
||||||
@ -159,7 +155,7 @@ static int loadlib (lua_State *L, const char *path, const char *init) {
|
|||||||
if(init_sym != NULL) {
|
if(init_sym != NULL) {
|
||||||
lua_CFunction f = (lua_CFunction)NSAddressOfSymbol(init_sym);
|
lua_CFunction f = (lua_CFunction)NSAddressOfSymbol(init_sym);
|
||||||
registerlib(L, lib);
|
registerlib(L, lib);
|
||||||
lua_pushcfunction(L,f);
|
lua_pushcfunction(L, f);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -177,11 +173,10 @@ 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)®isterlib; /* to avoid warnings */
|
||||||
(void)path; (void)init; (void)®isterlib; /* to avoid warnings */
|
lua_pushliteral(L,"`loadlib' not supported");
|
||||||
lua_pushliteral(L,"`loadlib' not supported");
|
return ERR_ABSENT;
|
||||||
return ERR_ABSENT;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@ -192,41 +187,39 @@ 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");
|
lua_setmetatable(L, -2);
|
||||||
lua_setmetatable(L, -2);
|
lua_pushboolean(L, 1);
|
||||||
lua_pushboolean(L, 1);
|
lua_settable(L, LUA_REGISTRYINDEX); /* registry[lib] = true */
|
||||||
lua_settable(L, LUA_REGISTRYINDEX); /* registry[lib] = true */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** __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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int ll_loadlib (lua_State *L) {
|
static int ll_loadlib (lua_State *L) {
|
||||||
static const char *const errcodes[] = {NULL, "open", "init", "absent"};
|
static const char *const errcodes[] = {NULL, "open", "init", "absent"};
|
||||||
const char *path=luaL_checkstring(L,1);
|
const char *path = luaL_checkstring(L, 1);
|
||||||
const char *init=luaL_checkstring(L,2);
|
const char *init = luaL_checkstring(L, 2);
|
||||||
int res = loadlib(L,path,init);
|
int res = loadlib(L,path,init);
|
||||||
if (res == 0) /* no error? */
|
if (res == 0) /* no error? */
|
||||||
return 1; /* function is at stack top */
|
return 1; /* function is at stack top */
|
||||||
else { /* error */
|
else { /* error */
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
lua_insert(L,-2); /* insert nil before error message */
|
lua_insert(L, -2); /* insert nil before error message */
|
||||||
lua_pushstring(L, errcodes[res]);
|
lua_pushstring(L, errcodes[res]);
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -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 */
|
||||||
|
67
luaconf.h
67
luaconf.h
@ -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 */
|
||||||
#define LUA_PATH_DEFAULT \
|
#if defined(_WIN32)
|
||||||
"./?.lua;/usr/local/share/lua/5.0/?.lua;/usr/local/share/lua/5.0/?/init.lua"
|
#define LUA_ROOT "C:\\Program Files\\Lua51"
|
||||||
#define LUA_CPATH_DEFAULT \
|
#define LUA_LDIR LUA_ROOT "\\lua"
|
||||||
"./?.so;/usr/local/lib/lua/5.0/?.so;/usr/local/lib/lua/5.0/lib?.so"
|
#define LUA_CDIR LUA_ROOT "\\dll"
|
||||||
|
#define LUA_PATH_DEFAULT \
|
||||||
|
"?.lua;" LUA_LDIR "\\?.lua;" LUA_LDIR "\\?\\init.lua"
|
||||||
|
#define LUA_CPATH_DEFAULT "?.dll;" LUA_CDIR "\\?.dll"
|
||||||
|
|
||||||
|
#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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -57,10 +69,10 @@
|
|||||||
#define LUA_API extern
|
#define LUA_API extern
|
||||||
|
|
||||||
/* mark for auxlib functions */
|
/* mark for auxlib functions */
|
||||||
#define LUALIB_API extern
|
#define LUALIB_API extern
|
||||||
|
|
||||||
/* buffer size used by lauxlib buffer system */
|
/* buffer size used by lauxlib buffer system */
|
||||||
#define LUAL_BUFFERSIZE BUFSIZ
|
#define LUAL_BUFFERSIZE BUFSIZ
|
||||||
|
|
||||||
|
|
||||||
/* assertions in Lua (mainly for internal debugging) */
|
/* assertions in Lua (mainly for internal debugging) */
|
||||||
@ -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))
|
||||||
|
|
||||||
@ -258,8 +291,8 @@
|
|||||||
** or when reading immutable fields from global objects
|
** or when reading immutable fields from global objects
|
||||||
** (such as string values and udata values).
|
** (such as string values and udata values).
|
||||||
*/
|
*/
|
||||||
#define lua_lock(L) ((void) 0)
|
#define lua_lock(L) ((void) 0)
|
||||||
#define lua_unlock(L) ((void) 0)
|
#define lua_unlock(L) ((void) 0)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** this macro allows a thread switch in appropriate places in the Lua
|
** this macro allows a thread switch in appropriate places in the Lua
|
||||||
@ -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
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user