Exemple #1
0
        public object TopObjects(int from)
        {
            int top  = LuaNativeMethods.lua_gettop(statePointer);
            int args = top - from;

            if (args == 0)
            {
                return(null);
            }
            else if (args == 1)
            {
                object o = LuaObject.CheckVar(statePointer, top);
                LuaNativeMethods.lua_pop(statePointer, 1);
                return(o);
            }
            else
            {
                object[] o = new object[args];
                for (int n = 1; n <= args; n++)
                {
                    o[n - 1] = LuaObject.CheckVar(statePointer, from + n);
                }

                LuaNativeMethods.lua_settop(statePointer, from);
                return(o);
            }
        }
Exemple #2
0
        public static int Constructor(IntPtr ptr)
        {
            try
            {
                int       argc = LuaNativeMethods.lua_gettop(ptr);
                ByteArray o;
                if (argc == 1)
                {
                    o = new ByteArray();
                    LuaObject.PushValue(ptr, true);
                    LuaObject.PushValue(ptr, o);
                    return(2);
                }
                else if (argc == 2)
                {
                    byte[] a1;
                    LuaObject.CheckArray(ptr, 2, out a1);
                    o = new ByteArray(a1);
                    LuaObject.PushValue(ptr, true);
                    LuaObject.PushValue(ptr, o);
                    return(2);
                }

                return(Error(ptr, "New object failed."));
            }
            catch (Exception e)
            {
                return(Error(ptr, e));
            }
        }
Exemple #3
0
        public void SetObject(int reference, string field, object o)
        {
            int oldTop = LuaNativeMethods.lua_gettop(statePointer);

            LuaNativeMethods.lua_getref(statePointer, reference);
            SetObject(field.Split(new char[] { '.' }), o);
            LuaNativeMethods.lua_settop(statePointer, oldTop);
        }
Exemple #4
0
 public void CheckTop()
 {
     if (LuaNativeMethods.lua_gettop(StatePointer) != errorReported)
     {
         errorReported = LuaNativeMethods.lua_gettop(StatePointer);
         Logger.LogError(string.Format("Some function not remove temp value({0}) from lua stack. You should fix it.", LuaNativeMethods.luaL_typename(StatePointer, errorReported)));
     }
 }
Exemple #5
0
        public void SetObject(int reference, object field, object o)
        {
            int oldTop = LuaNativeMethods.lua_gettop(statePointer);

            LuaNativeMethods.lua_getref(statePointer, reference);
            LuaObject.PushObject(statePointer, field);
            LuaObject.PushObject(statePointer, o);
            LuaNativeMethods.lua_settable(statePointer, -3);
            LuaNativeMethods.lua_settop(statePointer, oldTop);
        }
Exemple #6
0
        public object GetObject(int reference, string field)
        {
            int oldTop = LuaNativeMethods.lua_gettop(statePointer);

            LuaNativeMethods.lua_getref(statePointer, reference);
            object returnValue = GetObject(field.Split(new char[] { '.' }));

            LuaNativeMethods.lua_settop(statePointer, oldTop);
            return(returnValue);
        }
 public LuaThreadWrapper(LuaFunction function) : base()
 {
     Logger.Log(string.Format("LuaThreadWrapper.ctor/1: {0}", LuaNativeMethods.lua_gettop(function.VariablePointer)));
     this.state    = LuaState.Get(function.VariablePointer);
     this.thread   = LuaNativeMethods.lua_newthread(function.VariablePointer);
     this.valueref = LuaNativeMethods.luaL_ref(function.VariablePointer, LuaIndexes.LUARegistryIndex);
     function.Push(function.VariablePointer);
     LuaNativeMethods.lua_xmove(function.VariablePointer, this.thread, 1);
     Logger.Log(string.Format("LuaThreadWrapper.ctor/2: {0}", LuaNativeMethods.lua_gettop(function.VariablePointer)));
 }
Exemple #8
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);
        }
