コード例 #1
0
 protected internal override object GetFieldImp(object key)
 {
     if (_L != IntPtr.Zero)
     {
         if (_L.istable(StackPos))
         {
             using (LuaStateRecover lr = new LuaStateRecover(_L))
             {
                 _L.pushvalue(StackPos);
                 _L.PushLua(key);
                 _L.rawget(-2);
                 return(_L.GetLua(-1));
             }
         }
         else
         {
             if (GLog.IsLogInfoEnabled)
             {
                 GLog.LogInfo("luatable raw on stack: not a table.");
             }
         }
     }
     else
     {
         if (GLog.IsLogInfoEnabled)
         {
             GLog.LogInfo("luatable raw on stack: null state.");
         }
     }
     return(null);
 }
コード例 #2
0
 public override object[] Resume(params object[] args)
 {
     if (_Func != null && L != IntPtr.Zero)
     {
         using (var lr = new LuaStateRecover(L))
         {
             if (_NeedRestart)
             {
                 _NeedRestart = false;
                 _IsDone      = false;
                 if (_Func == null)
                 {
                     return(null);
                 }
                 L.PushLua(_Func);
                 return(ResumeRaw(args));
             }
             else if (IsRunning)
             {
                 return(ResumeRaw(args));
             }
         }
     }
     return(null);
 }
コード例 #3
0
 protected internal override bool SetFieldImp(object key, object val)
 {
     if (_L != IntPtr.Zero)
     {
         if (_L.istable(StackPos))
         {
             using (LuaStateRecover lr = new LuaStateRecover(_L))
             {
                 _L.pushvalue(StackPos);
                 _L.PushLua(key);
                 _L.PushLua(val);
                 _L.rawset(-3);
                 return(true);
             }
         }
         else
         {
             if (GLog.IsLogInfoEnabled)
             {
                 GLog.LogInfo("luatable raw on stack: not a table.");
             }
         }
     }
     else
     {
         if (GLog.IsLogInfoEnabled)
         {
             GLog.LogInfo("luatable raw on stack: null state.");
         }
     }
     return(false);
 }
コード例 #4
0
        public object[] CallGlobalSelf(string objname, string funcname, params object[] args)
        {
            var l = L;

            using (var lr = new LuaStateRecover(l))
            {
                if (!l.GetHierarchicalRaw(lua.LUA_GLOBALSINDEX, objname))
                {
                    return(null);
                }
                var _self = l.GetLuaOnStack(-1);
                if (!l.GetHierarchicalRaw(-1, funcname))
                {
                    return(null);
                }
                var paramCnt = 0;
                if (args != null)
                {
                    paramCnt = args.Length;
                }
                var rargs = ObjectPool.GetParamsFromPool(paramCnt + 1);
                rargs[0] = _self;
                for (int i = 0; i < paramCnt; ++i)
                {
                    rargs[i + 1] = args[i];
                }
                var rv = l.PushArgsAndCall(rargs);
                ObjectPool.ReturnParamsToPool(rargs);
                return(rv);
            }
        }
コード例 #5
0
 protected internal override object GetFieldImp(object key)
 {
     if (_L != IntPtr.Zero)
     {
         if (_L.istable(StackPos))
         {
             using (LuaStateRecover lr = new LuaStateRecover(_L))
             {
                 _L.pushvalue(StackPos);
                 _L.PushLua(key);
                 _L.gettable(-2);
                 return(_L.GetLua(-1));
             }
         }
         else
         {
             DynamicHelper.LogInfo("luatable on stack: not a table.");
         }
     }
     else
     {
         DynamicHelper.LogInfo("luatable on stack: null state.");
     }
     return(null);
 }
コード例 #6
0
        public static int MetaRefManGc(IntPtr l)
        {
            using (var lr = new LuaStateRecover(l))
            {
                var oldtop = l.gettop();
                if (oldtop < 1)
                {
                    return(0);
                }

                if (l.isuserdata(1) && !l.islightuserdata(1))
                {
                    IntPtr   pud    = l.touserdata(1);
                    IntPtr   hval   = Marshal.ReadIntPtr(pud);
                    GCHandle handle = (GCHandle)hval;

                    LuaThreadRefMan ud = null;
                    try
                    {
                        ud = handle.Target as LuaThreadRefMan;
                    }
                    catch { }
                    if (!object.ReferenceEquals(ud, null))
                    {
                        ud.Close();
                    }

                    handle.Free();
                }
            }
            return(0);
        }
