lua-users home
lua-l archive

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


> Is it possible to dynamically register C functions in Lua?
> 
> Pseudocode:
> 
>     add_function_to_lua(lua_State *L, char *libname, char *fname, int (*func)(lua_State *)) {
>         struct luaL_reg *reg = malloc(sizeof(*reg));
>         reg->name = fname;
>         reg->func = func;
> 
>         luaL_register(L, libname, reg);


	lua_getglobal(L,libname);
	lua_pushcfuntion(L,func);
	lua_setfield(L,fname);
	lua_pop(L,1);	/* remove library from stack when done */