Here is what I want to do:
1) There is a lua function defined by the user, which I dont know by name. Say it is:
function f(x) return 2*x; end
2) The user will then call a function (that is designed in step 3) from Lua say like:
a=foo(f,3) --expecting a=6
3) The C++ function for foo is:
int lua_foo(lua_State *L)
{
int nargs = lua_gettop(L);
if(nargs<2) throw "ERROR: At least two arguments i) Function ii) number must be supplied";
int type = lua_type(L, 1);
if(type!=LUA_TFUNCTION) throw "ERROR: First argument must be a function";
double arg2=lua_tonumber(L,2);
lua_pushnumber(L,arg2);
lua_pcall(L, 1, 1, 0) ; //trying to call the function f
double result=lua_tonumber(L,-1); //expecting the result to be 6
lua_pushnumber(L,result);
lua_pop(L,nargs);
return 1;
}
In the C++ code, I know the first argument is a function and second argument is a number. I am trying to call the first argument (the function) with the second argument (the number) as its arg.