コード例 #7
0
 protected internal override bool SetFieldImp(object key, object val)
 {
     if (_L != IntPtr.Zero)
     {
         if (_L.istable(StackPos))
         {
             using (LuaStateRecover lr = new LuaStateRecover(_L))
             {
                 _L.pushvalue(StackPos);
                 _L.PushLua(key);
                 _L.PushLua(val);
                 _L.settable(-3);
                 return(true);
             }
         }
         else
         {
             DynamicHelper.LogInfo("luatable on stack: not a table.");
         }
     }
     else
     {
         DynamicHelper.LogInfo("luatable on stack: null state.");
     }
     return(false);
 }
コード例 #8
0
        public static object GetLua(this IntPtr l, int index)
        {
            if (l != IntPtr.Zero)
            {
                using (var lr = new LuaStateRecover(l))
                {
                    l.pushvalue(index);
                    int typecode = l.type(-1);
                    switch (typecode)
                    {
                    case lua.LUA_TBOOLEAN:
                        return(l.toboolean(-1));

                    case lua.LUA_TFUNCTION:
                        //if (l.iscfunction(-1))
                        //    return l.tocfunction(-1);
                        //else
                        return(new LuaFunc(l, -1));

                    case lua.LUA_TLIGHTUSERDATA:
                        return(l.touserdata(-1));

                    case lua.LUA_TUSERDATA:
                        return(l.GetUserDataRaw(-1));

                    case lua.LUA_TNIL:
                        return(null);

                    case lua.LUA_TNONE:
                        return(null);

                    case lua.LUA_TNUMBER:
                        double num = l.tonumber(-1);
                        //int inum = (int)num;
                        //if (num == (double)inum)
                        //{
                        //    return inum;
                        //}
                        //else
                        {
                            return(num);
                        }

                    case lua.LUA_TSTRING:
                        //return l.tostring(-1);
                        return(l.GetString(-1));

                    case lua.LUA_TTABLE:
                        return(new LuaTable(l, -1));

                    case lua.LUA_TTHREAD:
                        IntPtr lthd  = l.tothread(-1);
                        int    refid = l.refer();
                        return(new LuaOnStackThread(lthd, refid));
                    }
                }
            }
            return(null);
        }
コード例 #9
0
 public virtual object[] Resume(params object[] args)
 {
     if (L != IntPtr.Zero)
     {
         using (var lr = new LuaStateRecover(L))
         {
             return(ResumeRaw(args));
         }
     }
     return(null);
 }
コード例 #10
0
        public object[] CallGlobal(string funcname, params object[] args)
        {
            var l = L;

            using (var lr = new LuaStateRecover(l))
            {
                if (!l.GetHierarchicalRaw(lua.LUA_GLOBALSINDEX, funcname))
                {
                    return(null);
                }
                return(l.PushArgsAndCall(args));
            }
        }
コード例 #11
0
 protected internal override object GetFieldImp(object key)
 {
     if (_L != IntPtr.Zero && Refid != 0)
     {
         using (LuaStateRecover lr = new LuaStateRecover(_L))
         {
             _L.getref(Refid);
             _L.PushLua(key);
             _L.rawget(-2);
             return(_L.GetLua(-1));
         }
     }
     DynamicHelper.LogInfo("luatable raw: null ref");
     return(null);
 }
コード例 #12
0
 protected internal override bool SetFieldImp(object key, object val)
 {
     if (_L != IntPtr.Zero && Refid != 0)
     {
         using (LuaStateRecover lr = new LuaStateRecover(_L))
         {
             _L.getref(Refid);
             _L.PushLua(key);
             _L.PushLua(val);
             _L.rawset(-3);
             return(true);
         }
     }
     DynamicHelper.LogInfo("luatable raw: null ref");
     return(false);
 }
コード例 #13
0
        public static bool SetHierarchical(this LuaOnStackUserData ud, string key, object val)
        {
            if (ud == null || ud.L == IntPtr.Zero)
            {
                return(false);
            }
            var l = ud.L;

            using (var lr = new LuaStateRecover(l))
            {
                if (ud.PushToLua())
                {
                    return(SetHierarchical(l, -1, key, val));
                }
                return(false);
            }
        }
コード例 #14
0
 protected internal override object GetFieldImp(object key)
 {
     if (_L != IntPtr.Zero && Refid != 0)
     {
         using (LuaStateRecover lr = new LuaStateRecover(_L))
         {
             _L.getref(Refid);
             _L.PushLua(key);
             _L.gettable(-2);
             return(_L.GetLua(-1));
         }
     }
     if (GLog.IsLogInfoEnabled)
     {
         GLog.LogInfo("luatable: null ref");
     }
     return(null);
 }
