src/lua_objs.c
author Tero Marttila <terom@fixme.fi>
Thu, 21 May 2009 16:23:50 +0300
branchlua-threads
changeset 207 3fa22abb5421
parent 203 ffdf53fd0337
child 211 b460d958a685
permissions -rw-r--r--
fix lua_nexus_sleep to use EV_TIMEOUT + misc
#include "lua_objs.h"
#include "lua_irc.h"
#include "lua_func.h"
#include "log.h"

#include <stdlib.h>
#include <string.h>

/**
 * Wrapper for module
 */
struct lua_module {
    struct module *module;
};

static struct lua_type lua_module_type = LUA_TYPE("spbot.module");

/**
 * Create a lua_module userdata from the given module and push it onto the stack, returning 1.
 *
 * The given module should be a reference of its own right.
 */
static int lua_module_create (lua_State *L, struct module *module)
{
    // create the new obj
    struct lua_module *lua_module = lua_type_create(L, &lua_module_type, sizeof(*lua_module));

    // initialize
    lua_module->module = module;

    // ok
    return 1;
}

/**
 * module_put() our module reference
 */
static int lua_module__gc (lua_State *L)
{
    struct lua_module *lua_module = lua_type_get(L, &lua_module_type, 1);
    
    // put it
    module_put(lua_module->module);

    return 0;
}

static int lua_module_conf (lua_State *L)
{
    struct lua_module *lua_module = lua_type_get(L, &lua_module_type, 1);
    const struct config_option *option;
    struct error_info err;
    bool is_err = true;

    // the list of given config values, and temporary storage for string values
    struct config_value values[CONFIG_VALUES_MAX], *value = values;
    char *value_bufs[CONFIG_VALUES_MAX], **value_buf = value_bufs;

    // number of arguments given
    int nargs = lua_gettop(L), argidx = 2;

    // init to zero
    memset(values, 0, sizeof(values));
    memset(value_bufs, 0, sizeof(value_bufs));

    // XXX: come up with some better way...
    struct nexus *nexus = lua_module->module->modules->nexus;

    // the config name
    const char *conf_name = luaL_checkstring(L, argidx++);

    // look it up
    if ((option = module_conf_lookup(lua_module->module, conf_name, &err)) == NULL)
        return luaL_error(L, "module_conf_lookup: %s/%s: %s", module_name(lua_module->module), conf_name, error_msg(&err));

    // maximum number of arguments accepted
    int maxargs = config_params_count(option);

    // too many arguments?
    if (nargs - argidx > maxargs)
        return luaL_error(L, "lua_module_conf: too many arguments (>%d) given (%d)", maxargs, nargs - argidx);
    
    // the current param
    const struct config_param *param = option->params;

    // apply each given argument to the correct param, storing it in value
    for (; argidx <= nargs; argidx++, value++, param++) {
        // the given config value
        switch (lua_type(L, argidx)) {
            case LUA_TNONE:
            case LUA_TNIL:
                // no value
                value->type = CONFIG_NULL;

                break;

            case LUA_TSTRING: {
                // string arg
                const char *arg_str = lua_tostring(L, argidx);

                // copy it as a mutable string buffer
                if ((*value_buf = strdup(arg_str)) == NULL) {
                    lua_pushfstring(L, "strdup");
                    goto error;
                }

                // parse it as a raw value
                if (config_parse_param(param, nexus, value, *value_buf, &err)) {
                    lua_pushfstring(L, "config_parse: %s/%s: %s", option->name, *value_buf, error_msg(&err));
                    goto error;
                }

                // seek to next value_buf
                value_buf++;

            } break;
    
            case LUA_TUSERDATA:
                // some kind of userdata, use its metatable to figure out what type it is
                if (!lua_getmetatable(L, argidx)) {
                    lua_pushfstring(L, "config value is userdata without metatable");
                    goto error;
                }

                // get the target metatable
                lua_getfield(L, LUA_REGISTRYINDEX, "evirc.chan");

                // is it a chan?
                if (!lua_rawequal(L, -1, -2)) {
                    lua_pushfstring(L, "config value is userdata of unknown type");
                    goto error;
                }

                // pop the metatables
                lua_pop(L, 2);

                // get the irc_chan
                struct lua_chan *lua_chan = lua_touserdata(L, argidx);

                // build the value
                value->type = CONFIG_IRC_CHAN;
                value->irc_chan = lua_chan->chan;
                
                break;
            
            default:
                lua_pushfstring(L, "config value is of unknown lua type '%s'", lua_typename(L, argidx));
                goto error;

        }
    }

    // apply it
    if (module_conf(lua_module->module, option, values, &err)) {
        lua_pushfstring(L, "module_conf: %s/%s: %s", module_name(lua_module->module), option->name, error_msg(&err));
        goto error;
    }
    
    // ok
    is_err = false;

error:
    // release any allocated strings
    for (value_buf = value_bufs; value_buf <= value_bufs + CONFIG_VALUES_MAX && *value_buf; value_buf++)
        free(*value_buf);
    
    // either error or successful return
    if (is_err)
        return lua_error(L);
    else
        return 0;
}

