[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Storing Lua code in a C library
- From: steve donovan <steve.j.donovan@...>
- Date: Fri, 25 May 2012 12:17:59 +0200
On Wed, May 23, 2012 at 8:33 AM, steve donovan
<steve.j.donovan@gmail.com> wrote:
> And that's to embed the source directly in a C file, as a properly
> escaped multiline C string, which is then loaded.
And, due to popular demand, a simple script for converting a Lua
module into a loadable C module.
https://gist.github.com/2787105
foo.c is the result of 'lua2c foo', with foo.lua looking like this:
module 'foo'
function answer()
return 42
end
However, there is a problem when the module starts 'module(...)' since
luaL_loadstring does not compile the chunk correctly for this case.
I tested this on ml.lua, which is about 782 lines; lua2c strips
comments and leading space, so the final output is about 500 lines.
The compiled C extension is a bit smaller than the original Lua file;
another comparison is that it's a 18K dll versus 23K .luac file.
To test packages, I put ml.lua in a directory libs, and then said
'lua2c libs.ml', which resulted in libs_ml.c in the current directory,
and libs/ml.dll.
This should work fine for Lua 5.2 as well
And there's always yet another method; use squsih/soar to get all your
Lua files into one archive, and glue that to a Lua executable with
srlua. (that's basically the rationale behind LuaBuild)
steve d.