Ejemplo n.º 1
0
 public static LuaValueEnum TypeOf(Object val)
 {
     if (val == null)
     {
         return(LuaValueEnum.LUA_TNIL);
     }
     else if (TypeExtension.TypeEqual <bool>(val))
     {
         return(LuaValueEnum.LUA_TBOOLEAN);
     }
     else if (TypeExtension.TypeEqual <long>(val) ||
              TypeExtension.TypeEqual <double>(val))
     {
         return(LuaValueEnum.LUA_TNUMBER);
     }
     else if (TypeExtension.TypeEqual <string>(val))
     {
         return(LuaValueEnum.LUA_TSTRING);
     }
     else if (TypeExtension.TypeEqual <Closure>(val))
     {
         return(LuaValueEnum.LUA_TFUNCTION);
     }
     else
     {
         return(LuaValueEnum.LUA_TNIL);
     }
 }
Ejemplo n.º 2
0
 void setTable(Object t, Object k, Object v)
 {
     if (TypeExtension.TypeEqual <LuaTable>(t))
     {
         ((LuaTable)t).Put(k, v);
         return;
     }
 }
Ejemplo n.º 3
0
 LuaValueEnum getTable(Object t, Object k)
 {
     if (TypeExtension.TypeEqual <LuaTable>(t))
     {
         Object v = ((LuaTable)t).Get(k);
         stack.Push(v);
         return(LuaValue.TypeOf(v));
     }
     throw new Exception("");
 }
Ejemplo n.º 4
0
        public void Put(Object key, Object val)
        {
            key = floatToInt(key);
            if (TypeExtension.TypeEqual <long>(key))
            {
                int idx = Convert.ToInt32((long)key);
                if (idx >= 1)
                {
                    if (arr == null)
                    {
                        arr = new List <Object>();
                    }

                    int arrLen = arr.Count;
                    if (idx <= arrLen)
                    {
                        arr[idx - 1] = val;
                        if (idx == arrLen && val == null)
                        {
                            shrinkArray();
                        }
                        return;
                    }
                    if (idx == arrLen + 1)
                    {
                        if (map != null)
                        {
                            map.Remove(key);
                        }
                        if (val != null)
                        {
                            arr.Add(val);
                            expandArray();
                        }
                        return;
                    }
                }
            }

            if (val != null)
            {
                if (map == null)
                {
                    map = new Dictionary <object, object>();
                }
                map.Add(key, val);
            }
            else
            {
                if (map != null)
                {
                    map.Remove(key);
                }
            }
        }
Ejemplo n.º 5
0
 private Object floatToInt(Object key)
 {
     if (TypeExtension.TypeEqual <double>(key))
     {
         double f = (double)key;
         if (LuaNumber.IsInteger(f))
         {
             return(Convert.ToInt64(f));
         }
     }
     return(key);
 }
Ejemplo n.º 6
0
 public Object Get(Object key)
 {
     key = floatToInt(key);
     if (arr != null && TypeExtension.TypeEqual <long>(key))
     {
         int idx = Convert.ToInt32((long)key);
         if (idx >= 1 && idx <= arr.Count)
         {
             return(arr[idx - 1]);
         }
     }
     return(map != null && map.ContainsKey(key) ? map[key] : null);
 }
Ejemplo n.º 7
0
        public void Call(int nArgs, int nResults)
        {
            Object val = stack.Get(-(nArgs + 1));

            if (TypeExtension.TypeEqual <Closure>(val))
            {
                Closure c = (Closure)val;
                Console.WriteLine("");
                callLuaClosure(nArgs, nResults, c);
            }
            else
            {
                throw new Exception("val is not a Closure");
            }
        }
Ejemplo n.º 8
0
 public static bool ToBoolean(Object val)
 {
     if (val == null)
     {
         return(false);
     }
     else if (TypeExtension.TypeEqual <bool>(val))
     {
         return((bool)val);
     }
     else
     {
         return(true);
     }
 }
Ejemplo n.º 9
0
        public double?ToNumberX(int idx)
        {
            Object val = stack.Get(idx);

            if (TypeExtension.TypeEqual <double>(val))
            {
                return((double)val);
            }
            else if (TypeExtension.TypeEqual <long>(val))
            {
                return(Convert.ToDouble(((long)val)));
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 10
0
        public string ToString(int idx)
        {
            Object val = stack.Get(idx);

            if (TypeExtension.TypeEqual <string>(val))
            {
                return((string)val);
            }
            else if (TypeExtension.TypeEqual <long>(val) ||
                     TypeExtension.TypeEqual <double>(val))
            {
                return(val.ToString());
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 11
0
 public static double?ToFloat(Object val)
 {
     if (TypeExtension.TypeEqual <double>(val))
     {
         return((double)val);
     }
     else if (TypeExtension.TypeEqual <long>(val))
     {
         return(Convert.ToDouble(((long)val)));
     }
     else if (TypeExtension.TypeEqual <string>(val))
     {
         return(LuaNumber.ParseDouble((string)val));
     }
     else
     {
         return(null);
     }
 }
Ejemplo n.º 12
0
 public static long?ToInteger(Object val)
 {
     if (TypeExtension.TypeEqual <long>(val))
     {
         return((long)val);
     }
     else if (TypeExtension.TypeEqual <double>(val))
     {
         double n = (double)val;
         return(LuaNumber.IsInteger(n) ? (long?)n : null);
     }
     else if (TypeExtension.TypeEqual <string>(val))
     {
         return(ToInteger((string)val));
     }
     else
     {
         return(null);
     }
 }
Ejemplo n.º 13
0
        public static Object Arith(Object a, Object b, ArithOpEnum op)
        {
            LongBinaryOperator   integerFunc = integerOps[(int)op];
            DoubleBinaryOperator floatFunc   = floatOps[(int)op];

            if (floatFunc == null)
            {
                long?x = LuaValue.ToInteger(a);
                if (x != null)
                {
                    long?y = LuaValue.ToInteger(b);
                    if (y != null)
                    {
                        return(integerFunc((long)x, (long)y));
                    }
                }
            }
            else
            {
                if (integerFunc != null)
                {
                    if (TypeExtension.TypeEqual <long>(a) &&
                        TypeExtension.TypeEqual <long>(b))
                    {
                        return(integerFunc((long)a, (long)b));
                    }
                }
                double?x = LuaValue.ToFloat(a);
                if (x != null)
                {
                    double?y = LuaValue.ToFloat(b);
                    if (y != null)
                    {
                        return(floatFunc((double)x, (double)y));
                    }
                }
            }
            return(null);
        }
Ejemplo n.º 14
0
 public bool IsInteger(int idx)
 {
     return(TypeExtension.TypeEqual <int>(stack.Get(idx)));
 }