EmitValue() public static method

Emits the given value. Only possible for certain types.
public static EmitValue ( ILGenerator generator, object value ) : void
generator ILGenerator The IL generator.
value object The value to emit.
return void
Exemplo n.º 1
0
 /// <summary>
 /// Pushes the result of converting <c>undefined</c> to the given type onto the stack.
 /// </summary>
 /// <param name="il"> The IL generator. </param>
 /// <param name="targetParameter"> The type to convert to, and optionally a default value. </param>
 private static void EmitUndefined(ILGenerator il, BinderArgument argument)
 {
     // Emit either the default value if there is one, otherwise emit "undefined".
     if (argument.HasDefaultValue == true)
     {
         // Emit the default value.
         EmitHelpers.EmitValue(il, argument.DefaultValue);
     }
     else
     {
         // Convert Undefined to the target type and emit.
         EmitHelpers.EmitUndefined(il);
         EmitTypeConversion(il, typeof(object), argument.Type);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Gets an enumerable list of argument objects, equal in size to
        /// <paramref name="argumentCount"/> while generating code to prepare those arguments for
        /// a method call.
        /// </summary>
        /// <param name="argumentCount"> The number of arguments to return. </param>
        /// <param name="generator"> The IL generator used to create an array if the method has a
        /// ParamArray parameter. </param>
        /// <returns> An enumerable list of argument objects. </returns>
        public IEnumerable <BinderArgument> GenerateArguments(ILGenerator generator, int argumentCount)
        {
            if (generator == null)
            {
                throw new ArgumentNullException(nameof(generator));
            }

            int paramArrayIndex = 0;

            foreach (var argument in this.GetArguments(argumentCount))
            {
                if (argument.IsParamArrayArgument == true)
                {
                    if (paramArrayIndex == 0)
                    {
                        // This is the start of the ParamArray arguments.
                        // Create an array.
                        int paramArraySize = Math.Max(0, argumentCount - this.OptionalParameterCount - this.RequiredParameterCount);
                        generator.LoadInt32(paramArraySize);
                        generator.NewArray(argument.Type);
                    }

                    // Load the array and index.
                    generator.Duplicate();
                    generator.LoadInt32(paramArrayIndex++);
                }

                // Yield will have the side effect of generating a value.
                yield return(argument);

                if (argument.IsParamArrayArgument == true)
                {
                    // Store the value in the ParamArray array.
                    generator.StoreArrayElement(argument.Type);
                }
            }

            // Populate any missing optional arguments with the default value.
            if (this.RequiredParameterCount + this.OptionalParameterCount - argumentCount > 0)
            {
                var parameters = this.GetParameters();
                for (int i = 0; i < this.RequiredParameterCount + this.OptionalParameterCount - argumentCount; i++)
                {
                    var optionalParameter = parameters[argumentCount + i];
                    if ((optionalParameter.Attributes & ParameterAttributes.HasDefault) == ParameterAttributes.HasDefault)
                    {
                        // Emit the default value.
                        EmitHelpers.EmitValue(generator, new BinderArgument(optionalParameter, 0).DefaultValue);
                    }
                    else
                    {
                        // Emit default(T).
                        EmitHelpers.EmitDefaultValue(generator, optionalParameter.ParameterType);
                    }
                }
            }

            // Create an empty array if a ParamArray argument exists but no arguments were provided.
            if (this.HasParamArray == true && paramArrayIndex == 0)
            {
                // Create an array.
                generator.LoadInt32(0);
                generator.NewArray(this.ParamArrayElementType);
            }
        }