return 'main' function to call on the lua stack
| 3022 | |
| 3023 | // return 'main' function to call on the lua stack |
| 3024 | static int get_func_by_name(Lua L, const char *func, char **err) |
| 3025 | { |
| 3026 | lua_getglobal(L, "_SP"); |
| 3027 | lua_pushstring(L, "cdb2_func_names"); |
| 3028 | lua_gettable(L, -2); // result, _SP{} |
| 3029 | if (!lua_istable(L, -1)) { |
| 3030 | logmsg(LOGMSG_ERROR, "%s fail at %d\n", __func__, __LINE__); |
| 3031 | *err = strdup("get_func_by_name failed"); |
| 3032 | return -1; |
| 3033 | } |
| 3034 | lua_pushstring(L, func); |
| 3035 | lua_gettable(L, -2); |
| 3036 | if (!lua_isfunction(L, -1)) { |
| 3037 | if (strcmp(func, "main") != 0) { |
| 3038 | logmsg(LOGMSG_ERROR, "%s fail at %d\n", __func__, __LINE__); |
| 3039 | *err = strdup("get_func_by_name failed"); |
| 3040 | return -2; |
| 3041 | } |
| 3042 | lua_pop(L, 1); // nil |
| 3043 | // local function main() -- not found |
| 3044 | // look for _SP.spname |
| 3045 | SP sp = getsp(L); |
| 3046 | lua_pushstring(L, sp->spname); |
| 3047 | lua_gettable(L, -3); // _SP.spname ? |
| 3048 | if (!lua_isfunction(L, -1)) { |
| 3049 | logmsg(LOGMSG_ERROR, "%s fail at %d\n", __func__, __LINE__); |
| 3050 | *err = strdup("get_func_by_name failed"); |
| 3051 | return -3; |
| 3052 | } |
| 3053 | } |
| 3054 | lua_insert(L, 1); |
| 3055 | lua_settop(L, 1); |
| 3056 | return 0; |
| 3057 | } |
| 3058 | |
| 3059 | static int process_src(Lua L, const char *src, char **err) |
| 3060 | { |
no test coverage detected