예제 #1
0
        public static bool TryPush(IntPtr l, object obj)
        {
#if DEBUG_LUA_THREADSAFE
            LuaStateAttachmentManager.CheckThread(l);
#endif
#if DEBUG_LUA_PERFORMANCE
            System.Threading.Interlocked.Increment(ref _TotalCnt);
#endif
            if (obj != null)
            {
                LuaObjCacheSlimStorageRecord record;
                if (Storage.ObjMap.TryGetValue(obj, out record))
                {
                    var pos     = record.StackPos;
                    var pointer = record.Pointer;
                    if (l.topointer(pos) == pointer)
                    {
                        l.pushvalue(pos);
#if DEBUG_LUA_PERFORMANCE
                        System.Threading.Interlocked.Increment(ref _HitCnt);
                        UnityEngine.Debug.Log(((float)_HitCnt) / _TotalCnt);
#endif
                        return(true);
                    }
                }
            }
#if DEBUG_LUA_PERFORMANCE
            UnityEngine.Debug.Log(((float)_HitCnt) / _TotalCnt);
#endif
            return(false);
        }
예제 #2
0
        public static bool TryGet(IntPtr l, int index, out object obj)
        {
#if DEBUG_LUA_THREADSAFE
            LuaStateAttachmentManager.CheckThread(l);
#endif
#if DEBUG_LUA_PERFORMANCE
            System.Threading.Interlocked.Increment(ref _TotalCnt);
#endif
            var pointer = l.topointer(index);
            if (pointer != IntPtr.Zero)
            {
                LuaObjCacheSlimStorageRecord record;
                if (Storage.PointerMap.TryGetValue(pointer, out record))
                {
                    obj = record.Obj;
#if DEBUG_LUA_PERFORMANCE
                    System.Threading.Interlocked.Increment(ref _HitCnt);
                    UnityEngine.Debug.Log(((float)_HitCnt) / _TotalCnt);
#endif
                    return(true);
                }
            }
            obj = null;
#if DEBUG_LUA_PERFORMANCE
            UnityEngine.Debug.Log(((float)_HitCnt) / _TotalCnt);
#endif
            return(false);
        }
예제 #3
0
        public static LuaStateAttachmentManager GetAttachmentManager(IntPtr l)
        {
#if DEBUG_LUA_THREADSAFE
            LuaStateAttachmentManager.CheckThread(l);
#endif
            var indicator = l.Indicator();
            LuaStateAttachmentManager rv = GetAttachmentManagerForIndicator(indicator);
            if (rv != null)
            {
                return(rv);
            }

            l.checkstack(1);
            l.pushlightuserdata(LuaConst.LRKEY_REF_ATTACH); // #man
            l.gettable(lua.LUA_REGISTRYINDEX);              // man
            if (l.isuserdata(-1))
            {
                LuaStateAttachmentManager man = null;
                try
                {
                    IntPtr pud = l.touserdata(-1);
                    if (pud != IntPtr.Zero)
                    {
                        IntPtr   hval   = Marshal.ReadIntPtr(pud);
                        GCHandle handle = (GCHandle)hval;
                        man = handle.Target as LuaStateAttachmentManager;
                    }
                }
                catch { }
                l.pop(1); // X
                if (man != null)
                {
                    Map[indicator] = man;
                }
                return(man);
            }
            else
            {
                //l.checkstack(5);
                l.pop(1); // X
                //l.pushlightuserdata(LuaConst.LRKEY_REF_ATTACH); // #man
                //LuaStateAttachmentManager man = new LuaStateAttachmentManager(l);
                //var h = l.PushLuaRawObject(man); // #man man
                //l.PushCommonMetaTable(); // #man man meta
                //l.setmetatable(-2); // #man man
                //l.newtable(); // #man man env
                //l.pushlightuserdata(LuaConst.LRKEY_OBJ_META_EX); // #man man env #meta
                //l.pushlightuserdata(h); // #man man env #meta meta
                //l.settable(-3); // #man man env
                //l.setfenv(-2); // #man man
                //l.settable(lua.LUA_REGISTRYINDEX); // X
                //Map[indicator] = man;
                //return man;

                return(null);
            }
        }
예제 #4
0
        public void gc(IntPtr l, object obj)
        {
#if DEBUG_LUA_THREADSAFE
            LuaStateAttachmentManager.CheckThread(l);
#endif
            var map = _Map;
            if (map != null)
            {
                map.Remove(l.Indicator());
            }
        }
예제 #5
0
        public static LuaStateAttachmentManager GetAttachmentManagerForIndicator(IntPtr indicator)
        {
            LuaStateAttachmentManager rv = null;
            var map = _Map;

            if (map != null)
            {
                map.TryGetValue(indicator, out rv);
            }
            return(rv);
        }
