Skip to content

Instantly share code, notes, and snippets.

@jyrkive
Created July 6, 2016 13:04
Show Gist options
  • Save jyrkive/bdcd77001a65d4d4fe276cc200574f86 to your computer and use it in GitHub Desktop.
Save jyrkive/bdcd77001a65d4d4fe276cc200574f86 to your computer and use it in GitHub Desktop.
const char* TABLE_NAME = "functions";
const char* FUNCTION_NAME = "my_lua_function";
int my_lua_function(lua_State* L);
lua_State* L;
/* Assuming that the table exists already */
/* Get the table */
lua_getglobal(L, TABLE_NAME);
/* Push the function name */
lua_pushstring(L, FUNCTION_NAME);
/* Register the function */
lua_pushcfunction(L, my_lua_function);
/* Assign the function to the table */
lua_settable(L, -3);
/* Remove the table from the stack (i.e. ensure that the stack is like before registering the function) */
lua_pop(L, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment