Advertisement
Guest User

Untitled

a guest
Oct 29th, 2020
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1. #include <lua.h>
  2. #include <lualib.h>
  3. #include <lauxlib.h>
  4.  
  5. static int counter = 0;
  6.  
  7. #if LUA_VERSION_NUM <= 503
  8. #define lua_resumec(state, from, nargs, res) lua_resume(state, from, nargs)
  9. #else
  10. #define lua_resumec(state, from, nargs, res) lua_resume(state, from, nargs, res)
  11. #endif
  12.  
  13. static int test(lua_State* L) {
  14.     if(counter < 10) {
  15.         counter++;
  16.         lua_getglobal(L, "TestFunction");
  17.         lua_pcall(L, 0, 1, 0);
  18.     } else
  19.         lua_pushstring(L, "TEST");
  20.     return 1;
  21. }
  22.  
  23. int main() {
  24.     lua_State* lua_state = luaL_newstate();
  25.     luaL_openlibs(lua_state);
  26.     lua_register(lua_state, "TestFunction", test);
  27.     const char lua_script[] = "print(TestFunction())";
  28.     luaL_loadbuffer(lua_state, lua_script, sizeof(lua_script) - 1, lua_script);
  29.     lua_pcall(lua_state, 0, 0, 0);
  30.     counter = 0;
  31.     lua_State* rthread = lua_newthread(lua_state);
  32.     luaL_loadbuffer(rthread, lua_script, sizeof(lua_script) - 1, lua_script);
  33.     int result = lua_resumec(rthread, 0, 0, &result);
  34.     lua_close(lua_state);
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement