Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile Lua scripts into a single executable, while still gaining the fast LuaJIT compiler?

How can I compile my Lua scripts into a single executable file, while also gaining the super fast performance benefits of LuaJIT?

Background:

  • My Lua scripts are for a web application I created (e.g. to host http://example.com)
  • My current technology stack is NGINX (web server), Lua/LuaJIT (language to retrieve dynamic content)
  • I have around 50+ .lua files that make up my web application (from Models/Views/Controllers)
  • FreeBSD 9 operating system

For simplicity sake in deployment, I'd like to compile down all of my .lua scripts that run my web application down to a single executable.

  1. Is this possible and how?

    It appears that Lua official comes with a library called SRLua

  2. What are the negatives to compiling down my .lua to a single executable (e.g. would performance be worse, etc)?
like image 499
nickb Avatar asked Jul 03 '12 18:07

nickb


People also ask

Does Lua have a compiler?

DESCRIPTION. luac is the Lua compiler. It translates programs written in the Lua programming language into binary files that can be loaded and executed with lua_dofile in C or with dofile in Lua.


2 Answers

Translate all of the Lua source code files to object files and put them in a static library:

for f in *.lua; do     luajit -b $f `basename $f .lua`.o done ar rcus libmyluafiles.a *.o 

Then link the libmyluafiles.a library into your main program using -Wl,--whole-archive -lmyluafiles -Wl,--no-whole-archive -Wl,-E.

This line forces the linker to include all object files from the archive and to export all symbols.

For example, a file named foo.lua can now be loaded with local foo = require("foo") from within your application.

Details about the -b option can be found on Running LuaJIT.

like image 147
Mike Pall Avatar answered Oct 02 '22 14:10

Mike Pall


For a web app that you are currently deploying as a nest of related .lua files, your easiest answer will be to condense them into a single file. This can often be done for simple cases with luac. However, for complex applications with a mix of modules you want something smarter.

I personally use Mathew Wild's utility squish to do something similar.

After running squish, you will have a single .lua file containing all the Lua source code bundled up conveniently. You could just deploy that single file.

If you need to also bundle any binary modules, or the Lua or LuaJIT interpreter, then you can easily use SRLua to bundle it with the Lua interpreter, or similar techniques to bundle it with LuaJIT.

like image 27
RBerteig Avatar answered Oct 02 '22 16:10

RBerteig