1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-27 20:18:01 +02:00

Comptime method reflection, method entry/exit emission

This commit is contained in:
Brian Fiete 2021-01-13 05:09:09 -08:00
parent bc8758bbac
commit 8f3060fd3c
18 changed files with 944 additions and 117 deletions

View file

@ -1,4 +1,6 @@
using System;
using System.Diagnostics;
using System.Reflection;
namespace Tests
{
@ -27,11 +29,26 @@ namespace Tests
[Comptime]
public void ApplyToType(Type type)
{
Compiler.EmitDefinition(type, scope $"""
Compiler.EmitTypeBody(type, scope $"""
public int32 m{mMemberName} = {mInitVal};
public int32 GetVal{mMemberName}() => mC;
""");
}
}
[AttributeUsage(.Method)]
struct LogAttribute : Attribute, IComptimeMethodApply
{
public static String gLog = new .() ~ delete _;
[Comptime]
public void ApplyToMethod(ComptimeMethodInfo method)
{
String emit = scope $"LogAttribute.gLog.AppendF($\"Called {method}";
for (var fieldIdx < method.ParamCount)
emit.AppendF($" {{ {method.GetParamName(fieldIdx)} }}");
emit.Append("\");");
Compiler.EmitMethodEntry(method, emit);
}
}
@ -43,13 +60,19 @@ namespace Tests
[OnCompile(.TypeInit), Comptime]
public static void Generate()
{
Compiler.EmitDefinition(typeof(Self), """
Compiler.EmitTypeBody(typeof(Self), """
public int32 mB = 234;
public int32 GetValB() => mB;
""");
}
}
[Log]
public static void MethodA(int a, int b)
{
}
[Test]
public static void TestBasics()
{
@ -59,6 +82,12 @@ namespace Tests
Test.Assert(ca.GetValB() == 234);
Test.Assert(ca.mC == 345);
Test.Assert(ca.GetValC() == 345);
Compiler.Mixin("int val = 99;");
Test.Assert(val == 99);
MethodA(34, 45);
Debug.Assert(LogAttribute.gLog == "Called Tests.Comptime.MethodA(int a, int b) 34 45");
}
}
}