Ejemplo n.º 1
0
 /// <summary>
 /// Pops the value on the stack, converts it to an object, then pushes the result onto the
 /// stack.
 /// </summary>
 /// <param name="generator"> The IL generator. </param>
 /// <param name="fromType"> The type to convert from. </param>
 public static void ToAny(ILGenerator generator, PrimitiveType fromType)
 {
     if (PrimitiveTypeUtilities.IsValueType(fromType))
     {
         generator.Box(fromType);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Pops the value on the stack, converts it to a primitive value, then pushes the result
        /// onto the stack.
        /// </summary>
        /// <param name="generator"> The IL generator. </param>
        /// <param name="fromType"> The type to convert from. </param>
        /// <param name="preferredType"> Specifies whether toString() or valueOf() should be
        /// preferred when converting to a primitive. </param>
        public static void ToPrimitive(ILGenerator generator, PrimitiveType fromType, PrimitiveTypeHint preferredType)
        {
            switch (fromType)
            {
            case PrimitiveType.Undefined:
            case PrimitiveType.Null:
            case PrimitiveType.Bool:
            case PrimitiveType.String:
            case PrimitiveType.ConcatenatedString:
            case PrimitiveType.Int32:
            case PrimitiveType.UInt32:
            case PrimitiveType.Number:
                // These are primitives already.
                break;

            case PrimitiveType.Any:
            case PrimitiveType.Object:
                // Otherwise, fall back to calling TypeConverter.ToPrimitive()
                if (PrimitiveTypeUtilities.IsValueType(fromType))
                {
                    generator.Box(fromType);
                }
                generator.LoadInt32((int)preferredType);
                generator.Call(ReflectionHelpers.TypeConverter_ToPrimitive.Value);
                break;

            default:
                throw new NotImplementedException(string.Format("Unsupported primitive type: {0}", fromType));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Pops the value on the stack, converts it to a string, then pushes the result onto the
        /// stack.
        /// </summary>
        /// <param name="generator"> The IL generator. </param>
        /// <param name="fromType"> The type to convert from. </param>
        public static void ToString(ILGenerator generator, PrimitiveType fromType)
        {
            // Check that a conversion is actually necessary.
            if (fromType == PrimitiveType.String)
            {
                return;
            }

            switch (fromType)
            {
            case PrimitiveType.Undefined:
                // Converting from undefined produces "undefined".
                generator.Pop();
                generator.LoadString("undefined");
                break;

            case PrimitiveType.Null:
                // Converting from null produces "null".
                generator.Pop();
                generator.LoadString("null");
                break;

            case PrimitiveType.Bool:
                // Converting from a boolean produces "false" if the boolean is false, or "true" if the boolean is true.
                var elseClause = generator.CreateLabel();
                var endOfIf    = generator.CreateLabel();
                generator.BranchIfFalse(elseClause);
                generator.LoadString("true");
                generator.Branch(endOfIf);
                generator.DefineLabelPosition(elseClause);
                generator.LoadString("false");
                generator.DefineLabelPosition(endOfIf);
                break;

            case PrimitiveType.ConcatenatedString:
                generator.Call(ReflectionHelpers.ConcatenatedString_ToString.Value);
                break;

            case PrimitiveType.Int32:
            case PrimitiveType.UInt32:
            case PrimitiveType.Number:
            case PrimitiveType.Any:
            case PrimitiveType.Object:
                // Otherwise, fall back to calling TypeConverter.ToString()
                if (PrimitiveTypeUtilities.IsValueType(fromType))
                {
                    generator.Box(fromType);
                }
                generator.Call(ReflectionHelpers.TypeConverter_ToString.Value);
                break;

            default:
                throw new NotImplementedException(string.Format("Unsupported primitive type: {0}", fromType));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Pops the value on the stack, converts it to a concatenated string, then pushes the result
        /// onto the stack.
        /// </summary>
        /// <param name="generator"> The IL generator. </param>
        /// <param name="fromType"> The type to convert from. </param>
        public static void ToConcatenatedString(ILGenerator generator, PrimitiveType fromType)
        {
            // Check that a conversion is actually necessary.
            if (fromType == PrimitiveType.ConcatenatedString)
            {
                return;
            }

            switch (fromType)
            {
            case PrimitiveType.Undefined:
            case PrimitiveType.Null:
            case PrimitiveType.Bool:
            case PrimitiveType.String:
                // Convert as per ToString, then create a new ConcatenatedString instance.
                ToString(generator, fromType);
                generator.NewObject(ReflectionHelpers.ConcatenatedString_Constructor_String.Value);
                break;

            case PrimitiveType.Int32:
            case PrimitiveType.UInt32:
            case PrimitiveType.Number:
            case PrimitiveType.Any:
            case PrimitiveType.Object:
                // Otherwise, fall back to calling TypeConverter.ToConcatenatedString()
                if (PrimitiveTypeUtilities.IsValueType(fromType))
                {
                    generator.Box(fromType);
                }
                generator.Call(ReflectionHelpers.TypeConverter_ToConcatenatedString.Value);
                break;

            default:
                throw new NotImplementedException(string.Format("Unsupported primitive type: {0}", fromType));
            }
        }