Ejemplo n.º 1
0
 public Option <DeserializeInfo <B> > deserialize(byte[] serialized, int startIndex)
 {
     try {
         var a1Opt = a1RW.deserialize(serialized, startIndex);
         if (a1Opt.isNone)
         {
             return(Option <DeserializeInfo <B> > .None);
         }
         var a1Info = a1Opt.__unsafeGetValue;
         var a2Opt  = a2RW.deserialize(serialized, startIndex + a1Info.bytesRead);
         if (a2Opt.isNone)
         {
             return(Option <DeserializeInfo <B> > .None);
         }
         var a2Info = a2Opt.__unsafeGetValue;
         var a3Opt  = a3RW.deserialize(serialized, startIndex + a1Info.bytesRead + a2Info.bytesRead);
         if (a3Opt.isNone)
         {
             return(Option <DeserializeInfo <B> > .None);
         }
         var a3Info = a3Opt.__unsafeGetValue;
         var info   = new DeserializeInfo <B>(
             mapper(a1Info.value, a2Info.value, a3Info.value),
             a1Info.bytesRead + a2Info.bytesRead + a3Info.bytesRead
             );
         return(F.some(info));
     }
     catch (Exception) { return(Option <DeserializeInfo <B> > .None); }
 }
        private static LocalBuilder EmitNewObject(ILGenerator il, Type type, UnionSerializationInfo info, DeserializeInfo[] members)
        {
            if (info.IsClass)
            {
                foreach (UnionSerializationInfo.EmittableMember item in info.MethodParameters)
                {
                    DeserializeInfo local = members.First(x => x.MemberInfo == item);
                    il.EmitLdloc(local.LocalField);
                }

                il.Emit(OpCodes.Call, info.NewMethod);

                return(null);
            }
            else
            {
                LocalBuilder result = il.DeclareLocal(type);
                foreach (var item in info.MethodParameters)
                {
                    var local = members.First(x => x.MemberInfo == item);
                    il.EmitLdloc(local.LocalField);
                }

                il.Emit(OpCodes.Call, info.NewMethod);
                il.Emit(OpCodes.Stloc, result);

                return(result); // struct returns local result field
            }
        }
Ejemplo n.º 3
0
        static void EmitDeserializeValue(ILGenerator il, DeserializeInfo info)
        {
            var member = info.MemberInfo;
            var t      = member.Type;

            if (MessagePackBinary.IsMessagePackPrimitive(t))
            {
                il.EmitLdarg(1);
                il.EmitLdarg(2);
                il.EmitLdarg(4);
                if (t == typeof(byte[]))
                {
                    il.EmitCall(MessagePackBinaryTypeInfo.ReadBytes);
                }
                else
                {
                    il.EmitCall(MessagePackBinaryTypeInfo.TypeInfo.GetDeclaredMethod("Read" + t.Name));
                }
            }
            else
            {
                il.EmitLdarg(3);
                il.EmitCall(getFormatterWithVerify.MakeGenericMethod(t));
                il.EmitLdarg(1);
                il.EmitLdarg(2);
                il.EmitLdarg(3);
                il.EmitLdarg(4);
                il.EmitCall(getDeserialize(t));
            }

            il.EmitStloc(info.LocalField);
        }
Ejemplo n.º 4
0
        static void EmitDeserializeValue(ILGenerator il, DeserializeInfo info)
        {
            var member = info.MemberInfo;
            var t      = member.Type;

            if (IsOptimizeTargetType(t))
            {
                il.EmitLdarg(1);
                il.EmitLdarg(2);
                il.EmitLdarg(4);
                if (t == typeof(byte[]))
                {
                    il.EmitCall(MessagePackBinaryTypeInfo.ReadBytes);
                }
                else
                {
                    il.EmitCall(MessagePackBinaryTypeInfo.TypeInfo.GetDeclaredMethods("Read" + t.Name).OrderByDescending(x => x.GetParameters().Length).First());
                }
            }
            else
            {
                il.EmitLdarg(3);
                il.EmitCall(getFormatterWithVerify.MakeGenericMethod(t));
                il.EmitLdarg(1);
                il.EmitLdarg(2);
                il.EmitLdarg(3);
                il.EmitLdarg(4);
                il.EmitCall(getDeserialize(t));
            }

            il.EmitStloc(info.LocalField);
        }
