From 2c4ba39153c8fd7b03e81689ed6103dc91ce0168 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Tue, 25 Aug 2015 12:49:31 -0400 Subject: Finish Lua->C FFI setup --- diff --git a/src/scripting/ffi.c b/src/scripting/ffi.c index 6457f73..9029b25 100644 --- a/src/scripting/ffi.c +++ b/src/scripting/ffi.c @@ -41,6 +41,7 @@ static int call_arg_index; static void ffi_register_functions_in_namespace(lua_State *l, struct ffi_namespace *parent); +static int ffi_handle_function(lua_State *l); struct ffi_namespace * ffi_add_namespace(struct ffi_namespace *parent, const char *name) @@ -108,11 +109,32 @@ ffi_register_functions_in_namespace(lua_State *l, struct ffi_namespace *parent) struct ffi_namespace *ns; struct ffi_function *fn; + lua_checkstack(l, 2); for (ns = parent->ns_head; ns != NULL; ns = ns->next) { + lua_newtable(l); + ffi_register_functions_in_namespace(l, ns); + if (parent == &root) { + lua_setglobal(l, ns->name); + } else { + lua_setfield(l, -1, ns->name); + } } for (fn = ns->fn_head; fn != NULL; fn = fn->next) { + lua_pushlightuserdata(l, fn); + lua_pushcclosure(l, &ffi_handle_function, 1); + lua_setfield(l, -2, fn->name); } - l = l; +} + +static int +ffi_handle_function(lua_State *l) +{ + struct ffi_function *fn; + + fn = (struct ffi_function *) lua_touserdata(l, lua_upvalueindex(1)); + fn->func(); + + return 0; } void ffi_context_switch(struct script *script) -- cgit v0.9.1