コード例 #15
0
        public void CallGlobalVoid(string funcname, params object[] args)
        {
            var l = L;

            using (var lr = new LuaStateRecover(l))
            {
                if (!l.GetHierarchicalRaw(lua.LUA_GLOBALSINDEX, funcname))
                {
                    return;
                }
                if (l.PushArgsAndCallRaw(args) != 0)
                {
                    if (GLog.IsLogErrorEnabled)
                    {
                        GLog.LogError(l.GetLua(-1).UnwrapDynamic());
                    }
                }
            }
        }
コード例 #16
0
 protected internal override bool SetFieldImp(object key, object val)
 {
     if (_L != IntPtr.Zero && Refid != 0)
     {
         using (LuaStateRecover lr = new LuaStateRecover(_L))
         {
             _L.getref(Refid);
             _L.PushLua(key);
             _L.PushLua(val);
             _L.settable(-3);
             return(true);
         }
     }
     if (GLog.IsLogInfoEnabled)
     {
         GLog.LogInfo("luatable: null ref");
     }
     return(false);
 }
コード例 #17
0
        public static LuaThreadRefMan PrepareRefMan(this IntPtr l)
        {
            if (l != IntPtr.Zero)
            {
                using (var lr = new LuaStateRecover(l))
                {
                    l.GetField(lua.LUA_REGISTRYINDEX, "___thdreg");
                    IntPtr   pud = l.touserdata(-1);
                    GCHandle handle;
                    if (pud != IntPtr.Zero)
                    {
                        IntPtr hval = Marshal.ReadIntPtr(pud);
                        handle = (GCHandle)hval;
                        LuaThreadRefMan ud = null;
                        try
                        {
                            ud = handle.Target as LuaThreadRefMan;
                        }
                        catch { }
                        if (ud != null)
                        {
                            return(ud);
                        }
                    }
                    l.pop(1);

                    l.newtable();                  // meta
                    l.pushcfunction(FuncRefManGc); // meta, func
                    l.SetField(-2, "__gc");        // meta

                    LuaThreadRefMan man = new LuaThreadRefMan();
                    pud    = l.newuserdata(new IntPtr(Marshal.SizeOf(typeof(IntPtr)))); // meta, man
                    handle = GCHandle.Alloc(man);
                    Marshal.WriteIntPtr(pud, (IntPtr)handle);

                    l.insert(-2);                                   // man, meta
                    l.setmetatable(-2);                             // man
                    l.SetField(lua.LUA_REGISTRYINDEX, "___thdreg"); // (empty)
                    return(man);
                }
            }
            return(null);
        }
コード例 #18
0
            public void RemoveFromCache(LuaCachedStringInfo info)
            {
                if (info.Node != null)
                {
                    RemoveListNode(info.Node);
                    info.Node = null;
                }
                CacheMap.Remove(info.Str);
                CacheRevMap.Remove(info.Id);

                var id = info.Id;
                var l  = L;

                using (var lr = new LuaStateRecover(l))
                {
                    l.pushbuffer(LuaStringCache.LuaRefEntry); // rkey
                    l.gettable(lua.LUA_REGISTRYINDEX);        // reg
                    if (l.istable(-1))
                    {
                        l.pushnumber(1); // reg 1
                        l.gettable(-2);  // reg map
                        l.pushnumber(2); // reg map 2
                        l.gettable(-3);  // reg map revmap
                        if (l.istable(-2))
                        {
                            l.pushnumber(id); // reg map revmap id
                            l.pushvalue(-1);  // reg map revmap id id
                            l.gettable(-4);   // reg map revmap id str
                            l.pushvalue(-2);  // reg map revmap id str id
                            l.pushnil();      // reg map revmap id str id nil
                            l.settable(-6);   // reg map revmap id str
                            if (l.isstring(-1) && l.istable(-3))
                            {
                                l.pushnil();    // reg map revmap id str nil
                                l.settable(-4); // reg map revmap id
                            }
                        }
                    }
                }
            }
