Skip to content

Commit

Permalink
Merge branch 'main' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabii committed Jan 10, 2024
2 parents aff110d + 14a46fc commit 4106c24
Show file tree
Hide file tree
Showing 97 changed files with 10,373 additions and 9,495 deletions.
48 changes: 48 additions & 0 deletions src/IKVM.Reflection.Tests/ModuleWriterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.IO;

using IKVM.Reflection.Emit;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace IKVM.Reflection.Tests
{

[TestClass]
public class ModuleWriterTests
{

public TestContext TestContext { get; set; }

[TestMethod]
public void CanWriteNetFxModule()
{
var d = Path.Combine(TestContext.TestRunResultsDirectory, "IKVM.Reflection.Tests", "ModuleWriterTests");
if (Directory.Exists(d))
Directory.Delete(d, true);
Directory.CreateDirectory(d);

var u = new Universe("mscorlib");
var a = u.DefineDynamicAssembly(new AssemblyName("Test"), AssemblyBuilderAccess.Save, d);
var m = a.DefineDynamicModule("Test", "Test.dll", false);
m.DefineType("Type");
a.Save("Test.dll");
}

[TestMethod]
public void CanWriteNetCoreModule()
{
var d = Path.Combine(TestContext.TestRunResultsDirectory, "IKVM.Reflection.Tests", "ModuleWriterTests");
if (Directory.Exists(d))
Directory.Delete(d, true);
Directory.CreateDirectory(d);

var u = new Universe("System.Runtime");
var a = u.DefineDynamicAssembly(new AssemblyName("Test"), AssemblyBuilderAccess.Save, d);
var m = a.DefineDynamicModule("Test", "Test.dll", false);
m.DefineType("Type");
a.Save("Test.dll");
}

}

}
156 changes: 84 additions & 72 deletions src/IKVM.Reflection/ArrayType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,77 +26,89 @@ Jeroen Frijters

namespace IKVM.Reflection
{

sealed class ArrayType : ElementHolderType
{
internal static Type Make(Type type, CustomModifiers mods)
{
return type.Universe.CanonicalizeType(new ArrayType(type, mods));
}

private ArrayType(Type type, CustomModifiers mods)
: base(type, mods, Signature.ELEMENT_TYPE_SZARRAY)
{
}

public override Type BaseType
{
get { return elementType.Module.universe.System_Array; }
}

public override Type[] __GetDeclaredInterfaces()
{
return new Type[] {
this.Module.universe.Import(typeof(IList<>)).MakeGenericType(elementType),
this.Module.universe.Import(typeof(ICollection<>)).MakeGenericType(elementType),
this.Module.universe.Import(typeof(IEnumerable<>)).MakeGenericType(elementType)
};
}

public override MethodBase[] __GetDeclaredMethods()
{
Type[] int32 = new Type[] { this.Module.universe.System_Int32 };
List<MethodBase> list = new List<MethodBase>();
list.Add(new BuiltinArrayMethod(this.Module, this, "Set", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, new Type[] { this.Module.universe.System_Int32, elementType }));
list.Add(new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), int32));
list.Add(new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, int32));
list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
for (Type type = elementType; type.__IsVector; type = type.GetElementType())
{
Array.Resize(ref int32, int32.Length + 1);
int32[int32.Length - 1] = int32[0];
list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
}
return list.ToArray();
}

public override TypeAttributes Attributes
{
get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
}

public override int GetArrayRank()
{
return 1;
}

public override bool Equals(object o)
{
return EqualsHelper(o as ArrayType);
}

public override int GetHashCode()
{
return elementType.GetHashCode() * 5;
}

internal override string GetSuffix()
{
return "[]";
}