static int lua_module_unload (lua_State *L)
{
    struct lua_module *lua_module = lua_type_get(L, &lua_module_type, 1);
    struct error_info err;

    // just unload it
    if ((ERROR_CODE(&err) = module_unload(lua_module->module)))
        return luaL_error(L, "module_unload: %s: %s", module_name(lua_module->module), error_msg(&err));

    // ok
    return 0;
}

static struct lua_method lua_module_methods[] = LUA_METHODS(
        LUA_METHOD("__gc",      lua_module__gc,     NULL    ),
        LUA_METHOD("conf",      lua_module_conf,    NULL    ),
        LUA_METHOD("unload",    lua_module_unload,  NULL    )
);

/**
 * Wrapper for modules
 */
struct lua_modules {
    struct modules *modules;
    
    // strdup'd path for module_path
    // XXX: remove when gc'd
    char *path;
};

static struct lua_type lua_modules_type = LUA_TYPE("spbot.modules");

static int lua_modules__gc (lua_State *L)
{
    struct lua_modules *lua_modules = lua_type_get(L, &lua_modules_type, 1);
    
    // remove the modules path if it was set by us
    if (lua_modules->path && modules_path(lua_modules->modules, NULL) == lua_modules->path)
        modules_path(lua_modules->modules, "");

    // release any strdup'd path
    free(lua_modules->path);

    // ok
    return 0;
}

static int lua_modules_path (lua_State *L)
{
    struct lua_modules *lua_modules = lua_type_get(L, &lua_modules_type, 1);
    char *path = NULL;
    const char *old_path;
    
    if (!lua_isnoneornil(L, 2)) {
        // the new path
        if ((path = strdup(luaL_checkstring(L, 2))) == NULL)
            return luaL_error(L, "strdup");
    }

    // set or get
    old_path = modules_path(lua_modules->modules, path);

    // return the old path
    if (old_path)
        lua_pushstring(L, old_path);
    else
        lua_pushnil(L);

    if (path) {
        // replace the old path
        free(lua_modules->path); 
        lua_modules->path = path;
    }

    // ok
    return 1;
}

static int lua_modules_load (lua_State *L)
{
    struct lua_modules *lua_modules = lua_type_get(L, &lua_modules_type, 1);
    struct module *module;
    struct module_info info;
    struct error_info err;

    // the module name/path
    info.name = luaL_checkstring(L, 2);
    info.path = lua_isnoneornil(L, 3) ? NULL : luaL_checkstring(L, 3);

    // load and get a new reference
    if (module_load(lua_modules->modules, &module, &info, &err))
        return luaL_error(L, "module_load: %s/%s: %s", info.name, info.path, error_msg(&err));

    // wrap
    return lua_module_create(L, module);
}

static int lua_modules_module (lua_State *L)
{
    struct lua_modules *lua_modules = lua_type_get(L, &lua_modules_type, 1);
    struct module *module;
    
    // the module name
    const char *name = luaL_checkstring(L, 2);

    // look it up, as a new reference
    if ((module = modules_get(lua_modules->modules, name)) == NULL)
        return luaL_error(L, "module_get: %s: no such module", name);

    // wrap
    return lua_module_create(L, module);
}

static struct lua_method lua_modules_methods[] = LUA_METHODS(
        LUA_METHOD("__gc",      lua_modules__gc,    NULL    ),
        LUA_METHOD("path",      lua_modules_path,   NULL    ),
        LUA_METHOD("load",      lua_modules_load,   NULL    ),
        LUA_METHOD("module",    lua_modules_module, NULL    )
    );



/**
 * Initialize the spbot.modules type for lua_modules, and registers an instance bound to the given modules list at
 * 'modules'.
 */
static void lua_modules_init (lua_State *L, struct modules *modules)
{
    // allocate the global "modules" object
    struct lua_modules *lua_modules = lua_type_register_global(L, &lua_modules_type, lua_modules_methods, "modules", sizeof(*lua_modules));
    
    // initialize it
    lua_modules->modules = modules;
}

