Пример #1
0
        public static int MakeArray(IntPtr ptr)
        {
            try
            {
                Type t;
                CheckType(ptr, 1, out t);
                LuaNativeMethods.luaL_checktype(ptr, 2, LuaTypes.TYPE_TABLE);
                int   n     = LuaNativeMethods.lua_rawlen(ptr, 2);
                Array array = Array.CreateInstance(t, n);
                for (int k = 0; k < n; k++)
                {
                    LuaNativeMethods.lua_rawgeti(ptr, 2, k + 1);
                    object obj = LuaObject.CheckVar(ptr, -1);
                    array.SetValue(LuaObject.ChangeType(obj, t), k);
                    LuaNativeMethods.lua_pop(ptr, 1);
                }

                LuaObject.PushValue(ptr, true);
                LuaObject.PushValue(ptr, array);
                return(2);
            }
            catch (Exception e)
            {
                return(Error(ptr, e));
            }
        }
Пример #2
0
        public static bool CheckArray <T>(IntPtr ptr, int p, out T[] ta)
        {
            if (LuaNativeMethods.lua_type(ptr, p) == LuaTypes.TYPE_TABLE)
            {
                int n = LuaNativeMethods.lua_rawlen(ptr, p);
                ta = new T[n];
                for (int k = 0; k < n; k++)
                {
                    LuaNativeMethods.lua_rawgeti(ptr, p, k + 1);
                    object o     = CheckVar(ptr, -1);
                    Type   fromT = o.GetType();
                    Type   toT   = typeof(T);

                    if (toT.IsAssignableFrom(fromT))
                    {
                        ta[k] = (T)o;
                    }
                    else
                    {
                        ta[k] = (T)Convert.ChangeType(o, typeof(T));
                    }

                    LuaNativeMethods.lua_pop(ptr, 1);
                }

                return(true);
            }
            else
            {
                Array array = CheckObj(ptr, p) as Array;
                ta = array as T[];
                return(ta != null);
            }
        }
Пример #3
0
        public int Length()
        {
            int n = LuaNativeMethods.lua_gettop(VariablePointer);

            Push(VariablePointer);
            int l = LuaNativeMethods.lua_rawlen(VariablePointer, -1);

            LuaNativeMethods.lua_settop(VariablePointer, n);
            return(l);
        }
Пример #4
0
        public static int Init(IntPtr ptr)
        {
            LuaNativeMethods.lua_pushlightuserdata(ptr, ptr);
            LuaNativeMethods.lua_setglobal(ptr, "__main_state");

            LuaNativeMethods.lua_pushcfunction(ptr, Print);
            LuaNativeMethods.lua_setglobal(ptr, "print");

            LuaNativeMethods.lua_pushcfunction(ptr, PrintError);
            LuaNativeMethods.lua_setglobal(ptr, "printerror");

            LuaNativeMethods.lua_pushcfunction(ptr, ProtectedCall);
            LuaNativeMethods.lua_setglobal(ptr, "pcall");

            PushCSFunction(ptr, Import);
            LuaNativeMethods.lua_setglobal(ptr, "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(ptr).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 = string.Emptystring.Empty
    local func = debug.getinfo(f, string.Emptyfstring.Empty).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 ..  string.Empty    string.Empty .. name .. string.Empty=string.Empty .. tostring(value) .. string.Empty\nstring.Empty 
      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 ~= string.Empty(*vararg)string.Empty then break end
      dump = dump ..  string.Empty    string.Empty .. name .. string.Empty=string.Empty .. tostring(value) .. string.Empty\nstring.Empty
      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 ..  string.Empty    string.Empty .. name .. string.Empty=string.Empty .. tostring(value) .. string.Empty\nstring.Empty
      i = i + 1
    end
    return dump
  end
  local dump = string.Emptystring.Empty
  for i = 3, 100 do
    local source = debug.getinfo(i, string.EmptySstring.Empty)
    if not source then break end
    dump = dump .. string.Empty- stackstring.Empty .. tostring(i-2) .. string.Empty\nstring.Empty
    dump = dump .. vars(i+1)
    if source.what == 'main' then break end
  end
  printerror(dump)
end
";

            LuaState.Get(ptr).DoString(dumpstackfunc);

#if UNITY_ANDROID
            // fix android performance drop with JIT on according to luajit mailist post
            LuaState.get(ptr).doString("if jit then require('jit.opt').start('sizemcode=256','maxmcode=256') for i=1,1000 do end end");
#endif

            PushCSFunction(ptr, DoFile);
            LuaNativeMethods.lua_setglobal(ptr, "dofile");

            PushCSFunction(ptr, LoadFile);
            LuaNativeMethods.lua_setglobal(ptr, "loadfile");

            PushCSFunction(ptr, Loader);
            int loaderFunc = LuaNativeMethods.lua_gettop(ptr);

            LuaNativeMethods.lua_getglobal(ptr, "package");
#if LUA_5_3
            LuaNativeMethods.lua_getfield(ptr, -1, "searchers");
#else
            LuaNativeMethods.lua_getfield(ptr, -1, "loaders");
#endif
            int loaderTable = LuaNativeMethods.lua_gettop(ptr);

            // Shift table elements right
            for (int e = LuaNativeMethods.lua_rawlen(ptr, loaderTable) + 1; e > 2; e--)
            {
                LuaNativeMethods.lua_rawgeti(ptr, loaderTable, e - 1);
                LuaNativeMethods.lua_rawseti(ptr, loaderTable, e);
            }

            LuaNativeMethods.lua_pushvalue(ptr, loaderFunc);
            LuaNativeMethods.lua_rawseti(ptr, loaderTable, 2);
            LuaNativeMethods.lua_settop(ptr, 0);
            return(0);
        }