コード例 #19
0
        public void CallGlobalSelfVoid(string objname, string funcname, params object[] args)
        {
            var l = L;

            using (var lr = new LuaStateRecover(l))
            {
                if (!l.GetHierarchicalRaw(lua.LUA_GLOBALSINDEX, objname))
                {
                    return;
                }
                var _self = l.GetLuaOnStack(-1);
                if (!l.GetHierarchicalRaw(-1, funcname))
                {
                    return;
                }
                var paramCnt = 0;
                if (args != null)
                {
                    paramCnt = args.Length;
                }
                var rargs = ObjectPool.GetParamsFromPool(paramCnt + 1);
                rargs[0] = _self;
                for (int i = 0; i < paramCnt; ++i)
                {
                    rargs[i + 1] = args[i];
                }
                if (l.PushArgsAndCallRaw(rargs) != 0)
                {
                    if (GLog.IsLogErrorEnabled)
                    {
                        GLog.LogError(l.GetLua(-1).UnwrapDynamic());
                    }
                }
                ObjectPool.ReturnParamsToPool(rargs);
            }
        }
コード例 #20
0
 public static LuaThreadRefMan GetRefMan(this IntPtr l)
 {
     if (l != IntPtr.Zero)
     {
         using (var lr = new LuaStateRecover(l))
         {
             l.GetField(lua.LUA_REGISTRYINDEX, "___thdreg");
             IntPtr pud = l.touserdata(-1);
             if (pud != IntPtr.Zero)
             {
                 IntPtr          hval   = Marshal.ReadIntPtr(pud);
                 GCHandle        handle = (GCHandle)hval;
                 LuaThreadRefMan ud     = null;
                 try
                 {
                     ud = handle.Target as LuaThreadRefMan;
                 }
                 catch { }
                 return(ud);
             }
         }
     }
     return(null);
 }
コード例 #21
0
        public static Delegate CreateDelegate(Type t, BaseDynamic dyn)
        {
            if (dyn is BaseLua)
            {
                var dynlua = dyn as BaseLua;
                var l      = dynlua.L;
                var refid  = dynlua.Refid;
                if (l != IntPtr.Zero && refid != 0)
                {
                    using (var lr = new LuaStateRecover(l))
                    {
                        l.GetField(lua.LUA_REGISTRYINDEX, "___delreg");
                        if (!l.istable(-1))
                        {
                            l.pop(1);

                            l.newtable();                                   // reg
                            l.newtable();                                   // reg meta
                            l.PushString("k");                              // reg meta 'k'
                            l.SetField(-2, "__mode");                       // reg meta
                            l.setmetatable(-2);                             // reg
                            l.pushvalue(-1);                                // reg reg
                            l.SetField(lua.LUA_REGISTRYINDEX, "___delreg"); // reg
                        }

                        l.getref(refid); // reg func
                        l.gettable(-2);  // reg finfo
                        if (!l.istable(-1))
                        {
                            l.pop(1);        // reg
                            l.newtable();    // reg finfo
                            l.pushvalue(-2); // reg finfo reg
                            l.getref(refid); // reg finfo reg func
                            l.pushvalue(-3); // reg finfo reg func finfo
                            l.settable(-3);  // reg info reg
                            l.pop(1);        //reg info
                        }

                        l.PushLua(t);   // reg finfo dtype
                        l.gettable(-2); // reg finfo del
                        IDelegateDynamicWrapper delwrapper = null;
                        if (l.isuserdata(-1))
                        {
                            var wr = l.GetLua(-1).UnwrapDynamic <WeakReference>();
                            if (wr != null)
                            {
                                delwrapper = wr.GetWeakReference <IDelegateDynamicWrapper>();
                                if (delwrapper == null)
                                {
                                    wr.ReturnToPool();
                                }
                            }
                        }
                        if (delwrapper == null)
                        {
                            delwrapper = CreateDelegateImp(t, dyn);
                            if (delwrapper != null)
                            {
                                var wr = ObjectPool.GetWeakReferenceFromPool(delwrapper);
                                l.pop(1);         // reg finfo
                                l.PushLua(t);     // reg finfo dtype
                                l.PushRawObj(wr); // reg finfo dtype del
                                l.settable(-3);   // reg finfo
                            }
                        }
                        if (delwrapper != null)
                        {
                            return(CreateDelegateFromWrapper(t, delwrapper));
                        }
                    }
                }
            }
            var wrapper = CreateDelegateImp(t, dyn);

            if (wrapper != null)
            {
                return(CreateDelegateFromWrapper(t, wrapper));
            }
            return(null);
        }
