[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Problem while dumping the functions and loading in different state
- From: Tim Hill <drtimhill@...>
- Date: Thu, 18 Apr 2013 14:17:06 -0700
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").
--Tim
On Apr 18, 2013, at 12:12 PM, Ashish Mahamuni <mahamuni.ashish@gmail.com> 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