-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support disassembly of generic methods
- Loading branch information
Showing
13 changed files
with
141 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,76 @@ | ||
using System.Reflection; | ||
using System.Collections.Immutable; | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
|
||
using Sharp; | ||
using Sharp.Backend.Sandbox.Shared; | ||
|
||
var assembly = Utils.LoadAssembly(); | ||
|
||
const BindingFlags Flags = BindingFlags.DeclaredOnly | ||
| BindingFlags.Instance | ||
| BindingFlags.Static | ||
| BindingFlags.Public | ||
| BindingFlags.NonPublic; | ||
|
||
foreach (var type in assembly.DefinedTypes) | ||
{ | ||
const BindingFlags Flags = BindingFlags.DeclaredOnly | ||
| BindingFlags.Instance | ||
| BindingFlags.Static | ||
| BindingFlags.Public | ||
| BindingFlags.NonPublic; | ||
|
||
foreach (var method in type.GetMembers(Flags).OfType<MethodBase>()) | ||
RuntimeHelpers.PrepareMethod(method.MethodHandle); | ||
if (type.IsNested) | ||
continue; | ||
|
||
PrepareType(type, []); | ||
} | ||
|
||
static void PrepareType(Type type, ImmutableList<RuntimeTypeHandle> baseGenericTypes) | ||
{ | ||
if (type.IsGenericType) | ||
{ | ||
foreach (var jitGenericAttribute in type.GetCustomAttributes<JitGenericAttribute>()) | ||
PrepareTypeMembers(type, baseGenericTypes.AddRange(jitGenericAttribute.Types.Select(t => t.TypeHandle))); | ||
} | ||
else | ||
PrepareTypeMembers(type, baseGenericTypes); | ||
} | ||
|
||
static void PrepareMethod(MethodBase method, ImmutableList<RuntimeTypeHandle> genericTypes) | ||
{ | ||
if (method.IsGenericMethod) | ||
{ | ||
foreach (var jitGenericAttribute in method.GetCustomAttributes<JitGenericAttribute>()) | ||
{ | ||
try | ||
{ | ||
RuntimeHelpers.PrepareMethod(method.MethodHandle, [.. genericTypes, .. jitGenericAttribute.Types.Select(t => t.TypeHandle)]); | ||
} | ||
catch | ||
{ | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
try | ||
{ | ||
RuntimeHelpers.PrepareMethod(method.MethodHandle, [.. genericTypes]); | ||
} | ||
catch | ||
{ | ||
} | ||
} | ||
} | ||
|
||
static void PrepareTypeMembers(Type type, ImmutableList<RuntimeTypeHandle> genericTypes) | ||
{ | ||
foreach (var member in type.GetMembers(Flags)) | ||
{ | ||
if (member is MethodBase method) | ||
{ | ||
if (method.IsAbstract) | ||
continue; | ||
|
||
PrepareMethod(method, genericTypes); | ||
} | ||
else if (member is Type nestedType) | ||
PrepareType(nestedType, genericTypes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
services: | ||
bot: | ||
build: | ||
dockerfile: Sharp/Dockerfile | ||
context: .. | ||
dockerfile: Bot/Sharp/Dockerfile | ||
restart: always |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Sharp; | ||
|
||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface)] | ||
public sealed class JitGenericAttribute(params Type[] types) : Attribute | ||
{ | ||
public IReadOnlyList<Type> Types { get; } = types; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<RootNamespace>Sharp</RootNamespace> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters