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