Example #1
0
        public static DynValue concat(ScriptExecutionContext executionContext, CallbackArguments args)
        {
            if (args.Count < 1)
            {
                throw ScriptRuntimeException.BadArgumentValueExpected(0, "concat");
            }

            string toreturn = "";

            if (args.Count == 1)
            {
                toreturn = _castToString(args.RawGet(0, true));
            }
            else if (args.Count == 2)
            {
                toreturn = _castToString(args.RawGet(0, true)) + _castToString(args.RawGet(1, true));
            }
            else if (args.Count == 3)
            {
                toreturn = _castToString(args.RawGet(0, true)) + _castToString(args.RawGet(1, true)) + _castToString(args.RawGet(2, true));
            }
            else if (args.Count == 4)
            {
                toreturn = _castToString(args.RawGet(0, true)) + _castToString(args.RawGet(1, true)) + _castToString(args.RawGet(2, true)) + _castToString(args.RawGet(3, true));
            }
            else
            {
                for (int i = 0; i != args.Count; i++)
                {
                    toreturn += _castToString(args.RawGet(i + 1, true));
                }
            }
            return(DynValue.NewString(toreturn));
        }
Example #2
0
        public static DynValue tonumber(ScriptExecutionContext executionContext, CallbackArguments args)
        {
            if (args.Count < 1)
            {
                throw ScriptRuntimeException.BadArgumentValueExpected(0, "tonumber");
            }

            DynValue e = args[0];
            DynValue b = args.AsType(1, "tonumber", DataType.Number, true);

            if (b.IsNil())
            {
                if (e.Type == DataType.Number)
                {
                    return(e);
                }

                if (e.Type != DataType.String)
                {
                    return(DynValue.Nil);
                }

                double d;
                if (double.TryParse(e.String, NumberStyles.Any, CultureInfo.InvariantCulture, out d))
                {
                    return(DynValue.NewNumber(d));
                }
                return(DynValue.Nil);
            }
            else
            {
                //!COMPAT: tonumber supports only 2,8,10 or 16 as base
                DynValue ee;

                if (args[0].Type != DataType.Number)
                {
                    ee = args.AsType(0, "tonumber", DataType.String, false);
                }
                else
                {
                    ee = DynValue.NewString(args[0].Number.ToString(CultureInfo.InvariantCulture));
                };

                int bb = (int)b.Number;

                if (bb != 2 && bb != 8 && bb != 10 && bb != 16)
                {
                    throw new ScriptRuntimeException("bad argument #2 to 'tonumber' (base out of range)");
                }

                uint uiv = Convert.ToUInt32(ee.String.Trim(), bb);

                return(DynValue.NewNumber(uiv));
            }
        }
Example #3
0
        public static DynValue type(ScriptExecutionContext executionContext, CallbackArguments args)
        {
            if (args.Count < 1)
            {
                throw ScriptRuntimeException.BadArgumentValueExpected(0, "type");
            }

            DynValue v = args[0];

            return(DynValue.NewString(v.Type.ToLuaTypeString()));
        }
Example #4
0
        public static DynValue coalesce(ScriptExecutionContext executionContext, CallbackArguments args)
        {
            if (args.Count < 1)
            {
                throw ScriptRuntimeException.BadArgumentValueExpected(0, "coalesce");
            }

            for (int i = 0; i < args.Count; i++)
            {
                if (args[i].IsNotNil())
                {
                    return(args[i]);
                }
            }

            return(DynValue.NewString(""));
        }
Example #5
0
        public static DynValue tostring(ScriptExecutionContext executionContext, CallbackArguments args)
        {
            if (args.Count < 1)
            {
                throw ScriptRuntimeException.BadArgumentValueExpected(0, "tostring");
            }

            DynValue v = args[0];

            //DynValue tail = executionContext.GetMetamethodTailCall(v, "__tostring", v);

            //if (tail.IsNil())
            return(DynValue.NewString(v.ToPrintString()));

            //tail.TailCallData.Continuation = new CallbackFunction(__tostring_continuation, "__tostring");

            //return tail;
        }
