@@ -163,6 +163,42 @@ internal static async Task<IParseResult<TGlobals>> ParseFromAsync(Func<AppDomain
163163 script . Code = code ;
164164 script . m_ScriptingOptions = ScriptingOptions ;
165165
166+ // TODO: This is not a great solution.. can we improve this?
167+ // Ideal would be to avoid double-parsing as to not garbage up the appdomain with lot of unneeded assemblies (or having to create individual ones like below)
168+ // but then again we can't use the EntryMethodSelector that nicely anymore
169+ // maybe only compile to semanticmodel and work with that?
170+ var helper = new HostingHelper ( ) ;
171+ AppDomain domain = null ;
172+ ParseResult < TGlobals > result ;
173+
174+ try
175+ {
176+ domain = helper . IndividualScriptDomain ;
177+ var scriptRunner = ( HostedScriptRunner < TGlobals > ) Activator . CreateInstance ( domain , typeof ( HostedScriptRunner < TGlobals > ) . Assembly . FullName , typeof ( HostedScriptRunner < TGlobals > ) . FullName ) . Unwrap ( ) ;
178+ var task = RemoteTask . ClientComplete < RoslynScripting . Internal . IParseResult > ( scriptRunner . ParseAsync ( script . Code , script . m_ScriptingOptions , EntryMethodSelector , EntryMethodParameterFactory ) , CancellationToken . None ) ;
179+
180+ var parseResult = await task ;
181+
182+ IList < IParameter > marshalledParameters = new List < IParameter > ( ) ;
183+
184+ foreach ( var parameter in parseResult . Parameters )
185+ marshalledParameters . Add ( new Parameter { DefaultValue = parameter . DefaultValue , Description = parameter . Description , IsOptional = parameter . IsOptional , Name = parameter . Name , Type = parameter . Type } ) ;
186+
187+ script . Code = parseResult . RefactoredCode ;
188+ script . Parameters = marshalledParameters ;
189+ result = new ParseResult < TGlobals > ( script , parseResult . EntryMethodName ) ;
190+ } finally
191+ {
192+ if ( domain != null )
193+ AppDomain . Unload ( domain ) ;
194+ }
195+
196+
197+ return result ;
198+
199+
200+ /*
201+
166202 var hostedScriptRunner = script.GetOrCreateScriptRunner(new IParameter[0], script.Code, script.m_ScriptingOptions);
167203 var task = RemoteTask.ClientComplete<RoslynScripting.Internal.IParseResult>(hostedScriptRunner.ParseAsync(script.Code, script.m_ScriptingOptions, EntryMethodSelector, EntryMethodParameterFactory), CancellationToken.None);
168204 var parseResult = await task;
@@ -171,7 +207,7 @@ internal static async Task<IParseResult<TGlobals>> ParseFromAsync(Func<AppDomain
171207 script.Parameters = parseResult.Parameters.ToList();
172208
173209 var result = new ParseResult<TGlobals>(script, parseResult.EntryMethodName);
174- return result ;
210+ return result;*/
175211 }
176212
177213 public IScriptRunResult Run ( IEnumerable < IParameterValue > Parameters )
@@ -230,10 +266,13 @@ private IScriptRunner GetOrCreateScriptRunner(IParameter[] parameters, string sc
230266 // C) the AppDomain in which the script runner is hosted is the same that we would like to use now
231267 if ( m_ScriptRunnerCacheInfo != null && ! m_ScriptRunnerCacheInfo . ScriptRunner . NeedsRecompilationFor ( parameters , scriptCode , Options ) && m_ScriptRunnerCacheInfo . HostingDomain == sandbox )
232268 {
269+ Trace . WriteLine ( "Using cached script runner" ) ;
233270 return m_ScriptRunnerCacheInfo . ScriptRunner ;
234271 }
235272 else
236273 {
274+ Trace . WriteLine ( "Creating new script runner" ) ;
275+
237276 TryUnregisterLease ( ) ;
238277
239278 var scriptRunner = ( HostedScriptRunner < TGlobals > ) Activator . CreateInstance ( sandbox , typeof ( HostedScriptRunner < TGlobals > ) . Assembly . FullName , typeof ( HostedScriptRunner < TGlobals > ) . FullName ) . Unwrap ( ) ;
0 commit comments