Skip to content

Commit

Permalink
Types now know their name
Browse files Browse the repository at this point in the history
Plug Type.GetType
  • Loading branch information
quajak committed Dec 20, 2022
1 parent c699533 commit c478d94
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
23 changes: 23 additions & 0 deletions Tests/Kernels/Cosmos.Compiler.Tests.TypeSystem/Kernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ private void TestVTablesImpl()
Assert.AreEqual(((CosmosRuntimeType)typeof(object)).mTypeId, types[0], "GetGCFieldTypes returns object at offset 0");
Assert.AreEqual(((CosmosRuntimeType)typeof(TestStruct)).mTypeId, types[1], "GetGCFieldTypes returns TestStruct at offset 1");
Assert.AreEqual(((CosmosRuntimeType)typeof(object)).mTypeId, types[2], "GetGCFieldTypes returns object at offset 2");

// check that classes have the correct name
Assert.AreEqual("Int32", ((CosmosRuntimeType)typeof(int)).Name, "Name of Int32 is correctly stored");
Assert.AreEqual("Object", ((CosmosRuntimeType)typeof(object)).Name, "Name of Object is correctly stored");
}

private unsafe void TestGarbageCollectorMethods()
Expand Down Expand Up @@ -139,6 +143,7 @@ private unsafe void TestGarbageCollector()
Assert.AreEqual(0, collected, "Storing elements in static class keeps them referenced");
}

#region Test Methods
public void TestMethod1()
{
object a = new object();
Expand Down Expand Up @@ -208,6 +213,8 @@ void TestMethod6()
Console.WriteLine("Test: " + 3 + " vs " + 5);
}

#endregion

protected override void Run()
{
try
Expand Down Expand Up @@ -262,6 +269,7 @@ protected override void Run()
TestGarbageCollectorMethods();
TestGarbageCollector();
RealMethodsTest();
TestReflection();

TestController.Completed();
}
Expand All @@ -276,5 +284,20 @@ protected override void Run()
TestController.Failed();
}
}

private static void TestReflection()
{
Assert.AreEqual("Int32", typeof(int).Name, "Plug for Name of Int32 works");
Assert.AreEqual("Object", typeof(object).Name, "Plug for Name of Object works");
string intAQN = typeof(int).AssemblyQualifiedName;
Assert.IsTrue(intAQN.StartsWith("System.Int32, System.Private.CoreLib"), $"Plug for AssemblyQualifiedName of Int32 works ({intAQN})");
string objectAQN = typeof(object).AssemblyQualifiedName;
Assert.IsTrue(objectAQN.StartsWith("System.Object, System.Private.CoreLib"), $"Plug for AssemblyQualifiedName of Object works ({objectAQN})");

Assert.AreEqual("Int32", Type.GetType("Int32").Name, "GetType works on Int32");
Assert.AreEqual("Int32", Type.GetType(typeof(int).AssemblyQualifiedName).Name, "GetType works on Int32 using assembly qualified name");
Assert.AreEqual("Int32", Type.GetType("System.Int32, System.Private.CoreLib").Name, "GetType works on Int32 with shortened assembly qualified name");
Assert.AreEqual("Int32", Type.GetType("System.Int32").Name, "GetType works on Int32 with shortened assembly qualified name");
}
}
}
64 changes: 62 additions & 2 deletions source/Cosmos.Core/VTablesImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public static bool IsInstance(uint aObjectType, uint aDesiredObjectType, bool aI

public static void SetTypeInfo(int aType, uint aBaseType, uint aSize, uint aInterfaceCount, uint[] aInterfaceIndexes,
uint aMethodCount, uint[] aMethodIndexes, uint[] aMethodAddresses,
uint aInterfaceMethodCount, uint[] aInterfaceMethodIndexes, uint[] aTargetMethodIndexes, uint aGCFieldCount, uint[] aGCFieldOffsets, uint[] aGCFieldTypes,
bool aIsValueType, bool aIsStruct)
uint aInterfaceMethodCount, uint[] aInterfaceMethodIndexes, uint[] aTargetMethodIndexes, uint aGCFieldCount,
uint[] aGCFieldOffsets, uint[] aGCFieldTypes, bool aIsValueType, bool aIsStruct, string aName, string aAssemblyQualifiedName)
{
var vTable = new VTable();
vTable.BaseTypeIdentifier = aBaseType;
Expand All @@ -114,6 +114,8 @@ public static void SetTypeInfo(int aType, uint aBaseType, uint aSize, uint aInte
vTable.TargetMethodIndexes = aTargetMethodIndexes;
vTable.IsValueType = aIsValueType;
vTable.IsStruct = aIsStruct;
vTable.Name = aName;
vTable.AssemblyQualifiedName = aAssemblyQualifiedName;
mTypes[aType] = vTable;
var gcTable = new GCTable();
gcTable.GCFieldCount = aGCFieldCount;
Expand Down Expand Up @@ -353,10 +355,68 @@ public static bool IsStruct(uint aType)
{
return mTypes[aType].IsStruct;
}

/// <summary>
/// Gets the Name of the type
/// </summary>
/// <param name="aType"></param>
/// <returns></returns>
public static string GetName(uint aType)
{
return mTypes[aType].Name;
}

/// <summary>
/// Gets the Assembly Qualified Name for the type
/// </summary>
/// <param name="aType"></param>
/// <returns></returns>
public static string GetAssemblyQualifiedName(uint aType)
{
return mTypes[aType].AssemblyQualifiedName;
}

/// <summary>
/// Get type id of type matching the name
/// The name can either be name of the class or the assembly qualified name
/// Only inlcuding the first or first two parts of the assembly qualified name also works
/// </summary>
/// <param name="name"></param>
/// <returns>Returns -1 if no type can be found</returns>
public static int GetType(string name)
{
for (int i = 0; i < mTypes.Length; i++)
{
var currType = mTypes[i];
if (currType.Name == name || currType.AssemblyQualifiedName == name)
{
return i;
}
else
{
bool difference = false;
for (int k = 0; k < name.Length; k++)
{
if (name[k] != currType.AssemblyQualifiedName[k])
{
difference = true;
break;
}
}
if (!difference)
{
return i;
}
}
}
return -1;
}
}

public struct VTable
{
public string Name;
public string AssemblyQualifiedName;
public uint BaseTypeIdentifier;
public uint Size;

Expand Down
2 changes: 2 additions & 0 deletions source/Cosmos.Core_Plugs/CosmosRuntimeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public CosmosRuntimeType(uint aTypeId)
: this()
{
mTypeId = aTypeId;
Name = VTablesImpl.GetName(aTypeId);
AssemblyQualifiedName = VTablesImpl.GetAssemblyQualifiedName(aTypeId);
}

protected CosmosRuntimeType()
Expand Down
11 changes: 11 additions & 0 deletions source/Cosmos.Core_Plugs/System/TypeImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,16 @@ public static Type get_BaseType(CosmosRuntimeType aThis)
{
return aThis.BaseType;
}

[PlugMethod(Signature = "System_Type__System_Type_get_Type_System_String", IsOptional = false)]
public static CosmosRuntimeType GetType(string aName)
{
int typeId = VTablesImpl.GetType(aName);
if(typeId == -1)
{
return null;
}
return new CosmosRuntimeType((uint)typeId);
}
}
}

0 comments on commit c478d94

Please sign in to comment.