Ejemplo n.º 5
0
 public Either <string, DeserializeInfo <B> > deserialize(byte[] serialized, int startIndex)
 {
     try {
         var a1Opt = a1RW.deserialize(serialized, startIndex);
         if (a1Opt.leftValueOut(out var a1Err))
         {
             return($"{nameof(AndRW3<A1, A2, A3, B>)} a1 failed: {a1Err}");
         }
         var a1Info = a1Opt.__unsafeGetRight;
         var a2Opt  = a2RW.deserialize(serialized, startIndex + a1Info.bytesRead);
         if (a2Opt.leftValueOut(out var a2Err))
         {
             return($"{nameof(AndRW3<A1, A2, A3, B>)} a2 failed: {a2Err}");
         }
         var a2Info = a2Opt.__unsafeGetRight;
         var a3Opt  = a3RW.deserialize(serialized, startIndex + a1Info.bytesRead + a2Info.bytesRead);
         if (a3Opt.leftValueOut(out var a3Err))
         {
             return($"{nameof(AndRW3<A1, A2, A3, B>)} a3 failed: {a3Err}");
         }
         var a3Info = a3Opt.__unsafeGetRight;
         var info   = new DeserializeInfo <B>(
             mapper(a1Info.value, a2Info.value, a3Info.value),
             a1Info.bytesRead + a2Info.bytesRead + a3Info.bytesRead
             );
         return(info);
     }
     catch (Exception e) {
         return($"{nameof(AndRW3<A1, A2, A3, B>)} threw {e}");
     }
 }
        static void EmitDeserializeValue(
            ILGenerator il,
            DeserializeInfo info,
            int index,
            ArgumentField argReader,
            ArgumentField argOptions,
            LocalBuilder localResolver
            )
        {
            Label storeLabel = il.DefineLabel();

            UnionSerializationInfo.EmittableMember member = info.MemberInfo;
            Type t = member.Type;

            if (IsOptimizeTargetType(t))
            {
                if (!t.GetTypeInfo().IsValueType)
                {
                    // As a nullable type (e.g. byte[] and string) we need to first call TryReadNil
                    // if (reader.TryReadNil())
                    Label readNonNilValueLabel = il.DefineLabel();
                    argReader.EmitLdarg();
                    il.EmitCall(MessagePackReaderTypeInfo.TryReadNil);
                    il.Emit(OpCodes.Brfalse_S, readNonNilValueLabel);
                    il.Emit(OpCodes.Ldnull);
                    il.Emit(OpCodes.Br, storeLabel);

                    il.MarkLabel(readNonNilValueLabel);
                }

                argReader.EmitLdarg();
                if (t == typeof(byte[]))
                {
                    LocalBuilder local = il.DeclareLocal(typeof(ReadOnlySequence <byte>?));
                    il.EmitCall(MessagePackReaderTypeInfo.ReadBytes);
                    il.EmitStloc(local);
                    il.EmitLdloca(local);
                    il.EmitCall(ArrayFromNullableReadOnlySequence);
                }
                else
                {
                    il.EmitCall(MessagePackReaderTypeInfo.TypeInfo.GetDeclaredMethods("Read" + t.Name).First(x => x.GetParameters().Length == 0));
                }
            }
            else
            {
                il.EmitLdloc(localResolver);
                il.EmitCall(getFormatterWithVerify.MakeGenericMethod(t));
                argReader.EmitLdarg();
                argOptions.EmitLoad();
                il.EmitCall(getDeserialize(t));
            }

            il.MarkLabel(storeLabel);
            il.EmitStloc(info.LocalField);
        }
Ejemplo n.º 7
0
        public Either <string, DeserializeInfo <B> > deserialize(byte[] serialized, int startIndex)
        {
            const string rwName = nameof(AndRW4 <A1, A2, A3, A4, B>);
            var          step   = "a1";

            try {
                var a1Opt = a1RW.deserialize(serialized, startIndex);
                if (a1Opt.leftValueOut(out var a1Err))
                {
                    return($"{rwName} a1 failed: {a1Err}");
                }
                var a1Info = a1Opt.__unsafeGetRight;

                step = "a2";
                var a2Opt = a2RW.deserialize(serialized, startIndex + a1Info.bytesRead);
                if (a2Opt.leftValueOut(out var a2Err))
                {
                    return($"{rwName} a2 failed: {a2Err}");
                }
                var a2Info = a2Opt.__unsafeGetRight;

                step = "a3";
                var a3Opt = a3RW.deserialize(serialized, startIndex + a1Info.bytesRead + a2Info.bytesRead);
                if (a3Opt.leftValueOut(out var a3Err))
                {
                    return($"{rwName} a3 failed: {a3Err}");
                }
                var a3Info = a3Opt.__unsafeGetRight;

                step = "a4";
                var a4Opt = a4RW.deserialize(
                    serialized,
                    startIndex + a1Info.bytesRead + a2Info.bytesRead + a3Info.bytesRead
                    );
                if (a4Opt.leftValueOut(out var a4Err))
                {
                    return($"{rwName} a4 failed: {a4Err}");
                }
                var a4Info = a4Opt.__unsafeGetRight;

                step = "mapper";
                var info = new DeserializeInfo <B>(
                    mapper(a1Info.value, a2Info.value, a3Info.value, a4Info.value),
                    a1Info.bytesRead + a2Info.bytesRead + a3Info.bytesRead + a4Info.bytesRead
                    );
                return(info);
            }
            catch (Exception e) {
                return($"{nameof(rwName)} at index {startIndex}, step {step} threw {e}");
            }
        }
Ejemplo n.º 8
0
        public static Option <DeserializeInfo <B> > deserialize(
            IDeserializer <A> aDeserializer, Fn <A, Option <B> > mapper,
            byte[] serialized, int startIndex
            )
        {
            var aInfoOpt = aDeserializer.deserialize(serialized, startIndex);

            if (aInfoOpt.isNone)
            {
                return(Option <DeserializeInfo <B> > .None);
            }
            var aInfo = aInfoOpt.get;
            var bOpt  = mapper(aInfo.value);

            if (bOpt.isNone)
            {
                return(Option <DeserializeInfo <B> > .None);
            }
            var bInfo = new DeserializeInfo <B>(bOpt.get, aInfo.bytesRead);

            return(F.some(bInfo));
        }
Ejemplo n.º 9
0
        public static Either <string, DeserializeInfo <B> > deserialize(
            IDeserializer <A> aDeserializer, Func <A, Either <string, B> > mapper,
            byte[] serialized, int startIndex
            )
        {
            var aInfoEither = aDeserializer.deserialize(serialized, startIndex);

            if (aInfoEither.leftValueOut(out var aErr))
            {
                return(aErr);
            }
            var aInfo   = aInfoEither.__unsafeGetRight;
            var bEither = mapper(aInfo.value);

            if (bEither.leftValueOut(out var bErr))
            {
                return($"mapped deserializer failed in mapper: {bErr}");
            }
            var bInfo = new DeserializeInfo <B>(bEither.__unsafeGetRight, aInfo.bytesRead);

            return(bInfo);
        }
Ejemplo n.º 10
0
        public Either <string, DeserializeInfo <B> > deserialize(byte[] serialized, int startIndex)
        {
            const string rwName = nameof(AndRW7 <A1, A2, A3, A4, A5, A6, A7, B>);
            var          step   = "a1";

            try {
                var a1Opt = a1RW.deserialize(serialized, startIndex);
                if (a1Opt.leftValueOut(out var a1Err))
                {
                    return($"{rwName} a1 failed: {a1Err}");
                }
                var a1Info = a1Opt.__unsafeGetRight;

                step = "a2";
                var a2Opt = a2RW.deserialize(serialized, startIndex + a1Info.bytesRead);
                if (a2Opt.leftValueOut(out var a2Err))
                {
                    return($"{rwName} a2 failed: {a2Err}");
                }
                var a2Info = a2Opt.__unsafeGetRight;

                step = "a3";
                var a3Opt = a3RW.deserialize(serialized, startIndex + a1Info.bytesRead + a2Info.bytesRead);
                if (a3Opt.leftValueOut(out var a3Err))
                {
                    return($"{rwName} a3 failed: {a3Err}");
                }
                var a3Info = a3Opt.__unsafeGetRight;

                step = "a4";
                var a4Opt = a4RW.deserialize(
                    serialized,
                    startIndex + a1Info.bytesRead + a2Info.bytesRead + a3Info.bytesRead
                    );
                if (a4Opt.leftValueOut(out var a4Err))
                {
                    return($"{rwName} a4 failed: {a4Err}");
                }
                var a4Info = a4Opt.__unsafeGetRight;

                step = "a5";
                var a5Opt = a5RW.deserialize(
                    serialized,
                    startIndex + a1Info.bytesRead + a2Info.bytesRead + a3Info.bytesRead + a4Info.bytesRead
                    );
                if (a5Opt.leftValueOut(out var a5Err))
                {
                    return($"{rwName} a5 failed: {a5Err}");
                }
                var a5Info = a5Opt.__unsafeGetRight;

                step = "a6";
                var a6Opt = a6RW.deserialize(
                    serialized,
                    startIndex + a1Info.bytesRead + a2Info.bytesRead + a3Info.bytesRead + a4Info.bytesRead +
                    a5Info.bytesRead
                    );
                if (a6Opt.leftValueOut(out var a6Err))
                {
                    return($"{rwName} a6 failed: {a6Err}");
                }
                var a6Info = a6Opt.__unsafeGetRight;

                step = "a7";
                var a7Opt = a7RW.deserialize(
                    serialized,
                    startIndex + a1Info.bytesRead + a2Info.bytesRead + a3Info.bytesRead + a4Info.bytesRead +
                    a5Info.bytesRead + a6Info.bytesRead
                    );
                if (a7Opt.leftValueOut(out var a7Err))
                {
                    return($"{rwName} a7 failed: {a7Err}");
                }
                var a7Info = a7Opt.__unsafeGetRight;

                step = "a8";
                var a8Opt = a8RW.deserialize(
                    serialized,
                    startIndex + a1Info.bytesRead + a2Info.bytesRead + a3Info.bytesRead + a4Info.bytesRead +
                    a5Info.bytesRead + a6Info.bytesRead + a7Info.bytesRead
                    );
                if (a8Opt.leftValueOut(out var a8Err))
                {
                    return($"{rwName} a8 failed: {a8Err}");
                }
                var a8Info = a8Opt.__unsafeGetRight;

                step = "mapper";
                var info = new DeserializeInfo <B>(
                    mapper(a1Info.value, a2Info.value, a3Info.value, a4Info.value, a5Info.value, a6Info.value, a7Info.value, a8Info.value),
                    a1Info.bytesRead + a2Info.bytesRead + a3Info.bytesRead + a4Info.bytesRead + a5Info.bytesRead +
                    a6Info.bytesRead + a7Info.bytesRead + a8Info.bytesRead
                    );
                return(info);
            }
            catch (Exception e) {
                return($"{rwName} at index {startIndex}, step {step} threw {e}");
            }
        }