#include #include #include #include #include #include #include using namespace std; extern "C" { #include "lua.h" #include "lualib.h" #include "lauxlib.h" } static void output_stack_size(lua_State *L, int line){ int n = lua_gettop(L); printf("line %d: stack size is %d\n", line, n); for(int i = -1; i >= 0 - n; i--){ string type = string(lua_typename(L, lua_type(L, i))); string value = "unknown"; if(type == "string"){ value = lua_tostring(L, i); } printf("index is %d, type is %s, value is %s\n", i, type.c_str(), value.c_str()); } } static void lua_traceback(const char *func_name, lua_State *L){ int n = lua_gettop(L); printf("%s error: stack size is %d, traceback is:\n", func_name, n); for(int i = -1; i >= 0 - n; i--){ string type = string(lua_typename(L, lua_type(L, i))); if(type == "string"){ printf("%s\n", lua_tostring(L, i)); } } lua_pop(L, n); } //copy from suricata static int lua_push_string_buffer(lua_State *lua_state, const char *input, size_t input_len) { if (input_len % 4 != 0) { /* we're using a buffer sized at a multiple of 4 as lua_pushlstring generates * invalid read errors in valgrind otherwise. Adding in a nul to be sure. * Buffer size = len + 1 (for nul) + whatever makes it a multiple of 4 */ size_t buflen = input_len + 1 + ((input_len + 1) % 4); char buf[buflen]; memset(buf, 0x00, buflen); memcpy(buf, input, input_len); buf[input_len] = '\0'; /* return value through lua_state, as a luastring */ lua_pushlstring(lua_state, buf, input_len); } else { lua_pushlstring(lua_state, input, input_len); } return 1; } static void test_coroutine(int argc, char *argv[]){ int max_state_num = 10; int max_coroutine_num = 100; if(argc > 2){ max_state_num = atoi(argv[1]); max_coroutine_num = atoi(argv[2]); } const char *filename = "../../test/test_coroutine/test.lua"; vector states; for(int i = 0; i < max_state_num; i++){ lua_State *L = luaL_newstate(); luaL_openlibs(L); luaL_dofile(L, filename); states.push_back(L); } vector> coroutines; for(auto state : states){ int i = 0; for(; i < max_coroutine_num; i++){ lua_State* l = lua_newthread(state); int ret = lua_checkstack(state, 1); if(ret != 1) printf("do not have enough space, ret is %d\n", ret); coroutines.push_back({l, state}); lua_getglobal(l, "test"); //cout<<"after getglobal, size = "< regions; output_stack_size(L); lua_pushnil(L); while(lua_next(L, -2) != 0){ printf("%s - %s\n", lua_typename(L, lua_type(L, -2)), lua_typename(L, lua_type(L, -1))); regions.insert(string(lua_tostring(L, -1))); lua_pop(L, 1); } for(auto region : regions) cout<