Example #1
0
        private bool Eq(LuaValue a, LuaValue b, LuaState ls)
        {
            if (a.IsNil())
            {
                return(b.IsNil());
            }
            else if (a.IsBool())
            {
                return(b.IsBool() && a.GetBoolValue() == b.GetBoolValue());
            }
            else if (a.IsString())
            {
                return(b.IsString() && a.GetStrValue() == b.GetStrValue());
            }
            else if (a.IsInt())
            {
                if (b.IsInt())
                {
                    return(a.GetIntValue() == b.GetIntValue());
                }
                else if (b.IsFloat())
                {
                    return(a.GetIntValue() == b.GetFloatValue());
                }
                else
                {
                    return(false);
                }
            }
            else if (a.IsFloat())
            {
                if (b.IsFloat())
                {
                    return(a.GetFloatValue() == b.GetFloatValue());
                }
                else if (b.IsInt())
                {
                    return(a.GetFloatValue() == b.GetIntValue());
                }
                else
                {
                    return(false);
                }
            }
            else if (a.IsTable() &&
                     b.IsTable() &&
                     a.GetTableValue() != b.GetTableValue() && ls != null)
            {
                if (LuaValue.CallMetaMethod(a, b, "__eq", ls, out var metaMethodRet))
                {
                    return(metaMethodRet.ToBoolean());
                }

                return(a.GetValue() == b.GetValue());
            }
            else
            {
                return(a.GetValue() == b.GetValue());
            }
        }
Example #2
0
        private void InnerSetTable(LuaValue t, LuaValue k, LuaValue v, bool raw)
        {
            if (t.IsTable())
            {
                var table = t.GetTableValue();
                if (raw || !table.Get(k).IsNil() || !table.HasMetaField("__newindex"))
                {
                    table.Put(k, v);
                    return;
                }
            }

            if (!raw)
            {
                var mf = LuaValue.GetMetaField(t, "__newindex", this);
                if (mf != null)
                {
                    if (mf.IsTable())
                    {
                        InnerSetTable(mf, k, v, false);
                    }
                    else if (mf.IsFunction())
                    {
                        _stack.Push(mf);
                        _stack.Push(t);
                        _stack.Push(k);
                        _stack.Push(v);
                        Call(3, 0);
                        return;
                    }
                }
            }

            Debug.Panic("not a table!");
        }
Example #3
0
        public static LuaTable GetMetaTable(LuaValue val, LuaState ls)
        {
            if (val.IsTable())
            {
                return((val.GetTableValue()).MetaTable);
            }

            var key = $"_MT{val.Type.GetParentType()}";
            var mt  = ls.Registry.Get(key);

            return(mt?.GetTableValue());
        }
Example #4
0
        public static void SetMetaTable(LuaValue val, LuaTable mt, LuaState ls)
        {
            if (val.IsTable())
            {
                var lt = val.GetTableValue();
                lt.MetaTable = mt;
                return;
            }

            var key = $"_MT{val.Type.GetParentType()}";

            ls.Registry.Put(key, mt);
        }
Example #5
0
        private ELuaType InnerGetTable(LuaValue t, LuaValue k, bool raw)
        {
            if (t.IsTable())
            {
                var table = t.GetTableValue();
                var v     = table.Get(k);

                if (raw || !v.IsNil() || !table.HasMetaField("__index"))
                {
                    _stack.Push(v);
                    return(v.Type);
                }
            }

            if (!raw)
            {
                var mf = LuaValue.GetMetaField(t, "__index", this);
                if (mf != null)
                {
                    if (mf.IsTable())
                    {
                        return(InnerGetTable(mf, k, false));
                    }

                    if (mf.IsFunction())
                    {
                        _stack.Push(mf);
                        _stack.Push(t);
                        _stack.Push(k);
                        Call(2, 1);
                        var v = _stack.Get(-1);
                        return(v.Type);
                    }
                }
            }

            Debug.Panic("index error!");
            return(ELuaType.None);
        }