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