コード例 #1
0
        public static int GetOpFunction(IntPtr ptr, string f, string tip)
        {
            int err = PushTry(ptr);

            CheckLuaObject(ptr, 1);

            while (!LuaNativeMethods.lua_isnil(ptr, -1))
            {
                LuaNativeMethods.lua_getfield(ptr, -1, f);
                if (!LuaNativeMethods.lua_isnil(ptr, -1))
                {
                    LuaNativeMethods.lua_remove(ptr, -2);
                    break;
                }

                LuaNativeMethods.lua_pop(ptr, 1);     // pop nil
                LuaNativeMethods.lua_getfield(ptr, -1, "__parent");
                LuaNativeMethods.lua_remove(ptr, -2); // pop base
            }

            if (LuaNativeMethods.lua_isnil(ptr, -1))
            {
                LuaNativeMethods.lua_pop(ptr, 1);
                throw new Exception(string.Format("No {0} operator", tip));
            }

            return(err);
        }
コード例 #2
0
ファイル: Lua3rdDLL.cs プロジェクト: BraedonWooding/slua
        public static void Open(IntPtr ptr)
        {
            List <string> typenames = Lua3rdMeta.Instance.TypesWithAttributes;

            Assembly[] assemblys = AppDomain.CurrentDomain.GetAssemblies();
            Assembly   assembly  = null;

            foreach (Assembly ass in assemblys)
            {
                if (ass.GetName().Name == "Assembly-CSharp")
                {
                    assembly = ass;
                    break;
                }
            }

            if (assembly != null)
            {
                foreach (string typename in typenames)
                {
                    Type         type    = assembly.GetType(typename);
                    MethodInfo[] methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public);
                    foreach (MethodInfo method in methods)
                    {
                        LualibRegAttribute attr = System.Attribute.GetCustomAttribute(method, typeof(LualibRegAttribute)) as LualibRegAttribute;
                        if (attr != null)
                        {
                            LuaCSFunction csfunc = Delegate.CreateDelegate(typeof(LuaCSFunction), method) as LuaCSFunction;
                            DLLRegFuncs.Add(attr.LuaName, csfunc);
                        }
                    }
                }
            }

            if (DLLRegFuncs.Count == 0)
            {
                return;
            }

            LuaNativeMethods.lua_getglobal(ptr, "package");
            LuaNativeMethods.lua_getfield(ptr, -1, "preload");
            foreach (KeyValuePair <string, LuaCSFunction> pair in DLLRegFuncs)
            {
                LuaNativeMethods.lua_pushcfunction(ptr, pair.Value);
                LuaNativeMethods.lua_setfield(ptr, -2, pair.Key);
            }

            LuaNativeMethods.lua_settop(ptr, 0);
        }
コード例 #3
0
ファイル: LuaState.cs プロジェクト: BraedonWooding/slua
        public static int Import(IntPtr ptr)
        {
            try
            {
                LuaNativeMethods.luaL_checktype(ptr, 1, LuaTypes.TYPE_STRING);
                string str = LuaNativeMethods.lua_tostring(ptr, 1);

                string[] ns = str.Split('.');

                LuaNativeMethods.lua_pushglobaltable(ptr);

                for (int n = 0; n < ns.Length; n++)
                {
                    LuaNativeMethods.lua_getfield(ptr, -1, ns[n]);
                    if (!LuaNativeMethods.lua_istable(ptr, -1))
                    {
                        return(LuaObject.Error(ptr, "expect {0} is type table", ns));
                    }

                    LuaNativeMethods.lua_remove(ptr, -2);
                }

                LuaNativeMethods.lua_pushnil(ptr);
                while (LuaNativeMethods.lua_next(ptr, -2) != 0)
                {
                    string key = LuaNativeMethods.lua_tostring(ptr, -2);
                    LuaNativeMethods.lua_getglobal(ptr, key);
                    if (!LuaNativeMethods.lua_isnil(ptr, -1))
                    {
                        LuaNativeMethods.lua_pop(ptr, 1);
                        return(LuaObject.Error(ptr, "{0} had existed, import can't overload it.", key));
                    }

                    LuaNativeMethods.lua_pop(ptr, 1);
                    LuaNativeMethods.lua_setglobal(ptr, key);
                }

                LuaNativeMethods.lua_pop(ptr, 1);

                LuaObject.PushValue(ptr, true);
                return(1);
            }
            catch (Exception e)
            {
                return(LuaObject.Error(ptr, e));
            }
        }
コード例 #4
0
ファイル: LuaState.cs プロジェクト: BraedonWooding/slua
        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);
        }
コード例 #5
0
ファイル: LuaState.cs プロジェクト: BraedonWooding/slua
        public static int ErrorReport(IntPtr ptr)
        {
            LuaNativeMethods.lua_getglobal(ptr, "debug");
            LuaNativeMethods.lua_getfield(ptr, -1, "traceback");
            LuaNativeMethods.lua_pushvalue(ptr, 1);
            LuaNativeMethods.lua_pushnumber(ptr, 2);
            LuaNativeMethods.lua_call(ptr, 2, 1);
            LuaNativeMethods.lua_remove(ptr, -2);
            string error = LuaNativeMethods.lua_tostring(ptr, -1);

            LuaNativeMethods.lua_pop(ptr, 1);

            Logger.LogError(error, true);
            if (ErrorEvent != null)
            {
                ErrorEvent(error);
            }

            LuaNativeMethods.lua_getglobal(ptr, "dumpstack");
            LuaNativeMethods.lua_call(ptr, 0, 0);

            return(0);
        }
コード例 #6
0
ファイル: LuaState.cs プロジェクト: BraedonWooding/slua
        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);
        }
コード例 #7
0
 public static void luaL_getmetatable(IntPtr luaState, string meta)
 {
     LuaNativeMethods.lua_getfield(luaState, LuaIndexes.LUARegistryIndex, meta);
 }