@@ -17,7 +17,8 @@ internal class AtomWeaverV2
17
17
private const string ConstructorName = ".ctor" ;
18
18
private const string DirectEvaluateMethodName = nameof ( ComputedAtom < int > . DirectEvaluate ) ;
19
19
private const string CompAndInvalidateMethodName = nameof ( ComputedAtom < int > . CompareAndInvalidate ) ;
20
- private const string CreateAtomMethodName = nameof ( CodeGenAtom . Create ) ;
20
+ private const string CreateAtomMethodName = nameof ( CodeGenAtom . CreatePooled ) ;
21
+ private const string ThrowIfDisposedMethodName = nameof ( LifetimeScopeExtension . ThrowIfDisposed ) ;
21
22
private const string KeepAliveParameterName = nameof ( AtomAttribute . KeepAlive ) ;
22
23
23
24
private List < DiagnosticMessage > _diagnosticMessages = new List < DiagnosticMessage > ( ) ;
@@ -31,6 +32,7 @@ internal class AtomWeaverV2
31
32
private MethodReference _atomGetValueMethod ;
32
33
private MethodReference _atomDirectEvalMethod ;
33
34
private MethodReference _atomCompAndInvalidateMethod ;
35
+ private MethodReference _throwIfDisposedMethod ;
34
36
35
37
private MethodReference _atomPullCtorMethod ;
36
38
@@ -69,6 +71,7 @@ private void Prepare(AssemblyDefinition assembly)
69
71
var atomTypeDef = _atomType . Resolve ( ) ;
70
72
var atomPullDef = _module . ImportReference ( typeof ( Func < > ) ) . Resolve ( ) ;
71
73
var atomFactoryDef = _module . ImportReference ( typeof ( CodeGenAtom ) ) . Resolve ( ) ;
74
+ var lifetimeScopeExtensionsDef = _module . ImportReference ( typeof ( LifetimeScopeExtension ) ) . Resolve ( ) ;
72
75
73
76
_atomGetValueMethod = _module . ImportReference ( atomTypeDef . FindProperty ( ValuePropertyName ) . GetMethod ) ;
74
77
@@ -78,6 +81,8 @@ private void Prepare(AssemblyDefinition assembly)
78
81
_module . ImportReference ( atomTypeDef . FindMethod ( CompAndInvalidateMethodName , 1 ) ) ;
79
82
80
83
_atomPullCtorMethod = _module . ImportReference ( atomPullDef . FindMethod ( ConstructorName , 2 ) ) ;
84
+ _throwIfDisposedMethod =
85
+ _module . ImportReference ( lifetimeScopeExtensionsDef . FindMethod ( ThrowIfDisposedMethodName , 1 ) ) ;
81
86
}
82
87
83
88
public bool Weave ( PropertyDefinition property )
@@ -123,12 +128,12 @@ public bool Weave(PropertyDefinition property)
123
128
_diagnosticMessages . Add ( UserError . CannotUseAtomAttributeOnAbstractProperty ( property ) ) ;
124
129
return false ;
125
130
}
126
-
131
+
127
132
var atomOptions = new AtomOptions
128
133
{
129
134
KeepAlive = atomAttribute . GetArgumentValueOrDefault ( KeepAliveParameterName , false ) ,
130
135
} ;
131
-
136
+
132
137
property . CustomAttributes . Remove ( atomAttribute ) ;
133
138
134
139
FixAutoPropertyBackingField ( property ) ;
@@ -167,7 +172,7 @@ private FieldDefinition CreateAtomField(PropertyDefinition property)
167
172
var atomFieldType = Helpers . MakeGenericType ( _atomType , property . PropertyType ) ;
168
173
return new FieldDefinition ( name , FieldAttributes . Private , atomFieldType ) ;
169
174
}
170
-
175
+
171
176
private struct AtomOptions
172
177
{
173
178
public bool KeepAlive ;
@@ -190,6 +195,7 @@ private struct AtomGetterMethodWeaver
190
195
private MethodReference _atomPullCtorMethod ;
191
196
private MethodReference _tryEnterMethod ;
192
197
private MethodReference _atomGetMethod ;
198
+ private MethodReference _throwIfDisposedMethod ;
193
199
194
200
public AtomGetterMethodWeaver ( AtomWeaverV2 weaver , PropertyDefinition property , FieldReference atomField ,
195
201
AtomOptions options )
@@ -212,6 +218,7 @@ public AtomGetterMethodWeaver(AtomWeaverV2 weaver, PropertyDefinition property,
212
218
_atomPullCtorMethod = Helpers . MakeHostInstanceGeneric ( weaver . _atomPullCtorMethod , propertyType ) ;
213
219
_tryEnterMethod = Helpers . MakeHostInstanceGeneric ( weaver . _atomDirectEvalMethod , propertyType ) ;
214
220
_atomGetMethod = Helpers . MakeHostInstanceGeneric ( weaver . _atomGetValueMethod , propertyType ) ;
221
+ _throwIfDisposedMethod = weaver . _throwIfDisposedMethod ;
215
222
216
223
var body = property . GetMethod . Body ;
217
224
body . InitLocals = true ;
@@ -247,6 +254,10 @@ public void Weave()
247
254
248
255
private void Prepend ( ref int ind , IList < Instruction > il )
249
256
{
257
+ // LifetimeScopeExtension.ThrowIfDisposed(this);
258
+ il . Insert ( ind ++ , Instruction . Create ( OpCodes . Ldarg_0 ) ) ;
259
+ il . Insert ( ind ++ , Instruction . Create ( OpCodes . Call , _throwIfDisposedMethod ) ) ;
260
+
250
261
// if (atom != null) goto nullCheckEnd;
251
262
il . Insert ( ind ++ , Instruction . Create ( OpCodes . Nop ) ) ;
252
263
il . Insert ( ind ++ , Instruction . Create ( OpCodes . Ldarg_0 ) ) ;
@@ -308,6 +319,7 @@ private struct AtomSetterMethodWeaver
308
319
private Instruction _preReturnInstruction ;
309
320
310
321
private MethodReference _compAndInvalidateMethod ;
322
+ private MethodReference _throwIfDisposedMethod ;
311
323
312
324
public AtomSetterMethodWeaver ( AtomWeaverV2 weaver , PropertyDefinition property , FieldReference atomField )
313
325
{
@@ -320,6 +332,7 @@ public AtomSetterMethodWeaver(AtomWeaverV2 weaver, PropertyDefinition property,
320
332
var propertyType = property . PropertyType ;
321
333
_compAndInvalidateMethod =
322
334
Helpers . MakeHostInstanceGeneric ( weaver . _atomCompAndInvalidateMethod , propertyType ) ;
335
+ _throwIfDisposedMethod = weaver . _throwIfDisposedMethod ;
323
336
}
324
337
325
338
public void Weave ( )
@@ -336,6 +349,10 @@ public void Weave()
336
349
337
350
private void Prepend ( ref int ind , IList < Instruction > il )
338
351
{
352
+ // LifetimeScopeExtension.ThrowIfDisposed(this);
353
+ il . Insert ( ind ++ , Instruction . Create ( OpCodes . Ldarg_0 ) ) ;
354
+ il . Insert ( ind ++ , Instruction . Create ( OpCodes . Call , _throwIfDisposedMethod ) ) ;
355
+
339
356
// if (atom == null) goto nullCheckEnd;
340
357
il . Insert ( ind ++ , Instruction . Create ( OpCodes . Nop ) ) ;
341
358
il . Insert ( ind ++ , Instruction . Create ( OpCodes . Ldarg_0 ) ) ;
0 commit comments