lua-users home
lua-l archive

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


It was thus said that the Great Rena once stated:
> I'm writing a Lua module in C, but I want to store some Lua code in it
> as well to perform some initialization, and use lua_load() to run it
> as needed. However, I'm wondering about the best way to store this
> code.
> If I store it compiled, it may not work, because the bytecode format
> differs from one system to the next, correct?

  Yes, the bytecode differs from system to system, but since you are
including the compiled bytecode in the program that executes said bytecode,
I don't think this is much of an issue.

  At work, I have a custom Lua executable that includes all the modules
we use built in.  In the C code for the custom Lua interpreter, I have:

	/* kslua.c */
	extern const char   c_testbed[];
	extern const size_t c_testbed_size;
	
	  /* ... */
	
	  luaL_loadbuffer(L,c_testbed,c_testbed_size,"testbed");
	  lua_call(L,0,0);

The build process does this:

	kslua : kslua.o built/testbed.lua.o
		$(CC) -o $@ $^ $(LFLAGS)

	kslua.c : 
		$(CC) -c -o $@ $<

	built/testbed.lua.o : lua/testbed.lua
	        $(LUAC) -o testbedlua.out $<
	        $(BIN2C) -o testbedlua.c -t testbed testbedlua.out
	        $(CC) $(CFLAGS) -c -o $@ testbedlua.c
	        /bin/rm testbedlua.c testbedlua.out

(well, there are a a ton more modules, both in C and Lua included)

  The bin2c program will generate a C file that can be compiled.  Since I
was more concerned with simplicity (this "kitchen sink" Lua interpreter is
used for testing, not for production) I did the simplist thing that would
work.

  -spc