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