DynamicILGenerator doesn't pop "this" from the stack when reading an instance field

Jan 13, 2011 at 8:50 PM

Fixed code :

         /// <summary>
        /// Pushes the value of the given field onto the stack.
        /// </summary>
        /// <param name="field"> The field whose value will be pushed. </param>
        public override void LoadField(System.Reflection.FieldInfo field)
        {
            if (field == null)
                throw new ArgumentNullException("field");

            int token = this.GetToken(field);
            if (field.IsStatic == true)
            {
                // ldsfld = 7E <token>
                Emit1ByteOpCodeInt32(0x7E, 0, 1, token);
            }
            else
            {
                // ldfld = 7B <token>
                // TODO: Check this!!!
                PopStackOperands(ToVESType(field.DeclaringType));
                Emit1ByteOpCodeInt32(0x7B, 1, 1, token);
            }
            PushStackOperand(ToVESType(field.FieldType));
        }



Well, I was checking my own code since nearly two hours because either your IL generator of Visual Studio complained about stack imbalance. After some time, I ended up by doing a step by step execution to find out where the problem was and I found this issue.

After changing the code, my program worked great (halas I already had deleted some piece of code because I thought it was the culprit :D)

Regards,
François

Coordinator
Jan 13, 2011 at 10:37 PM

You're right - thanks for the bug report!

(As it happens, Jurassic never actually loads an instance field so that code was never tested!)