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

Commit

Permalink
Add System.Reflection.Emit.DynamicILInfo (#21945)
Browse files Browse the repository at this point in the history
* Making DynamicILInfo and its API impls public

* Remove DynamicScope input from ctor args
  • Loading branch information
maryamariyan authored and jkotas committed Jan 13, 2019
1 parent c4e4036 commit 908891c
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
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(DynamicMethod method, byte[] methodSignature)
{
m_scope = new DynamicScope();
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,92 @@ 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 Span<byte>(code, codeSize).ToArray();
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 Span<byte>(exceptions, exceptionsSize).ToArray();
}

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 Span<byte>(localSignature, signatureSize).ToArray();
}
#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,17 @@ public ParameterBuilder DefineParameter(int position, ParameterAttributes attrib
return null;
}

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

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

0 comments on commit 908891c

Please sign in to comment.