author | Tero Marttila <terom@fixme.fi> |
Thu, 28 May 2009 01:17:36 +0300 | |
branch | new-lib-errors |
changeset 219 | cefec18b8268 |
parent 218 | 5229a5d098b2 |
permissions | -rw-r--r-- |
105
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
1 |
#include "nexus_lua.h" |
219
cefec18b8268
some of the lib/transport stuff compiles
Tero Marttila <terom@fixme.fi>
parents:
218
diff
changeset
|
2 |
#include "lua_objs.h" |
218
5229a5d098b2
some of spbot and lib compiles
Tero Marttila <terom@fixme.fi>
parents:
217
diff
changeset
|
3 |
#include "../lua_irc.h" |
105
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
4 |
|
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
5 |
#include <stdlib.h> |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
6 |
|
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
7 |
#include <lua5.1/lualib.h> |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
8 |
#include <lua5.1/lauxlib.h> |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
9 |
|
116
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
10 |
/** |
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
11 |
* Initialize the lua state, in protected mode |
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
12 |
*/ |
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
13 |
int nexus_lua_init (lua_State *L) |
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
14 |
{ |
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
15 |
struct nexus_lua *lua; |
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
16 |
|
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
17 |
// read the nexus_lua off the stack |
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
18 |
if ((lua = lua_touserdata(L, 1)) == NULL) |
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
19 |
luaL_error(L, "lua_touserdata: NULL"); |
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
20 |
|
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
21 |
// open the standard libraries |
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
22 |
luaL_openlibs(L); |
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
23 |
|
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
24 |
// open our core lua api |
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
25 |
lua_objs_init(lua); |
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
26 |
|
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
27 |
// and the irc_* stuff |
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
28 |
lua_irc_init(lua); |
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
29 |
|
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
30 |
// ok |
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
31 |
return 0; |
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
32 |
} |
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
33 |
|
217 | 34 |
err_t nexus_lua_create (struct nexus_lua **lua_ptr, struct nexus *nexus, error_t *err) |
105
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
35 |
{ |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
36 |
struct nexus_lua *lua; |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
37 |
|
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
38 |
// alloc |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
39 |
if ((lua = calloc(1, sizeof(*lua))) == NULL) |
219
cefec18b8268
some of the lib/transport stuff compiles
Tero Marttila <terom@fixme.fi>
parents:
218
diff
changeset
|
40 |
return SET_ERROR_MEM(err); |
105
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
41 |
|
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
42 |
// store |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
43 |
lua->nexus = nexus; |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
44 |
|
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
45 |
// create the lua state |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
46 |
if ((lua->st = luaL_newstate()) == NULL) |
219
cefec18b8268
some of the lib/transport stuff compiles
Tero Marttila <terom@fixme.fi>
parents:
218
diff
changeset
|
47 |
JUMP_SET_ERROR(err, &lua_errors, ERR_LUA_MEM); |
105
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
48 |
|
116
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
49 |
// init in protected mode |
92e71129074d
split off lua_irc from lua_objs
Tero Marttila <terom@fixme.fi>
parents:
106
diff
changeset
|
50 |
if (nexus_lua_error(lua->st, lua_cpcall(lua->st, &nexus_lua_init, lua), err)) |
105
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
51 |
goto error; |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
52 |
|
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
53 |
// ok |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
54 |
*lua_ptr = lua; |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
55 |
|
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
56 |
return SUCCESS; |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
57 |
|
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
58 |
error: |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
59 |
nexus_lua_destroy(lua); |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
60 |
|
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
61 |
return ERROR_CODE(err); |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
62 |
} |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
63 |
|
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
64 |
void nexus_lua_destroy (struct nexus_lua *lua) |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
65 |
{ |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
66 |
// close the lua stuff |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
67 |
lua_close(lua->st); |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
68 |
|
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
69 |
free(lua); |
b6b183fbf373
implement a separate nexus_lua module
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
70 |
} |
106
f00661136ac2
add nexus_lua_error for unified LUA_ERR* -> ERR_LUA_* mapping, and lua configuration support
Tero Marttila <terom@fixme.fi>
parents:
105
diff
changeset
|
71 |
|
217 | 72 |
err_t nexus_lua_eval (struct nexus_lua *lua, const char *chunk, error_t *err) |
136 | 73 |
{ |
74 |
int ret; |
|
144
51f96539f9f3
fix nexus_lua_eval error handling
Tero Marttila <terom@fixme.fi>
parents:
136
diff
changeset
|
75 |
bool loaded = false; |
51f96539f9f3
fix nexus_lua_eval error handling
Tero Marttila <terom@fixme.fi>
parents:
136
diff
changeset
|
76 |
|
219
cefec18b8268
some of the lib/transport stuff compiles
Tero Marttila <terom@fixme.fi>
parents:
218
diff
changeset
|
77 |
error_reset(err); |
136 | 78 |
|
79 |
// load the line as a lua function |
|
80 |
if ((ret = luaL_loadstring(lua->st, chunk))) |
|
81 |
goto error; |
|
144
51f96539f9f3
fix nexus_lua_eval error handling
Tero Marttila <terom@fixme.fi>
parents:
136
diff
changeset
|
82 |
|
51f96539f9f3
fix nexus_lua_eval error handling
Tero Marttila <terom@fixme.fi>
parents:
136
diff
changeset
|
83 |
loaded = true; |
136 | 84 |
|
85 |
// execute it |
|
144
51f96539f9f3
fix nexus_lua_eval error handling
Tero Marttila <terom@fixme.fi>
parents:
136
diff
changeset
|
86 |
if ((ret = lua_pcall(lua->st, 0, 0, 0))) |
51f96539f9f3
fix nexus_lua_eval error handling
Tero Marttila <terom@fixme.fi>
parents:
136
diff
changeset
|
87 |
goto error; |
51f96539f9f3
fix nexus_lua_eval error handling
Tero Marttila <terom@fixme.fi>
parents:
136
diff
changeset
|
88 |
|
51f96539f9f3
fix nexus_lua_eval error handling
Tero Marttila <terom@fixme.fi>
parents:
136
diff
changeset
|
89 |
error: |
51f96539f9f3
fix nexus_lua_eval error handling
Tero Marttila <terom@fixme.fi>
parents:
136
diff
changeset
|
90 |
if (ret) { |
51f96539f9f3
fix nexus_lua_eval error handling
Tero Marttila <terom@fixme.fi>
parents:
136
diff
changeset
|
91 |
// build the error_info |
51f96539f9f3
fix nexus_lua_eval error handling
Tero Marttila <terom@fixme.fi>
parents:
136
diff
changeset
|
92 |
nexus_lua_error(lua->st, ret, err); |
51f96539f9f3
fix nexus_lua_eval error handling
Tero Marttila <terom@fixme.fi>
parents:
136
diff
changeset
|
93 |
|
202
210c43e6c088
slight tweaks to nexus_lua, use error_t and no need to pop the loaded chunk ourselv - lua_pcall will always do that
Tero Marttila <terom@fixme.fi>
parents:
144
diff
changeset
|
94 |
// pop the error message |
144
51f96539f9f3
fix nexus_lua_eval error handling
Tero Marttila <terom@fixme.fi>
parents:
136
diff
changeset
|
95 |
// XXX: err points at GC:able memory |
136 | 96 |
lua_pop(lua->st, 1); |
97 |
} |
|
98 |
||
99 |
return ERROR_CODE(err); |
|
100 |
} |
|
101 |
||
217 | 102 |
err_t nexus_lua_error (lua_State *L, int ret, error_t *err) |
106
f00661136ac2
add nexus_lua_error for unified LUA_ERR* -> ERR_LUA_* mapping, and lua configuration support
Tero Marttila <terom@fixme.fi>
parents:
105
diff
changeset
|
103 |
{ |
207
3fa22abb5421
fix lua_nexus_sleep to use EV_TIMEOUT + misc
Tero Marttila <terom@fixme.fi>
parents:
202
diff
changeset
|
104 |
// XXX: this can raise an erorr itself |
106
f00661136ac2
add nexus_lua_error for unified LUA_ERR* -> ERR_LUA_* mapping, and lua configuration support
Tero Marttila <terom@fixme.fi>
parents:
105
diff
changeset
|
105 |
const char *error = lua_tostring(L, -1); |
f00661136ac2
add nexus_lua_error for unified LUA_ERR* -> ERR_LUA_* mapping, and lua configuration support
Tero Marttila <terom@fixme.fi>
parents:
105
diff
changeset
|
106 |
|
f00661136ac2
add nexus_lua_error for unified LUA_ERR* -> ERR_LUA_* mapping, and lua configuration support
Tero Marttila <terom@fixme.fi>
parents:
105
diff
changeset
|
107 |
switch (ret) { |
219
cefec18b8268
some of the lib/transport stuff compiles
Tero Marttila <terom@fixme.fi>
parents:
218
diff
changeset
|
108 |
case 0: return SUCCESS; |
cefec18b8268
some of the lib/transport stuff compiles
Tero Marttila <terom@fixme.fi>
parents:
218
diff
changeset
|
109 |
case LUA_ERRSYNTAX: return SET_ERROR_STR(err, &lua_errors, ERR_LUA_SYNTAX, error); |
cefec18b8268
some of the lib/transport stuff compiles
Tero Marttila <terom@fixme.fi>
parents:
218
diff
changeset
|
110 |
case LUA_ERRRUN: return SET_ERROR_STR(err, &lua_errors, ERR_LUA_RUN, error); |
cefec18b8268
some of the lib/transport stuff compiles
Tero Marttila <terom@fixme.fi>
parents:
218
diff
changeset
|
111 |
case LUA_ERRMEM: return SET_ERROR_STR(err, &lua_errors, ERR_LUA_MEM, error); |
cefec18b8268
some of the lib/transport stuff compiles
Tero Marttila <terom@fixme.fi>
parents:
218
diff
changeset
|
112 |
case LUA_ERRERR: return SET_ERROR_STR(err, &lua_errors, ERR_LUA_ERR, error); |
cefec18b8268
some of the lib/transport stuff compiles
Tero Marttila <terom@fixme.fi>
parents:
218
diff
changeset
|
113 |
case LUA_ERRFILE: return SET_ERROR_STR(err, &lua_errors, ERR_LUA_FILE, error); |
cefec18b8268
some of the lib/transport stuff compiles
Tero Marttila <terom@fixme.fi>
parents:
218
diff
changeset
|
114 |
default: return SET_ERROR(err, &general_errors, ERR_UNKNOWN); |
106
f00661136ac2
add nexus_lua_error for unified LUA_ERR* -> ERR_LUA_* mapping, and lua configuration support
Tero Marttila <terom@fixme.fi>
parents:
105
diff
changeset
|
115 |
}; |
f00661136ac2
add nexus_lua_error for unified LUA_ERR* -> ERR_LUA_* mapping, and lua configuration support
Tero Marttila <terom@fixme.fi>
parents:
105
diff
changeset
|
116 |
} |
136 | 117 |