Beispiel #1
0
        private static Value ParseJsonNumber(VmGlobals globals, char[] rawValue, int length, Index i)
        {
            int  sign = 1;
            char c    = rawValue[i.Value];

            if (c == '-')
            {
                sign = -1;
                i.Value++;
            }
            StringBuilder sb = new StringBuilder();

            while (i.Value < length)
            {
                c = rawValue[i.Value++];

                if (c != '.' && (c < '0' || c > '9'))
                {
                    i.Value--;
                    break;
                }
                else
                {
                    sb.Append(c);
                }
            }

            string stringValue = sb.ToString();

            if (stringValue.Contains('.'))
            {
                double value;
                if (double.TryParse(stringValue, out value))
                {
                    return(CrayonWrapper.buildFloat(globals, value * sign));
                }
            }
            else
            {
                int value;
                if (int.TryParse(stringValue, out value))
                {
                    return(CrayonWrapper.buildInteger(globals, value * sign));
                }
            }

            throw new JsonParserException();
        }
Beispiel #2
0
        private Value ConvertNativeArg(object na)
        {
            if (na == null)
            {
                return(vm.globalNull);
            }
            if (na is Value)
            {
                return((Value)na);             // allow mixed native/Value
            }
            if (na is bool)
            {
                return((bool)na ? vm.globalTrue : vm.globalFalse);
            }
            if (na is string)
            {
                return(CrayonWrapper.buildString(vm.globals, (string)na));
            }
            if (na is int)
            {
                return(CrayonWrapper.buildInteger(vm.globals, (int)na));
            }
            if (na is double)
            {
                return(CrayonWrapper.buildFloat(vm.globals, (double)na));
            }
            if (na is float)
            {
                return(CrayonWrapper.buildFloat(vm.globals, (float)na));
            }
            if (na is object[])
            {
                List <Value> list = new List <Value>(((object[])na).Select(a => ConvertNativeArg(a)));
                return(CrayonWrapper.buildList(list));
            }

            throw new NotImplementedException("Unsupported type for native arg: " + na.GetType());
        }