Exemple #9
0
        public object GetObject(int reference, object field)
        {
            int oldTop = LuaNativeMethods.lua_gettop(statePointer);

            LuaNativeMethods.lua_getref(statePointer, reference);
            LuaObject.PushObject(statePointer, field);
            LuaNativeMethods.lua_gettable(statePointer, -2);
            object returnValue = GetObject(statePointer, -1);

            LuaNativeMethods.lua_settop(statePointer, oldTop);
            return(returnValue);
        }
Exemple #10
0
        public static int ProtectedCall(IntPtr ptr)
        {
            int status;

            if (LuaNativeMethods.lua_type(ptr, 1) != LuaTypes.TYPE_FUNCTION)
            {
                return(LuaObject.Error(ptr, "arg 1 expect function"));
            }

            LuaNativeMethods.luaL_checktype(ptr, 1, LuaTypes.TYPE_FUNCTION);
            status = LuaNativeMethods.lua_pcall(ptr, LuaNativeMethods.lua_gettop(ptr) - 1, LuaNativeMethods.LUAMultRet, 0);
            LuaNativeMethods.lua_pushboolean(ptr, status == 0);
            LuaNativeMethods.lua_insert(ptr, 1);
            return(LuaNativeMethods.lua_gettop(ptr));  /* return status + all results */
        }
Exemple #11
0
        public void SetObject(string[] remainingPath, object o)
        {
            int top = LuaNativeMethods.lua_gettop(statePointer);

            for (int i = 0; i < remainingPath.Length - 1; i++)
            {
                LuaNativeMethods.lua_pushstring(statePointer, remainingPath[i]);
                LuaNativeMethods.lua_gettable(statePointer, -2);
            }

            LuaNativeMethods.lua_pushstring(statePointer, remainingPath[remainingPath.Length - 1]);
            LuaObject.PushVar(statePointer, o);
            LuaNativeMethods.lua_settable(statePointer, -3);
            LuaNativeMethods.lua_settop(statePointer, top);
        }
Exemple #12
0
        public int PushTry()
        {
            if (errorRef == 0)
            {
                LuaNativeMethods.lua_pushcfunction(statePointer, LuaState.errorFunc);
                LuaNativeMethods.lua_pushvalue(statePointer, -1);
                errorRef = LuaNativeMethods.luaL_ref(statePointer, LuaIndexes.LUARegistryIndex);
            }
            else
            {
                LuaNativeMethods.lua_getref(statePointer, errorRef);
            }

            return(LuaNativeMethods.lua_gettop(statePointer));
        }
Exemple #13
0
        public static int CreateClass(IntPtr ptr)
        {
            try
            {
                string cls;
                CheckType(ptr, 1, out cls);
                Type t = LuaObject.FindType(cls);
                if (t == null)
                {
                    return(Error(ptr, string.Format("Can't find {0} to create", cls)));
                }

                ConstructorInfo[] cis    = t.GetConstructors();
                ConstructorInfo   target = null;
                for (int n = 0; n < cis.Length; n++)
                {
                    ConstructorInfo ci = cis[n];
                    if (LuaObject.MatchType(ptr, LuaNativeMethods.lua_gettop(ptr), 2, ci.GetParameters()))
                    {
                        target = ci;
                        break;
                    }
                }

                if (target != null)
                {
                    ParameterInfo[] pis  = target.GetParameters();
                    object[]        args = new object[pis.Length];
                    for (int n = 0; n < pis.Length; n++)
                    {
                        args[n] = LuaObject.ChangeType(LuaObject.CheckVar(ptr, n + 2), pis[n].ParameterType);
                    }

                    object ret = target.Invoke(args);
                    LuaObject.PushValue(ptr, true);
                    LuaObject.PushVar(ptr, ret);
                    return(2);
                }

                LuaObject.PushValue(ptr, true);
                return(1);
            }
            catch (Exception e)
            {
                return(Error(ptr, e));
            }
        }
