Example #1
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);
            }
        }
Example #2
0
        public object getMetafield(object val, string fieldName, LuaState ls)
        {
            var mt = LuaValue.getMetatable(val, ls);

            return(mt?.get(fieldName));
        }
Example #3
0
        public LuaType GetI(int idx, long i)
        {
            var t = new LuaValue(stack.get(idx)).toLuaTable();

            return(getTable(new LuaValue(t), new LuaValue(i)));
        }
Example #4
0
        public (long, bool) ToIntegerX(int idx)
        {
            var val = stack.get(idx);

            return(LuaValue.convertToInteger(val));
        }
Example #5
0
        public (double, bool) ToNumberX(int idx)
        {
            var val = stack.get(idx);

            return(LuaValue.convertToFloat(val));
        }
Example #6
0
        public Tuple <double, bool> ToNumberX(int idx)
        {
            var val = stack.get(idx);

            return(LuaValue.convertToFloat(new LuaValue(val)));
        }