public LuaState() { mainThread = System.Threading.Thread.CurrentThread.ManagedThreadId; L = LuaDLL.luaL_newstate(); statemap[L] = this; if (main == null) { main = this; } refQueue = new Queue <UnrefPair>(); LuaDLL.luaL_openlibs(L); ObjectCache.make(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"); 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); }
public LuaState() { mainThread = System.Threading.Thread.CurrentThread.ManagedThreadId; // 2016-7-19 : use memory pool allocator // LuaDLL.lua_init_mpool_alloc(4, 11); // L = LuaDLL.lua_newstate_with_mpool(); L = LuaDLL.luaL_newstate(); statemap[L] = this; if (main == null) { main = this; } #if UNITY_ANDROID // fix android performance drop with JIT on according to luajit mailist post // LuaState.get(L).doString( // @"if jit then // print(jit.status()) // require('jit.opt').start('sizemcode=256','maxmcode=256') // for i=1,1000 do end // print(jit.status()) // end", "WarmUpJit"); #endif // UNITY_ANDROID refQueue = new Queue <UnrefPair>(); ObjectCache.make(L); LuaDLL.lua_atpanic(L, panicCallback); LuaDLL.luaL_openlibs(L); string PCallCSFunction = @" local assert = assert local function check(ok,...) assert(ok, ...) return ... end return function(cs_func) return function(...) return check(cs_func(...)) end end "; LuaDLL.lua_dostring(L, PCallCSFunction); PCallCSFunctionRef = LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); pcall(L, init); }
public LuaState() { mainThread = System.Threading.Thread.CurrentThread.ManagedThreadId; L = LuaDLL.luaL_newstate(); statemap[L] = this; if (main == null) { main = this; } refQueue = new Queue <UnrefPair>(); ObjectCache.make(L); pcall(L, init); }
public LuaState() { mainThread = Thread.CurrentThread.ManagedThreadId; L = LuaDLL.luaL_newstate(); statemap[L] = this; if (main == null) { main = this; } refQueue = new Queue <UnrefPair>(); ObjectCache.make(L); LuaDLL.lua_atpanic(L, panicCallback); LuaDLL.luaL_openlibs(L); /*string PCallCSFunction = @" * local assert = assert * local function check(ok,...) * assert(ok, ...) * return ... * end * return function(cs_func) * return function(...) * return check(cs_func(...)) * end * end * ";*/ string PCallCSFunction = @" return function(cs_func) return function(...) return cs_func(...) end end "; LuaDLL.lua_dostring(L, PCallCSFunction); PCallCSFunctionRef = LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); pcall(L, init); }
public LuaState() { L = LuaDLL.luaL_newstate(); statemap[L] = this; if (main == null) { main = this; } refQueue = new Queue <int>(); LuaDLL.luaL_openlibs(L); ObjectCache.make(L); LuaDLL.lua_pushstdcallcfunction(L, print); LuaDLL.lua_setglobal(L, "print"); LuaDLL.lua_pushstdcallcfunction(L, pcall); LuaDLL.lua_setglobal(L, "pcall"); LuaDLL.lua_pushstdcallcfunction(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); }
public LuaState() { if (mainThread == 0) { mainThread = System.Threading.Thread.CurrentThread.ManagedThreadId; } L = LuaDLL.luaL_newstate(); statemap[L] = this; if (main == null) { main = this; } refQueue = new Queue <UnrefPair>(); ObjectCache.make(L); LuaDLL.lua_atpanic(L, panicCallback); LuaDLL.luaL_openlibs(L); string PCallCSFunction = @" local assert = assert local function check(ok,...) assert(ok, ...) return ... end return function(cs_func) return function(...) return check(cs_func(...)) end end "; LuaDLL.lua_dostring(L, PCallCSFunction); PCallCSFunctionRef = LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); setupPushVar(); pcall(L, init); }
public LuaState() { mainThread = System.Threading.Thread.CurrentThread.ManagedThreadId; L = LuaDLL.luaL_newstate(); statemap[L] = this; if (main == null) { main = this; } refQueue = new Queue <UnrefPair>(); ObjectCache.make(L); //lua作爲host的時候設置錯誤處理函數 LuaDLL.lua_atpanic(L, panicCallback); LuaDLL.luaL_openlibs(L); string PCallCSFunction = @" local assert = assert local function check(ok,...) assert(ok, ...) return ... end return function(cs_func) return function(...) return check(cs_func(...)) end end "; //wrap the cs_func in a new function, which check it's result whether is OK //call PCallCSFunction will return the wraped function LuaDLL.lua_dostring(L, PCallCSFunction); PCallCSFunctionRef = LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); setupPushVar(); pcall(L, init); }
public LuaState() { if (mainThread == 0) { mainThread = System.Threading.Thread.CurrentThread.ManagedThreadId; } L = LuaDLL.luaL_newstate(); statemap[L] = this; refQueue = new Queue <UnrefPair>(); ObjectCache.make(L); LuaDLL.lua_atpanic(L, panicCallback); LuaDLL.luaL_openlibs(L); string PCallCSFunction = @" local assert = assert local function check(ok,...) assert(ok, ...) return ... end return function(cs_func) return function(...) return check(cs_func(...)) end end "; LuaDLL.lua_dostring(L, PCallCSFunction); PCallCSFunctionRef = LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); string newindexfun = @" local getmetatable=getmetatable local rawget=rawget local error=error local type=type local function newindex(ud,k,v) local t=getmetatable(ud) repeat local h=rawget(t,k) if h then if h[2] then h[2](ud,v) return else error('property '..k..' is read only') end end t=rawget(t,'__parent') until t==nil error('can not find '..k) end return newindex "; string indexfun = @" local type=type local error=error local rawget=rawget local getmetatable=getmetatable local function index(ud,k) local t=getmetatable(ud) repeat local fun=rawget(t,k) local tp=type(fun) if tp=='function' then return fun elseif tp=='table' then local f=fun[1] if f then return f(ud) else error('property '..k..' is write only') end end t = rawget(t,'__parent') until t==nil error('Can not find '..k) end return index "; newindex_func = (LuaFunction)doString(newindexfun); index_func = (LuaFunction)doString(indexfun); setupPushVar(); pcall(L, init); createGameObject(); }
public LuaState() { mainThread = System.Threading.Thread.CurrentThread.ManagedThreadId; L = LuaDLL.luaL_newstate(); statemap[L] = this; if (main == null) { main = this; } refQueue = new Queue <UnrefPair>(); LuaDLL.luaL_openlibs(L); ObjectCache.make(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 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.throwLuaError(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); }