Skip to content

Commit

Permalink
Add test for CecilILGenerator declare local
Browse files Browse the repository at this point in the history
  • Loading branch information
zznty committed Nov 24, 2024
1 parent 2011243 commit 499ba09
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/MonoMod.UnitTest/CecilIlGeneratorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using Mono.Cecil;
using MonoMod.Utils.Cil;
using System;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using Xunit;
using Xunit.Abstractions;
using MethodAttributes = Mono.Cecil.MethodAttributes;
using TypeAttributes = Mono.Cecil.TypeAttributes;

namespace MonoMod.UnitTest
{
public class CecilIlGeneratorTest : TestBase
{
public CecilIlGeneratorTest(ITestOutputHelper helper) : base(helper)
{
}

[Fact]
public void TestLocalEmit()
{
using var moduleStream = new MemoryStream();
using (var moduleDef = ModuleDefinition.CreateModule("TestModule", ModuleKind.Dll))
{
var methodDef = new MethodDefinition("TestMethod",
MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Static,
moduleDef.TypeSystem.String);

moduleDef.Types.Add(new TypeDefinition("Test", "TestType",
TypeAttributes.Public | TypeAttributes.AutoClass | TypeAttributes.AnsiClass |
TypeAttributes.BeforeFieldInit)
{
Methods = { methodDef },
BaseType = moduleDef.TypeSystem.Object
});

methodDef.Parameters.Add(new ParameterDefinition(moduleDef.TypeSystem.Boolean));

{
var il = new CecilILGenerator(methodDef.Body.GetILProcessor()).GetProxy();

var local = il.DeclareLocal(typeof(string));

il.Emit(OpCodes.Ldarga_S, 0);
var toStringMethod = typeof(bool).GetMethod(nameof(bool.ToString), []);

Assert.NotNull(toStringMethod);

il.Emit(OpCodes.Callvirt, toStringMethod);
il.Emit(OpCodes.Stloc, local);

il.Emit(OpCodes.Ldloc, local);
il.Emit(OpCodes.Ret);
}

moduleDef.Write(moduleStream);
}

var assembly = Assembly.Load(moduleStream.ToArray());

var type = assembly.GetType("Test.TestType");

Assert.NotNull(type);

var method = type.GetMethod("TestMethod");

Assert.NotNull(method);
Assert.Equal(bool.TrueString, method.Invoke(null, new object[] { true }));
Assert.Equal(bool.FalseString, method.Invoke(null, new object[] { false }));
}
}
}

0 comments on commit 499ba09

Please sign in to comment.