/// <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, Type fromType) { if (PrimitiveTypeUtilities.IsValueType(fromType)) { generator.Box(fromType); } }
/// <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, Type fromType) { // Check that a conversion is actually necessary. if (fromType == typeof(ConcatenatedString)) { return; } if (fromType == typeof(Nitrassic.Undefined) || fromType == typeof(Nitrassic.Null) || fromType == typeof(bool) || fromType == typeof(string)) { // Convert as per ToString, then create a new ConcatenatedString instance. ToString(generator, fromType); generator.NewObject(ReflectionHelpers.ConcatenatedString_Constructor_String); } else if (fromType == typeof(int) || fromType == typeof(uint) || fromType == typeof(double) || fromType == typeof(object) || fromType == typeof(Library.ObjectInstance)) { // Otherwise, fall back to calling TypeConverter.ToConcatenatedString() if (PrimitiveTypeUtilities.IsValueType(fromType)) { generator.Box(fromType); } generator.Call(ReflectionHelpers.TypeConverter_ToConcatenatedString); } else { throw new NotImplementedException(string.Format("Unsupported primitive type: {0}", fromType)); } }
/// <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, Type fromType) { // Check that a conversion is actually necessary. if (fromType == typeof(string)) { return; } if (fromType == typeof(Nitrassic.Undefined)) { // Converting from undefined produces "undefined". generator.Pop(); generator.LoadString("undefined"); } else if (fromType == typeof(Nitrassic.Null)) { // Converting from null produces "null". generator.Pop(); generator.LoadString("null"); } else if (fromType == typeof(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); } else if (fromType == typeof(ConcatenatedString)) { generator.Call(ReflectionHelpers.ConcatenatedString_ToString); } else { // Otherwise, fall back to calling TypeConverter.ToString() if (PrimitiveTypeUtilities.IsValueType(fromType)) { generator.Box(fromType); } generator.Call(ReflectionHelpers.TypeConverter_ToString); } }
/// <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, Type fromType, PrimitiveTypeHint preferredType) { if (fromType == typeof(Nitrassic.Undefined) || fromType == typeof(Nitrassic.Null) || fromType == typeof(bool) || fromType == typeof(string) || fromType == typeof(ConcatenatedString) || fromType == typeof(int) || fromType == typeof(uint) || fromType == typeof(double)) { return; } else if (fromType == typeof(object) || fromType == typeof(Library.ObjectInstance)) { // Otherwise, fall back to calling TypeConverter.ToPrimitive() if (PrimitiveTypeUtilities.IsValueType(fromType)) { generator.Box(fromType); } generator.LoadInt32((int)preferredType); generator.Call(ReflectionHelpers.TypeConverter_ToPrimitive); } else { throw new NotImplementedException("Unsupported primitive type: " + fromType); } }