Exemple #14
0
        public static bool CheckValueParams <T>(IntPtr ptr, int p, out T[] pars) where T : struct
        {
            int top = LuaNativeMethods.lua_gettop(ptr);

            if (top - p >= 0)
            {
                pars = new T[top - p + 1];
                for (int n = p, k = 0; n <= top; n++, k++)
                {
                    CheckValueType(ptr, n, out pars[k]);
                }

                return(true);
            }

            pars = new T[0];
            return(true);
        }
Exemple #15
0
        public static bool CheckParams(IntPtr ptr, int p, out string[] pars)
        {
            int top = LuaNativeMethods.lua_gettop(ptr);

            if (top - p >= 0)
            {
                pars = new string[top - p + 1];
                for (int n = p, k = 0; n <= top; n++, k++)
                {
                    CheckType(ptr, n, out pars[k]);
                }

                return(true);
            }

            pars = new string[0];
            return(true);
        }
Exemple #16
0
        public static int PrintError(IntPtr ptr)
        {
            int n = LuaNativeMethods.lua_gettop(ptr);

            s.Length = 0;

            LuaNativeMethods.lua_getglobal(ptr, "tostring");

            for (int i = 1; i <= n; i++)
            {
                if (i > 1)
                {
                    s.Append("    ");
                }

                LuaNativeMethods.lua_pushvalue(ptr, -1);
                LuaNativeMethods.lua_pushvalue(ptr, i);

                LuaNativeMethods.lua_call(ptr, 1, 1);
                s.Append(LuaNativeMethods.lua_tostring(ptr, -1));
                LuaNativeMethods.lua_pop(ptr, 1);
            }

            LuaNativeMethods.lua_settop(ptr, n);

            LuaNativeMethods.lua_getglobal(ptr, "debug");
            LuaNativeMethods.lua_getfield(ptr, -1, "traceback");
            LuaNativeMethods.lua_call(ptr, 0, 1);
            s.Append("\n");
            s.Append(LuaNativeMethods.lua_tostring(ptr, -1));
            LuaNativeMethods.lua_pop(ptr, 1);
            Logger.LogError(s.ToString(), true);

            if (ErrorEvent != null)
            {
                ErrorEvent(s.ToString());
            }

            return(0);
        }
Exemple #17
0
        public static int DoFile(IntPtr ptr)
        {
            int n = LuaNativeMethods.lua_gettop(ptr);

            Loader(ptr);
            if (!LuaNativeMethods.lua_toboolean(ptr, -2))
            {
                return(2);
            }
            else
            {
                if (LuaNativeMethods.lua_isnil(ptr, -1))
                {
                    string fileName = LuaNativeMethods.lua_tostring(ptr, 1);
                    return(LuaObject.Error(ptr, "Can't find {0}", fileName));
                }

                int k = LuaNativeMethods.lua_gettop(ptr);
                LuaNativeMethods.lua_call(ptr, 0, LuaNativeMethods.LUAMultRet);
                k = LuaNativeMethods.lua_gettop(ptr);
                return(k - n);
            }
        }
