src/nexus_lua.c
changeset 116 92e71129074d
parent 106 f00661136ac2
child 136 81dbeb5bc38e
--- a/src/nexus_lua.c	Thu Apr 02 03:02:54 2009 +0300
+++ b/src/nexus_lua.c	Thu Apr 02 03:19:44 2009 +0300
@@ -1,11 +1,36 @@
 #include "nexus_lua.h"
 #include "lua_objs.h"
+#include "lua_irc.h"
 
 #include <stdlib.h>
 
 #include <lua5.1/lualib.h>
 #include <lua5.1/lauxlib.h>
 
+/**
+ * Initialize the lua state, in protected mode
+ */
+int nexus_lua_init (lua_State *L)
+{
+    struct nexus_lua *lua;
+   
+    // read the nexus_lua off the stack
+    if ((lua = lua_touserdata(L, 1)) == NULL)
+        luaL_error(L, "lua_touserdata: NULL");
+
+    // open the standard libraries
+    luaL_openlibs(L);
+ 
+    // open our core lua api
+    lua_objs_init(lua);
+
+    // and the irc_* stuff
+    lua_irc_init(lua);
+
+    // ok
+    return 0;
+}
+
 err_t nexus_lua_create (struct nexus_lua **lua_ptr, struct nexus *nexus, struct error_info *err)
 {
     struct nexus_lua *lua;
@@ -21,13 +46,8 @@
     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 (lua_objs_init(lua, err))
+    // init in protected mode
+    if (nexus_lua_error(lua->st, lua_cpcall(lua->st, &nexus_lua_init, lua), err))
         goto error;
 
     // ok