Question

I am using Visual C++ 2012 and trying to write a c extension for Lua. Currently I am design a function prototype:

lib.myfunc(number, {a=1,b=2,c=3},{d=4,e=5,...})

There are 3 parameters for 'myfunc' function, the 1st parameter is a number of integer, the 2nd and 3rd parameters are table type, and I need to access the values by keys(the keys are 'a','b','c'...)

I've read the lua manual and googled for many tutorials but I still cannot get it work. I want a sample C code doing this job, thank you~

Was it helpful?

Solution

I don't really know luabind, so I'm not sure, if they offer any of their own facilities to do that, but in Lua you'd do that like this:

int myLuaFunc(lua_State *L)
{
  int arg1 = luaL_toint(L, 1);
  luaL_checktype(L, 2, LUA_TTABLE);    //Throws an error, if it's not a table
  luaL_checktype(L, 3, LUA_TTABLE);

  //Get values for the first table and push it on the stack
  lua_getfield(L, 2, "keyname");   //Or use lua_gettable
  //Assuming it's a string, get it
  const char *tmpstr = lua_tostring(L, -1);

  //..... Similariliy for all the other keys
}

You might want to refer to Lua Reference Manual for description of the functions I used.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top