Esempio n. 1
0
        Expression FieldValue(IParser parser, Expression var, Expression valueType, Type schemaType, bool initialize)
        {
            Expression body;

            if (schemaType.IsBondStruct() && var.Type.IsValueType())
            {
                // Special handling for properties of struct types: we deserialize into
                // a temp variable and then assign the value to the property.
                var temp = Expression.Variable(var.Type, $"{var.Type.Name}_temp");
                body = Expression.Block(
                    new[] { temp },
                    Value(parser, temp, valueType, schemaType, true),
                    Expression.Assign(var, temp));
            }
            else
            {
                body = Value(parser, var, valueType, schemaType, initialize);
            }

            if (schemaType.IsBondContainer() || schemaType.IsBondStruct() || schemaType.IsBondNullable())
            {
                var expectedType = Expression.Constant(schemaType.GetBondDataType());
                return(PrunedExpression.IfThenElse(
                           Expression.Equal(valueType, expectedType),
                           body,
                           ThrowExpression.InvalidTypeException(expectedType, valueType)));
            }

            return(body);
        }
Esempio n. 2
0
 public static Expression While(Expression whileCondition, Expression body, LabelTarget breakLabel)
 {
     return(Expression.Loop(
                PrunedExpression.IfThenElse(
                    whileCondition,
                    body,
                    Expression.Break(breakLabel)),
                breakLabel));
 }
Esempio n. 3
0
        Expression Container(IParser parser, RuntimeSchema schema)
        {
            var expectedValueType = schema.HasValue ? schema.TypeDef.element.id : (BondDataType?)null;

            return(parser.Container(expectedValueType,
                                    (valueParser, elementType, next, count, arraySegment) =>
            {
                var body = ControlExpression.While(next,
                                                   Expression.Block(
                                                       writer.WriteItemBegin(),
                                                       schema.HasValue ?
                                                       Value(valueParser, elementType, schema.GetElementSchema()) :
                                                       Value(valueParser, elementType),
                                                       writer.WriteItemEnd()));

                var blob = parser.Blob(count);
                if ((blob != null) || (arraySegment != null))
                {
                    body = PrunedExpression.IfThenElse(
                        Expression.Equal(elementType, Expression.Constant(BondDataType.BT_INT8)),
                        writer.WriteBytes(arraySegment ?? blob),
                        body);

                    // For binary protocols we can write blob directly using protocols's WriteBytes
                    // even if the container is not a blob (blob is BT_LIST of BT_INT8).
                    if (binaryWriter)
                    {
                        body = PrunedExpression.IfThenElse(
                            Expression.Equal(elementType, Expression.Constant(BondDataType.BT_UINT8)),
                            writer.WriteBytes(arraySegment ?? blob),
                            body);
                    }
                }

                return Expression.Block(
                    writer.WriteContainerBegin(count, elementType),
                    body,
                    writer.WriteContainerEnd());
            }));
        }