public LuaVar(IntPtr l, int r) { state = LuaState.get(l); valueref = r; }
public static void init(IntPtr l) { 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 if fun[1] then return fun[1](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 "; LuaState L = LuaState.get(l); newindex_func = (LuaFunction)L.doString(newindexfun); index_func = (LuaFunction)L.doString(indexfun); // object method LuaDLL.lua_createtable(l, 0, 4); addMember(l, ToString); addMember(l, GetHashCode); addMember(l, Equals); addMember(l, GetType); LuaDLL.lua_setfield(l, LuaIndexes.LUA_REGISTRYINDEX, "__luabaseobject"); LuaVarObject.init(l); LuaDLL.lua_newtable(l); LuaDLL.lua_setglobal(l, DelgateTable); setupPushVar(); }
static int init(IntPtr 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"); pushcsfunction(L, import); LuaDLL.lua_setglobal(L, "import"); string resumefunc = @" local resume = coroutine.resume local function check(co, ok, err, ...) if not ok then UnityEngine.Debug.LogError(debug.traceback(co,err)) end return ok, err, ... end coroutine.resume=function(co,...) return check(co, resume(co,...)) end "; // overload resume function for report error LuaState.get(L).doString(resumefunc); #if UNITY_ANDROID // fix android performance drop with JIT on according to luajit mailist post LuaState.get(L).doString("if jit then require('jit.opt').start('sizemcode=256','maxmcode=256') for i=1,1000 do end end"); #endif pushcsfunction(L, dofile); LuaDLL.lua_setglobal(L, "dofile"); pushcsfunction(L, loadfile); LuaDLL.lua_setglobal(L, "loadfile"); pushcsfunction(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 > 2; e--) { LuaDLL.lua_rawgeti(L, loaderTable, e - 1); LuaDLL.lua_rawseti(L, loaderTable, e); } LuaDLL.lua_pushvalue(L, loaderFunc); LuaDLL.lua_rawseti(L, loaderTable, 2); LuaDLL.lua_settop(L, 0); return(0); }
public static int Add(IntPtr l) { try{ int top = LuaDLL.lua_gettop(l); if (top == 2) { int delay; checkType(l, 1, out delay); LuaDelegate ld; checkType(l, 2, out ld); Action <int> ua; if (ld.d != null) { ua = (Action <int>)ld.d; } else { IntPtr ml = LuaState.get(l).L; ua = (int id) => { int error = pushTry(ml); pushValue(ml, id); ld.pcall(1, error); LuaDLL.lua_settop(ml, error - 1); }; } ld.d = ua; pushValue(l, true); pushValue(l, add(delay, ua)); return(2); } else if (top == 3) { int delay, cycle; checkType(l, 1, out delay); checkType(l, 2, out cycle); LuaDelegate ld; checkType(l, 3, out ld); Func <int, bool> ua; if (ld.d != null) { ua = (Func <int, bool>)ld.d; } else { IntPtr ml = LuaState.get(l).L; ua = (int id) => { int error = pushTry(ml); pushValue(ml, id); ld.pcall(1, error); bool ret = LuaDLL.lua_toboolean(ml, -1); LuaDLL.lua_settop(ml, error - 1); return(ret); }; } ld.d = ua; pushValue(l, true); pushValue(l, add(delay, cycle, ua)); return(2); } return(LuaObject.error(l, "Argument error")); }catch (Exception e) { return(LuaObject.error(l, e)); } }
static int init(IntPtr 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, printerror); LuaDLL.lua_setglobal(L, "printerror"); LuaDLL.lua_pushcfunction(L, pcall); LuaDLL.lua_setglobal(L, "pcall"); pushcsfunction(L, import); LuaDLL.lua_setglobal(L, "import"); string resumefunc = @" local resume = coroutine.resume local function check(co, ok, err, ...) if not ok then UnityEngine.Debug.LogError(debug.traceback(co,err)) end return ok, err, ... end coroutine.resume=function(co,...) return check(co, resume(co,...)) end "; // overload resume function for report error LuaState.get(L).doString(resumefunc); // https://github.com/pkulchenko/MobDebug/blob/master/src/mobdebug.lua#L290 // Dump only 3 stacks, or it will return null (I don't know why) string dumpstackfunc = @" local printerror=printerror dumpstack=function() function vars(f) local dump = """" local func = debug.getinfo(f, ""f"").func local i = 1 local locals = {} -- get locals while true do local name, value = debug.getlocal(f, i) if not name then break end if string.sub(name, 1, 1) ~= '(' then dump = dump .. "" "" .. name .. ""="" .. tostring(value) .. ""\n"" end i = i + 1 end -- get varargs (these use negative indices) i = 1 while true do local name, value = debug.getlocal(f, -i) -- `not name` should be enough, but LuaJIT 2.0.0 incorrectly reports `(*temporary)` names here if not name or name ~= ""(*vararg)"" then break end dump = dump .. "" "" .. name .. ""="" .. tostring(value) .. ""\n"" i = i + 1 end -- get upvalues i = 1 while func do -- check for func as it may be nil for tail calls local name, value = debug.getupvalue(func, i) if not name then break end dump = dump .. "" "" .. name .. ""="" .. tostring(value) .. ""\n"" i = i + 1 end return dump end local dump = """" for i = 3, 100 do local source = debug.getinfo(i, ""S"") if not source then break end dump = dump .. ""- stack"" .. tostring(i-2) .. ""\n"" dump = dump .. vars(i+1) if source.what == 'main' then break end end printerror(dump) end "; LuaState.get(L).doString(dumpstackfunc); #if UNITY_ANDROID // fix android performance drop with JIT on according to luajit mailist post LuaState.get(L).doString("if jit then require('jit.opt').start('sizemcode=256','maxmcode=256') for i=1,1000 do end end"); #endif pushcsfunction(L, dofile); LuaDLL.lua_setglobal(L, "dofile"); pushcsfunction(L, loadfile); LuaDLL.lua_setglobal(L, "loadfile"); pushcsfunction(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 > 2; e--) { LuaDLL.lua_rawgeti(L, loaderTable, e - 1); LuaDLL.lua_rawseti(L, loaderTable, e); } LuaDLL.lua_pushvalue(L, loaderFunc); LuaDLL.lua_rawseti(L, loaderTable, 2); LuaDLL.lua_settop(L, 0); return(0); }
private static void completeInstanceMeta(IntPtr l, Type self) { //lua stack on enter: // instance metatable(-1) // static metatable(-2) // type table(-3) LuaState L = LuaState.get(l); LuaDLL.lua_pushstring(l, "__fullname"); LuaDLL.lua_pushstring(l, self.FullName); LuaDLL.lua_rawset(l, -3);//set instance metatable LuaDLL.lua_pushstring(l, "__typename"); LuaDLL.lua_pushstring(l, self.Name); LuaDLL.lua_rawset(l, -3);//set instance metatable // for instance L.index_func.push(l); LuaDLL.lua_setfield(l, -2, "__index"); L.newindex_func.push(l); LuaDLL.lua_setfield(l, -2, "__newindex"); //因为cs2lua为了更好的匹配重载方法,给存在重载的方法添加了一个签名参数 //lua元方法调用传过来的参数是没有签名参数的,这样会导致方法匹配失败。 //cs2lua翻译的代码会明确调用c#的重载操作符方法。slua对lua元方法的支持 //只保留__gc与__tostring /* * pushValue(l, lua_add); * LuaDLL.lua_setfield(l, -2, "__add"); * pushValue(l, lua_sub); * LuaDLL.lua_setfield(l, -2, "__sub"); * pushValue(l, lua_mul); * LuaDLL.lua_setfield(l, -2, "__mul"); * pushValue(l, lua_div); * LuaDLL.lua_setfield(l, -2, "__div"); * pushValue(l, lua_unm); * LuaDLL.lua_setfield(l, -2, "__unm"); * pushValue(l, lua_eq); * LuaDLL.lua_setfield(l, -2, "__eq"); * pushValue(l, lua_le); * LuaDLL.lua_setfield(l, -2, "__le"); * pushValue(l, lua_lt); * LuaDLL.lua_setfield(l, -2, "__lt"); */ pushValue(l, lua_tostring); LuaDLL.lua_setfield(l, -2, "__tostring"); LuaDLL.lua_pushcfunction(l, lua_gc); LuaDLL.lua_setfield(l, -2, "__gc"); if (self.IsValueType && isImplByLua(self)) { LuaDLL.lua_pushvalue(l, -1); LuaDLL.lua_setglobal(l, self.FullName + ".Instance");//valuetype's instance metatable assign to FullTypeName+".Instance" global variable } LuaDLL.lua_pushvalue(l, -1); LuaDLL.lua_setglobal(l, "__cslib_instance_meta_" + self.FullName.Replace(".", "_")); //instance metatable assign to "__cslib_instance_meta_{full_name_rep_dot_with_}" global variable LuaDLL.lua_setfield(l, LuaIndexes.LUA_REGISTRYINDEX, ObjectCache.getAQName(self)); //pop instance metatable and reg instance metatable }
public static void reg(IntPtr l) { LuaState ls = LuaState.get(l); ls.doString(script, "LuaSocketMini"); }