static public void reg(IntPtr l, MonoBehaviour m) { mb = m; reg(l, Yieldk, "UnityEngine"); string yield = @" local Yield = UnityEngine.Yieldk UnityEngine.Yield = function(x) local co,ismain=coroutine.running() if ismain then error('Can not yield in main thread') end Yield(x,function() coroutine.resume(co) end) coroutine.yield() end return yield "; // overload resume function for report error if (LuaDLL.lua_dostring(l, yield) != 0) { LuaObject.lastError(l); } LuaDLL.lua_pop(l, 1); }
static int init(IntPtr L) { LuaDLL.luaL_openlibs(L); LuaDLL.lua_pushlightuserdata(L, L); LuaDLL.lua_setglobal(L, "__main_state"); LuaDLL.lua_pushcfunction(L, print); LuaDLL.lua_setglobal(L, "print"); LuaDLL.lua_pushcfunction(L, pcall); LuaDLL.lua_setglobal(L, "pcall"); LuaDLL.lua_pushcfunction(L, import); LuaDLL.lua_setglobal(L, "import"); string resumefunc = @" local resume = coroutine.resume local unpack = unpack or table.unpack coroutine.resume=function(co,...) local ret={resume(co,...)} if not ret[1] then UnityEngine.Debug.LogError(debug.traceback(co,ret[2])) end return unpack(ret) end "; // overload resume function for report error if (LuaDLL.lua_dostring(L, resumefunc) != 0) { LuaObject.lastError(L); } LuaDLL.lua_pushcfunction(L, dofile); LuaDLL.lua_setglobal(L, "dofile"); LuaDLL.lua_pushcfunction(L, loadfile); LuaDLL.lua_setglobal(L, "loadfile"); LuaDLL.lua_pushcfunction(L, loader); int loaderFunc = LuaDLL.lua_gettop(L); LuaDLL.lua_getglobal(L, "package"); #if LUA_5_3 LuaDLL.lua_getfield(L, -1, "searchers"); #else LuaDLL.lua_getfield(L, -1, "loaders"); #endif int loaderTable = LuaDLL.lua_gettop(L); // Shift table elements right for (int e = LuaDLL.lua_rawlen(L, loaderTable) + 1; e > 1; e--) { LuaDLL.lua_rawgeti(L, loaderTable, e - 1); LuaDLL.lua_rawseti(L, loaderTable, e); } LuaDLL.lua_pushvalue(L, loaderFunc); LuaDLL.lua_rawseti(L, loaderTable, 1); LuaDLL.lua_settop(L, 0); return(0); }