forked from pharo-project/pharo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlockClosure.extension.st
35 lines (25 loc) · 1.13 KB
/
BlockClosure.extension.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Extension { #name : #BlockClosure }
{ #category : #'*UnifiedFFI' }
BlockClosure >> on: exception fork: handlerAction return: answerAction [
"This is the same as #on:fork: but instead just fork and letting the flow continues, in
case of an error it also evaluates answerAction and returns its result."
^ self on: exception do: [:ex |
| onDoCtx handler bottom thisCtx |
onDoCtx := thisContext.
thisCtx := onDoCtx home.
"find the context on stack for which this method's is sender"
[ onDoCtx sender == thisCtx] whileFalse: [
onDoCtx := onDoCtx sender.
onDoCtx ifNil: [
"Can't find our home context. seems like we're already forked
and handling another exception in new thread. In this case, just pass it through handler."
^ handlerAction cull: ex ] ].
bottom := [ Processor terminateActive ] asContext.
onDoCtx privSender: bottom.
handler := [ handlerAction cull: ex ] asContext.
handler privSender: thisContext sender.
(Process forContext: handler priority: Processor activePriority) resume.
"cut the stack of current process"
thisContext privSender: thisCtx.
answerAction cull: exception ]
]