2003-03-17 16:04:58 +03:00
|
|
|
/*
|
2016-12-04 23:17:24 +03:00
|
|
|
** $Id: loadlib.c,v 1.128 2016/07/18 17:55:59 roberto Exp roberto $
|
2003-04-02 17:09:14 +04:00
|
|
|
** Dynamic library loader for Lua
|
2003-03-17 16:04:58 +03:00
|
|
|
** See Copyright Notice in lua.h
|
2005-03-18 19:38:43 +03:00
|
|
|
**
|
|
|
|
** This module contains an implementation of loadlib for Unix systems
|
2010-06-18 21:23:02 +04:00
|
|
|
** that have dlfcn, an implementation for Windows, and a stub for other
|
|
|
|
** systems.
|
2003-03-17 16:04:58 +03:00
|
|
|
*/
|
|
|
|
|
2014-11-02 22:19:04 +03:00
|
|
|
#define loadlib_c
|
|
|
|
#define LUA_LIB
|
2004-11-18 22:53:49 +03:00
|
|
|
|
2014-11-02 22:19:04 +03:00
|
|
|
#include "lprefix.h"
|
2011-11-09 19:18:04 +04:00
|
|
|
|
|
|
|
|
2015-02-16 16:14:33 +03:00
|
|
|
#include <stdio.h>
|
2004-11-19 18:52:12 +03:00
|
|
|
#include <stdlib.h>
|
2004-11-18 22:53:49 +03:00
|
|
|
#include <string.h>
|
|
|
|
|
2005-12-19 23:56:39 +03:00
|
|
|
#include "lua.h"
|
2007-03-26 19:57:35 +04:00
|
|
|
|
|
|
|
#include "lauxlib.h"
|
2003-03-17 16:04:58 +03:00
|
|
|
#include "lualib.h"
|
|
|
|
|
|
|
|
|
2009-12-17 15:26:09 +03:00
|
|
|
/*
|
|
|
|
** LUA_IGMARK is a mark to ignore all before it when building the
|
|
|
|
** luaopen_ function name.
|
|
|
|
*/
|
|
|
|
#if !defined (LUA_IGMARK)
|
|
|
|
#define LUA_IGMARK "-"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-11-09 19:18:04 +04:00
|
|
|
/*
|
|
|
|
** LUA_CSUBSEP is the character that replaces dots in submodule names
|
|
|
|
** when searching for a C loader.
|
|
|
|
** LUA_LSUBSEP is the character that replaces dots in submodule names
|
|
|
|
** when searching for a Lua loader.
|
|
|
|
*/
|
|
|
|
#if !defined(LUA_CSUBSEP)
|
|
|
|
#define LUA_CSUBSEP LUA_DIRSEP
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(LUA_LSUBSEP)
|
|
|
|
#define LUA_LSUBSEP LUA_DIRSEP
|
|
|
|
#endif
|
|
|
|
|
2009-12-17 15:26:09 +03:00
|
|
|
|
2005-03-18 19:38:43 +03:00
|
|
|
/* prefix for open functions in C libraries */
|
|
|
|
#define LUA_POF "luaopen_"
|
|
|
|
|
|
|
|
/* separator for open functions in C libraries */
|
|
|
|
#define LUA_OFSEP "_"
|
|
|
|
|
|
|
|
|
2014-11-10 17:28:31 +03:00
|
|
|
/*
|
|
|
|
** unique key for table in the registry that keeps handles
|
|
|
|
** for all loaded C libraries
|
|
|
|
*/
|
|
|
|
static const int CLIBS = 0;
|
2004-11-11 18:42:57 +03:00
|
|
|
|
2004-12-27 18:58:15 +03:00
|
|
|
#define LIB_FAIL "open"
|
|
|
|
|
2005-07-11 20:41:57 +04:00
|
|
|
|
2010-01-04 19:36:39 +03:00
|
|
|
/*
|
|
|
|
** system-dependent functions
|
|
|
|
*/
|
2014-07-28 21:47:53 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** unload library 'lib'
|
|
|
|
*/
|
|
|
|
static void lsys_unloadlib (void *lib);
|
|
|
|
|
|
|
|
/*
|
|
|
|
** load C library in file 'path'. If 'seeglb', load with all names in
|
|
|
|
** the library global.
|
|
|
|
** Returns the library; in case of error, returns NULL plus an
|
|
|
|
** error string in the stack.
|
|
|
|
*/
|
|
|
|
static void *lsys_load (lua_State *L, const char *path, int seeglb);
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Try to find a function named 'sym' in library 'lib'.
|
|
|
|
** Returns the function; in case of error, returns NULL plus an
|
|
|
|
** error string in the stack.
|
|
|
|
*/
|
|
|
|
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym);
|
|
|
|
|
2004-11-11 18:42:57 +03:00
|
|
|
|
2003-03-17 16:04:58 +03:00
|
|
|
|
|
|
|
|
2014-10-27 21:05:37 +03:00
|
|
|
#if defined(LUA_USE_DLOPEN) /* { */
|
2003-03-17 16:04:58 +03:00
|
|
|
/*
|
2004-12-27 18:58:15 +03:00
|
|
|
** {========================================================================
|
|
|
|
** This is an implementation of loadlib based on the dlfcn interface.
|
|
|
|
** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
|
|
|
|
** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least
|
|
|
|
** as an emulation layer on top of native functions.
|
|
|
|
** =========================================================================
|
2003-03-17 16:04:58 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
2015-01-05 16:51:39 +03:00
|
|
|
/*
|
2015-01-13 16:18:04 +03:00
|
|
|
** Macro to convert pointer-to-void* to pointer-to-function. This cast
|
|
|
|
** is undefined according to ISO C, but POSIX assumes that it works.
|
2015-01-05 16:51:39 +03:00
|
|
|
** (The '__extension__' in gnu compilers is only to avoid warnings.)
|
|
|
|
*/
|
|
|
|
#if defined(__GNUC__)
|
|
|
|
#define cast_func(p) (__extension__ (lua_CFunction)(p))
|
|
|
|
#else
|
|
|
|
#define cast_func(p) ((lua_CFunction)(p))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-07-28 21:47:53 +04:00
|
|
|
static void lsys_unloadlib (void *lib) {
|
2004-12-27 18:58:15 +03:00
|
|
|
dlclose(lib);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-28 21:47:53 +04:00
|
|
|
static void *lsys_load (lua_State *L, const char *path, int seeglb) {
|
2011-11-28 21:27:51 +04:00
|
|
|
void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL));
|
2004-12-27 18:58:15 +03:00
|
|
|
if (lib == NULL) lua_pushstring(L, dlerror());
|
|
|
|
return lib;
|
2003-03-17 16:04:58 +03:00
|
|
|
}
|
|
|
|
|
2003-04-08 00:11:53 +04:00
|
|
|
|
2014-07-28 21:47:53 +04:00
|
|
|
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
2015-01-05 16:51:39 +03:00
|
|
|
lua_CFunction f = cast_func(dlsym(lib, sym));
|
2004-12-27 18:58:15 +03:00
|
|
|
if (f == NULL) lua_pushstring(L, dlerror());
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
2003-04-08 00:11:53 +04:00
|
|
|
|
2014-10-27 21:05:37 +03:00
|
|
|
#elif defined(LUA_DL_DLL) /* }{ */
|
2003-03-17 16:04:58 +03:00
|
|
|
/*
|
2004-12-27 18:58:15 +03:00
|
|
|
** {======================================================================
|
|
|
|
** This is an implementation of loadlib for Windows using native functions.
|
|
|
|
** =======================================================================
|
2003-03-17 16:04:58 +03:00
|
|
|
*/
|
|
|
|
|
2014-11-03 18:11:10 +03:00
|
|
|
#include <windows.h>
|
|
|
|
|
2005-07-11 20:41:57 +04:00
|
|
|
|
2010-01-13 19:30:27 +03:00
|
|
|
/*
|
|
|
|
** optional flags for LoadLibraryEx
|
|
|
|
*/
|
|
|
|
#if !defined(LUA_LLE_FLAGS)
|
|
|
|
#define LUA_LLE_FLAGS 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2004-12-27 18:58:15 +03:00
|
|
|
static void pusherror (lua_State *L) {
|
2004-12-22 20:43:27 +03:00
|
|
|
int error = GetLastError();
|
|
|
|
char buffer[128];
|
2010-06-13 23:36:17 +04:00
|
|
|
if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
|
2011-02-07 22:15:24 +03:00
|
|
|
NULL, error, 0, buffer, sizeof(buffer)/sizeof(char), NULL))
|
2005-03-29 18:30:16 +04:00
|
|
|
lua_pushstring(L, buffer);
|
2004-12-22 20:43:27 +03:00
|
|
|
else
|
|
|
|
lua_pushfstring(L, "system error %d\n", error);
|
2003-03-17 16:04:58 +03:00
|
|
|
}
|
|
|
|
|
2014-07-28 21:47:53 +04:00
|
|
|
static void lsys_unloadlib (void *lib) {
|
2010-01-13 19:30:27 +03:00
|
|
|
FreeLibrary((HMODULE)lib);
|
2004-12-27 18:58:15 +03:00
|
|
|
}
|
2004-12-22 20:43:27 +03:00
|
|
|
|
2004-12-27 18:58:15 +03:00
|
|
|
|
2014-07-28 21:47:53 +04:00
|
|
|
static void *lsys_load (lua_State *L, const char *path, int seeglb) {
|
2010-06-13 23:36:17 +04:00
|
|
|
HMODULE lib = LoadLibraryExA(path, NULL, LUA_LLE_FLAGS);
|
2011-11-30 16:58:57 +04:00
|
|
|
(void)(seeglb); /* not used: symbols are 'global' by default */
|
2004-12-27 18:58:15 +03:00
|
|
|
if (lib == NULL) pusherror(L);
|
|
|
|
return lib;
|
2003-03-17 16:04:58 +03:00
|
|
|
}
|
|
|
|
|
2004-10-18 22:07:31 +04:00
|
|
|
|
2014-07-28 21:47:53 +04:00
|
|
|
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
2010-01-13 19:30:27 +03:00
|
|
|
lua_CFunction f = (lua_CFunction)GetProcAddress((HMODULE)lib, sym);
|
2004-12-27 18:58:15 +03:00
|
|
|
if (f == NULL) pusherror(L);
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
2014-10-27 21:05:37 +03:00
|
|
|
#else /* }{ */
|
2004-12-27 18:58:15 +03:00
|
|
|
/*
|
|
|
|
** {======================================================
|
|
|
|
** Fallback for other systems
|
|
|
|
** =======================================================
|
|
|
|
*/
|
2003-04-08 00:11:53 +04:00
|
|
|
|
2004-12-27 18:58:15 +03:00
|
|
|
#undef LIB_FAIL
|
|
|
|
#define LIB_FAIL "absent"
|
2003-03-17 16:04:58 +03:00
|
|
|
|
2003-05-15 01:01:53 +04:00
|
|
|
|
2005-12-07 18:42:32 +03:00
|
|
|
#define DLMSG "dynamic libraries not enabled; check your Lua installation"
|
|
|
|
|
2005-03-29 18:30:16 +04:00
|
|
|
|
2014-07-28 21:47:53 +04:00
|
|
|
static void lsys_unloadlib (void *lib) {
|
2011-11-30 16:58:57 +04:00
|
|
|
(void)(lib); /* not used */
|
2004-12-27 18:58:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-28 21:47:53 +04:00
|
|
|
static void *lsys_load (lua_State *L, const char *path, int seeglb) {
|
2011-11-30 16:58:57 +04:00
|
|
|
(void)(path); (void)(seeglb); /* not used */
|
2005-03-29 18:30:16 +04:00
|
|
|
lua_pushliteral(L, DLMSG);
|
2004-12-27 18:58:15 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-28 21:47:53 +04:00
|
|
|
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
2011-11-30 16:58:57 +04:00
|
|
|
(void)(lib); (void)(sym); /* not used */
|
2005-03-29 18:30:16 +04:00
|
|
|
lua_pushliteral(L, DLMSG);
|
2004-12-27 18:58:15 +03:00
|
|
|
return NULL;
|
2003-03-17 16:04:58 +03:00
|
|
|
}
|
2004-10-18 22:07:31 +04:00
|
|
|
|
2004-12-27 18:58:15 +03:00
|
|
|
/* }====================================================== */
|
2014-10-27 21:05:37 +03:00
|
|
|
#endif /* } */
|
2003-03-17 16:04:58 +03:00
|
|
|
|
2004-10-18 22:07:31 +04:00
|
|
|
|
2014-07-28 21:47:53 +04:00
|
|
|
/*
|
|
|
|
** return registry.CLIBS[path]
|
|
|
|
*/
|
|
|
|
static void *checkclib (lua_State *L, const char *path) {
|
2012-04-11 20:35:32 +04:00
|
|
|
void *plib;
|
2014-11-10 17:28:31 +03:00
|
|
|
lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS);
|
2012-04-11 20:35:32 +04:00
|
|
|
lua_getfield(L, -1, path);
|
|
|
|
plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */
|
|
|
|
lua_pop(L, 2); /* pop CLIBS table and 'plib' */
|
2004-12-27 18:58:15 +03:00
|
|
|
return plib;
|
2004-10-18 22:07:31 +04:00
|
|
|
}
|
|
|
|
|
2004-12-27 18:58:15 +03:00
|
|
|
|
2014-07-28 21:47:53 +04:00
|
|
|
/*
|
|
|
|
** registry.CLIBS[path] = plib -- for queries
|
|
|
|
** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries
|
|
|
|
*/
|
|
|
|
static void addtoclib (lua_State *L, const char *path, void *plib) {
|
2014-11-10 17:28:31 +03:00
|
|
|
lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS);
|
2012-04-11 20:35:32 +04:00
|
|
|
lua_pushlightuserdata(L, plib);
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_setfield(L, -3, path); /* CLIBS[path] = plib */
|
|
|
|
lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */
|
|
|
|
lua_pop(L, 1); /* pop CLIBS table */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-18 22:07:31 +04:00
|
|
|
/*
|
2014-07-28 21:47:53 +04:00
|
|
|
** __gc tag method for CLIBS table: calls 'lsys_unloadlib' for all lib
|
2012-04-11 20:35:32 +04:00
|
|
|
** handles in list CLIBS
|
2004-10-18 22:07:31 +04:00
|
|
|
*/
|
2004-12-22 20:43:27 +03:00
|
|
|
static int gctm (lua_State *L) {
|
2014-07-29 20:01:00 +04:00
|
|
|
lua_Integer n = luaL_len(L, 1);
|
2012-04-11 20:35:32 +04:00
|
|
|
for (; n >= 1; n--) { /* for each handle, in reverse order */
|
|
|
|
lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */
|
2014-07-28 21:47:53 +04:00
|
|
|
lsys_unloadlib(lua_touserdata(L, -1));
|
2012-04-11 20:35:32 +04:00
|
|
|
lua_pop(L, 1); /* pop handle */
|
|
|
|
}
|
2004-12-22 20:43:27 +03:00
|
|
|
return 0;
|
2004-10-18 22:07:31 +04:00
|
|
|
}
|
|
|
|
|
2004-11-11 18:42:57 +03:00
|
|
|
|
2014-07-28 21:47:53 +04:00
|
|
|
|
|
|
|
/* error codes for 'lookforfunc' */
|
|
|
|
#define ERRLIB 1
|
|
|
|
#define ERRFUNC 2
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Look for a C function named 'sym' in a dynamically loaded library
|
|
|
|
** 'path'.
|
|
|
|
** First, check whether the library is already loaded; if not, try
|
|
|
|
** to load it.
|
|
|
|
** Then, if 'sym' is '*', return true (as library has been loaded).
|
|
|
|
** Otherwise, look for symbol 'sym' in the library and push a
|
|
|
|
** C function with that symbol.
|
|
|
|
** Return 0 and 'true' or a function in the stack; in case of
|
|
|
|
** errors, return an error code and an error message in the stack.
|
|
|
|
*/
|
|
|
|
static int lookforfunc (lua_State *L, const char *path, const char *sym) {
|
|
|
|
void *reg = checkclib(L, path); /* check loaded C libraries */
|
2012-04-11 20:35:32 +04:00
|
|
|
if (reg == NULL) { /* must load library? */
|
2014-07-28 21:47:53 +04:00
|
|
|
reg = lsys_load(L, path, *sym == '*'); /* global symbols if 'sym'=='*' */
|
2012-04-11 20:35:32 +04:00
|
|
|
if (reg == NULL) return ERRLIB; /* unable to load library */
|
2014-07-28 21:47:53 +04:00
|
|
|
addtoclib(L, path, reg);
|
2012-04-11 20:35:32 +04:00
|
|
|
}
|
2010-01-04 19:36:39 +03:00
|
|
|
if (*sym == '*') { /* loading only library (no function)? */
|
2009-03-27 01:25:05 +03:00
|
|
|
lua_pushboolean(L, 1); /* return 'true' */
|
2010-01-04 19:36:39 +03:00
|
|
|
return 0; /* no errors */
|
2009-03-27 01:25:05 +03:00
|
|
|
}
|
2004-12-27 18:58:15 +03:00
|
|
|
else {
|
2014-07-28 21:47:53 +04:00
|
|
|
lua_CFunction f = lsys_sym(L, reg, sym);
|
2005-08-01 08:21:17 +04:00
|
|
|
if (f == NULL)
|
|
|
|
return ERRFUNC; /* unable to find function */
|
2010-03-18 00:37:37 +03:00
|
|
|
lua_pushcfunction(L, f); /* else create new function */
|
2010-01-04 19:36:39 +03:00
|
|
|
return 0; /* no errors */
|
2004-12-27 18:58:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-13 15:14:21 +03:00
|
|
|
static int ll_loadlib (lua_State *L) {
|
2004-12-22 20:43:27 +03:00
|
|
|
const char *path = luaL_checkstring(L, 1);
|
|
|
|
const char *init = luaL_checkstring(L, 2);
|
2014-07-28 21:47:53 +04:00
|
|
|
int stat = lookforfunc(L, path, init);
|
2005-08-01 08:21:17 +04:00
|
|
|
if (stat == 0) /* no errors? */
|
|
|
|
return 1; /* return the loaded function */
|
|
|
|
else { /* error; error message is on stack top */
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_insert(L, -2);
|
|
|
|
lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
|
|
|
|
return 3; /* return nil, error message, and where */
|
|
|
|
}
|
2004-11-11 18:42:57 +03:00
|
|
|
}
|
|
|
|
|
2004-11-18 22:53:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** {======================================================
|
2005-08-01 08:21:17 +04:00
|
|
|
** 'require' function
|
2004-11-18 22:53:49 +03:00
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2005-08-01 08:21:17 +04:00
|
|
|
static int readable (const char *filename) {
|
|
|
|
FILE *f = fopen(filename, "r"); /* try to open file */
|
2005-07-13 23:02:42 +04:00
|
|
|
if (f == NULL) return 0; /* open failed */
|
|
|
|
fclose(f);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const char *pushnexttemplate (lua_State *L, const char *path) {
|
|
|
|
const char *l;
|
2009-12-17 16:06:47 +03:00
|
|
|
while (*path == *LUA_PATH_SEP) path++; /* skip separators */
|
2005-07-13 23:02:42 +04:00
|
|
|
if (*path == '\0') return NULL; /* no more templates */
|
2009-12-17 16:06:47 +03:00
|
|
|
l = strchr(path, *LUA_PATH_SEP); /* find next separator */
|
2005-07-13 23:02:42 +04:00
|
|
|
if (l == NULL) l = path + strlen(path);
|
|
|
|
lua_pushlstring(L, path, l - path); /* template */
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-12 17:36:12 +03:00
|
|
|
static const char *searchpath (lua_State *L, const char *name,
|
2011-01-07 21:54:49 +03:00
|
|
|
const char *path,
|
2011-11-09 19:18:04 +04:00
|
|
|
const char *sep,
|
|
|
|
const char *dirsep) {
|
2011-11-10 15:42:58 +04:00
|
|
|
luaL_Buffer msg; /* to build error message */
|
|
|
|
luaL_buffinit(L, &msg);
|
2011-01-07 21:54:49 +03:00
|
|
|
if (*sep != '\0') /* non-empty separator? */
|
2011-11-09 19:18:04 +04:00
|
|
|
name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */
|
2005-07-13 23:02:42 +04:00
|
|
|
while ((path = pushnexttemplate(L, path)) != NULL) {
|
2007-12-12 17:36:12 +03:00
|
|
|
const char *filename = luaL_gsub(L, lua_tostring(L, -1),
|
|
|
|
LUA_PATH_MARK, name);
|
2006-07-04 00:16:49 +04:00
|
|
|
lua_remove(L, -2); /* remove path template */
|
2005-08-01 08:21:17 +04:00
|
|
|
if (readable(filename)) /* does file exist and is readable? */
|
|
|
|
return filename; /* return that file name */
|
2014-10-17 20:28:21 +04:00
|
|
|
lua_pushfstring(L, "\n\tno file '%s'", filename);
|
2006-07-04 00:16:49 +04:00
|
|
|
lua_remove(L, -2); /* remove file name */
|
2011-11-10 15:42:58 +04:00
|
|
|
luaL_addvalue(&msg); /* concatenate error msg. entry */
|
2005-07-13 23:02:42 +04:00
|
|
|
}
|
2011-11-10 15:42:58 +04:00
|
|
|
luaL_pushresult(&msg); /* create error message */
|
2005-07-13 23:02:42 +04:00
|
|
|
return NULL; /* not found */
|
2005-07-13 01:17:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-12 17:36:12 +03:00
|
|
|
static int ll_searchpath (lua_State *L) {
|
2011-01-07 21:54:49 +03:00
|
|
|
const char *f = searchpath(L, luaL_checkstring(L, 1),
|
|
|
|
luaL_checkstring(L, 2),
|
2011-11-09 19:18:04 +04:00
|
|
|
luaL_optstring(L, 3, "."),
|
|
|
|
luaL_optstring(L, 4, LUA_DIRSEP));
|
2007-12-12 17:36:12 +03:00
|
|
|
if (f != NULL) return 1;
|
|
|
|
else { /* error message is on top of the stack */
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_insert(L, -2);
|
|
|
|
return 2; /* return nil + error message */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const char *findfile (lua_State *L, const char *name,
|
2011-11-09 19:18:04 +04:00
|
|
|
const char *pname,
|
|
|
|
const char *dirsep) {
|
2007-12-12 17:36:12 +03:00
|
|
|
const char *path;
|
2010-03-18 00:37:37 +03:00
|
|
|
lua_getfield(L, lua_upvalueindex(1), pname);
|
2007-12-12 17:36:12 +03:00
|
|
|
path = lua_tostring(L, -1);
|
|
|
|
if (path == NULL)
|
2014-10-17 20:28:21 +04:00
|
|
|
luaL_error(L, "'package.%s' must be a string", pname);
|
2011-11-09 19:18:04 +04:00
|
|
|
return searchpath(L, name, path, ".", dirsep);
|
2007-12-12 17:36:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-01 20:01:53 +03:00
|
|
|
static int checkload (lua_State *L, int stat, const char *filename) {
|
|
|
|
if (stat) { /* module loaded successfully? */
|
|
|
|
lua_pushstring(L, filename); /* will be 2nd argument to module */
|
|
|
|
return 2; /* return open function and file name */
|
|
|
|
}
|
|
|
|
else
|
2014-10-17 20:28:21 +04:00
|
|
|
return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s",
|
2011-03-01 20:01:53 +03:00
|
|
|
lua_tostring(L, 1), filename, lua_tostring(L, -1));
|
2005-07-13 01:17:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-28 21:14:12 +04:00
|
|
|
static int searcher_Lua (lua_State *L) {
|
2005-08-01 08:21:17 +04:00
|
|
|
const char *filename;
|
|
|
|
const char *name = luaL_checkstring(L, 1);
|
2011-11-09 19:18:04 +04:00
|
|
|
filename = findfile(L, name, "path", LUA_LSUBSEP);
|
2011-03-01 20:01:53 +03:00
|
|
|
if (filename == NULL) return 1; /* module not found in this path */
|
|
|
|
return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename);
|
2004-11-18 22:53:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-28 21:47:53 +04:00
|
|
|
/*
|
|
|
|
** Try to find a load function for module 'modname' at file 'filename'.
|
|
|
|
** First, change '.' to '_' in 'modname'; then, if 'modname' has
|
|
|
|
** the form X-Y (that is, it has an "ignore mark"), build a function
|
|
|
|
** name "luaopen_X" and look for it. (For compatibility, if that
|
|
|
|
** fails, it also tries "luaopen_Y".) If there is no ignore mark,
|
|
|
|
** look for a function named "luaopen_modname".
|
|
|
|
*/
|
2010-01-11 19:10:47 +03:00
|
|
|
static int loadfunc (lua_State *L, const char *filename, const char *modname) {
|
2014-07-28 21:47:53 +04:00
|
|
|
const char *openfunc;
|
2010-01-11 19:10:47 +03:00
|
|
|
const char *mark;
|
|
|
|
modname = luaL_gsub(L, modname, ".", LUA_OFSEP);
|
|
|
|
mark = strchr(modname, *LUA_IGMARK);
|
|
|
|
if (mark) {
|
|
|
|
int stat;
|
2014-07-28 21:47:53 +04:00
|
|
|
openfunc = lua_pushlstring(L, modname, mark - modname);
|
|
|
|
openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc);
|
|
|
|
stat = lookforfunc(L, filename, openfunc);
|
2010-01-11 19:10:47 +03:00
|
|
|
if (stat != ERRFUNC) return stat;
|
|
|
|
modname = mark + 1; /* else go ahead and try old-style name */
|
|
|
|
}
|
2014-07-28 21:47:53 +04:00
|
|
|
openfunc = lua_pushfstring(L, LUA_POF"%s", modname);
|
|
|
|
return lookforfunc(L, filename, openfunc);
|
2005-08-01 08:21:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-28 21:14:12 +04:00
|
|
|
static int searcher_C (lua_State *L) {
|
2005-08-01 08:21:17 +04:00
|
|
|
const char *name = luaL_checkstring(L, 1);
|
2011-11-09 19:18:04 +04:00
|
|
|
const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP);
|
2011-03-01 20:01:53 +03:00
|
|
|
if (filename == NULL) return 1; /* module not found in this path */
|
|
|
|
return checkload(L, (loadfunc(L, filename, name) == 0), filename);
|
2005-01-14 17:17:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-28 21:14:12 +04:00
|
|
|
static int searcher_Croot (lua_State *L) {
|
2005-08-01 08:21:17 +04:00
|
|
|
const char *filename;
|
|
|
|
const char *name = luaL_checkstring(L, 1);
|
|
|
|
const char *p = strchr(name, '.');
|
|
|
|
int stat;
|
|
|
|
if (p == NULL) return 0; /* is root */
|
|
|
|
lua_pushlstring(L, name, p - name);
|
2011-11-09 19:18:04 +04:00
|
|
|
filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP);
|
2005-12-19 23:56:39 +03:00
|
|
|
if (filename == NULL) return 1; /* root not found */
|
2010-01-04 19:36:39 +03:00
|
|
|
if ((stat = loadfunc(L, filename, name)) != 0) {
|
2011-03-01 20:01:53 +03:00
|
|
|
if (stat != ERRFUNC)
|
|
|
|
return checkload(L, 0, filename); /* real error */
|
|
|
|
else { /* open function not found */
|
2014-10-17 20:28:21 +04:00
|
|
|
lua_pushfstring(L, "\n\tno module '%s' in file '%s'", name, filename);
|
2011-03-01 20:01:53 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2005-08-01 08:21:17 +04:00
|
|
|
}
|
2011-03-01 20:01:53 +03:00
|
|
|
lua_pushstring(L, filename); /* will be 2nd argument to module */
|
|
|
|
return 2;
|
2005-08-01 08:21:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-28 21:14:12 +04:00
|
|
|
static int searcher_preload (lua_State *L) {
|
2005-12-19 23:56:39 +03:00
|
|
|
const char *name = luaL_checkstring(L, 1);
|
2016-12-04 23:17:24 +03:00
|
|
|
lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
|
2014-03-13 00:57:40 +04:00
|
|
|
if (lua_getfield(L, -1, name) == LUA_TNIL) /* not found? */
|
2007-03-26 19:57:35 +04:00
|
|
|
lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
|
2005-01-14 17:17:18 +03:00
|
|
|
return 1;
|
2004-11-18 22:53:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-09 23:11:20 +04:00
|
|
|
static void findloader (lua_State *L, const char *name) {
|
2005-01-14 17:17:18 +03:00
|
|
|
int i;
|
2011-11-10 15:42:58 +04:00
|
|
|
luaL_Buffer msg; /* to build error message */
|
|
|
|
luaL_buffinit(L, &msg);
|
2014-11-12 16:31:51 +03:00
|
|
|
/* push 'package.searchers' to index 3 in the stack */
|
|
|
|
if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE)
|
2014-10-17 20:28:21 +04:00
|
|
|
luaL_error(L, "'package.searchers' must be a table");
|
2012-05-30 16:33:44 +04:00
|
|
|
/* iterate over available searchers to find a loader */
|
2011-11-09 23:11:20 +04:00
|
|
|
for (i = 1; ; i++) {
|
2014-03-13 00:57:40 +04:00
|
|
|
if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */
|
2011-11-09 23:11:20 +04:00
|
|
|
lua_pop(L, 1); /* remove nil */
|
2011-11-10 15:42:58 +04:00
|
|
|
luaL_pushresult(&msg); /* create error message */
|
2014-10-17 20:28:21 +04:00
|
|
|
luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1));
|
2011-11-09 23:11:20 +04:00
|
|
|
}
|
2005-01-14 17:17:18 +03:00
|
|
|
lua_pushstring(L, name);
|
2011-03-01 20:01:53 +03:00
|
|
|
lua_call(L, 1, 2); /* call it */
|
2011-06-28 21:14:12 +04:00
|
|
|
if (lua_isfunction(L, -2)) /* did it find a loader? */
|
2011-11-09 23:11:20 +04:00
|
|
|
return; /* module loader found */
|
2011-06-28 21:14:12 +04:00
|
|
|
else if (lua_isstring(L, -2)) { /* searcher returned error message? */
|
2011-03-01 20:01:53 +03:00
|
|
|
lua_pop(L, 1); /* remove extra return */
|
2011-11-10 15:42:58 +04:00
|
|
|
luaL_addvalue(&msg); /* concatenate error message */
|
2011-03-01 20:01:53 +03:00
|
|
|
}
|
2005-12-19 23:56:39 +03:00
|
|
|
else
|
2011-03-01 20:01:53 +03:00
|
|
|
lua_pop(L, 2); /* remove both returns */
|
2004-11-18 22:53:49 +03:00
|
|
|
}
|
2011-11-09 23:11:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int ll_require (lua_State *L) {
|
|
|
|
const char *name = luaL_checkstring(L, 1);
|
2016-12-04 23:17:24 +03:00
|
|
|
lua_settop(L, 1); /* LOADED table will be at index 2 */
|
|
|
|
lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
|
|
|
|
lua_getfield(L, 2, name); /* LOADED[name] */
|
2011-11-09 23:11:20 +04:00
|
|
|
if (lua_toboolean(L, -1)) /* is it there? */
|
|
|
|
return 1; /* package is already loaded */
|
|
|
|
/* else must load package */
|
|
|
|
lua_pop(L, 1); /* remove 'getfield' result */
|
|
|
|
findloader(L, name);
|
2011-06-28 21:14:12 +04:00
|
|
|
lua_pushstring(L, name); /* pass name as argument to module loader */
|
2011-03-01 20:01:53 +03:00
|
|
|
lua_insert(L, -2); /* name is 1st argument (before search data) */
|
2011-06-28 21:14:12 +04:00
|
|
|
lua_call(L, 2, 1); /* run loader to load module */
|
2004-11-18 22:53:49 +03:00
|
|
|
if (!lua_isnil(L, -1)) /* non-nil return? */
|
2016-12-04 23:17:24 +03:00
|
|
|
lua_setfield(L, 2, name); /* LOADED[name] = returned value */
|
2014-03-13 00:57:40 +04:00
|
|
|
if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */
|
2005-09-01 03:17:29 +04:00
|
|
|
lua_pushboolean(L, 1); /* use true as result */
|
|
|
|
lua_pushvalue(L, -1); /* extra copy to be returned */
|
2016-12-04 23:17:24 +03:00
|
|
|
lua_setfield(L, 2, name); /* LOADED[name] = true */
|
2005-09-01 03:17:29 +04:00
|
|
|
}
|
2005-07-05 23:29:03 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2005-08-01 08:21:17 +04:00
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** {======================================================
|
|
|
|
** 'module' function
|
|
|
|
** =======================================================
|
|
|
|
*/
|
2010-07-28 19:51:59 +04:00
|
|
|
#if defined(LUA_COMPAT_MODULE)
|
2005-07-05 23:29:03 +04:00
|
|
|
|
2010-03-19 18:02:34 +03:00
|
|
|
/*
|
2010-09-07 23:21:39 +04:00
|
|
|
** changes the environment variable of calling function
|
2010-03-19 18:02:34 +03:00
|
|
|
*/
|
|
|
|
static void set_env (lua_State *L) {
|
2004-11-18 22:53:49 +03:00
|
|
|
lua_Debug ar;
|
2008-08-05 23:25:42 +04:00
|
|
|
if (lua_getstack(L, 1, &ar) == 0 ||
|
|
|
|
lua_getinfo(L, "f", &ar) == 0 || /* get calling function */
|
|
|
|
lua_iscfunction(L, -1))
|
2014-10-17 20:28:21 +04:00
|
|
|
luaL_error(L, "'module' not called from a Lua function");
|
2008-08-05 23:25:42 +04:00
|
|
|
lua_pushvalue(L, -2); /* copy new environment table to top */
|
2010-03-19 18:02:34 +03:00
|
|
|
lua_setupvalue(L, -2, 1);
|
2008-08-05 23:25:42 +04:00
|
|
|
lua_pop(L, 1); /* remove function */
|
2005-09-01 03:17:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void dooptions (lua_State *L, int n) {
|
|
|
|
int i;
|
|
|
|
for (i = 2; i <= n; i++) {
|
2011-11-06 17:59:12 +04:00
|
|
|
if (lua_isfunction(L, i)) { /* avoid 'calling' extra info. */
|
|
|
|
lua_pushvalue(L, i); /* get option (a function) */
|
|
|
|
lua_pushvalue(L, -2); /* module */
|
|
|
|
lua_call(L, 1, 0);
|
|
|
|
}
|
2005-09-01 03:17:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void modinit (lua_State *L, const char *modname) {
|
|
|
|
const char *dot;
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_setfield(L, -2, "_M"); /* module._M = module */
|
|
|
|
lua_pushstring(L, modname);
|
|
|
|
lua_setfield(L, -2, "_NAME");
|
|
|
|
dot = strrchr(modname, '.'); /* look for last dot in module name */
|
|
|
|
if (dot == NULL) dot = modname;
|
|
|
|
else dot++;
|
|
|
|
/* set _PACKAGE as package name (full module name minus last part) */
|
|
|
|
lua_pushlstring(L, modname, dot - modname);
|
|
|
|
lua_setfield(L, -2, "_PACKAGE");
|
2004-11-18 22:53:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int ll_module (lua_State *L) {
|
|
|
|
const char *modname = luaL_checkstring(L, 1);
|
2010-05-31 20:34:19 +04:00
|
|
|
int lastarg = lua_gettop(L); /* last parameter */
|
|
|
|
luaL_pushmodule(L, modname, 1); /* get/create module table */
|
2004-11-18 22:53:49 +03:00
|
|
|
/* check whether table already has a _NAME field */
|
2014-03-13 00:57:40 +04:00
|
|
|
if (lua_getfield(L, -1, "_NAME") != LUA_TNIL)
|
|
|
|
lua_pop(L, 1); /* table is an initialized module */
|
2004-11-18 22:53:49 +03:00
|
|
|
else { /* no; initialize it */
|
|
|
|
lua_pop(L, 1);
|
2005-09-01 03:17:29 +04:00
|
|
|
modinit(L, modname);
|
2004-11-18 22:53:49 +03:00
|
|
|
}
|
2005-09-01 03:17:29 +04:00
|
|
|
lua_pushvalue(L, -1);
|
2010-03-19 18:02:34 +03:00
|
|
|
set_env(L);
|
2010-05-31 20:34:19 +04:00
|
|
|
dooptions(L, lastarg);
|
2009-11-16 18:51:19 +03:00
|
|
|
return 1;
|
2005-09-01 03:17:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int ll_seeall (lua_State *L) {
|
|
|
|
luaL_checktype(L, 1, LUA_TTABLE);
|
|
|
|
if (!lua_getmetatable(L, 1)) {
|
2005-12-29 19:23:32 +03:00
|
|
|
lua_createtable(L, 0, 1); /* create new metatable */
|
2005-09-01 03:17:29 +04:00
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_setmetatable(L, 1);
|
|
|
|
}
|
2009-12-22 18:32:50 +03:00
|
|
|
lua_pushglobaltable(L);
|
2005-09-01 03:17:29 +04:00
|
|
|
lua_setfield(L, -2, "__index"); /* mt.__index = _G */
|
2004-11-18 22:53:49 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-28 19:51:59 +04:00
|
|
|
#endif
|
2004-11-18 22:53:49 +03:00
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
2005-08-01 08:21:17 +04:00
|
|
|
|
2005-08-26 21:36:32 +04:00
|
|
|
static const luaL_Reg pk_funcs[] = {
|
2005-08-26 21:32:05 +04:00
|
|
|
{"loadlib", ll_loadlib},
|
2007-12-12 17:36:12 +03:00
|
|
|
{"searchpath", ll_searchpath},
|
2011-07-05 16:49:35 +04:00
|
|
|
#if defined(LUA_COMPAT_MODULE)
|
2005-09-01 03:17:29 +04:00
|
|
|
{"seeall", ll_seeall},
|
2011-07-05 16:49:35 +04:00
|
|
|
#endif
|
2014-07-16 17:56:14 +04:00
|
|
|
/* placeholders */
|
|
|
|
{"preload", NULL},
|
|
|
|
{"cpath", NULL},
|
|
|
|
{"path", NULL},
|
|
|
|
{"searchers", NULL},
|
|
|
|
{"loaded", NULL},
|
2005-08-26 21:32:05 +04:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-08-26 21:36:32 +04:00
|
|
|
static const luaL_Reg ll_funcs[] = {
|
2011-07-05 16:49:35 +04:00
|
|
|
#if defined(LUA_COMPAT_MODULE)
|
2004-11-18 22:53:49 +03:00
|
|
|
{"module", ll_module},
|
2011-07-05 16:49:35 +04:00
|
|
|
#endif
|
2005-07-13 01:17:46 +04:00
|
|
|
{"require", ll_require},
|
2004-11-18 22:53:49 +03:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-04-26 23:38:52 +04:00
|
|
|
static void createsearcherstable (lua_State *L) {
|
|
|
|
static const lua_CFunction searchers[] =
|
|
|
|
{searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL};
|
|
|
|
int i;
|
|
|
|
/* create 'searchers' table */
|
|
|
|
lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0);
|
2015-11-23 14:30:45 +03:00
|
|
|
/* fill it with predefined searchers */
|
2012-04-26 23:38:52 +04:00
|
|
|
for (i=0; searchers[i] != NULL; i++) {
|
|
|
|
lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */
|
|
|
|
lua_pushcclosure(L, searchers[i], 1);
|
|
|
|
lua_rawseti(L, -2, i+1);
|
|
|
|
}
|
2014-07-28 21:47:53 +04:00
|
|
|
#if defined(LUA_COMPAT_LOADERS)
|
|
|
|
lua_pushvalue(L, -1); /* make a copy of 'searchers' table */
|
2014-10-25 15:50:46 +04:00
|
|
|
lua_setfield(L, -3, "loaders"); /* put it in field 'loaders' */
|
2014-07-28 21:47:53 +04:00
|
|
|
#endif
|
|
|
|
lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */
|
2012-04-26 23:38:52 +04:00
|
|
|
}
|
2005-01-14 17:17:18 +03:00
|
|
|
|
2004-11-18 22:53:49 +03:00
|
|
|
|
2014-07-28 21:47:53 +04:00
|
|
|
/*
|
|
|
|
** create table CLIBS to keep track of loaded C libraries,
|
|
|
|
** setting a finalizer to close all libraries when closing state.
|
|
|
|
*/
|
|
|
|
static void createclibstable (lua_State *L) {
|
2014-11-10 17:28:31 +03:00
|
|
|
lua_newtable(L); /* create CLIBS table */
|
2014-07-28 21:47:53 +04:00
|
|
|
lua_createtable(L, 0, 1); /* create metatable for CLIBS */
|
2004-11-18 22:53:49 +03:00
|
|
|
lua_pushcfunction(L, gctm);
|
2012-04-11 20:35:32 +04:00
|
|
|
lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */
|
|
|
|
lua_setmetatable(L, -2);
|
2014-11-10 17:28:31 +03:00
|
|
|
lua_rawsetp(L, LUA_REGISTRYINDEX, &CLIBS); /* set CLIBS table in registry */
|
2014-07-28 21:47:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LUAMOD_API int luaopen_package (lua_State *L) {
|
|
|
|
createclibstable(L);
|
2014-10-25 15:50:46 +04:00
|
|
|
luaL_newlib(L, pk_funcs); /* create 'package' table */
|
2012-04-26 23:38:52 +04:00
|
|
|
createsearcherstable(L);
|
2016-07-18 20:55:59 +03:00
|
|
|
lua_pushstring(L, LUA_PATH_DEFAULT);
|
|
|
|
lua_setfield(L, -2, "path"); /* package.path = default path */
|
|
|
|
lua_pushstring(L, LUA_CPATH_DEFAULT);
|
|
|
|
lua_setfield(L, -2, "cpath"); /* package.cpath = default cpath */
|
2005-08-09 21:58:09 +04:00
|
|
|
/* store config information */
|
2009-12-17 16:06:47 +03:00
|
|
|
lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n"
|
|
|
|
LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
|
2005-08-09 21:58:09 +04:00
|
|
|
lua_setfield(L, -2, "config");
|
2014-10-25 15:50:46 +04:00
|
|
|
/* set field 'loaded' */
|
2016-12-04 23:17:24 +03:00
|
|
|
luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
|
2004-11-18 22:53:49 +03:00
|
|
|
lua_setfield(L, -2, "loaded");
|
2014-10-25 15:50:46 +04:00
|
|
|
/* set field 'preload' */
|
2016-12-04 23:17:24 +03:00
|
|
|
luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
|
2004-11-18 22:53:49 +03:00
|
|
|
lua_setfield(L, -2, "preload");
|
2009-12-22 18:32:50 +03:00
|
|
|
lua_pushglobaltable(L);
|
2010-03-18 00:37:37 +03:00
|
|
|
lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */
|
2010-07-28 19:51:59 +04:00
|
|
|
luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */
|
2010-03-18 00:37:37 +03:00
|
|
|
lua_pop(L, 1); /* pop global table */
|
2005-08-26 21:32:05 +04:00
|
|
|
return 1; /* return 'package' table */
|
2003-03-17 16:04:58 +03:00
|
|
|
}
|
|
|
|
|