@@ -163,6 +163,42 @@ internal static async Task<IParseResult<TGlobals>> ParseFromAsync(Func<AppDomain
163
163
script . Code = code ;
164
164
script . m_ScriptingOptions = ScriptingOptions ;
165
165
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
+
166
202
var hostedScriptRunner = script.GetOrCreateScriptRunner(new IParameter[0], script.Code, script.m_ScriptingOptions);
167
203
var task = RemoteTask.ClientComplete<RoslynScripting.Internal.IParseResult>(hostedScriptRunner.ParseAsync(script.Code, script.m_ScriptingOptions, EntryMethodSelector, EntryMethodParameterFactory), CancellationToken.None);
168
204
var parseResult = await task;
@@ -171,7 +207,7 @@ internal static async Task<IParseResult<TGlobals>> ParseFromAsync(Func<AppDomain
171
207
script.Parameters = parseResult.Parameters.ToList();
172
208
173
209
var result = new ParseResult<TGlobals>(script, parseResult.EntryMethodName);
174
- return result ;
210
+ return result;*/
175
211
}
176
212
177
213
public IScriptRunResult Run ( IEnumerable < IParameterValue > Parameters )
@@ -230,10 +266,13 @@ private IScriptRunner GetOrCreateScriptRunner(IParameter[] parameters, string sc
230
266
// C) the AppDomain in which the script runner is hosted is the same that we would like to use now
231
267
if ( m_ScriptRunnerCacheInfo != null && ! m_ScriptRunnerCacheInfo . ScriptRunner . NeedsRecompilationFor ( parameters , scriptCode , Options ) && m_ScriptRunnerCacheInfo . HostingDomain == sandbox )
232
268
{
269
+ Trace . WriteLine ( "Using cached script runner" ) ;
233
270
return m_ScriptRunnerCacheInfo . ScriptRunner ;
234
271
}
235
272
else
236
273
{
274
+ Trace . WriteLine ( "Creating new script runner" ) ;
275
+
237
276
TryUnregisterLease ( ) ;
238
277
239
278
var scriptRunner = ( HostedScriptRunner < TGlobals > ) Activator . CreateInstance ( sandbox , typeof ( HostedScriptRunner < TGlobals > ) . Assembly . FullName , typeof ( HostedScriptRunner < TGlobals > ) . FullName ) . Unwrap ( ) ;
0 commit comments