88 * You must not remove this notice, or any other, from this software.
99 **/
1010
11+ using clojure . lang . CljCompiler . Ast ;
1112using clojure . lang . Runtime ;
1213using Microsoft . Scripting . Generation ;
1314using System ;
1415using System . Collections ;
16+ using System . Collections . Concurrent ;
1517using System . Collections . Generic ;
1618using System . Diagnostics . SymbolStore ;
1719using System . Linq . Expressions ;
@@ -25,8 +27,8 @@ public sealed class GenContext
2527 {
2628 #region Data
2729
28- readonly AssemblyGen _assyGen ;
29- public AssemblyGen AssemblyGen
30+ readonly MyAssemblyGen _assyGen ;
31+ public MyAssemblyGen AssemblyGen
3032 {
3133 get { return _assyGen ; }
3234 }
@@ -73,27 +75,16 @@ public ISymbolDocumentWriter DocWriter
7375
7476 #region C-tors & factory methods
7577
76- private readonly static Dictionary < Assembly , bool > InternalAssemblies = new Dictionary < Assembly , bool > ( ) ;
78+ private readonly static ConcurrentDictionary < Assembly , bool > _internalAssemblies = new ConcurrentDictionary < Assembly , bool > ( ) ;
79+ private static void AddInternalAssembly ( Assembly a ) => _internalAssemblies [ a ] = true ;
80+ public static bool IsInternalAssembly ( Assembly a ) => _internalAssemblies . ContainsKey ( a ) ;
7781
78- private static void AddInternalAssembly ( Assembly a )
79- {
80- lock ( ( ( ICollection ) InternalAssemblies ) . SyncRoot )
81- {
82- InternalAssemblies [ a ] = true ;
83- }
84- }
82+ enum AssemblyType { Internal , External }
8583
86- public static bool IsInternalAssembly ( Assembly a )
87- {
88- lock ( ( ( ICollection ) InternalAssemblies ) . SyncRoot )
89- {
90- return InternalAssemblies . ContainsKey ( a ) ;
91- }
92- }
9384
9485 public static GenContext CreateWithInternalAssembly ( string assyName , bool createDynInitHelper )
9586 {
96- GenContext ctx = CreateGenContext ( assyName , assyName , ".dll" , null , createDynInitHelper ) ;
87+ GenContext ctx = CreateGenContext ( AssemblyType . Internal , assyName , assyName , ".dll" , null , createDynInitHelper ) ;
9788 AddInternalAssembly ( ctx . AssemblyBuilder ) ;
9889 return ctx ;
9990 }
@@ -108,15 +99,15 @@ public static GenContext CreateWithInternalAssembly(string assyName, bool create
10899 public static GenContext CreateWithExternalAssembly ( string sourceName , string assyName , string extension , bool createDynInitHelper )
109100 {
110101 string path = Compiler . CompilePathVar . deref ( ) as string ;
111- return CreateGenContext ( sourceName , assyName , extension , path ?? System . IO . Directory . GetCurrentDirectory ( ) , createDynInitHelper ) ;
102+ return CreateGenContext ( AssemblyType . External , sourceName , assyName , extension , path ?? System . IO . Directory . GetCurrentDirectory ( ) , createDynInitHelper ) ;
112103 }
113104
114105 public static GenContext CreateWithExternalAssembly ( string assyName , string extension , bool createDynInitHelper )
115106 {
116107 return CreateWithExternalAssembly ( assyName , assyName , extension , createDynInitHelper ) ;
117108 }
118109
119- private static GenContext CreateGenContext ( string sourceName , string assyName , string extension , string directory , bool createDynInitHelper )
110+ private static GenContext CreateGenContext ( AssemblyType assemblyType , string sourceName , string assyName , string extension , string directory , bool createDynInitHelper )
120111 {
121112 if ( directory != null )
122113 {
@@ -125,10 +116,10 @@ private static GenContext CreateGenContext(string sourceName, string assyName, s
125116 }
126117
127118 AssemblyName aname = new AssemblyName ( assyName ) ;
128- return new GenContext ( directory , aname , extension , createDynInitHelper , sourceName ) ;
119+ return new GenContext ( assemblyType , directory , aname , extension , createDynInitHelper , sourceName ) ;
129120 }
130121
131- private GenContext ( string directory , AssemblyName aname , string extension , bool createDynInitHelper , string sourceName )
122+ private GenContext ( AssemblyType assemblyType , string directory , AssemblyName aname , string extension , bool createDynInitHelper , string sourceName )
132123 {
133124 // TODO: Make this settable from a *debug* flag
134125#if DEBUG
@@ -137,7 +128,22 @@ private GenContext(string directory, AssemblyName aname, string extension, bool
137128 _isDebuggable = false ;
138129#endif
139130
140- _assyGen = new AssemblyGen ( aname , directory , extension , _isDebuggable ) ;
131+ #if NETFRAMEWORK || NET9_0_OR_GREATER
132+ switch ( assemblyType )
133+ {
134+ case AssemblyType . Internal :
135+ _assyGen = new MyAssemblyGen ( aname , _isDebuggable ) ;
136+ break ;
137+ case AssemblyType . External :
138+ _assyGen = new MyAssemblyGen ( aname , directory , extension , _isDebuggable ) ;
139+ break ;
140+ default :
141+ throw new InvalidOperationException ( "Unknown AssemblyType" ) ;
142+ }
143+ #else
144+ _assyGen = new MyAssemblyGen ( aname , _isDebuggable ) ;
145+ #endif
146+
141147 if ( createDynInitHelper )
142148 _dynInitHelper = new DynInitHelper ( _assyGen , GenerateName ( ) ) ;
143149
@@ -150,6 +156,9 @@ private GenContext(string directory, AssemblyName aname, string extension, bool
150156#if NETFRAMEWORK
151157 if ( _isDebuggable )
152158 _docWriter = ModuleBuilder . DefineDocument ( sourceName , ClojureContext . Default . LanguageGuid , ClojureContext . Default . VendorGuid , Guid . Empty ) ;
159+ #elif NET9_0_OR_GREATER
160+ if ( _isDebuggable && assemblyType == AssemblyType . External )
161+ _docWriter = ModuleBuilder . DefineDocument ( sourceName , ClojureContext . Default . LanguageGuid ) ;
153162#endif
154163 }
155164
@@ -239,22 +248,22 @@ public Expression MaybeAddDebugInfo(Expression expr, IPersistentMap spanMap)
239248 return expr ;
240249 }
241250
242- public static void EmitDebugInfo ( ILGen ilg , IPersistentMap spanMap )
251+ public static void EmitDebugInfo ( CljILGen ilg , IPersistentMap spanMap )
243252 {
244253 if ( Compiler . CompilerContextVar . deref ( ) is GenContext context )
245254 context . MaybeEmitDebugInfo ( ilg , spanMap ) ;
246255 }
247256
248- public void MaybeEmitDebugInfo ( ILGen ilg , IPersistentMap spanMap )
257+ public void MaybeEmitDebugInfo ( CljILGen ilg , IPersistentMap spanMap )
249258 {
250- #if NETFRAMEWORK
259+ #if NETFRAMEWORK || NET9_0_OR_GREATER
251260 if ( _docWriter != null && spanMap != null )
252261 {
253262 if ( Compiler . GetLocations ( spanMap , out int startLine , out int startCol , out int finishLine , out int finishCol ) )
254263 {
255264 try
256265 {
257- ilg . MarkSequencePoint ( _docWriter , startLine , startCol , finishLine , finishCol ) ;
266+ ilg . ILGenerator . MarkSequencePoint ( _docWriter , startLine , startCol , finishLine , finishCol ) ;
258267 }
259268 catch ( NotSupportedException )
260269 {
0 commit comments