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