lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Yes, you can use lua_dump() for exactly that. A sample implementation can be found in lstrlib.c:

        static int writer (lua_State *L, const void* b, size_t size, void* B) {
          luaL_addlstring((luaL_Buffer*) B, (const char *)b, size);
          return 0;

        static int str_dump (lua_State *L) {
          luaL_Buffer b;
          luaL_checktype(L, 1, LUA_TFUNCTION);
          lua_settop(L, 1);
          if (lua_dump(L, writer, &b) != 0)
            return luaL_error(L, "unable to dump given function");
          return 1;

However, you could also just compile an entire lua file into bytecode and load it again later with do_file()

On Tue, Oct 7, 2014 at 12:56 PM, Philipp Kraus <> wrote:

thanks a lot of this information. In my case I would like to copy the current script only, all internal states / variables can be resetted. I have got a Lua script, which is run by the interpreter and I don’t want to store a copy of the „plain text“ script, so I would like to copy the state with the loaded script and all other things can be cleared

Can I do this with the C API?



Am 06.10.2014 um 20:29 schrieb Tim Hill <>:

> Well there is no Lua API to do this and a full clone would be immensely difficult. You would have to deep copy the registry, and in the process would hit things that are not easily copied such as user data and threads. So in practice this could only work if the source Lua state had no userdata and no threads. You would also have to handle issues with loaded libraries and special case some of the standard libraries.
> --Tim
>> On Oct 6, 2014, at 9:25 AM, Philipp Kraus <> wrote:
>> Hello,
>> I have encapsulate the lua_state in a C++ class and I can run my Lua scripts well, but I would like to create a Copy-Ctor which creates a fill in depended clone of the current lua_state.
>> How can I clone the current lua_state with a full deep-copy? Does there exists a C API function, how can I do this?
>> Thanks a lot
>> Phil