Skip to content

Commit

Permalink
Reorder checks so the 1st looks less special-casey
Browse files Browse the repository at this point in the history
 * As the previous commit showed, the `IsGenericParameter` check must
   have priority... probably not just over enums, but over any other
   value types, too.

 * The `IsEnum` check can be put behind `IsValueType` so it does not run
   for non-value types.
  • Loading branch information
stakx committed Aug 28, 2023
1 parent 18b514c commit e9c957c
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions src/Castle.Core/DynamicProxy/Generators/Emitters/OpCodeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ internal abstract class OpCodeUtil
/// </summary>
public static void EmitLoadIndirectOpCodeForType(ILGenerator gen, Type type)
{
if (type.IsEnum && !type.IsGenericParameter)
{
EmitLoadIndirectOpCodeForType(gen, GetUnderlyingTypeOfEnum(type));
return;
}

if (type.IsByRef)
{
throw new NotSupportedException("Cannot load ByRef values");
Expand All @@ -48,13 +42,20 @@ public static void EmitLoadIndirectOpCodeForType(ILGenerator gen, Type type)

gen.Emit(opCode);
}
else if (type.IsValueType)
else if (type.IsGenericParameter)
{
gen.Emit(OpCodes.Ldobj, type);
}
else if (type.IsGenericParameter)
else if (type.IsValueType)
{
gen.Emit(OpCodes.Ldobj, type);
if (type.IsEnum)
{
EmitLoadIndirectOpCodeForType(gen, GetUnderlyingTypeOfEnum(type));
}
else
{
gen.Emit(OpCodes.Ldobj, type);
}
}
else
{
Expand Down Expand Up @@ -107,12 +108,6 @@ public static void EmitLoadOpCodeForDefaultValueOfType(ILGenerator gen, Type typ
/// </summary>
public static void EmitStoreIndirectOpCodeForType(ILGenerator gen, Type type)
{
if (type.IsEnum && !type.IsGenericParameter)
{
EmitStoreIndirectOpCodeForType(gen, GetUnderlyingTypeOfEnum(type));
return;
}

if (type.IsByRef)
{
throw new NotSupportedException("Cannot store ByRef values");
Expand All @@ -128,13 +123,20 @@ public static void EmitStoreIndirectOpCodeForType(ILGenerator gen, Type type)

gen.Emit(opCode);
}
else if (type.IsValueType)
else if (type.IsGenericParameter)
{
gen.Emit(OpCodes.Stobj, type);
}
else if (type.IsGenericParameter)
else if (type.IsValueType)
{
gen.Emit(OpCodes.Stobj, type);
if (type.IsEnum)
{
EmitStoreIndirectOpCodeForType(gen, GetUnderlyingTypeOfEnum(type));
}
else
{
gen.Emit(OpCodes.Stobj, type);
}
}
else
{
Expand Down

0 comments on commit e9c957c

Please sign in to comment.