1
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1988-93 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
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
|
|
15 |
lineNr retvalTemp handle'
|
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
|
18 |
category:'Kernel-Methods'
|
|
19 |
!
|
|
20 |
|
|
21 |
Context comment:'
|
|
22 |
|
|
23 |
COPYRIGHT (c) 1988-93 by Claus Gittinger
|
|
24 |
All Rights Reserved
|
|
25 |
|
|
26 |
Context represents the stack context objects; each message send adds a context
|
|
27 |
to a chain, which can be traced back via the sender field. (The actual implementation
|
|
28 |
uses the machines stack for this, building real contexts when needed only).
|
|
29 |
For both method- and block-contexts, the layout is the same. For method contexts, the
|
|
30 |
home-field is nil, while for blockcontexts the home-field is either the context of its
|
|
31 |
surrounding block (if its a nested block) or of its method. Contexts of cheap blocks do not
|
|
32 |
have a home context - their home field is also nil.
|
|
33 |
|
|
34 |
Warning: layout and size known by the compiler and runtime system - do not change.
|
|
35 |
|
3
|
36 |
$Header: /cvs/stx/stx/libbasic/Context.st,v 1.3 1993-10-13 00:15:26 claus Exp $
|
1
|
37 |
'!
|
|
38 |
|
3
|
39 |
!Context class methodsFor:'queries'!
|
|
40 |
|
|
41 |
isBuiltInClass
|
|
42 |
"this class is known by the run-time-system"
|
|
43 |
|
|
44 |
^ true
|
|
45 |
! !
|
|
46 |
|
1
|
47 |
!Context methodsFor:'testing'!
|
|
48 |
|
|
49 |
isContext
|
|
50 |
"return true, iff the receiver is a Context, false otherwise"
|
|
51 |
|
|
52 |
^ true
|
|
53 |
!
|
|
54 |
|
|
55 |
isBlockContext
|
|
56 |
"return true, iff the receiver is a BlockContext, false otherwise"
|
|
57 |
|
|
58 |
^ false
|
|
59 |
! !
|
|
60 |
|
|
61 |
!Context methodsFor:'accessing'!
|
|
62 |
|
|
63 |
instVarAt:index
|
|
64 |
"have to catch instVar access to retVal and handle - they are invalid"
|
|
65 |
|
|
66 |
(index == 8) ifTrue:[^ nil].
|
|
67 |
(index == 9) ifTrue:[^ nil].
|
|
68 |
^ super instVarAt:index
|
|
69 |
!
|
|
70 |
|
|
71 |
instVarAt:index put:value
|
|
72 |
"have to catch instVar access to retVal and handle - they are invalid"
|
|
73 |
|
|
74 |
(index == 8) ifTrue:[^ nil].
|
|
75 |
(index == 9) ifTrue:[^ nil].
|
|
76 |
^ super instVarAt:index put:value
|
|
77 |
!
|
|
78 |
|
|
79 |
methodHome
|
|
80 |
"return the method-home - for method contexts this is the receiver"
|
|
81 |
|
|
82 |
^ self
|
|
83 |
!
|
|
84 |
|
|
85 |
home
|
|
86 |
"return the immediate home of the receiver.
|
|
87 |
for block contexts, this is the methodcontext, where the block was created,
|
|
88 |
for nested block contexts, its the surrounding blocks context.
|
|
89 |
for method-contexts this is nil."
|
|
90 |
|
|
91 |
^ nil "home"
|
|
92 |
!
|
|
93 |
|
|
94 |
method
|
|
95 |
"return the method which corresponds to the receiver"
|
|
96 |
|
|
97 |
|c|
|
|
98 |
|
|
99 |
c := self searchClass whichClassImplements:selector.
|
|
100 |
c notNil ifTrue:[
|
|
101 |
^ c compiledMethodAt:selector
|
|
102 |
].
|
|
103 |
^ nil
|
|
104 |
!
|
|
105 |
|
|
106 |
sender
|
|
107 |
"return the sender of the context"
|
|
108 |
|
|
109 |
"this special test is for the very first context (startup-context)"
|
|
110 |
(sender isNil or:[sender selector isNil]) ifTrue:[^ nil].
|
|
111 |
|
|
112 |
^ sender
|
|
113 |
!
|
|
114 |
|
|
115 |
receiver
|
|
116 |
"return the receiver of the context"
|
|
117 |
|
|
118 |
^ receiver
|
|
119 |
!
|
|
120 |
|
|
121 |
searchClass
|
|
122 |
"this is the class where the method-lookup started"
|
|
123 |
|
|
124 |
searchClass notNil ifTrue:[^ searchClass].
|
|
125 |
^ receiver class
|
|
126 |
!
|
|
127 |
|
|
128 |
selector
|
|
129 |
"return the selector of the context"
|
|
130 |
|
|
131 |
^ selector
|
|
132 |
!
|
|
133 |
|
|
134 |
nargs
|
|
135 |
"return the number of arguments to the Block/Method"
|
|
136 |
|
|
137 |
%{ /* NOCONTEXT */
|
|
138 |
|
|
139 |
RETURN ( _MKSMALLINT( (_intVal(_INST(flags)) >> __NARG_SHIFT) & __NARG_MASK) );
|
|
140 |
%}
|
|
141 |
!
|
|
142 |
|
|
143 |
nvars
|
|
144 |
"return the number of variables to the Block/Method"
|
|
145 |
|
|
146 |
%{ /* NOCONTEXT */
|
|
147 |
|
|
148 |
RETURN ( _MKSMALLINT( (_intVal(_INST(flags)) >> __NVAR_SHIFT) & __NVAR_MASK) );
|
|
149 |
%}
|
|
150 |
!
|
|
151 |
|
|
152 |
args
|
|
153 |
"return an array filled with the arguments of this context"
|
|
154 |
|
|
155 |
|newArray n "{ Class: SmallInteger }" |
|
|
156 |
|
|
157 |
n := self nargs.
|
|
158 |
newArray := Array new:n.
|
|
159 |
1 to:n do:[:index |
|
|
160 |
newArray at:index put:(self at:index)
|
|
161 |
].
|
|
162 |
^ newArray
|
|
163 |
!
|
|
164 |
|
|
165 |
argsAndVars
|
|
166 |
"return an array filled with the arguments and variables of this context"
|
|
167 |
|
|
168 |
|newArray mySize "{ Class: SmallInteger }" |
|
|
169 |
|
|
170 |
mySize := self nargs + self nvars.
|
|
171 |
newArray := Array new:mySize.
|
|
172 |
1 to:mySize do:[:index |
|
|
173 |
newArray at:index put:(self at:index)
|
|
174 |
].
|
|
175 |
^ newArray
|
|
176 |
!
|
|
177 |
|
|
178 |
lineNumber
|
|
179 |
"this returns the lineNumber within the methods source, where the context was
|
|
180 |
interrupted or called another method. (currently, sometimes this information
|
|
181 |
is not available - in this case 0 is returned)"
|
|
182 |
|
|
183 |
^ lineNr
|
|
184 |
! !
|
|
185 |
|
|
186 |
!Context methodsFor:'printing'!
|
|
187 |
|
|
188 |
argsPrintString
|
|
189 |
|fullString n "{ Class: SmallInteger }" |
|
|
190 |
|
|
191 |
fullString := ''.
|
|
192 |
n := self nargs.
|
|
193 |
1 to:n do:[:index |
|
|
194 |
fullString := fullString , (' ' , (self at:index) printString)
|
|
195 |
].
|
|
196 |
^ fullString
|
|
197 |
!
|
|
198 |
|
|
199 |
printReceiver
|
|
200 |
|implementorClass|
|
|
201 |
|
|
202 |
(receiver class == SmallInteger "isKindOf:Number") ifTrue:[
|
|
203 |
'(' print. receiver print. ') ' print
|
|
204 |
].
|
|
205 |
receiver class name print.
|
|
206 |
|
|
207 |
selector notNil ifTrue:[
|
|
208 |
implementorClass := self searchClass whichClassImplements:selector.
|
|
209 |
implementorClass notNil ifTrue: [
|
|
210 |
(implementorClass ~= receiver class) ifTrue: [
|
|
211 |
'>>>' print.
|
|
212 |
implementorClass name print
|
|
213 |
]
|
|
214 |
] ifFalse:[
|
|
215 |
'>>>**NONE**' print
|
|
216 |
]
|
|
217 |
]
|
|
218 |
!
|
|
219 |
|
|
220 |
receiverPrintString
|
|
221 |
|newString receiverClassName implementorClass|
|
|
222 |
|
|
223 |
receiverClassName := receiver class name.
|
|
224 |
(receiver class == SmallInteger) ifTrue:[
|
|
225 |
newString := '(' , receiver printString , ') ' , receiverClassName
|
|
226 |
] ifFalse:[
|
|
227 |
newString := receiverClassName
|
|
228 |
].
|
|
229 |
|
|
230 |
selector notNil ifTrue:[
|
|
231 |
implementorClass := self searchClass whichClassImplements:selector.
|
|
232 |
implementorClass notNil ifTrue: [
|
|
233 |
(implementorClass ~~ receiver class) ifTrue: [
|
|
234 |
newString := newString , '>>>',
|
|
235 |
implementorClass name printString
|
|
236 |
]
|
|
237 |
] ifFalse:[
|
|
238 |
newString := newString , '>>>**NONE**'
|
|
239 |
]
|
|
240 |
].
|
|
241 |
|
|
242 |
^ newString
|
|
243 |
!
|
|
244 |
|
|
245 |
printString
|
|
246 |
^ self receiverPrintString , ' ' , self selector printString
|
|
247 |
!
|
|
248 |
|
|
249 |
printOn:aStream
|
|
250 |
aStream nextPutAll:(self receiverPrintString).
|
|
251 |
aStream space.
|
|
252 |
self selector printOn:aStream
|
|
253 |
!
|
|
254 |
|
|
255 |
fullPrint
|
|
256 |
self printReceiver.
|
|
257 |
' ' print.
|
|
258 |
selector print.
|
|
259 |
self size ~~ 0 ifTrue: [
|
|
260 |
' ' print.
|
|
261 |
self argsPrintString print
|
|
262 |
].
|
|
263 |
' [' print. lineNr print. ']' printNewline
|
|
264 |
!
|
|
265 |
|
|
266 |
fullPrintOn:aStream
|
|
267 |
aStream nextPutAll:self receiverPrintString.
|
|
268 |
aStream space.
|
|
269 |
aStream nextPutAll:selector printString.
|
|
270 |
self size ~~ 0 ifTrue: [
|
|
271 |
aStream space.
|
|
272 |
aStream nextPutAll:self argsPrintString
|
|
273 |
]
|
|
274 |
!
|
|
275 |
|
|
276 |
debugPrint
|
|
277 |
| n "{ Class: SmallInteger }" |
|
|
278 |
|
|
279 |
'context ' print. self address printNewline.
|
|
280 |
'receiver: ' print. receiver address printNewline.
|
|
281 |
'selector: ' print. selector address printNewline.
|
|
282 |
n := self size.
|
|
283 |
n ~~ 0 ifTrue:[
|
|
284 |
1 to:n do:[:index |
|
|
285 |
'arg ' print. index print. ' : ' print.
|
|
286 |
(self at:index) address printNewline
|
|
287 |
]
|
|
288 |
].
|
|
289 |
'' printNewline
|
|
290 |
!
|
|
291 |
|
|
292 |
fullPrintString
|
|
293 |
|aString|
|
|
294 |
|
|
295 |
aString := self receiverPrintString , ' ' , selector printString.
|
|
296 |
self size ~~ 0 ifTrue: [
|
|
297 |
aString := aString , ' ' , (self argsPrintString)
|
|
298 |
].
|
|
299 |
^ aString
|
|
300 |
!
|
|
301 |
|
|
302 |
printAll
|
|
303 |
|context|
|
|
304 |
context := self.
|
|
305 |
[context notNil] whileTrue: [
|
|
306 |
context print.
|
|
307 |
context := context sender
|
|
308 |
]
|
|
309 |
!
|
|
310 |
|
|
311 |
fullPrintAll
|
|
312 |
|context|
|
|
313 |
context := self.
|
|
314 |
[context notNil] whileTrue: [
|
|
315 |
context fullPrint.
|
|
316 |
context := context sender
|
|
317 |
]
|
|
318 |
!
|
|
319 |
|
|
320 |
debugPrintAll
|
|
321 |
|context|
|
|
322 |
context := self.
|
|
323 |
[context notNil] whileTrue:[
|
|
324 |
context debugPrint.
|
|
325 |
context := context sender
|
|
326 |
]
|
|
327 |
! !
|
|
328 |
|
|
329 |
!Context methodsFor:'non local control flow'!
|
|
330 |
|
|
331 |
restart
|
|
332 |
"restart the receiver - i.e. the method is evaluated again.
|
|
333 |
if the context to restart already died - do nothing"
|
|
334 |
|
|
335 |
sender isNil ifTrue:[^ nil].
|
|
336 |
%{
|
|
337 |
__RESUMECONTEXT(SND_COMMA self, (OBJ)1);
|
|
338 |
|
|
339 |
/* when we reach here, something went wrong */
|
|
340 |
printf("restart failed\n");
|
|
341 |
%}
|
|
342 |
.
|
|
343 |
^ nil
|
|
344 |
!
|
|
345 |
|
|
346 |
resume
|
|
347 |
"resume the receiver with nil - i.e. return nil from the receiver.
|
|
348 |
if the context to resume already died - do nothing"
|
|
349 |
|
|
350 |
self resume:nil
|
|
351 |
!
|
|
352 |
|
|
353 |
resume:value
|
|
354 |
"resume the receiver - i.e. return value from the receiver.
|
|
355 |
if the context to resume already died - do nothing. No unwind
|
|
356 |
blocks are evaluated (see unwind: in this class)."
|
|
357 |
|
|
358 |
sender isNil ifTrue:[^ nil].
|
|
359 |
%{
|
|
360 |
__RESUMECONTEXT(SND_COMMA self, value);
|
|
361 |
|
|
362 |
/* when we reach here, something went wrong */
|
|
363 |
printf("resume failed\n");
|
|
364 |
%}
|
|
365 |
.
|
|
366 |
^ nil
|
|
367 |
!
|
|
368 |
|
|
369 |
unwind
|
|
370 |
"resume the receiver - i.e. return nil from the receiver.
|
|
371 |
if the context to resume already died - do nothing.
|
|
372 |
Evaluate all unwind-blocks as specified in Block>>valueNowOrOnUnwind:
|
|
373 |
and Block>>valueOnUnwindDo: on the way."
|
|
374 |
|
|
375 |
self unwind:nil
|
|
376 |
!
|
|
377 |
|
|
378 |
unwind:value
|
|
379 |
"resume the receiver - i.e. return value from the receiver.
|
|
380 |
if the context to resume already died - do nothing.
|
|
381 |
Evaluate all unwind-blocks as specified in Block>>valueNowOrOnUnwind:
|
|
382 |
and Block>>valueOnUnwindDo: on the way."
|
|
383 |
|
|
384 |
|con|
|
|
385 |
|
|
386 |
sender isNil ifTrue:[^ nil].
|
|
387 |
|
|
388 |
"start with this context, moving up"
|
|
389 |
con := thisContext.
|
|
390 |
[con ~~ self] whileTrue:[
|
|
391 |
con isBlockContext ifFalse:[
|
|
392 |
"the way we find those unwind contexts seems kludgy"
|
|
393 |
((con selector == #valueNowOrOnUnwindDo:) or:[con selector == #valueOnUnwindDo:]) ifTrue:[
|
|
394 |
"the way we evaluate the unwind blocks too"
|
|
395 |
(con args at:1) value
|
|
396 |
]
|
|
397 |
].
|
|
398 |
con := con sender
|
|
399 |
].
|
|
400 |
self resume:value
|
|
401 |
! !
|