Exemple #1
0
        public LuaValue get(LuaValue key)
        {
            key = _floatToInteger(key);
            if (key.isInteger())
            {
                var idx = key.toInteger();
                if (idx >= 1 && idx <= arr.Length)
                {
                    return(new LuaValue(arr[idx - 1]));
                }
            }

            return(new LuaValue(_map[key.value]));
        }
Exemple #2
0
        public object get(object key)
        {
            key = _floatToInteger(key);
            if (LuaValue.isInteger(key) && arr != null)
            {
                var idx = LuaValue.toInteger(key);
                if (idx >= 1 && idx <= arr.Length)
                {
                    return(arr[idx - 1]);
                }
            }

            return(_map[key]);
        }
Exemple #3
0
        object _arith(LuaValue a, LuaValue b, Operator op)
        {
            if (op.floatFunc == null)
            {
                Tuple <long, bool> v = LuaValue.convertToInteger(a);
                if (v.Item2)
                {
                    Tuple <long, bool> v2 = LuaValue.convertToInteger(b);
                    if (v2.Item2)
                    {
                        return(op.integerFunc(v.Item1, v2.Item1));
                    }
                }
            }
            else
            {
                if (op.integerFunc != null)
                {
                    if (a.isInteger() && b.isInteger())
                    {
                        var x = a.toInteger();
                        var y = b.toInteger();
                        return(op.integerFunc(x, y));
                    }
                }

                var v = LuaValue.convertToFloat(a);
                if (v.Item2)
                {
                    var v2 = LuaValue.convertToFloat(b);
                    if (v2.Item2)
                    {
                        return(op.floatFunc(v.Item1, v2.Item1));
                    }
                }
            }

            return(null);
        }
Exemple #4
0
        object _arith(object a, object b, Operator op)
        {
            if (op.floatFunc == null)
            {
                var v = LuaValue.convertToInteger(a);
                if (v.Item2)
                {
                    var v2 = LuaValue.convertToInteger(b);
                    if (v2.Item2)
                    {
                        return(op.integerFunc(v.Item1, v2.Item1));
                    }
                }
            }
            else
            {
                if (op.integerFunc != null)
                {
                    if (LuaValue.isInteger(a) && LuaValue.isInteger(b))
                    {
                        var x = LuaValue.toInteger(a);
                        var y = LuaValue.toInteger(b);
                        return(op.integerFunc(x, y));
                    }
                }

                var v = LuaValue.convertToFloat(a);
                if (v.Item2)
                {
                    var v2 = LuaValue.convertToFloat(b);
                    if (v2.Item2)
                    {
                        return(op.floatFunc(v.Item1, v2.Item1));
                    }
                }
            }

            return(null);
        }
Exemple #5
0
        public void put(LuaValue key, LuaValue val)
        {
            if (key == null)
            {
                throw new Exception("table index is nil!");
            }

            if (key.isFloat() && double.IsNaN(key.toFloat()))
            {
                throw new Exception("table index is NaN!");
            }

            key = _floatToInteger(key);

            if (key.isInteger())
            {
                var idx = key.toInteger();
                if (idx >= 1)
                {
                    var arrLen = arr.Length;
                    if (idx <= arrLen)
                    {
                        arr[idx - 1] = val;
                        if (idx == arrLen && val.value == null)
                        {
                            _shrinkArray();
                        }

                        return;
                    }

                    if (idx == arrLen + 1)
                    {
                        _map.Remove(idx);
                        if (val != null)
                        {
                            var b = arr.ToList();
                            b.Add(val);
                            arr = b.ToArray();
                            _expandArray();
                        }

                        return;
                    }
                }
            }

            if (val != null)
            {
                if (_map == null)
                {
                    _map = new Dictionary <object, LuaValue>(8);
                }

                _map.Add(key.value, val);
            }
            else
            {
                _map.Remove(key.value);
            }
        }
Exemple #6
0
        public void put(object key, object val)
        {
            if (key == null)
            {
                throw new Exception("table index is nil!");
            }

            if (LuaValue.isFloat(key) && double.IsNaN(LuaValue.toFloat(key)))
            {
                throw new Exception("table index is NaN!");
            }

            key = _floatToInteger(key);

            if (LuaValue.isInteger(key))
            {
                var idx = LuaValue.toInteger(key);
                if (idx >= 1)
                {
                    var arrLen = arr?.Length ?? 0;
                    if (idx <= arrLen)
                    {
                        arr[idx - 1] = val;
                        if (idx == arrLen && val == null)
                        {
                            _shrinkArray();
                        }

                        return;
                    }

                    if (idx == arrLen + 1)
                    {
                        _map?.Remove(idx);

                        if (val != null)
                        {
                            if (arr == null)
                            {
                                var b = new List <object> {
                                    val
                                };
                                arr = b.ToArray();
                                _expandArray();
                            }
                            else
                            {
                                var b = arr.ToList();
                                b.Add(val);
                                arr = b.ToArray();
                                _expandArray();
                            }
                        }

                        return;
                    }
                }
            }

            if (val != null)
            {
                if (_map == null)
                {
                    _map = new Dictionary <object, object>(8);
                }

                if (!_map.ContainsKey(key))
                {
                    _map.Add(key, val);
                }
                else
                {
                    _map[key] = val;
                }
            }
            else
            {
                _map.Remove(key);
            }
        }