Skip to content

Commit

Permalink
Added support for using event keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
liiir1985 committed Oct 7, 2022
1 parent 10a5df4 commit c6715cc
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 116 deletions.
30 changes: 30 additions & 0 deletions ILRuntime/CLR/Method/ILMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public sealed class ILMethod : IMethod
IType[] genericArguments;
Dictionary<int, int[]> jumptables, jumptablesR;
bool isDelegateInvoke;
bool isEventAdd, isEventRemove;
int eventFieldIndex;
bool jitPending;
ILRuntimeMethodInfo refletionMethodInfo;
ILRuntimeConstructorInfo reflectionCtorInfo;
Expand Down Expand Up @@ -347,6 +349,27 @@ public bool IsDelegateInvoke
}
}

public bool IsEventAdd
{
get
{
return isEventAdd;
}
}

public bool IsEventRemove
{
get
{
return isEventRemove;
}
}

public int EventFieldIndex
{
get { return eventFieldIndex; }
}

public bool IsStatic
{
get { return def.IsStatic; }
Expand Down Expand Up @@ -797,6 +820,13 @@ unsafe void InitToken(ref OpCode code, object token, Dictionary<Mono.Cecil.Cil.I
}
}

public void SetEventAddOrRemove(bool isEventAdd, bool isEventRemove, int fieldIdx)
{
this.isEventRemove = isEventRemove;
this.isEventAdd = isEventAdd;
eventFieldIndex = fieldIdx;
}

internal int GetTypeTokenHashCode(object token)
{
var t = appdomain.GetType(token, declaringType, this);
Expand Down
58 changes: 38 additions & 20 deletions ILRuntime/CLR/TypeSystem/ILType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -716,52 +716,70 @@ public IMethod GetMethod ( string name, int paramCount, bool declaredOnly = fals
}
}

void InitializeMethods ()
void InitializeMethods()
{
methods = new Dictionary<string, List<ILMethod>> ();
constructors = new List<ILMethod> ();
if ( definition == null )
methods = new Dictionary<string, List<ILMethod>>();
constructors = new List<ILMethod>();
if (definition == null)
return;
if ( definition.HasCustomAttributes )
if (definition.HasCustomAttributes)
{
for ( int i = 0; i < definition.CustomAttributes.Count; i++ )
for (int i = 0; i < definition.CustomAttributes.Count; i++)
{
int f;
if ( definition.CustomAttributes [ i ].GetJITFlags ( AppDomain, out f ) )
if (definition.CustomAttributes[i].GetJITFlags(AppDomain, out f))
{
this.jitFlags = f;
break;
}
}
}
foreach ( var i in definition.Methods )
foreach (var i in definition.Methods)
{
if ( i.IsConstructor )
if (i.IsConstructor)
{
if ( i.IsStatic )
staticConstructor = new ILMethod ( i, this, appdomain, jitFlags );
if (i.IsStatic)
staticConstructor = new ILMethod(i, this, appdomain, jitFlags);
else
constructors.Add ( new ILMethod ( i, this, appdomain, jitFlags ) );
constructors.Add(new ILMethod(i, this, appdomain, jitFlags));
}
else
{
List<ILMethod> lst;
if ( !methods.TryGetValue ( i.Name, out lst ) )
if (!methods.TryGetValue(i.Name, out lst))
{
lst = new List<ILMethod> ();
methods [ i.Name ] = lst;
lst = new List<ILMethod>();
methods[i.Name] = lst;
}
var m = new ILMethod ( i, this, appdomain, jitFlags );
lst.Add ( m );
var m = new ILMethod(i, this, appdomain, jitFlags);
lst.Add(m);
}
}

foreach (var i in definition.Events)
{
int fieldIdx = -1;
InitializeFields();
if(i.AddMethod.IsStatic)
staticFieldMapping.TryGetValue(i.Name,out fieldIdx);
else
fieldMapping.TryGetValue(i.Name, out fieldIdx);
if (methods.TryGetValue(i.AddMethod.Name, out var lst))
{
lst[0].SetEventAddOrRemove(true, false, fieldIdx);
}
if (methods.TryGetValue(i.RemoveMethod.Name, out lst))
{
lst[0].SetEventAddOrRemove(false, true, fieldIdx);
}
}

if ( !appdomain.SuppressStaticConstructor && !staticConstructorCalled )
if (!appdomain.SuppressStaticConstructor && !staticConstructorCalled)
{
staticConstructorCalled = true;
if ( staticConstructor != null && ( !TypeReference.HasGenericParameters || IsGenericInstance ) )
if (staticConstructor != null && (!TypeReference.HasGenericParameters || IsGenericInstance))
{
appdomain.Invoke ( staticConstructor, null, null );
appdomain.Invoke(staticConstructor, null, null);
}
}
}
Expand Down
Loading

0 comments on commit c6715cc

Please sign in to comment.