summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/scripting/ffi.c24
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)