wie man ein lua-callback-Funktion aus einer c-Funktion

Ich habe eine C-Funktion (A) test_callback akzeptiert einen Zeiger auf eine Funktion(B) als parameter und Einen "Rückruf" B.

//typedef int(*data_callback_t)(int i);
int test_callback(data_callback_t f)
{
    f(3);   
}


int datacallback(int a )
{
    printf("called back %d\n",a);
    return 0;
}


//example 
test_callback(datacallback); // print : called back 3 

Nun, ich will wickeln test_callback so, dass Sie aufgerufen werden kann, lua, nehme an, der name ist lua_test_callback ;und auch die input-parameter an, es wäre eine lua-Funktion. Wie sollte ich dieses Ziel erreichen?

function lua_datacallback (a )
    print "hey , this is callback in lua" ..a
end


lua_test_callback(lua_datacallback)  //expect to get "hey this is callback in lua 3 "

EDIT:

Dieser link bieten eine Möglichkeit zum speichern der callback-Funktion für die spätere Verwendung .

//save function for later use 
callback_function = luaL_ref(L,LUA_REGISTRYINDEX);


//retrive function and call it 
lua_rawgeti(L,LUA_REGISTRYINDEX,callback_function);
//push the parameters and call it
lua_pushnumber(L, 5); // push first argument to the function
lua_pcall(L, 1, 0, 0); // call a function with one argument and no return values
InformationsquelleAutor pierrotlefou | 2010-04-22
Schreibe einen Kommentar