The pattern is something like:
int fvalues (lua_State *L) {
... get function arguments with lua_to* functions ...
... get upvalues with lua_getupvalue and lua_upvalueindex and lua_to* functions ...
... use arguments and upvalues ...
}
int callFValues(lua_State *L) {
const char* cstr = "This_is_a_test";
// push two values that will be upvalues of fvalues
lua_pushstring(L, cstr);
lua_pushstring(L, cstr);
// create closure using the two upvalues:
lua_pushcclosure(L, &fvalues, 2);
// call it:
lua_pcall(L, 0,0,0);
return 1;
}
Then register callFValues. If you want to return a closure (as is common in Lua script), for use by your Lua script, pretty much same:
int createClosure(lua_State *L) {
const char* cstr = "This_is_a_test";
// push two values that will be upvalues of fvalues
lua_pushstring(L, cstr);
lua_pushstring(L, cstr);
// create closure using the two upvalues:
lua_pushcclosure(L, &fvalues, 2);
return 1; // return it to caller
}
Then register createClosure, call it from script as fval = createClosure() then fval is a closure of fvalues with the two upvalues created by createClosure.
Read section 27.3.3 of PIL for detailed example. But in your code, your fvalues, which is a lua_CFunction you want to close and call, is itself creating a closure of itself, this makes my head spin :) Also,
lua_pushstring(L, cstr);
lua_pushcclosure(L, &fvalues, 1);
lua_pushstring(L, cstr);
lua_pushcclosure(L, &fvalues, 2);
creates a closure for fvalues with the first string added to stack being its one unique upvalue; this removes the string from stack, and puts closure on stack; then you push another string, and create another closure of fvalues, this time with 2 upvalues: the first upvalue is the first closure created, the second one is the second string added. My head spins even more. Hopefully with the PIL section and the pattern I show you have clearer picture.