コード例 #22
0
        public static string GetString(this IntPtr l, int index)
        {
            string rv = null;

            using (var lr = new LuaStateRecover(l))
            {
                if (l.isstring(index))
                {
                    l.pushvalue(index);                       // lstr
                    l.pushbuffer(LuaStringCache.LuaRefEntry); // lstr rkey
                    l.gettable(lua.LUA_REGISTRYINDEX);        // lstr reg
                    if (l.istable(-1))
                    {
                        l.pushnumber(2); // lstr reg 2
                        l.gettable(-2);  // lstr reg revmap

                        if (l.istable(-1))
                        {
                            l.pushvalue(-3); // lstr reg revmap lstr
                            l.gettable(-2);  // lstr reg revmap id

                            if (l.isnumber(-1))
                            {
                                var id = (int)l.tonumber(-1);
                                if (id != 0)
                                {
                                    l.pushnumber(0); // lstr reg revmap id 0
                                    l.gettable(-4);  // lstr reg revmap id cache
                                    var cache = l.GetRawObj(-1) as LuaStringCache;
                                    if (cache != null)
                                    {
                                        cache.L = l;
                                        LuaStringCache.LuaCachedStringInfo info;
                                        if (cache.TryGetCacheInfo(id, out info))
                                        {
                                            return(info.Str);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (l.isstring(index))
            {
                rv = l.tostring(index);

                l.pushbuffer(LuaStringCache.LuaRefEntry); // rkey
                l.gettable(lua.LUA_REGISTRYINDEX);        // reg
                if (!l.istable(-1))
                {
                    l.pop(1);                                 // X
                    l.newtable();                             // reg
                    l.pushbuffer(LuaStringCache.LuaRefEntry); // reg rkey
                    l.pushvalue(-2);                          // reg rkey reg
                    l.settable(lua.LUA_REGISTRYINDEX);
                }

                l.pushnumber(0); // reg 0
                l.gettable(-2);  // reg cache
                var cache = l.GetUserDataRaw(-1) as LuaStringCache;
                l.pop(1);        // reg
                if (cache == null)
                {
                    l.pushnumber(0); // reg 0
                    cache = new LuaStringCache()
                    {
                        L = l
                    };
                    l.PushRawObj(cache); // reg 0 cache
                    l.settable(-3);      // reg
                }
                else
                {
                    cache.L = l;
                }
                l.pop(1); // X
                cache.PutIntoCache(rv);
            }
            return(rv);
        }
コード例 #23
0
            public LuaCachedStringInfo PutIntoCache(string str)
            {
                if (str == null)
                {
                    return(null);
                }
                LuaCachedStringInfo rv;

                if (TryGetCacheInfo(str, out rv))
                {
                    return(rv);
                }
                if (str.Length > CachedStringMaxLen)
                {
                    return(null);
                }
                rv       = new LuaCachedStringInfo();
                rv.Str   = str;
                rv.Cache = this;
                rv.Id    = ++LastId;

                if (string.IsInterned(str) != null)
                {
                    rv.IsInterned = true;
                }
                else
                {
                    if (CacheList.Count >= CacheMaxCount)
                    {
                        var last = CacheList.Last.Value;
                        RemoveFromCache(last);
                    }
                    rv.Node = CacheList.AddLast(rv);
                }
                CacheMap[str]      = rv;
                CacheRevMap[rv.Id] = rv;
                rv.AddVisitCount();

                var id = rv.Id;
                var l  = L;

                using (var lr = new LuaStateRecover(l))
                {
                    l.pushbuffer(LuaStringCache.LuaRefEntry); // rkey
                    l.gettable(lua.LUA_REGISTRYINDEX);        // reg
                    if (l.istable(-1))
                    {
                        l.pushnumber(1); // reg 1
                        l.gettable(-2);  // reg map
                        if (!l.istable(-1))
                        {
                            l.pop(1);        // reg
                            l.newtable();    // reg map
                            l.pushnumber(1); // reg map 1
                            l.pushvalue(-2); // reg map 1 map
                            l.settable(-4);  // reg map
                        }
                        l.pushnumber(2);     // reg map 2
                        l.gettable(-3);      // reg map revmap
                        if (!l.istable(-1))
                        {
                            l.pop(1);        // reg map
                            l.newtable();    // reg map revmap
                            l.pushnumber(2); // reg map revmap 2
                            l.pushvalue(-2); // reg map revmap 2 revmap
                            l.settable(-5);  // reg map revmap
                        }

                        l.pushnumber(id);  // reg map revmap id
                        l.pushstring(str); // reg map revmap id str
                        l.pushvalue(-1);   // reg map revmap id str str
                        l.pushvalue(-3);   // reg map revmap id str str id
                        l.settable(-5);    // reg map revmap id str
                        l.settable(-4);    // reg map revmap
                    }
                }

                return(rv);
            }