# HG changeset patch # User Tero Marttila # Date 1238536654 -10800 # Node ID b6b183fbf373f5786772ebba6b8c4c2b9f9c1648 # Parent fc196bb4bcc2f607d56950fad521c8c5f6c84b03 implement a separate nexus_lua module diff -r fc196bb4bcc2 -r b6b183fbf373 src/CMakeLists.txt --- a/src/CMakeLists.txt Wed Apr 01 00:38:16 2009 +0300 +++ b/src/CMakeLists.txt Wed Apr 01 00:57:34 2009 +0300 @@ -12,9 +12,10 @@ set (CORE_SOURCES error.c log.c) set (SOCK_SOURCES sock.c sock_tcp.c sock_gnutls.c sock_test.c line_proto.c) set (IRC_SOURCES irc_line.c irc_conn.c irc_net.c irc_chan.c chain.c irc_cmd.c irc_proto.c irc_client.c irc_user.c irc_queue.c) -set (CONSOLE_SOURCES console.c lua_console.c lua_objs.c) +set (LUA_SOURCES nexus_lua.c lua_objs.c) +set (CONSOLE_SOURCES console.c lua_console.c) -set (NEXUS_SOURCES nexus.c ${CORE_SOURCES} ${SOCK_SOURCES} ${IRC_SOURCES} ${CONSOLE_SOURCES} signals.c module.c config.c) +set (NEXUS_SOURCES nexus.c ${CORE_SOURCES} ${SOCK_SOURCES} ${IRC_SOURCES} ${LUA_SOURCES} ${CONSOLE_SOURCES} signals.c module.c config.c) set (TEST_SOURCES test.c ${CORE_SOURCES} ${SOCK_SOURCES} ${IRC_SOURCES}) set (IRC_LOG_SOURCES irc_log.c) diff -r fc196bb4bcc2 -r b6b183fbf373 src/lua_console.c --- a/src/lua_console.c Wed Apr 01 00:38:16 2009 +0300 +++ b/src/lua_console.c Wed Apr 01 00:57:34 2009 +0300 @@ -4,19 +4,20 @@ #include -#include #include static void lua_console_on_line (const char *line, void *arg) { struct lua_console *lc = arg; - lua_State *L = lc->st; + lua_State *L = lc->lua->st; int ret; // ignore empty lines and EOF if (!line || !(*line)) return; + // XXX: move to nexus_lua + // load the line as a lua function if ((ret = luaL_loadstring(L, line))) goto error; @@ -61,7 +62,7 @@ .on_line = &lua_console_on_line, }; -err_t lua_console_create (struct lua_console **lc_ptr, struct console *console, struct nexus *nexus, struct error_info *err) +err_t lua_console_create (struct lua_console **lc_ptr, struct console *console, struct nexus_lua *lua, struct error_info *err) { struct lua_console *lc; @@ -71,41 +72,20 @@ // store lc->console = console; + lc->lua = lua; // set our console callbacks console_set_callbacks(console, &_console_callbacks, lc); - // create the lua state - if ((lc->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(lc->st); - - // then our own things - if ((ERROR_CODE(err) = lua_objs_init(lc->st, nexus))) - goto error; - // ok *lc_ptr = lc; return SUCCESS; - -error: - // destroy - lua_console_destroy(lc); - - return ERROR_CODE(err); } void lua_console_destroy (struct lua_console *lc) { - // close the lua stuff - lua_close(lc->st); - - // and the console + // the console console_destroy(lc->console); free(lc); diff -r fc196bb4bcc2 -r b6b183fbf373 src/lua_console.h --- a/src/lua_console.h Wed Apr 01 00:38:16 2009 +0300 +++ b/src/lua_console.h Wed Apr 01 00:57:34 2009 +0300 @@ -6,7 +6,7 @@ * * An interactive lua console */ -#include "nexus.h" +#include "nexus_lua.h" #include "console.h" #include @@ -18,8 +18,8 @@ /** The lowlevel line-based console */ struct console *console; - /** Our lua-state */ - lua_State *st; + /** Our lua state */ + struct nexus_lua *lua; }; /** @@ -27,7 +27,7 @@ * * This overrides the console callbacks. */ -err_t lua_console_create (struct lua_console **lc_ptr, struct console *console, struct nexus *nexus, struct error_info *err); +err_t lua_console_create (struct lua_console **lc_ptr, struct console *console, struct nexus_lua *lua, struct error_info *err); /** * Destroy the lua console state diff -r fc196bb4bcc2 -r b6b183fbf373 src/lua_objs.c --- a/src/lua_objs.c Wed Apr 01 00:38:16 2009 +0300 +++ b/src/lua_objs.c Wed Apr 01 00:57:34 2009 +0300 @@ -589,10 +589,10 @@ return 0; } -err_t lua_objs_init (lua_State *L, struct nexus *nexus) +err_t lua_objs_init (struct nexus_lua *lua) { // call in protected mode - switch (lua_cpcall(L, &_lua_objs_init, nexus)) { + switch (lua_cpcall(lua->st, &_lua_objs_init, lua->nexus)) { case 0: return SUCCESS; case LUA_ERRRUN: return ERR_LUA_RUN; case LUA_ERRMEM: return ERR_LUA_MEM; diff -r fc196bb4bcc2 -r b6b183fbf373 src/lua_objs.h --- a/src/lua_objs.h Wed Apr 01 00:38:16 2009 +0300 +++ b/src/lua_objs.h Wed Apr 01 00:57:34 2009 +0300 @@ -6,13 +6,11 @@ * * Defines lua functions to access the various objects in a nexus */ -#include "nexus.h" - -#include +#include "nexus_lua.h" /** * Registers our lua runtime objects into the given lua state */ -err_t lua_objs_init (lua_State *L, struct nexus *nexus); +err_t lua_objs_init (struct nexus_lua *lua); #endif /* LUA_OBJS_H */ diff -r fc196bb4bcc2 -r b6b183fbf373 src/module.h --- a/src/module.h Wed Apr 01 00:38:16 2009 +0300 +++ b/src/module.h Wed Apr 01 00:57:34 2009 +0300 @@ -15,6 +15,7 @@ #include "error.h" #include +#include /** * Information required to load/identify a module. diff -r fc196bb4bcc2 -r b6b183fbf373 src/nexus.c --- a/src/nexus.c Wed Apr 01 00:38:16 2009 +0300 +++ b/src/nexus.c Wed Apr 01 00:57:34 2009 +0300 @@ -272,19 +272,25 @@ struct console_config config = { .prompt = "> ", }; + struct console *console; log_info("initializing the console"); // init the console - if (console_init(&nexus->console, nexus->ev_base, &config, NULL, NULL, err)) + if (console_init(&console, nexus->ev_base, &config, NULL, NULL, err)) return ERROR_CODE(err); // create the lua console on top of that - if (lua_console_create(&nexus->lua_console, nexus->console, nexus, err)) - return ERROR_CODE(err); + if (lua_console_create(&nexus->lua_console, console, nexus->lua, err)) + goto error; // ok return SUCCESS; + +error: + console_destroy(console); + + return ERROR_CODE(err); } /** @@ -381,6 +387,10 @@ void nexus_destroy (struct nexus *nexus) { + // destroy the lua state + if (nexus->lua) + nexus_lua_destroy(nexus->lua); + // destroy the modules if (nexus->modules) modules_destroy(nexus->modules); @@ -447,8 +457,12 @@ // the IRC client if (irc_client_create(&nexus->client, &err)) FATAL_ERROR(&err, "irc_client_create"); + + // lua state + if (nexus_lua_create(&nexus->lua, nexus, &err)) + FATAL_ERROR(&err, "nexus_lua_create"); - + // parse args if (parse_args(nexus, argc, argv, &err)) FATAL_ERROR(&err, "parse_args"); diff -r fc196bb4bcc2 -r b6b183fbf373 src/nexus.h --- a/src/nexus.h Wed Apr 01 00:38:16 2009 +0300 +++ b/src/nexus.h Wed Apr 01 00:57:34 2009 +0300 @@ -10,10 +10,10 @@ #include #include "signals.h" -#include "console.h" -#include "lua_console.h" #include "module.h" #include "irc_client.h" +#include "nexus_lua.h" +#include "lua_console.h" /** * The central brain, as created in the main() function. @@ -25,18 +25,18 @@ /** Our signal handlers */ struct signals *signals; - /** Our console */ - struct console *console; - - /** Our lua console */ - struct lua_console *lua_console; - /** Our loaded modules */ struct modules *modules; /** The IRC client state */ struct irc_client *client; + /** Our lua state */ + struct nexus_lua *lua; + + /** Our lua console */ + struct lua_console *lua_console; + /** Shutting down? */ bool shutdown; }; diff -r fc196bb4bcc2 -r b6b183fbf373 src/nexus_lua.c --- /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 + +#include +#include + +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); +} diff -r fc196bb4bcc2 -r b6b183fbf373 src/nexus_lua.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/nexus_lua.h Wed Apr 01 00:57:34 2009 +0300 @@ -0,0 +1,34 @@ +#ifndef NEXUS_LUA_H +#define NEXUS_LUA_H + +/** + * @file + * + * Defines the lua environment for use with nexus + */ +#include "nexus.h" + +#include + +/** + * The global lua state + */ +struct nexus_lua { + /** The nexus we are operating on */ + struct nexus *nexus; + + /** The lua state */ + lua_State *st; +}; + +/** + * Create a new lua state for nexus + */ +err_t nexus_lua_create (struct nexus_lua **lua_ptr, struct nexus *nexus, struct error_info *err); + +/** + * Destroy the lua state + */ +void nexus_lua_destroy (struct nexus_lua *lua); + +#endif /* NEXUS_LUA_H */