lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Apr 15, 2011, at 2:21 PM, jseb wrote:

> 
> foo = require ("libfoo")
> 
> function lua_callback()
>   print ("I am the function located in Lua script")
> end
> 
> foo:set_callback (lua_callback)


This calls foo.set_callback(foo, lua_callback)

-- ...

> static void fx_callback (lua_State *L)
> {
>    static lua_State *L_context = 0;
>    static int index_fx_lua = LUA_REFNIL;
> 
>    if (L) { // initialised only at startup
>    L_context = L; // we store the context
>    // and store the function given as Lua parameter
>    index_fx_lua = luaL_ref (L, LUA_REGISTRYINDEX);
>    if (index_fx_lua == LUA_REFNIL) {
>      fprintf(stderr,"luaL_ref : erreur\n");
>    }
> 
>  } else { // it was called from the thread loop
>    lua_rawgeti (L_context, LUA_REGISTRYINDEX, index_fx_lua);
>    lua_pcall(L_context, 0,0,0);
>  }
> }

This stores foo in index_fx_lua.

Perhaps you meant:

 foo.set_callback (lua_callback)

e