Exemple #18
0
        public static int SetData(IntPtr ptr)
        {
            try
            {
                int argc = LuaNativeMethods.lua_gettop(ptr);
                if (argc == 2)
                {
                    ByteArray self = (ByteArray)LuaObject.CheckSelf(ptr);
                    byte[]    a1;
                    LuaObject.CheckArray(ptr, 2, out a1);
                    self.SetData(a1);
                    LuaObject.PushValue(ptr, true);
                    return(1);
                }
                else if (argc == 4)
                {
                    ByteArray self = (ByteArray)LuaObject.CheckSelf(ptr);
                    byte[]    a1;
                    LuaObject.CheckArray(ptr, 2, out a1);
                    int a2;
                    CheckType(ptr, 3, out a2);
                    int a3;
                    CheckType(ptr, 4, out a3);
                    self.SetData(a1, a2, a3);
                    LuaObject.PushValue(ptr, true);
                    return(1);
                }

                LuaObject.PushValue(ptr, false);
                LuaNativeMethods.lua_pushstring(ptr, "No matched override function SetData to call");
                return(2);
            }
            catch (Exception e)
            {
                return(Error(ptr, e));
            }
        }
        public bool Resume(out object retVal)
        {
            if (this.thread == IntPtr.Zero)
            {
                Logger.LogError("thread: already disposed?");
                retVal = null;
                return(false);
            }

            int status = LuaNativeMethods.lua_status(this.thread);

            if (status != 0 && status != (int)LuaThreadStatus.LUA_YIELD)
            {
                Logger.LogError("thread: wrong status ?= " + status);
                retVal = null;
                return(false);
            }

            int result = LuaNativeMethods.lua_resume(this.thread, 0);

            if (result != (int)LuaThreadStatus.LUA_YIELD)
            {
                if (result != 0)
                {
                    string error = LuaNativeMethods.lua_tostring(this.thread, -1);
                    Logger.LogError(string.Format("wrong result ?= {0} err: {1}", result, error));
                }

                retVal = null;
                return(false);
            }

            int argsFromYield = LuaNativeMethods.lua_gettop(this.thread);

            retVal = this.TopObjects(argsFromYield);
            return(true);
        }
Exemple #20
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);
        }
Exemple #21
0
 public void Reset()
 {
     LuaNativeMethods.lua_getref(table.VariablePointer, table.Reference);
     tableIndex = LuaNativeMethods.lua_gettop(table.VariablePointer);
 }
Exemple #22
0
        public static int Add(IntPtr ptr)
        {
            try
            {
                int top = LuaNativeMethods.lua_gettop(ptr);
                if (top == 2)
                {
                    int delay;
                    CheckType(ptr, 1, out delay);
                    LuaDelegate luaDelegate;
                    CheckType(ptr, 2, out luaDelegate);
                    Action <int> ua;
                    if (luaDelegate.Delegate != null)
                    {
                        ua = (Action <int>)luaDelegate.Delegate;
                    }
                    else
                    {
                        IntPtr ml = LuaState.Get(ptr).StatePointer;
                        ua = (int id) =>
                        {
                            int error = PushTry(ml);
                            LuaObject.PushValue(ml, id);
                            luaDelegate.ProtectedCall(1, error);
                            LuaNativeMethods.lua_settop(ml, error - 1);
                        };
                    }

                    luaDelegate.Delegate = ua;
                    LuaObject.PushValue(ptr, true);
                    LuaObject.PushValue(ptr, Add(delay, ua));
                    return(2);
                }
                else if (top == 3)
                {
                    int delay, cycle;
                    CheckType(ptr, 1, out delay);
                    CheckType(ptr, 2, out cycle);
                    LuaDelegate luaDelegate;
                    CheckType(ptr, 3, out luaDelegate);
                    Func <int, bool> ua;

                    if (luaDelegate.Delegate != null)
                    {
                        ua = (Func <int, bool>)luaDelegate.Delegate;
                    }
                    else
                    {
                        IntPtr ml = LuaState.Get(ptr).StatePointer;
                        ua = (int id) =>
                        {
                            int error = PushTry(ml);
                            LuaObject.PushValue(ml, id);
                            luaDelegate.ProtectedCall(1, error);
                            bool ret = LuaNativeMethods.lua_toboolean(ml, -1);
                            LuaNativeMethods.lua_settop(ml, error - 1);
                            return(ret);
                        };
                    }

                    luaDelegate.Delegate = ua;
                    LuaObject.PushValue(ptr, true);
                    LuaObject.PushValue(ptr, Add(delay, cycle, ua));
                    return(2);
                }

                return(Error(ptr, "Argument error"));
            }
            catch (Exception e)
            {
                return(Error(ptr, e));
            }
        }