Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Add System.Reflection.Emit.DynamicILInfo #21945

Merged
merged 7 commits into from
Jan 13, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ internal override MethodInfo GetDynamicMethod()
}


internal class DynamicILInfo
public class DynamicILInfo
{
#region Private Data Members
private DynamicMethod m_method;
Expand All @@ -875,6 +875,18 @@ internal class DynamicILInfo
private int m_methodSignature;
#endregion

#region Constructor
internal DynamicILInfo(object scope, DynamicMethod method, byte[] methodSignature)
{
m_scope = scope as DynamicScope ?? throw new InvalidCastException(nameof(scope));
m_method = method;
m_methodSignature = m_scope.GetTokenFor(methodSignature);
m_exceptions = Array.Empty<byte>();
m_code = Array.Empty<byte>();
m_localSignature = Array.Empty<byte>();
}
#endregion

#region Internal Methods
internal void GetCallableMethod(RuntimeModule module, DynamicMethod dm)
{
Expand All @@ -901,8 +913,109 @@ internal byte[] LocalSignature
public DynamicMethod DynamicMethod { get { return m_method; } }
internal DynamicScope DynamicScope { get { return m_scope; } }

public void SetCode(byte[] code, int maxStackSize)
{
m_code = (code != null) ? (byte[])code.Clone() : Array.Empty<byte>();
m_maxStackSize = maxStackSize;
}

[CLSCompliant(false)]
public unsafe void SetCode(byte* code, int codeSize, int maxStackSize)
{
if (codeSize < 0)
throw new ArgumentOutOfRangeException(nameof(codeSize), SR.ArgumentOutOfRange_GenericPositive);
if (codeSize > 0 && code == null)
throw new ArgumentNullException(nameof(code));

m_code = new byte[codeSize];
for (int i = 0; i < codeSize; i++)
{
m_code[i] = *code;
code++;
}

m_maxStackSize = maxStackSize;
}

public void SetExceptions(byte[] exceptions)
{
m_exceptions = (exceptions != null) ? (byte[])exceptions.Clone() : Array.Empty<byte>();
}

[CLSCompliant(false)]
public unsafe void SetExceptions(byte* exceptions, int exceptionsSize)
{
if (exceptionsSize < 0)
throw new ArgumentOutOfRangeException(nameof(exceptionsSize), SR.ArgumentOutOfRange_GenericPositive);

if (exceptionsSize > 0 && exceptions == null)
throw new ArgumentNullException(nameof(exceptions));

m_exceptions = new byte[exceptionsSize];

for (int i = 0; i < exceptionsSize; i++)
{
m_exceptions[i] = *exceptions;
exceptions++;
}
}

public void SetLocalSignature(byte[] localSignature)
{
m_localSignature = (localSignature != null) ? (byte[])localSignature.Clone() : Array.Empty<byte>();
}

[CLSCompliant(false)]
public unsafe void SetLocalSignature(byte* localSignature, int signatureSize)
{
if (signatureSize < 0)
throw new ArgumentOutOfRangeException(nameof(signatureSize), SR.ArgumentOutOfRange_GenericPositive);

if (signatureSize > 0 && localSignature == null)
throw new ArgumentNullException(nameof(localSignature));

m_localSignature = new byte[signatureSize];
for (int i = 0; i < signatureSize; i++)
{
m_localSignature[i] = *localSignature;
localSignature++;
}
}
#endregion

#region Public Scope Methods
public int GetTokenFor(RuntimeMethodHandle method)
{
return DynamicScope.GetTokenFor(method);
}
public int GetTokenFor(DynamicMethod method)
{
return DynamicScope.GetTokenFor(method);
}
public int GetTokenFor(RuntimeMethodHandle method, RuntimeTypeHandle contextType)
{
return DynamicScope.GetTokenFor(method, contextType);
}
public int GetTokenFor(RuntimeFieldHandle field)
{
return DynamicScope.GetTokenFor(field);
}
public int GetTokenFor(RuntimeFieldHandle field, RuntimeTypeHandle contextType)
{
return DynamicScope.GetTokenFor(field, contextType);
}
public int GetTokenFor(RuntimeTypeHandle type)
{
return DynamicScope.GetTokenFor(type);
}
public int GetTokenFor(string literal)
{
return DynamicScope.GetTokenFor(literal);
}
public int GetTokenFor(byte[] signature)
{
return DynamicScope.GetTokenFor(signature);
}
#endregion
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,18 @@ public ParameterBuilder DefineParameter(int position, ParameterAttributes attrib
return null;
}

public DynamicILInfo GetDynamicILInfo()
{
if (m_DynamicILInfo == null)
{
var scope = new DynamicScope();
byte[] methodSignature = SignatureHelper.GetMethodSigHelper(
null, CallingConvention, ReturnType, null, null, m_parameterTypes, null, null).GetSignature(true);
m_DynamicILInfo = new DynamicILInfo(scope, this, methodSignature);
}
return m_DynamicILInfo;
}

public ILGenerator GetILGenerator()
{
return GetILGenerator(64);
Expand Down