Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the lua command when a c function is called

Tags:

c++

c

lua

Supposed I register many different function names in Lua to the same function in C. Now, everytime my C function is called, is there a way to determine which function name was invoked?

for example:

int runCommand(lua_State *lua)
{
  const char *name = // getFunctionName(lua) ? how would I do this part
  for(int i = 0; i < functions.size; i++)
    if(functions[i].name == name)
      functions[i].Call()
}

int main()
{
  ...

  lua_register(lua, "delay", runCommand);
  lua_register(lua, "execute", runCommand);
  lua_register(lua, "loadPlugin", runCommand);
  lua_register(lua, "loadModule", runCommand);
  lua_register(lua, "delay", runCommand);
}

So, how do I get the name of what ever function called it?

like image 217
Nick Banks Avatar asked May 25 '10 18:05

Nick Banks


People also ask

How do I call a Lua function from c?

So, the complete round trip is: Start the lua interpreter Register the C functions you want lua to use Call a lua function from within C, either by a call to luaL_dofile, or by calling a lua function directly The lua function that you called from C can access the C function

How to call Lua_pushnil from C/C++?

lua_pushnil (lua_state *L) Call the function, using the function: We need to pass in both the number of arguments that we are passing in, and the number of arguments that we expect in return. Lua is very loose about the number of arguments and return value, but C/C++ is less so -- hence we need to be explicit when calling lua from C/C++.

How to get the input parameter of a Lua function?

C/C++ functions that are called from lua need to take as an input parameter the lua state. The parameters can be examined on the call stack using the functions lua_tointeger, lua_tonumber, etc (described above). The first parameter is at index 1, the second parameter is at index 2, and so on.

How do I call FIB from the top of the Lua stack?

// Push the fib function on the top of the lua stack lua_getglobal (L, "fib"); // Push the argument (the number 13) on the stack lua_pushnumber (L, 13); // call the function with 1 argument, returning a single result. Note that the function actually // returns 2 results -- we just want one of them.


2 Answers

Another way to attack your question is by using upvalues. Basically, you register the C functions with the function below instead of lua_register:

void my_lua_register(lua_State *L, const char *name, lua_CFunction f)
{
      lua_pushstring(L, name);
      lua_pushcclosure(L, f, 1);
      lua_setglobal(L, name);
}

Then, getFunctionName is straight forward

const char* getFunctionName(lua_State* L)
{
    return lua_tostring(L, lua_upvalueindex(1));
}

That said, what you trying to do seems fishy - what are you trying to achieve? The runCommand function posted in the question looks like a horribly inefficient way to do something that Lua does for you anyway.

like image 68
sbk Avatar answered Sep 18 '22 13:09

sbk


You can use lua_getinfo : http://pgl.yoyo.org/luai/i/lua_getinfo

This might work:

const char* lua_getcurrentfunction(lua_State* L) {
    lua_Debug ar;
    lua_getstack(L, 1, &ar);
    lua_getinfo(L, "f", &ar);
    return ar.name;
}

There is one caveat:

name: a reasonable name for the given function. Because functions in Lua are first-class values, they do not have a fixed name: some functions may be the value of multiple global variables, while others may be stored only in a table field. The lua_getinfo function checks how the function was called to find a suitable name. If it cannot find a name, then name is set to NULL.

like image 27
Nick Van Brunt Avatar answered Sep 19 '22 13:09

Nick Van Brunt