lua-users home
lua-l archive

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


Peter Cawley wrote:
On Fri, Sep 4, 2009 at 12:22 AM, Andre de
Leiradella<aleirade@sct.microlink.com.br> wrote:
Thanks, but how do I handle packages that include Lua files?
Some some bin2c variant to convert the Lua source files to C byte
arrays, embed said arrays in C application, then call luaL_loadbuffer
for each one and store in in package.preload. It shouldn't be too hard
to write a script which converts a directory of Lua files into a C
file which contains the arrays and code to load them.
I'm like Andre in this regard, remembering fondly the good old days. But even I have started created mixed libraries with the C part just for interfacing to the C library and the Lua code providing the Lua-friendly abstraction layer.

bin2c does indeed work for simple files; I've used that technique many times. The tendency these days, though, is to do more and more in Lua and push only the essentials into C. That's a good thing in many ways. But it also makes it harder to retarget each library as an all-in-one, statically compiled, sans file system binary. Each library has to be combed over to find which Lua files in the hierarchy are truly part of the install base and then the proper load order has to be determined. I'm not convinced (yet) that this can be an automatic process.

Doing so with LuaSocket would be a good test. It was very easy for me to do so with luaproc (the diff is tiny enough to attach). I compiled the luaproc objects separately, ran bin2c on luaproc.lua, then made the attached mods to the standard lua-5.1.4 source tree. Only when I finished the whole exercise did I notice that luaproc.lua did nothing but load the shared object - which isn't needed and doesn't exist - and the preload wasn't getting called. Easy enough to fix for test purposes, but I'm outa time today.

Doug





______________________________________________________________________________________
The information contained in this email transmission may contain proprietary and business sensitive information. If you are not the intended recipient, you are hereby notified that any review, dissemination, distribution or duplication of this communication is strictly prohibited. Unauthorized interception of this e-mail is a violation of law. If you are not the intended recipient, please contact the sender by reply email and immediately destroy all copies of the original message.

Any technical data and/or information provided with or in this email may be subject to U.S. export controls law. Export, diversion or disclosure contrary to U.S. law is prohibited. Such technical data or information is not to be exported from the U.S. or given to any foreign person in the U.S. without prior written authorization of Elbit Systems of America and the appropriate U.S. Government agency.
diff -r -u lua-5.1.4-orig/src/linit.c lua-5.1.4/src/linit.c
--- lua-5.1.4-orig/src/linit.c	2007-12-27 08:02:25.000000000 -0500
+++ lua-5.1.4/src/linit.c	2009-09-03 21:02:04.000000000 -0400
@@ -8,11 +8,16 @@
 #define linit_c
 #define LUA_LIB
 
+#include <string.h>
+
 #include "lua.h"
 
 #include "lualib.h"
 #include "lauxlib.h"
 
+extern char file_luaproc_lua[];
+
+extern int luaopen_luaproc(lua_State* L);
 
 static const luaL_Reg lualibs[] = {
   {"", luaopen_base},
@@ -23,6 +28,7 @@
   {LUA_STRLIBNAME, luaopen_string},
   {LUA_MATHLIBNAME, luaopen_math},
   {LUA_DBLIBNAME, luaopen_debug},
+  {"luaproc", luaopen_luaproc},
   {NULL, NULL}
 };
 
@@ -34,5 +40,18 @@
     lua_pushstring(L, lib->name);
     lua_call(L, 1, 0);
   }
+
+  if (luaL_loadbuffer(L, file_luaproc_lua, strlen(file_luaproc_lua),
+                      "luaproc.lua") != 0)
+  {
+    luaL_error(L, "could not load luaproc.lua");
+    return;
+  }
+
+  lua_getglobal(L, "package");
+  lua_getfield(L, -1, "preload");
+  lua_pushvalue(L, -3);   /* Get the loaded buffer. */
+  lua_setfield(L, -2, "luaproc");
+  lua_pop(L, 3);          /* preload, package, and original luaproc.lua. */
 }
 
diff -r -u lua-5.1.4-orig/src/Makefile lua-5.1.4/src/Makefile
--- lua-5.1.4-orig/src/Makefile	2008-01-19 14:37:58.000000000 -0500
+++ lua-5.1.4/src/Makefile	2009-09-03 20:59:39.000000000 -0400
@@ -51,8 +51,12 @@
 	$(AR) $@ $?
 	$(RANLIB) $@
 
+LUAPROC_O = channel.o list.o sched.o luaproc.o file_luaproc_lua.o
+LUAPROC_DIR = ../..
+LUAPROC_OBJS = $(addprefix $(LUAPROC_DIR)/,$(LUAPROC_O))
+
 $(LUA_T): $(LUA_O) $(LUA_A)
-	$(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(LUA_A) $(LIBS)
+	$(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(LUA_A) $(LUAPROC_OBJS) $(LIBS) -lpthread
 
 $(LUAC_T): $(LUAC_O) $(LUA_A)
 	$(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS)