Skip to content

Commit

Permalink
Handle System.Type.FullName
Browse files Browse the repository at this point in the history
Added for convenience
  • Loading branch information
ltrzesniewski committed Mar 23, 2024
1 parent 1838f56 commit 5b33c95
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/InlineIL.Fody/Processing/ArgumentConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private TypeRefBuilder ConsumeArgTypeRefBuilder(Instruction instruction)
{
var args = _il.GetArgumentPushInstructionsInSameBasicBlock(instruction);
var assemblyName = ConsumeArgString(args[0]);
var typeName = ConsumeArgString(args[1]);
var typeName = ConsumeArgTypeName(args[1]);
var builder = TypeRefBuilder.FromAssemblyNameAndTypeName(_context, assemblyName, typeName);

_il.Remove(instruction);
Expand Down Expand Up @@ -266,7 +266,7 @@ private TypeRefBuilder ConsumeArgTypeRefBuilder(Instruction instruction)
{
var args = _il.GetArgumentPushInstructionsInSameBasicBlock(instruction);
var assemblyPath = ConsumeArgString(args[0]);
var typeName = ConsumeArgString(args[1]);
var typeName = ConsumeArgTypeName(args[1]);
var builder = TypeRefBuilder.FromInjectedAssembly(_context, assemblyPath, typeName);

_il.Remove(instruction);
Expand Down Expand Up @@ -315,6 +315,26 @@ private GenericParameterType ConsumeArgGenericParameterType(Instruction instruct
}
}

private string ConsumeArgTypeName(Instruction instruction)
{
if (instruction is { OpCode.FlowControl: FlowControl.Call, Operand: MethodReference method })
{
switch (method.GetElementMethod().FullName)
{
case "System.String System.Type::get_FullName()":
{
var args = _il.GetArgumentPushInstructionsInSameBasicBlock(instruction);
var typeRef = ConsumeArgTypeRef(args[0]);

_il.Remove(instruction);
return typeRef.FullName;
}
}
}

return ConsumeArgString(instruction);
}

public MethodReference ConsumeArgMethodRef(Instruction methodRefInstruction)
{
return ConsumeArgMethodRefBuilder(methodRefInstruction).Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ public void UseMethodsFromDifferentVersionsOfDllUsingTypeReference()
Pop();
}

public void UseMethodsFromDifferentVersionsOfDllUsingTypeFullNameProperty()
{
// Use referenced DLL
InjectedType.AddInt32(40, 2);

// Use alternative version of the referenced DLL
Ldc_I4(40);
Ldc_I4_2();

Call(
MethodRef.Method(
TypeRef.FromDllFile(_injectedAltAssemblyPath, typeof(InjectedType).FullName!),
"MultiplyInt32"
)
);

Pop();
}

public void InvalidInjectedDllFile()
{
Ldtoken(
Expand All @@ -68,13 +87,27 @@ public void InvalidInjectedTypeSpec()
);
}

public void InvalidInjectedTypeSpecWithFullName()
{
Ldtoken(
TypeRef.FromDllFile(_injectedAltAssemblyPath, typeof(InjectedType[]).FullName!)
);
}

public void InvalidInjectedTypeSpec2()
{
Ldtoken(
TypeRef.FromDllFile(_injectedAltAssemblyPath, typeof(InjectedType).MakeByRefType())
);
}

public void InvalidInjectedTypeSpec2WithFullName()
{
Ldtoken(
TypeRef.FromDllFile(_injectedAltAssemblyPath, typeof(InjectedType).MakeByRefType().FullName!)
);
}

public void InvalidInjectedFnPtr()
{
Ldtoken(
Expand Down
13 changes: 13 additions & 0 deletions src/InlineIL.Tests/Weaving/TypeRefTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ public void should_return_constructed_generic_type_spec_from_injected_type()
[Theory]
[InlineData(nameof(TypeRefTestCases.UseMethodsFromDifferentVersionsOfDll))]
[InlineData(nameof(TypeRefTestCases.UseMethodsFromDifferentVersionsOfDllUsingTypeReference))]
[InlineData(nameof(TypeRefTestCases.UseMethodsFromDifferentVersionsOfDllUsingTypeFullNameProperty))]
public void should_use_methods_from_injected_type_and_referenced_type(string methodName)
{
var calls = InvalidAssemblyToProcessFixture.ResultModule
Expand Down Expand Up @@ -385,12 +386,24 @@ public void should_report_injected_type_spec()
ShouldHaveError(nameof(TypeRefTestCases.InvalidInjectedTypeSpec)).ShouldContain("The provided type does not represent an element type");
}

[Fact]
public void should_report_injected_type_spec_with_full_name()
{
ShouldHaveError(nameof(TypeRefTestCases.InvalidInjectedTypeSpecWithFullName)).ShouldContain("Could not find type 'InlineIL.Tests.InjectedAssembly.InjectedType[]'");
}

[Fact]
public void should_report_injected_type_spec_2()
{
ShouldHaveError(nameof(TypeRefTestCases.InvalidInjectedTypeSpec2)).ShouldContain("The provided type does not represent an element type");
}

[Fact]
public void should_report_injected_type_spec_2_with_full_name()
{
ShouldHaveError(nameof(TypeRefTestCases.InvalidInjectedTypeSpec2WithFullName)).ShouldContain("Could not find type 'InlineIL.Tests.InjectedAssembly.InjectedType&'");
}

[Fact]
public void should_report_injected_fn_ptr()
{
Expand Down

0 comments on commit 5b33c95

Please sign in to comment.