Original Post
Gday! I am trying to load a Lua file and have Lua store it in the registry instead of the stack (or at least that is my understanding of the matter), as discussed here. Here is my source: It reaches the first lua_call() call, then panics. What am I doing wrong? Oh, the lua source co.lua is:
int main()
{
lua_State* lua;
lua = luaL_newstate();
luaL_openlibs(lua);
luaL_loadfile(lua, "co.lua");
int ref = luaL_ref(lua, LUA_REGISTRYINDEX);
lua_rawgeti(lua, LUA_REGISTRYINDEX, ref);
lua_call(lua, 0, 0);
lua_getglobal(lua, "Init");
lua_call(lua, 0, 0);
do {
lua_rawgeti(lua, LUA_REGISTRYINDEX, ref);
lua_getglobal(lua, "Update");
lua_call(lua, 0, 1);
} while ((int)lua_tonumber(lua, -1) == 0);
luaL_unref(lua, LUA_REGISTRYINDEX, ref);
lua_close(lua);
return 0;
}
function Init()
print "Init"
co = coroutine.create(Move)
end
function Move()
print "first"
coroutine.yield()
print "second"
coroutine.yield()
print "third"
end
function Update()
coroutine.resume(co)
if coroutine.running(co)
return 0
else
return 1
end
end