Beispiel #1
0
        public static VMObject CastTo(VMObject srcObj, VMType type)
        {
            if (srcObj.Type == type)
            {
                var result = new VMObject();
                result.Copy(srcObj);
                return(result);
            }

            switch (type)
            {
            case VMType.None:
                return(new VMObject());

            case VMType.String:
            {
                var result = new VMObject();
                result.SetValue(srcObj.AsString());         // TODO does this work for all types?
                return(result);
            }

            case VMType.Timestamp:
            {
                var result = new VMObject();
                result.SetValue(srcObj.AsTimestamp());         // TODO does this work for all types?
                return(result);
            }

            case VMType.Bool:
            {
                var result = new VMObject();
                result.SetValue(srcObj.AsBool());         // TODO does this work for all types?
                return(result);
            }

            case VMType.Bytes:
            {
                var result = new VMObject();
                result.SetValue(srcObj.AsByteArray());         // TODO does this work for all types?
                return(result);
            }

            case VMType.Number:
            {
                var result = new VMObject();
                result.SetValue(srcObj.AsNumber());         // TODO does this work for all types?
                return(result);
            }

            case VMType.Struct:
                switch (srcObj.Type)
                {
                case VMType.Object: return(CastViaReflection(srcObj.Data, 0));

                default: throw new Exception($"invalid cast: {srcObj.Type} to {type}");
                }

            default: throw new Exception($"invalid cast: {srcObj.Type} to {type}");
            }
        }
Beispiel #2
0
        public static VMObject CastTo(VMObject srcObj, VMType type)
        {
            if (srcObj.Type == type)
            {
                var result = new VMObject();
                result.Copy(srcObj);
                return(result);
            }

            switch (type)
            {
            case VMType.None:
                return(new VMObject());

            case VMType.String:
            {
                var result = new VMObject();
                result.SetValue(srcObj.AsString());         // TODO does this work for all types?
                return(result);
            }

            case VMType.Timestamp:
            {
                var result = new VMObject();
                result.SetValue(srcObj.AsTimestamp());         // TODO does this work for all types?
                return(result);
            }

            case VMType.Bool:
                // TODO move this stuff to AsBool()
                switch (srcObj.Type)
                {
                case VMType.Number:
                {
                    var result = new VMObject();
                    result.SetValue(srcObj.AsNumber() != 0);
                    return(result);
                }

                case VMType.String:
                {
                    var result = new VMObject();
                    result.SetValue(!(((string)srcObj.Data).Equals("false", StringComparison.OrdinalIgnoreCase)));
                    return(result);
                }

                default: throw new Exception($"invalid cast: {srcObj.Type} to {type}");
                }

            case VMType.Bytes:
                switch (srcObj.Type)
                {
                case VMType.Bool:
                {
                    var result = new VMObject();
                    result.SetValue(new byte[] { (byte)(srcObj.AsBool() ? 1 : 0) }, VMType.Bytes);
                    return(result);
                }

                case VMType.String:
                {
                    var result = new VMObject();
                    result.SetValue(Encoding.UTF8.GetBytes((string)srcObj.Data), VMType.Bytes);
                    return(result);
                }

                case VMType.Number:
                {
                    var result = new VMObject();
                    result.SetValue(((BigInteger)srcObj.Data).ToSignedByteArray(), VMType.Bytes);
                    return(result);
                }

                default: throw new Exception($"invalid cast: {srcObj.Type} to {type}");
                }

            case VMType.Number:
                switch (srcObj.Type)
                {
                case VMType.Bool:
                {
                    var result = new VMObject();
                    result.SetValue(srcObj.AsBool() ? 1 : 0);
                    return(result);
                }

                case VMType.String:
                {
                    var result = new VMObject();
                    result.SetValue(BigInteger.Parse((string)srcObj.Data));
                    return(result);
                }

                case VMType.Bytes:
                {
                    var result = new VMObject();
                    result.SetValue(BigInteger.FromSignedArray((byte[])srcObj.Data));
                    return(result);
                }

                default: throw new Exception($"invalid cast: {srcObj.Type} to {type}");
                }

            case VMType.Struct:
                switch (srcObj.Type)
                {
                case VMType.Object: return(CastViaReflection(srcObj.Data, 0));

                default: throw new Exception($"invalid cast: {srcObj.Type} to {type}");
                }

            default: throw new Exception($"invalid cast: {srcObj.Type} to {type}");
            }
        }