Skip to content
This repository was archived by the owner on Nov 8, 2020. It is now read-only.

Commit 7366c54

Browse files
committed
Fix OpCodes.
1 parent cd1fc1e commit 7366c54

2 files changed

Lines changed: 30 additions & 25 deletions

File tree

src/Ray.Core/Core/Grains/ObserverGrain.cs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,16 @@ public ObserverGrain()
6767
if (!sortList.Contains(item))
6868
sortList.Add(item);
6969
}
70+
var declare_1 = ilGen.DeclareLocal(typeof(Task));
7071
var defaultLabel = ilGen.DefineLabel();
7172
var lastLable = ilGen.DefineLabel();
72-
var declare_1 = ilGen.DeclareLocal(typeof(Task));
73-
var isShort = sortList.Count < 12;
7473
foreach (var item in sortList)
7574
{
7675
ilGen.Emit(OpCodes.Ldarg_1);
7776
ilGen.Emit(OpCodes.Isinst, item.CaseType);
7877
if (item.Index > 3)
7978
{
80-
if (isShort)
79+
if (item.DeclareLocal.LocalIndex > 0 && item.DeclareLocal.LocalIndex <= 255)
8180
{
8281
ilGen.Emit(OpCodes.Stloc_S, item.DeclareLocal);
8382
ilGen.Emit(OpCodes.Ldloc_S, item.DeclareLocal);
@@ -111,10 +110,12 @@ public ObserverGrain()
111110
ilGen.Emit(OpCodes.Ldloc_3);
112111
}
113112
}
114-
115-
ilGen.Emit(OpCodes.Brtrue, item.Lable);
113+
if (item.DeclareLocal.LocalIndex > 0 && item.DeclareLocal.LocalIndex < 255)
114+
ilGen.Emit(OpCodes.Brtrue_S, item.Lable);
115+
else
116+
ilGen.Emit(OpCodes.Brtrue, item.Lable);
116117
}
117-
if (isShort)
118+
if (declare_1.LocalIndex > 0 && declare_1.LocalIndex <= 255)
118119
ilGen.Emit(OpCodes.Br_S, defaultLabel);
119120
else
120121
ilGen.Emit(OpCodes.Br, defaultLabel);
@@ -124,34 +125,36 @@ public ObserverGrain()
124125
ilGen.Emit(OpCodes.Ldarg_0);
125126
//加载第一个参数
126127
if (item.Parameters[0].ParameterType == item.CaseType)
127-
LdEventArgs(item, ilGen, isShort);
128+
LdEventArgs(item, ilGen);
128129
else if (item.Parameters[0].ParameterType == typeof(EventBase))
129130
ilGen.Emit(OpCodes.Ldarg_2);
130131
//加载第二个参数
131132
if (item.Parameters.Length == 2)
132133
{
133134
if (item.Parameters[1].ParameterType == item.CaseType)
134-
LdEventArgs(item, ilGen, isShort);
135+
LdEventArgs(item, ilGen);
135136
else if (item.Parameters[1].ParameterType == typeof(EventBase))
136137
ilGen.Emit(OpCodes.Ldarg_2);
137138
}
138139
ilGen.Emit(OpCodes.Call, item.Mehod);
139-
if (isShort)
140+
if (item.DeclareLocal.LocalIndex > 0 && item.DeclareLocal.LocalIndex <= 255)
140141
{
141142
ilGen.Emit(OpCodes.Stloc_S, declare_1);
142-
ilGen.Emit(OpCodes.Br_S, lastLable);
143143
}
144144
else
145145
{
146146
ilGen.Emit(OpCodes.Stloc, declare_1);
147-
ilGen.Emit(OpCodes.Br, lastLable);
148147
}
148+
if (declare_1.LocalIndex > 0 && declare_1.LocalIndex <= 255)
149+
ilGen.Emit(OpCodes.Br_S, lastLable);
150+
else
151+
ilGen.Emit(OpCodes.Br, lastLable);
149152
}
150153
ilGen.MarkLabel(defaultLabel);
151154
ilGen.Emit(OpCodes.Ldarg_0);
152155
ilGen.Emit(OpCodes.Ldarg_1);
153156
ilGen.Emit(OpCodes.Call, GrainType.GetMethod(nameof(DefaultHandler)));
154-
if (isShort)
157+
if (declare_1.LocalIndex > 0 && declare_1.LocalIndex <= 255)
155158
{
156159
ilGen.Emit(OpCodes.Stloc_S, declare_1);
157160
ilGen.Emit(OpCodes.Br_S, lastLable);
@@ -163,18 +166,18 @@ public ObserverGrain()
163166
}
164167
//last
165168
ilGen.MarkLabel(lastLable);
166-
if (isShort)
169+
if (declare_1.LocalIndex > 0 && declare_1.LocalIndex <= 255)
167170
ilGen.Emit(OpCodes.Ldloc_S, declare_1);
168171
else
169172
ilGen.Emit(OpCodes.Ldloc, declare_1);
170173
ilGen.Emit(OpCodes.Ret);
171174
handlerInvokeFunc = (Func<object, IEvent, EventBase, Task>)dynamicMethod.CreateDelegate(typeof(Func<object, IEvent, EventBase, Task>));
172175
//加载Event参数
173-
static void LdEventArgs(SwitchMethodEmit item, ILGenerator gen, bool isShort)
176+
static void LdEventArgs(SwitchMethodEmit item, ILGenerator gen)
174177
{
175178
if (item.Index > 3)
176179
{
177-
if (isShort)
180+
if (item.DeclareLocal.LocalIndex > 0 && item.DeclareLocal.LocalIndex <= 255)
178181
gen.Emit(OpCodes.Ldloc_S, item.DeclareLocal);
179182
else
180183
gen.Emit(OpCodes.Ldloc, item.DeclareLocal);

src/Ray.Core/Snapshot/SnapshotHandler.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public SnapshotHandler()
4545
});
4646
}
4747
var sortList = new List<SwitchMethodEmit>();
48-
foreach (var item in switchMethods.Where(m => m.CaseType.BaseType ==typeof(object)))
48+
foreach (var item in switchMethods.Where(m => m.CaseType.BaseType == typeof(object)))
4949
{
5050
sortList.Add(item);
5151
GetInheritor(item, switchMethods, sortList);
@@ -57,14 +57,14 @@ public SnapshotHandler()
5757
sortList.Add(item);
5858
}
5959
var defaultLabel = ilGen.DefineLabel();
60-
var isShort = sortList.Count < 12;
60+
var maxLocalIndex = 0;
6161
foreach (var item in sortList)
6262
{
6363
ilGen.Emit(OpCodes.Ldarg_2);
6464
ilGen.Emit(OpCodes.Isinst, item.CaseType);
6565
if (item.Index > 3)
6666
{
67-
if (isShort)
67+
if (item.DeclareLocal.LocalIndex > 0 && item.DeclareLocal.LocalIndex <= 255)
6868
{
6969
ilGen.Emit(OpCodes.Stloc_S, item.DeclareLocal);
7070
ilGen.Emit(OpCodes.Ldloc_S, item.DeclareLocal);
@@ -98,12 +98,14 @@ public SnapshotHandler()
9898
ilGen.Emit(OpCodes.Ldloc_3);
9999
}
100100
}
101-
if (isShort)
101+
if (item.DeclareLocal.LocalIndex > 0 && item.DeclareLocal.LocalIndex < 255)
102102
ilGen.Emit(OpCodes.Brtrue_S, item.Lable);
103103
else
104104
ilGen.Emit(OpCodes.Brtrue, item.Lable);
105+
if (item.DeclareLocal.LocalIndex > maxLocalIndex)
106+
maxLocalIndex = item.DeclareLocal.LocalIndex;
105107
}
106-
if (isShort)
108+
if (maxLocalIndex > 0 && maxLocalIndex < 255)
107109
ilGen.Emit(OpCodes.Br_S, defaultLabel);
108110
else
109111
ilGen.Emit(OpCodes.Br, defaultLabel);
@@ -117,14 +119,14 @@ public SnapshotHandler()
117119
else if (item.Parameters[0].ParameterType == typeof(EventBase))
118120
ilGen.Emit(OpCodes.Ldarg_3);
119121
else
120-
LdEventArgs(item, ilGen, isShort);
122+
LdEventArgs(item, ilGen);
121123
//加载第二个参数
122124
if (item.Parameters[1].ParameterType == typeof(Snapshot))
123125
ilGen.Emit(OpCodes.Ldarg_1);
124126
else if (item.Parameters[1].ParameterType == typeof(EventBase))
125127
ilGen.Emit(OpCodes.Ldarg_3);
126128
else
127-
LdEventArgs(item, ilGen, isShort);
129+
LdEventArgs(item, ilGen);
128130
//加载第三个参数
129131
if (item.Parameters.Length == 3)
130132
{
@@ -133,7 +135,7 @@ public SnapshotHandler()
133135
else if (item.Parameters[1].ParameterType == typeof(EventBase))
134136
ilGen.Emit(OpCodes.Ldarg_3);
135137
else
136-
LdEventArgs(item, ilGen, isShort);
138+
LdEventArgs(item, ilGen);
137139
}
138140
ilGen.Emit(OpCodes.Call, item.Mehod);
139141
ilGen.Emit(OpCodes.Ret);
@@ -145,11 +147,11 @@ public SnapshotHandler()
145147
ilGen.Emit(OpCodes.Ret);
146148
handlerInvokeFunc = (Action<object, Snapshot, IEvent, EventBase>)dynamicMethod.CreateDelegate(typeof(Action<object, Snapshot, IEvent, EventBase>));
147149
//加载Event参数
148-
static void LdEventArgs(SwitchMethodEmit item, ILGenerator gen, bool isShort)
150+
static void LdEventArgs(SwitchMethodEmit item, ILGenerator gen)
149151
{
150152
if (item.Index > 3)
151153
{
152-
if (isShort)
154+
if (item.DeclareLocal.LocalIndex > 0 && item.DeclareLocal.LocalIndex <= 255)
153155
gen.Emit(OpCodes.Ldloc_S, item.DeclareLocal);
154156
else
155157
gen.Emit(OpCodes.Ldloc, item.DeclareLocal);

0 commit comments

Comments
 (0)