src/nexus_lua.c
changeset 105 b6b183fbf373
child 106 f00661136ac2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/nexus_lua.c	Wed Apr 01 00:57:34 2009 +0300
@@ -0,0 +1,50 @@
+#include "nexus_lua.h"
+#include "lua_objs.h"
+
+#include <stdlib.h>
+
+#include <lua5.1/lualib.h>
+#include <lua5.1/lauxlib.h>
+
+err_t nexus_lua_create (struct nexus_lua **lua_ptr, struct nexus *nexus, struct error_info *err)
+{
+    struct nexus_lua *lua;
+
+    // alloc
+    if ((lua = calloc(1, sizeof(*lua))) == NULL)
+        return SET_ERROR(err, ERR_CALLOC);
+
+    // store
+    lua->nexus = nexus;
+
+    // create the lua state
+    if ((lua->st = luaL_newstate()) == NULL)
+        JUMP_SET_ERROR(err, ERR_LUA_MEM);
+    
+    // we can then load the core libs
+    // XXX: we don't need all of these
+    // XXX: errors?
+    luaL_openlibs(lua->st);
+
+    // then our own things
+    if ((ERROR_CODE(err) = lua_objs_init(lua)))
+        goto error;
+
+    // ok
+    *lua_ptr = lua;
+
+    return SUCCESS;
+
+error:
+    nexus_lua_destroy(lua);
+
+    return ERROR_CODE(err);
+}
+
+void nexus_lua_destroy (struct nexus_lua *lua)
+{
+    // close the lua stuff
+    lua_close(lua->st);
+
+    free(lua);
+}