diff options
author | P. J. McDermott <pj@pehjota.net> | 2015-08-25 12:49:31 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2015-08-25 12:49:31 (EDT) |
commit | 2c4ba39153c8fd7b03e81689ed6103dc91ce0168 (patch) | |
tree | 515e94d7d7512a0065d092774215042a11a6c142 /src | |
parent | 970942b41aa603672e7589ecf3dbedf480b25a62 (diff) | |
download | overworld-rpg-2c4ba39153c8fd7b03e81689ed6103dc91ce0168.zip overworld-rpg-2c4ba39153c8fd7b03e81689ed6103dc91ce0168.tar.gz overworld-rpg-2c4ba39153c8fd7b03e81689ed6103dc91ce0168.tar.bz2 |
Finish Lua->C FFI setup
Diffstat (limited to 'src')
-rw-r--r-- | src/scripting/ffi.c | 24 |
1 files changed, 23 insertions, 1 deletions
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) |