lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi list,

I'm working on a project where I'd like to have a lua interpreter with
standard libraries made optionnal and individually loadable.

I initially thought I would just have to compile the libs separately,
but I ended up facing two problems. The first is that the coroutine
library is defined in the same file as the base library, and while the
former can be made optionnal, the later cannot. So I had to split the
file lbaselib.c. The second issue is that luaL_openlibs in linit.c
directly references the init function for all standard libs. I ended
up redefining luaopen_* symbols with compiler switches to some dummy
function that I had to add above in the same file.

Below are two very short patches I used that ease separating standard
libraries like I did without interfering too much with the Lua
sources. Please consider integrating them in a future official Lua
version. As doing such a modification of the Lua interpreter requires
many modifications to the build process, and as I use a custom build
tool, I didn't created a patch to the makefile, but I could help in
creating one if someone is interested.

The first patch add #ifdef directives to lbaselib.c. To split base lib
and coroutine lib in two object files (and two shared libraries), you
have to compile the file twice, with LUA_SPLIT_COROLIB defined in both
compilations, but LUA_BUILD_COROLIB defined in only one (the one that
end up creating lcorolib.o).

The second patch just introduces an empty lua_CFunction. Maybe there
is already one that I could use instead, tell me if that's the case.

Jérôme Vuarand.

PS: I also attached the patch files to the mail as I don't know if
gmail will keep them usable in the mail body.

split-corolib.patch
diff -ur a/lua-5.1.2/src/lbaselib.c b/lua-5.1.2/src/lbaselib.c
--- a/lua-5.1.2/src/lbaselib.c	Fri Mar 23 13:06:08 2007
+++ b/lua-5.1.2/src/lbaselib.c	Fri May 18 01:11:31 2007
@@ -11,7 +11,9 @@
#include <stdlib.h>
#include <string.h>

+#if !defined(LUA_BUILD_COROLIB)
#define lbaselib_c
+#endif
#define LUA_LIB

#include "lua.h"
@@ -21,6 +23,7 @@



+#if !defined(LUA_BUILD_COROLIB)

/*
** If your system does not support `stdout', you can just remove this function.
@@ -470,6 +473,7 @@
  {NULL, NULL}
};

+#endif

/*
** {======================================================
@@ -477,6 +481,8 @@
** =======================================================
*/

+#if !defined(LUA_SPLIT_COROLIB) || defined(LUA_BUILD_COROLIB)
+
static int auxresume (lua_State *L, lua_State *co, int narg) {
  int status;
  if (!lua_checkstack(co, narg))
@@ -602,8 +608,11 @@
  {NULL, NULL}
};

+#endif
+
/* }====================================================== */

+#if !defined(LUA_BUILD_COROLIB)

static void auxopen (lua_State *L, const char *name,
                     lua_CFunction f, lua_CFunction u) {
@@ -637,7 +646,22 @@

LUALIB_API int luaopen_base (lua_State *L) {
  base_open(L);
+#if defined(LUA_SPLIT_COROLIB)
+  return 1;
+#else
  luaL_register(L, LUA_COLIBNAME, co_funcs);
  return 2;
+#endif
+}
+
+#endif
+
+#if defined(LUA_SPLIT_COROLIB) && defined(LUA_BUILD_COROLIB)
+
+LUALIB_API int luaopen_coroutine (lua_State *L) {
+  luaL_register(L, LUA_COLIBNAME, co_funcs);
+  return 1;
}
+
+#endif


optionnal-libs.patch
diff -ur a/lua-5.1.2/src/linit.c b/lua-5.1.2/src/linit.c
--- a/lua-5.1.2/src/linit.c	Thu Dec 29 10:32:12 2005
+++ b/lua-5.1.2/src/linit.c	Fri May 18 00:45:38 2007
@@ -14,6 +14,12 @@
#include "lauxlib.h"


+/* You can define luaopen_* functions to luaopen_dummy to prevent some default
+** libraries from being loaded. */
+static int luaopen_dummy (lua_State *L) {
+  return 0;
+}
+
static const luaL_Reg lualibs[] = {
  {"", luaopen_base},
  {LUA_LOADLIBNAME, luaopen_package},

Attachment: optionnal-libs.patch
Description: Binary data

Attachment: split-corolib.patch
Description: Binary data