Advertisement
Guest User

Untitled

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