|
1 #include "nexus_lua.h" |
|
2 #include "../lua_objs.h" |
|
3 #include "../lua_irc.h" |
|
4 |
|
5 #include <stdlib.h> |
|
6 |
|
7 #include <lua5.1/lualib.h> |
|
8 #include <lua5.1/lauxlib.h> |
|
9 |
|
10 /** |
|
11 * Initialize the lua state, in protected mode |
|
12 */ |
|
13 int nexus_lua_init (lua_State *L) |
|
14 { |
|
15 struct nexus_lua *lua; |
|
16 |
|
17 // read the nexus_lua off the stack |
|
18 if ((lua = lua_touserdata(L, 1)) == NULL) |
|
19 luaL_error(L, "lua_touserdata: NULL"); |
|
20 |
|
21 // open the standard libraries |
|
22 luaL_openlibs(L); |
|
23 |
|
24 // open our core lua api |
|
25 lua_objs_init(lua); |
|
26 |
|
27 // and the irc_* stuff |
|
28 lua_irc_init(lua); |
|
29 |
|
30 // ok |
|
31 return 0; |
|
32 } |
|
33 |
|
34 err_t nexus_lua_create (struct nexus_lua **lua_ptr, struct nexus *nexus, error_t *err) |
|
35 { |
|
36 struct nexus_lua *lua; |
|
37 |
|
38 // alloc |
|
39 if ((lua = calloc(1, sizeof(*lua))) == NULL) |
|
40 return SET_ERROR(err, ERR_CALLOC); |
|
41 |
|
42 // store |
|
43 lua->nexus = nexus; |
|
44 |
|
45 // create the lua state |
|
46 if ((lua->st = luaL_newstate()) == NULL) |
|
47 JUMP_SET_ERROR(err, ERR_LUA_MEM); |
|
48 |
|
49 // init in protected mode |
|
50 if (nexus_lua_error(lua->st, lua_cpcall(lua->st, &nexus_lua_init, lua), err)) |
|
51 goto error; |
|
52 |
|
53 // ok |
|
54 *lua_ptr = lua; |
|
55 |
|
56 return SUCCESS; |
|
57 |
|
58 error: |
|
59 nexus_lua_destroy(lua); |
|
60 |
|
61 return ERROR_CODE(err); |
|
62 } |
|
63 |
|
64 void nexus_lua_destroy (struct nexus_lua *lua) |
|
65 { |
|
66 // close the lua stuff |
|
67 lua_close(lua->st); |
|
68 |
|
69 free(lua); |
|
70 } |
|
71 |
|
72 err_t nexus_lua_eval (struct nexus_lua *lua, const char *chunk, error_t *err) |
|
73 { |
|
74 int ret; |
|
75 bool loaded = false; |
|
76 |
|
77 RESET_ERROR(err); |
|
78 |
|
79 // load the line as a lua function |
|
80 if ((ret = luaL_loadstring(lua->st, chunk))) |
|
81 goto error; |
|
82 |
|
83 loaded = true; |
|
84 |
|
85 // execute it |
|
86 if ((ret = lua_pcall(lua->st, 0, 0, 0))) |
|
87 goto error; |
|
88 |
|
89 error: |
|
90 if (ret) { |
|
91 // build the error_info |
|
92 nexus_lua_error(lua->st, ret, err); |
|
93 |
|
94 // pop the error message |
|
95 // XXX: err points at GC:able memory |
|
96 lua_pop(lua->st, 1); |
|
97 } |
|
98 |
|
99 return ERROR_CODE(err); |
|
100 } |
|
101 |
|
102 err_t nexus_lua_error (lua_State *L, int ret, error_t *err) |
|
103 { |
|
104 // XXX: this can raise an erorr itself |
|
105 const char *error = lua_tostring(L, -1); |
|
106 |
|
107 switch (ret) { |
|
108 case 0: RETURN_SET_ERROR(err, SUCCESS); |
|
109 case LUA_ERRSYNTAX: RETURN_SET_ERROR_STR(err, ERR_LUA_SYNTAX, error); |
|
110 case LUA_ERRRUN: RETURN_SET_ERROR_STR(err, ERR_LUA_RUN, error); |
|
111 case LUA_ERRMEM: RETURN_SET_ERROR_STR(err, ERR_LUA_MEM, error); |
|
112 case LUA_ERRERR: RETURN_SET_ERROR_STR(err, ERR_LUA_ERR, error); |
|
113 case LUA_ERRFILE: RETURN_SET_ERROR_STR(err, ERR_LUA_FILE, error); |
|
114 default: RETURN_SET_ERROR_EXTRA(err, ERR_UNKNOWN, ret); |
|
115 }; |
|
116 } |
|
117 |