[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Calling a function defined in lua from C/C++
- From: "Jerome Vuarand" <jerome.vuarand@...>
- Date: Wed, 10 May 2006 16:29:38 -0400
The function is not a C one, but it is on the stack. You can call it with lua_*call functions. Use lua_tostring to get a text representation of the Lua function:
int msg(lua_State *L)
{
int n = lua_gettop(L);
std::string msg;
for (uint i = 1; i <= n; i++) {
switch(lua_type(L, i)) {
case LUA_TFUNCTION :
msg += (const char *) stringf("(function) : %s", lua_tostring(L, i));
break;
}
}
MessageBox(0, msg.c_str(), "Message", MB_OK);
return 0;
}
-----Message d'origine-----
De : lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br] De la part de Turgut Hakki Ozdemir
Envoyé : 10 mai 2006 14:40
À : lua@bazar2.conectiva.com.br
Objet : Re: Calling a function defined in lua from C/C++
Greetings,
>>>
>>> lua_call(L, 0, 0); // when i use lua_pcall instead of lua_call
>>> here, surrounded with error handling functionality it complains
>>> about nil values
>>>
>>> lua_close(L);
>>> }
>>
>>
>> After loading the test.lua file, you must run it because it's
>>just a function on the stack at this point. After running it your
>>declarations will have effects.
>>
>>
>>
Does'nt lua_*call functions do it?
------------------------ some lua code ---------------------- function funk()
end
--- If the body of script does not have any code it works
------------------------ some c code -------------------------
luaL_loadfile(L, "test.lua"); // Ok
lua_call(L, 0, 0); // Works as expected with 1st script (does nothing) 2nd script fails here
// My first message does not contains following since it failed above lua_getglobal(L, "funk"); // funk is on top of the stack?
lua_call(L, 0, 0); // Ok, funk being called
------------------------ end of some c code ------------
but... when i run above code with this script, int msg(lua_State *L); function gets 0x0 as function address
------------------------ another lua code --------------- function funk()
end
msg(funk) -- msg will fail to receive address
----------------------------------------------------------------------
so lua scripts can't register their functions as callbacks :)