1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 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 |
|
249
|
13 |
CompiledCode subclass:#Block
|
623
|
14 |
instanceVariableNames:'home nargs sourcePos initialPC'
|
|
15 |
classVariableNames:'InvalidNewSignal'
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Kernel-Methods'
|
1
|
18 |
!
|
|
19 |
|
|
20 |
!Block class methodsFor:'documentation'!
|
|
21 |
|
92
|
22 |
copyright
|
|
23 |
"
|
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
154
|
25 |
All Rights Reserved
|
92
|
26 |
|
|
27 |
This software is furnished under a license and may be used
|
|
28 |
only in accordance with the terms of that license and with the
|
|
29 |
inclusion of the above copyright notice. This software may not
|
|
30 |
be provided or otherwise made available to, or used by, any
|
|
31 |
other person. No title to or ownership of the software is
|
|
32 |
hereby transferred.
|
|
33 |
"
|
|
34 |
!
|
|
35 |
|
1
|
36 |
documentation
|
|
37 |
"
|
67
|
38 |
Blocks are pieces of executable code which can be evaluated by sending
|
|
39 |
them a value-message (''value'', ''value:'', ''value:value:'' etc).
|
1
|
40 |
|
67
|
41 |
Blocks with arguments need a message of type ''value:arg1 ... value:argn''
|
|
42 |
for evaluation; the number of arguments passed when evaluating must match
|
|
43 |
the number of arguments the block was declared with otherwise an error is
|
|
44 |
raised. Blocks without args need a ''value'' message for evaluation.
|
1
|
45 |
|
154
|
46 |
Blocks keep a reference to the context where the block was declared -
|
67
|
47 |
this allows blocks to access the methods arguments and/or variables.
|
154
|
48 |
This is still true after the method has returned - since the
|
67
|
49 |
block keeps this reference, the methods context will NOT die in this case.
|
222
|
50 |
(i.e. Blocks are closures in Smalltalk/X)
|
1
|
51 |
|
67
|
52 |
A return (via ^-statement) out of a block will force a return from the
|
|
53 |
blocks method context (if it is still living) - this make the implementation
|
|
54 |
of long-jumps and control structures possible.
|
154
|
55 |
(If the method is not alive (i.e. has already returned), a return out of the
|
|
56 |
block will trigger an error)
|
1
|
57 |
|
67
|
58 |
Long-jump is done by defining a catchBlock as ''[^ self]''
|
|
59 |
somewhere up in the calling-tree. Then, to do the long-jump from out of some
|
|
60 |
deeply nested method, simply do: ''catchBlock value''.
|
1
|
61 |
|
67
|
62 |
Instance variables:
|
1
|
63 |
|
222
|
64 |
home <Context> the context where this block was created (i.e. defined)
|
|
65 |
this may be a blockContext or a methodContext
|
|
66 |
nargs <SmallInteger> the number of arguments the block expects
|
|
67 |
sourcePos <SmallInteger> the character position of its source, in chars
|
|
68 |
relative to methods source beginning
|
|
69 |
initialPC <SmallInteger> the start position within the byteCode
|
|
70 |
for compiled blocks, this is nil.
|
|
71 |
|
|
72 |
|
|
73 |
Class variables:
|
|
74 |
|
|
75 |
InvalidNewSignal raised if a Block is tried to be created
|
|
76 |
with new (which is not allowed).
|
|
77 |
Only the VM is allowed to create Blocks.
|
1
|
78 |
|
67
|
79 |
NOTICE: layout known by runtime system and compiler - do not change
|
1
|
80 |
"
|
623
|
81 |
!
|
|
82 |
|
|
83 |
version
|
1037
|
84 |
^ '$Header: /cvs/stx/stx/libbasic/Block.st,v 1.45 1996-02-28 23:25:38 cg Exp $'
|
1
|
85 |
! !
|
|
86 |
|
623
|
87 |
!Block class methodsFor:'initialization'!
|
1
|
88 |
|
|
89 |
initialize
|
|
90 |
"setup the signals"
|
|
91 |
|
48
|
92 |
InvalidNewSignal isNil ifTrue:[
|
302
|
93 |
InvalidNewSignal := ErrorSignal newSignalMayProceed:false.
|
154
|
94 |
InvalidNewSignal nameClass:self message:#invalidNewSignal.
|
|
95 |
InvalidNewSignal notifierString:'blocks are only created by the system'.
|
48
|
96 |
]
|
1
|
97 |
! !
|
|
98 |
|
|
99 |
!Block class methodsFor:'instance creation'!
|
|
100 |
|
154
|
101 |
code:codeAddress byteCode:bCode numArgs:numArgs sourcePosition:sourcePos initialPC:initialPC literals:literals dynamic:dynamic
|
1
|
102 |
"create a new cheap (homeless) block.
|
222
|
103 |
Not for public use - this is a special hook for the compiler."
|
1
|
104 |
|
|
105 |
|newBlock|
|
|
106 |
|
11
|
107 |
newBlock := super basicNew code:codeAddress
|
154
|
108 |
byteCode:bCode
|
|
109 |
numArgs:numArgs
|
|
110 |
sourcePosition:sourcePos
|
|
111 |
initialPC:initialPC
|
|
112 |
literals:literals
|
|
113 |
dynamic:dynamic.
|
1
|
114 |
^ newBlock
|
|
115 |
!
|
|
116 |
|
22
|
117 |
new
|
1
|
118 |
"catch creation of blocks - only the system creates blocks"
|
|
119 |
|
128
|
120 |
^ InvalidNewSignal raise.
|
1
|
121 |
!
|
|
122 |
|
22
|
123 |
new:size
|
1
|
124 |
"catch creation of blocks - only the system creates blocks"
|
|
125 |
|
128
|
126 |
^ InvalidNewSignal raise.
|
1
|
127 |
! !
|
|
128 |
|
623
|
129 |
!Block class methodsFor:'queries'!
|
1
|
130 |
|
623
|
131 |
isBuiltInClass
|
|
132 |
"this class is known by the run-time-system"
|
11
|
133 |
|
1
|
134 |
^ true
|
|
135 |
! !
|
|
136 |
|
623
|
137 |
!Block methodsFor:'STV compatibility'!
|
1
|
138 |
|
623
|
139 |
on:aSignal do:exceptionBlock
|
|
140 |
"added for ST/V compatibility; evaluate the receiver,
|
|
141 |
handling aSignal. The argument, exceptionBlock is evaluated
|
|
142 |
if the signal is raised during evaluation.
|
|
143 |
Warning: no warranty, if the code below mimics ST/V's behavior
|
|
144 |
correctly - give me a note if it does not ."
|
1
|
145 |
|
623
|
146 |
aSignal handle:[:ex |
|
|
147 |
exceptionBlock value.
|
|
148 |
ex return
|
|
149 |
] do:self
|
1
|
150 |
|
623
|
151 |
"
|
|
152 |
[
|
|
153 |
1 foo
|
|
154 |
] on:MessageNotUnderstoodSignal do:[]
|
|
155 |
"
|
|
156 |
! !
|
|
157 |
|
|
158 |
!Block methodsFor:'accessing'!
|
154
|
159 |
|
|
160 |
home
|
|
161 |
"return the receivers home context (the context where it was
|
|
162 |
created). For cheap blocks, nil is returned"
|
|
163 |
|
|
164 |
^ home
|
161
|
165 |
!
|
|
166 |
|
|
167 |
method
|
|
168 |
"return the receivers method
|
|
169 |
(the method where the block was created)."
|
|
170 |
|
|
171 |
home notNil ifTrue:[
|
|
172 |
^ home method
|
|
173 |
].
|
|
174 |
^ nil
|
623
|
175 |
!
|
11
|
176 |
|
623
|
177 |
methodHome
|
|
178 |
"return the receivers method home context (the context where it was
|
|
179 |
defined). For cheap blocks, nil is returned"
|
|
180 |
|
|
181 |
home notNil ifTrue:[
|
|
182 |
^ home methodHome
|
|
183 |
].
|
|
184 |
^ home
|
11
|
185 |
!
|
|
186 |
|
623
|
187 |
numArgs
|
|
188 |
"return the number of arguments I expect for evaluation"
|
1
|
189 |
|
623
|
190 |
^ nargs
|
|
191 |
! !
|
1
|
192 |
|
623
|
193 |
!Block methodsFor:'copying'!
|
1
|
194 |
|
623
|
195 |
deepCopy
|
|
196 |
"raise an error - deepCopy is not allowed for blocks"
|
1
|
197 |
|
623
|
198 |
^ self deepCopyError
|
1
|
199 |
! !
|
|
200 |
|
|
201 |
!Block methodsFor:'error handling'!
|
|
202 |
|
328
|
203 |
invalidCodeObject
|
67
|
204 |
"this error is triggered by the interpreter when a non-Block object
|
|
205 |
is about to be executed.
|
|
206 |
In this case, the VM sends this to the bad method (the receiver).
|
|
207 |
Can only happen when the Compiler/runtime system is broken or
|
|
208 |
someone played around."
|
1
|
209 |
|
128
|
210 |
^ InvalidCodeSignal
|
154
|
211 |
raiseRequestWith:self
|
|
212 |
errorString:'invalid block - not executable'
|
623
|
213 |
!
|
|
214 |
|
|
215 |
wrongNumberOfArguments:numberGiven
|
|
216 |
"report that the number of arguments given does not match the number expected"
|
|
217 |
|
|
218 |
^ ArgumentSignal
|
|
219 |
raiseRequestWith:self
|
|
220 |
errorString:('block got ' , numberGiven printString ,
|
|
221 |
' args while ' , nargs printString , ' where expected')
|
1
|
222 |
! !
|
|
223 |
|
|
224 |
!Block methodsFor:'evaluation'!
|
|
225 |
|
|
226 |
value
|
|
227 |
"evaluate the receiver with no block args. The receiver must be a block without arguments."
|
|
228 |
|
|
229 |
%{ /* NOCONTEXT */
|
|
230 |
|
|
231 |
REGISTER OBJFUNC thecode;
|
|
232 |
OBJ home;
|
|
233 |
|
|
234 |
if (_INST(nargs) == _MKSMALLINT(0)) {
|
|
235 |
#if defined(THIS_CONTEXT)
|
328
|
236 |
if (__ISVALID_ILC_LNO(__pilc))
|
|
237 |
_ContextInstPtr(__thisContext)->c_lineno = __ILC_LNO_AS_OBJ(__pilc);
|
1
|
238 |
#endif
|
328
|
239 |
|
154
|
240 |
thecode = _BlockInstPtr(self)->b_code;
|
11
|
241 |
#ifdef NEW_BLOCK_CALL
|
154
|
242 |
if (thecode != (OBJFUNC)nil) {
|
|
243 |
/* compiled machine code */
|
|
244 |
RETURN ( (*thecode)(self, COMMA_SND) );
|
|
245 |
}
|
|
246 |
/* interpreted code */
|
328
|
247 |
# ifdef PASS_ARG_POINTER
|
400
|
248 |
RETURN ( __interpret(self, 0, nil, nil COMMA_SND, nil) );
|
328
|
249 |
# else
|
1037
|
250 |
RETURN ( __interpret(self, 0, nil, nil COMMA_SND, nil) );
|
328
|
251 |
# endif
|
11
|
252 |
#else
|
154
|
253 |
home = _BlockInstPtr(self)->b_home;
|
|
254 |
if (thecode != (OBJFUNC)nil) {
|
|
255 |
/* compiled machine code */
|
|
256 |
RETURN ( (*thecode)(home COMMA_SND) );
|
|
257 |
}
|
|
258 |
/* interpreted code */
|
328
|
259 |
# ifdef PASS_ARG_POINTER
|
400
|
260 |
RETURN ( __interpret(self, 0, nil, home COMMA_SND, nil) );
|
328
|
261 |
# else
|
1037
|
262 |
RETURN ( __interpret(self, 0, nil, home COMMA_SND, nil) );
|
328
|
263 |
# endif
|
11
|
264 |
#endif
|
1
|
265 |
}
|
|
266 |
%}
|
|
267 |
.
|
103
|
268 |
^ self wrongNumberOfArguments:0
|
1
|
269 |
!
|
|
270 |
|
|
271 |
value:arg
|
|
272 |
"evaluate the receiver with one argument. The receiver must be a 1-arg block."
|
|
273 |
|
|
274 |
%{ /* NOCONTEXT */
|
|
275 |
|
|
276 |
REGISTER OBJFUNC thecode;
|
|
277 |
OBJ home;
|
|
278 |
|
|
279 |
if (_INST(nargs) == _MKSMALLINT(1)) {
|
|
280 |
#if defined(THIS_CONTEXT)
|
328
|
281 |
if (__ISVALID_ILC_LNO(__pilc))
|
|
282 |
_ContextInstPtr(__thisContext)->c_lineno = __ILC_LNO_AS_OBJ(__pilc);
|
1
|
283 |
#endif
|
154
|
284 |
thecode = _BlockInstPtr(self)->b_code;
|
11
|
285 |
#ifdef NEW_BLOCK_CALL
|
154
|
286 |
if (thecode != (OBJFUNC)nil) {
|
|
287 |
RETURN ( (*thecode)(self COMMA_SND, arg) );
|
|
288 |
}
|
|
289 |
/* interpreted code */
|
328
|
290 |
# ifdef PASS_ARG_POINTER
|
|
291 |
RETURN ( __interpret(self, 1, nil, nil COMMA_SND, nil, &arg) );
|
|
292 |
# else
|
1037
|
293 |
RETURN ( __interpret(self, 1, nil, nil COMMA_SND, nil, arg) );
|
328
|
294 |
# endif
|
1
|
295 |
#else
|
154
|
296 |
home = _BlockInstPtr(self)->b_home;
|
|
297 |
if (thecode != (OBJFUNC)nil) {
|
|
298 |
RETURN ( (*thecode)(home COMMA_SND, arg) );
|
|
299 |
}
|
|
300 |
/* interpreted code */
|
328
|
301 |
# ifdef PASS_ARG_POINTER
|
|
302 |
RETURN ( __interpret(self, 1, nil, home COMMA_SND, nil, &arg) );
|
|
303 |
# else
|
1037
|
304 |
RETURN ( __interpret(self, 1, nil, home COMMA_SND, nil, arg) );
|
328
|
305 |
# endif
|
1
|
306 |
#endif
|
|
307 |
}
|
|
308 |
%}
|
|
309 |
.
|
103
|
310 |
^ self wrongNumberOfArguments:1
|
1
|
311 |
!
|
|
312 |
|
|
313 |
value:arg1 value:arg2
|
|
314 |
"evaluate the receiver with two arguments. The receiver must be a 2-arg block."
|
|
315 |
|
|
316 |
%{ /* NOCONTEXT */
|
|
317 |
|
|
318 |
REGISTER OBJFUNC thecode;
|
|
319 |
OBJ home;
|
|
320 |
|
|
321 |
if (_INST(nargs) == _MKSMALLINT(2)) {
|
|
322 |
#if defined(THIS_CONTEXT)
|
328
|
323 |
if (__ISVALID_ILC_LNO(__pilc))
|
|
324 |
_ContextInstPtr(__thisContext)->c_lineno = __ILC_LNO_AS_OBJ(__pilc);
|
1
|
325 |
#endif
|
154
|
326 |
thecode = _BlockInstPtr(self)->b_code;
|
11
|
327 |
#ifdef NEW_BLOCK_CALL
|
154
|
328 |
if (thecode != (OBJFUNC)nil) {
|
|
329 |
RETURN ( (*thecode)(self COMMA_SND, arg1, arg2) );
|
|
330 |
}
|
328
|
331 |
# ifdef PASS_ARG_POINTER
|
|
332 |
RETURN ( __interpret(self, 2, nil, nil COMMA_SND, nil, &arg1) );
|
|
333 |
# else
|
1037
|
334 |
RETURN ( __interpret(self, 2, nil, nil COMMA_SND, nil, arg1, arg2) );
|
328
|
335 |
# endif
|
1
|
336 |
#else
|
154
|
337 |
home = _BlockInstPtr(self)->b_home;
|
|
338 |
if (thecode != (OBJFUNC)nil) {
|
|
339 |
RETURN ( (*thecode)(home COMMA_SND, arg1, arg2) );
|
|
340 |
}
|
328
|
341 |
# ifdef PASS_ARG_POINTER
|
|
342 |
RETURN ( __interpret(self, 2, nil, home COMMA_SND, nil, &arg1) );
|
|
343 |
# else
|
1037
|
344 |
RETURN ( __interpret(self, 2, nil, home COMMA_SND, nil, arg1, arg2) );
|
328
|
345 |
# endif
|
1
|
346 |
#endif
|
|
347 |
}
|
|
348 |
%}
|
|
349 |
.
|
103
|
350 |
^ self wrongNumberOfArguments:2
|
1
|
351 |
!
|
|
352 |
|
|
353 |
value:arg1 value:arg2 value:arg3
|
|
354 |
"evaluate the receiver with three arguments. The receiver must be a 3-arg block."
|
|
355 |
|
|
356 |
%{ /* NOCONTEXT */
|
|
357 |
|
|
358 |
REGISTER OBJFUNC thecode;
|
|
359 |
OBJ home;
|
|
360 |
|
|
361 |
if (_INST(nargs) == _MKSMALLINT(3)) {
|
|
362 |
#if defined(THIS_CONTEXT)
|
328
|
363 |
if (__ISVALID_ILC_LNO(__pilc))
|
|
364 |
_ContextInstPtr(__thisContext)->c_lineno = __ILC_LNO_AS_OBJ(__pilc);
|
1
|
365 |
#endif
|
154
|
366 |
thecode = _BlockInstPtr(self)->b_code;
|
11
|
367 |
#ifdef NEW_BLOCK_CALL
|
154
|
368 |
if (thecode != (OBJFUNC)nil) {
|
|
369 |
RETURN ( (*thecode)(self COMMA_SND, arg1, arg2, arg3) );
|
|
370 |
}
|
328
|
371 |
# ifdef PASS_ARG_POINTER
|
|
372 |
RETURN ( __interpret(self, 3, nil, nil COMMA_SND, nil, &arg1) );
|
|
373 |
# else
|
1037
|
374 |
RETURN ( __interpret(self, 3, nil, nil COMMA_SND, nil, arg1, arg2, arg3) );
|
328
|
375 |
# endif
|
1
|
376 |
#else
|
154
|
377 |
home = _BlockInstPtr(self)->b_home;
|
|
378 |
if (thecode != (OBJFUNC)nil) {
|
|
379 |
RETURN ( (*thecode)(home COMMA_SND, arg1, arg2, arg3) );
|
|
380 |
}
|
328
|
381 |
# ifdef PASS_ARG_POINTER
|
|
382 |
RETURN ( __interpret(self, 3, nil, home COMMA_SND, nil, &arg1) );
|
|
383 |
# else
|
1037
|
384 |
RETURN ( __interpret(self, 3, nil, home COMMA_SND, nil, arg1, arg2, arg3) );
|
328
|
385 |
# endif
|
1
|
386 |
#endif
|
|
387 |
}
|
|
388 |
%}
|
|
389 |
.
|
103
|
390 |
^ self wrongNumberOfArguments:3
|
1
|
391 |
!
|
|
392 |
|
|
393 |
value:arg1 value:arg2 value:arg3 value:arg4
|
|
394 |
"evaluate the receiver with four arguments. The receiver must be a 4-arg block."
|
|
395 |
|
|
396 |
%{ /* NOCONTEXT */
|
|
397 |
|
|
398 |
REGISTER OBJFUNC thecode;
|
|
399 |
OBJ home;
|
|
400 |
|
|
401 |
if (_INST(nargs) == _MKSMALLINT(4)) {
|
|
402 |
#if defined(THIS_CONTEXT)
|
328
|
403 |
if (__ISVALID_ILC_LNO(__pilc))
|
|
404 |
_ContextInstPtr(__thisContext)->c_lineno = __ILC_LNO_AS_OBJ(__pilc);
|
1
|
405 |
#endif
|
154
|
406 |
thecode = _BlockInstPtr(self)->b_code;
|
11
|
407 |
#ifdef NEW_BLOCK_CALL
|
154
|
408 |
if (thecode != (OBJFUNC)nil) {
|
|
409 |
RETURN ( (*thecode)(self COMMA_SND, arg1, arg2, arg3, arg4) );
|
|
410 |
}
|
328
|
411 |
# ifdef PASS_ARG_POINTER
|
|
412 |
RETURN ( __interpret(self, 4, nil, nil COMMA_SND, nil, &arg1) );
|
|
413 |
# else
|
1037
|
414 |
RETURN ( __interpret(self, 4, nil, nil COMMA_SND, nil, arg1, arg2, arg3, arg4) );
|
328
|
415 |
# endif
|
1
|
416 |
#else
|
154
|
417 |
home = _BlockInstPtr(self)->b_home;
|
|
418 |
if (thecode != (OBJFUNC)nil) {
|
|
419 |
RETURN ( (*thecode)(home COMMA_SND, arg1, arg2, arg3, arg4) );
|
|
420 |
}
|
328
|
421 |
# ifdef PASS_ARG_POINTER
|
|
422 |
RETURN ( __interpret(self, 4, nil, home COMMA_SND, nil, &arg1) );
|
|
423 |
# else
|
1037
|
424 |
RETURN ( __interpret(self, 4, nil, home COMMA_SND, nil, arg1, arg2, arg3, arg4) );
|
328
|
425 |
# endif
|
1
|
426 |
#endif
|
|
427 |
}
|
|
428 |
%}
|
|
429 |
.
|
103
|
430 |
^ self wrongNumberOfArguments:4
|
1
|
431 |
!
|
|
432 |
|
|
433 |
value:arg1 value:arg2 value:arg3 value:arg4 value:arg5
|
|
434 |
"evaluate the receiver with four arguments. The receiver must be a 5-arg block."
|
|
435 |
|
|
436 |
%{ /* NOCONTEXT */
|
|
437 |
|
|
438 |
REGISTER OBJFUNC thecode;
|
|
439 |
OBJ home;
|
|
440 |
|
|
441 |
if (_INST(nargs) == _MKSMALLINT(5)) {
|
|
442 |
#if defined(THIS_CONTEXT)
|
328
|
443 |
if (__ISVALID_ILC_LNO(__pilc))
|
|
444 |
_ContextInstPtr(__thisContext)->c_lineno = __ILC_LNO_AS_OBJ(__pilc);
|
1
|
445 |
#endif
|
154
|
446 |
thecode = _BlockInstPtr(self)->b_code;
|
11
|
447 |
#ifdef NEW_BLOCK_CALL
|
154
|
448 |
if (thecode != (OBJFUNC)nil) {
|
|
449 |
RETURN ( (*thecode)(self COMMA_SND, arg1, arg2, arg3, arg4, arg5) );
|
|
450 |
}
|
328
|
451 |
# ifdef PASS_ARG_POINTER
|
|
452 |
RETURN ( __interpret(self, 5, nil, nil COMMA_SND, nil, &arg1) );
|
|
453 |
# else
|
1037
|
454 |
RETURN ( __interpret(self, 5, nil, nil COMMA_SND, nil, arg1, arg2, arg3, arg4, arg5) );
|
328
|
455 |
# endif
|
1
|
456 |
#else
|
154
|
457 |
home = _BlockInstPtr(self)->b_home;
|
|
458 |
if (thecode != (OBJFUNC)nil) {
|
|
459 |
RETURN ( (*thecode)(home COMMA_SND, arg1, arg2, arg3, arg4, arg5) );
|
|
460 |
}
|
328
|
461 |
# ifdef PASS_ARG_POINTER
|
|
462 |
RETURN ( __interpret(self, 5, nil, home COMMA_SND, nil, &arg1) );
|
|
463 |
# else
|
1037
|
464 |
RETURN ( __interpret(self, 5, nil, home COMMA_SND, nil, arg1, arg2, arg3, arg4, arg5) );
|
328
|
465 |
# endif
|
1
|
466 |
#endif
|
|
467 |
}
|
|
468 |
%}
|
|
469 |
.
|
103
|
470 |
^ self wrongNumberOfArguments:5
|
1
|
471 |
!
|
|
472 |
|
|
473 |
value:arg1 value:arg2 value:arg3 value:arg4 value:arg5 value:arg6
|
|
474 |
"evaluate the receiver with four arguments. The receiver must be a 6-arg block."
|
|
475 |
|
|
476 |
%{ /* NOCONTEXT */
|
|
477 |
|
|
478 |
REGISTER OBJFUNC thecode;
|
|
479 |
OBJ home;
|
|
480 |
|
|
481 |
if (_INST(nargs) == _MKSMALLINT(6)) {
|
|
482 |
#if defined(THIS_CONTEXT)
|
328
|
483 |
if (__ISVALID_ILC_LNO(__pilc))
|
|
484 |
_ContextInstPtr(__thisContext)->c_lineno = __ILC_LNO_AS_OBJ(__pilc);
|
1
|
485 |
#endif
|
154
|
486 |
thecode = _BlockInstPtr(self)->b_code;
|
11
|
487 |
#ifdef NEW_BLOCK_CALL
|
154
|
488 |
if (thecode != (OBJFUNC)nil) {
|
|
489 |
RETURN ( (*thecode)(self COMMA_SND, arg1, arg2, arg3, arg4, arg5, arg6) );
|
|
490 |
}
|
328
|
491 |
# ifdef PASS_ARG_POINTER
|
|
492 |
RETURN ( __interpret(self, 6, nil, nil COMMA_SND, nil, &arg1) );
|
|
493 |
# else
|
1037
|
494 |
RETURN ( __interpret(self, 6, nil, nil COMMA_SND, nil, arg1, arg2, arg3, arg4, arg5, arg6) );
|
328
|
495 |
# endif
|
1
|
496 |
#else
|
154
|
497 |
home = _BlockInstPtr(self)->b_home;
|
|
498 |
if (thecode != (OBJFUNC)nil) {
|
|
499 |
RETURN ( (*thecode)(home COMMA_SND, arg1, arg2, arg3, arg4, arg5, arg6) );
|
|
500 |
}
|
328
|
501 |
# ifdef PASS_ARG_POINTER
|
|
502 |
RETURN ( __interpret(self, 6, nil, home COMMA_SND, nil, &arg1) );
|
|
503 |
# else
|
1037
|
504 |
RETURN ( __interpret(self, 6, nil, home COMMA_SND, nil, arg1, arg2, arg3, arg4, arg5, arg6) );
|
328
|
505 |
# endif
|
1
|
506 |
#endif
|
|
507 |
}
|
|
508 |
%}
|
|
509 |
.
|
103
|
510 |
^ self wrongNumberOfArguments:6
|
1
|
511 |
!
|
|
512 |
|
|
513 |
valueWithArguments:argArray
|
|
514 |
"evaluate the receiver with arguments taken from argArray.
|
313
|
515 |
ArgArray must be either an Array or nil.
|
1
|
516 |
The size of the argArray must match the number of arguments the receiver expects."
|
|
517 |
|
1037
|
518 |
|a1 a2 a3 a4 a5 a6 a7 a8|
|
1
|
519 |
|
319
|
520 |
(argArray notNil and:[argArray class ~~ Array]) ifTrue:[
|
154
|
521 |
^ self badArgumentArry
|
1
|
522 |
].
|
|
523 |
(argArray size == nargs) ifFalse:[
|
154
|
524 |
^ self wrongNumberOfArguments:(argArray size)
|
1
|
525 |
].
|
|
526 |
%{
|
|
527 |
|
|
528 |
REGISTER OBJFUNC thecode;
|
|
529 |
OBJ home;
|
67
|
530 |
REGISTER OBJ *ap;
|
328
|
531 |
int nargs;
|
1
|
532 |
|
|
533 |
#if defined(THIS_CONTEXT)
|
328
|
534 |
if (__ISVALID_ILC_LNO(__pilc))
|
|
535 |
_ContextInstPtr(__thisContext)->c_lineno = __ILC_LNO_AS_OBJ(__pilc);
|
1
|
536 |
#endif
|
1037
|
537 |
thecode = _BlockInstPtr(self)->b_code;
|
|
538 |
|
|
539 |
#ifndef NEW_BLOCK_CALL
|
|
540 |
home = _BlockInstPtr(self)->b_home;
|
|
541 |
if (thecode != (OBJFUNC)nil) {
|
|
542 |
switch (nargs = _intVal(_INST(nargs))) {
|
|
543 |
default:
|
|
544 |
goto error;
|
|
545 |
case 8:
|
|
546 |
ap = _ArrayInstPtr(argArray)->a_element;
|
|
547 |
RETURN ( (*thecode)(home COMMA_SND, ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6], ap[7]) );
|
|
548 |
case 7:
|
|
549 |
ap = _ArrayInstPtr(argArray)->a_element;
|
|
550 |
RETURN ( (*thecode)(home COMMA_SND, ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6]) );
|
|
551 |
case 6:
|
|
552 |
ap = _ArrayInstPtr(argArray)->a_element;
|
|
553 |
RETURN ( (*thecode)(home COMMA_SND, ap[0], ap[1], ap[2], ap[3], ap[4], ap[5]) );
|
|
554 |
case 5:
|
|
555 |
ap = _ArrayInstPtr(argArray)->a_element;
|
|
556 |
RETURN ( (*thecode)(home COMMA_SND, ap[0], ap[1], ap[2], ap[3], ap[4]) );
|
|
557 |
case 4:
|
|
558 |
ap = _ArrayInstPtr(argArray)->a_element;
|
|
559 |
RETURN ( (*thecode)(home COMMA_SND, ap[0], ap[1], ap[2], ap[3]) );
|
|
560 |
case 3:
|
|
561 |
ap = _ArrayInstPtr(argArray)->a_element;
|
|
562 |
RETURN ( (*thecode)(home COMMA_SND, ap[0], ap[1], ap[2]) );
|
|
563 |
case 2:
|
|
564 |
ap = _ArrayInstPtr(argArray)->a_element;
|
|
565 |
RETURN ( (*thecode)(home COMMA_SND, ap[0], ap[1]) );
|
|
566 |
case 1:
|
|
567 |
RETURN ( (*thecode)(home COMMA_SND, _ArrayInstPtr(argArray)->a_element[0]) );
|
|
568 |
case 0:
|
|
569 |
RETURN ( (*thecode)(home COMMA_SND) );
|
|
570 |
break;
|
|
571 |
}
|
|
572 |
}
|
|
573 |
#endif
|
|
574 |
|
328
|
575 |
switch (nargs = _intVal(_INST(nargs))) {
|
154
|
576 |
default:
|
|
577 |
goto error;
|
1037
|
578 |
case 8:
|
|
579 |
a8 = _ArrayInstPtr(argArray)->a_element[7];
|
154
|
580 |
case 7:
|
|
581 |
a7 = _ArrayInstPtr(argArray)->a_element[6];
|
|
582 |
case 6:
|
|
583 |
a6 = _ArrayInstPtr(argArray)->a_element[5];
|
|
584 |
case 5:
|
|
585 |
a5 = _ArrayInstPtr(argArray)->a_element[4];
|
|
586 |
case 4:
|
|
587 |
a4 = _ArrayInstPtr(argArray)->a_element[3];
|
|
588 |
case 3:
|
|
589 |
a3 = _ArrayInstPtr(argArray)->a_element[2];
|
|
590 |
case 2:
|
|
591 |
a2 = _ArrayInstPtr(argArray)->a_element[1];
|
|
592 |
case 1:
|
|
593 |
a1 = _ArrayInstPtr(argArray)->a_element[0];
|
|
594 |
case 0:
|
|
595 |
break;
|
1
|
596 |
}
|
11
|
597 |
#ifdef NEW_BLOCK_CALL
|
1
|
598 |
if (thecode != (OBJFUNC)nil) {
|
1037
|
599 |
RETURN ( (*thecode)(self COMMA_SND, a1, a2, a3, a4, a5, a6, a7, a8) );
|
11
|
600 |
}
|
328
|
601 |
# ifdef PASS_ARG_POINTER
|
|
602 |
RETURN ( __interpret(self, nargs, nil, nil COMMA_SND, nil, &a1) );
|
|
603 |
# else
|
1037
|
604 |
RETURN ( __interpret(self, nargs, nil, nil COMMA_SND, nil, a1, a2, a3, a4, a5, a6, a7, a8) );
|
328
|
605 |
# endif
|
1037
|
606 |
|
1
|
607 |
#else
|
1037
|
608 |
|
328
|
609 |
# ifdef PASS_ARG_POINTER
|
|
610 |
RETURN ( __interpret(self, nargs, nil, home COMMA_SND, nil, &a1) );
|
|
611 |
# else
|
1037
|
612 |
RETURN ( __interpret(self, nargs, nil, home COMMA_SND, nil, a1, a2, a3, a4, a5, a6, a7, a8) );
|
328
|
613 |
# endif
|
1037
|
614 |
|
1
|
615 |
#endif
|
67
|
616 |
|
11
|
617 |
error: ;
|
1
|
618 |
%}
|
11
|
619 |
.
|
138
|
620 |
"
|
|
621 |
the above code only supports up-to 7 arguments
|
|
622 |
"
|
128
|
623 |
^ ArgumentSignal
|
154
|
624 |
raiseRequestWith:self
|
|
625 |
errorString:'only blocks with up-to 7 arguments supported'
|
|
626 |
! !
|
|
627 |
|
623
|
628 |
!Block methodsFor:'looping'!
|
325
|
629 |
|
623
|
630 |
doUntil:aBlock
|
|
631 |
"repeat the receiver block until aBlock evaluates to true.
|
|
632 |
The receiver is evaluated at least once.
|
|
633 |
This is the same as '... doWhile:[... not]' "
|
325
|
634 |
|
623
|
635 |
"this implementation is for purists ... :-)"
|
|
636 |
|
|
637 |
self value.
|
|
638 |
aBlock value ifTrue:[^ nil].
|
|
639 |
thisContext restart
|
325
|
640 |
|
|
641 |
"
|
623
|
642 |
|n|
|
154
|
643 |
|
623
|
644 |
n := 1.
|
|
645 |
[n printNewline] doUntil:[ (n := n + 1) > 5 ]
|
154
|
646 |
"
|
1
|
647 |
!
|
|
648 |
|
623
|
649 |
doWhile:aBlock
|
|
650 |
"repeat the receiver block until aBlock evaluates to false.
|
|
651 |
The receiver is evaluated at least once."
|
67
|
652 |
|
154
|
653 |
"this implementation is for purists ... :-)"
|
|
654 |
|
623
|
655 |
self value.
|
|
656 |
aBlock value ifFalse:[^ nil].
|
|
657 |
thisContext restart
|
|
658 |
|
|
659 |
"
|
|
660 |
|n|
|
|
661 |
|
|
662 |
n := 1.
|
|
663 |
[n printNewline] doWhile:[ (n := n + 1) <= 5 ]
|
|
664 |
"
|
|
665 |
!
|
|
666 |
|
|
667 |
loop
|
|
668 |
"repeat the receiver forever (should contain a return somewhere).
|
|
669 |
Inspired by a corresponding Self method."
|
|
670 |
|
|
671 |
self value.
|
67
|
672 |
thisContext restart
|
|
673 |
|
|
674 |
"
|
|
675 |
|n|
|
|
676 |
|
|
677 |
n := 1.
|
623
|
678 |
[
|
154
|
679 |
n printNewline.
|
623
|
680 |
n >= 10 ifTrue:[^ nil].
|
154
|
681 |
n := n + 1
|
623
|
682 |
] loop
|
|
683 |
"
|
|
684 |
!
|
|
685 |
|
|
686 |
loopWithExit
|
|
687 |
"the receiver must be a block of one argument. It is evaluated in a loop forever,
|
|
688 |
and is passed a block, which, if sent a value:-message, will exit the receiver block,
|
|
689 |
returning the parameter of the value:-message. Used for loops with exit in the middle.
|
|
690 |
Inspired by a corresponding Self method."
|
|
691 |
|
|
692 |
|exitBlock|
|
|
693 |
|
|
694 |
exitBlock := [:exitValue | ^ exitValue].
|
|
695 |
[true] whileTrue:[self value:exitBlock]
|
|
696 |
|
|
697 |
"
|
|
698 |
|i|
|
|
699 |
i := 1.
|
|
700 |
[:exit |
|
|
701 |
Transcript showCr:i.
|
|
702 |
i == 5 ifTrue:[exit value:'thats it'].
|
|
703 |
i := i + 1
|
|
704 |
] loopWithExit
|
|
705 |
"
|
|
706 |
!
|
|
707 |
|
|
708 |
repeat
|
|
709 |
"repeat the receiver forever - same as loop, for ST-80 compatibility"
|
|
710 |
|
|
711 |
self value.
|
|
712 |
thisContext restart
|
|
713 |
!
|
|
714 |
|
|
715 |
valueWithExit
|
|
716 |
"the receiver must be a block of one argument. It is evaluated, and is passed a block,
|
|
717 |
which, if sent a value:-message, will exit the receiver block, returning the parameter of the
|
|
718 |
value:-message. Used for premature returns to the caller.
|
|
719 |
Taken from a manchester goody (a similar construct also appears in Self)."
|
|
720 |
|
|
721 |
^ self value: [:exitValue | ^exitValue]
|
|
722 |
|
|
723 |
"
|
|
724 |
[:exit |
|
|
725 |
1 to:10 do:[:i |
|
|
726 |
Transcript showCr:i.
|
|
727 |
i == 5 ifTrue:[exit value:'thats it']
|
|
728 |
].
|
|
729 |
'regular block-value; never returned'
|
|
730 |
] valueWithExit
|
|
731 |
"
|
|
732 |
!
|
|
733 |
|
|
734 |
whileFalse
|
|
735 |
"evaluate the receiver while it evaluates to false (ST80 compatibility)"
|
|
736 |
|
|
737 |
"this implementation is for purists ... :-)"
|
|
738 |
|
|
739 |
self value ifTrue:[^ nil].
|
|
740 |
thisContext restart
|
|
741 |
|
|
742 |
"
|
|
743 |
|n|
|
|
744 |
|
|
745 |
n := 1.
|
|
746 |
[n printNewline. (n := n + 1) > 10] whileFalse
|
67
|
747 |
"
|
|
748 |
!
|
1
|
749 |
|
67
|
750 |
whileFalse:aBlock
|
|
751 |
"evaluate the argument, aBlock while the receiver evaluates to false.
|
|
752 |
- usually open coded by compilers, but needed here for #perform
|
|
753 |
and expression evaluation."
|
|
754 |
|
154
|
755 |
"this implementation is for purists ... :-)"
|
|
756 |
|
67
|
757 |
self value ifTrue:[^ nil].
|
|
758 |
aBlock value.
|
|
759 |
thisContext restart
|
|
760 |
|
|
761 |
"
|
|
762 |
|n|
|
|
763 |
|
|
764 |
n := 1.
|
|
765 |
[n > 10] whileFalse:[
|
154
|
766 |
n printNewline.
|
|
767 |
n := n + 1
|
67
|
768 |
]
|
|
769 |
"
|
1
|
770 |
!
|
|
771 |
|
|
772 |
whileTrue
|
67
|
773 |
"evaluate the receiver while it evaluates to true (ST80 compatibility)"
|
1
|
774 |
|
154
|
775 |
"this implementation is for purists ... :-)"
|
|
776 |
|
67
|
777 |
self value ifFalse:[^ nil].
|
|
778 |
thisContext restart
|
1
|
779 |
|
67
|
780 |
"
|
|
781 |
|n|
|
1
|
782 |
|
67
|
783 |
n := 1.
|
|
784 |
[n printNewline. (n := n + 1) <= 10] whileTrue
|
|
785 |
"
|
1
|
786 |
!
|
|
787 |
|
623
|
788 |
whileTrue:aBlock
|
|
789 |
"evaluate the argument, aBlock while the receiver evaluates to true.
|
|
790 |
- usually open coded by compilers, but needed here for #perform
|
|
791 |
and expression evaluation."
|
1
|
792 |
|
154
|
793 |
"this implementation is for purists ... :-)"
|
|
794 |
|
623
|
795 |
self value ifFalse:[^ nil].
|
|
796 |
aBlock value.
|
67
|
797 |
thisContext restart
|
|
798 |
|
|
799 |
"
|
|
800 |
|n|
|
1
|
801 |
|
67
|
802 |
n := 1.
|
623
|
803 |
[n <= 10] whileTrue:[
|
154
|
804 |
n printNewline.
|
|
805 |
n := n + 1
|
623
|
806 |
]
|
68
|
807 |
"
|
67
|
808 |
! !
|
|
809 |
|
92
|
810 |
!Block methodsFor:'printing & storing'!
|
1
|
811 |
|
|
812 |
printOn:aStream
|
44
|
813 |
"append a a printed representation of the block to aStream"
|
|
814 |
|
213
|
815 |
|homeClass h sel methodClass|
|
54
|
816 |
|
|
817 |
"cheap blocks have no home context, but a method instead"
|
|
818 |
|
|
819 |
(home isNil or:[home isContext not]) ifTrue:[
|
154
|
820 |
aStream nextPutAll:'[] in '.
|
1
|
821 |
|
154
|
822 |
"
|
|
823 |
currently, some cheap blocks don't know where they have been created
|
|
824 |
"
|
|
825 |
aStream nextPutAll:' ??? (optimized)'.
|
|
826 |
^ self
|
54
|
827 |
].
|
|
828 |
|
|
829 |
"a full blown block (with home, but without method)"
|
|
830 |
|
|
831 |
aStream nextPutAll:'[] in '.
|
154
|
832 |
h := self methodHome.
|
|
833 |
sel := h selector.
|
|
834 |
"/ old:
|
|
835 |
"/ home receiver class name printOn:aStream.
|
|
836 |
"/ new:
|
212
|
837 |
"/ (h searchClass whichClassImplements:sel) name printOn:aStream.
|
213
|
838 |
methodClass := h methodClass.
|
|
839 |
methodClass isNil ifTrue:[
|
|
840 |
'UnboundMethod' printOn:aStream.
|
|
841 |
] ifFalse:[
|
|
842 |
methodClass name printOn:aStream.
|
|
843 |
].
|
54
|
844 |
aStream nextPut:$-.
|
154
|
845 |
sel printOn:aStream.
|
54
|
846 |
|
|
847 |
"
|
1
|
848 |
aStream nextPutAll:'[] in '.
|
|
849 |
homeClass := home containingClass.
|
|
850 |
homeClass notNil ifTrue:[
|
154
|
851 |
homeClass name printOn:aStream.
|
|
852 |
aStream space.
|
|
853 |
(homeClass selectorForMethod:home) printOn:aStream
|
1
|
854 |
] ifFalse:[
|
154
|
855 |
aStream nextPutAll:' ???'
|
1
|
856 |
]
|
54
|
857 |
"
|
1
|
858 |
! !
|
623
|
859 |
|
|
860 |
!Block methodsFor:'private accessing'!
|
|
861 |
|
|
862 |
code:codeAddress byteCode:bCode numArgs:numArgs sourcePosition:srcPos initialPC:iPC literals:lits dynamic:dynamic
|
|
863 |
"set all relevant internals"
|
|
864 |
|
|
865 |
self code:codeAddress.
|
|
866 |
byteCode := bCode.
|
|
867 |
nargs := numArgs.
|
|
868 |
sourcePos := srcPos.
|
|
869 |
initialPC := iPC.
|
|
870 |
literals := lits.
|
|
871 |
self dynamic:dynamic
|
|
872 |
!
|
|
873 |
|
|
874 |
initialPC:initial
|
|
875 |
"set the initial pc for evaluation - DANGER ALERT"
|
|
876 |
|
|
877 |
initialPC := initial
|
|
878 |
!
|
|
879 |
|
|
880 |
numArgs:numArgs
|
|
881 |
"set the number of arguments I expect for evaluation - DANGER ALERT"
|
|
882 |
|
|
883 |
nargs := numArgs
|
|
884 |
!
|
|
885 |
|
|
886 |
sourcePosition:position
|
|
887 |
"set the position of the source within my method"
|
|
888 |
|
|
889 |
sourcePos := position
|
|
890 |
! !
|
|
891 |
|
|
892 |
!Block methodsFor:'privileged evaluation'!
|
|
893 |
|
|
894 |
valueUninterruptably
|
|
895 |
"evaluate the receiver with interrupts blocked.
|
|
896 |
This does not prevent preemption by a higher priority processes
|
|
897 |
if any becomes runnable due to the evaluation of the receiver
|
|
898 |
(i.e. if a semaphore is signalled)."
|
|
899 |
|
864
|
900 |
|wasBlocked|
|
|
901 |
|
|
902 |
wasBlocked := OperatingSystem blockInterrupts.
|
|
903 |
^ self valueNowOrOnUnwindDo:[wasBlocked ifTrue:[OperatingSystem unblockInterrupts]].
|
623
|
904 |
!
|
|
905 |
|
|
906 |
valueUnpreemptively
|
|
907 |
"evaluate the receiver without the possiblity of preemption
|
|
908 |
(i.e. at a very high priority)"
|
|
909 |
|
|
910 |
|oldPrio activeProcess|
|
|
911 |
|
|
912 |
activeProcess := Processor activeProcess.
|
|
913 |
oldPrio := activeProcess changePriority:(Processor highestPriority).
|
864
|
914 |
^ self valueNowOrOnUnwindDo:[
|
623
|
915 |
activeProcess priority:oldPrio
|
|
916 |
]
|
|
917 |
! !
|
|
918 |
|
|
919 |
!Block methodsFor:'process creation'!
|
|
920 |
|
|
921 |
fork
|
|
922 |
"create a new process executing the receiver at the current priority."
|
|
923 |
|
|
924 |
^ self newProcess resume
|
|
925 |
!
|
|
926 |
|
|
927 |
forkAt:priority
|
|
928 |
"create a new process executing the receiver at a different priority."
|
|
929 |
|
|
930 |
^ (self newProcess priority:priority) resume
|
|
931 |
!
|
|
932 |
|
|
933 |
forkWith:argArray
|
|
934 |
"create a new process executing the receiver,
|
|
935 |
passing elements in argArray as arguments to the receiver block."
|
|
936 |
|
|
937 |
^ [self valueWithArguments:argArray] fork.
|
|
938 |
!
|
|
939 |
|
|
940 |
newProcess
|
|
941 |
"create a new (unscheduled) process executing the receiver"
|
|
942 |
|
|
943 |
^ Process for:self priority:(Processor activePriority)
|
|
944 |
!
|
|
945 |
|
|
946 |
newProcessWithArguments:argArray
|
|
947 |
"create a new (unscheduled) process executing the receiver,
|
|
948 |
passing the elements in argArray as arguments to the receiver block."
|
|
949 |
|
|
950 |
^ [self valueWithArguments:argArray] newProcess
|
|
951 |
!
|
|
952 |
|
|
953 |
promise
|
|
954 |
"create a promise on the receiver. The promise will evaluate the
|
|
955 |
receiver and promise to return the value with the #value message.
|
|
956 |
The evaluation will be performed as a separate process.
|
|
957 |
Asking the promise for its value will either block the asking process
|
|
958 |
(if the evaluation has not yet been finished) or return the value
|
|
959 |
immediately."
|
|
960 |
|
|
961 |
^ Promise value:self
|
|
962 |
|
|
963 |
"
|
|
964 |
|p|
|
|
965 |
|
|
966 |
p := [1000 factorial] promise.
|
|
967 |
'do something else ...'.
|
|
968 |
p value
|
|
969 |
"
|
|
970 |
!
|
|
971 |
|
|
972 |
promiseAt:prio
|
|
973 |
"create a promise on the receiver. The promise will evaluate the
|
|
974 |
receiver and promise to return the value with the #value message.
|
|
975 |
The evaluation will be performed as a separate process running at prio.
|
|
976 |
Asking the promise for its value will either block the asking process
|
|
977 |
(if the evaluation has not yet been finished) or return the value
|
|
978 |
immediately."
|
|
979 |
|
|
980 |
^ Promise value:self priority:prio
|
|
981 |
! !
|
|
982 |
|
|
983 |
!Block methodsFor:'testing'!
|
|
984 |
|
|
985 |
isBlock
|
|
986 |
"return true, if this is a block - yes I am"
|
|
987 |
|
|
988 |
^ true
|
|
989 |
! !
|
|
990 |
|
|
991 |
!Block methodsFor:'unwinding'!
|
|
992 |
|
|
993 |
valueNowOrOnUnwindDo:aBlock
|
|
994 |
"evaluate the receiver - after that, or when some method sent within unwinds (i.e. does
|
|
995 |
a long return), evaluate the argument, aBlock.
|
|
996 |
This is used to make certain that cleanup actions (for example closing files etc.) are
|
|
997 |
executed regardless of error actions"
|
|
998 |
|
|
999 |
|v|
|
|
1000 |
|
|
1001 |
thisContext markForUnwind.
|
|
1002 |
v := self value. "the real logic is in Context>>unwind"
|
|
1003 |
aBlock value.
|
|
1004 |
^ v
|
|
1005 |
|
|
1006 |
"
|
|
1007 |
in the following example, f will be closed even if the block
|
|
1008 |
returns with 'oops'. There are many more applications of this kind
|
|
1009 |
found in the system.
|
|
1010 |
"
|
|
1011 |
"
|
|
1012 |
|f|
|
|
1013 |
|
|
1014 |
f := 'Makefile' asFilename readStream.
|
|
1015 |
[
|
|
1016 |
l := f nextLine.
|
|
1017 |
l isNil ifTrue:[^ 'oops']
|
|
1018 |
] valueNowOrOnUnwindDo:[
|
|
1019 |
f close
|
|
1020 |
]
|
|
1021 |
"
|
|
1022 |
!
|
|
1023 |
|
|
1024 |
valueOnUnwindDo:aBlock
|
|
1025 |
"evaluate the receiver - when some method sent within unwinds (i.e. does
|
|
1026 |
a long return), evaluate the argument, aBlock.
|
|
1027 |
This is used to make certain that cleanup actions (for example closing files etc.) are
|
|
1028 |
executed regardless of error actions"
|
|
1029 |
|
|
1030 |
thisContext markForUnwind.
|
|
1031 |
^ self value "the real logic is in Context>>unwind"
|
|
1032 |
! !
|
|
1033 |
|
|
1034 |
Block initialize!
|