예제 #6
0
 public static LuaObjCache GetOrCreateObjCache(IntPtr l)
 {
     return(LuaStateAttachmentManager.GetOrCreateAttachmentManager(l).ObjCache);
     //var rv = GetObjCache(l);
     //if (rv == null)
     //{
     //    l.checkstack(2);
     //    rv = new LuaObjCache();
     //    l.pushlightuserdata(LuaConst.LRKEY_OBJ_CACHE); // key
     //    l.PushLuaRawObject(rv); // key cache
     //    l.settable(lua.LUA_REGISTRYINDEX); // X
     //}
     //return rv;
 }
예제 #7
0
        public static void PushString(this IntPtr l, string str)
        {
#if DEBUG_LUA_PERFORMANCE
            PushOrGetStringTimingWatch.Restart();
            try
            {
#endif
            l.pushstring(str);
            return;            // test result shows it is faster to NOT use the cache.

#pragma warning disable CS0162 // 检测到无法访问的代码
            LuaString predefined = LuaString.GetString(str);
            if (predefined != null)
            {
                predefined.PushString(l);
                return;
            }

            var cache = LuaStateAttachmentManager.GetOrCreateAttachmentManager(l).StrCache;
            cache.L = l;

            var info = cache.PutIntoCache(str);
            if (info == null)
            {
                l.pushstring(str); // str
            }
            else
            {
                if (!PushString(l, info.Id))
                {
                    // this should not happen
                    l.pop(3);          // X
                    l.pushstring(str); // str
                }
            }
#pragma warning restore CS0162 // 检测到无法访问的代码
#if DEBUG_LUA_PERFORMANCE
        }

        finally
        {
            PushOrGetStringTimingWatch.Stop();
            long delta = PushOrGetStringTimingWatch.ElapsedTicks;
            var  cnt   = System.Threading.Interlocked.Increment(ref PushOrGetStringCallCount);
            var  time  = System.Threading.Interlocked.Add(ref PushOrGetStringCallTotalTime, delta);
            UnityEngine.Debug.Log(((double)time) / (double)System.Diagnostics.Stopwatch.Frequency * 1000.0 / cnt);
        }
#endif
        }
예제 #8
0
        public static LuaObjCache GetObjCache(IntPtr l)
        {
            var attachman = LuaStateAttachmentManager.GetAttachmentManager(l);

            if (attachman != null)
            {
                return(attachman.ObjCache);
            }
            return(null);
            //l.checkstack(1);
            //l.pushlightuserdata(LuaConst.LRKEY_OBJ_CACHE); // key
            //l.gettable(lua.LUA_REGISTRYINDEX); // cache
            //var rv = l.GetLuaObject(-1) as LuaObjCache;
            //l.pop(1);
            //return rv;
        }
예제 #9
0
        public static string GetString(this IntPtr l, int index)
        {
#if DEBUG_LUA_PERFORMANCE
            PushOrGetStringTimingWatch.Restart();
            try
            {
#endif
            return(l.tostring(index)); // test result shows it is faster to NOT use the cache.

#pragma warning disable CS0162         // 检测到无法访问的代码
            int id = GetStringRegId(l, index);
            if (id != 0)
            {
                if (id < 0)
                {
                    LuaString predefined = LuaString.GetString(id);
                    if (predefined != null)
                    {
                        return(predefined.Str);
                    }
                    else
                    {
                        // this should not happen.
                        return(l.tostring(index));
                    }
                }
                else
                {
                    LuaStringCache cache = LuaStateAttachmentManager.GetOrCreateAttachmentManager(l).StrCache;
                    cache.L = l;
                    LuaStringCache.LuaCachedStringInfo info;
                    if (cache.TryGetCacheInfo(id, out info))
                    {
                        return(info.Str);
                    }
                    else
                    {
                        // this should not happen.
                        var str = l.tostring(index);
                        cache.PutIntoCache(str);
                        return(str);
                    }
                }
            }
            else
            {
                // Not cached.
                var str = l.tostring(index);
                if (str != null && str.Length >= LuaStringCache.CachedStringMinLen && str.Length <= LuaStringCache.CachedStringMaxLen)
                {
                    var cache = LuaStateAttachmentManager.GetOrCreateAttachmentManager(l).StrCache;
                    cache.L = l;
                    cache.PutIntoCache(str);
                }
                return(str);
            }
#pragma warning restore CS0162 // 检测到无法访问的代码
#if DEBUG_LUA_PERFORMANCE
        }

        finally
        {
            PushOrGetStringTimingWatch.Stop();
            long delta = PushOrGetStringTimingWatch.ElapsedTicks;
            var  cnt   = System.Threading.Interlocked.Increment(ref PushOrGetStringCallCount);
            var  time  = System.Threading.Interlocked.Add(ref PushOrGetStringCallTotalTime, delta);
            UnityEngine.Debug.Log(((double)time) / (double)System.Diagnostics.Stopwatch.Frequency * 1000.0 / cnt);
        }
#endif
        }