Compile Lua scripts used on OpenResty

1.5k views Asked by At

I'm currently using OpenResty + Lua for several projects, and I like the flexibility that Lua gives me, in fact I wrote some micro-web apps directly in Lua scripts, that are served by Nginx-OpenResty.

But, if I want to distribute the web app, obviously the Lua code should be either "plain" or, at least, slightly obfuscated. Instead, considering that LuaJIT that I'm currently using compiles Lua to native code, is it possible to pre-compile all Lua scripts as native code (not lua .o object files), and load them in OpenResty, instead of direct .lua source files?

1

There are 1 answers

2
Colonel Thirty Two On BEST ANSWER

Nope.

There's no way to compile LuaJIT code to machine code. It simply doesn't work that way, for two main reasons:

  1. LuaJIT selects traces to compile based on how often they are ran at runtime. This means that the generated traces can change depending on the data being processed (ex. different if branches can be compiled depending on which one is taken more often). Thus, it's impossible to precompile them ahead of time.
  2. Some operations cannot be compiled, because it hasn't been implemented yet (ex. closure creation), it will not ever be in areas worth optimizing (ex. require), or because it's simply not possible (ex. calls to Lua C API functions)

Your best bet is to compile the Lua files to LuaJIT bytecode with the debugging info stripped instead. This means stuff like local variable names, line numbers, etc. are omitted, but can still be disassembled.