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 |
|
384
|
25 |
$Header: /cvs/stx/stx/libbasic/Context.st,v 1.37 1995-08-11 02:59:50 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 |
"
|
384
|
46 |
$Header: /cvs/stx/stx/libbasic/Context.st,v 1.37 1995-08-11 02:59:50 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
|
339
|
106 |
(kind of p-counter). Only the low 16bits
|
|
107 |
are valid.
|
141
|
108 |
|
216
|
109 |
retValTemp nil temporary - always nil, when you see the context
|
|
110 |
(used in the VM as temporary)
|
|
111 |
|
|
112 |
handle *noObject* used by the VM; not accessable, not an object
|
141
|
113 |
|
216
|
114 |
<indexed> arguments of the send followed by
|
|
115 |
locals of the method/block followed by
|
|
116 |
temporaries.
|
1
|
117 |
|
216
|
118 |
class variables:
|
|
119 |
InvalidReturnSignal signal raised when a block tries
|
|
120 |
to return ('^') from a method context
|
|
121 |
which itself has already returned
|
|
122 |
(i.e. there is no place to return to)
|
|
123 |
|
69
|
124 |
WARNING: layout and size known by the compiler and runtime system - do not change.
|
54
|
125 |
"
|
|
126 |
! !
|
1
|
127 |
|
105
|
128 |
!Context class methodsFor:'initialization'!
|
|
129 |
|
|
130 |
initialize
|
|
131 |
InvalidReturnSignal isNil ifTrue:[
|
302
|
132 |
InvalidReturnSignal := ErrorSignal newSignalMayProceed:true.
|
154
|
133 |
InvalidReturnSignal nameClass:self message:#invalidReturnSignal.
|
|
134 |
InvalidReturnSignal notifierString:'invalid return; method cannot return twice'.
|
105
|
135 |
]
|
|
136 |
! !
|
|
137 |
|
345
|
138 |
!Context class methodsFor:'Signal constants'!
|
105
|
139 |
|
|
140 |
invalidReturnSignal
|
141
|
141 |
"return the signal used when a method is tried to be returned twice
|
|
142 |
or, when some dead context is unwound or restarted."
|
|
143 |
|
105
|
144 |
^ InvalidReturnSignal
|
|
145 |
! !
|
|
146 |
|
3
|
147 |
!Context class methodsFor:'queries'!
|
|
148 |
|
|
149 |
isBuiltInClass
|
54
|
150 |
"this class & subclasses are known by the run-time-system"
|
3
|
151 |
|
|
152 |
^ true
|
|
153 |
! !
|
|
154 |
|
154
|
155 |
!Context methodsFor:'copying'!
|
|
156 |
|
|
157 |
deepCopy
|
|
158 |
"raise an error - deepCopy is not allowed for contexts"
|
|
159 |
|
|
160 |
^ self deepCopyError
|
|
161 |
! !
|
|
162 |
|
1
|
163 |
!Context methodsFor:'testing'!
|
|
164 |
|
|
165 |
isContext
|
|
166 |
"return true, iff the receiver is a Context, false otherwise"
|
|
167 |
|
|
168 |
^ true
|
|
169 |
!
|
|
170 |
|
|
171 |
isBlockContext
|
|
172 |
"return true, iff the receiver is a BlockContext, false otherwise"
|
|
173 |
|
|
174 |
^ false
|
54
|
175 |
!
|
25
|
176 |
|
|
177 |
isRecursive
|
|
178 |
"return true, if this context is one of a recursive send of the same
|
154
|
179 |
selector and same arguments to the same receiver before.
|
141
|
180 |
Used to detect recursive errors or recursive printing - for example."
|
25
|
181 |
|
170
|
182 |
|c rec "numArgs" "{Class: SmallInteger }"|
|
25
|
183 |
|
69
|
184 |
rec := 0.
|
25
|
185 |
c := self sender.
|
|
186 |
[c notNil] whileTrue:[
|
69
|
187 |
|
154
|
188 |
((c selector == selector)
|
|
189 |
and:[(c receiver == receiver)]) ifTrue:[
|
|
190 |
"
|
|
191 |
stupid: the current ST/X context does not include
|
|
192 |
the method, but the class, in which the search started ...
|
|
193 |
"
|
328
|
194 |
"/ (c searchClass whichClassIncludesSelector:selector) == (self searchClass whichClassIncludesSelector:selector) ifTrue:[
|
212
|
195 |
c methodClass == self methodClass ifTrue:[
|
170
|
196 |
"/ "
|
|
197 |
"/ finally, look for different arguments
|
|
198 |
"/ "
|
|
199 |
"/ numArgs := self numArgs.
|
|
200 |
"/ 1 to:numArgs do:[:argIndex |
|
|
201 |
"/ (self argAt:argIndex) == (c argAt:argIndex) ifFalse:[^ false]
|
|
202 |
"/ ].
|
154
|
203 |
^ true
|
|
204 |
]
|
|
205 |
].
|
|
206 |
c := c sender.
|
|
207 |
"
|
|
208 |
this special test was added to get out after a while
|
|
209 |
if the sender chain is corrupt - this gives us at least
|
|
210 |
a chance to find those errors.
|
|
211 |
"
|
|
212 |
rec := rec + 1.
|
|
213 |
rec >= 100000 ifTrue:[
|
|
214 |
'bad context chain' errorPrintNL.
|
|
215 |
^ true
|
|
216 |
]
|
25
|
217 |
].
|
|
218 |
^ false
|
|
219 |
! !
|
|
220 |
|
1
|
221 |
!Context methodsFor:'accessing'!
|
|
222 |
|
|
223 |
instVarAt:index
|
69
|
224 |
"have to catch instVar access to retVal and handle - they are invalid.
|
|
225 |
Notice, that one of the next ST/X versions will get some syntactic
|
|
226 |
extension to get this automatically)."
|
1
|
227 |
|
|
228 |
(index == 8) ifTrue:[^ nil].
|
|
229 |
(index == 9) ifTrue:[^ nil].
|
|
230 |
^ super instVarAt:index
|
|
231 |
!
|
|
232 |
|
|
233 |
instVarAt:index put:value
|
69
|
234 |
"have to catch instVar access to retVal and handle - they are invalid.
|
|
235 |
Notice, that one of the next ST/X versions will get some syntactic
|
|
236 |
extension to get this automatically)."
|
1
|
237 |
|
|
238 |
(index == 8) ifTrue:[^ nil].
|
|
239 |
(index == 9) ifTrue:[^ nil].
|
|
240 |
^ super instVarAt:index put:value
|
|
241 |
!
|
|
242 |
|
|
243 |
methodHome
|
|
244 |
"return the method-home - for method contexts this is the receiver"
|
|
245 |
|
|
246 |
^ self
|
|
247 |
!
|
|
248 |
|
|
249 |
home
|
|
250 |
"return the immediate home of the receiver.
|
|
251 |
for block contexts, this is the methodcontext, where the block was created,
|
|
252 |
for nested block contexts, its the surrounding blocks context.
|
|
253 |
for method-contexts this is nil."
|
|
254 |
|
|
255 |
^ nil "home"
|
|
256 |
!
|
|
257 |
|
|
258 |
method
|
54
|
259 |
"return the method for which the receiver was created.
|
|
260 |
To save time during normal execution, this information is not held in the
|
|
261 |
context, but computed here on request."
|
1
|
262 |
|
|
263 |
|c|
|
|
264 |
|
325
|
265 |
c := self searchClass.
|
|
266 |
"
|
|
267 |
the below cannot happen in normal circumstances
|
|
268 |
(added to avoid recursive errors in case of a broken sender chain)
|
|
269 |
"
|
|
270 |
c isBehavior ifFalse:[
|
|
271 |
'OOPS: non class in searchClass' errorPrintNL.
|
|
272 |
' selector: ' errorPrint. selector errorPrint.
|
|
273 |
' receiver: ' errorPrint. receiver errorPrintNL.
|
|
274 |
^ nil
|
|
275 |
].
|
|
276 |
|
328
|
277 |
c := c whichClassIncludesSelector:selector.
|
1
|
278 |
c notNil ifTrue:[
|
154
|
279 |
^ c compiledMethodAt:selector
|
1
|
280 |
].
|
216
|
281 |
|
|
282 |
"mhmh - seems to be a context for an unbound method;
|
|
283 |
look in the senders context. Consider this a kludge.
|
|
284 |
(maybe it was not too good of an idea to not keep the current
|
325
|
285 |
method in the context ....
|
|
286 |
future versions of ST/X's message lookup may store the method in
|
|
287 |
the context.)
|
216
|
288 |
"
|
|
289 |
(sender notNil and:[sender selector startsWith:'valueWithReceiver:']) ifTrue:[
|
|
290 |
^ sender receiver
|
|
291 |
].
|
|
292 |
|
1
|
293 |
^ nil
|
|
294 |
!
|
|
295 |
|
212
|
296 |
methodClass
|
|
297 |
"return the class in which the method for which the receiver was created is."
|
|
298 |
|
328
|
299 |
^ self searchClass whichClassIncludesSelector:selector.
|
212
|
300 |
!
|
|
301 |
|
1
|
302 |
sender
|
|
303 |
"return the sender of the context"
|
|
304 |
|
69
|
305 |
"this special test is for the very first context (startup-context);
|
|
306 |
actually, its cosmetics, to avoid a visible nil>>nil context in the debugger."
|
1
|
307 |
|
69
|
308 |
"
|
|
309 |
(sender isNil or:[sender selector isNil and:[sender sender isNil]]) ifTrue:[^ nil].
|
|
310 |
"
|
1
|
311 |
^ sender
|
|
312 |
!
|
|
313 |
|
|
314 |
receiver
|
|
315 |
"return the receiver of the context"
|
|
316 |
|
|
317 |
^ receiver
|
|
318 |
!
|
|
319 |
|
|
320 |
searchClass
|
69
|
321 |
"this is the class where the method-lookup started;
|
|
322 |
for normal sends, it is nil (or sometimes the receivers class).
|
|
323 |
For supersends, its the superclass of the one, in which the
|
|
324 |
caller was defined."
|
1
|
325 |
|
|
326 |
searchClass notNil ifTrue:[^ searchClass].
|
|
327 |
^ receiver class
|
|
328 |
!
|
|
329 |
|
|
330 |
selector
|
54
|
331 |
"return the selector of the method for which the context was created"
|
1
|
332 |
|
|
333 |
^ selector
|
|
334 |
!
|
|
335 |
|
154
|
336 |
numArgs
|
1
|
337 |
"return the number of arguments to the Block/Method"
|
|
338 |
|
|
339 |
%{ /* NOCONTEXT */
|
|
340 |
|
|
341 |
RETURN ( _MKSMALLINT( (_intVal(_INST(flags)) >> __NARG_SHIFT) & __NARG_MASK) );
|
|
342 |
%}
|
|
343 |
!
|
|
344 |
|
|
345 |
nvars
|
54
|
346 |
"return the number of local variables of the Block/Method"
|
1
|
347 |
|
|
348 |
%{ /* NOCONTEXT */
|
|
349 |
|
|
350 |
RETURN ( _MKSMALLINT( (_intVal(_INST(flags)) >> __NVAR_SHIFT) & __NVAR_MASK) );
|
|
351 |
%}
|
|
352 |
!
|
|
353 |
|
54
|
354 |
ntemp
|
|
355 |
"return the number of temporary variables of the Block/Method.
|
|
356 |
(for debugging only)"
|
|
357 |
|
154
|
358 |
^ self size - self numArgs - self nvars
|
54
|
359 |
!
|
|
360 |
|
1
|
361 |
args
|
|
362 |
"return an array filled with the arguments of this context"
|
|
363 |
|
308
|
364 |
|n|
|
1
|
365 |
|
154
|
366 |
n := self numArgs.
|
282
|
367 |
^ (Array new:n) replaceFrom:1 to:n with:self.
|
1
|
368 |
!
|
|
369 |
|
|
370 |
argsAndVars
|
|
371 |
"return an array filled with the arguments and variables of this context"
|
|
372 |
|
308
|
373 |
|n|
|
1
|
374 |
|
282
|
375 |
n := self numArgs + self nvars.
|
|
376 |
^ (Array new:n) replaceFrom:1 to:n with:self.
|
1
|
377 |
!
|
|
378 |
|
340
|
379 |
vars
|
|
380 |
"return an array filled with the local variables of this context"
|
|
381 |
|
|
382 |
|nonVars mySize|
|
|
383 |
|
|
384 |
nonVars := self numArgs.
|
|
385 |
mySize := self nvars.
|
|
386 |
^ (Array new:mySize) replaceFrom:1 to:mySize with:self startingAt:nonVars+1
|
|
387 |
!
|
|
388 |
|
202
|
389 |
temporaries
|
|
390 |
"return an array filled with the temporaries of this context"
|
|
391 |
|
308
|
392 |
|nonTemps mySize|
|
202
|
393 |
|
|
394 |
nonTemps := self numArgs + self nvars.
|
|
395 |
mySize := self ntemp.
|
308
|
396 |
^ (Array new:mySize) replaceFrom:1 to:mySize with:self startingAt:nonTemps+1
|
202
|
397 |
!
|
|
398 |
|
10
|
399 |
argAt:n
|
|
400 |
"return the n'th argument"
|
|
401 |
|
|
402 |
^ self at:n
|
|
403 |
!
|
|
404 |
|
|
405 |
argAt:n put:value
|
|
406 |
"set the n'th argument - useful when the receiver should be restarted"
|
|
407 |
|
|
408 |
^ self at:n put:value
|
|
409 |
!
|
|
410 |
|
|
411 |
varAt:n
|
|
412 |
"return the n'th local variable"
|
|
413 |
|
154
|
414 |
^ self at:(n + self numArgs)
|
10
|
415 |
!
|
|
416 |
|
|
417 |
varAt:n put:value
|
|
418 |
"set the n'th local variable - useful when the receiver should be restarted
|
54
|
419 |
or continued"
|
10
|
420 |
|
154
|
421 |
self at:(n + self numArgs) put:value
|
10
|
422 |
!
|
|
423 |
|
1
|
424 |
lineNumber
|
|
425 |
"this returns the lineNumber within the methods source, where the context was
|
|
426 |
interrupted or called another method. (currently, sometimes this information
|
|
427 |
is not available - in this case 0 is returned)"
|
|
428 |
|
339
|
429 |
lineNr isNil ifTrue:[^ nil].
|
|
430 |
^ lineNr bitAnd:16rFFFF
|
360
|
431 |
!
|
|
432 |
|
375
|
433 |
setLineNumber:aNumber
|
|
434 |
"private entry for uncompiledCodeObject ..."
|
|
435 |
|
|
436 |
lineNr := aNumber
|
|
437 |
!
|
|
438 |
|
360
|
439 |
canReturn
|
|
440 |
"return true, if the receiver allows returning through it.
|
375
|
441 |
For normal method contexts, this returns true;
|
360
|
442 |
for blocks, it (currently) always returns false.
|
|
443 |
|
|
444 |
However, the system can be compiled (for production code), to create
|
|
445 |
contexts which cannot be returned or restarted
|
|
446 |
(except, if the method contains a returning block).
|
|
447 |
This saves some administrative work in every method
|
|
448 |
invocation and makes overall execution faster. However, it limits
|
|
449 |
the debugger, in that it cannot return from or restart those contexts.
|
|
450 |
(unwinding and termination is not affected by this optimization)
|
|
451 |
Currently, the system as delivered has this optimization disabled."
|
|
452 |
|
|
453 |
%{ /* NOCONTEXT */
|
|
454 |
|
|
455 |
RETURN ( (_intVal(_INST(flags)) & __CANNOT_RETURN) ? false : true );
|
|
456 |
%}
|
1
|
457 |
! !
|
|
458 |
|
92
|
459 |
!Context methodsFor:'printing & storing'!
|
1
|
460 |
|
42
|
461 |
argsDisplayString
|
1
|
462 |
|fullString n "{ Class: SmallInteger }" |
|
|
463 |
|
|
464 |
fullString := ''.
|
154
|
465 |
n := self numArgs.
|
1
|
466 |
1 to:n do:[:index |
|
154
|
467 |
fullString := fullString , (' ' , (self at:index) displayString)
|
1
|
468 |
].
|
|
469 |
^ fullString
|
|
470 |
!
|
|
471 |
|
|
472 |
receiverPrintString
|
37
|
473 |
"return a string describing the receiver of the context"
|
|
474 |
|
170
|
475 |
|receiverClass receiverClassName newString implementorClass|
|
1
|
476 |
|
170
|
477 |
receiverClass := receiver class.
|
|
478 |
receiverClassName := receiverClass name.
|
|
479 |
(receiverClass == SmallInteger) ifTrue:[
|
154
|
480 |
newString := '(' , receiver printString , ') ' , receiverClassName
|
1
|
481 |
] ifFalse:[
|
154
|
482 |
newString := receiverClassName
|
1
|
483 |
].
|
|
484 |
|
|
485 |
selector notNil ifTrue:[
|
328
|
486 |
"/ implementorClass := self searchClass whichClassIncludesSelector:selector.
|
293
|
487 |
|
|
488 |
"
|
|
489 |
kludge to avoid slow search for containing class
|
|
490 |
"
|
|
491 |
selector ~~ #doIt ifTrue:[
|
|
492 |
implementorClass := self methodClass.
|
|
493 |
].
|
154
|
494 |
implementorClass notNil ifTrue: [
|
170
|
495 |
(implementorClass ~~ receiverClass) ifTrue: [
|
154
|
496 |
newString := newString , '>>>',
|
|
497 |
implementorClass name printString
|
|
498 |
]
|
|
499 |
] ifFalse:[
|
170
|
500 |
self searchClass ~~ receiverClass ifTrue:[
|
154
|
501 |
newString := newString , '>>>' , self searchClass name
|
|
502 |
].
|
216
|
503 |
"
|
|
504 |
kludge for doIt - these unbound methods are not
|
|
505 |
found in the classes methodDictionary
|
|
506 |
"
|
|
507 |
selector ~~ #doIt ifTrue:[
|
|
508 |
newString := newString , '>>>**NONE**'
|
|
509 |
]
|
154
|
510 |
]
|
1
|
511 |
].
|
|
512 |
|
|
513 |
^ newString
|
|
514 |
!
|
|
515 |
|
|
516 |
printString
|
37
|
517 |
"return a string describing the context"
|
|
518 |
|
1
|
519 |
^ self receiverPrintString , ' ' , self selector printString
|
|
520 |
!
|
|
521 |
|
12
|
522 |
displayString
|
37
|
523 |
"return a string describing the context - for display in Inspector"
|
|
524 |
|
42
|
525 |
^ self class name , '(' , self printString , ')'
|
12
|
526 |
!
|
|
527 |
|
1
|
528 |
printOn:aStream
|
37
|
529 |
"append a printed description of the receiver onto aStream"
|
|
530 |
|
1
|
531 |
aStream nextPutAll:(self receiverPrintString).
|
|
532 |
aStream space.
|
|
533 |
self selector printOn:aStream
|
282
|
534 |
! !
|
|
535 |
|
|
536 |
!Context methodsFor:'minidebugger printing'!
|
|
537 |
|
|
538 |
fullPrint
|
|
539 |
"print the receiver, selector and args of the context
|
|
540 |
- used only for MiniDebuggers walkback print"
|
|
541 |
|
|
542 |
self receiverPrintString print.
|
|
543 |
' ' print.
|
|
544 |
selector print.
|
|
545 |
self size ~~ 0 ifTrue: [
|
|
546 |
' ' print.
|
|
547 |
self argsDisplayString print
|
|
548 |
].
|
339
|
549 |
' [' print. self lineNumber print. ']' printNewline
|
282
|
550 |
|
|
551 |
"
|
|
552 |
thisContext fullPrint
|
|
553 |
"
|
1
|
554 |
!
|
|
555 |
|
|
556 |
fullPrintAll
|
37
|
557 |
"print a full walkback starting at the receiver
|
282
|
558 |
- used only for MiniDebuggers walkback print"
|
37
|
559 |
|
1
|
560 |
|context|
|
282
|
561 |
|
1
|
562 |
context := self.
|
|
563 |
[context notNil] whileTrue: [
|
154
|
564 |
context fullPrint.
|
|
565 |
context := context sender
|
1
|
566 |
]
|
282
|
567 |
|
|
568 |
"
|
|
569 |
thisContext fullPrintAll
|
|
570 |
"
|
1
|
571 |
! !
|
|
572 |
|
77
|
573 |
!Context methodsFor:'error handling'!
|
|
574 |
|
|
575 |
invalidReturn:returnValue
|
|
576 |
"this message is sent by the VM, when a methods context
|
|
577 |
which has already returned is about to return again.
|
|
578 |
(i.e. about to execute a return from an already returned
|
105
|
579 |
method in a block).
|
141
|
580 |
We raise a signal here, to allow catching of that situation."
|
77
|
581 |
|
360
|
582 |
self canReturn ifTrue:[
|
379
|
583 |
"
|
|
584 |
in previous versions of ST/X and ST-80, this was no error;
|
|
585 |
(instead, a normal blockreturn was performed to the value-sender)
|
|
586 |
comment out the raise to get that (old) behavior
|
|
587 |
BETTER REWRITE YOUR APPLICATION
|
|
588 |
"
|
282
|
589 |
"/ new behavior:
|
|
590 |
|
379
|
591 |
^ InvalidReturnSignal
|
360
|
592 |
raiseRequestWith:returnValue.
|
282
|
593 |
|
|
594 |
"/ old behavior:
|
360
|
595 |
"/ ^ returnValue
|
|
596 |
].
|
|
597 |
^ InvalidReturnSignal
|
|
598 |
raiseRequestWith:returnValue
|
|
599 |
errorString:'method was compiled non-resumable'
|
|
600 |
!
|
|
601 |
|
|
602 |
invalidReturnOrRestartError:how with:value
|
|
603 |
"common error reporter for restart/return errors"
|
|
604 |
|
|
605 |
self canReturn ifTrue:[
|
379
|
606 |
"
|
|
607 |
tried to return from/restart a context which is already dead
|
|
608 |
(i.e. the method/block has already executed a return)
|
|
609 |
"
|
|
610 |
^ InvalidReturnSignal
|
360
|
611 |
raiseRequestWith:value
|
|
612 |
errorString:(how , ': context not on calling chain')
|
|
613 |
].
|
|
614 |
"
|
|
615 |
tried to return from/restart a context of a method which was compiled
|
|
616 |
unrestartable or of a block (which is never restartable)
|
|
617 |
"
|
|
618 |
^ InvalidReturnSignal
|
379
|
619 |
raiseRequestWith:value
|
360
|
620 |
errorString:(how , ': context cannot be restarted/returned from')
|
77
|
621 |
! !
|
|
622 |
|
1
|
623 |
!Context methodsFor:'non local control flow'!
|
|
624 |
|
|
625 |
restart
|
|
626 |
"restart the receiver - i.e. the method is evaluated again.
|
69
|
627 |
if the context to restart already died - do nothing.
|
141
|
628 |
This is a low level helper for unwindAndRestart.
|
|
629 |
|
|
630 |
NOTICE:
|
154
|
631 |
NO unwind actions are performed - this is usually not
|
|
632 |
what you want (see Context>>unwindAndRestart).
|
141
|
633 |
|
|
634 |
LIMITATION:
|
154
|
635 |
currently a context can only be restarted by
|
|
636 |
the owning process - not from outside."
|
1
|
637 |
|
|
638 |
sender isNil ifTrue:[^ nil].
|
|
639 |
%{
|
141
|
640 |
__RESUMECONTEXT__(SND_COMMA self, RESTART_VALUE, 0);
|
360
|
641 |
%}.
|
69
|
642 |
"
|
360
|
643 |
when we arrive here, something went wrong.
|
69
|
644 |
debugging ...
|
|
645 |
"
|
360
|
646 |
^ self invalidReturnOrRestartError:#restart with:nil
|
1
|
647 |
!
|
|
648 |
|
69
|
649 |
return
|
141
|
650 |
"return from this context with nil. I.e. as if it did a ^ nil.
|
|
651 |
NOTICE:
|
154
|
652 |
NO unwind actions are performed - this is usually not
|
|
653 |
what you want (See Context>>unwind).
|
|
654 |
This is a low level method - a helper for unwind.
|
1
|
655 |
|
141
|
656 |
LIMITATION:
|
154
|
657 |
currently a context can only be returned by
|
|
658 |
the owning process - not from outside."
|
141
|
659 |
|
|
660 |
^ self return:nil
|
1
|
661 |
!
|
|
662 |
|
69
|
663 |
return:value
|
|
664 |
"return from this context as if it did a '^ value'.
|
141
|
665 |
NOTICE:
|
154
|
666 |
NO unwind actions are performed- this is usually not
|
|
667 |
what you want (See Context>>unwind:).
|
|
668 |
This is a low level method - a helper for unwind.
|
141
|
669 |
|
|
670 |
LIMITATION:
|
154
|
671 |
currently a context can only be returned by
|
|
672 |
the owning process - not from outside."
|
1
|
673 |
|
|
674 |
sender isNil ifTrue:[^ nil].
|
|
675 |
%{
|
141
|
676 |
__RESUMECONTEXT__(SND_COMMA self, value, 0);
|
1
|
677 |
|
360
|
678 |
%}.
|
69
|
679 |
"
|
360
|
680 |
when we arrive here, something went wrong.
|
69
|
681 |
debugging ...
|
|
682 |
"
|
360
|
683 |
^ self invalidReturnOrRestartError:#return with:value
|
1
|
684 |
!
|
|
685 |
|
328
|
686 |
returnDoing:aBlock
|
|
687 |
"return from this context as if it did a '^ aBlock value'.
|
|
688 |
The block is evaluated as if called by the receiver context;
|
|
689 |
NOT the true executing context.
|
|
690 |
NOTICE:
|
|
691 |
NO unwind actions are performed- this is usually not
|
|
692 |
what you want (See Context>>unwind:).
|
|
693 |
This is a low level method - a helper for unwind.
|
|
694 |
|
|
695 |
LIMITATION:
|
|
696 |
currently a context can only be returned by
|
|
697 |
the owning process - not from outside."
|
|
698 |
|
|
699 |
sender isNil ifTrue:[^ nil].
|
|
700 |
%{
|
|
701 |
__RESUMECONTEXT__(SND_COMMA self, aBlock, 2);
|
360
|
702 |
%}.
|
328
|
703 |
"
|
360
|
704 |
when we arrive here, something went wrong.
|
328
|
705 |
debugging ...
|
|
706 |
"
|
360
|
707 |
^ self invalidReturnOrRestartError:#return with:aBlock
|
328
|
708 |
!
|
|
709 |
|
69
|
710 |
resume
|
141
|
711 |
"resume execution in this context. I.e. as if the method called
|
|
712 |
last by the receiver did a ^ nil.
|
360
|
713 |
If the context has already returned, report an error.
|
141
|
714 |
|
|
715 |
NOTICE:
|
154
|
716 |
NO unwind actions are performed (see Context>>unwind).
|
69
|
717 |
|
141
|
718 |
LIMITATION:
|
154
|
719 |
currently a context can only be resumed by
|
|
720 |
the owning process - not from outside."
|
141
|
721 |
|
|
722 |
^ self resume:nil
|
69
|
723 |
!
|
|
724 |
|
|
725 |
resume:value
|
|
726 |
"resume the receiver - as if it got 'value' from whatever
|
360
|
727 |
it called. This continues execution in the receivers method
|
|
728 |
after the point where it did its last send.
|
|
729 |
If the context has already returned - report an error.
|
141
|
730 |
|
|
731 |
NOTICE:
|
154
|
732 |
NO unwind actions are performed (see Context>>unwind:).
|
141
|
733 |
|
|
734 |
LIMITATION:
|
154
|
735 |
currently a context can only be resumed by
|
|
736 |
the owning process - not from outside."
|
69
|
737 |
|
|
738 |
|con|
|
|
739 |
|
141
|
740 |
"
|
360
|
741 |
starting with this context, find the one below (i.e. the one that I
|
|
742 |
have called) and return from it.
|
141
|
743 |
"
|
69
|
744 |
con := thisContext.
|
216
|
745 |
%{
|
|
746 |
while ((con != nil) && (_ContextInstPtr(con)->c_sender != self)) {
|
|
747 |
con = _ContextInstPtr(con)->c_sender;
|
|
748 |
}
|
|
749 |
%}.
|
|
750 |
|
69
|
751 |
con isNil ifTrue:[
|
154
|
752 |
"
|
360
|
753 |
tried to resume in context which is already dead
|
154
|
754 |
(i.e. the method/block has already executed a return)
|
|
755 |
"
|
379
|
756 |
^ con invalidReturnOrRestartError:#resume with:value
|
69
|
757 |
].
|
141
|
758 |
^ con return:value
|
69
|
759 |
!
|
|
760 |
|
1
|
761 |
unwind
|
69
|
762 |
"return nil from the receiver - i.e. simulate a '^ nil'.
|
360
|
763 |
If the context has already retruned, report an error.
|
1
|
764 |
Evaluate all unwind-blocks as specified in Block>>valueNowOrOnUnwind:
|
69
|
765 |
and Block>>valueOnUnwindDo: on the way.
|
1
|
766 |
|
141
|
767 |
LIMITATION:
|
154
|
768 |
currently a context can only be unwound by
|
|
769 |
the owning process - not from outside."
|
141
|
770 |
|
|
771 |
^ self unwind:nil
|
1
|
772 |
!
|
|
773 |
|
|
774 |
unwind:value
|
69
|
775 |
"return value from the receiver - i.e. simulate a '^ value'.
|
360
|
776 |
If the context has already returned , report an error.
|
1
|
777 |
Evaluate all unwind-blocks as specified in Block>>valueNowOrOnUnwind:
|
69
|
778 |
and Block>>valueOnUnwindDo: on the way.
|
141
|
779 |
|
|
780 |
LIMITATION:
|
154
|
781 |
currently a context can only be unwound by
|
|
782 |
the owning process - not from outside."
|
1
|
783 |
|
216
|
784 |
|con sel unwindBlock|
|
1
|
785 |
|
141
|
786 |
sender isNil ifTrue:[
|
154
|
787 |
"
|
|
788 |
tried to return to a context which is already dead
|
|
789 |
(i.e. the method/block has already executed a return)
|
|
790 |
"
|
379
|
791 |
^ self invalidReturnOrRestartError:#unwind with:value
|
141
|
792 |
].
|
1
|
793 |
|
141
|
794 |
"
|
|
795 |
start with this context, moving up, looking for unwind actions
|
|
796 |
"
|
1
|
797 |
con := thisContext.
|
69
|
798 |
[con notNil and:[con ~~ self]] whileTrue:[
|
154
|
799 |
con isBlockContext ifFalse:[
|
|
800 |
"
|
|
801 |
the way we find those unwind contexts seems kludgy ...
|
|
802 |
"
|
|
803 |
sel := con selector.
|
|
804 |
((sel == #valueNowOrOnUnwindDo:) or:[sel == #valueOnUnwindDo:]) ifTrue:[
|
|
805 |
"
|
|
806 |
... the way we evaluate the unwind blocks too
|
|
807 |
"
|
216
|
808 |
unwindBlock := con argAt:1.
|
|
809 |
unwindBlock value
|
154
|
810 |
]
|
|
811 |
].
|
|
812 |
con := con sender
|
1
|
813 |
].
|
69
|
814 |
|
141
|
815 |
"oops, I am not on the calling chain
|
|
816 |
(should we check for this situation first and NOT evaluate
|
|
817 |
the unwind actions in this case ?)
|
|
818 |
"
|
69
|
819 |
con isNil ifTrue:[
|
154
|
820 |
"
|
|
821 |
tried to return to a context which is already dead
|
|
822 |
(i.e. the method/block has already executed a return)
|
|
823 |
"
|
379
|
824 |
^ self invalidReturnOrRestartError:#unwind with:value
|
69
|
825 |
].
|
141
|
826 |
"
|
|
827 |
now, that all unwind-actions are done, I can use the
|
|
828 |
low-level return ...
|
|
829 |
"
|
|
830 |
^ self return:value
|
|
831 |
!
|
|
832 |
|
|
833 |
unwindAndRestart
|
|
834 |
"restart the receiver - i.e. the method is evaluated again.
|
|
835 |
if the context to restart already died - do nothing.
|
|
836 |
Evaluate all unwind-blocks as specified in Block>>valueNowOrOnUnwind:
|
|
837 |
and Block>>valueOnUnwindDo: before restarting.
|
|
838 |
|
|
839 |
LIMITATION:
|
154
|
840 |
currently a context can only be restarted by
|
|
841 |
the owning process - not from outside."
|
141
|
842 |
|
216
|
843 |
|con sel unwindBlock|
|
141
|
844 |
|
|
845 |
"
|
|
846 |
start with this context, moving up, looking for unwind actions
|
|
847 |
"
|
|
848 |
con := thisContext.
|
|
849 |
[con notNil and:[con ~~ self]] whileTrue:[
|
154
|
850 |
con isBlockContext ifFalse:[
|
|
851 |
"
|
|
852 |
the way we find those unwind contexts seems kludgy ...
|
|
853 |
"
|
|
854 |
sel := con selector.
|
|
855 |
((sel == #valueNowOrOnUnwindDo:) or:[sel == #valueOnUnwindDo:]) ifTrue:[
|
|
856 |
"
|
|
857 |
... the way we evaluate the unwind blocks too
|
|
858 |
"
|
216
|
859 |
unwindBlock := con argAt:1.
|
|
860 |
unwindBlock value
|
154
|
861 |
]
|
|
862 |
].
|
|
863 |
con := con sender
|
141
|
864 |
].
|
|
865 |
|
|
866 |
"oops, I am not on the calling chain
|
|
867 |
(should we check for this situation first and NOT evaluate
|
|
868 |
the unwind actions in this case ?)
|
|
869 |
"
|
|
870 |
con isNil ifTrue:[
|
154
|
871 |
"
|
|
872 |
tried to return to a context which is already dead
|
|
873 |
(i.e. the method/block has already executed a return)
|
|
874 |
"
|
379
|
875 |
^ self invalidReturnOrRestartError:#unwindAndRestart with:nil
|
141
|
876 |
].
|
|
877 |
"
|
|
878 |
now, that all unwind-actions are done, I can use the
|
|
879 |
low-level restart ...
|
|
880 |
"
|
|
881 |
^ self restart
|
328
|
882 |
!
|
|
883 |
|
|
884 |
unwindThenDo:aBlock
|
|
885 |
"return the value of aBlock from the receiver - i.e. simulate a '^ aBlock value'.
|
360
|
886 |
If the context has already returned , report an error.
|
328
|
887 |
Evaluate all unwind-blocks as specified in Block>>valueNowOrOnUnwind:
|
|
888 |
and Block>>valueOnUnwindDo: on the way.
|
|
889 |
The block is evaluated AFTER all unwind actions are performed
|
|
890 |
(i.e. the blocks sender will be the receiving context, not the
|
|
891 |
currently executing context).
|
|
892 |
|
|
893 |
LIMITATION:
|
|
894 |
currently a context can only be unwound by
|
|
895 |
the owning process - not from outside."
|
|
896 |
|
|
897 |
|con sel unwindBlock|
|
|
898 |
|
|
899 |
sender isNil ifTrue:[
|
|
900 |
"
|
|
901 |
tried to return to a context which is already dead
|
|
902 |
(i.e. the method/block has already executed a return)
|
|
903 |
"
|
379
|
904 |
^ self invalidReturnOrRestartError:#unwind with:aBlock
|
328
|
905 |
].
|
|
906 |
|
|
907 |
"
|
|
908 |
start with this context, moving up, looking for unwind actions
|
|
909 |
"
|
|
910 |
con := thisContext.
|
|
911 |
[con notNil and:[con ~~ self]] whileTrue:[
|
|
912 |
con isBlockContext ifFalse:[
|
|
913 |
"
|
|
914 |
the way we find those unwind contexts seems kludgy ...
|
|
915 |
"
|
|
916 |
sel := con selector.
|
|
917 |
((sel == #valueNowOrOnUnwindDo:) or:[sel == #valueOnUnwindDo:]) ifTrue:[
|
|
918 |
"
|
|
919 |
... the way we evaluate the unwind blocks too
|
|
920 |
"
|
|
921 |
unwindBlock := con argAt:1.
|
|
922 |
unwindBlock value
|
|
923 |
]
|
|
924 |
].
|
|
925 |
con := con sender
|
|
926 |
].
|
|
927 |
|
|
928 |
"oops, I am not on the calling chain
|
|
929 |
(should we check for this situation first and NOT evaluate
|
|
930 |
the unwind actions in this case ?)
|
|
931 |
"
|
|
932 |
con isNil ifTrue:[
|
|
933 |
"
|
|
934 |
tried to return to a context which is already dead
|
|
935 |
(i.e. the method/block has already executed a return)
|
|
936 |
"
|
379
|
937 |
^ self invalidReturnOrRestartError:#unwind with:aBlock
|
328
|
938 |
].
|
|
939 |
"
|
|
940 |
now, that all unwind-actions are done, I can use the
|
|
941 |
low-level return ...
|
|
942 |
"
|
|
943 |
^ self returnDoing:aBlock
|
1
|
944 |
! !
|