protected override Type Wrap(Type type, CustomModifiers mods)
{
return Make(type, mods);
}
}
{

internal static Type Make(Type type, CustomModifiers mods)
{
return type.Universe.CanonicalizeType(new ArrayType(type, mods));
}

/// <summary>
/// Initializes a new instance.
/// </summary>
/// <param name="type"></param>
/// <param name="mods"></param>
ArrayType(Type type, CustomModifiers mods) :
base(type, mods, Signature.ELEMENT_TYPE_SZARRAY)
{

}

public override Type BaseType
{
get { return elementType.Module.universe.System_Array; }
}

public override Type[] __GetDeclaredInterfaces()
{
return new Type[] {
Module.universe.Import(typeof(IList<>)).MakeGenericType(elementType),
Module.universe.Import(typeof(ICollection<>)).MakeGenericType(elementType),
Module.universe.Import(typeof(IEnumerable<>)).MakeGenericType(elementType)
};
}

public override MethodBase[] __GetDeclaredMethods()
{
var int32 = new Type[] { Module.universe.System_Int32 };
var list = new List<MethodBase>();
list.Add(new BuiltinArrayMethod(Module, this, "Set", CallingConventions.Standard | CallingConventions.HasThis, Module.universe.System_Void, new Type[] { Module.universe.System_Int32, elementType }));
list.Add(new BuiltinArrayMethod(Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), int32));
list.Add(new BuiltinArrayMethod(Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, int32));
list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, Module.universe.System_Void, int32)));

for (var type = elementType; type.__IsVector; type = type.GetElementType())
{
Array.Resize(ref int32, int32.Length + 1);
int32[int32.Length - 1] = int32[0];
list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, Module.universe.System_Void, int32)));
}

return list.ToArray();
}

public override TypeAttributes Attributes
{
get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
}

public override int GetArrayRank()
{
return 1;
}

public override bool Equals(object o)
{
return EqualsHelper(o as ArrayType);
}

public override int GetHashCode()
{
return elementType.GetHashCode() * 5;
}

internal override string GetSuffix()
{
return "[]";
}

protected override Type Wrap(Type type, CustomModifiers mods)
{
return Make(type, mods);
}

}

}
4 changes: 1 addition & 3 deletions src/IKVM.Reflection/Assembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ Jeroen Frijters
namespace IKVM.Reflection
{

public delegate Module ModuleResolveEventHandler(object sender, ResolveEventArgs e);

public abstract class Assembly : ICustomAttributeProvider
{

Expand Down Expand Up @@ -90,7 +88,7 @@ internal Type ResolveType(Module requester, TypeName typeName)

public string FullName
{
get { return fullName ?? (fullName = GetName().FullName); }
get { return fullName ??= GetName().FullName; }
}

public Module[] GetModules()
Expand Down
33 changes: 20 additions & 13 deletions src/IKVM.Reflection/AssemblyName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ Jeroen Frijters
namespace IKVM.Reflection
{

public sealed class AssemblyName
: ICloneable
public sealed class AssemblyName :
ICloneable
{

private string name;
private string culture;
private Version version;
private byte[] publicKeyToken;
private byte[] publicKey;
private StrongNameKeyPair keyPair;
private AssemblyNameFlags flags;
private AssemblyHashAlgorithm hashAlgorithm;
private AssemblyVersionCompatibility versionCompatibility = AssemblyVersionCompatibility.SameMachine;
private string codeBase;
string name;
string culture;
Version version;
byte[] publicKeyToken;
byte[] publicKey;
StrongNameKeyPair keyPair;
AssemblyNameFlags flags;
AssemblyHashAlgorithm hashAlgorithm;
AssemblyVersionCompatibility versionCompatibility = AssemblyVersionCompatibility.SameMachine;
string codeBase;
internal byte[] hash;

/// <summary>
Expand All @@ -56,6 +56,13 @@ public AssemblyName()

}

/// <summary>
/// Initializes a new instance.
/// </summary>
/// <param name="assemblyName"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="FileLoadException"></exception>
public AssemblyName(string assemblyName)
{
if (assemblyName == null)
Expand Down Expand Up @@ -383,7 +390,7 @@ internal static string ComputePublicKeyToken(string publicKey)
return sb.ToString();
}

private static void AppendPublicKey(StringBuilder sb, byte[] publicKey)
static void AppendPublicKey(StringBuilder sb, byte[] publicKey)
{
for (int i = 0; i < publicKey.Length; i++)
{
Expand Down
Loading

0 comments on commit 4106c24

Please sign in to comment.