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