1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
154
|
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 variableSubclass:#Context
|
|
14 |
instanceVariableNames:'flags sender home receiver selector searchClass
|
154
|
15 |
lineNr retvalTemp handle*'
|
105
|
16 |
classVariableNames:'InvalidReturnSignal'
|
1
|
17 |
poolDictionaries:''
|
|
18 |
category:'Kernel-Methods'
|
|
19 |
!
|
|
20 |
|
|
21 |
Context comment:'
|
5
|
22 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
154
|
23 |
All Rights Reserved
|
92
|
24 |
|
325
|
25 |
$Header: /cvs/stx/stx/libbasic/Context.st,v 1.28 1995-04-02 11:06:10 claus Exp $
|
54
|
26 |
'!
|
|
27 |
|
|
28 |
!Context class methodsFor:'documentation'!
|
|
29 |
|
88
|
30 |
copyright
|
|
31 |
"
|
|
32 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
154
|
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 |
"
|
325
|
46 |
$Header: /cvs/stx/stx/libbasic/Context.st,v 1.28 1995-04-02 11:06:10 claus Exp $
|
88
|
47 |
"
|
|
48 |
!
|
|
49 |
|
54
|
50 |
documentation
|
|
51 |
"
|
69
|
52 |
Context represents the stack context objects; each message send adds a context
|
|
53 |
to a chain, which can be traced back via the sender field.
|
141
|
54 |
(The actual implementation uses the machines stack for this, building real
|
|
55 |
contexts when needed only).
|
54
|
56 |
|
69
|
57 |
For both method- and block-contexts, the layout is the same.
|
141
|
58 |
For method contexts, the home-field is nil, while for blockcontexts the home-
|
|
59 |
field is either the context of its surrounding block (i.e. the context of the
|
|
60 |
block, in which the receiving block was created, if its a nested block) or of
|
|
61 |
its home method.
|
|
62 |
|
|
63 |
Contexts of cheap blocks do not have a home context - their home field is
|
|
64 |
also nil.
|
54
|
65 |
|
141
|
66 |
Currently, contexts do not contain a reference to the method or block which
|
|
67 |
created it - this is not needed for program execution, but could get the debugger
|
|
68 |
somewhat into trouble: it has to search the class hierarchy for receiver/selector
|
|
69 |
combinations to find the method. This usually works, but fails in case of methods
|
|
70 |
which are not anchored in any class - especially leading to problems with wrapper-
|
|
71 |
and lazy methods. Also, Method>>valueWithReceiver - type of invocations cannot
|
|
72 |
be easily debugged.
|
|
73 |
Therefore, the implementation will be changed in the near future, to include a
|
|
74 |
field for the method/block, and set it in the VM during program execution.
|
|
75 |
(there may be some small performance penalty for this, though).
|
|
76 |
|
216
|
77 |
Notice: due to the compilation to machine code, methods and/or block do not
|
|
78 |
always (actually: do seldom) contain bytecodes. Thus, there is no such concept
|
|
79 |
as a bytecode p-counter. To support debugging, the linenumber within the
|
|
80 |
original source is instead remembered when a send or loop entry is performed.
|
|
81 |
Since linenumbers are not always sufficient for debugging (multiple sends in one
|
|
82 |
line), this will be changed in future versions to a character offset, giving
|
|
83 |
the position of the selector in the source.
|
1
|
84 |
|
69
|
85 |
instance variables:
|
216
|
86 |
flags <SmallInteger> used by the VM; never touch.
|
|
87 |
contains info about number of args,
|
|
88 |
locals and temporaries.
|
|
89 |
|
|
90 |
sender <Context> the 'calling / sending' context
|
|
91 |
|
|
92 |
home <Context> the context, where this block was
|
|
93 |
created, or nil if its a method context
|
|
94 |
There are also cheap blocks, which do
|
|
95 |
not need a reference to the home context,
|
|
96 |
for those, its nil too.
|
|
97 |
|
|
98 |
receiver <Object> the receiver of this message
|
|
99 |
|
|
100 |
selector <Symbol> the selector of this message
|
|
101 |
|
|
102 |
searchClass <Class> the class, where the message lookup started
|
|
103 |
(for super sends) or nil, for regular sends.
|
141
|
104 |
|
216
|
105 |
lineNr <SmallInteger> the position where the context left off
|
|
106 |
(kind of p-counter)
|
141
|
107 |
|
216
|
108 |
retValTemp nil temporary - always nil, when you see the context
|
|
109 |
(used in the VM as temporary)
|
|
110 |
|
|
111 |
handle *noObject* used by the VM; not accessable, not an object
|
141
|
112 |
|
216
|
113 |
<indexed> arguments of the send followed by
|
|
114 |
locals of the method/block followed by
|
|
115 |
temporaries.
|
1
|
116 |
|
216
|
117 |
class variables:
|
|
118 |
InvalidReturnSignal signal raised when a block tries
|
|
119 |
to return ('^') from a method context
|
|
120 |
which itself has already returned
|
|
121 |
(i.e. there is no place to return to)
|
|
122 |
|
69
|
123 |
WARNING: layout and size known by the compiler and runtime system - do not change.
|
54
|
124 |
"
|
|
125 |
! !
|
1
|
126 |
|
105
|
127 |
!Context class methodsFor:'initialization'!
|
|
128 |
|
|
129 |
initialize
|
|
130 |
InvalidReturnSignal isNil ifTrue:[
|
302
|
131 |
ErrorSignal isNil ifTrue:[Object initialize].
|
154
|
132 |
|
302
|
133 |
InvalidReturnSignal := ErrorSignal newSignalMayProceed:true.
|
154
|
134 |
InvalidReturnSignal nameClass:self message:#invalidReturnSignal.
|
|
135 |
InvalidReturnSignal notifierString:'invalid return; method cannot return twice'.
|
105
|
136 |
]
|
|
137 |
! !
|
|
138 |
|
|
139 |
!Context class methodsFor:'signal access'!
|
|
140 |
|
|
141 |
invalidReturnSignal
|
141
|
142 |
"return the signal used when a method is tried to be returned twice
|
|
143 |
or, when some dead context is unwound or restarted."
|
|
144 |
|
105
|
145 |
^ InvalidReturnSignal
|
|
146 |
! !
|
|
147 |
|
3
|
148 |
!Context class methodsFor:'queries'!
|
|
149 |
|
|
150 |
isBuiltInClass
|
54
|
151 |
"this class & subclasses are known by the run-time-system"
|
3
|
152 |
|
|
153 |
^ true
|
|
154 |
! !
|
|
155 |
|
154
|
156 |
!Context methodsFor:'copying'!
|
|
157 |
|
|
158 |
deepCopy
|
|
159 |
"raise an error - deepCopy is not allowed for contexts"
|
|
160 |
|
|
161 |
^ self deepCopyError
|
|
162 |
! !
|
|
163 |
|
1
|
164 |
!Context methodsFor:'testing'!
|
|
165 |
|
|
166 |
isContext
|
|
167 |
"return true, iff the receiver is a Context, false otherwise"
|
|
168 |
|
|
169 |
^ true
|
|
170 |
!
|
|
171 |
|
|
172 |
isBlockContext
|
|
173 |
"return true, iff the receiver is a BlockContext, false otherwise"
|
|
174 |
|
|
175 |
^ false
|
54
|
176 |
!
|
25
|
177 |
|
|
178 |
isRecursive
|
|
179 |
"return true, if this context is one of a recursive send of the same
|
154
|
180 |
selector and same arguments to the same receiver before.
|
141
|
181 |
Used to detect recursive errors or recursive printing - for example."
|
25
|
182 |
|
170
|
183 |
|c rec "numArgs" "{Class: SmallInteger }"|
|
25
|
184 |
|
69
|
185 |
rec := 0.
|
25
|
186 |
c := self sender.
|
|
187 |
[c notNil] whileTrue:[
|
69
|
188 |
|
154
|
189 |
((c selector == selector)
|
|
190 |
and:[(c receiver == receiver)]) ifTrue:[
|
|
191 |
"
|
|
192 |
stupid: the current ST/X context does not include
|
|
193 |
the method, but the class, in which the search started ...
|
|
194 |
"
|
212
|
195 |
"/ (c searchClass whichClassImplements:selector) == (self searchClass whichClassImplements:selector) ifTrue:[
|
|
196 |
c methodClass == self methodClass ifTrue:[
|
170
|
197 |
"/ "
|
|
198 |
"/ finally, look for different arguments
|
|
199 |
"/ "
|
|
200 |
"/ numArgs := self numArgs.
|
|
201 |
"/ 1 to:numArgs do:[:argIndex |
|
|
202 |
"/ (self argAt:argIndex) == (c argAt:argIndex) ifFalse:[^ false]
|
|
203 |
"/ ].
|
154
|
204 |
^ true
|
|
205 |
]
|
|
206 |
].
|
|
207 |
c := c sender.
|
|
208 |
"
|
|
209 |
this special test was added to get out after a while
|
|
210 |
if the sender chain is corrupt - this gives us at least
|
|
211 |
a chance to find those errors.
|
|
212 |
"
|
|
213 |
rec := rec + 1.
|
|
214 |
rec >= 100000 ifTrue:[
|
|
215 |
'bad context chain' errorPrintNL.
|
|
216 |
^ true
|
|
217 |
]
|
25
|
218 |
].
|
|
219 |
^ false
|
|
220 |
! !
|
|
221 |
|
1
|
222 |
!Context methodsFor:'accessing'!
|
|
223 |
|
|
224 |
instVarAt:index
|
69
|
225 |
"have to catch instVar access to retVal and handle - they are invalid.
|
|
226 |
Notice, that one of the next ST/X versions will get some syntactic
|
|
227 |
extension to get this automatically)."
|
1
|
228 |
|
|
229 |
(index == 8) ifTrue:[^ nil].
|
|
230 |
(index == 9) ifTrue:[^ nil].
|
|
231 |
^ super instVarAt:index
|
|
232 |
!
|
|
233 |
|
|
234 |
instVarAt:index put:value
|
69
|
235 |
"have to catch instVar access to retVal and handle - they are invalid.
|
|
236 |
Notice, that one of the next ST/X versions will get some syntactic
|
|
237 |
extension to get this automatically)."
|
1
|
238 |
|
|
239 |
(index == 8) ifTrue:[^ nil].
|
|
240 |
(index == 9) ifTrue:[^ nil].
|
|
241 |
^ super instVarAt:index put:value
|
|
242 |
!
|
|
243 |
|
|
244 |
methodHome
|
|
245 |
"return the method-home - for method contexts this is the receiver"
|
|
246 |
|
|
247 |
^ self
|
|
248 |
!
|
|
249 |
|
|
250 |
home
|
|
251 |
"return the immediate home of the receiver.
|
|
252 |
for block contexts, this is the methodcontext, where the block was created,
|
|
253 |
for nested block contexts, its the surrounding blocks context.
|
|
254 |
for method-contexts this is nil."
|
|
255 |
|
|
256 |
^ nil "home"
|
|
257 |
!
|
|
258 |
|
|
259 |
method
|
54
|
260 |
"return the method for which the receiver was created.
|
|
261 |
To save time during normal execution, this information is not held in the
|
|
262 |
context, but computed here on request."
|
1
|
263 |
|
|
264 |
|c|
|
|
265 |
|
325
|
266 |
c := self searchClass.
|
|
267 |
"
|
|
268 |
the below cannot happen in normal circumstances
|
|
269 |
(added to avoid recursive errors in case of a broken sender chain)
|
|
270 |
"
|
|
271 |
c isBehavior ifFalse:[
|
|
272 |
'OOPS: non class in searchClass' errorPrintNL.
|
|
273 |
' selector: ' errorPrint. selector errorPrint.
|
|
274 |
' receiver: ' errorPrint. receiver errorPrintNL.
|
|
275 |
^ nil
|
|
276 |
].
|
|
277 |
|
|
278 |
c := c whichClassImplements:selector.
|
1
|
279 |
c notNil ifTrue:[
|
154
|
280 |
^ c compiledMethodAt:selector
|
1
|
281 |
].
|
216
|
282 |
|
|
283 |
"mhmh - seems to be a context for an unbound method;
|
|
284 |
look in the senders context. Consider this a kludge.
|
|
285 |
(maybe it was not too good of an idea to not keep the current
|
325
|
286 |
method in the context ....
|
|
287 |
future versions of ST/X's message lookup may store the method in
|
|
288 |
the context.)
|
216
|
289 |
"
|
|
290 |
(sender notNil and:[sender selector startsWith:'valueWithReceiver:']) ifTrue:[
|
|
291 |
^ sender receiver
|
|
292 |
].
|
|
293 |
|
1
|
294 |
^ nil
|
|
295 |
!
|
|
296 |
|
212
|
297 |
methodClass
|
|
298 |
"return the class in which the method for which the receiver was created is."
|
|
299 |
|
|
300 |
^ self searchClass whichClassImplements:selector.
|
|
301 |
!
|
|
302 |
|
1
|
303 |
sender
|
|
304 |
"return the sender of the context"
|
|
305 |
|
69
|
306 |
"this special test is for the very first context (startup-context);
|
|
307 |
actually, its cosmetics, to avoid a visible nil>>nil context in the debugger."
|
1
|
308 |
|
69
|
309 |
"
|
|
310 |
(sender isNil or:[sender selector isNil and:[sender sender isNil]]) ifTrue:[^ nil].
|
|
311 |
"
|
1
|
312 |
^ sender
|
|
313 |
!
|
|
314 |
|
|
315 |
receiver
|
|
316 |
"return the receiver of the context"
|
|
317 |
|
|
318 |
^ receiver
|
|
319 |
!
|
|
320 |
|
|
321 |
searchClass
|
69
|
322 |
"this is the class where the method-lookup started;
|
|
323 |
for normal sends, it is nil (or sometimes the receivers class).
|
|
324 |
For supersends, its the superclass of the one, in which the
|
|
325 |
caller was defined."
|
1
|
326 |
|
|
327 |
searchClass notNil ifTrue:[^ searchClass].
|
|
328 |
^ receiver class
|
|
329 |
!
|
|
330 |
|
|
331 |
selector
|
54
|
332 |
"return the selector of the method for which the context was created"
|
1
|
333 |
|
|
334 |
^ selector
|
|
335 |
!
|
|
336 |
|
154
|
337 |
numArgs
|
1
|
338 |
"return the number of arguments to the Block/Method"
|
|
339 |
|
|
340 |
%{ /* NOCONTEXT */
|
|
341 |
|
|
342 |
RETURN ( _MKSMALLINT( (_intVal(_INST(flags)) >> __NARG_SHIFT) & __NARG_MASK) );
|
|
343 |
%}
|
|
344 |
!
|
|
345 |
|
|
346 |
nvars
|
54
|
347 |
"return the number of local variables of the Block/Method"
|
1
|
348 |
|
|
349 |
%{ /* NOCONTEXT */
|
|
350 |
|
|
351 |
RETURN ( _MKSMALLINT( (_intVal(_INST(flags)) >> __NVAR_SHIFT) & __NVAR_MASK) );
|
|
352 |
%}
|
|
353 |
!
|
|
354 |
|
54
|
355 |
ntemp
|
|
356 |
"return the number of temporary variables of the Block/Method.
|
|
357 |
(for debugging only)"
|
|
358 |
|
154
|
359 |
^ self size - self numArgs - self nvars
|
54
|
360 |
!
|
|
361 |
|
1
|
362 |
args
|
|
363 |
"return an array filled with the arguments of this context"
|
|
364 |
|
308
|
365 |
|n|
|
1
|
366 |
|
154
|
367 |
n := self numArgs.
|
282
|
368 |
^ (Array new:n) replaceFrom:1 to:n with:self.
|
1
|
369 |
!
|
|
370 |
|
|
371 |
argsAndVars
|
|
372 |
"return an array filled with the arguments and variables of this context"
|
|
373 |
|
308
|
374 |
|n|
|
1
|
375 |
|
282
|
376 |
n := self numArgs + self nvars.
|
|
377 |
^ (Array new:n) replaceFrom:1 to:n with:self.
|
1
|
378 |
!
|
|
379 |
|
202
|
380 |
temporaries
|
|
381 |
"return an array filled with the temporaries of this context"
|
|
382 |
|
308
|
383 |
|nonTemps mySize|
|
202
|
384 |
|
|
385 |
nonTemps := self numArgs + self nvars.
|
|
386 |
mySize := self ntemp.
|
308
|
387 |
^ (Array new:mySize) replaceFrom:1 to:mySize with:self startingAt:nonTemps+1
|
202
|
388 |
!
|
|
389 |
|
10
|
390 |
argAt:n
|
|
391 |
"return the n'th argument"
|
|
392 |
|
|
393 |
^ self at:n
|
|
394 |
!
|
|
395 |
|
|
396 |
argAt:n put:value
|
|
397 |
"set the n'th argument - useful when the receiver should be restarted"
|
|
398 |
|
|
399 |
^ self at:n put:value
|
|
400 |
!
|
|
401 |
|
|
402 |
varAt:n
|
|
403 |
"return the n'th local variable"
|
|
404 |
|
154
|
405 |
^ self at:(n + self numArgs)
|
10
|
406 |
!
|
|
407 |
|
|
408 |
varAt:n put:value
|
|
409 |
"set the n'th local variable - useful when the receiver should be restarted
|
54
|
410 |
or continued"
|
10
|
411 |
|
154
|
412 |
self at:(n + self numArgs) put:value
|
10
|
413 |
!
|
|
414 |
|
1
|
415 |
lineNumber
|
|
416 |
"this returns the lineNumber within the methods source, where the context was
|
|
417 |
interrupted or called another method. (currently, sometimes this information
|
|
418 |
is not available - in this case 0 is returned)"
|
|
419 |
|
|
420 |
^ lineNr
|
|
421 |
! !
|
|
422 |
|
92
|
423 |
!Context methodsFor:'printing & storing'!
|
1
|
424 |
|
42
|
425 |
argsDisplayString
|
1
|
426 |
|fullString n "{ Class: SmallInteger }" |
|
|
427 |
|
|
428 |
fullString := ''.
|
154
|
429 |
n := self numArgs.
|
1
|
430 |
1 to:n do:[:index |
|
154
|
431 |
fullString := fullString , (' ' , (self at:index) displayString)
|
1
|
432 |
].
|
|
433 |
^ fullString
|
|
434 |
!
|
|
435 |
|
|
436 |
receiverPrintString
|
37
|
437 |
"return a string describing the receiver of the context"
|
|
438 |
|
170
|
439 |
|receiverClass receiverClassName newString implementorClass|
|
1
|
440 |
|
170
|
441 |
receiverClass := receiver class.
|
|
442 |
receiverClassName := receiverClass name.
|
|
443 |
(receiverClass == SmallInteger) ifTrue:[
|
154
|
444 |
newString := '(' , receiver printString , ') ' , receiverClassName
|
1
|
445 |
] ifFalse:[
|
154
|
446 |
newString := receiverClassName
|
1
|
447 |
].
|
|
448 |
|
|
449 |
selector notNil ifTrue:[
|
212
|
450 |
"/ implementorClass := self searchClass whichClassImplements:selector.
|
293
|
451 |
|
|
452 |
"
|
|
453 |
kludge to avoid slow search for containing class
|
|
454 |
"
|
|
455 |
selector ~~ #doIt ifTrue:[
|
|
456 |
implementorClass := self methodClass.
|
|
457 |
].
|
154
|
458 |
implementorClass notNil ifTrue: [
|
170
|
459 |
(implementorClass ~~ receiverClass) ifTrue: [
|
154
|
460 |
newString := newString , '>>>',
|
|
461 |
implementorClass name printString
|
|
462 |
]
|
|
463 |
] ifFalse:[
|
170
|
464 |
self searchClass ~~ receiverClass ifTrue:[
|
154
|
465 |
newString := newString , '>>>' , self searchClass name
|
|
466 |
].
|
216
|
467 |
"
|
|
468 |
kludge for doIt - these unbound methods are not
|
|
469 |
found in the classes methodDictionary
|
|
470 |
"
|
|
471 |
selector ~~ #doIt ifTrue:[
|
|
472 |
newString := newString , '>>>**NONE**'
|
|
473 |
]
|
154
|
474 |
]
|
1
|
475 |
].
|
|
476 |
|
|
477 |
^ newString
|
|
478 |
!
|
|
479 |
|
|
480 |
printString
|
37
|
481 |
"return a string describing the context"
|
|
482 |
|
1
|
483 |
^ self receiverPrintString , ' ' , self selector printString
|
|
484 |
!
|
|
485 |
|
12
|
486 |
displayString
|
37
|
487 |
"return a string describing the context - for display in Inspector"
|
|
488 |
|
42
|
489 |
^ self class name , '(' , self printString , ')'
|
12
|
490 |
!
|
|
491 |
|
1
|
492 |
printOn:aStream
|
37
|
493 |
"append a printed description of the receiver onto aStream"
|
|
494 |
|
1
|
495 |
aStream nextPutAll:(self receiverPrintString).
|
|
496 |
aStream space.
|
|
497 |
self selector printOn:aStream
|
282
|
498 |
! !
|
|
499 |
|
|
500 |
!Context methodsFor:'minidebugger printing'!
|
|
501 |
|
|
502 |
fullPrint
|
|
503 |
"print the receiver, selector and args of the context
|
|
504 |
- used only for MiniDebuggers walkback print"
|
|
505 |
|
|
506 |
self receiverPrintString print.
|
|
507 |
' ' print.
|
|
508 |
selector print.
|
|
509 |
self size ~~ 0 ifTrue: [
|
|
510 |
' ' print.
|
|
511 |
self argsDisplayString print
|
|
512 |
].
|
|
513 |
' [' print. lineNr print. ']' printNewline
|
|
514 |
|
|
515 |
"
|
|
516 |
thisContext fullPrint
|
|
517 |
"
|
1
|
518 |
!
|
|
519 |
|
|
520 |
fullPrintAll
|
37
|
521 |
"print a full walkback starting at the receiver
|
282
|
522 |
- used only for MiniDebuggers walkback print"
|
37
|
523 |
|
1
|
524 |
|context|
|
282
|
525 |
|
1
|
526 |
context := self.
|
|
527 |
[context notNil] whileTrue: [
|
154
|
528 |
context fullPrint.
|
|
529 |
context := context sender
|
1
|
530 |
]
|
282
|
531 |
|
|
532 |
"
|
|
533 |
thisContext fullPrintAll
|
|
534 |
"
|
1
|
535 |
! !
|
|
536 |
|
77
|
537 |
!Context methodsFor:'error handling'!
|
|
538 |
|
|
539 |
invalidReturn:returnValue
|
|
540 |
"this message is sent by the VM, when a methods context
|
|
541 |
which has already returned is about to return again.
|
|
542 |
(i.e. about to execute a return from an already returned
|
105
|
543 |
method in a block).
|
141
|
544 |
We raise a signal here, to allow catching of that situation."
|
77
|
545 |
|
141
|
546 |
"
|
|
547 |
in previous versions of ST/X and ST-80, this was no error;
|
282
|
548 |
(instead, a normal blockreturn was performed to the value-sender)
|
141
|
549 |
comment out the raise to get that (old) behavior
|
282
|
550 |
BETTER REWRITE YOUR APPLICATION
|
77
|
551 |
"
|
282
|
552 |
"/ new behavior:
|
|
553 |
|
141
|
554 |
^ InvalidReturnSignal raiseRequestWith:returnValue.
|
282
|
555 |
|
|
556 |
"/ old behavior:
|
|
557 |
"/ ^ returnValue
|
77
|
558 |
! !
|
|
559 |
|
1
|
560 |
!Context methodsFor:'non local control flow'!
|
|
561 |
|
|
562 |
restart
|
|
563 |
"restart the receiver - i.e. the method is evaluated again.
|
69
|
564 |
if the context to restart already died - do nothing.
|
141
|
565 |
This is a low level helper for unwindAndRestart.
|
|
566 |
|
|
567 |
NOTICE:
|
154
|
568 |
NO unwind actions are performed - this is usually not
|
|
569 |
what you want (see Context>>unwindAndRestart).
|
141
|
570 |
|
|
571 |
LIMITATION:
|
154
|
572 |
currently a context can only be restarted by
|
|
573 |
the owning process - not from outside."
|
1
|
574 |
|
|
575 |
sender isNil ifTrue:[^ nil].
|
|
576 |
%{
|
141
|
577 |
__RESUMECONTEXT__(SND_COMMA self, RESTART_VALUE, 0);
|
1
|
578 |
|
|
579 |
/* when we reach here, something went wrong */
|
|
580 |
%}
|
|
581 |
.
|
69
|
582 |
"
|
|
583 |
debugging ...
|
|
584 |
"
|
141
|
585 |
"
|
|
586 |
'restart: context not on calling chain' errorPrintNL.
|
|
587 |
^ self error:'restart: context not on calling chain'.
|
|
588 |
"
|
|
589 |
"
|
|
590 |
tried to restart a context which is already dead
|
|
591 |
(i.e. the method/block has already executed a return)
|
|
592 |
"
|
|
593 |
^ InvalidReturnSignal
|
154
|
594 |
raiseRequestWith:nil
|
|
595 |
errorString:'restart: context not on calling chain'
|
1
|
596 |
!
|
|
597 |
|
69
|
598 |
return
|
141
|
599 |
"return from this context with nil. I.e. as if it did a ^ nil.
|
|
600 |
NOTICE:
|
154
|
601 |
NO unwind actions are performed - this is usually not
|
|
602 |
what you want (See Context>>unwind).
|
|
603 |
This is a low level method - a helper for unwind.
|
1
|
604 |
|
141
|
605 |
LIMITATION:
|
154
|
606 |
currently a context can only be returned by
|
|
607 |
the owning process - not from outside."
|
141
|
608 |
|
|
609 |
^ self return:nil
|
1
|
610 |
!
|
|
611 |
|
69
|
612 |
return:value
|
|
613 |
"return from this context as if it did a '^ value'.
|
141
|
614 |
NOTICE:
|
154
|
615 |
NO unwind actions are performed- this is usually not
|
|
616 |
what you want (See Context>>unwind:).
|
|
617 |
This is a low level method - a helper for unwind.
|
141
|
618 |
|
|
619 |
LIMITATION:
|
154
|
620 |
currently a context can only be returned by
|
|
621 |
the owning process - not from outside."
|
1
|
622 |
|
|
623 |
sender isNil ifTrue:[^ nil].
|
|
624 |
%{
|
141
|
625 |
__RESUMECONTEXT__(SND_COMMA self, value, 0);
|
1
|
626 |
|
|
627 |
/* when we reach here, something went wrong */
|
|
628 |
%}
|
|
629 |
.
|
69
|
630 |
"
|
|
631 |
debugging ...
|
|
632 |
"
|
141
|
633 |
"
|
|
634 |
'return: context not on calling chain' errorPrintNL.
|
|
635 |
^ self error:'return: context not on calling chain'.
|
|
636 |
"
|
|
637 |
"
|
|
638 |
tried to return a context which is already dead
|
|
639 |
(i.e. the method/block has already executed a return)
|
|
640 |
"
|
|
641 |
^ InvalidReturnSignal
|
154
|
642 |
raiseRequestWith:value
|
|
643 |
errorString:'return: context not on calling chain'
|
1
|
644 |
!
|
|
645 |
|
69
|
646 |
resume
|
141
|
647 |
"resume execution in this context. I.e. as if the method called
|
|
648 |
last by the receiver did a ^ nil.
|
69
|
649 |
If the context has already returned, do nothing.
|
141
|
650 |
|
|
651 |
NOTICE:
|
154
|
652 |
NO unwind actions are performed (see Context>>unwind).
|
69
|
653 |
|
141
|
654 |
LIMITATION:
|
154
|
655 |
currently a context can only be resumed by
|
|
656 |
the owning process - not from outside."
|
141
|
657 |
|
|
658 |
^ self resume:nil
|
69
|
659 |
!
|
|
660 |
|
|
661 |
resume:value
|
|
662 |
"resume the receiver - as if it got 'value' from whatever
|
|
663 |
it called.
|
141
|
664 |
If the context has already returned - do nothing.
|
|
665 |
|
|
666 |
NOTICE:
|
154
|
667 |
NO unwind actions are performed (see Context>>unwind:).
|
141
|
668 |
|
|
669 |
LIMITATION:
|
154
|
670 |
currently a context can only be resumed by
|
|
671 |
the owning process - not from outside."
|
69
|
672 |
|
|
673 |
|con|
|
|
674 |
|
141
|
675 |
"
|
|
676 |
starting with this context, find the one below and return from it
|
|
677 |
"
|
69
|
678 |
con := thisContext.
|
216
|
679 |
%{
|
|
680 |
while ((con != nil) && (_ContextInstPtr(con)->c_sender != self)) {
|
|
681 |
con = _ContextInstPtr(con)->c_sender;
|
|
682 |
}
|
|
683 |
%}.
|
|
684 |
"/ [con notNil and:[con sender ~~ self]] whileTrue:[
|
|
685 |
"/ con := con sender
|
|
686 |
"/ ].
|
|
687 |
|
69
|
688 |
con isNil ifTrue:[
|
154
|
689 |
"
|
|
690 |
debugging ...
|
|
691 |
"
|
141
|
692 |
"
|
154
|
693 |
'resume: context not on calling chain' errorPrintNL.
|
|
694 |
^ self error:'resume: context not on calling chain'.
|
141
|
695 |
"
|
154
|
696 |
"
|
|
697 |
tried to continue in context which is already dead
|
|
698 |
(i.e. the method/block has already executed a return)
|
|
699 |
"
|
|
700 |
^ InvalidReturnSignal
|
|
701 |
raiseRequestWith:value
|
|
702 |
errorString:'resume: context not on calling chain'
|
69
|
703 |
].
|
141
|
704 |
^ con return:value
|
69
|
705 |
!
|
|
706 |
|
1
|
707 |
unwind
|
69
|
708 |
"return nil from the receiver - i.e. simulate a '^ nil'.
|
|
709 |
If the context has already retruned, do nothing.
|
1
|
710 |
Evaluate all unwind-blocks as specified in Block>>valueNowOrOnUnwind:
|
69
|
711 |
and Block>>valueOnUnwindDo: on the way.
|
1
|
712 |
|
141
|
713 |
LIMITATION:
|
154
|
714 |
currently a context can only be unwound by
|
|
715 |
the owning process - not from outside."
|
141
|
716 |
|
|
717 |
^ self unwind:nil
|
1
|
718 |
!
|
|
719 |
|
|
720 |
unwind:value
|
69
|
721 |
"return value from the receiver - i.e. simulate a '^ value'.
|
|
722 |
If the context has already returned , do nothing.
|
1
|
723 |
Evaluate all unwind-blocks as specified in Block>>valueNowOrOnUnwind:
|
69
|
724 |
and Block>>valueOnUnwindDo: on the way.
|
141
|
725 |
|
|
726 |
LIMITATION:
|
154
|
727 |
currently a context can only be unwound by
|
|
728 |
the owning process - not from outside."
|
1
|
729 |
|
216
|
730 |
|con sel unwindBlock|
|
1
|
731 |
|
141
|
732 |
sender isNil ifTrue:[
|
154
|
733 |
"
|
|
734 |
tried to return to a context which is already dead
|
|
735 |
(i.e. the method/block has already executed a return)
|
|
736 |
"
|
|
737 |
^ InvalidReturnSignal
|
|
738 |
raiseRequestWith:value
|
|
739 |
errorString:'unwind: no sender to unwind to'
|
141
|
740 |
].
|
1
|
741 |
|
141
|
742 |
"
|
|
743 |
start with this context, moving up, looking for unwind actions
|
|
744 |
"
|
1
|
745 |
con := thisContext.
|
69
|
746 |
[con notNil and:[con ~~ self]] whileTrue:[
|
154
|
747 |
con isBlockContext ifFalse:[
|
|
748 |
"
|
|
749 |
the way we find those unwind contexts seems kludgy ...
|
|
750 |
"
|
|
751 |
sel := con selector.
|
|
752 |
((sel == #valueNowOrOnUnwindDo:) or:[sel == #valueOnUnwindDo:]) ifTrue:[
|
|
753 |
"
|
|
754 |
... the way we evaluate the unwind blocks too
|
|
755 |
"
|
216
|
756 |
unwindBlock := con argAt:1.
|
|
757 |
unwindBlock value
|
154
|
758 |
]
|
|
759 |
].
|
|
760 |
con := con sender
|
1
|
761 |
].
|
69
|
762 |
|
141
|
763 |
"oops, I am not on the calling chain
|
|
764 |
(should we check for this situation first and NOT evaluate
|
|
765 |
the unwind actions in this case ?)
|
|
766 |
"
|
69
|
767 |
con isNil ifTrue:[
|
154
|
768 |
"
|
|
769 |
debugging ...
|
|
770 |
"
|
141
|
771 |
"
|
154
|
772 |
'unwind: context not on calling chain' errorPrintNL.
|
|
773 |
^ self error:'unwind: context not on calling chain'.
|
141
|
774 |
"
|
154
|
775 |
"
|
|
776 |
tried to return to a context which is already dead
|
|
777 |
(i.e. the method/block has already executed a return)
|
|
778 |
"
|
|
779 |
^ InvalidReturnSignal
|
|
780 |
raiseRequestWith:value
|
|
781 |
errorString:'unwind: context not on calling chain'
|
69
|
782 |
].
|
141
|
783 |
"
|
|
784 |
now, that all unwind-actions are done, I can use the
|
|
785 |
low-level return ...
|
|
786 |
"
|
|
787 |
^ self return:value
|
|
788 |
!
|
|
789 |
|
|
790 |
unwindAndRestart
|
|
791 |
"restart the receiver - i.e. the method is evaluated again.
|
|
792 |
if the context to restart already died - do nothing.
|
|
793 |
Evaluate all unwind-blocks as specified in Block>>valueNowOrOnUnwind:
|
|
794 |
and Block>>valueOnUnwindDo: before restarting.
|
|
795 |
|
|
796 |
LIMITATION:
|
154
|
797 |
currently a context can only be restarted by
|
|
798 |
the owning process - not from outside."
|
141
|
799 |
|
216
|
800 |
|con sel unwindBlock|
|
141
|
801 |
|
|
802 |
"
|
|
803 |
start with this context, moving up, looking for unwind actions
|
|
804 |
"
|
|
805 |
con := thisContext.
|
|
806 |
[con notNil and:[con ~~ self]] whileTrue:[
|
154
|
807 |
con isBlockContext ifFalse:[
|
|
808 |
"
|
|
809 |
the way we find those unwind contexts seems kludgy ...
|
|
810 |
"
|
|
811 |
sel := con selector.
|
|
812 |
((sel == #valueNowOrOnUnwindDo:) or:[sel == #valueOnUnwindDo:]) ifTrue:[
|
|
813 |
"
|
|
814 |
... the way we evaluate the unwind blocks too
|
|
815 |
"
|
216
|
816 |
unwindBlock := con argAt:1.
|
|
817 |
unwindBlock value
|
154
|
818 |
]
|
|
819 |
].
|
|
820 |
con := con sender
|
141
|
821 |
].
|
|
822 |
|
|
823 |
"oops, I am not on the calling chain
|
|
824 |
(should we check for this situation first and NOT evaluate
|
|
825 |
the unwind actions in this case ?)
|
|
826 |
"
|
|
827 |
con isNil ifTrue:[
|
154
|
828 |
"
|
|
829 |
debugging ...
|
|
830 |
"
|
141
|
831 |
"
|
154
|
832 |
'unwindAndRestart: context not on calling chain' errorPrintNL.
|
|
833 |
^ self error:'unwindAndRestart: context not on calling chain'.
|
141
|
834 |
"
|
154
|
835 |
"
|
|
836 |
tried to return to a context which is already dead
|
|
837 |
(i.e. the method/block has already executed a return)
|
|
838 |
"
|
|
839 |
^ InvalidReturnSignal
|
|
840 |
raiseRequestWith:nil
|
|
841 |
errorString:'unwindAndRestart: context not on calling chain'
|
141
|
842 |
].
|
|
843 |
"
|
|
844 |
now, that all unwind-actions are done, I can use the
|
|
845 |
low-level restart ...
|
|
846 |
"
|
|
847 |
^ self restart
|
1
|
848 |
! !
|