简体   繁体   中英

get the function called from a C++ code

for example i have this:

static int callFunction(lua_State* L)
{
 int p = lua_gettop(L);
 if (p == 1 && lua_isfunction(L, -1)) {
  /*
   * now i need something like "get the function thats in the first parametre
   */
 }
 return 0;
}

now i need to get the function thats in the first parametre of the function in this C++ code, sry for not being clear, i suck at explaining.

If you need to call the function, you can use lua_call. Lua however won't allow you to take any sort of useful pointer to Lua functions. If you want to store a function in Lua, then you will have to use the Lua registry to store it.

我猜想您可能想研究lua_tocfunction()函数。

If you want to store a "pointer" to a Lua function in C++, you could just store the /name/ of the Lua function and then do as DeadMG says and call it with lua_call , as here: http://pgl.yoyo.org/luai/i/lua_call .

If you are stuck with the code you have already, it's kind of a sticky problem; I'm not sure you can get the name of the Lua function from the stack you have. In other words, you may need to modify the code one level up from what you posted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM