コード例 #1
0
        /// <summary>
        ///     Reads TypeCode at <c>data</c>[<c>index</c>] and checks it
        ///     <para></para>
        ///     Returns default(<typeparamref name="T" />) if TypeCode = Empty
        /// </summary>
        /// <param name="context">Current context</param>
        private static void ReadTypeCodeAndCheck(ReaderMethodBuilderContext context)
        {
            var il            = context.Il;
            var notEmptyLabel = il.DefineLabel("notEmpty");

            il.Ldc_I4(1);
            context.AssertLength();

            context.GoToCurrentLocation(); // stack: [&data[index]]
            il.Ldind(typeof(byte));        // stack: [data[index]]
            il.Dup();                      // stack: [data[index], data[index]]
            il.Stloc(context.TypeCode);    // typeCode = data[index]; stack: [typeCode]

            il.Brtrue(notEmptyLabel);      // if(typeCode != 0) goto notNull;

            context.IncreaseIndexBy1();    // index = index + 1
            il.Ret();

            il.MarkLabel(notEmptyLabel);

            context.CheckTypeCode();
        }
コード例 #2
0
        protected override void ReadNotEmpty(ReaderMethodBuilderContext context)
        {
            MemberInfo[] dataMembers;
            ulong[]      hashCodes;
            BuildMembersTable(context.Context, out hashCodes, out dataMembers);

            var il       = context.Il;
            var end      = context.Length;
            var typeCode = context.TypeCode;

            var setters = dataMembers.Select(member => member == null ? default(KeyValuePair <Delegate, IntPtr>) : GetMemberSetter(context.Context, member)).ToArray();

            var settersField = context.Context.InitConstField(Type, 0, setters.Select(pair => pair.Value).ToArray());

            context.Context.InitConstField(Type, 1, setters.Select(pair => pair.Key).ToArray());
            var hashCodesField = context.Context.InitConstField(Type, 2, hashCodes);

            context.IncreaseIndexBy1(); // index = index + 1
            context.AssertTypeCode(GroBufTypeCode.Object);

            il.Ldc_I4(4);
            context.AssertLength();

            context.GoToCurrentLocation(); // stack: [&data[index]]
            il.Ldind(typeof(uint));        // stack: [(uint)data[index] = data length]
            context.IncreaseIndexBy4();    // index = index + 4; stack: [data length]

            il.Dup();                      // stack: [data length, data length]
            il.Stloc(end);                 // end = data length; stack: [data length]

            if (!Type.IsValueType)
            {
                context.LoadResultByRef(); // stack: [data length, ref result]
                il.Ldind(Type);            // stack: [data length, result]
                var notNullLabel = il.DefineLabel("notNull");
                il.Brtrue(notNullLabel);   // if(result != null) goto notNull; stack: [data length]
                context.LoadResultByRef(); // stack: [data length, ref result]
                ObjectConstructionHelper.EmitConstructionOfType(Type, il);
                il.Stind(Type);            // result = new type(); stack: [data length]
                il.MarkLabel(notNullLabel);
            }

            context.StoreObject(Type);

            var doneLabel = il.DefineLabel("done");

            il.Brfalse(doneLabel);  // if(data length == 0) goto done; stack: []
            il.Ldloc(end);          // stack: [data length]

            context.AssertLength(); // stack: []

            il.Ldloc(end);          // stack: [data length]
            context.LoadIndex();    // stack: [data length, index]
            il.Add();               // stack: [data length + index]
            il.Stloc(end);          // end = data length + index

            var cycleStartLabel = il.DefineLabel("cycleStart");

            il.MarkLabel(cycleStartLabel);

            il.Ldc_I4(9);
            context.AssertLength();

            context.GoToCurrentLocation(); // stack: [&data[index]]
            il.Ldind(typeof(long));        // stack: [*(int64*)&data[index] = hashCode]
            context.IncreaseIndexBy8();    // index = index + 8; stack: [*(int64*)&data[index] = hashCode]

            il.Dup();                      // stack: [hashCode, hashCode]
            il.Ldc_I8(dataMembers.Length); // stack: [hashCode, hashCode, (int64)hashCodes.Length]
            il.Rem(true);                  // stack: [hashCode, hashCode % hashCodes.Length]
            il.Conv <int>();               // stack: [hashCode, (int)(hashCode % hashCodes.Length)]
            var idx = il.DeclareLocal(typeof(int));

            il.Stloc(idx);                     // idx = (int)(hashCode % hashCodes.Length); stack: [hashCode]

            context.LoadField(hashCodesField); // stack: [hashCode, hashCodes]
            il.Ldloc(idx);                     // stack: [hashCode, hashCodes, idx]
            il.Ldelem(typeof(long));           // stack: [hashCode, hashCodes[idx]]

            var skipDataLabel = il.DefineLabel("skipData");

            il.Bne_Un(skipDataLabel); // if(hashCode != hashCodes[idx]) goto skipData; stack: []

            // Read data
            context.LoadData();              // stack: [pinnedData]
            context.LoadIndexByRef();        // stack: [pinnedData, ref index]
            context.LoadResultByRef();       // stack: [pinnedData, ref index, ref result]
            context.LoadContext();           // stack: [pinnedData, ref index, ref result, context]

            context.LoadField(settersField); // stack: [pinnedData, ref index, ref result, context, setters]
            il.Ldloc(idx);                   // stack: [pinnedData, ref index, ref result, context, setters, idx]
            il.Ldelem(typeof(IntPtr));       // stack: [pinnedData, ref index, ref result, context, setters[idx]]
            var parameterTypes = new[] { typeof(byte *), typeof(int).MakeByRefType(), Type.MakeByRefType(), typeof(ReaderContext) };

            il.Calli(CallingConventions.Standard, typeof(void), parameterTypes); // setters[idx](pinnedData, ref index, ref result, context); stack: []

            var checkIndexLabel = il.DefineLabel("checkIndex");

            il.Br(checkIndexLabel); // goto checkIndex

            il.MarkLabel(skipDataLabel);
            // Skip data
            context.GoToCurrentLocation(); // stack: [&data[index]]
            il.Ldind(typeof(byte));        // stack: [data[index]]
            il.Stloc(typeCode);            // typeCode = data[index]; stack: []
            context.IncreaseIndexBy1();    // index = index + 1
            context.CheckTypeCode();
            context.SkipValue();

            il.MarkLabel(checkIndexLabel);

            context.LoadIndex();           // stack: [index]
            il.Ldloc(end);                 // stack: [index, end]
            il.Blt(cycleStartLabel, true); // if(index < end) goto cycleStart; stack: []

            var onDeserializedMethod = Type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                                       .SingleOrDefault(method => method.GetCustomAttribute <OnDeserializedAttribute>() != null);

            if (onDeserializedMethod != null)
            {
                var parameters = onDeserializedMethod.GetParameters();
                if (parameters.Length != 1 || parameters[0].ParameterType != typeof(StreamingContext))
                {
                    throw new InvalidOperationException(string.Format("The method '{0}' marked with 'OnDeserialized' attribute must accept exactly one parameter of type '{1}'", onDeserializedMethod, typeof(StreamingContext).FullName));
                }
                context.LoadResult(Type);
                il.Ldc_I4((int)StreamingContextStates.Other);
                il.Newobj(typeof(StreamingContext).GetConstructor(new[] { typeof(StreamingContextStates) }));
                il.Call(onDeserializedMethod);
            }

            il.MarkLabel(doneLabel);
        }