@@ -1272,32 +1272,12 @@ internal virtual void OnAccessSettingsChanged()
12721272
12731273 internal virtual void HostInvoke ( Action action )
12741274 {
1275- var previousEngine = currentEngine ;
1276- currentEngine = this ;
1277-
1278- try
1279- {
1280- action ( ) ;
1281- }
1282- finally
1283- {
1284- currentEngine = previousEngine ;
1285- }
1275+ action ( ) ;
12861276 }
12871277
12881278 internal virtual T HostInvoke < T > ( Func < T > func )
12891279 {
1290- var previousEngine = currentEngine ;
1291- currentEngine = this ;
1292-
1293- try
1294- {
1295- return func ( ) ;
1296- }
1297- finally
1298- {
1299- currentEngine = previousEngine ;
1300- }
1280+ return func ( ) ;
13011281 }
13021282
13031283 #endregion
@@ -1306,7 +1286,28 @@ internal virtual T HostInvoke<T>(Func<T> func)
13061286
13071287 internal ScriptFrame CurrentScriptFrame { get ; private set ; }
13081288
1289+ internal IDisposable CreateEngineScope ( )
1290+ {
1291+ return Scope . Create ( ( ) => MiscHelpers . Exchange ( ref currentEngine , this ) , previousEngine => currentEngine = previousEngine ) ;
1292+ }
1293+
13091294 internal virtual void ScriptInvoke ( Action action )
1295+ {
1296+ using ( CreateEngineScope ( ) )
1297+ {
1298+ ScriptInvokeInternal ( action ) ;
1299+ }
1300+ }
1301+
1302+ internal virtual T ScriptInvoke < T > ( Func < T > func )
1303+ {
1304+ using ( CreateEngineScope ( ) )
1305+ {
1306+ return ScriptInvokeInternal ( func ) ;
1307+ }
1308+ }
1309+
1310+ internal void ScriptInvokeInternal ( Action action )
13101311 {
13111312 var previousScriptFrame = CurrentScriptFrame ;
13121313 CurrentScriptFrame = new ScriptFrame ( ) ;
@@ -1321,7 +1322,7 @@ internal virtual void ScriptInvoke(Action action)
13211322 }
13221323 }
13231324
1324- internal virtual T ScriptInvoke < T > ( Func < T > func )
1325+ internal T ScriptInvokeInternal < T > ( Func < T > func )
13251326 {
13261327 var previousScriptFrame = CurrentScriptFrame ;
13271328 CurrentScriptFrame = new ScriptFrame ( ) ;
@@ -1350,10 +1351,10 @@ internal static void ThrowScriptError(IScriptEngineException scriptError)
13501351 {
13511352 if ( scriptError is ScriptInterruptedException )
13521353 {
1353- throw new ScriptInterruptedException ( scriptError . EngineName , scriptError . Message , scriptError . ErrorDetails , scriptError . HResult , scriptError . IsFatal , scriptError . ExecutionStarted , scriptError . InnerException ) ;
1354+ throw new ScriptInterruptedException ( scriptError . EngineName , scriptError . Message , scriptError . ErrorDetails , scriptError . HResult , scriptError . IsFatal , scriptError . ExecutionStarted , scriptError . ScriptException , scriptError . InnerException ) ;
13541355 }
13551356
1356- throw new ScriptEngineException ( scriptError . EngineName , scriptError . Message , scriptError . ErrorDetails , scriptError . HResult , scriptError . IsFatal , scriptError . ExecutionStarted , scriptError . InnerException ) ;
1357+ throw new ScriptEngineException ( scriptError . EngineName , scriptError . Message , scriptError . ErrorDetails , scriptError . HResult , scriptError . IsFatal , scriptError . ExecutionStarted , scriptError . ScriptException , scriptError . InnerException ) ;
13571358 }
13581359 }
13591360
0 commit comments