Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 1 | #include <lauxlib.h> |
| 2 | #include <lua.h> |
| 3 | #include <lualib.h> |
| 4 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 5 | #include <ebpttree.h> |
| 6 | |
| 7 | #include <common/cfgparse.h> |
| 8 | |
| 9 | #include <types/hlua.h> |
| 10 | #include <types/proxy.h> |
| 11 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame^] | 12 | #include <proto/task.h> |
| 13 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 14 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 15 | * macro is used only for identifying the function that can |
| 16 | * not return because a longjmp is executed. |
| 17 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 18 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 19 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 20 | */ |
| 21 | #define __LJMP |
| 22 | #define WILL_LJMP(func) func |
| 23 | #define MAY_LJMP(func) func |
| 24 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 25 | /* The main Lua execution context. */ |
| 26 | struct hlua gL; |
| 27 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame^] | 28 | /* This is the memory pool containing all the signal structs. These |
| 29 | * struct are used to store each requiered signal between two tasks. |
| 30 | */ |
| 31 | struct pool_head *pool2_hlua_com; |
| 32 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 33 | /* Store the fast lua context for coroutines. This tree uses the |
| 34 | * Lua stack pointer value as indexed entry, and store the associated |
| 35 | * hlua context. |
| 36 | */ |
| 37 | struct eb_root hlua_ctx = EB_ROOT_UNIQUE; |
| 38 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 39 | /* Used to check an Lua function type in the stack. It creates and |
| 40 | * returns a reference of the function. This function throws an |
| 41 | * error if the rgument is not a "function". |
| 42 | */ |
| 43 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 44 | { |
| 45 | if (!lua_isfunction(L, argno)) { |
| 46 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, -1)); |
| 47 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 48 | } |
| 49 | lua_pushvalue(L, argno); |
| 50 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 51 | } |
| 52 | |
| 53 | /* The three following functions are useful for adding entries |
| 54 | * in a table. These functions takes a string and respectively an |
| 55 | * integer, a string or a function and add it to the table in the |
| 56 | * top of the stack. |
| 57 | * |
| 58 | * These functions throws an error if no more stack size is |
| 59 | * available. |
| 60 | */ |
| 61 | __LJMP static inline void hlua_class_const_int(lua_State *L, const char *name, |
| 62 | unsigned int value) |
| 63 | { |
| 64 | if (!lua_checkstack(L, 2)) |
| 65 | WILL_LJMP(luaL_error(L, "full stack")); |
| 66 | lua_pushstring(L, name); |
| 67 | lua_pushunsigned(L, value); |
| 68 | lua_settable(L, -3); |
| 69 | } |
| 70 | __LJMP static inline void hlua_class_const_str(lua_State *L, const char *name, |
| 71 | const char *value) |
| 72 | { |
| 73 | if (!lua_checkstack(L, 2)) |
| 74 | WILL_LJMP(luaL_error(L, "full stack")); |
| 75 | lua_pushstring(L, name); |
| 76 | lua_pushstring(L, value); |
| 77 | lua_settable(L, -3); |
| 78 | } |
| 79 | __LJMP static inline void hlua_class_function(lua_State *L, const char *name, |
| 80 | int (*function)(lua_State *L)) |
| 81 | { |
| 82 | if (!lua_checkstack(L, 2)) |
| 83 | WILL_LJMP(luaL_error(L, "full stack")); |
| 84 | lua_pushstring(L, name); |
| 85 | lua_pushcclosure(L, function, 0); |
| 86 | lua_settable(L, -3); |
| 87 | } |
| 88 | |
| 89 | /* This function check the number of arguments available in the |
| 90 | * stack. If the number of arguments available is not the same |
| 91 | * then <nb> an error is throwed. |
| 92 | */ |
| 93 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 94 | { |
| 95 | if (lua_gettop(L) == nb) |
| 96 | return; |
| 97 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 98 | } |
| 99 | |
| 100 | /* Return true if the data in stack[<ud>] is an object of |
| 101 | * type <class_ref>. |
| 102 | */ |
| 103 | static int hlua_udataistype(lua_State *L, int ud, int class_ref) |
| 104 | { |
| 105 | void *p = lua_touserdata(L, ud); |
| 106 | if (!p) |
| 107 | return 0; |
| 108 | |
| 109 | if (!lua_getmetatable(L, ud)) |
| 110 | return 0; |
| 111 | |
| 112 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref); |
| 113 | if (!lua_rawequal(L, -1, -2)) { |
| 114 | lua_pop(L, 2); |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | lua_pop(L, 2); |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | /* Return an object of the expected type, or throws an error. */ |
| 123 | __LJMP static void *hlua_checkudata(lua_State *L, int ud, int class_ref) |
| 124 | { |
| 125 | if (!hlua_udataistype(L, ud, class_ref)) |
| 126 | WILL_LJMP(luaL_argerror(L, 1, NULL)); |
| 127 | return lua_touserdata(L, ud); |
| 128 | } |
| 129 | |
| 130 | /* This fucntion push an error string prefixed by the file name |
| 131 | * and the line number where the error is encountered. |
| 132 | */ |
| 133 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 134 | { |
| 135 | va_list argp; |
| 136 | va_start(argp, fmt); |
| 137 | luaL_where(L, 1); |
| 138 | lua_pushvfstring(L, fmt, argp); |
| 139 | va_end(argp); |
| 140 | lua_concat(L, 2); |
| 141 | return 1; |
| 142 | } |
| 143 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame^] | 144 | /* This function register a new signal. "lua" is the current lua |
| 145 | * execution context. It contains a pointer to the associated task. |
| 146 | * "link" is a list head attached to an other task that must be wake |
| 147 | * the lua task if an event occurs. This is useful with external |
| 148 | * events like TCP I/O or sleep functions. This funcion allocate |
| 149 | * memory for the signal. |
| 150 | */ |
| 151 | static int hlua_com_new(struct hlua *lua, struct list *link) |
| 152 | { |
| 153 | struct hlua_com *com = pool_alloc2(pool2_hlua_com); |
| 154 | if (!com) |
| 155 | return 0; |
| 156 | LIST_ADDQ(&lua->com, &com->purge_me); |
| 157 | LIST_ADDQ(link, &com->wake_me); |
| 158 | com->task = lua->task; |
| 159 | return 1; |
| 160 | } |
| 161 | |
| 162 | /* This function purge all the pending signals when the LUA execution |
| 163 | * is finished. This prevent than a coprocess try to wake a deleted |
| 164 | * task. This function remove the memory associated to the signal. |
| 165 | */ |
| 166 | static void hlua_com_purge(struct hlua *lua) |
| 167 | { |
| 168 | struct hlua_com *com, *back; |
| 169 | |
| 170 | /* Delete all pending communication signals. */ |
| 171 | list_for_each_entry_safe(com, back, &lua->com, purge_me) { |
| 172 | LIST_DEL(&com->purge_me); |
| 173 | LIST_DEL(&com->wake_me); |
| 174 | pool_free2(pool2_hlua_com, com); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /* This function sends signals. It wakes all the tasks attached |
| 179 | * to a list head, and remove the signal, and free the used |
| 180 | * memory. |
| 181 | */ |
| 182 | static void hlua_com_wake(struct list *wake) |
| 183 | { |
| 184 | struct hlua_com *com, *back; |
| 185 | |
| 186 | /* Wake task and delete all pending communication signals. */ |
| 187 | list_for_each_entry_safe(com, back, wake, wake_me) { |
| 188 | LIST_DEL(&com->purge_me); |
| 189 | LIST_DEL(&com->wake_me); |
| 190 | task_wakeup(com->task, TASK_WOKEN_MSG); |
| 191 | pool_free2(pool2_hlua_com, com); |
| 192 | } |
| 193 | } |
| 194 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 195 | /* |
| 196 | * The following functions are used to make correspondance between the the |
| 197 | * executed lua pointer and the "struct hlua *" that contain the context. |
| 198 | * They run with the tree head "hlua_ctx", they just perform lookup in the |
| 199 | * tree. |
| 200 | * |
| 201 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
| 202 | * - hlua_delhlua : remove the association between hlua context and lua_state. |
| 203 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 204 | */ |
| 205 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 206 | { |
| 207 | struct ebpt_node *node; |
| 208 | |
| 209 | node = ebpt_lookup(&hlua_ctx, L); |
| 210 | if (!node) |
| 211 | return NULL; |
| 212 | return ebpt_entry(node, struct hlua, node); |
| 213 | } |
| 214 | static inline void hlua_delhlua(struct hlua *hlua) |
| 215 | { |
| 216 | if (hlua->node.key) |
| 217 | ebpt_delete(&hlua->node); |
| 218 | } |
| 219 | static inline void hlua_sethlua(struct hlua *hlua) |
| 220 | { |
| 221 | hlua->node.key = hlua->T; |
| 222 | ebpt_insert(&hlua_ctx, &hlua->node); |
| 223 | } |
| 224 | |
| 225 | /* This function initialises the Lua environment stored in the session. |
| 226 | * It must be called at the start of the session. This function creates |
| 227 | * an LUA coroutine. It can not be use to crete the main LUA context. |
| 228 | */ |
| 229 | int hlua_ctx_init(struct hlua *lua, struct task *task) |
| 230 | { |
| 231 | lua->Mref = LUA_REFNIL; |
| 232 | lua->state = HLUA_STOP; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame^] | 233 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 234 | lua->T = lua_newthread(gL.T); |
| 235 | if (!lua->T) { |
| 236 | lua->Tref = LUA_REFNIL; |
| 237 | return 0; |
| 238 | } |
| 239 | hlua_sethlua(lua); |
| 240 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 241 | lua->task = task; |
| 242 | return 1; |
| 243 | } |
| 244 | |
| 245 | /* Used to destroy the Lua coroutine when the attached session or task |
| 246 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 247 | * is not freed. |
| 248 | */ |
| 249 | void hlua_ctx_destroy(struct hlua *lua) |
| 250 | { |
| 251 | /* Remove context. */ |
| 252 | hlua_delhlua(lua); |
| 253 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame^] | 254 | /* Purge all the pending signals. */ |
| 255 | hlua_com_purge(lua); |
| 256 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 257 | /* The thread is garbage collected by Lua. */ |
| 258 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 259 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 260 | } |
| 261 | |
| 262 | /* This function is used to restore the Lua context when a coroutine |
| 263 | * fails. This function copy the common memory between old coroutine |
| 264 | * and the new coroutine. The old coroutine is destroyed, and its |
| 265 | * replaced by the new coroutine. |
| 266 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 267 | * as string error message and it is copied in the new stack. |
| 268 | */ |
| 269 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 270 | { |
| 271 | lua_State *T; |
| 272 | int new_ref; |
| 273 | |
| 274 | /* Renew the main LUA stack doesn't have sense. */ |
| 275 | if (lua == &gL) |
| 276 | return 0; |
| 277 | |
| 278 | /* Remove context. */ |
| 279 | hlua_delhlua(lua); |
| 280 | |
| 281 | /* New Lua coroutine. */ |
| 282 | T = lua_newthread(gL.T); |
| 283 | if (!T) |
| 284 | return 0; |
| 285 | |
| 286 | /* Copy last error message. */ |
| 287 | if (keep_msg) |
| 288 | lua_xmove(lua->T, T, 1); |
| 289 | |
| 290 | /* Copy data between the coroutines. */ |
| 291 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 292 | lua_xmove(lua->T, T, 1); |
| 293 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 294 | |
| 295 | /* Destroy old data. */ |
| 296 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 297 | |
| 298 | /* The thread is garbage collected by Lua. */ |
| 299 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 300 | |
| 301 | /* Fill the struct with the new coroutine values. */ |
| 302 | lua->Mref = new_ref; |
| 303 | lua->T = T; |
| 304 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 305 | |
| 306 | /* Set context. */ |
| 307 | hlua_sethlua(lua); |
| 308 | |
| 309 | return 1; |
| 310 | } |
| 311 | |
| 312 | /* This function start or resumes the Lua stack execution. If the flag |
| 313 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 314 | * The function return an error. |
| 315 | * |
| 316 | * The function can returns 4 values: |
| 317 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 318 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 319 | * task wakeup. |
| 320 | * - HLUA_E_ERRMSG : An error has occured, an error message is set in |
| 321 | * the top of the stack. |
| 322 | * - HLUA_E_ERR : An error has occured without error message. |
| 323 | * |
| 324 | * If an error occured, the stack is renewed and it is ready to run new |
| 325 | * LUA code. |
| 326 | */ |
| 327 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 328 | { |
| 329 | int ret; |
| 330 | const char *msg; |
| 331 | |
| 332 | lua->state = HLUA_RUN; |
| 333 | |
| 334 | /* Call the function. */ |
| 335 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 336 | switch (ret) { |
| 337 | |
| 338 | case LUA_OK: |
| 339 | ret = HLUA_E_OK; |
| 340 | break; |
| 341 | |
| 342 | case LUA_YIELD: |
| 343 | if (!yield_allowed) { |
| 344 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 345 | if (!lua_checkstack(lua->T, 1)) { |
| 346 | ret = HLUA_E_ERR; |
| 347 | break; |
| 348 | } |
| 349 | lua_pushfstring(lua->T, "yield not allowed"); |
| 350 | ret = HLUA_E_ERRMSG; |
| 351 | break; |
| 352 | } |
| 353 | ret = HLUA_E_AGAIN; |
| 354 | break; |
| 355 | |
| 356 | case LUA_ERRRUN: |
| 357 | if (!lua_checkstack(lua->T, 1)) { |
| 358 | ret = HLUA_E_ERR; |
| 359 | break; |
| 360 | } |
| 361 | msg = lua_tostring(lua->T, -1); |
| 362 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 363 | lua_pop(lua->T, 1); |
| 364 | if (msg) |
| 365 | lua_pushfstring(lua->T, "runtime error: %s", msg); |
| 366 | else |
| 367 | lua_pushfstring(lua->T, "unknown runtime error"); |
| 368 | ret = HLUA_E_ERRMSG; |
| 369 | break; |
| 370 | |
| 371 | case LUA_ERRMEM: |
| 372 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 373 | if (!lua_checkstack(lua->T, 1)) { |
| 374 | ret = HLUA_E_ERR; |
| 375 | break; |
| 376 | } |
| 377 | lua_pushfstring(lua->T, "out of memory error"); |
| 378 | ret = HLUA_E_ERRMSG; |
| 379 | break; |
| 380 | |
| 381 | case LUA_ERRERR: |
| 382 | if (!lua_checkstack(lua->T, 1)) { |
| 383 | ret = HLUA_E_ERR; |
| 384 | break; |
| 385 | } |
| 386 | msg = lua_tostring(lua->T, -1); |
| 387 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 388 | lua_pop(lua->T, 1); |
| 389 | if (msg) |
| 390 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 391 | else |
| 392 | lua_pushfstring(lua->T, "message handler error"); |
| 393 | ret = HLUA_E_ERRMSG; |
| 394 | break; |
| 395 | |
| 396 | default: |
| 397 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 398 | if (!lua_checkstack(lua->T, 1)) { |
| 399 | ret = HLUA_E_ERR; |
| 400 | break; |
| 401 | } |
| 402 | lua_pushfstring(lua->T, "unknonwn error"); |
| 403 | ret = HLUA_E_ERRMSG; |
| 404 | break; |
| 405 | } |
| 406 | |
| 407 | switch (ret) { |
| 408 | case HLUA_E_AGAIN: |
| 409 | break; |
| 410 | |
| 411 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame^] | 412 | hlua_com_purge(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 413 | hlua_ctx_renew(lua, 1); |
| 414 | lua->state = HLUA_STOP; |
| 415 | break; |
| 416 | |
| 417 | case HLUA_E_ERR: |
| 418 | lua->state = HLUA_STOP; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame^] | 419 | hlua_com_purge(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 420 | hlua_ctx_renew(lua, 0); |
| 421 | break; |
| 422 | |
| 423 | case HLUA_E_OK: |
| 424 | lua->state = HLUA_STOP; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame^] | 425 | hlua_com_purge(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 426 | break; |
| 427 | } |
| 428 | |
| 429 | return ret; |
| 430 | } |
| 431 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 432 | void hlua_init(void) |
| 433 | { |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame^] | 434 | /* Initialise com signals pool session. */ |
| 435 | pool2_hlua_com = create_pool("hlua_com", sizeof(struct hlua_com), MEM_F_SHARED); |
| 436 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 437 | /* Init main lua stack. */ |
| 438 | gL.Mref = LUA_REFNIL; |
| 439 | gL.state = HLUA_STOP; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame^] | 440 | LIST_INIT(&gL.com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 441 | gL.T = luaL_newstate(); |
| 442 | hlua_sethlua(&gL); |
| 443 | gL.Tref = LUA_REFNIL; |
| 444 | gL.task = NULL; |
| 445 | |
| 446 | /* Initialise lua. */ |
| 447 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 448 | } |