diff --git a/.travis.yml b/.travis.yml
index 5e2f995..29871c3 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,10 +1,10 @@
-dist: trusty
+dist: xenial
sudo: false
language: csharp
+dotnet: 2.2.100
matrix:
include:
- - dotnet: 2.1.400
- mono: none
+ - mono: none
env: DOTNETCORE=1
- mono: latest
script:
diff --git a/AsmToBytes/AsmToBytes.csproj b/AsmToBytes/AsmToBytes.csproj
index 016d150..e50a26f 100644
--- a/AsmToBytes/AsmToBytes.csproj
+++ b/AsmToBytes/AsmToBytes.csproj
@@ -3,7 +3,7 @@
Exe
false
- netcoreapp2.0;net45
+ netcoreapp2.2;net45
\ No newline at end of file
diff --git a/AsmToBytes/EntryPoint.cs b/AsmToBytes/EntryPoint.cs
index 1062b52..0d26d2e 100644
--- a/AsmToBytes/EntryPoint.cs
+++ b/AsmToBytes/EntryPoint.cs
@@ -23,7 +23,7 @@ public static void Main(string[] args)
var tempFile = Path.Combine(dirName, "temp.asm");
var dllFile = Path.Combine(dirName, "temp.dll");
var marker = Guid.NewGuid().ToByteArray();
- var stringMarker = string.Join(Environment.NewLine, marker.Select(x => string.Format("db 0{0:x2}h", x)));
+ var stringMarker = string.Join(Environment.NewLine, marker.Select(x => $"db 0{x:x2}h"));
string content;
if (architecture == "x86")
content = string.Format(@"
@@ -92,7 +92,7 @@ end data
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false,
- Arguments = string.Format(@"""{0}"" ""{1}""", tempFile, dllFile),
+ Arguments = $@"""{tempFile}"" ""{dllFile}""",
FileName = Path.Combine(pathToFasm, "fasm.exe")
};
var process = new Process
@@ -139,7 +139,7 @@ end data
{
var result = new StringBuilder();
for (var j = i; j < k; ++j)
- result.Append(string.Format("0x{0:x2},", dllContent[j]));
+ result.Append($"0x{dllContent[j]:x2},");
result.AppendLine();
result.AppendLine(Convert.ToBase64String(dllContent, i, k - i));
File.WriteAllText(outputFile, result.ToString());
diff --git a/Common.DotSettings b/Common.DotSettings
index 3db4f8b..a3d6674 100644
--- a/Common.DotSettings
+++ b/Common.DotSettings
@@ -170,6 +170,7 @@
True
True
True
+ True
True
True
True
diff --git a/Directory.Build.props b/Directory.Build.props
index 07b6ba4..b412b23 100644
--- a/Directory.Build.props
+++ b/Directory.Build.props
@@ -14,7 +14,7 @@
-
+
\ No newline at end of file
diff --git a/GrEmit.Tests/GrEmit.Tests.csproj b/GrEmit.Tests/GrEmit.Tests.csproj
index 30e0742..47967fb 100644
--- a/GrEmit.Tests/GrEmit.Tests.csproj
+++ b/GrEmit.Tests/GrEmit.Tests.csproj
@@ -2,13 +2,13 @@
false
- netcoreapp2.0;net45
+ netcoreapp2.2;net45
-
-
-
+
+
+
diff --git a/GrEmit.Tests/Test.cs b/GrEmit.Tests/Test.cs
index 9497a58..c4999cf 100644
--- a/GrEmit.Tests/Test.cs
+++ b/GrEmit.Tests/Test.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
@@ -7,7 +7,6 @@
using NUnit.Framework;
-using MethodBody = GrEmit.MethodBodyParsing.MethodBody;
#if NET45
using System.Diagnostics;
using System.Linq;
@@ -18,24 +17,6 @@ namespace GrEmit.Tests
[TestFixture]
public class Test
{
- public static void Qzz()
- {
- var list = new List();
- list.Add(1);
- }
-
- [Test, Ignore("Is used for debugging")]
- public void TestZzz()
- {
- var method = HackHelpers.GetMethodDefinition(x => Qzz());
- var body = MethodBody.Read(method, true);
- var z = body.CreateDelegate();
- z();
- var body2 = MethodBody.Read(z.Method, true);
- var z2 = body2.CreateDelegate();
- z2();
- }
-
[Test]
public void TestDifferentStructs()
{
@@ -480,7 +461,7 @@ public void TestPerformance_Ifs_vs_Switch()
var ifs = BuildIfs(module, keys);
var switCh = BuildSwitch(module, keys);
- const int iterations = 1000000000;
+ const int iterations = 100_000_000;
Console.WriteLine("Worst case:");
@@ -600,7 +581,7 @@ private static void CheckDifferent(string[] values)
foreach (var str in values)
{
if (hashSet.Contains(str))
- throw new InvalidOperationException(string.Format("Duplicate value '{0}'", str));
+ throw new InvalidOperationException($"Duplicate value '{str}'");
hashSet.Add(str);
}
}
diff --git a/GrEmit.Tests/TestGrobufUsages.cs b/GrEmit.Tests/TestGrobufUsages.cs
index af2448a..53a9d44 100644
--- a/GrEmit.Tests/TestGrobufUsages.cs
+++ b/GrEmit.Tests/TestGrobufUsages.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Reflection;
using System.Reflection.Emit;
@@ -45,10 +45,10 @@ private static void AssertDateTimeOffsetFields(DateTime dateTime, short offsetMi
private static string SelectName(string netcoreName, string net45Name)
{
-#if NETCOREAPP2_0
- return netcoreName;
-#else
+#if NET45
return net45Name;
+#else
+ return netcoreName;
#endif
}
diff --git a/GrEmit.sln.DotSettings b/GrEmit.sln.DotSettings
index 3cadf8c..393e966 100644
--- a/GrEmit.sln.DotSettings
+++ b/GrEmit.sln.DotSettings
@@ -5,4 +5,5 @@
C:\projects\open-source\skbkontur\gremit\Common.DotSettings
..\Common.DotSettings
True
- 1
\ No newline at end of file
+ 1
+ True
\ No newline at end of file
diff --git a/GrEmit/GroboIL.cs b/GrEmit/GroboIL.cs
index 76cbc49..d290838 100644
--- a/GrEmit/GroboIL.cs
+++ b/GrEmit/GroboIL.cs
@@ -464,14 +464,14 @@ public void Jmp(MethodInfo method)
if (method == null)
throw new ArgumentNullException("method");
if (method.ReturnType != methodReturnType)
- throw new ArgumentException(string.Format("The return type must be of type '{0}'", methodReturnType), "method");
+ throw new ArgumentException($"The return type must be of type '{methodReturnType}'", "method");
var parameterTypes = method.GetParameters().Select(info => info.ParameterType).ToArray();
if (parameterTypes.Length != methodParameterTypes.Length)
- throw new ArgumentException(string.Format("The number of arguments must be equal to {0}", methodParameterTypes.Length), "method");
+ throw new ArgumentException($"The number of arguments must be equal to {methodParameterTypes.Length}", "method");
for (var i = 0; i < parameterTypes.Length; ++i)
{
if (parameterTypes[i] != methodParameterTypes[i])
- throw new ArgumentException(string.Format("The argument #{0} must be of type '{1}'", i + 1, methodParameterTypes[i]), "method");
+ throw new ArgumentException($"The argument #{i + 1} must be of type '{methodParameterTypes[i]}'", "method");
}
Emit(OpCodes.Jmp, method);
}
@@ -1749,7 +1749,7 @@ public void Conv()
opCode = OpCodes.Conv_R8;
break;
default:
- throw new ArgumentException(string.Format("Expected numeric type but was '{0}'", type), "type");
+ throw new ArgumentException($"Expected numeric type but was '{type}'", "type");
}
}
Emit(opCode);
@@ -1821,7 +1821,7 @@ public void Conv_Ovf(bool unsigned)
opCode = OpCodes.Conv_Ovf_U8;
break;
default:
- throw new ArgumentException(string.Format("Expected integer type but was '{0}'", type), "T");
+ throw new ArgumentException($"Expected integer type but was '{type}'", "T");
}
}
}
@@ -1860,7 +1860,7 @@ public void Conv_Ovf(bool unsigned)
opCode = OpCodes.Conv_Ovf_U8_Un;
break;
default:
- throw new ArgumentException(string.Format("Expected integer type but was '{0}'", type), "T");
+ throw new ArgumentException($"Expected integer type but was '{type}'", "T");
}
}
}
@@ -2077,7 +2077,7 @@ private void Emit(OpCode opCode, ILInstructionParameter parameter)
il.Emit(opCode, (float)primitiveParameter.Value);
break;
default:
- throw new InvalidOperationException(string.Format("Type code '{0}' is not valid at this point", typeCode));
+ throw new InvalidOperationException($"Type code '{typeCode}' is not valid at this point");
}
}
}
diff --git a/GrEmit/ILCode.cs b/GrEmit/ILCode.cs
index a02a7c7..fadb2ff 100644
--- a/GrEmit/ILCode.cs
+++ b/GrEmit/ILCode.cs
@@ -13,7 +13,7 @@ internal class ILCode
public int MarkLabel(GroboIL.Label label, ILInstructionComment comment)
{
if (labelLineNumbers.ContainsKey(label))
- throw new InvalidOperationException(string.Format("The label '{0}' has already been marked", label.Name));
+ throw new InvalidOperationException($"The label '{label.Name}' has already been marked");
labelLineNumbers.Add(label, Count);
instructions.Add(new ILInstruction(InstructionKind.Label, default(OpCode), new LabelILInstructionParameter(label), comment));
return Count++;
@@ -27,14 +27,14 @@ public void CheckLabels()
{
var label = (LabelILInstructionParameter)instruction.Parameter;
if (!labelLineNumbers.ContainsKey(label.Label))
- throw new InvalidOperationException(string.Format("The label '{0}' has not been marked", label.Label.Name));
+ throw new InvalidOperationException($"The label '{label.Label.Name}' has not been marked");
}
if (instruction.Parameter is LabelsILInstructionParameter)
{
foreach (var label in ((LabelsILInstructionParameter)instruction.Parameter).Labels)
{
if (!labelLineNumbers.ContainsKey(label))
- throw new InvalidOperationException(string.Format("The label '{0}' has not been marked", label.Name));
+ throw new InvalidOperationException($"The label '{label.Name}' has not been marked");
}
}
}
diff --git a/GrEmit/Injection/MethodUtil.cs b/GrEmit/Injection/MethodUtil.cs
deleted file mode 100644
index df8231f..0000000
--- a/GrEmit/Injection/MethodUtil.cs
+++ /dev/null
@@ -1,288 +0,0 @@
-using System;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Runtime.CompilerServices;
-using System.Runtime.InteropServices;
-using System.Threading;
-
-namespace GrEmit.Injection
-{
- public static class MethodUtil
- {
- [DllImport("kernel32.dll")]
- public static extern unsafe bool VirtualProtect(IntPtr lpAddress, uint dwSize, MEMORY_PROTECTION_CONSTANTS flNewProtect, MEMORY_PROTECTION_CONSTANTS* lpflOldProtect);
-
- [Flags]
- public enum MEMORY_PROTECTION_CONSTANTS
- {
- PAGE_EXECUTE = 0x10,
- PAGE_EXECUTE_READ = 0x20,
- PAGE_EXECUTE_READWRITE = 0x40,
- PAGE_EXECUTE_WRITECOPY = 0x80,
- PAGE_NOACCESS = 0x01,
- PAGE_READONLY = 0x02,
- PAGE_READWRITE = 0x04,
- PAGE_WRITECOPY = 0x08,
- PAGE_GUARD = 0x100,
- PAGE_NOCACHE = 0x200,
- PAGE_WRITECOMBINE = 0x400,
- PAGE_TARGETS_INVALID = 0x40000000,
- PAGE_TARGETS_NO_UPDATE = 0x40000000,
- }
-
- public static unsafe bool HookMethod(MethodBase victim, MethodBase dest, out Action unhook)
- {
- var destAddr = GetMethodAddress(dest);
- unhook = null;
-
- //var genericArguments = new Type[0];
-
- //if (victim.DeclaringType.GetGenericArguments() != null)
- //{
- // genericArguments = victim.DeclaringType.GetGenericArguments().ToArray();
- //}
-
- //if (victim.GetGenericArguments() != null)
- //{
- // genericArguments = genericArguments.Concat(victim.GetGenericArguments()).ToArray();
- //}
-
- RuntimeHelpers.PrepareMethod(victim.MethodHandle);
- //RuntimeHelpers.PrepareMethod(victim.MethodHandle, victim.DeclaringType.GetGenericArguments().Concat(victim.GetGenericArguments()).Select(type => type.TypeHandle).ToArray());
- var victimAddr = GetMethodAddress(victim);
-
- bool canMakeRelJmp;
- int dist = 0;
- if (IntPtr.Size == 4)
- {
- canMakeRelJmp = true;
- dist = destAddr.ToInt32() - victimAddr.ToInt32() - 5;
- }
- else
- {
- var dist64 = destAddr.ToInt64() - victimAddr.ToInt64() - 5;
- canMakeRelJmp = dist64 >= int.MinValue && dist64 <= int.MaxValue;
- if (canMakeRelJmp)
- dist = (int)dist64;
- }
- if (canMakeRelJmp)
- {
- // Make relative jump
- var bytes = BitConverter.GetBytes(dist);
- var hookCode = new byte[]
- {
- 0xE9, bytes[0], bytes[1], bytes[2], bytes[3], // jmp dest
- 0x00,
- 0x00,
- 0x00,
- };
- long x = BitConverter.ToInt64(hookCode, 0);
-
- long oldCode;
- if (!PlantRelJmpHook(victimAddr, x, out oldCode))
- return false;
-
- unhook = () =>
- {
- long tmp;
- PlantRelJmpHook(victimAddr, oldCode, out tmp);
- };
-
- return true;
- }
- else
- {
- // todo: set unhook delegate
- if (compareExchange2Words == null)
- return false;
- // Make absolute jump
- UIntPtr lo, hi;
- if (IntPtr.Size == 8)
- {
- // x64
- var bytes = BitConverter.GetBytes(destAddr.ToInt64());
- var hookCode = new byte[16]
- {
- 0x48, 0xB8, bytes[0], bytes[1], bytes[2], bytes[3],
- bytes[4], bytes[5], bytes[6], bytes[7], // movabs rax, hookMethod
- 0xFF, 0xE0, // jmp rax
- 0x90, // nop
- 0x90, // nop
- 0x90, // nop
- 0x90 // nop
- };
- lo = new UIntPtr(BitConverter.ToUInt64(hookCode, 0));
- hi = new UIntPtr(BitConverter.ToUInt64(hookCode, 8));
- }
- else
- {
- // x86
- var bytes = BitConverter.GetBytes(destAddr.ToInt32());
- var hookCode = new byte[8]
- {
- 0xB8, bytes[0], bytes[1], bytes[2], bytes[3], // mov eax, hookMethod
- 0xFF, 0xE0, // jmp eax
- 0x90 // nop
- };
- lo = new UIntPtr(BitConverter.ToUInt32(hookCode, 0));
- hi = new UIntPtr(BitConverter.ToUInt32(hookCode, 4));
- }
-
- MEMORY_PROTECTION_CONSTANTS oldProtect;
- if (!VirtualProtect(victimAddr, (uint)(IntPtr.Size * 2), MEMORY_PROTECTION_CONSTANTS.PAGE_EXECUTE_READWRITE, &oldProtect))
- return false;
-
- compareExchange2Words(victimAddr, lo, hi);
-
- VirtualProtect(victimAddr, (uint)(IntPtr.Size * 2), oldProtect, &oldProtect);
-
- return true;
- }
- }
-
- private static unsafe bool PlantRelJmpHook(IntPtr victimAddr, long newCode, out long oldCode)
- {
- MEMORY_PROTECTION_CONSTANTS oldProtect;
- if (!VirtualProtect(victimAddr, 8, MEMORY_PROTECTION_CONSTANTS.PAGE_EXECUTE_READWRITE, &oldProtect))
- {
- oldCode = 0;
- return false;
- }
-
- oldCode = relJmpHooker(victimAddr, newCode);
-
- VirtualProtect(victimAddr, 8, oldProtect, &oldProtect);
- return true;
- }
-
- private static Func EmitRelJmpHooker()
- {
- var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(long), new[] {typeof(IntPtr), typeof(long)}, typeof(string), true);
- using (var il = new GroboIL(method))
- {
- il.VerificationKind = TypesAssignabilityVerificationKind.LowLevelOnly;
- var cycleLabel = il.DefineLabel("cycle");
- il.MarkLabel(cycleLabel);
- il.Ldarg(0); // stack: [ptr]
- il.Dup(); // stack: [ptr, ptr]
- var x = il.DeclareLocal(typeof(long));
- il.Ldind(typeof(long)); // stack: [ptr, *ptr]
- il.Dup();
- il.Stloc(x); // x = *ptr; stack: [ptr, newCode]
- il.Ldc_I8(unchecked((long)0xFFFFFF0000000000));
- il.And(); // stack: [ptr, x & 0xFFFFFF0000000000]
- il.Ldarg(1); // stack: [ptr, x & 0xFFFFFF0000000000, code]
- il.Or(); // stack: [ptr, (x & 0xFFFFFF0000000000) | code]
- il.Ldloc(x); // stack: [ptr, (x & 0xFFFFFF0000000000) | code, newCode]
- var methodInfo = typeof(Interlocked).GetMethod("CompareExchange", BindingFlags.Static | BindingFlags.Public, null, new[] {typeof(long).MakeByRefType(), typeof(long), typeof(long)}, null);
- il.Call(methodInfo); // stack: [Interlocked.CompareExchange(ptr, (x & 0xFFFFFF0000000000) | code, newCode)]
- il.Ldloc(x); // stack: [Interlocked.CompareExchange(ptr, (x & 0xFFFFFF0000000000) | code, newCode), newCode]
- il.Bne_Un(cycleLabel); // if(Interlocked.CompareExchange(ptr, (x & 0xFFFFFF0000000000) | code, newCode) != newCode) goto cycle; stack: []
- il.Ldloc(x);
- il.Ret();
- }
- return (Func)method.CreateDelegate(typeof(Func));
- }
-
- [UnmanagedFunctionPointer(CallingConvention.StdCall)]
- private delegate void CompareExchange2WordsDelegate(IntPtr location, UIntPtr lo, UIntPtr hi);
-
- private static unsafe CompareExchange2WordsDelegate EmitCompareExchange2Words()
- {
- byte[] code;
- if (IntPtr.Size == 4)
- {
- code = new byte[]
- {
- 0x56, // push esi
- 0x53, // push ebx
- 0x8B, 0x74, 0x24, 0x0C, // mov esi, dword [esp + 12]
- 0x8B, 0x5C, 0x24, 0x10, // mov ebx, dword [esp + 16]
- 0x8B, 0x4C, 0x24, 0x14, // mov ecx, dword [esp + 20]
- 0x8B, 0x06, // mov eax, dword [esi]
- 0x8B, 0x56, 0x04, // mov edx, dword [esi + 4]
- // _loop:
- 0xF0, 0x0F, 0xC7, 0x0E, // lock cmpxchg8b qword [esi]
- 0x75, 0xFA, // jne _loop
- 0x5B, // pop ebx
- 0x5E, // pop esi
- 0xC2, 0x0C, 0x00 // ret 12
- };
- }
- else
- {
- code = new byte[]
- {
- 0x56, // push rsi
- 0x53, // push rbx
- 0x48, 0x89, 0xCE, // mov rsi, rcx
- 0x48, 0x89, 0xD3, // mov rbx, rdx
- 0x4C, 0x89, 0xC1, // mov rcx, r8
- 0x48, 0x8B, 0x06, // mov rax, qword [rsi]
- 0x48, 0x8B, 0x56, 0x08, // mov rdx, qword [rsi + 8]
- // _loop:
- 0xF0, 0x48, 0x0F, 0xC7, 0x0E, // lock cmpxchg16b dqword [rsi]
- 0x75, 0xF9, // jne _loop
- 0x5B, // pop rbx
- 0x5E, // pop rsi
- 0xC3 // ret
- };
- }
- var ptr = Marshal.AllocHGlobal(code.Length);
- MEMORY_PROTECTION_CONSTANTS oldProtect;
- if (!VirtualProtect(ptr, (uint)code.Length, MEMORY_PROTECTION_CONSTANTS.PAGE_EXECUTE_READWRITE, &oldProtect))
- return null;
- Marshal.Copy(code, 0, ptr, code.Length);
- return (CompareExchange2WordsDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(CompareExchange2WordsDelegate));
- }
-
- ///
- /// Gets the address of the method stub
- ///
- /// The method handle.
- ///
- public static IntPtr GetMethodAddress(MethodBase method)
- {
- if (method is DynamicMethod)
- return GetDynamicMethodAddress(method);
- if (method.GetType() == rtDynamicMethodType)
- return GetDynamicMethodAddress(m_ownerExtractor(method));
-
- // Prepare the method so it gets jited
- RuntimeHelpers.PrepareMethod(method.MethodHandle);
-
- return method.MethodHandle.GetFunctionPointer();
- }
-
- private static IntPtr GetDynamicMethodAddress(MethodBase method)
- {
- var handle = GetDynamicMethodRuntimeHandle(method);
- RuntimeHelpers.PrepareMethod(handle);
- return handle.GetFunctionPointer();
- }
-
- private static RuntimeMethodHandle GetDynamicMethodRuntimeHandle(MethodBase method)
- {
- RuntimeMethodHandle handle;
-
- if (Environment.Version.Major == 4)
- {
- var getMethodDescriptorInfo = typeof(DynamicMethod).GetMethod("GetMethodDescriptor",
- BindingFlags.NonPublic | BindingFlags.Instance);
- handle = (RuntimeMethodHandle)getMethodDescriptorInfo.Invoke(method, null);
- }
- else
- {
- var fieldInfo = typeof(DynamicMethod).GetField("m_method", BindingFlags.NonPublic | BindingFlags.Instance);
- handle = ((RuntimeMethodHandle)fieldInfo.GetValue(method));
- }
-
- return handle;
- }
-
- private static readonly CompareExchange2WordsDelegate compareExchange2Words = EmitCompareExchange2Words();
- private static readonly Func relJmpHooker = EmitRelJmpHooker();
- private static readonly Type rtDynamicMethodType = typeof(DynamicMethod).GetNestedType("RTDynamicMethod", BindingFlags.NonPublic);
- private static readonly Func m_ownerExtractor = FieldsExtractor.GetExtractor(rtDynamicMethodType.GetField("m_owner", BindingFlags.NonPublic | BindingFlags.Instance));
- }
-}
\ No newline at end of file
diff --git a/GrEmit/InstructionParameters/LabelsILInstructionParameter.cs b/GrEmit/InstructionParameters/LabelsILInstructionParameter.cs
index e788022..5a854a9 100644
--- a/GrEmit/InstructionParameters/LabelsILInstructionParameter.cs
+++ b/GrEmit/InstructionParameters/LabelsILInstructionParameter.cs
@@ -11,7 +11,7 @@ public LabelsILInstructionParameter(GroboIL.Label[] labels)
public override string Format()
{
- return string.Format("({0})", string.Join(", ", Labels.Select(label => label.Name).ToArray()));
+ return $"({string.Join(", ", Labels.Select(label => label.Name).ToArray())})";
}
public GroboIL.Label[] Labels { get; private set; }
diff --git a/GrEmit/MethodBodyParsing/ByteBuffer.cs b/GrEmit/MethodBodyParsing/ByteBuffer.cs
deleted file mode 100644
index 5e3de16..0000000
--- a/GrEmit/MethodBodyParsing/ByteBuffer.cs
+++ /dev/null
@@ -1,348 +0,0 @@
-//
-// Author:
-// Jb Evain (jbevain@gmail.com)
-//
-// Copyright (c) 2008 - 2015 Jb Evain
-// Copyright (c) 2008 - 2011 Novell, Inc.
-//
-// Licensed under the MIT/X11 license.
-//
-
-using System;
-
-namespace GrEmit.MethodBodyParsing
-{
- public class ByteBuffer
- {
- public ByteBuffer()
- {
- this.buffer = Empty.Array;
- }
-
- public ByteBuffer(int length)
- {
- this.buffer = new byte[length];
- }
-
- public ByteBuffer(byte[] buffer)
- {
- this.buffer = buffer ?? Empty.Array;
- this.length = this.buffer.Length;
- }
-
- public void Reset(byte[] buffer)
- {
- this.buffer = buffer ?? Empty.Array;
- this.length = this.buffer.Length;
- }
-
- public void Advance(int length)
- {
- position += length;
- }
-
- public byte ReadByte()
- {
- return buffer[position++];
- }
-
- public sbyte ReadSByte()
- {
- return (sbyte)ReadByte();
- }
-
- public byte[] ReadBytes(int length)
- {
- var bytes = new byte[length];
- Buffer.BlockCopy(buffer, position, bytes, 0, length);
- position += length;
- return bytes;
- }
-
- public ushort ReadUInt16()
- {
- ushort value = (ushort)(buffer[position]
- | (buffer[position + 1] << 8));
- position += 2;
- return value;
- }
-
- public short ReadInt16()
- {
- return (short)ReadUInt16();
- }
-
- public uint ReadUInt32()
- {
- uint value = (uint)(buffer[position]
- | (buffer[position + 1] << 8)
- | (buffer[position + 2] << 16)
- | (buffer[position + 3] << 24));
- position += 4;
- return value;
- }
-
- public int ReadInt32()
- {
- return (int)ReadUInt32();
- }
-
- public ulong ReadUInt64()
- {
- uint low = ReadUInt32();
- uint high = ReadUInt32();
-
- return (((ulong)high) << 32) | low;
- }
-
- public long ReadInt64()
- {
- return (long)ReadUInt64();
- }
-
- public uint ReadCompressedUInt32()
- {
- byte first = ReadByte();
- if ((first & 0x80) == 0)
- return first;
-
- if ((first & 0x40) == 0)
- {
- return ((uint)(first & ~0x80) << 8)
- | ReadByte();
- }
-
- return ((uint)(first & ~0xc0) << 24)
- | (uint)ReadByte() << 16
- | (uint)ReadByte() << 8
- | ReadByte();
- }
-
- public int ReadCompressedInt32()
- {
- var value = (int)(ReadCompressedUInt32() >> 1);
- if ((value & 1) == 0)
- return value;
- if (value < 0x40)
- return value - 0x40;
- if (value < 0x2000)
- return value - 0x2000;
- if (value < 0x10000000)
- return value - 0x10000000;
- return value - 0x20000000;
- }
-
- public float ReadSingle()
- {
- if (!BitConverter.IsLittleEndian)
- {
- var bytes = ReadBytes(4);
- Array.Reverse(bytes);
- return BitConverter.ToSingle(bytes, 0);
- }
-
- float value = BitConverter.ToSingle(buffer, position);
- position += 4;
- return value;
- }
-
- public double ReadDouble()
- {
- if (!BitConverter.IsLittleEndian)
- {
- var bytes = ReadBytes(8);
- Array.Reverse(bytes);
- return BitConverter.ToDouble(bytes, 0);
- }
-
- double value = BitConverter.ToDouble(buffer, position);
- position += 8;
- return value;
- }
-
- internal byte[] buffer;
- internal int length;
- internal int position;
-
-#if !READ_ONLY
-
- public void WriteByte(byte value)
- {
- if (position == buffer.Length)
- Grow(1);
-
- buffer[position++] = value;
-
- if (position > length)
- length = position;
- }
-
- public void WriteSByte(sbyte value)
- {
- WriteByte((byte)value);
- }
-
- public void WriteUInt16(ushort value)
- {
- if (position + 2 > buffer.Length)
- Grow(2);
-
- buffer[position++] = (byte)value;
- buffer[position++] = (byte)(value >> 8);
-
- if (position > length)
- length = position;
- }
-
- public void WriteInt16(short value)
- {
- WriteUInt16((ushort)value);
- }
-
- public void WriteUInt32(uint value)
- {
- if (position + 4 > buffer.Length)
- Grow(4);
-
- buffer[position++] = (byte)value;
- buffer[position++] = (byte)(value >> 8);
- buffer[position++] = (byte)(value >> 16);
- buffer[position++] = (byte)(value >> 24);
-
- if (position > length)
- length = position;
- }
-
- public void WriteInt32(int value)
- {
- WriteUInt32((uint)value);
- }
-
- public void WriteUInt64(ulong value)
- {
- if (position + 8 > buffer.Length)
- Grow(8);
-
- buffer[position++] = (byte)value;
- buffer[position++] = (byte)(value >> 8);
- buffer[position++] = (byte)(value >> 16);
- buffer[position++] = (byte)(value >> 24);
- buffer[position++] = (byte)(value >> 32);
- buffer[position++] = (byte)(value >> 40);
- buffer[position++] = (byte)(value >> 48);
- buffer[position++] = (byte)(value >> 56);
-
- if (position > length)
- length = position;
- }
-
- public void WriteInt64(long value)
- {
- WriteUInt64((ulong)value);
- }
-
- public void WriteCompressedUInt32(uint value)
- {
- if (value < 0x80)
- WriteByte((byte)value);
- else if (value < 0x4000)
- {
- WriteByte((byte)(0x80 | (value >> 8)));
- WriteByte((byte)(value & 0xff));
- }
- else
- {
- WriteByte((byte)((value >> 24) | 0xc0));
- WriteByte((byte)((value >> 16) & 0xff));
- WriteByte((byte)((value >> 8) & 0xff));
- WriteByte((byte)(value & 0xff));
- }
- }
-
- public void WriteCompressedInt32(int value)
- {
- if (value >= 0)
- {
- WriteCompressedUInt32((uint)(value << 1));
- return;
- }
-
- if (value > -0x40)
- value = 0x40 + value;
- else if (value >= -0x2000)
- value = 0x2000 + value;
- else if (value >= -0x20000000)
- value = 0x20000000 + value;
-
- WriteCompressedUInt32((uint)((value << 1) | 1));
- }
-
- public void WriteBytes(byte[] bytes)
- {
- var length = bytes.Length;
- if (position + length > buffer.Length)
- Grow(length);
-
- Buffer.BlockCopy(bytes, 0, buffer, position, length);
- position += length;
-
- if (position > this.length)
- this.length = position;
- }
-
- public void WriteBytes(int length)
- {
- if (position + length > buffer.Length)
- Grow(length);
-
- position += length;
-
- if (position > this.length)
- this.length = position;
- }
-
- public void WriteBytes(ByteBuffer buffer)
- {
- if (position + buffer.length > this.buffer.Length)
- Grow(buffer.length);
-
- Buffer.BlockCopy(buffer.buffer, 0, this.buffer, position, buffer.length);
- position += buffer.length;
-
- if (position > this.length)
- this.length = position;
- }
-
- public void WriteSingle(float value)
- {
- var bytes = BitConverter.GetBytes(value);
-
- if (!BitConverter.IsLittleEndian)
- Array.Reverse(bytes);
-
- WriteBytes(bytes);
- }
-
- public void WriteDouble(double value)
- {
- var bytes = BitConverter.GetBytes(value);
-
- if (!BitConverter.IsLittleEndian)
- Array.Reverse(bytes);
-
- WriteBytes(bytes);
- }
-
- void Grow(int desired)
- {
- var current = this.buffer;
- var current_length = current.Length;
-
- var buffer = new byte[Math.Max(current_length + desired, current_length * 2)];
- Buffer.BlockCopy(current, 0, buffer, 0, current_length);
- this.buffer = buffer;
- }
-
-#endif
- }
-}
\ No newline at end of file
diff --git a/GrEmit/MethodBodyParsing/Code.cs b/GrEmit/MethodBodyParsing/Code.cs
deleted file mode 100644
index 1a98f8f..0000000
--- a/GrEmit/MethodBodyParsing/Code.cs
+++ /dev/null
@@ -1,235 +0,0 @@
-//
-// Author:
-// Jb Evain (jbevain@gmail.com)
-//
-// Copyright (c) 2008 - 2015 Jb Evain
-// Copyright (c) 2008 - 2011 Novell, Inc.
-//
-// Licensed under the MIT/X11 license.
-//
-
-namespace GrEmit.MethodBodyParsing
-{
- public enum Code
- {
- Nop,
- Break,
- Ldarg_0,
- Ldarg_1,
- Ldarg_2,
- Ldarg_3,
- Ldloc_0,
- Ldloc_1,
- Ldloc_2,
- Ldloc_3,
- Stloc_0,
- Stloc_1,
- Stloc_2,
- Stloc_3,
- Ldarg_S,
- Ldarga_S,
- Starg_S,
- Ldloc_S,
- Ldloca_S,
- Stloc_S,
- Ldnull,
- Ldc_I4_M1,
- Ldc_I4_0,
- Ldc_I4_1,
- Ldc_I4_2,
- Ldc_I4_3,
- Ldc_I4_4,
- Ldc_I4_5,
- Ldc_I4_6,
- Ldc_I4_7,
- Ldc_I4_8,
- Ldc_I4_S,
- Ldc_I4,
- Ldc_I8,
- Ldc_R4,
- Ldc_R8,
- Dup,
- Pop,
- Jmp,
- Call,
- Calli,
- Ret,
- Br_S,
- Brfalse_S,
- Brtrue_S,
- Beq_S,
- Bge_S,
- Bgt_S,
- Ble_S,
- Blt_S,
- Bne_Un_S,
- Bge_Un_S,
- Bgt_Un_S,
- Ble_Un_S,
- Blt_Un_S,
- Br,
- Brfalse,
- Brtrue,
- Beq,
- Bge,
- Bgt,
- Ble,
- Blt,
- Bne_Un,
- Bge_Un,
- Bgt_Un,
- Ble_Un,
- Blt_Un,
- Switch,
- Ldind_I1,
- Ldind_U1,
- Ldind_I2,
- Ldind_U2,
- Ldind_I4,
- Ldind_U4,
- Ldind_I8,
- Ldind_I,
- Ldind_R4,
- Ldind_R8,
- Ldind_Ref,
- Stind_Ref,
- Stind_I1,
- Stind_I2,
- Stind_I4,
- Stind_I8,
- Stind_R4,
- Stind_R8,
- Add,
- Sub,
- Mul,
- Div,
- Div_Un,
- Rem,
- Rem_Un,
- And,
- Or,
- Xor,
- Shl,
- Shr,
- Shr_Un,
- Neg,
- Not,
- Conv_I1,
- Conv_I2,
- Conv_I4,
- Conv_I8,
- Conv_R4,
- Conv_R8,
- Conv_U4,
- Conv_U8,
- Callvirt,
- Cpobj,
- Ldobj,
- Ldstr,
- Newobj,
- Castclass,
- Isinst,
- Conv_R_Un,
- Unbox,
- Throw,
- Ldfld,
- Ldflda,
- Stfld,
- Ldsfld,
- Ldsflda,
- Stsfld,
- Stobj,
- Conv_Ovf_I1_Un,
- Conv_Ovf_I2_Un,
- Conv_Ovf_I4_Un,
- Conv_Ovf_I8_Un,
- Conv_Ovf_U1_Un,
- Conv_Ovf_U2_Un,
- Conv_Ovf_U4_Un,
- Conv_Ovf_U8_Un,
- Conv_Ovf_I_Un,
- Conv_Ovf_U_Un,
- Box,
- Newarr,
- Ldlen,
- Ldelema,
- Ldelem_I1,
- Ldelem_U1,
- Ldelem_I2,
- Ldelem_U2,
- Ldelem_I4,
- Ldelem_U4,
- Ldelem_I8,
- Ldelem_I,
- Ldelem_R4,
- Ldelem_R8,
- Ldelem_Ref,
- Stelem_I,
- Stelem_I1,
- Stelem_I2,
- Stelem_I4,
- Stelem_I8,
- Stelem_R4,
- Stelem_R8,
- Stelem_Ref,
- Ldelem_Any,
- Stelem_Any,
- Unbox_Any,
- Conv_Ovf_I1,
- Conv_Ovf_U1,
- Conv_Ovf_I2,
- Conv_Ovf_U2,
- Conv_Ovf_I4,
- Conv_Ovf_U4,
- Conv_Ovf_I8,
- Conv_Ovf_U8,
- Refanyval,
- Ckfinite,
- Mkrefany,
- Ldtoken,
- Conv_U2,
- Conv_U1,
- Conv_I,
- Conv_Ovf_I,
- Conv_Ovf_U,
- Add_Ovf,
- Add_Ovf_Un,
- Mul_Ovf,
- Mul_Ovf_Un,
- Sub_Ovf,
- Sub_Ovf_Un,
- Endfinally,
- Leave,
- Leave_S,
- Stind_I,
- Conv_U,
- Arglist,
- Ceq,
- Cgt,
- Cgt_Un,
- Clt,
- Clt_Un,
- Ldftn,
- Ldvirtftn,
- Ldarg,
- Ldarga,
- Starg,
- Ldloc,
- Ldloca,
- Stloc,
- Localloc,
- Endfilter,
- Unaligned,
- Volatile,
- Tail,
- Initobj,
- Constrained,
- Cpblk,
- Initblk,
- No,
- Rethrow,
- Sizeof,
- Refanytype,
- Readonly,
- }
-}
\ No newline at end of file
diff --git a/GrEmit/MethodBodyParsing/Collection.cs b/GrEmit/MethodBodyParsing/Collection.cs
deleted file mode 100644
index be9786f..0000000
--- a/GrEmit/MethodBodyParsing/Collection.cs
+++ /dev/null
@@ -1,432 +0,0 @@
-//
-// Author:
-// Jb Evain (jbevain@gmail.com)
-//
-// Copyright (c) 2008 - 2015 Jb Evain
-// Copyright (c) 2008 - 2011 Novell, Inc.
-//
-// Licensed under the MIT/X11 license.
-//
-
-using System;
-using System.Collections;
-using System.Collections.Generic;
-
-namespace GrEmit.MethodBodyParsing
-{
- public class Collection : IList, IList
- {
- public Collection()
- {
- items = Empty.Array;
- }
-
- public Collection(int capacity)
- {
- if (capacity < 0)
- throw new ArgumentOutOfRangeException();
-
- items = new T[capacity];
- }
-
- public Collection(ICollection items)
- {
- if (items == null)
- throw new ArgumentNullException("items");
-
- this.items = new T[items.Count];
- items.CopyTo(this.items, 0);
- this.size = this.items.Length;
- }
-
- public int Count { get { return size; } }
-
- public T this[int index]
- {
- get
- {
- if (index >= size)
- throw new ArgumentOutOfRangeException();
-
- return items[index];
- }
- set
- {
- CheckIndex(index);
- if (index == size)
- throw new ArgumentOutOfRangeException();
-
- OnSet(value, index);
-
- items[index] = value;
- }
- }
-
- public int Capacity
- {
- get { return items.Length; }
- set
- {
- if (value < 0 || value < size)
- throw new ArgumentOutOfRangeException();
-
- Resize(value);
- }
- }
-
- bool ICollection.IsReadOnly { get { return false; } }
-
- bool IList.IsFixedSize { get { return false; } }
-
- bool IList.IsReadOnly { get { return false; } }
-
- object IList.this[int index]
- {
- get { return this[index]; }
- set
- {
- CheckIndex(index);
-
- try
- {
- this[index] = (T)value;
- return;
- }
- catch (InvalidCastException)
- {
- }
- catch (NullReferenceException)
- {
- }
-
- throw new ArgumentException();
- }
- }
-
- int ICollection.Count { get { return Count; } }
-
- bool ICollection.IsSynchronized { get { return false; } }
-
- object ICollection.SyncRoot { get { return this; } }
-
- public void Add(T item)
- {
- if (size == items.Length)
- Grow(1);
-
- OnAdd(item, size);
-
- items[size++] = item;
- version++;
- }
-
- public bool Contains(T item)
- {
- return IndexOf(item) != -1;
- }
-
- public int IndexOf(T item)
- {
- return Array.IndexOf(items, item, 0, size);
- }
-
- public void Insert(int index, T item)
- {
- CheckIndex(index);
- if (size == items.Length)
- Grow(1);
-
- OnInsert(item, index);
-
- Shift(index, 1);
- items[index] = item;
- version++;
- }
-
- public void RemoveAt(int index)
- {
- if (index < 0 || index >= size)
- throw new ArgumentOutOfRangeException();
-
- var item = items[index];
-
- OnRemove(item, index);
-
- Shift(index, -1);
- version++;
- }
-
- public bool Remove(T item)
- {
- var index = IndexOf(item);
- if (index == -1)
- return false;
-
- OnRemove(item, index);
-
- Shift(index, -1);
- version++;
-
- return true;
- }
-
- public void Clear()
- {
- OnClear();
-
- Array.Clear(items, 0, size);
- size = 0;
- version++;
- }
-
- public void CopyTo(T[] array, int arrayIndex)
- {
- Array.Copy(items, 0, array, arrayIndex, size);
- }
-
- public T[] ToArray()
- {
- var array = new T[size];
- Array.Copy(items, 0, array, 0, size);
- return array;
- }
-
- void CheckIndex(int index)
- {
- if (index < 0 || index > size)
- throw new ArgumentOutOfRangeException();
- }
-
- void Shift(int start, int delta)
- {
- if (delta < 0)
- start -= delta;
-
- if (start < size)
- Array.Copy(items, start, items, start + delta, size - start);
-
- size += delta;
-
- if (delta < 0)
- Array.Clear(items, size, -delta);
- }
-
- protected virtual void OnAdd(T item, int index)
- {
- }
-
- protected virtual void OnInsert(T item, int index)
- {
- }
-
- protected virtual void OnSet(T item, int index)
- {
- }
-
- protected virtual void OnRemove(T item, int index)
- {
- }
-
- protected virtual void OnClear()
- {
- }
-
- internal virtual void Grow(int desired)
- {
- int new_size = size + desired;
- if (new_size <= items.Length)
- return;
-
- const int default_capacity = 4;
-
- new_size = Math.Max(
- Math.Max(items.Length * 2, default_capacity),
- new_size);
-
- Resize(new_size);
- }
-
- protected void Resize(int new_size)
- {
- if (new_size == size)
- return;
- if (new_size < size)
- throw new ArgumentOutOfRangeException();
-
- items = items.Resize(new_size);
- }
-
- int IList.Add(object value)
- {
- try
- {
- Add((T)value);
- return size - 1;
- }
- catch (InvalidCastException)
- {
- }
- catch (NullReferenceException)
- {
- }
-
- throw new ArgumentException();
- }
-
- void IList.Clear()
- {
- Clear();
- }
-
- bool IList.Contains(object value)
- {
- return ((IList)this).IndexOf(value) > -1;
- }
-
- int IList.IndexOf(object value)
- {
- try
- {
- return IndexOf((T)value);
- }
- catch (InvalidCastException)
- {
- }
- catch (NullReferenceException)
- {
- }
-
- return -1;
- }
-
- void IList.Insert(int index, object value)
- {
- CheckIndex(index);
-
- try
- {
- Insert(index, (T)value);
- return;
- }
- catch (InvalidCastException)
- {
- }
- catch (NullReferenceException)
- {
- }
-
- throw new ArgumentException();
- }
-
- void IList.Remove(object value)
- {
- try
- {
- Remove((T)value);
- }
- catch (InvalidCastException)
- {
- }
- catch (NullReferenceException)
- {
- }
- }
-
- void IList.RemoveAt(int index)
- {
- RemoveAt(index);
- }
-
- void ICollection.CopyTo(Array array, int index)
- {
- Array.Copy(items, 0, array, index, size);
- }
-
- public Enumerator GetEnumerator()
- {
- return new Enumerator(this);
- }
-
- IEnumerator IEnumerable.GetEnumerator()
- {
- return new Enumerator(this);
- }
-
- IEnumerator IEnumerable.GetEnumerator()
- {
- return new Enumerator(this);
- }
-
- public struct Enumerator : IEnumerator, IDisposable
- {
- internal Enumerator(Collection collection)
- : this()
- {
- this.collection = collection;
- this.version = collection.version;
- }
-
- public T Current { get; private set; }
-
- object IEnumerator.Current
- {
- get
- {
- CheckState();
-
- if (next <= 0)
- throw new InvalidOperationException();
-
- return Current;
- }
- }
-
- public bool MoveNext()
- {
- CheckState();
-
- if (next < 0)
- return false;
-
- if (next < collection.size)
- {
- Current = collection.items[next++];
- return true;
- }
-
- next = -1;
- return false;
- }
-
- public void Reset()
- {
- CheckState();
-
- next = 0;
- }
-
- void CheckState()
- {
- if (collection == null)
- throw new ObjectDisposedException(GetType().FullName);
-
- if (version != collection.version)
- throw new InvalidOperationException();
- }
-
- public void Dispose()
- {
- collection = null;
- }
-
- Collection collection;
-
- int next;
- readonly int version;
- }
-
- internal T[] items;
- internal int size;
- int version;
- }
-}
\ No newline at end of file
diff --git a/GrEmit/MethodBodyParsing/DynamicILGenerator.cs b/GrEmit/MethodBodyParsing/DynamicILGenerator.cs
deleted file mode 100644
index 20b7790..0000000
--- a/GrEmit/MethodBodyParsing/DynamicILGenerator.cs
+++ /dev/null
@@ -1,42 +0,0 @@
-using System;
-using System.Reflection;
-using System.Reflection.Emit;
-
-namespace GrEmit.MethodBodyParsing
-{
- internal class DynamicILGenerator
- {
- static DynamicILGenerator()
- {
- var assembly = typeof(DynamicMethod).Assembly;
- var dynamicILGeneratorType = assembly.GetType("System.Reflection.Emit.DynamicILGenerator");
-
- var m_methodSigTokenField = dynamicILGeneratorType.GetField("m_methodSigToken", BindingFlags.Instance | BindingFlags.NonPublic);
- if (m_methodSigTokenField == null)
- throw new InvalidOperationException("Field 'DynamicILGenerator.m_methodSigToken' is not found");
- m_methodSigTokenExtractor = FieldsExtractor.GetExtractor(m_methodSigTokenField);
-
- var m_scopeField = dynamicILGeneratorType.GetField("m_scope", BindingFlags.Instance | BindingFlags.NonPublic);
- if (m_scopeField == null)
- throw new InvalidOperationException("Field 'DynamicILGenerator.m_scope' is not found");
- m_scopeExtractor = FieldsExtractor.GetExtractor(m_scopeField);
- }
-
- public DynamicILGenerator(ILGenerator inst)
- {
- this.inst = inst;
- }
-
- public int m_methodSigToken => m_methodSigTokenExtractor(inst);
- public DynamicScope m_scope => new DynamicScope(m_scopeExtractor(inst));
-
- public static void Init()
- {
- }
-
- public ILGenerator inst;
-
- private static readonly Func m_methodSigTokenExtractor;
- private static readonly Func m_scopeExtractor;
- }
-}
\ No newline at end of file
diff --git a/GrEmit/MethodBodyParsing/DynamicILInfoWrapper.cs b/GrEmit/MethodBodyParsing/DynamicILInfoWrapper.cs
deleted file mode 100644
index 27a0df9..0000000
--- a/GrEmit/MethodBodyParsing/DynamicILInfoWrapper.cs
+++ /dev/null
@@ -1,69 +0,0 @@
-using System;
-using System.Reflection;
-
-namespace GrEmit.MethodBodyParsing
-{
- internal class DynamicILInfoWrapper
- {
- static DynamicILInfoWrapper()
- {
- var m_methodSignatureField = typeof(GrEmit.Utils.DynamicILInfo).GetField("m_methodSignature", BindingFlags.Instance | BindingFlags.NonPublic);
- if (m_methodSignatureField == null)
- throw new InvalidOperationException("Field 'DynamicILInfo.m_methodSignature' is not found");
- m_methodSignatureExtractor = FieldsExtractor.GetExtractor(m_methodSignatureField);
-
- var m_scopeField = typeof(GrEmit.Utils.DynamicILInfo).GetField("m_scope", BindingFlags.Instance | BindingFlags.NonPublic);
- if (m_scopeField == null)
- throw new InvalidOperationException("Field 'DynamicILInfo.m_scope' is not found");
- m_scopeExtractor = FieldsExtractor.GetExtractor(m_scopeField);
- }
-
- public DynamicILInfoWrapper(GrEmit.Utils.DynamicILInfo inst)
- {
- this.inst = inst;
- }
-
- public int m_methodSignature => m_methodSignatureExtractor(inst);
- public DynamicScope m_scope => new DynamicScope(m_scopeExtractor(inst));
-
- public static void Init()
- {
- }
-
- public MetadataToken GetTokenFor(OpCode opCode, object value)
- {
- if (value is MetadataToken)
- return (MetadataToken)value;
- if (value is MethodBase)
- return GetTokenForMethod((MethodBase)value, opCode);
- if (value is FieldInfo)
- {
- var field = (FieldInfo)value;
- if (field.DeclaringType != null && field.DeclaringType.IsGenericType)
- return new MetadataToken((uint)inst.GetTokenFor(field.FieldHandle, field.DeclaringType.TypeHandle));
- return new MetadataToken((uint)inst.GetTokenFor(field.FieldHandle));
- }
- if (value is Type)
- return new MetadataToken((uint)inst.GetTokenFor(((Type)value).TypeHandle));
- if (value is byte[])
- return new MetadataToken((uint)inst.GetTokenFor((byte[])value));
- if (value is string)
- return new MetadataToken((uint)inst.GetTokenFor((string)value));
- throw new InvalidOperationException($"Unable to build token for {value.GetType()}");
- }
-
- private MetadataToken GetTokenForMethod(MethodBase methodBase, OpCode opcode)
- {
- if (opcode == OpCodes.Call || opcode == OpCodes.Callvirt)
- return m_scope.GetTokenFor(methodBase, MetadataExtensions.BuildMemberRefSignature(methodBase));
- if (methodBase.DeclaringType != null && methodBase.DeclaringType.IsGenericType)
- return new MetadataToken((uint)inst.GetTokenFor(methodBase.MethodHandle, methodBase.DeclaringType.TypeHandle));
- return new MetadataToken((uint)inst.GetTokenFor(methodBase.MethodHandle));
- }
-
- public GrEmit.Utils.DynamicILInfo inst;
-
- private static readonly Func m_methodSignatureExtractor;
- private static readonly Func m_scopeExtractor;
- }
-}
\ No newline at end of file
diff --git a/GrEmit/MethodBodyParsing/DynamicMethodWrapper.cs b/GrEmit/MethodBodyParsing/DynamicMethodWrapper.cs
deleted file mode 100644
index e7ebba0..0000000
--- a/GrEmit/MethodBodyParsing/DynamicMethodWrapper.cs
+++ /dev/null
@@ -1,97 +0,0 @@
-using System;
-using System.Reflection;
-using System.Reflection.Emit;
-
-namespace GrEmit.MethodBodyParsing
-{
- internal class DynamicMethodWrapper
- {
- static DynamicMethodWrapper()
- {
- var assembly = typeof(DynamicMethod).Assembly;
- dynamicScopeType = assembly.GetType("System.Reflection.Emit.DynamicScope");
-
- var m_DynamicILInfoField = typeof(DynamicMethod).GetField("m_DynamicILInfo", BindingFlags.Instance | BindingFlags.NonPublic);
- if (m_DynamicILInfoField == null)
- throw new InvalidOperationException("Field 'DynamicMethod.m_DynamicILInfo' is not found");
- m_DynamicILInfoExtractor = FieldsExtractor.GetExtractor(m_DynamicILInfoField);
-
- var m_ilGeneratorField = typeof(DynamicMethod).GetField("m_ilGenerator", BindingFlags.Instance | BindingFlags.NonPublic);
- if (m_ilGeneratorField == null)
- throw new InvalidOperationException("Field 'DynamicMethod.m_ilGenerator' is not found");
- m_ilGeneratorExtractor = FieldsExtractor.GetExtractor(m_ilGeneratorField);
-
- getDynamicIlInfo = BuildGetDynamicILInfo();
- }
-
- public DynamicMethodWrapper(DynamicMethod inst)
- {
- this.inst = inst;
- }
-
- private static Func BuildGetDynamicILInfo()
- {
- var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(GrEmit.Utils.DynamicILInfo), new[] {typeof(DynamicMethod), typeof(object)}, typeof(string), true);
- using (var il = new GroboIL(method))
- {
- il.Ldarg(0); // stack: [dynamicMethod]
- il.Ldarg(1); // stack: [dynamicMethod, scope]
- il.Castclass(dynamicScopeType);
- var getDynamicILInfoMethod = typeof(DynamicMethod).GetMethod("GetDynamicILInfo", BindingFlags.Instance | BindingFlags.NonPublic);
- if (getDynamicILInfoMethod == null)
- throw new MissingMethodException("DynamicMethod", "GetDynamicILInfo");
- il.Call(getDynamicILInfoMethod); // stack: [dynamicMethod.GetDynamicILInfo(scope)]
- il.Ret();
- }
-
- return (Func)method.CreateDelegate(typeof(Func));
- }
-
- public GrEmit.Utils.DynamicILInfo m_DynamicILInfo => m_DynamicILInfoExtractor(inst);
- public ILGenerator m_ilGenerator => m_ilGeneratorExtractor(inst);
-
- public static void Init()
- {
- }
-
- public GrEmit.Utils.DynamicILInfo GetDynamicILInfoWithOldScope()
- {
- return m_DynamicILInfo ?? (m_ilGenerator == null ? GetDynamicILInfo() : getDynamicIlInfo(inst, new DynamicILGenerator(m_ilGenerator).m_scope.inst));
- }
-
- private GrEmit.Utils.DynamicILInfo GetDynamicILInfo()
- {
- if (info == null)
- {
- info = CreateDynamicILInfo(new GrEmit.Utils.DynamicScope());
- }
-
- return info;
- }
-
- private GrEmit.Utils.DynamicILInfo CreateDynamicILInfo(GrEmit.Utils.DynamicScope scope)
- {
- SignatureHelper helper = SignatureHelper.GetMethodSigHelper(inst.CallingConvention, inst.ReturnType);
- foreach (var parameter in inst.GetParameters())
- {
- helper.AddArgument(parameter.ParameterType);
- }
- byte[] methodSignature = helper.GetSignature();
-
- // Have to do this, since needed method is internal
- byte[] methodSignatureWithEnd = new byte[methodSignature.Length + 1];
- Array.Copy(methodSignature, methodSignatureWithEnd, methodSignature.Length);
- methodSignatureWithEnd[methodSignatureWithEnd.Length - 1] = 0;
-
- return new GrEmit.Utils.DynamicILInfo(scope, inst, methodSignatureWithEnd);
- }
-
- public DynamicMethod inst;
- public GrEmit.Utils.DynamicILInfo info;
-
- private static readonly Func m_DynamicILInfoExtractor;
- private static readonly Func m_ilGeneratorExtractor;
- private static readonly Type dynamicScopeType;
- private static readonly Func getDynamicIlInfo;
- }
-}
\ No newline at end of file
diff --git a/GrEmit/MethodBodyParsing/DynamicResolver.cs b/GrEmit/MethodBodyParsing/DynamicResolver.cs
deleted file mode 100644
index 4911079..0000000
--- a/GrEmit/MethodBodyParsing/DynamicResolver.cs
+++ /dev/null
@@ -1,186 +0,0 @@
-using System;
-using System.Reflection;
-using System.Reflection.Emit;
-
-namespace GrEmit.MethodBodyParsing
-{
- internal class DynamicResolver : IDisposable
- {
- static DynamicResolver()
- {
- var assembly = typeof(DynamicMethod).Assembly;
- dynamicResolverType = assembly.GetType("System.Reflection.Emit.DynamicResolver");
- dynamicILInfoType = assembly.GetType("System.Reflection.Emit.DynamicILInfo");
- dynamicILGeneratorType = assembly.GetType("System.Reflection.Emit.DynamicILGenerator");
- BuildFactoryByDynamicILInfo();
- BuildFactoryByDynamicILGenerator();
- BuildGetCodeInfoDelegate();
- BuildGetRawEHInfoDelegate();
- BuildGetEHInfoDelegate();
-
- var m_methodField = dynamicResolverType.GetField("m_method", BindingFlags.Instance | BindingFlags.NonPublic);
- if (m_methodField == null)
- throw new InvalidOperationException("Field 'DynamicResolver.m_methodField' is not found");
- m_methodSetter = FieldsExtractor.GetSetter(m_methodField);
-
- var m_resolverField = typeof(DynamicMethod).GetField("m_resolver", BindingFlags.Instance | BindingFlags.NonPublic);
- if (m_resolverField == null)
- throw new InvalidOperationException("Field 'DynamicResolver.m_resolver' is not found");
- m_resolverSetter = FieldsExtractor.GetSetter(m_resolverField);
-
- var m_localSignatureField = dynamicResolverType.GetField("m_localSignature", BindingFlags.Instance | BindingFlags.NonPublic);
- if (m_localSignatureField == null)
- throw new InvalidOperationException("Field 'DynamicResolver.m_localSignature' is not found");
- m_localSignatureExtractor = FieldsExtractor.GetExtractor