Skip to content

Commit

Permalink
Merge pull request #592 from ikvmnet/symbols
Browse files Browse the repository at this point in the history
New CoreLib Symbols support
  • Loading branch information
wasabii authored Sep 14, 2024
2 parents 9e06f5f + 588af4e commit b9ffe68
Show file tree
Hide file tree
Showing 99 changed files with 9,958 additions and 2,414 deletions.
13 changes: 12 additions & 1 deletion .github/workflows/IKVM.yml
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,16 @@ jobs:
path: /tmp/tests--IKVM.Reflection.Tests.tar.gz
- name: Delete Tests (IKVM.Reflection.Tests)
run: rm /tmp/tests--IKVM.Reflection.Tests.tar.gz
- name: Package Tests (IKVM.CoreLib.Tests)
run: tar czvf /tmp/tests--IKVM.CoreLib.Tests.tar.gz tests/IKVM.CoreLib.Tests
working-directory: dist
- name: Upload Tests (IKVM.CoreLib.Tests)
uses: actions/upload-artifact@v4
with:
name: tests--IKVM.CoreLib.Tests
path: /tmp/tests--IKVM.CoreLib.Tests.tar.gz
- name: Delete Tests (IKVM.CoreLib.Tests)
run: rm /tmp/tests--IKVM.CoreLib.Tests.tar.gz
- name: Package Tests (IKVM.Tests)
run: tar czvf /tmp/tests--IKVM.Tests.tar.gz tests/IKVM.Tests
working-directory: dist
Expand Down Expand Up @@ -378,8 +388,9 @@ jobs:
$tfm += @( "net6.0" )
}
$run = @(
$run = (
"IKVM.Reflection.Tests",
"IKVM.CoreLib.Tests",
"IKVM.Tests",
"IKVM.Java.Tests",
"IKVM.Tools.Exporter.Tests",
Expand Down
4 changes: 3 additions & 1 deletion IKVM.deps.targets
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

<ItemGroup>
<PackageReference Include="IKVM.ByteCode" Version="9.1.3" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
<PackageReference Include="System.Collections.Immutable" Version="8.0.0" GeneratePathProperty="true" />
<PackageReference Include="System.Reflection.Metadata" Version="8.0.0" GeneratePathProperty="true" />
<PackageReference Include="System.Text.Json" Version="8.0.4" GeneratePathProperty="true" />
</ItemGroup>

<Choose>
Expand Down
5 changes: 5 additions & 0 deletions IKVM.refs.targets
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="$(MSBuildThisFileDirectory)src\IKVM.Reflection\IKVM.Reflection.csproj">
<ReferenceOutputAssembly>true</ReferenceOutputAssembly>
<PrivateAssets>all</PrivateAssets>
<OutputItemType>IkvmLibsItem</OutputItemType>
</ProjectReference>
<ProjectReference Include="$(MSBuildThisFileDirectory)src\IKVM.CoreLib\IKVM.CoreLib.csproj">
<ReferenceOutputAssembly>true</ReferenceOutputAssembly>
<PrivateAssets>all</PrivateAssets>
Expand Down
8 changes: 7 additions & 1 deletion IKVM.sln
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@ Project("{6DE1C62B-E8D7-451A-8734-87EAEB46E35B}") = "libsspi_bridge", "src\libss
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IKVM.CoreLib", "src\IKVM.CoreLib\IKVM.CoreLib.csproj", "{C6556982-85B8-4E58-A693-1239D4823BD7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IKVM.Tools.Core", "src\IKVM.Tools.Core\IKVM.Tools.Core.csproj", "{0D74BDB7-F1FE-4E9E-B195-640C66DA19D2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IKVM.Tools.Core", "src\IKVM.Tools.Core\IKVM.Tools.Core.csproj", "{0D74BDB7-F1FE-4E9E-B195-640C66DA19D2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IKVM.CoreLib.Tests", "src\IKVM.CoreLib.Tests\IKVM.CoreLib.Tests.csproj", "{21179656-9D00-45C9-A8BF-50E456AA29D4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -921,6 +923,10 @@ Global
{0D74BDB7-F1FE-4E9E-B195-640C66DA19D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0D74BDB7-F1FE-4E9E-B195-640C66DA19D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0D74BDB7-F1FE-4E9E-B195-640C66DA19D2}.Release|Any CPU.Build.0 = Release|Any CPU
{21179656-9D00-45C9-A8BF-50E456AA29D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{21179656-9D00-45C9-A8BF-50E456AA29D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{21179656-9D00-45C9-A8BF-50E456AA29D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{21179656-9D00-45C9-A8BF-50E456AA29D4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
16 changes: 16 additions & 0 deletions src/IKVM.CoreLib.Tests/IKVM.CoreLib.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="MSTest.Sdk">
<PropertyGroup>
<TargetFrameworks>net472;net6.0;net7.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.8.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\IKVM.CoreLib\IKVM.CoreLib.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
using System.IO;

using FluentAssertions;

using IKVM.CoreLib.Symbols.IkvmReflection;
using IKVM.Reflection;

using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace IKVM.CoreLib.Tests.Symbols.IkvmReflection
{

[TestClass]
public class IkvmReflectionSymbolTests
{

class Foo<T>
{

T? field;

bool Method(int p1) => true;

}

Universe? universe;
Assembly? coreAssembly;
Assembly? thisAssembly;

[TestInitialize]
public void Setup()
{
universe = new Universe(typeof(object).Assembly.GetName().Name);
universe.AssemblyResolve += Universe_AssemblyResolve;
coreAssembly = universe.LoadFile(typeof(object).Assembly.GetAssemblyLocation());
thisAssembly = universe.LoadFile(typeof(IkvmReflectionSymbolTests).Assembly.GetAssemblyLocation());
}

/// <summary>
/// Attempt to load assembly from system.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
/// <returns></returns>
Assembly? Universe_AssemblyResolve(object sender, ResolveEventArgs args)
{
try
{
var asm = System.Reflection.Assembly.Load(args.Name);
if (asm != null && File.Exists(asm.Location))
return universe!.LoadFile(asm.Location);
}
catch
{

}

return null;
}

[TestMethod]
public void SameTypeShouldBeSame()
{
var c = new IkvmReflectionSymbolContext();
var s1 = c.GetOrCreateTypeSymbol(universe!.GetBuiltInType("System", "Object"));
var s2 = c.GetOrCreateTypeSymbol(universe!.GetBuiltInType("System", "Object"));
s1.Should().BeSameAs(s2);
}

[TestMethod]
public void GenericTypeDefinitionShouldBeSame()
{
var t = thisAssembly!.GetType("IKVM.CoreLib.Tests.Symbols.IkvmReflection.IkvmReflectionSymbolTests+Foo`1");
var c = new IkvmReflectionSymbolContext();
var s1 = c.GetOrCreateTypeSymbol(t);
var s2 = c.GetOrCreateTypeSymbol(t);
s1.Should().BeSameAs(s2);
}

[TestMethod]
public void GenericTypeShouldBeSame()
{
var t = thisAssembly!.GetType("IKVM.CoreLib.Tests.Symbols.IkvmReflection.IkvmReflectionSymbolTests+Foo`1").MakeGenericType(universe!.GetBuiltInType("System", "Int32"));
var c = new IkvmReflectionSymbolContext();
var s1 = c.GetOrCreateTypeSymbol(t);
var s2 = c.GetOrCreateTypeSymbol(t);
s1.Should().BeSameAs(s2);
}

[TestMethod]
public void ArrayTypeShouldBeSame()
{
var t = universe!.GetBuiltInType("System", "Object").MakeArrayType(2);
var c = new IkvmReflectionSymbolContext();
var s1 = c.GetOrCreateTypeSymbol(t);
var s2 = c.GetOrCreateTypeSymbol(t);
s1.Should().BeSameAs(s2);
}

[TestMethod]
public void SZArrayTypeShouldBeSame()
{
var t = universe!.GetBuiltInType("System", "Object").MakeArrayType();
var c = new IkvmReflectionSymbolContext();
var s1 = c.GetOrCreateTypeSymbol(t);
var s2 = c.GetOrCreateTypeSymbol(t);
s1.Should().BeSameAs(s2);
}

[TestMethod]
public unsafe void PointerTypeShouldBeSame()
{
var t = universe!.GetBuiltInType("System", "Int32").MakePointerType();
var c = new IkvmReflectionSymbolContext();
var s1 = c.GetOrCreateTypeSymbol(t);
var s2 = c.GetOrCreateTypeSymbol(t);
s1.Should().BeSameAs(s2);
}

[TestMethod]
public unsafe void ByRefTypeShouldBeSame()
{
var t = universe!.GetBuiltInType("System", "Int32").MakeByRefType();
var c = new IkvmReflectionSymbolContext();
var s1 = c.GetOrCreateTypeSymbol(t);
var s2 = c.GetOrCreateTypeSymbol(t);
s1.Should().BeSameAs(s2);
}

[TestMethod]
public void EnumTypeShouldBeSame()
{
var a = universe!.Load(typeof(System.AttributeTargets).Assembly.FullName);
var t = a.GetType("System.AttributeTargets");
var c = new IkvmReflectionSymbolContext();
var s1 = c.GetOrCreateTypeSymbol(t);
var s2 = c.GetOrCreateTypeSymbol(t);
s1.Should().BeSameAs(s2);
}

[TestMethod]
public void CanGetType()
{
var t = universe!.GetBuiltInType("System", "Object");
var c = new IkvmReflectionSymbolContext();
var s = c.GetOrCreateTypeSymbol(t);
s.Name.Should().Be("Object");
s.FullName.Should().Be("System.Object");
}

[TestMethod]
public void CanGetFieldOfGenericTypeDefinition()
{
var t = thisAssembly!.GetType("IKVM.CoreLib.Tests.Symbols.IkvmReflection.IkvmReflectionSymbolTests+Foo`1");
var c = new IkvmReflectionSymbolContext();
var s = c.GetOrCreateTypeSymbol(t);
s.IsGenericType.Should().BeTrue();
s.IsGenericTypeDefinition.Should().BeTrue();
var f = s.GetField("field", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
f.Name.Should().Be("field");
f.FieldType.IsGenericType.Should().BeFalse();
f.FieldType.IsGenericParameter.Should().BeTrue();
}

[TestMethod]
public void CanGetFieldOfGenericType()
{
var t = thisAssembly!.GetType("IKVM.CoreLib.Tests.Symbols.IkvmReflection.IkvmReflectionSymbolTests+Foo`1").MakeGenericType(universe!.GetBuiltInType("System", "Int32"));
var c = new IkvmReflectionSymbolContext();
var s = c.GetOrCreateTypeSymbol(t);
s.IsGenericType.Should().BeTrue();
s.IsGenericTypeDefinition.Should().BeFalse();
var f = s.GetField("field", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
f.Name.Should().Be("field");
f.FieldType.IsGenericType.Should().BeFalse();
f.FieldType.IsGenericParameter.Should().BeFalse();
f.FieldType.Should().BeSameAs(c.GetOrCreateTypeSymbol(universe!.GetBuiltInType("System", "Int32")));
}

[TestMethod]
public void CanGetMethod()
{
var t = universe!.GetBuiltInType("System", "Object");
var c = new IkvmReflectionSymbolContext();
var s = c.GetOrCreateTypeSymbol(t);
var m = s.GetMethod("ToString");
m.Name.Should().Be("ToString");
m.ReturnType.Should().BeSameAs(c.GetOrCreateTypeSymbol(universe!.GetBuiltInType("System", "String")));
m.ReturnParameter.ParameterType.Should().BeSameAs(c.GetOrCreateTypeSymbol(universe!.GetBuiltInType("System", "String")));
m.IsGenericMethod.Should().BeFalse();
m.IsGenericMethodDefinition.Should().BeFalse();
m.IsPublic.Should().BeTrue();
m.IsPrivate.Should().BeFalse();
}

}

}
Loading

0 comments on commit b9ffe68

Please sign in to comment.