/**
 * Wrapper for nexus
 */
struct lua_nexus {
    struct nexus *nexus;
};

static struct lua_type lua_nexus_type = LUA_TYPE("spbot.nexus");

static int lua_nexus_shutdown (lua_State *L)
{
    struct lua_nexus *lua_nexus = lua_type_get(L, &lua_nexus_type, 1);

    // just shut it down
    nexus_shutdown(lua_nexus->nexus);

    return 0;
}

static int lua_nexus_load_config (lua_State *L)
{
    struct lua_nexus *lua_nexus = lua_type_get(L, &lua_nexus_type, 1);
    struct error_info err;

    const char *path = luaL_checkstring(L, 2);
    
    // just load it
    if (nexus_load_config(lua_nexus->nexus, path, &err))
        return luaL_error(L, "nexus_load_config(%s): %s", path, error_msg(&err));

    return 0;
}

static struct lua_func lua_nexus_sleep_func = LUA_FUNC(&lua_nexus_type, "sleep",
        "Schedules itself to resume after the given delay (in seconds) and yields",

        LUA_FUNC_ARG_INT("tv_sec",  LUA_ARG_REQUIRED)
    );

static void lua_nexus_sleep_wakeup (evutil_socket_t fd, short what, void *arg)
{
    lua_State *L = arg;

    (void) fd;
    (void) what;

    // resume the thread that called lua_nexus_sleep
    lua_thread_resume_state(L);
}

static int lua_nexus_sleep (lua_State *L)
{
    struct lua_nexus *lua_nexus;
    long tv_sec;

    // parse args
    lua_args_parse(L, &lua_nexus_sleep_func, (void *) &lua_nexus, &tv_sec);

    // build tv
    struct timeval tv = { tv_sec, 0 };
    
    // schedule wakeup
    // use a pure-timeout event
    if (event_base_once(lua_nexus->nexus->ev_base, -1, EV_TIMEOUT, lua_nexus_sleep_wakeup, L, &tv))
        return luaL_error(L, "event_base_once");

    // yield
    return lua_yield(L, 0);
}

static struct lua_method lua_nexus_methods[] = LUA_METHODS(
        LUA_METHOD("shutdown",      lua_nexus_shutdown,     NULL                    ),
        LUA_METHOD("load_config",   lua_nexus_load_config,  NULL                    ),
        LUA_METHOD("sleep",         lua_nexus_sleep,        &lua_nexus_sleep_func   )
    );

/**
 * Initialize the spbot.nexus type for lua_nexus, and registers an instance bound to the given nexus list at
 * 'nexus'.
 */
static void lua_nexus_init (lua_State *L, struct nexus *nexus)
{
    // allocate the global "nexus" object
    struct lua_nexus *lua_nexus = lua_type_register_global(L, &lua_nexus_type, lua_nexus_methods, "nexus", sizeof(*lua_nexus));
    
    // initialize it
    lua_nexus->nexus = nexus;
}


/**
 * Global functions
 */
static int lua_log_level (lua_State *L)
{
    // log level as a string
    enum log_level new_level = luaL_checkoption(L, 1, NULL, log_level_names);

    // set it
    set_log_level(new_level);

    // ok
    return 0;
}

static int lua_log (lua_State *L)
{
    // log level as a string
    enum log_level level = luaL_checkoption(L, 1, NULL, log_level_names);
    
    // log message
    const char *msg = luaL_checkstring(L, 2);

    // log it
    _log_msg(level, "lua", "%s", msg);

    // ok
    return 0;
}

static const struct luaL_Reg lua_global_functions[] = {
    {   "log_level",    lua_log_level               },
    {   "log",          lua_log                     },
    {   NULL,           NULL                        }
};

static void lua_global_init (lua_State *L)
{
    const struct luaL_Reg *reg;

    for (reg = lua_global_functions; reg->name && reg->func; reg++) {
        // put the function on the stack
        lua_pushcfunction(L, reg->func);

        // set the global
        lua_setglobal(L, reg->name);
    }
}

void lua_objs_init (struct nexus_lua *lua)
{
    // register types
    lua_type_register(lua->st, &lua_module_type, lua_module_methods);
    
    // globals
    lua_nexus_init(lua->st, lua->nexus);
    lua_modules_init(lua->st, lua->nexus->modules);
    
    // global functions
    lua_global_init(lua->st);
}