Beispiel #1
0
        void IndexOf3_works(Instruction[] data, Instruction instr, int index, int count, int expected)
        {
            var list   = new InstructionList(data);
            var result = list.IndexOf(instr, index, count);

            Assert.Equal(expected, result);
        }
Beispiel #2
0
        public static T ParseOperand <T>(byte id, InstructionList instr, InstructionBase i, Action <byte, T, bool> setHandler, bool condition = false)
        {
            object value = null;

            if (typeof(T) == typeof(string))
            {
                string accum = "";
                //string accum = (string)value;
                var ix3 = instr.IndexOf(i);
                while (true)
                {
                    var i2 = instr[ix3++];
                    if (i2 is LabelInstruction)
                    {
                        var cond = instr[ix3 - 2];
                        if (cond is ParameterInstruction)
                        {
                            //We can only use bool here
                            if (i2.ILCode == ILCode.Brfalse || i2.ILCode == ILCode.Brfalse_S)
                            {
                                condition = true;
                            }
                            else if (i2.ILCode != ILCode.Brtrue && i2.ILCode != ILCode.Brtrue_S)
                            {
                                throw new InvalidOperationException("Can't handle this condition properly!");
                            }

                            ParseOperand(id, instr, ((LabelInstruction)i2).JumpToInstruction, setHandler, !condition);
                        }
                    }
                    else if (i2.ILCode == ILCode.Ret)
                    {
                        break;
                    }
                    else if (instr[ix3].ILCode == ILCode.Newarr)
                    {
                        ix3++;
                        continue;
                    }
                    else if (i2 is StringInstruction || instr[ix3].ILCode == ILCode.Box)
                    {
                        accum += i2.RawOperand;
                    }
                }
                value = accum;
            }
            else if (typeof(T) == typeof(byte[]))
            {
                if (i.ILCode != ILCode.Ldnull)
                {
                    var    ix3    = instr.IndexOf(i) + 1;
                    int    length = (int)i.RawOperand;
                    byte[] data   = new byte[length];
                    value = data;
                    while (true)
                    {
                        var i2 = instr[ix3++];

                        if (i2.ILCode == ILCode.Ldtoken)
                        {
                            System.Runtime.CompilerServices.RuntimeHelpers.InitializeArray(data, ((FieldInfo)((MemberInstruction)i2).Operand).FieldHandle);
                            break;
                        }
                        else if (i2.ILCode == ILCode.Stelem_I1)
                        {
                            var ind = (int)instr[ix3 - 3].RawOperand;
                            var val = Convert.ToByte(instr[ix3 - 2].RawOperand);
                            data[ind] = val;
                        }
                        else if (i2.ILCode == ILCode.Ret)
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                value = i.RawOperand;
            }

            if (value != null && !(value is T))
            {
                value = Convert.ChangeType(value, typeof(T));
            }

            setHandler(id, (T)value, condition);

            return((T)value);
        }