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