@@ -72,7 +72,7 @@ type ITrampolineInvocation =
7272type CancellableStateMachineData < 'T > =
7373
7474 [<DefaultValue( false ) >]
75- val mutable Result : Result < 'T , exn >
75+ val mutable Result : 'T
7676
7777and CancellableStateMachine < 'TOverall > = ResumableStateMachine< CancellableStateMachineData< 'TOverall>>
7878and ICancellableStateMachine < 'TOverall > = IResumableStateMachine< CancellableStateMachineData< 'TOverall>>
@@ -83,27 +83,8 @@ and CancellableCode<'TOverall, 'T> = ResumableCode<CancellableStateMachineData<'
8383[<Sealed>]
8484type Trampoline ( cancellationToken : CancellationToken ) =
8585 let mutable bindDepth = 0
86- let mutable storedException : ExceptionDispatchInfo voption = ValueNone
87- let mutable capturedFramesCount = 0
8886
89- let captureStackFrame exn =
90- match storedException with
91- | ValueSome edi when edi.SourceException = exn ->
92- try
93- edi.Throw()
94- Unchecked.defaultof<_>
95- with exn ->
96- capturedFramesCount <- capturedFramesCount + 1
97- let edi = ExceptionDispatchInfo.Capture exn
98- storedException <- ValueSome edi
99- edi.SourceException
100- | _ ->
101- capturedFramesCount <- 1
102- let edi = ExceptionDispatchInfo.Capture exn
103- storedException <- ValueSome edi
104- edi.SourceException
105-
106- let stack = System.Collections.Generic.Stack< ITrampolineInvocation>()
87+ let stack = Collections.Generic.Stack< ITrampolineInvocation>()
10788
10889 static let current = new ThreadLocal< Trampoline>()
10990
@@ -114,15 +95,9 @@ type Trampoline(cancellationToken: CancellationToken) =
11495
11596 member this.ShoudBounce = bindDepth % 100 = 0
11697
117- member this.CaptureStackFrame ( exn ) =
118- if not this.IsCancelled && ( bindDepth < 100 || capturedFramesCount < 200 ) then
119- captureStackFrame exn
120- else
121- exn
122-
12398 static member Install ct = current.Value <- Trampoline ct
12499
125- member _.Set ( invocation : ITrampolineInvocation ) = stack.Push( invocation)
100+ member _.Set ( invocation ) = stack.Push( invocation)
126101
127102 [<DebuggerHidden>]
128103 member this.Execute ( invocation ) =
@@ -142,10 +117,10 @@ type Trampoline(cancellationToken: CancellationToken) =
142117
143118type ITrampolineInvocation < 'T > =
144119 inherit ITrampolineInvocation
145- abstract Result: Result < 'T , exn >
120+ abstract Result: 'T
146121
147- type IMachineTemplateWrapper < 'T > =
148- abstract Clone : unit -> ITrampolineInvocation < 'T >
122+ type ICancellableInvokable < 'T > =
123+ abstract Create : unit -> ITrampolineInvocation < 'T >
149124
150125[<NoEquality; NoComparison>]
151126type CancellableInvocation < 'T , 'Machine when 'Machine :> IAsyncStateMachine and 'Machine :> ICancellableStateMachine < 'T >>( machine : 'Machine )
@@ -158,13 +133,13 @@ type CancellableInvocation<'T, 'Machine when 'Machine :> IAsyncStateMachine and
158133 member _.IsCompleted = machine.ResumptionPoint = - 1
159134 member _.Result = machine.Data.Result
160135
161- interface IMachineTemplateWrapper < 'T> with
162- member _.Clone () = CancellableInvocation<_, _>( machine)
136+ interface ICancellableInvokable < 'T> with
137+ member _.Create () = CancellableInvocation<_, _>( machine)
163138
164139[<Struct; NoComparison>]
165- type Cancellable < 'T >( template : IMachineTemplateWrapper <'T >) =
140+ type Cancellable < 'T >( invokable : ICancellableInvokable <'T >) =
166141
167- member _.GetInvocation () = template.Clone ()
142+ member _.GetInvocation () = invokable.Create ()
168143
169144[<AutoOpen>]
170145module CancellableCode =
@@ -181,48 +156,34 @@ module CancellableCode =
181156 Trampoline.Current.ThrowIfCancellationRequested()
182157 code.Invoke(& sm))
183158
184- let inline getResult ( invocation : ITrampolineInvocation < _ >) =
185- match invocation.Result with
186- | Ok value -> value
187- | Error exn -> raise exn
188-
189159type CancellableBuilder () =
190160
191161 member inline _.Delay ( generator : unit -> CancellableCode < 'TOverall , 'T >) : CancellableCode < 'TOverall , 'T > =
192162 ResumableCode.Delay( fun () -> generator () |> throwIfCancellationRequested)
193163
194- /// Used to represent no-ops like the implicit empty "else" branch of an "if" expression.
195164 [<DefaultValue>]
196165 member inline _.Zero () : CancellableCode < 'TOverall , unit > = ResumableCode.Zero()
197166
198167 member inline _.Return ( value : 'T ) : CancellableCode < 'T , 'T > =
199168 CancellableCode< 'T, _>( fun sm ->
200- sm.Data.Result <- Ok value
169+ sm.Data.Result <- value
201170 true )
202171
203- /// Chains together a step with its following step.
204- /// Note that this requires that the first step has no result.
205- /// This prevents constructs like `task { return 1; return 2; }`.
206172 member inline _.Combine
207173 ( code1 : CancellableCode < 'TOverall , unit >, code2 : CancellableCode < 'TOverall , 'T >)
208174 : CancellableCode < 'TOverall , 'T > =
209175 ResumableCode.Combine( code1, code2)
210176
211- /// Builds a step that executes the body while the condition predicate is true.
212177 member inline _.While
213178 ( [<InlineIfLambda>] condition : unit -> bool , body : CancellableCode < 'TOverall , unit >)
214179 : CancellableCode < 'TOverall , unit > =
215180 ResumableCode.While( condition, throwIfCancellationRequested body)
216181
217- /// Wraps a step in a try/with. This catches exceptions both in the evaluation of the function
218- /// to retrieve the step, and in the continuation of the step (if any).
219182 member inline _.TryWith
220183 ( body : CancellableCode < 'TOverall , 'T >, catch : exn -> CancellableCode < 'TOverall , 'T >)
221184 : CancellableCode < 'TOverall , 'T > =
222185 ResumableCode.TryWith( body, filterCancellation catch)
223186
224- /// Wraps a step in a try/finally. This catches exceptions both in the evaluation of the function
225- /// to retrieve the step, and in the continuation of the step (if any).
226187 member inline _.TryFinally
227188 ( body : CancellableCode < 'TOverall , 'T >, [<InlineIfLambda>] compensation : unit -> unit )
228189 : CancellableCode < 'TOverall , 'T > =
@@ -256,10 +217,10 @@ type CancellableBuilder() =
256217 sm.ResumptionPoint <- contID
257218 Trampoline.Current.Set invocation
258219 false
259- | None -> ( invocation |> getResult |> continuation) .Invoke(& sm)
220+ | None -> ( continuation invocation.Result ) .Invoke(& sm)
260221 else
261222 Trampoline.Current.Execute invocation
262- ( invocation |> getResult |> continuation) .Invoke(& sm)
223+ ( continuation invocation.Result ) .Invoke(& sm)
263224
264225 else
265226 // Dynamic Bind.
@@ -268,14 +229,15 @@ type CancellableBuilder() =
268229
269230 if Trampoline.Current.ShoudBounce then
270231 let cont =
271- CancellableResumptionFunc< 'Data>( fun sm -> ( invocation |> getResult |> continuation) .Invoke(& sm))
232+ CancellableResumptionFunc< 'Data>( fun sm -> ( continuation invocation.Result ) .Invoke(& sm))
272233
273234 Trampoline.Current.Set invocation
274235 sm.ResumptionDynamicInfo.ResumptionFunc <- cont
275236 false
276237 else
277238 Trampoline.Current.Execute invocation
278- ( invocation |> getResult |> continuation) .Invoke(& sm))
239+ ( continuation invocation.Result) .Invoke(& sm))
240+ |> throwIfCancellationRequested
279241
280242 member inline this.ReturnFrom ( comp : Cancellable < 'T >) : CancellableCode < 'T , 'T > = this.Bind( comp, this.Return)
281243
@@ -285,14 +247,9 @@ type CancellableBuilder() =
285247
286248 ( MoveNextMethodImpl<_>( fun sm ->
287249 __ resumeAt sm.ResumptionPoint
250+ let __stack_code_fin = code.Invoke(& sm)
288251
289- try
290- let __stack_code_fin = code.Invoke(& sm)
291-
292- if __ stack_ code_ fin then
293- sm.ResumptionPoint <- - 1
294- with exn ->
295- sm.Data.Result <- Error <| Trampoline.Current.CaptureStackFrame exn
252+ if __ stack_ code_ fin then
296253 sm.ResumptionPoint <- - 1 ))
297254
298255 ( SetStateMachineMethodImpl<_>( fun _ _ -> ()))
@@ -306,11 +263,7 @@ type CancellableBuilder() =
306263 let resumptionInfo =
307264 { new CancellableResumptionDynamicInfo<_>( initialResumptionFunc) with
308265 member info.MoveNext ( sm ) =
309- try
310- if info.ResumptionFunc.Invoke(& sm) then
311- sm.ResumptionPoint <- - 1
312- with exn ->
313- sm.Data.Result <- Error <| Trampoline.Current.CaptureStackFrame exn
266+ if info.ResumptionFunc.Invoke(& sm) then
314267 sm.ResumptionPoint <- - 1
315268
316269 member _.SetStateMachine ( _ , _ ) = ()
@@ -344,23 +297,13 @@ module Cancellable =
344297 invocation
345298
346299 let runWithoutCancellation code =
347- let invocation = run CancellationToken.None code
348-
349- if Trampoline.Current.IsCancelled then
350- failwith " Unexpected cancellation in Cancellable.runWithoutCancellation"
351- else
352- getResult invocation
300+ code |> run CancellationToken.None |> _. Result
353301
354302 let toAsync code =
355303 async {
356304 let! ct = Async.CancellationToken
357305
358- return !
359- Async.FromContinuations( fun ( cont , econt , ccont ) ->
360- match run ct code |> _. Result with
361- | _ when Trampoline.Current.IsCancelled -> ccont ( OperationCanceledException ct)
362- | Ok value -> cont value
363- | Error exn -> econt exn)
306+ return run ct code |> _. Result
364307 }
365308
366309 let token () =
0 commit comments