Hi,
I attempted at running multiple lua scripts from C. Pasted below is the code I have. The 2 lua scripts are in the strings prog1 and prog2 which are basically 2 unequal loops. I create threads and set hooks for the thread for a mask count of 4. I resume each alternately but I do not get the expected result at all. I never see an output from prog2 and prog1 i somehow becomes a decimal number. The output is as follows:
Hook Called
Flag0
Hook Called
Flag0
Hook Called
flag1
Hook Called
Flag0
x1
Hook Called
flag1
Hook Called
Flag0
x2.0000000013693
Hook Called
flag1
Hook Called
Flag0
x3.0000000027386
Hook Called
flag1
Hook Called
Flag0
x4.0000000027386
And the program is as follows:
#include <stdio.h>
#include <string.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
int flag = 0;
lua_State *luaVM1, *luaVM2, *thread1, *thread2;
lua_Hook hookFunc(lua_State *L, lua_Debug *dbg);
int main(int argc, char* argv[ ])
{
luaVM1 = lua_open();
luaVM2 = lua_open();
luaL_openlibs(luaVM1);
luaL_openlibs(luaVM2);
thread1 = lua_newthread(luaVM1);
thread2 = lua_newthread(luaVM2);
if (NULL == luaVM1 || NULL == luaVM2)
{
printf("Error Initializing lua\n");
return -1;
}
char* prog1 = "for i = 1,5 do print('x'..tostring(i)) end";
char* prog2 = "for i = 1,10 do print('y'..tostring(i)) end";
// Create threads in each lua state
// Now push the function into the stack of each thread
luaL_loadstring(thread1,prog1);
luaL_loadstring(thread2,prog2);
lua_sethook(thread1,hookFunc,LUA_MASKCOUNT,4);
lua_sethook(thread2,hookFunc,LUA_MASKCOUNT,4);
lua_resume(thread1,0);
//lua_resume(thread2,0);
lua_close( luaVM1 );
lua_close( luaVM2 );
return 0;
}
lua_Hook hookFunc(lua_State *L, lua_Debug *dbg)
{
printf("Hook Called\n");
if(flag==0)
{
printf("Flag0\n");
lua_resume(thread2,0);
flag = 1;
}
else
{
printf("flag1\n");
lua_resume(thread1,0);
flag = 0;
}
}
Any help would be appreciated.
Thanks,
Milind