42

I'm trying to compile a lua script that calls some exported functions, save the resulting bytecode to a file and then load this bytecode and execute it, but I haven't found any example on how to do this. Is there any example available on how to do this? How can I do this?

Edit: I'm using Lua + Luabind (C++)

1 Answer 1

71

This is all very simple.

First, you load the Lua script without executing it. It does not matter if you have connected the Lua state with your exported functions; all you're doing is compiling the script file.

You could use luaL_loadfile, which uses C-standard library functions to read a file from disk and load it into the lua_State. Alternatively, you can load the file yourself into a string and use luaL_loadstring to load it into the lua_State.

Both of these functions will emit return values and compiler errors as per the documentation for lua_load.

If the compilation was successful, the lua_State now has the compiled Lua chunk as a Lua function at the top of the stack. To get the compiled binary, you must use the lua_dump function. It's rather complicated as it uses a callback interface to pass you data. See the documentation for details.

After that process, you have the compiled Lua byte code. Shove that into a file of your choice. Just remember: write it as binary, not with text translation.

When it comes time to load the byte code, all you need to do is... exactly what you did before. Well, almost. Lua has heuristics to detect that a "string" it is given is a Lua source string or byte code. So yes, you can load byte code with luaL_loadfile just like before.

The difference is that you can't use luaL_loadstring with byte code. That function expects a NULL-terminated string, which is bad. Byte code can have embedded NULL characters in it, which would screw everything up. So if you want to do the file IO yourself (because you're using a special filesystem or something), you have to use lua_load directly (or luaL_loadbuffer). Which also uses a callback interface like lua_dump. So read up on how to use it.

Sign up to request clarification or add additional context in comments.

6 Comments

Wish there were more such sound answers!
Great answer, that's exactly what i thought it would be, but I just didn't know what to use. I'll try it out. Now tell me, how hard is to put something like this (clear and objective) in a FAQ or even in the documentation? Thank you!
@WoLfulus: If you Googled "Lua load bytecode", the very first link would have told you everything you needed to know. So I don't see the need for a FAQ on this. And the application luac already can pre-compile Lua scripts into bytecode, so you don't even need a program to do it. So really, there's no need for a FAQ; just proper use of Google.
@NicolBolas Actually, this SO question is now the top google result for "Lua load bytecode", so I think the FAQ request is valid.
Actually luaL_loadstring won't (always) work correctly when loading the stored bytecode because it uses strlen(s) to determine the length of the input buffer to load (github.com/LuaDist/lua/blob/…), and as the byte code can contain null characters it will give a wrong length. What you need to do instead is to call luaL_loadbuffer and pass it the byte code array with its size. Apart from that the answer is correct.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.