Example #6
0
        public static DynValue tonumber(ScriptExecutionContext executionContext, CallbackArguments args)
        {
            if (args.Count < 1)
            {
                throw ScriptRuntimeException.BadArgumentValueExpected(0, "tonumber");
            }

            DynValue e = args[0];
            DynValue b = args.AsType(1, "tonumber", DataType.Number, true);

            if (b.IsNil())
            {
                if (e.Type == DataType.Number)
                {
                    return(e);
                }

                if (e.Type != DataType.String)
                {
                    return(DynValue.Nil);
                }

                double d;
                if (double.TryParse(e.String, NumberStyles.Any, CultureInfo.InvariantCulture, out d))
                {
                    return(DynValue.NewNumber(d));
                }
                return(DynValue.Nil);
            }
            else
            {
                //!COMPAT: tonumber supports only 2,8,10 or 16 as base
                //UPDATE: added support for 3-9 base numbers
                DynValue ee;

                if (args[0].Type != DataType.Number)
                {
                    ee = args.AsType(0, "tonumber", DataType.String, false);
                }
                else
                {
                    ee = DynValue.NewString(args[0].Number.ToString(CultureInfo.InvariantCulture));
                };

                int bb = (int)b.Number;

                uint uiv = 0;
                if (bb == 2 || bb == 8 || bb == 10 || bb == 16)
                {
                    uiv = Convert.ToUInt32(ee.String.Trim(), bb);
                }
                else if (bb < 10 && bb > 2)             // Support for 3, 4, 5, 6, 7 and 9 based numbers
                {
                    foreach (char digit in ee.String.Trim())
                    {
                        int value = digit - 48;
                        if (value < 0 || value >= bb)
                        {
                            throw new ScriptRuntimeException("bad argument #1 to 'tonumber' (invalid character)");
                        }

                        uiv = (uint)(uiv * bb) + (uint)value;
                    }
                }
                else
                {
                    throw new ScriptRuntimeException("bad argument #2 to 'tonumber' (base out of range)");
                }

                return(DynValue.NewNumber(uiv));
            }
        }
Example #7
0
        public static DynValue tonumber(ScriptExecutionContext executionContext, CallbackArguments args)
        {
            if (args.Count < 1)
            {
                throw ScriptRuntimeException.BadArgumentValueExpected(0, "tonumber");
            }

            DynValue e = args[0];
            DynValue b = args.AsType(1, "tonumber", DataType.Number, true);

            if (b.IsNil())
            {
                if (e.Type == DataType.Number)
                {
                    return(e);
                }

                if (e.Type != DataType.String)
                {
                    return(DynValue.Nil);
                }

                double?res = e.CastToNumber();
                if (res != null)
                {
                    return(DynValue.NewNumber(res.Value));
                }
                return(DynValue.Nil);
            }
            else
            {
                DynValue ee;

                if (args[0].Type != DataType.Number)
                {
                    ee = args.AsType(0, "tonumber", DataType.String, false);
                }
                else
                {
                    ee = DynValue.NewString(args[0].Number.ToString(CultureInfo.InvariantCulture));
                };
                if (ee.String.Length < 0)
                {
                    return(DynValue.Nil);
                }
                int bb = (int)b.Number;

                double uiv     = 0;
                var    trimmed = ee.String.Trim();
                if (trimmed.Length == 0)
                {
                    return(DynValue.Nil);
                }
                bool negate   = false;
                int  startIdx = 0;
                if (trimmed[0] == '-')
                {
                    negate   = true;
                    startIdx = 1;
                }
                if (trimmed[0] == '+')
                {
                    startIdx = 1;
                }
                if (bb <= 36 && bb > 1)
                {
                    for (int ij = startIdx; ij < trimmed.Length; ij++)
                    {
                        char ch = trimmed[ij];
                        int  value;
                        if (ch >= 97)
                        {
                            value = ch - 87;
                        }
                        else if (ch >= 65)
                        {
                            value = ch - 55;
                        }
                        else
                        {
                            value = ch - 48;
                        }
                        if (value < 0 || value >= bb)
                        {
                            return(DynValue.Nil);
                        }
                        uiv = uiv * bb + value;
                    }
                }
                else
                {
                    throw new ScriptRuntimeException("bad argument #2 to 'tonumber' (base out of range)");
                }

                return(DynValue.NewNumber(negate ? -uiv : uiv));
            }
        }