2

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?

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
Carmine Giangregorio
  • 943
  • 2
  • 14
  • 35
  • you already accepted an answer, and I'm not sure if maybe this helps you, but the docs say you can precompile to lua bytecode and ship that. http://wiki.nginx.org/HttpLuaModule#Lua.2FLuaJIT_bytecode_support – user2240431 Dec 08 '14 at 15:19

1 Answers1

3

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.

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
  • Thank you for the detailed response. Regarding your suggestion regarding compiling LUA files to byte code, can I compile *every* LUA file that OpenResty uses, or only LUA shared modules? – Carmine Giangregorio Nov 30 '14 at 18:13
  • 1
    I'm not sure what you mean by shared modules, but LuaJIT is capable of loading source code and bytecode files interchangeably. (Also note that _Lua_ is not an acronym and should not be all caps-ed) – Colonel Thirty Two Nov 30 '14 at 18:50