Exemplo n.º 1
0
        /// <summary>
        /// 执行算术和按位运算
        /// </summary>
        public void Arith(EArithOp op)
        {
            var b = _stack.Pop();
            var a = b;

            if (op != EArithOp.Unm && op != EArithOp.BNot)
            {
                a = _stack.Pop();
            }

            var o   = Operators.Ops[(int)op];
            var ret = InnerArith(a, b, o);

            if (ret != null)
            {
                _stack.Push(ret);
                return;
            }

            var mm = Operators.Ops[(int)op].MetaMethod;

            if (LuaValue.CallMetaMethod(a, b, mm, this, out ret))
            {
                _stack.Push(ret);
                return;
            }

            Debug.Panic("arithmetic error!");
        }
Exemplo n.º 2
0
        /// <summary>
        /// 执行字符串拼接运算
        /// </summary>
        public void Concat(int n)
        {
            if (n == 0)
            {
                _stack.Push("");
            }
            else if (n >= 2)
            {
                for (var i = 1; i < n; i++)
                {
                    if (IsString(-1) && IsString(-2))
                    {
                        var s2 = ToString(-1);
                        var s1 = ToString(-2);
                        _stack.Pop();
                        _stack.Pop();
                        _stack.Push(s1 + s2);
                        continue;
                    }

                    var b = _stack.Pop();
                    var a = _stack.Pop();
                    if (LuaValue.CallMetaMethod(a, b, "__concat", this, out var metaMethodRet))
                    {
                        _stack.Push(metaMethodRet);
                        continue;
                    }

                    Debug.Panic("concatenation error!");
                }
            }
        }
Exemplo n.º 3
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());
            }
        }
Exemplo n.º 4
0
        private bool Lt(LuaValue a, LuaValue b, LuaState ls)
        {
            if (a.IsString())
            {
                return(b.IsString() && string.Compare(a.GetStrValue(), b.GetStrValue(), StringComparison.Ordinal) < 0);
            }
            else if (a.IsNumber() && b.IsNumber())
            {
                return(a.GetFloatValue() < b.GetFloatValue());
            }

            if (LuaValue.CallMetaMethod(a, b, "__lt", ls, out var metaMethodRet))
            {
                return(metaMethodRet.ToBoolean());
            }

            Debug.Panic("comparison error!");
            return(false);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 访问指定索引处的值,取其长度,然后推入栈顶
        /// </summary>
        /// <param name="idx"></param>
        public void Len(int idx)
        {
            var val = _stack[idx];

            if (val.IsString())
            {
                _stack.Push((LuaInt)val.GetStrValue().Length);
            }
            else if (LuaValue.CallMetaMethod(val, val, "__len", this, out var metaMethodRet))
            {
                _stack.Push(metaMethodRet);
            }
            else if (val.IsTable())
            {
                _stack.Push((LuaInt)val.GetTableValue().Len());
            }
            else
            {
                Debug.Panic("length error!");
            }
        }