how to callback a lua function from a c function

Posted by pierr on Stack Overflow See other posts from Stack Overflow or by pierr
Published on 2010-04-22T03:25:51Z Indexed on 2010/04/22 8:53 UTC
Read the original article Hit count: 260

Filed under:
|

Hi,

I have a c function test_callback accepting a point to a function as the parameter and It will "callback" that function.

//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 

Now, I want to wrap test_callback so that they can be called from lua, suppose the name is lua_test_callback ;and also the input parameter to it would be a lua function. How should I achieve this goal?

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 "

© Stack Overflow or respective owner

Related posts about lua

Related posts about callback