Beispiel #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);
        }
Beispiel #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);
        }
Beispiel #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);
            }
        }
Beispiel #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());
            }
        }