src/spbot/nexus_lua.c
branchnew-lib-errors
changeset 219 cefec18b8268
parent 218 5229a5d098b2
equal deleted inserted replaced
218:5229a5d098b2 219:cefec18b8268
     1 #include "nexus_lua.h"
     1 #include "nexus_lua.h"
     2 #include "../lua_objs.h"
     2 #include "lua_objs.h"
     3 #include "../lua_irc.h"
     3 #include "../lua_irc.h"
     4 
     4 
     5 #include <stdlib.h>
     5 #include <stdlib.h>
     6 
     6 
     7 #include <lua5.1/lualib.h>
     7 #include <lua5.1/lualib.h>
    35 {
    35 {
    36     struct nexus_lua *lua;
    36     struct nexus_lua *lua;
    37 
    37 
    38     // alloc
    38     // alloc
    39     if ((lua = calloc(1, sizeof(*lua))) == NULL)
    39     if ((lua = calloc(1, sizeof(*lua))) == NULL)
    40         return SET_ERROR(err, ERR_CALLOC);
    40         return SET_ERROR_MEM(err);
    41 
    41 
    42     // store
    42     // store
    43     lua->nexus = nexus;
    43     lua->nexus = nexus;
    44 
    44 
    45     // create the lua state
    45     // create the lua state
    46     if ((lua->st = luaL_newstate()) == NULL)
    46     if ((lua->st = luaL_newstate()) == NULL)
    47         JUMP_SET_ERROR(err, ERR_LUA_MEM);
    47         JUMP_SET_ERROR(err, &lua_errors, ERR_LUA_MEM);
    48     
    48     
    49     // init in protected mode
    49     // init in protected mode
    50     if (nexus_lua_error(lua->st, lua_cpcall(lua->st, &nexus_lua_init, lua), err))
    50     if (nexus_lua_error(lua->st, lua_cpcall(lua->st, &nexus_lua_init, lua), err))
    51         goto error;
    51         goto error;
    52 
    52 
    72 err_t nexus_lua_eval (struct nexus_lua *lua, const char *chunk, error_t *err)
    72 err_t nexus_lua_eval (struct nexus_lua *lua, const char *chunk, error_t *err)
    73 {
    73 {
    74     int ret;
    74     int ret;
    75     bool loaded = false;
    75     bool loaded = false;
    76 
    76 
    77     RESET_ERROR(err);
    77     error_reset(err);
    78 
    78 
    79     // load the line as a lua function
    79     // load the line as a lua function
    80     if ((ret = luaL_loadstring(lua->st, chunk)))
    80     if ((ret = luaL_loadstring(lua->st, chunk)))
    81         goto error;
    81         goto error;
    82     
    82     
   103 {
   103 {
   104     // XXX: this can raise an erorr itself
   104     // XXX: this can raise an erorr itself
   105     const char *error = lua_tostring(L, -1);
   105     const char *error = lua_tostring(L, -1);
   106 
   106 
   107     switch (ret) {
   107     switch (ret) {
   108         case 0:                 RETURN_SET_ERROR(err, SUCCESS);
   108         case 0:                 return SUCCESS;
   109         case LUA_ERRSYNTAX:     RETURN_SET_ERROR_STR(err, ERR_LUA_SYNTAX, error);
   109         case LUA_ERRSYNTAX:     return SET_ERROR_STR(err, &lua_errors, ERR_LUA_SYNTAX, error);
   110         case LUA_ERRRUN:        RETURN_SET_ERROR_STR(err, ERR_LUA_RUN, error);
   110         case LUA_ERRRUN:        return SET_ERROR_STR(err, &lua_errors, ERR_LUA_RUN, error);
   111         case LUA_ERRMEM:        RETURN_SET_ERROR_STR(err, ERR_LUA_MEM, error);
   111         case LUA_ERRMEM:        return SET_ERROR_STR(err, &lua_errors, ERR_LUA_MEM, error);
   112         case LUA_ERRERR:        RETURN_SET_ERROR_STR(err, ERR_LUA_ERR, error);
   112         case LUA_ERRERR:        return SET_ERROR_STR(err, &lua_errors, ERR_LUA_ERR, error);
   113         case LUA_ERRFILE:       RETURN_SET_ERROR_STR(err, ERR_LUA_FILE, error);
   113         case LUA_ERRFILE:       return SET_ERROR_STR(err, &lua_errors, ERR_LUA_FILE, error);
   114         default:                RETURN_SET_ERROR_EXTRA(err, ERR_UNKNOWN, ret);
   114         default:                return SET_ERROR(err, &general_errors, ERR_UNKNOWN);
   115     };
   115     };
   116 }
   116 }
   117 
   117