| 438 | } |
| 439 | |
| 440 | int |
| 441 | ts_lua_reload_module(ts_lua_instance_conf *conf, ts_lua_main_ctx *arr, int n) |
| 442 | { |
| 443 | int i; |
| 444 | lua_State *L; |
| 445 | |
| 446 | for (i = 0; i < n; i++) { |
| 447 | TSMutexLock(arr[i].mutexp); |
| 448 | |
| 449 | L = arr[i].lua; |
| 450 | |
| 451 | /* call "__reload__", to clean resources if necessary */ |
| 452 | lua_pushlightuserdata(L, conf); |
| 453 | lua_rawget(L, LUA_REGISTRYINDEX); |
| 454 | lua_replace(L, LUA_GLOBALSINDEX); /* L[GLOBAL] = L[REG][conf] */ |
| 455 | |
| 456 | lua_getglobal(L, "__reload__"); /* get __clean__ function */ |
| 457 | |
| 458 | if (lua_type(L, -1) == LUA_TFUNCTION) { |
| 459 | if (lua_pcall(L, 0, 0, 0)) { |
| 460 | TSError("[ts_lua][%s] lua_pcall %s failed: %s", __FUNCTION__, conf->script, lua_tostring(L, -1)); |
| 461 | } |
| 462 | } else { |
| 463 | lua_pop(L, 1); /* pop nil */ |
| 464 | } |
| 465 | |
| 466 | // new global context |
| 467 | lua_newtable(L); /* new TB1 */ |
| 468 | lua_pushvalue(L, -1); /* new TB2 */ |
| 469 | lua_setfield(L, -2, "_G"); /* TB1[_G] = TB2 empty table, we can change _G to xx */ |
| 470 | lua_newtable(L); /* new TB3 */ |
| 471 | lua_rawgeti(L, LUA_REGISTRYINDEX, arr[i].gref); /* push L[GLOBAL] */ |
| 472 | lua_setfield(L, -2, "__index"); /* TB3[__index] = L[GLOBAL] which has ts.xxx api */ |
| 473 | lua_setmetatable(L, -2); /* TB1[META] = TB3 */ |
| 474 | lua_replace(L, LUA_GLOBALSINDEX); /* L[GLOBAL] = TB1 */ |
| 475 | |
| 476 | ts_lua_set_instance_conf(L, conf); |
| 477 | |
| 478 | if (strlen(conf->script)) { |
| 479 | if (luaL_loadfile(L, conf->script)) { |
| 480 | TSError("[ts_lua][%s] luaL_loadfile %s failed: %s", __FUNCTION__, conf->script, lua_tostring(L, -1)); |
| 481 | } else { |
| 482 | if (lua_pcall(L, 0, 0, 0)) { |
| 483 | TSError("[ts_lua][%s] lua_pcall %s failed: %s", __FUNCTION__, conf->script, lua_tostring(L, -1)); |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | lua_pushlightuserdata(L, conf); |
| 489 | lua_pushvalue(L, LUA_GLOBALSINDEX); |
| 490 | lua_rawset(L, LUA_REGISTRYINDEX); /* L[REG][conf] = L[GLOBAL] */ |
| 491 | |
| 492 | lua_newtable(L); |
| 493 | lua_replace(L, LUA_GLOBALSINDEX); /* L[GLOBAL] = EMPTY */ |
| 494 | |
| 495 | if (conf->ljgc > 0) { |
| 496 | Dbg(dbg_ctl, "ljgc = %d, running LuaJIT Garbage Collector...", conf->ljgc); |
| 497 | lua_gc(L, LUA_GCCOLLECT, 0); |
no test coverage detected