lua-users home
lua-l archive

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

"call_me" is the dynamically generated function.
Whole definition is stored in variable "str".

I was under assumption that when I do a "load(str)" pushes that function on stack.
I would like to dump call_me and test2 functions in single bytecode chunk. so that when I load it in L2, it would work straight away.

It is worth noting that if I load my dumped buffer using L1 it works fine ( test2 is able to invoke call_me method ). that means call_me is available on stack.
Trouble is, why it is not coming in dumped buffer. It appears that dumped buffer has only test2.

I also tried pushing call_me explicitly before I dump. somthing like:

lua_getglobal( L1, "call_me" );
if( luaL_loadfile( L1, "test2.lua" ) ) return 1;
combine(L1, 2); 
if( lua_dump( L1, writer, buffer_ptr ) ) return 1;

Still no luck.


On Thu, Apr 18, 2013 at 10:17 PM, Tim Hill <> wrote:
I might be missing something, but where in your code do you set "call_me" ?? Running test() in test.lua leaves behind a global in L1 named "bytecode". You need to get this onto the stack and then dump it (you can do this in Lua or C), then copy over the string from L1 to L2, then use "load()" in L2 to load the function there (presumably as "call_me").


On Apr 18, 2013, at 12:12 PM, Ashish Mahamuni <> wrote:

> I am using Lua 5.2.1 and have problems while dealing with dumped bytecode.
> I am not able to load it correctly among different states.
> Could you please guide me?
> test.lua
>     function test()
>         print("I can execute test")
>         str = "function call_me()\nprint(\"how about this?\")\nend\n"
>         bytecode = load(str)
>         --here I am not calling "bytecode()" as I have to dump call_me function.
>     end
> test2.lua
>     function test2()
>         call_me()
>     end
> //Using first lua_State, lets say L1
>     if( luaL_loadfile( L1, "test.lua" ) || lua_pcall(L1, 0, LUA_MULTRET, 0)) return 1;
>     lua_getglobal( L1, "test" );
>     lua_call( L, 0, 0 );
> //At this point, my "call_me" function should be loaded.
> //Here, after loading test2.lua, I am *not* calling lua_pcall, as I am going to dump this.
>     if( luaL_loadfile( L1, "test2.lua" ) )     return 1;
> //Now call "combine", this is the same function which is in luac.c, I am using it before I dump.
> //without this lua_dump fails( rather crashes. )
>     combine(L1, 1);
>     if( lua_dump( L1, writer, buffer_ptr ) ) return 1;
> //Here(using state L1) If I load my dumped buffer and try to execute test2(). it all works fine.
> //Problem starts when I load this buffer in different state, lets say L2.
>     if( luaL_loadbufferx( L2, buffer_ptr, buffer_size, "blob", "b" ) || lua_pcall(L2, 0, LUA_MULTRET, 0) ) return 1;
>     lua_getglobal( L2, "test2" );
>     lua_call( L2, 0, 0 );
> //In this case, it is able to execute test2() but inside that it is unable to find "call_me". it says "nil".
> --Ashish