1
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
159
|
3 |
All Rights Reserved
|
1
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
Object subclass:#Signal
|
159
|
14 |
instanceVariableNames:'mayProceed notifierString nameClass message
|
|
15 |
handlerBlock parent'
|
171
|
16 |
classVariableNames:'NoHandlerSignal'
|
159
|
17 |
poolDictionaries:''
|
|
18 |
category:'Kernel-Exceptions'
|
1
|
19 |
!
|
|
20 |
|
|
21 |
Signal comment:'
|
|
22 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
159
|
23 |
All Rights Reserved
|
95
|
24 |
|
329
|
25 |
$Header: /cvs/stx/stx/libbasic/Signal.st,v 1.21 1995-05-01 21:38:57 claus Exp $
|
44
|
26 |
'!
|
|
27 |
|
|
28 |
!Signal class methodsFor:'documentation'!
|
|
29 |
|
88
|
30 |
copyright
|
|
31 |
"
|
|
32 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
159
|
33 |
All Rights Reserved
|
88
|
34 |
|
|
35 |
This software is furnished under a license and may be used
|
|
36 |
only in accordance with the terms of that license and with the
|
|
37 |
inclusion of the above copyright notice. This software may not
|
|
38 |
be provided or otherwise made available to, or used by, any
|
|
39 |
other person. No title to or ownership of the software is
|
|
40 |
hereby transferred.
|
|
41 |
"
|
|
42 |
!
|
|
43 |
|
|
44 |
version
|
|
45 |
"
|
329
|
46 |
$Header: /cvs/stx/stx/libbasic/Signal.st,v 1.21 1995-05-01 21:38:57 claus Exp $
|
88
|
47 |
"
|
|
48 |
!
|
|
49 |
|
44
|
50 |
documentation
|
|
51 |
"
|
70
|
52 |
Signal and Exception provide a framework for exception handling.
|
44
|
53 |
|
70
|
54 |
A Signal object is usually defined somewhere up in the calling chain
|
|
55 |
and associated with some abnormal event. Many signals are also
|
|
56 |
created at startup time and reused.
|
44
|
57 |
|
70
|
58 |
When the event is raised (by Signal>>raise) the control will be either
|
|
59 |
given to a debugger or - if a handler was defined - to the handler.
|
|
60 |
The handler will get a description of what (and where) happened in an
|
120
|
61 |
Exception object and can decide how to react on the situation (i.e.
|
|
62 |
proceed, return or restart).
|
70
|
63 |
|
|
64 |
There is also a companion class called SignalSet, which allows handling
|
|
65 |
multiple signals with one handler (for example all arithmetic signals).
|
|
66 |
And, finally there is a very special SignalSet which allows catching
|
|
67 |
any signal (SignalSet>>anySignal).
|
|
68 |
|
171
|
69 |
Since there is no official documentation on signal handling (i.e. none
|
|
70 |
of the books describes it), this Signal implementation has been modeled
|
|
71 |
after what some PD programs seem to expect and what alpha/beta testers told
|
|
72 |
me it should look like.
|
|
73 |
It may not be perfect and undergo minor changes.
|
44
|
74 |
|
120
|
75 |
special:
|
|
76 |
|
70
|
77 |
In addition to the nested catch & throw mechanism, signals can also be
|
120
|
78 |
used when no such handler scope exists. To support this, signals can be
|
|
79 |
assigned a handlerBlock, which gets evaluated with the exception as argument
|
|
80 |
in case no handler was found (on the stack).
|
70
|
81 |
|
171
|
82 |
If no handler was found (i.e. neither a handler context on the stack, nor
|
|
83 |
a static handler block), the NoHandlerSignal will be raised instead,
|
120
|
84 |
passing it the original exception in its exception-parameter.
|
171
|
85 |
This NoHandlerSignal can be handled just like any other signal.
|
|
86 |
(therefore, it is possible to catch any error by catching NoHandlerSignal.
|
70
|
87 |
|
120
|
88 |
When the NoHandler signal is raised, and neither a handler-context, nor
|
|
89 |
a handler block is defined for it, an emergencyHandler(-block) is evaluated.
|
|
90 |
This block is either provided by the current process
|
|
91 |
(see Process>>emergencySignalHandler) or as a global default by the Exception
|
|
92 |
class (see Exception>>emergencyHandler).
|
|
93 |
The default emergencyHandlerBlock (from Exception) will bring up a debugger.
|
|
94 |
|
|
95 |
HandlerBlocks allow a global (if its the EmergencyHandler in Exception)
|
|
96 |
or per-process signal handling to be added. Even to code which was never
|
|
97 |
planned to handle signals.
|
70
|
98 |
|
171
|
99 |
See samples in 'doc/coding' and actual raise code in Exception.
|
1
|
100 |
|
70
|
101 |
Instance variables:
|
|
102 |
|
159
|
103 |
mayProceed <Boolean> hint for the debugger - program may
|
|
104 |
proceed (currently not honored by the
|
|
105 |
debugger)
|
70
|
106 |
|
171
|
107 |
notifierString <String> error message to be output
|
1
|
108 |
|
171
|
109 |
nameClass <Class> for the printOn-implementation; nameClass
|
|
110 |
is the class, to which message (below)
|
|
111 |
should be sent to create the receiver.
|
1
|
112 |
|
171
|
113 |
message <Symbol> for the printOn-implementation; message
|
|
114 |
is the selector, which should be sent to
|
|
115 |
nameClass (above) to create the receiver.
|
70
|
116 |
|
159
|
117 |
handlerBlock <Block> if nonNil, a 1-arg block to be
|
|
118 |
evaluated when no handler context is
|
|
119 |
found. The block gets the exception
|
171
|
120 |
object as argument. This will play the role
|
|
121 |
of an on-stack handler.
|
|
122 |
|
|
123 |
Class variables:
|
120
|
124 |
|
171
|
125 |
NoHandlerSignal <Signal> signal raised when no handler for a signal
|
|
126 |
was found in raise.
|
|
127 |
If this one is not handled either,
|
|
128 |
Exceptions emergencyHandler will be evaluated
|
|
129 |
instead (or a per-proces handler, if there
|
|
130 |
is one).
|
44
|
131 |
"
|
|
132 |
! !
|
1
|
133 |
|
|
134 |
!Signal class methodsFor:'initialization'!
|
|
135 |
|
159
|
136 |
initialize
|
44
|
137 |
"setup the signal used to handle unhandled signals"
|
|
138 |
|
61
|
139 |
NoHandlerSignal isNil ifTrue:[
|
302
|
140 |
NoHandlerSignal := ErrorSignal newSignalMayProceed:true.
|
159
|
141 |
NoHandlerSignal nameClass:self message:#noHandlerSignal.
|
|
142 |
NoHandlerSignal notifierString:'unhandled exception'.
|
61
|
143 |
]
|
1
|
144 |
! !
|
|
145 |
|
|
146 |
!Signal class methodsFor:'instance creation'!
|
|
147 |
|
|
148 |
new
|
|
149 |
"return a new signal"
|
|
150 |
|
|
151 |
^ self basicNew notifierString:'signal'
|
|
152 |
! !
|
|
153 |
|
|
154 |
!Signal class methodsFor:'signal access'!
|
|
155 |
|
|
156 |
noHandlerSignal
|
44
|
157 |
"return the signal used to handle unhandled signals"
|
|
158 |
|
1
|
159 |
^ NoHandlerSignal
|
|
160 |
! !
|
|
161 |
|
|
162 |
!Signal methodsFor:'instance creation'!
|
|
163 |
|
|
164 |
newSignalMayProceed:aBoolean
|
159
|
165 |
"create a new signal, using the receiver as a prototype and
|
|
166 |
setting the parent of the new signal to the receiver."
|
1
|
167 |
|
159
|
168 |
|newSignal|
|
|
169 |
|
|
170 |
newSignal := (self copy) mayProceed:aBoolean.
|
|
171 |
newSignal parent:self.
|
|
172 |
^ newSignal
|
1
|
173 |
!
|
|
174 |
|
|
175 |
newSignal
|
159
|
176 |
"create a new signal, using the receiver as a prototype and
|
|
177 |
setting the parent of the new signal to the receiver."
|
|
178 |
|
|
179 |
^ (self copy) parent:self
|
|
180 |
! !
|
1
|
181 |
|
159
|
182 |
!Signal methodsFor:'copying'!
|
|
183 |
|
|
184 |
deepCopy
|
|
185 |
"raise an error - deepCopy is not allowed for signals"
|
|
186 |
|
|
187 |
^ self deepCopyError
|
1
|
188 |
! !
|
|
189 |
|
|
190 |
!Signal methodsFor:'accessing'!
|
|
191 |
|
|
192 |
nameClass:aClass message:aSelector
|
159
|
193 |
"this sets the class & selector of a method which returns
|
|
194 |
that signal - this is simply for documentation purposes -
|
|
195 |
see Signal>>printOn: implementation.
|
|
196 |
(took me a while to find that one out ;-)"
|
1
|
197 |
|
|
198 |
nameClass := aClass.
|
|
199 |
message := aSelector
|
|
200 |
!
|
|
201 |
|
|
202 |
mayProceed:aBoolean
|
44
|
203 |
"set/clear the signals ability to proceed.
|
|
204 |
This flag is not checked in the current version of
|
|
205 |
the debugger."
|
1
|
206 |
|
|
207 |
mayProceed := aBoolean
|
|
208 |
!
|
|
209 |
|
159
|
210 |
parent:aSignal
|
|
211 |
"set the parent-signal of the receiver."
|
|
212 |
|
|
213 |
parent := aSignal
|
|
214 |
!
|
|
215 |
|
|
216 |
parent
|
|
217 |
"return the parent-signal of the receiver"
|
|
218 |
|
|
219 |
^ parent
|
|
220 |
!
|
|
221 |
|
1
|
222 |
notifierString:aString
|
|
223 |
"set the notifier string"
|
|
224 |
|
|
225 |
notifierString := aString
|
|
226 |
!
|
|
227 |
|
|
228 |
notifierString
|
|
229 |
"return the notifier string"
|
|
230 |
|
|
231 |
^ notifierString
|
70
|
232 |
!
|
|
233 |
|
120
|
234 |
handlerBlock:aOneArgBlock
|
|
235 |
"set the handlerblock - this will be evaluated with the exception
|
|
236 |
object as argument, if no #handle:do: context was found on the stack."
|
70
|
237 |
|
120
|
238 |
handlerBlock := aOneArgBlock
|
171
|
239 |
!
|
|
240 |
|
|
241 |
handlerBlock
|
|
242 |
"return the handlerblock - if non-nil, this will be evaluated with the exception
|
|
243 |
object as argument, if no #handle:do: context was found on the stack."
|
|
244 |
|
|
245 |
^ handlerBlock
|
1
|
246 |
! !
|
|
247 |
|
159
|
248 |
!Signal methodsFor:'printing'!
|
|
249 |
|
|
250 |
printOn:aStream
|
|
251 |
"append a printed representation of the receiver on aStream"
|
|
252 |
|
|
253 |
nameClass notNil ifTrue:[
|
|
254 |
aStream nextPutAll:nameClass name.
|
|
255 |
aStream space.
|
|
256 |
aStream nextPutAll:message.
|
|
257 |
^ self
|
|
258 |
].
|
|
259 |
^ super printOn:aStream
|
|
260 |
! !
|
|
261 |
|
1
|
262 |
!Signal methodsFor:'save evaluation'!
|
|
263 |
|
|
264 |
handle:handleBlock do:aBlock
|
|
265 |
"evaluate the argument, aBlock.
|
|
266 |
If the receiver-signal is raised during evaluation,
|
|
267 |
evaluate the handleBlock passing it an Exception argument.
|
|
268 |
The handler may decide how to react to the signal by sending
|
|
269 |
a corresponding message to the exception (see there).
|
|
270 |
If the signal is not raised, return the value of evaluating
|
|
271 |
aBlock."
|
|
272 |
|
|
273 |
^ aBlock value "the real logic is in raise/Exception"
|
95
|
274 |
|
|
275 |
"
|
|
276 |
Object messageNotUnderstoodSignal handle:[:ex |
|
159
|
277 |
'oops' printNL.
|
|
278 |
ex return
|
95
|
279 |
] do:[
|
159
|
280 |
123 size open
|
95
|
281 |
]
|
|
282 |
"
|
120
|
283 |
|
95
|
284 |
"
|
|
285 |
|num|
|
|
286 |
|
|
287 |
num := 0.
|
|
288 |
Number divisionByZeroSignal handle:[:ex |
|
159
|
289 |
'oops' printNL.
|
|
290 |
ex return
|
95
|
291 |
] do:[
|
159
|
292 |
123 / num
|
95
|
293 |
]
|
|
294 |
"
|
44
|
295 |
!
|
|
296 |
|
|
297 |
catch:aBlock
|
159
|
298 |
"evaluate the argument, aBlock.
|
44
|
299 |
If the receiver-signal is raised during evaluation, abort
|
159
|
300 |
the evaluation and return true; otherwise return false.
|
95
|
301 |
This is the catch & throw mechanism found in other languages,
|
|
302 |
where the returned value indicates if an exception occured."
|
44
|
303 |
|
70
|
304 |
|raiseOccurred|
|
|
305 |
|
|
306 |
raiseOccurred := false.
|
|
307 |
self handle:[:ex | raiseOccurred := true. ex return] do:aBlock.
|
|
308 |
^ raiseOccurred
|
44
|
309 |
|
61
|
310 |
"
|
|
311 |
Object messageNotUnderstoodSignal catch:[
|
159
|
312 |
123 size open
|
|
313 |
]
|
|
314 |
"
|
|
315 |
!
|
|
316 |
|
|
317 |
ignore:aBlock
|
|
318 |
"evaluate the argument, aBlock.
|
|
319 |
Ignore the receiver-signal during evaluation - i.e. simply
|
|
320 |
continue. This makes only sense for some signals, such as UserInterrupt
|
|
321 |
or AbortSignals, because continuing after an exception without any cleanup
|
|
322 |
will often lead to followup-errors."
|
|
323 |
|
|
324 |
^ self handle:[:ex | ex proceed] do:aBlock.
|
|
325 |
|
|
326 |
"
|
|
327 |
Object messageNotUnderstoodSignal ignore:[
|
|
328 |
123 size open
|
61
|
329 |
]
|
|
330 |
"
|
1
|
331 |
! !
|
|
332 |
|
70
|
333 |
!Signal methodsFor:'queries'!
|
|
334 |
|
159
|
335 |
accepts:aSignal
|
|
336 |
"return true, if the receiver accepts the argument, aSignal.
|
214
|
337 |
(i.e. the receiver is aSignal or a parent of it). False otherwise."
|
159
|
338 |
|
|
339 |
|s|
|
|
340 |
|
|
341 |
s := aSignal.
|
|
342 |
[s notNil] whileTrue:[
|
|
343 |
self == s ifTrue:[^ true].
|
|
344 |
s := s parent
|
|
345 |
].
|
|
346 |
^ false
|
|
347 |
!
|
|
348 |
|
259
|
349 |
isSignal
|
|
350 |
"return true, if the receiver is some kind of signal;
|
|
351 |
true returned here - the method is redefined from Object."
|
|
352 |
|
|
353 |
^ true
|
|
354 |
!
|
|
355 |
|
70
|
356 |
isHandled
|
|
357 |
"return true, if there is a handler for the receiver signal.
|
|
358 |
Raising an unhandled signal will usually lead into the debugger,
|
|
359 |
but can be cought globally by setting Exceptions EmergencyHandler."
|
|
360 |
|
171
|
361 |
^ self isHandledIn:(thisContext sender).
|
|
362 |
!
|
|
363 |
|
|
364 |
isHandledIn:aContext
|
|
365 |
"return true, if there is a handler for the receiver signal in the
|
|
366 |
contextChain starting with aContext."
|
|
367 |
|
70
|
368 |
|con|
|
|
369 |
|
171
|
370 |
con := aContext.
|
70
|
371 |
[con notNil] whileTrue:[
|
159
|
372 |
(con selector == #handle:do:) ifTrue:[
|
|
373 |
"
|
|
374 |
is this is the Signal>>handle:do: context
|
|
375 |
or a SignalSet>>handle:do: context with self in it ?
|
|
376 |
"
|
|
377 |
(con receiver accepts:self) ifTrue:[
|
|
378 |
"found a handler context"
|
70
|
379 |
|
159
|
380 |
^ true
|
|
381 |
].
|
|
382 |
].
|
|
383 |
con := con sender
|
70
|
384 |
].
|
|
385 |
^ false
|
|
386 |
! !
|
|
387 |
|
1
|
388 |
!Signal methodsFor:'raising'!
|
|
389 |
|
|
390 |
raise
|
|
391 |
"raise a signal - create an Exception object
|
2
|
392 |
and call the handler with this as argument.
|
70
|
393 |
The signals notifierString is used as errorString."
|
1
|
394 |
|
171
|
395 |
|ex|
|
1
|
396 |
|
61
|
397 |
ex := Exception new
|
159
|
398 |
signal:self
|
|
399 |
parameter:nil
|
|
400 |
errorString:notifierString
|
|
401 |
suspendedContext:thisContext sender.
|
44
|
402 |
|
171
|
403 |
^ ex raise
|
1
|
404 |
!
|
|
405 |
|
329
|
406 |
raiseFrom:something
|
|
407 |
"raise a signal - create an Exception object with aParameter
|
|
408 |
and call the handler with this as argument..
|
|
409 |
The printString of the argument is used as errorString.
|
|
410 |
XXX: I am not certain, if this is the correct behavior (seen in remoteInvocation-goodie)"
|
|
411 |
|
|
412 |
|ex|
|
|
413 |
|
|
414 |
ex := Exception new
|
|
415 |
signal:self
|
|
416 |
parameter:nil
|
|
417 |
errorString:something printString
|
|
418 |
suspendedContext:thisContext sender.
|
|
419 |
|
|
420 |
^ ex raise
|
|
421 |
!
|
|
422 |
|
255
|
423 |
raiseIn:aContext
|
|
424 |
"raise a signal - create an Exception object
|
|
425 |
and call the handler with this as argument.
|
|
426 |
The signals notifierString is used as errorString.
|
|
427 |
The additional context is passed as the context responsible for the raise."
|
|
428 |
|
|
429 |
|ex|
|
|
430 |
|
|
431 |
ex := Exception new
|
|
432 |
signal:self
|
|
433 |
parameter:nil
|
|
434 |
errorString:notifierString
|
|
435 |
suspendedContext:aContext.
|
|
436 |
|
|
437 |
^ ex raise
|
|
438 |
!
|
|
439 |
|
329
|
440 |
raiseWith:aParameter
|
|
441 |
"raise a signal - create an Exception object with aParameter
|
|
442 |
and call the handler with this as argument..
|
|
443 |
The signals notifierString is used as errorString."
|
|
444 |
|
|
445 |
^ self raiseRequestWith:aParameter "/ for now ...
|
|
446 |
!
|
|
447 |
|
|
448 |
raiseWith:aParameter errorString:aString
|
|
449 |
"raise a signal - create an Exception object with aParameter
|
|
450 |
and call the handler with this as argument..
|
|
451 |
The argument, aString is used as errorString."
|
|
452 |
|
|
453 |
^ self raiseRequestWith:aParameter errorString:aString "/ for now ...
|
|
454 |
!
|
|
455 |
|
1
|
456 |
raiseRequestWith:aParameter
|
|
457 |
"raise a signal - create an Exception object with aParameter
|
70
|
458 |
and call the handler with this as argument..
|
|
459 |
The signals notifierString is used as errorString."
|
|
460 |
|
171
|
461 |
|ex|
|
2
|
462 |
|
61
|
463 |
ex := Exception new
|
159
|
464 |
signal:self
|
|
465 |
parameter:aParameter
|
|
466 |
errorString:notifierString
|
|
467 |
suspendedContext:thisContext sender.
|
44
|
468 |
|
171
|
469 |
^ ex raise
|
2
|
470 |
!
|
|
471 |
|
255
|
472 |
raiseRequestWith:aParameter in:aContext
|
|
473 |
"raise a signal - create an Exception object with aParameter
|
|
474 |
and call the handler with this as argument..
|
|
475 |
The signals notifierString is used as errorString.
|
|
476 |
The additional context is passed as the context responsible for the raise."
|
|
477 |
|
|
478 |
|ex|
|
|
479 |
|
|
480 |
ex := Exception new
|
|
481 |
signal:self
|
|
482 |
parameter:aParameter
|
|
483 |
errorString:notifierString
|
|
484 |
suspendedContext:aContext.
|
|
485 |
|
|
486 |
^ ex raise
|
|
487 |
!
|
|
488 |
|
2
|
489 |
raiseRequestWith:aParameter errorString:aString
|
|
490 |
"raise a signal - create an Exception object with aParameter
|
70
|
491 |
and call the handler with this as argument..
|
|
492 |
The argument, aString is used as errorString."
|
1
|
493 |
|
171
|
494 |
|ex|
|
1
|
495 |
|
61
|
496 |
ex := Exception new
|
159
|
497 |
signal:self
|
|
498 |
parameter:aParameter
|
|
499 |
errorString:aString
|
|
500 |
suspendedContext:thisContext sender.
|
44
|
501 |
|
171
|
502 |
^ ex raise
|
255
|
503 |
!
|
|
504 |
|
|
505 |
raiseRequestWith:aParameter errorString:aString in:aContext
|
|
506 |
"raise a signal - create an Exception object with aParameter
|
|
507 |
and call the handler with this as argument..
|
|
508 |
The argument, aString is used as errorString.
|
|
509 |
The additional context is passed as the context responsible for the raise."
|
|
510 |
|
|
511 |
|ex|
|
|
512 |
|
|
513 |
ex := Exception new
|
|
514 |
signal:self
|
|
515 |
parameter:aParameter
|
|
516 |
errorString:aString
|
|
517 |
suspendedContext:aContext.
|
|
518 |
|
|
519 |
^ ex raise
|
1
|
520 |
! !
|