author | Claus Gittinger <cg@exept.de> |
Sat, 22 Mar 1997 14:14:21 +0100 | |
changeset 2481 | bb9bc51ce5f6 |
parent 2301 | e3976a1e863a |
child 2542 | 555749f035f4 |
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 |
||
1773
442d1b73ecb9
no longer allow Blocks with a dynamic-bit to be created
Claus Gittinger <cg@exept.de>
parents:
1672
diff
changeset
|
20 |
!Block class methodsFor:'documentation'! |
1 | 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 |
|
1265 | 41 |
In smalltalk, Blocks provide the basic (and heavily used) mechanism |
42 |
for looping, enumerating collection elements, visitors, exception |
|
43 |
handling, unwinding, delayed execution and processes. |
|
44 |
||
1254 | 45 |
Blocks are never created explicitely; the only creation |
46 |
is done by the compilers, when some sourceCode is compiled to either |
|
47 |
machine or byteCode. |
|
48 |
||
67 | 49 |
Blocks with arguments need a message of type ''value:arg1 ... value:argn'' |
50 |
for evaluation; the number of arguments passed when evaluating must match |
|
51 |
the number of arguments the block was declared with otherwise an error is |
|
52 |
raised. Blocks without args need a ''value'' message for evaluation. |
|
1 | 53 |
|
154 | 54 |
Blocks keep a reference to the context where the block was declared - |
67 | 55 |
this allows blocks to access the methods arguments and/or variables. |
154 | 56 |
This is still true after the method has returned - since the |
67 | 57 |
block keeps this reference, the methods context will NOT die in this case. |
222 | 58 |
(i.e. Blocks are closures in Smalltalk/X) |
1 | 59 |
|
67 | 60 |
A return (via ^-statement) out of a block will force a return from the |
61 |
blocks method context (if it is still living) - this make the implementation |
|
62 |
of long-jumps and control structures possible. |
|
154 | 63 |
(If the method is not alive (i.e. has already returned), a return out of the |
64 |
block will trigger an error) |
|
1 | 65 |
|
67 | 66 |
Long-jump is done by defining a catchBlock as ''[^ self]'' |
67 |
somewhere up in the calling-tree. Then, to do the long-jump from out of some |
|
68 |
deeply nested method, simply do: ''catchBlock value''. |
|
1 | 69 |
|
1293 | 70 |
[Instance variables:] |
1 | 71 |
|
222 | 72 |
home <Context> the context where this block was created (i.e. defined) |
2238 | 73 |
this may be a blockContext or a methodContext |
222 | 74 |
nargs <SmallInteger> the number of arguments the block expects |
75 |
sourcePos <SmallInteger> the character position of its source, in chars |
|
2238 | 76 |
relative to methods source beginning |
222 | 77 |
initialPC <SmallInteger> the start position within the byteCode |
2238 | 78 |
for compiled blocks, this is nil. |
222 | 79 |
|
80 |
||
1293 | 81 |
[Class variables:] |
222 | 82 |
|
83 |
InvalidNewSignal raised if a Block is tried to be created |
|
2238 | 84 |
with new (which is not allowed). |
85 |
Only the VM is allowed to create Blocks. |
|
1 | 86 |
|
1293 | 87 |
|
67 | 88 |
NOTICE: layout known by runtime system and compiler - do not change |
1293 | 89 |
|
90 |
[author:] |
|
2238 | 91 |
Claus Gittinger |
1293 | 92 |
|
93 |
[see also:] |
|
2238 | 94 |
Process Context |
95 |
Collection |
|
96 |
( contexts. blocks & unwinding : programming/contexts.html) |
|
1 | 97 |
" |
1265 | 98 |
! |
99 |
||
100 |
examples |
|
101 |
" |
|
1293 | 102 |
define a block and evaluate it: |
2238 | 103 |
[exBegin] |
104 |
|b| |
|
1265 | 105 |
|
2238 | 106 |
b := [ Transcript showCR:'hello' ]. |
1293 | 107 |
|
2238 | 108 |
Transcript showCR:'now evaluating the block ...'. |
109 |
b value. |
|
110 |
[exEnd] |
|
1265 | 111 |
|
112 |
||
1293 | 113 |
|
1265 | 114 |
even here, blocks are involved: |
115 |
(although, the compiler optimizes things if possible) |
|
2238 | 116 |
[exBegin] |
117 |
Transcript showCR:'now evaluating one of two blocks ...'. |
|
118 |
1 > 4 ifTrue:[ |
|
119 |
Transcript showCR:'foo' |
|
120 |
] ifFalse:[ |
|
121 |
Transcript showCR:'bar' |
|
122 |
] |
|
123 |
[exEnd] |
|
1265 | 124 |
|
125 |
||
1293 | 126 |
|
1265 | 127 |
here things become obvious: |
2238 | 128 |
[exBegin] |
129 |
|yesBlock noBlock| |
|
1265 | 130 |
|
2238 | 131 |
yesBlock := [ Transcript showCR:'foo' ]. |
132 |
noBlock := [ Transcript showCR:'bar' ]. |
|
1265 | 133 |
|
2238 | 134 |
Transcript showCR:'now evaluating one of two blocks ...'. |
135 |
1 > 4 ifTrue:yesBlock |
|
136 |
ifFalse:noBlock |
|
137 |
[exEnd] |
|
1265 | 138 |
|
139 |
||
1293 | 140 |
|
1265 | 141 |
simple loops: |
142 |
not very objectOriented: |
|
2238 | 143 |
[exBegin] |
144 |
|i| |
|
1265 | 145 |
|
2238 | 146 |
i := 1. |
147 |
[i < 10] whileTrue:[ |
|
148 |
Transcript showCR:i. |
|
149 |
i := i + 1 |
|
150 |
] |
|
151 |
[exEnd] |
|
1265 | 152 |
|
153 |
||
154 |
using integer protocol: |
|
2238 | 155 |
[exBegin] |
156 |
1 to:10 do:[:i | |
|
157 |
Transcript showCR:i. |
|
158 |
] |
|
159 |
[exEnd] |
|
1265 | 160 |
|
161 |
||
162 |
interval protocol: |
|
2238 | 163 |
[exBegin] |
164 |
(1 to:10) do:[:i | |
|
165 |
Transcript showCR:i. |
|
166 |
] |
|
167 |
[exEnd] |
|
1265 | 168 |
|
169 |
||
1293 | 170 |
|
1265 | 171 |
looping over collections: |
172 |
||
1293 | 173 |
bad code: |
174 |
(only works with numeric-indexable collections) |
|
2238 | 175 |
[exBegin] |
176 |
|i coll| |
|
1265 | 177 |
|
2238 | 178 |
coll := #(9 8 7 6 5). |
179 |
i := 1. |
|
180 |
[i <= coll size] whileTrue:[ |
|
181 |
Transcript showCR:(coll at:i). |
|
182 |
i := i + 1. |
|
183 |
] |
|
184 |
[exEnd] |
|
1265 | 185 |
|
186 |
||
187 |
||
1293 | 188 |
just as bad (well, marginally better ;-): |
189 |
(only works with numeric-indexable collections) |
|
2238 | 190 |
[exBegin] |
191 |
|coll| |
|
1265 | 192 |
|
2238 | 193 |
coll := #(9 8 7 6 5). |
194 |
1 to:coll size do:[:i | |
|
195 |
Transcript showCR:(coll at:i). |
|
196 |
] |
|
197 |
[exEnd] |
|
1265 | 198 |
|
199 |
||
200 |
||
1293 | 201 |
the smalltalk way: |
202 |
(works with any collection) |
|
2238 | 203 |
[exBegin] |
204 |
|coll| |
|
1265 | 205 |
|
2238 | 206 |
coll := #(9 8 7 6 5). |
207 |
coll do:[:element | |
|
208 |
Transcript showCR:element. |
|
209 |
] |
|
210 |
[exEnd] |
|
1265 | 211 |
|
212 |
Rule: use enumeration protocol of the collection instead of |
|
2238 | 213 |
manually indexing it. [with few exceptions] |
1265 | 214 |
|
215 |
||
216 |
||
217 |
processes: |
|
218 |
||
219 |
forking a lightweight process (thread): |
|
2238 | 220 |
[exBegin] |
221 |
[ |
|
222 |
Transcript showCR:'waiting ...'. |
|
223 |
Delay waitForSeconds:2. |
|
224 |
Transcript showCR:'here I am'. |
|
225 |
] fork |
|
226 |
[exEnd] |
|
1265 | 227 |
|
228 |
||
229 |
||
230 |
some with low prio: |
|
2238 | 231 |
[exBegin] |
232 |
[ |
|
233 |
Transcript showCR:'computing ...'. |
|
234 |
10000 factorial. |
|
235 |
Transcript showCR:'here I am'. |
|
236 |
] forkAt:(Processor userBackgroundPriority) |
|
237 |
[exEnd] |
|
1265 | 238 |
|
239 |
||
1293 | 240 |
|
241 |
handling exceptions: |
|
2238 | 242 |
[exBegin] |
243 |
Object errorSignal handle:[:ex | |
|
244 |
Transcript showCR:'exception handler forces return'. |
|
245 |
ex return |
|
246 |
] do:[ |
|
247 |
Transcript showCR:'now, doing something bad ...'. |
|
248 |
1 / 0. |
|
249 |
Transcript showCR:'not reached' |
|
250 |
] |
|
251 |
[exEnd] |
|
1265 | 252 |
|
253 |
||
1293 | 254 |
|
255 |
performing cleanup actions: |
|
2238 | 256 |
[exBegin] |
257 |
Object errorSignal handle:[:ex | |
|
258 |
Transcript showCR:'exception handler forces return'. |
|
259 |
ex return |
|
260 |
] do:[ |
|
261 |
[ |
|
262 |
Transcript showCR:'doing something bad ...'. |
|
263 |
1 / 0. |
|
264 |
Transcript showCR:'not reached' |
|
265 |
] valueOnUnwindDo:[ |
|
266 |
Transcript showCR:'cleanup' |
|
267 |
] |
|
268 |
] |
|
269 |
[exEnd] |
|
1265 | 270 |
|
271 |
||
1293 | 272 |
delayed execution (visitor pattern): |
273 |
(looking carefully into the example, |
|
274 |
C/C++ programmers may raise their eyes ;-) |
|
2238 | 275 |
[exBegin] |
276 |
|showBlock countBlock |
|
277 |
howMany |
|
278 |
top panel b1 b2| |
|
1265 | 279 |
|
2238 | 280 |
howMany := 0. |
1265 | 281 |
|
2238 | 282 |
showBlock := [ Transcript showCR:howMany ]. |
283 |
countBlock := [ howMany := howMany + 1 ]. |
|
1265 | 284 |
|
2238 | 285 |
top := StandardSystemView extent:200@200. |
286 |
panel := HorizontalPanelView origin:0.0@0.0 corner:1.0@1.0 in:top. |
|
1265 | 287 |
|
2238 | 288 |
b1 := Button label:'count up' in:panel. |
289 |
b1 action:countBlock. |
|
1265 | 290 |
|
2238 | 291 |
b2 := Button label:'show value' in:panel. |
292 |
b2 action:showBlock. |
|
1265 | 293 |
|
2238 | 294 |
top open. |
1265 | 295 |
|
2238 | 296 |
Transcript showCR:'new process started;'. |
297 |
Transcript showCR:'notice: the blocks can still access the'. |
|
298 |
Transcript showCR:' howMany local variable.'. |
|
299 |
[exEnd] |
|
1265 | 300 |
" |
1 | 301 |
! ! |
302 |
||
1773
442d1b73ecb9
no longer allow Blocks with a dynamic-bit to be created
Claus Gittinger <cg@exept.de>
parents:
1672
diff
changeset
|
303 |
!Block class methodsFor:'initialization'! |
1 | 304 |
|
305 |
initialize |
|
1254 | 306 |
"create signals raised by various errors" |
1 | 307 |
|
48 | 308 |
InvalidNewSignal isNil ifTrue:[ |
2238 | 309 |
InvalidNewSignal := ErrorSignal newSignalMayProceed:false. |
310 |
InvalidNewSignal nameClass:self message:#invalidNewSignal. |
|
311 |
InvalidNewSignal notifierString:'blocks are only created by the system'. |
|
48 | 312 |
] |
1254 | 313 |
|
314 |
"Modified: 22.4.1996 / 16:34:20 / cg" |
|
1 | 315 |
! ! |
316 |
||
1773
442d1b73ecb9
no longer allow Blocks with a dynamic-bit to be created
Claus Gittinger <cg@exept.de>
parents:
1672
diff
changeset
|
317 |
!Block class methodsFor:'instance creation'! |
1 | 318 |
|
1773
442d1b73ecb9
no longer allow Blocks with a dynamic-bit to be created
Claus Gittinger <cg@exept.de>
parents:
1672
diff
changeset
|
319 |
byteCode:bCode numArgs:numArgs sourcePosition:sourcePos initialPC:initialPC literals:literals |
1 | 320 |
"create a new cheap (homeless) block. |
222 | 321 |
Not for public use - this is a special hook for the compiler." |
1 | 322 |
|
323 |
|newBlock| |
|
324 |
||
1493
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
325 |
newBlock := (super basicNew:(literals size)) |
2238 | 326 |
byteCode:bCode |
327 |
numArgs:numArgs |
|
328 |
sourcePosition:sourcePos |
|
329 |
initialPC:initialPC |
|
330 |
literals:literals. |
|
1 | 331 |
^ newBlock |
1493
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
332 |
|
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
333 |
"Modified: 24.6.1996 / 12:36:48 / stefan" |
1773
442d1b73ecb9
no longer allow Blocks with a dynamic-bit to be created
Claus Gittinger <cg@exept.de>
parents:
1672
diff
changeset
|
334 |
"Created: 17.10.1996 / 14:04:17 / cg" |
1 | 335 |
! |
336 |
||
22 | 337 |
new |
1 | 338 |
"catch creation of blocks - only the system creates blocks" |
339 |
||
128 | 340 |
^ InvalidNewSignal raise. |
1 | 341 |
! |
342 |
||
22 | 343 |
new:size |
1 | 344 |
"catch creation of blocks - only the system creates blocks" |
345 |
||
128 | 346 |
^ InvalidNewSignal raise. |
1 | 347 |
! ! |
348 |
||
1773
442d1b73ecb9
no longer allow Blocks with a dynamic-bit to be created
Claus Gittinger <cg@exept.de>
parents:
1672
diff
changeset
|
349 |
!Block class methodsFor:'queries'! |
1 | 350 |
|
623 | 351 |
isBuiltInClass |
1264 | 352 |
"return true if this class is known by the run-time-system. |
353 |
Here, true is returned for myself, false for subclasses." |
|
11 | 354 |
|
1181
6637fee79d7b
only Block & CheapBlock are fixed - subclasses may look different
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
355 |
^ self == Block |
6637fee79d7b
only Block & CheapBlock are fixed - subclasses may look different
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
356 |
|
1264 | 357 |
"Modified: 23.4.1996 / 15:55:58 / cg" |
1626 | 358 |
! |
359 |
||
360 |
maxNumberOfArguments |
|
361 |
"return the maximum number of arguments a block can have. |
|
362 |
This is a limit in the VM, which may be removed in one of |
|
363 |
the next versions ..." |
|
364 |
||
365 |
%{ /* NOCONTEXT */ |
|
366 |
RETURN (__MKSMALLINT(MAX_METHOD_ARGS)); |
|
367 |
%} |
|
1 | 368 |
! ! |
369 |
||
1979 | 370 |
!Block methodsFor:'Compatibility - ST/V'! |
1 | 371 |
|
623 | 372 |
on:aSignal do:exceptionBlock |
373 |
"added for ST/V compatibility; evaluate the receiver, |
|
374 |
handling aSignal. The argument, exceptionBlock is evaluated |
|
375 |
if the signal is raised during evaluation. |
|
376 |
Warning: no warranty, if the code below mimics ST/V's behavior |
|
377 |
correctly - give me a note if it does not ." |
|
1 | 378 |
|
623 | 379 |
aSignal handle:[:ex | |
380 |
exceptionBlock value. |
|
381 |
ex return |
|
382 |
] do:self |
|
1 | 383 |
|
623 | 384 |
" |
385 |
[ |
|
386 |
1 foo |
|
387 |
] on:MessageNotUnderstoodSignal do:[] |
|
388 |
" |
|
389 |
! ! |
|
390 |
||
1979 | 391 |
!Block methodsFor:'Compatibility - V''Age'! |
392 |
||
393 |
argumentCount |
|
394 |
"VisualAge compatibility: alias for #numArgs. |
|
395 |
return the number of arguments I expect for evaluation" |
|
396 |
||
397 |
^ nargs |
|
398 |
||
399 |
"Created: 15.11.1996 / 11:22:02 / cg" |
|
400 |
! |
|
401 |
||
402 |
valueOnReturnDo:aBlock |
|
403 |
"VisualAge compatibility: alias for #valueOnUnwindDo: |
|
404 |
evaluate the receiver - when some method sent within unwinds |
|
405 |
(i.e. does a long return), evaluate the argument, aBlock. |
|
406 |
This is used to make certain that cleanup actions |
|
407 |
(for example closing files etc.) are executed regardless of error actions. |
|
408 |
||
409 |
Q: is this the exact semantics of V'Ages method ? |
|
2238 | 410 |
the documentation is unclean; it could be a valueNowOrOnUnwindDo: ..." |
1979 | 411 |
|
412 |
^ self valueOnUnwindDo:aBlock |
|
413 |
||
414 |
"Created: 15.11.1996 / 11:38:37 / cg" |
|
415 |
! ! |
|
416 |
||
623 | 417 |
!Block methodsFor:'accessing'! |
154 | 418 |
|
419 |
home |
|
420 |
"return the receivers home context (the context where it was |
|
421 |
created). For cheap blocks, nil is returned" |
|
422 |
||
423 |
^ home |
|
161 | 424 |
! |
425 |
||
426 |
method |
|
427 |
"return the receivers method |
|
428 |
(the method where the block was created)." |
|
429 |
||
430 |
home notNil ifTrue:[ |
|
431 |
^ home method |
|
432 |
]. |
|
433 |
^ nil |
|
623 | 434 |
! |
11 | 435 |
|
623 | 436 |
methodHome |
437 |
"return the receivers method home context (the context where it was |
|
438 |
defined). For cheap blocks, nil is returned" |
|
439 |
||
440 |
home notNil ifTrue:[ |
|
441 |
^ home methodHome |
|
442 |
]. |
|
443 |
^ home |
|
11 | 444 |
! |
445 |
||
623 | 446 |
numArgs |
447 |
"return the number of arguments I expect for evaluation" |
|
1 | 448 |
|
623 | 449 |
^ nargs |
450 |
! ! |
|
1 | 451 |
|
2237
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
452 |
!Block methodsFor:'conversion'! |
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
453 |
|
2241 | 454 |
beVarArg |
2237
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
455 |
"convert myself into a varArg block; |
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
456 |
this one has 1 formal argument, which gets the list |
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
457 |
of actual arguments when evaluated." |
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
458 |
|
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
459 |
nargs ~~ 1 ifTrue:[ |
2241 | 460 |
self error:'vararg blocks must take exactly 1 argument - the arg list'. |
461 |
^ nil |
|
2237
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
462 |
]. |
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
463 |
|
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
464 |
self changeClassTo:VarArgBlock. |
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
465 |
^ self |
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
466 |
|
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
467 |
" |
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
468 |
|b| |
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
469 |
|
2241 | 470 |
b := [:argList | argList printCR] beVarArg. |
2237
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
471 |
b value. |
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
472 |
b value:'arg1' value:'arg2' value:'arg3' value:'arg4' |
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
473 |
" |
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
474 |
|
2241 | 475 |
"Created: 23.1.1997 / 13:35:28 / cg" |
476 |
"Modified: 23.1.1997 / 13:35:48 / cg" |
|
2237
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
477 |
! ! |
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
478 |
|
623 | 479 |
!Block methodsFor:'copying'! |
1 | 480 |
|
623 | 481 |
deepCopy |
482 |
"raise an error - deepCopy is not allowed for blocks" |
|
1 | 483 |
|
623 | 484 |
^ self deepCopyError |
1 | 485 |
! ! |
486 |
||
487 |
!Block methodsFor:'error handling'! |
|
488 |
||
328 | 489 |
invalidCodeObject |
1874 | 490 |
"{ Pragma: +optSpace }" |
491 |
||
67 | 492 |
"this error is triggered by the interpreter when a non-Block object |
493 |
is about to be executed. |
|
494 |
In this case, the VM sends this to the bad method (the receiver). |
|
495 |
Can only happen when the Compiler/runtime system is broken or |
|
496 |
someone played around." |
|
1 | 497 |
|
128 | 498 |
^ InvalidCodeSignal |
2238 | 499 |
raiseRequestWith:self |
500 |
errorString:'invalid block - not executable' |
|
1874 | 501 |
|
502 |
"Modified: 4.11.1996 / 22:46:39 / cg" |
|
623 | 503 |
! |
504 |
||
505 |
wrongNumberOfArguments:numberGiven |
|
1874 | 506 |
"{ Pragma: +optSpace }" |
507 |
||
623 | 508 |
"report that the number of arguments given does not match the number expected" |
509 |
||
510 |
^ ArgumentSignal |
|
2238 | 511 |
raiseRequestWith:self |
512 |
errorString:('block got ' , numberGiven printString , |
|
513 |
' args while ' , nargs printString , ' where expected') |
|
1874 | 514 |
|
515 |
"Modified: 4.11.1996 / 22:46:43 / cg" |
|
1 | 516 |
! ! |
517 |
||
518 |
!Block methodsFor:'evaluation'! |
|
519 |
||
520 |
value |
|
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
521 |
"evaluate the receiver with no block args. |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
522 |
The receiver must be a block without arguments." |
1 | 523 |
|
524 |
%{ /* NOCONTEXT */ |
|
525 |
||
526 |
REGISTER OBJFUNC thecode; |
|
527 |
OBJ home; |
|
528 |
||
1133 | 529 |
if (__INST(nargs) == __MKSMALLINT(0)) { |
1 | 530 |
#if defined(THIS_CONTEXT) |
328 | 531 |
if (__ISVALID_ILC_LNO(__pilc)) |
1133 | 532 |
__ContextInstPtr(__thisContext)->c_lineno = __ILC_LNO_AS_OBJ(__pilc); |
1 | 533 |
#endif |
328 | 534 |
|
1133 | 535 |
thecode = __BlockInstPtr(self)->b_code; |
11 | 536 |
#ifdef NEW_BLOCK_CALL |
154 | 537 |
if (thecode != (OBJFUNC)nil) { |
538 |
/* compiled machine code */ |
|
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
539 |
RETURN ( (*thecode)(self) ); |
154 | 540 |
} |
541 |
/* interpreted code */ |
|
328 | 542 |
# ifdef PASS_ARG_POINTER |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
543 |
RETURN ( __interpret(self, 0, nil, nil, nil, nil) ); |
328 | 544 |
# else |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
545 |
RETURN ( __interpret(self, 0, nil, nil, nil, nil) ); |
328 | 546 |
# endif |
11 | 547 |
#else |
1133 | 548 |
home = __BlockInstPtr(self)->b_home; |
154 | 549 |
if (thecode != (OBJFUNC)nil) { |
550 |
/* compiled machine code */ |
|
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
551 |
RETURN ( (*thecode)(home) ); |
154 | 552 |
} |
553 |
/* interpreted code */ |
|
328 | 554 |
# ifdef PASS_ARG_POINTER |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
555 |
RETURN ( __interpret(self, 0, nil, home, nil, nil) ); |
328 | 556 |
# else |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
557 |
RETURN ( __interpret(self, 0, nil, home, nil, nil) ); |
328 | 558 |
# endif |
11 | 559 |
#endif |
1 | 560 |
} |
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
561 |
%}. |
103 | 562 |
^ self wrongNumberOfArguments:0 |
1 | 563 |
! |
564 |
||
565 |
value:arg |
|
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
566 |
"evaluate the receiver with one argument. |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
567 |
The receiver must be a 1-arg block." |
1 | 568 |
|
569 |
%{ /* NOCONTEXT */ |
|
570 |
||
571 |
REGISTER OBJFUNC thecode; |
|
572 |
OBJ home; |
|
573 |
||
1133 | 574 |
if (__INST(nargs) == __MKSMALLINT(1)) { |
1 | 575 |
#if defined(THIS_CONTEXT) |
328 | 576 |
if (__ISVALID_ILC_LNO(__pilc)) |
1133 | 577 |
__ContextInstPtr(__thisContext)->c_lineno = __ILC_LNO_AS_OBJ(__pilc); |
1 | 578 |
#endif |
1133 | 579 |
thecode = __BlockInstPtr(self)->b_code; |
11 | 580 |
#ifdef NEW_BLOCK_CALL |
154 | 581 |
if (thecode != (OBJFUNC)nil) { |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
582 |
RETURN ( (*thecode)(self, arg) ); |
154 | 583 |
} |
584 |
/* interpreted code */ |
|
328 | 585 |
# ifdef PASS_ARG_POINTER |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
586 |
RETURN ( __interpret(self, 1, nil, nil, nil, nil, &arg) ); |
328 | 587 |
# else |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
588 |
RETURN ( __interpret(self, 1, nil, nil, nil, nil, arg) ); |
328 | 589 |
# endif |
1 | 590 |
#else |
1133 | 591 |
home = __BlockInstPtr(self)->b_home; |
154 | 592 |
if (thecode != (OBJFUNC)nil) { |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
593 |
RETURN ( (*thecode)(home, arg) ); |
154 | 594 |
} |
595 |
/* interpreted code */ |
|
328 | 596 |
# ifdef PASS_ARG_POINTER |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
597 |
RETURN ( __interpret(self, 1, nil, home, nil, nil, &arg) ); |
328 | 598 |
# else |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
599 |
RETURN ( __interpret(self, 1, nil, home, nil, nil, arg) ); |
328 | 600 |
# endif |
1 | 601 |
#endif |
602 |
} |
|
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
603 |
%}. |
103 | 604 |
^ self wrongNumberOfArguments:1 |
1 | 605 |
! |
606 |
||
607 |
value:arg1 value:arg2 |
|
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
608 |
"evaluate the receiver with two arguments. |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
609 |
The receiver must be a 2-arg block." |
1 | 610 |
|
611 |
%{ /* NOCONTEXT */ |
|
612 |
||
613 |
REGISTER OBJFUNC thecode; |
|
614 |
OBJ home; |
|
615 |
||
1133 | 616 |
if (__INST(nargs) == __MKSMALLINT(2)) { |
1 | 617 |
#if defined(THIS_CONTEXT) |
328 | 618 |
if (__ISVALID_ILC_LNO(__pilc)) |
1133 | 619 |
__ContextInstPtr(__thisContext)->c_lineno = __ILC_LNO_AS_OBJ(__pilc); |
1 | 620 |
#endif |
1133 | 621 |
thecode = __BlockInstPtr(self)->b_code; |
11 | 622 |
#ifdef NEW_BLOCK_CALL |
154 | 623 |
if (thecode != (OBJFUNC)nil) { |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
624 |
RETURN ( (*thecode)(self, arg1, arg2) ); |
154 | 625 |
} |
328 | 626 |
# ifdef PASS_ARG_POINTER |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
627 |
RETURN ( __interpret(self, 2, nil, nil, nil, nil, &arg1) ); |
328 | 628 |
# else |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
629 |
RETURN ( __interpret(self, 2, nil, nil, nil, nil, arg1, arg2) ); |
328 | 630 |
# endif |
1 | 631 |
#else |
1133 | 632 |
home = __BlockInstPtr(self)->b_home; |
154 | 633 |
if (thecode != (OBJFUNC)nil) { |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
634 |
RETURN ( (*thecode)(home, arg1, arg2) ); |
154 | 635 |
} |
328 | 636 |
# ifdef PASS_ARG_POINTER |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
637 |
RETURN ( __interpret(self, 2, nil, home, nil, nil, &arg1) ); |
328 | 638 |
# else |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
639 |
RETURN ( __interpret(self, 2, nil, home, nil, nil, arg1, arg2) ); |
328 | 640 |
# endif |
1 | 641 |
#endif |
642 |
} |
|
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
643 |
%}. |
103 | 644 |
^ self wrongNumberOfArguments:2 |
1 | 645 |
! |
646 |
||
647 |
value:arg1 value:arg2 value:arg3 |
|
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
648 |
"evaluate the receiver with three arguments. |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
649 |
The receiver must be a 3-arg block." |
1 | 650 |
|
651 |
%{ /* NOCONTEXT */ |
|
652 |
||
653 |
REGISTER OBJFUNC thecode; |
|
654 |
OBJ home; |
|
655 |
||
1133 | 656 |
if (__INST(nargs) == __MKSMALLINT(3)) { |
1 | 657 |
#if defined(THIS_CONTEXT) |
328 | 658 |
if (__ISVALID_ILC_LNO(__pilc)) |
1133 | 659 |
__ContextInstPtr(__thisContext)->c_lineno = __ILC_LNO_AS_OBJ(__pilc); |
1 | 660 |
#endif |
1133 | 661 |
thecode = __BlockInstPtr(self)->b_code; |
11 | 662 |
#ifdef NEW_BLOCK_CALL |
154 | 663 |
if (thecode != (OBJFUNC)nil) { |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
664 |
RETURN ( (*thecode)(self, arg1, arg2, arg3) ); |
154 | 665 |
} |
328 | 666 |
# ifdef PASS_ARG_POINTER |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
667 |
RETURN ( __interpret(self, 3, nil, nil, nil, nil, &arg1) ); |
328 | 668 |
# else |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
669 |
RETURN ( __interpret(self, 3, nil, nil, nil, nil, arg1, arg2, arg3) ); |
328 | 670 |
# endif |
1 | 671 |
#else |
1133 | 672 |
home = __BlockInstPtr(self)->b_home; |
154 | 673 |
if (thecode != (OBJFUNC)nil) { |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
674 |
RETURN ( (*thecode)(home, arg1, arg2, arg3) ); |
154 | 675 |
} |
328 | 676 |
# ifdef PASS_ARG_POINTER |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
677 |
RETURN ( __interpret(self, 3, nil, home, nil, nil, &arg1) ); |
328 | 678 |
# else |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
679 |
RETURN ( __interpret(self, 3, nil, home, nil, nil, arg1, arg2, arg3) ); |
328 | 680 |
# endif |
1 | 681 |
#endif |
682 |
} |
|
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
683 |
%}. |
103 | 684 |
^ self wrongNumberOfArguments:3 |
1 | 685 |
! |
686 |
||
687 |
value:arg1 value:arg2 value:arg3 value:arg4 |
|
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
688 |
"evaluate the receiver with four arguments. |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
689 |
The receiver must be a 4-arg block." |
1 | 690 |
|
691 |
%{ /* NOCONTEXT */ |
|
692 |
||
693 |
REGISTER OBJFUNC thecode; |
|
694 |
OBJ home; |
|
695 |
||
1133 | 696 |
if (__INST(nargs) == __MKSMALLINT(4)) { |
1 | 697 |
#if defined(THIS_CONTEXT) |
328 | 698 |
if (__ISVALID_ILC_LNO(__pilc)) |
1133 | 699 |
__ContextInstPtr(__thisContext)->c_lineno = __ILC_LNO_AS_OBJ(__pilc); |
1 | 700 |
#endif |
1133 | 701 |
thecode = __BlockInstPtr(self)->b_code; |
11 | 702 |
#ifdef NEW_BLOCK_CALL |
154 | 703 |
if (thecode != (OBJFUNC)nil) { |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
704 |
RETURN ( (*thecode)(self, arg1, arg2, arg3, arg4) ); |
154 | 705 |
} |
328 | 706 |
# ifdef PASS_ARG_POINTER |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
707 |
RETURN ( __interpret(self, 4, nil, nil, nil, nil, &arg1) ); |
328 | 708 |
# else |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
709 |
RETURN ( __interpret(self, 4, nil, nil, nil, nil, arg1, arg2, arg3, arg4) ); |
328 | 710 |
# endif |
1 | 711 |
#else |
1133 | 712 |
home = __BlockInstPtr(self)->b_home; |
154 | 713 |
if (thecode != (OBJFUNC)nil) { |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
714 |
RETURN ( (*thecode)(home, arg1, arg2, arg3, arg4) ); |
154 | 715 |
} |
328 | 716 |
# ifdef PASS_ARG_POINTER |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
717 |
RETURN ( __interpret(self, 4, nil, home, nil, nil, &arg1) ); |
328 | 718 |
# else |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
719 |
RETURN ( __interpret(self, 4, nil, home, nil, nil, arg1, arg2, arg3, arg4) ); |
328 | 720 |
# endif |
1 | 721 |
#endif |
722 |
} |
|
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
723 |
%}. |
103 | 724 |
^ self wrongNumberOfArguments:4 |
1 | 725 |
! |
726 |
||
727 |
value:arg1 value:arg2 value:arg3 value:arg4 value:arg5 |
|
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
728 |
"evaluate the receiver with five arguments. |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
729 |
The receiver must be a 5-arg block." |
1 | 730 |
|
731 |
%{ /* NOCONTEXT */ |
|
732 |
||
733 |
REGISTER OBJFUNC thecode; |
|
734 |
OBJ home; |
|
735 |
||
1133 | 736 |
if (__INST(nargs) == __MKSMALLINT(5)) { |
1 | 737 |
#if defined(THIS_CONTEXT) |
328 | 738 |
if (__ISVALID_ILC_LNO(__pilc)) |
1133 | 739 |
__ContextInstPtr(__thisContext)->c_lineno = __ILC_LNO_AS_OBJ(__pilc); |
1 | 740 |
#endif |
1133 | 741 |
thecode = __BlockInstPtr(self)->b_code; |
11 | 742 |
#ifdef NEW_BLOCK_CALL |
154 | 743 |
if (thecode != (OBJFUNC)nil) { |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
744 |
RETURN ( (*thecode)(self, arg1, arg2, arg3, arg4, arg5) ); |
154 | 745 |
} |
328 | 746 |
# ifdef PASS_ARG_POINTER |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
747 |
RETURN ( __interpret(self, 5, nil, nil, nil, nil, &arg1) ); |
328 | 748 |
# else |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
749 |
RETURN ( __interpret(self, 5, nil, nil, nil, nil, arg1, arg2, arg3, arg4, arg5) ); |
328 | 750 |
# endif |
1 | 751 |
#else |
1133 | 752 |
home = __BlockInstPtr(self)->b_home; |
154 | 753 |
if (thecode != (OBJFUNC)nil) { |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
754 |
RETURN ( (*thecode)(home, arg1, arg2, arg3, arg4, arg5) ); |
154 | 755 |
} |
328 | 756 |
# ifdef PASS_ARG_POINTER |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
757 |
RETURN ( __interpret(self, 5, nil, home, nil, nil, &arg1) ); |
328 | 758 |
# else |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
759 |
RETURN ( __interpret(self, 5, nil, home, nil, nil, arg1, arg2, arg3, arg4, arg5) ); |
328 | 760 |
# endif |
1 | 761 |
#endif |
762 |
} |
|
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
763 |
%}. |
103 | 764 |
^ self wrongNumberOfArguments:5 |
1 | 765 |
! |
766 |
||
767 |
value:arg1 value:arg2 value:arg3 value:arg4 value:arg5 value:arg6 |
|
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
768 |
"evaluate the receiver with six arguments. |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
769 |
The receiver must be a 6-arg block." |
1 | 770 |
|
771 |
%{ /* NOCONTEXT */ |
|
772 |
||
773 |
REGISTER OBJFUNC thecode; |
|
774 |
OBJ home; |
|
775 |
||
1133 | 776 |
if (__INST(nargs) == __MKSMALLINT(6)) { |
1 | 777 |
#if defined(THIS_CONTEXT) |
328 | 778 |
if (__ISVALID_ILC_LNO(__pilc)) |
1133 | 779 |
__ContextInstPtr(__thisContext)->c_lineno = __ILC_LNO_AS_OBJ(__pilc); |
1 | 780 |
#endif |
1133 | 781 |
thecode = __BlockInstPtr(self)->b_code; |
11 | 782 |
#ifdef NEW_BLOCK_CALL |
154 | 783 |
if (thecode != (OBJFUNC)nil) { |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
784 |
RETURN ( (*thecode)(self, arg1, arg2, arg3, arg4, arg5, arg6) ); |
154 | 785 |
} |
328 | 786 |
# ifdef PASS_ARG_POINTER |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
787 |
RETURN ( __interpret(self, 6, nil, nil, nil, nil, &arg1) ); |
328 | 788 |
# else |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
789 |
RETURN ( __interpret(self, 6, nil, nil, nil, nil, arg1, arg2, arg3, arg4, arg5, arg6) ); |
328 | 790 |
# endif |
1 | 791 |
#else |
1133 | 792 |
home = __BlockInstPtr(self)->b_home; |
154 | 793 |
if (thecode != (OBJFUNC)nil) { |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
794 |
RETURN ( (*thecode)(home, arg1, arg2, arg3, arg4, arg5, arg6) ); |
154 | 795 |
} |
328 | 796 |
# ifdef PASS_ARG_POINTER |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
797 |
RETURN ( __interpret(self, 6, nil, home, nil, nil, &arg1) ); |
328 | 798 |
# else |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
799 |
RETURN ( __interpret(self, 6, nil, home, nil, nil, arg1, arg2, arg3, arg4, arg5, arg6) ); |
328 | 800 |
# endif |
1 | 801 |
#endif |
802 |
} |
|
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
803 |
%}. |
103 | 804 |
^ self wrongNumberOfArguments:6 |
1 | 805 |
! |
806 |
||
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
807 |
value:arg1 value:arg2 value:arg3 value:arg4 value:arg5 value:arg6 value:arg7 |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
808 |
"evaluate the receiver with seven arguments. |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
809 |
The receiver must be a 7-arg block." |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
810 |
|
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
811 |
%{ /* NOCONTEXT */ |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
812 |
|
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
813 |
REGISTER OBJFUNC thecode; |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
814 |
OBJ home; |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
815 |
|
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
816 |
if (__INST(nargs) == __MKSMALLINT(7)) { |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
817 |
#if defined(THIS_CONTEXT) |
2238 | 818 |
if (__ISVALID_ILC_LNO(__pilc)) |
819 |
__ContextInstPtr(__thisContext)->c_lineno = __ILC_LNO_AS_OBJ(__pilc); |
|
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
820 |
#endif |
2238 | 821 |
thecode = __BlockInstPtr(self)->b_code; |
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
822 |
#ifdef NEW_BLOCK_CALL |
2238 | 823 |
if (thecode != (OBJFUNC)nil) { |
824 |
RETURN ( (*thecode)(self, arg1, arg2, arg3, arg4, arg5, arg6, arg7) ); |
|
825 |
} |
|
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
826 |
# ifdef PASS_ARG_POINTER |
2238 | 827 |
RETURN ( __interpret(self, 7, nil, nil, nil, nil, &arg1) ); |
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
828 |
# else |
2238 | 829 |
RETURN ( __interpret(self, 7, nil, nil, nil, nil, arg1, arg2, arg3, arg4, arg5, arg6, arg7) ); |
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
830 |
# endif |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
831 |
#else |
2238 | 832 |
home = __BlockInstPtr(self)->b_home; |
833 |
if (thecode != (OBJFUNC)nil) { |
|
834 |
RETURN ( (*thecode)(home, arg1, arg2, arg3, arg4, arg5, arg6, arg7) ); |
|
835 |
} |
|
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
836 |
# ifdef PASS_ARG_POINTER |
2238 | 837 |
RETURN ( __interpret(self, 7, nil, home, nil, nil, &arg1) ); |
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
838 |
# else |
2238 | 839 |
RETURN ( __interpret(self, 7, nil, home, nil, nil, arg1, arg2, arg3, arg4, arg5, arg6, arg7) ); |
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
840 |
# endif |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
841 |
#endif |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
842 |
} |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
843 |
%}. |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
844 |
^ self wrongNumberOfArguments:7 |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
845 |
! |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
846 |
|
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
847 |
value:arg1 value:arg2 value:arg3 value:arg4 value:arg5 value:arg6 value:arg7 value:arg8 |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
848 |
"evaluate the receiver with eight arguments. |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
849 |
The receiver must be a 8-arg block." |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
850 |
|
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
851 |
%{ /* NOCONTEXT */ |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
852 |
|
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
853 |
REGISTER OBJFUNC thecode; |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
854 |
OBJ home; |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
855 |
|
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
856 |
if (__INST(nargs) == __MKSMALLINT(8)) { |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
857 |
#if defined(THIS_CONTEXT) |
2238 | 858 |
if (__ISVALID_ILC_LNO(__pilc)) |
859 |
__ContextInstPtr(__thisContext)->c_lineno = __ILC_LNO_AS_OBJ(__pilc); |
|
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
860 |
#endif |
2238 | 861 |
thecode = __BlockInstPtr(self)->b_code; |
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
862 |
#ifdef NEW_BLOCK_CALL |
2238 | 863 |
if (thecode != (OBJFUNC)nil) { |
864 |
RETURN ( (*thecode)(self, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) ); |
|
865 |
} |
|
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
866 |
# ifdef PASS_ARG_POINTER |
2238 | 867 |
RETURN ( __interpret(self, 8, nil, nil, nil, nil, &arg1) ); |
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
868 |
# else |
2238 | 869 |
RETURN ( __interpret(self, 8, nil, nil, nil, nil, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) ); |
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
870 |
# endif |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
871 |
#else |
2238 | 872 |
home = __BlockInstPtr(self)->b_home; |
873 |
if (thecode != (OBJFUNC)nil) { |
|
874 |
RETURN ( (*thecode)(home, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) ); |
|
875 |
} |
|
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
876 |
# ifdef PASS_ARG_POINTER |
2238 | 877 |
RETURN ( __interpret(self, 8, nil, home, nil, nil, &arg1) ); |
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
878 |
# else |
2238 | 879 |
RETURN ( __interpret(self, 8, nil, home, nil, nil, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) ); |
1605
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
880 |
# endif |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
881 |
#endif |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
882 |
} |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
883 |
%}. |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
884 |
^ self wrongNumberOfArguments:8 |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
885 |
! |
61def0ffbdd6
added value:...value: with 7 and 8 args
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
886 |
|
1 | 887 |
valueWithArguments:argArray |
888 |
"evaluate the receiver with arguments taken from argArray. |
|
313 | 889 |
ArgArray must be either an Array or nil. |
1 | 890 |
The size of the argArray must match the number of arguments the receiver expects." |
891 |
||
1037 | 892 |
|a1 a2 a3 a4 a5 a6 a7 a8| |
1 | 893 |
|
319 | 894 |
(argArray notNil and:[argArray class ~~ Array]) ifTrue:[ |
154 | 895 |
^ self badArgumentArry |
1 | 896 |
]. |
897 |
(argArray size == nargs) ifFalse:[ |
|
154 | 898 |
^ self wrongNumberOfArguments:(argArray size) |
1 | 899 |
]. |
900 |
%{ |
|
901 |
||
902 |
REGISTER OBJFUNC thecode; |
|
903 |
OBJ home; |
|
67 | 904 |
REGISTER OBJ *ap; |
328 | 905 |
int nargs; |
1 | 906 |
|
907 |
#if defined(THIS_CONTEXT) |
|
328 | 908 |
if (__ISVALID_ILC_LNO(__pilc)) |
1133 | 909 |
__ContextInstPtr(__thisContext)->c_lineno = __ILC_LNO_AS_OBJ(__pilc); |
1 | 910 |
#endif |
1133 | 911 |
thecode = __BlockInstPtr(self)->b_code; |
1037 | 912 |
|
913 |
#ifndef NEW_BLOCK_CALL |
|
1133 | 914 |
home = __BlockInstPtr(self)->b_home; |
1037 | 915 |
if (thecode != (OBJFUNC)nil) { |
2238 | 916 |
switch (nargs = __intVal(__INST(nargs))) { |
917 |
default: |
|
918 |
goto error; |
|
919 |
case 8: |
|
920 |
ap = __ArrayInstPtr(argArray)->a_element; |
|
921 |
RETURN ( (*thecode)(home, ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6], ap[7]) ); |
|
922 |
case 7: |
|
923 |
ap = __ArrayInstPtr(argArray)->a_element; |
|
924 |
RETURN ( (*thecode)(home, ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6]) ); |
|
925 |
case 6: |
|
926 |
ap = __ArrayInstPtr(argArray)->a_element; |
|
927 |
RETURN ( (*thecode)(home, ap[0], ap[1], ap[2], ap[3], ap[4], ap[5]) ); |
|
928 |
case 5: |
|
929 |
ap = __ArrayInstPtr(argArray)->a_element; |
|
930 |
RETURN ( (*thecode)(home, ap[0], ap[1], ap[2], ap[3], ap[4]) ); |
|
931 |
case 4: |
|
932 |
ap = __ArrayInstPtr(argArray)->a_element; |
|
933 |
RETURN ( (*thecode)(home, ap[0], ap[1], ap[2], ap[3]) ); |
|
934 |
case 3: |
|
935 |
ap = __ArrayInstPtr(argArray)->a_element; |
|
936 |
RETURN ( (*thecode)(home, ap[0], ap[1], ap[2]) ); |
|
937 |
case 2: |
|
938 |
ap = __ArrayInstPtr(argArray)->a_element; |
|
939 |
RETURN ( (*thecode)(home, ap[0], ap[1]) ); |
|
940 |
case 1: |
|
941 |
RETURN ( (*thecode)(home, __ArrayInstPtr(argArray)->a_element[0]) ); |
|
942 |
case 0: |
|
943 |
RETURN ( (*thecode)(home) ); |
|
944 |
break; |
|
1037 | 945 |
} |
946 |
} |
|
947 |
#endif |
|
948 |
||
1133 | 949 |
switch (nargs = __intVal(__INST(nargs))) { |
154 | 950 |
default: |
951 |
goto error; |
|
1037 | 952 |
case 8: |
1133 | 953 |
a8 = __ArrayInstPtr(argArray)->a_element[7]; |
154 | 954 |
case 7: |
1133 | 955 |
a7 = __ArrayInstPtr(argArray)->a_element[6]; |
154 | 956 |
case 6: |
1133 | 957 |
a6 = __ArrayInstPtr(argArray)->a_element[5]; |
154 | 958 |
case 5: |
1133 | 959 |
a5 = __ArrayInstPtr(argArray)->a_element[4]; |
154 | 960 |
case 4: |
1133 | 961 |
a4 = __ArrayInstPtr(argArray)->a_element[3]; |
154 | 962 |
case 3: |
1133 | 963 |
a3 = __ArrayInstPtr(argArray)->a_element[2]; |
154 | 964 |
case 2: |
1133 | 965 |
a2 = __ArrayInstPtr(argArray)->a_element[1]; |
154 | 966 |
case 1: |
1133 | 967 |
a1 = __ArrayInstPtr(argArray)->a_element[0]; |
154 | 968 |
case 0: |
969 |
break; |
|
1 | 970 |
} |
1133 | 971 |
|
11 | 972 |
#ifdef NEW_BLOCK_CALL |
1 | 973 |
if (thecode != (OBJFUNC)nil) { |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
974 |
RETURN ( (*thecode)(self, a1, a2, a3, a4, a5, a6, a7, a8) ); |
11 | 975 |
} |
328 | 976 |
# ifdef PASS_ARG_POINTER |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
977 |
RETURN ( __interpret(self, nargs, nil, nil, nil, nil, &a1) ); |
328 | 978 |
# else |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
979 |
RETURN ( __interpret(self, nargs, nil, nil, nil, nil, a1, a2, a3, a4, a5, a6, a7, a8) ); |
328 | 980 |
# endif |
1037 | 981 |
|
1 | 982 |
#else |
1037 | 983 |
|
328 | 984 |
# ifdef PASS_ARG_POINTER |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
985 |
RETURN ( __interpret(self, nargs, nil, home, nil, nil, &a1) ); |
328 | 986 |
# else |
2201
db0f6e86c8bb
COMMA_SENDER arg is no longer supported / needed
Claus Gittinger <cg@exept.de>
parents:
1979
diff
changeset
|
987 |
RETURN ( __interpret(self, nargs, nil, home, nil, nil, a1, a2, a3, a4, a5, a6, a7, a8) ); |
328 | 988 |
# endif |
1037 | 989 |
|
1 | 990 |
#endif |
67 | 991 |
|
11 | 992 |
error: ; |
1 | 993 |
%} |
11 | 994 |
. |
138 | 995 |
" |
996 |
the above code only supports up-to 7 arguments |
|
997 |
" |
|
128 | 998 |
^ ArgumentSignal |
154 | 999 |
raiseRequestWith:self |
1000 |
errorString:'only blocks with up-to 7 arguments supported' |
|
1001 |
! ! |
|
1002 |
||
623 | 1003 |
!Block methodsFor:'looping'! |
325 | 1004 |
|
623 | 1005 |
doUntil:aBlock |
1006 |
"repeat the receiver block until aBlock evaluates to true. |
|
1007 |
The receiver is evaluated at least once. |
|
1008 |
This is the same as '... doWhile:[... not]' " |
|
325 | 1009 |
|
623 | 1010 |
"this implementation is for purists ... :-)" |
1011 |
||
1012 |
self value. |
|
1013 |
aBlock value ifTrue:[^ nil]. |
|
1014 |
thisContext restart |
|
325 | 1015 |
|
1016 |
" |
|
623 | 1017 |
|n| |
154 | 1018 |
|
623 | 1019 |
n := 1. |
1020 |
[n printNewline] doUntil:[ (n := n + 1) > 5 ] |
|
154 | 1021 |
" |
1 | 1022 |
! |
1023 |
||
623 | 1024 |
doWhile:aBlock |
1025 |
"repeat the receiver block until aBlock evaluates to false. |
|
1026 |
The receiver is evaluated at least once." |
|
67 | 1027 |
|
154 | 1028 |
"this implementation is for purists ... :-)" |
1029 |
||
623 | 1030 |
self value. |
1031 |
aBlock value ifFalse:[^ nil]. |
|
1032 |
thisContext restart |
|
1033 |
||
1034 |
" |
|
1035 |
|n| |
|
1036 |
||
1037 |
n := 1. |
|
1038 |
[n printNewline] doWhile:[ (n := n + 1) <= 5 ] |
|
1039 |
" |
|
1040 |
! |
|
1041 |
||
1042 |
loop |
|
1211 | 1043 |
"repeat the receiver forever |
1044 |
(the receiver block should contain a return somewhere). |
|
1045 |
The implementation below was inspired by a corresponding Self method." |
|
623 | 1046 |
|
1047 |
self value. |
|
67 | 1048 |
thisContext restart |
1049 |
||
1050 |
" |
|
1051 |
|n| |
|
1052 |
||
1053 |
n := 1. |
|
623 | 1054 |
[ |
2238 | 1055 |
n printNewline. |
1056 |
n >= 10 ifTrue:[^ nil]. |
|
1057 |
n := n + 1 |
|
623 | 1058 |
] loop |
1059 |
" |
|
1211 | 1060 |
|
1061 |
"Modified: 18.4.1996 / 13:50:40 / cg" |
|
623 | 1062 |
! |
1063 |
||
1064 |
loopWithExit |
|
1065 |
"the receiver must be a block of one argument. It is evaluated in a loop forever, |
|
1066 |
and is passed a block, which, if sent a value:-message, will exit the receiver block, |
|
1067 |
returning the parameter of the value:-message. Used for loops with exit in the middle. |
|
1068 |
Inspired by a corresponding Self method." |
|
1069 |
||
1070 |
|exitBlock| |
|
1071 |
||
1072 |
exitBlock := [:exitValue | ^ exitValue]. |
|
1073 |
[true] whileTrue:[self value:exitBlock] |
|
1074 |
||
1075 |
" |
|
1076 |
|i| |
|
1077 |
i := 1. |
|
1078 |
[:exit | |
|
1422 | 1079 |
Transcript showCR:i. |
623 | 1080 |
i == 5 ifTrue:[exit value:'thats it']. |
1081 |
i := i + 1 |
|
1082 |
] loopWithExit |
|
1083 |
" |
|
1084 |
! |
|
1085 |
||
1086 |
repeat |
|
1211 | 1087 |
"repeat the receiver forever - same as loop, for ST-80 compatibility. |
1088 |
(the receiver block should contain a return somewhere)." |
|
623 | 1089 |
|
1090 |
self value. |
|
1091 |
thisContext restart |
|
1211 | 1092 |
|
1093 |
"Modified: 18.4.1996 / 13:50:55 / cg" |
|
623 | 1094 |
! |
1095 |
||
1096 |
valueWithExit |
|
1097 |
"the receiver must be a block of one argument. It is evaluated, and is passed a block, |
|
1098 |
which, if sent a value:-message, will exit the receiver block, returning the parameter of the |
|
1099 |
value:-message. Used for premature returns to the caller. |
|
1100 |
Taken from a manchester goody (a similar construct also appears in Self)." |
|
1101 |
||
1211 | 1102 |
^ self value:[:exitValue | ^exitValue] |
623 | 1103 |
|
1104 |
" |
|
1105 |
[:exit | |
|
2238 | 1106 |
1 to:10 do:[:i | |
1107 |
Transcript showCR:i. |
|
1108 |
i == 5 ifTrue:[exit value:'thats it'] |
|
1109 |
]. |
|
1110 |
'regular block-value; never returned' |
|
623 | 1111 |
] valueWithExit |
1112 |
" |
|
1211 | 1113 |
|
1114 |
"Modified: 18.4.1996 / 13:51:38 / cg" |
|
623 | 1115 |
! |
1116 |
||
1117 |
whileFalse |
|
1118 |
"evaluate the receiver while it evaluates to false (ST80 compatibility)" |
|
1119 |
||
1120 |
"this implementation is for purists ... :-)" |
|
1121 |
||
1122 |
self value ifTrue:[^ nil]. |
|
1123 |
thisContext restart |
|
1124 |
||
1125 |
" |
|
1126 |
|n| |
|
1127 |
||
1128 |
n := 1. |
|
1129 |
[n printNewline. (n := n + 1) > 10] whileFalse |
|
67 | 1130 |
" |
1131 |
! |
|
1 | 1132 |
|
67 | 1133 |
whileFalse:aBlock |
1134 |
"evaluate the argument, aBlock while the receiver evaluates to false. |
|
1135 |
- usually open coded by compilers, but needed here for #perform |
|
1136 |
and expression evaluation." |
|
1137 |
||
154 | 1138 |
"this implementation is for purists ... :-)" |
1139 |
||
67 | 1140 |
self value ifTrue:[^ nil]. |
1141 |
aBlock value. |
|
1142 |
thisContext restart |
|
1143 |
||
1144 |
" |
|
1145 |
|n| |
|
1146 |
||
1147 |
n := 1. |
|
1148 |
[n > 10] whileFalse:[ |
|
154 | 1149 |
n printNewline. |
1150 |
n := n + 1 |
|
67 | 1151 |
] |
1152 |
" |
|
1 | 1153 |
! |
1154 |
||
1155 |
whileTrue |
|
67 | 1156 |
"evaluate the receiver while it evaluates to true (ST80 compatibility)" |
1 | 1157 |
|
154 | 1158 |
"this implementation is for purists ... :-)" |
1159 |
||
67 | 1160 |
self value ifFalse:[^ nil]. |
1161 |
thisContext restart |
|
1 | 1162 |
|
67 | 1163 |
" |
1164 |
|n| |
|
1 | 1165 |
|
67 | 1166 |
n := 1. |
1167 |
[n printNewline. (n := n + 1) <= 10] whileTrue |
|
1168 |
" |
|
1 | 1169 |
! |
1170 |
||
623 | 1171 |
whileTrue:aBlock |
1172 |
"evaluate the argument, aBlock while the receiver evaluates to true. |
|
1173 |
- usually open coded by compilers, but needed here for #perform |
|
1174 |
and expression evaluation." |
|
1 | 1175 |
|
154 | 1176 |
"this implementation is for purists ... :-)" |
1177 |
||
623 | 1178 |
self value ifFalse:[^ nil]. |
1179 |
aBlock value. |
|
67 | 1180 |
thisContext restart |
1181 |
||
1182 |
" |
|
1183 |
|n| |
|
1 | 1184 |
|
67 | 1185 |
n := 1. |
623 | 1186 |
[n <= 10] whileTrue:[ |
154 | 1187 |
n printNewline. |
1188 |
n := n + 1 |
|
623 | 1189 |
] |
68 | 1190 |
" |
67 | 1191 |
! ! |
1192 |
||
92 | 1193 |
!Block methodsFor:'printing & storing'! |
1 | 1194 |
|
1195 |
printOn:aStream |
|
44 | 1196 |
"append a a printed representation of the block to aStream" |
1197 |
||
213 | 1198 |
|homeClass h sel methodClass| |
54 | 1199 |
|
1200 |
"cheap blocks have no home context, but a method instead" |
|
1201 |
||
1202 |
(home isNil or:[home isContext not]) ifTrue:[ |
|
154 | 1203 |
aStream nextPutAll:'[] in '. |
1 | 1204 |
|
154 | 1205 |
" |
1206 |
currently, some cheap blocks don't know where they have been created |
|
1207 |
" |
|
1208 |
aStream nextPutAll:' ??? (optimized)'. |
|
1209 |
^ self |
|
54 | 1210 |
]. |
1211 |
||
1212 |
"a full blown block (with home, but without method)" |
|
1213 |
||
1214 |
aStream nextPutAll:'[] in '. |
|
154 | 1215 |
h := self methodHome. |
1216 |
sel := h selector. |
|
1217 |
"/ old: |
|
1218 |
"/ home receiver class name printOn:aStream. |
|
1219 |
"/ new: |
|
212 | 1220 |
"/ (h searchClass whichClassImplements:sel) name printOn:aStream. |
213 | 1221 |
methodClass := h methodClass. |
1222 |
methodClass isNil ifTrue:[ |
|
1223 |
'UnboundMethod' printOn:aStream. |
|
1224 |
] ifFalse:[ |
|
1225 |
methodClass name printOn:aStream. |
|
1226 |
]. |
|
54 | 1227 |
aStream nextPut:$-. |
154 | 1228 |
sel printOn:aStream. |
54 | 1229 |
|
1230 |
" |
|
1 | 1231 |
aStream nextPutAll:'[] in '. |
1232 |
homeClass := home containingClass. |
|
1233 |
homeClass notNil ifTrue:[ |
|
154 | 1234 |
homeClass name printOn:aStream. |
1235 |
aStream space. |
|
1236 |
(homeClass selectorForMethod:home) printOn:aStream |
|
1 | 1237 |
] ifFalse:[ |
154 | 1238 |
aStream nextPutAll:' ???' |
1 | 1239 |
] |
54 | 1240 |
" |
1 | 1241 |
! ! |
623 | 1242 |
|
1243 |
!Block methodsFor:'private accessing'! |
|
1244 |
||
1773
442d1b73ecb9
no longer allow Blocks with a dynamic-bit to be created
Claus Gittinger <cg@exept.de>
parents:
1672
diff
changeset
|
1245 |
byteCode:bCode numArgs:numArgs sourcePosition:srcPos initialPC:iPC literals:lits |
1265 | 1246 |
"set all relevant internals. |
1773
442d1b73ecb9
no longer allow Blocks with a dynamic-bit to be created
Claus Gittinger <cg@exept.de>
parents:
1672
diff
changeset
|
1247 |
DANGER ALERT: this interface is strictly private." |
623 | 1248 |
|
1249 |
byteCode := bCode. |
|
1250 |
nargs := numArgs. |
|
1251 |
sourcePos := srcPos. |
|
1252 |
initialPC := iPC. |
|
1777 | 1253 |
flags := 0. |
1493
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
1254 |
self literals:lits. |
1265 | 1255 |
|
1256 |
"Modified: 23.4.1996 / 16:05:30 / cg" |
|
1493
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1422
diff
changeset
|
1257 |
"Modified: 24.6.1996 / 12:37:37 / stefan" |
1773
442d1b73ecb9
no longer allow Blocks with a dynamic-bit to be created
Claus Gittinger <cg@exept.de>
parents:
1672
diff
changeset
|
1258 |
"Created: 17.10.1996 / 14:05:45 / cg" |
623 | 1259 |
! |
1260 |
||
1261 |
initialPC:initial |
|
1265 | 1262 |
"set the initial pc for evaluation. |
1263 |
DANGER ALERT: this interface is for the compiler only." |
|
623 | 1264 |
|
1265 |
initialPC := initial |
|
1265 | 1266 |
|
1267 |
"Modified: 23.4.1996 / 16:05:39 / cg" |
|
623 | 1268 |
! |
1269 |
||
1270 |
numArgs:numArgs |
|
1265 | 1271 |
"set the number of arguments the receiver expects for evaluation. |
1272 |
DANGER ALERT: this interface is for the compiler only." |
|
623 | 1273 |
|
1274 |
nargs := numArgs |
|
1265 | 1275 |
|
1276 |
"Modified: 23.4.1996 / 16:05:52 / cg" |
|
623 | 1277 |
! |
1278 |
||
1279 |
sourcePosition:position |
|
1265 | 1280 |
"set the position of the source within my method. |
1281 |
This interface is for the compiler only." |
|
623 | 1282 |
|
1283 |
sourcePos := position |
|
1265 | 1284 |
|
1285 |
"Modified: 23.4.1996 / 16:06:19 / cg" |
|
623 | 1286 |
! ! |
1287 |
||
1288 |
!Block methodsFor:'privileged evaluation'! |
|
1289 |
||
1290 |
valueUninterruptably |
|
1291 |
"evaluate the receiver with interrupts blocked. |
|
1292 |
This does not prevent preemption by a higher priority processes |
|
1293 |
if any becomes runnable due to the evaluation of the receiver |
|
1294 |
(i.e. if a semaphore is signalled)." |
|
1295 |
||
2301 | 1296 |
"we must keep track of blocking-state if this is called nested" |
1297 |
(OperatingSystem blockInterrupts) ifTrue:[ |
|
1298 |
"/ already blocked. |
|
1299 |
^ self value |
|
1300 |
]. |
|
864 | 1301 |
|
2301 | 1302 |
^ self valueNowOrOnUnwindDo:[OperatingSystem unblockInterrupts]. |
623 | 1303 |
! |
1304 |
||
1305 |
valueUnpreemptively |
|
1306 |
"evaluate the receiver without the possiblity of preemption |
|
1307 |
(i.e. at a very high priority)" |
|
1308 |
||
1309 |
|oldPrio activeProcess| |
|
1310 |
||
1311 |
activeProcess := Processor activeProcess. |
|
1312 |
oldPrio := activeProcess changePriority:(Processor highestPriority). |
|
864 | 1313 |
^ self valueNowOrOnUnwindDo:[ |
623 | 1314 |
activeProcess priority:oldPrio |
1315 |
] |
|
1316 |
! ! |
|
1317 |
||
1318 |
!Block methodsFor:'process creation'! |
|
1319 |
||
1320 |
fork |
|
1321 |
"create a new process executing the receiver at the current priority." |
|
1322 |
||
1323 |
^ self newProcess resume |
|
1324 |
! |
|
1325 |
||
1326 |
forkAt:priority |
|
1327 |
"create a new process executing the receiver at a different priority." |
|
1328 |
||
1329 |
^ (self newProcess priority:priority) resume |
|
1330 |
! |
|
1331 |
||
1332 |
forkWith:argArray |
|
1333 |
"create a new process executing the receiver, |
|
1334 |
passing elements in argArray as arguments to the receiver block." |
|
1335 |
||
1336 |
^ [self valueWithArguments:argArray] fork. |
|
1337 |
! |
|
1338 |
||
1339 |
newProcess |
|
1340 |
"create a new (unscheduled) process executing the receiver" |
|
1341 |
||
1342 |
^ Process for:self priority:(Processor activePriority) |
|
1343 |
! |
|
1344 |
||
1345 |
newProcessWithArguments:argArray |
|
1346 |
"create a new (unscheduled) process executing the receiver, |
|
1347 |
passing the elements in argArray as arguments to the receiver block." |
|
1348 |
||
1349 |
^ [self valueWithArguments:argArray] newProcess |
|
1350 |
! |
|
1351 |
||
1352 |
promise |
|
1353 |
"create a promise on the receiver. The promise will evaluate the |
|
1354 |
receiver and promise to return the value with the #value message. |
|
1355 |
The evaluation will be performed as a separate process. |
|
1356 |
Asking the promise for its value will either block the asking process |
|
1357 |
(if the evaluation has not yet been finished) or return the value |
|
1358 |
immediately." |
|
1359 |
||
1360 |
^ Promise value:self |
|
1361 |
||
1362 |
" |
|
1363 |
|p| |
|
1364 |
||
1365 |
p := [1000 factorial] promise. |
|
1366 |
'do something else ...'. |
|
1367 |
p value |
|
1368 |
" |
|
1369 |
! |
|
1370 |
||
1371 |
promiseAt:prio |
|
1372 |
"create a promise on the receiver. The promise will evaluate the |
|
1373 |
receiver and promise to return the value with the #value message. |
|
1374 |
The evaluation will be performed as a separate process running at prio. |
|
1375 |
Asking the promise for its value will either block the asking process |
|
1376 |
(if the evaluation has not yet been finished) or return the value |
|
1377 |
immediately." |
|
1378 |
||
1379 |
^ Promise value:self priority:prio |
|
1380 |
! ! |
|
1381 |
||
1382 |
!Block methodsFor:'testing'! |
|
1383 |
||
1384 |
isBlock |
|
1385 |
"return true, if this is a block - yes I am" |
|
1386 |
||
1387 |
^ true |
|
2237
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
1388 |
! |
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
1389 |
|
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
1390 |
isVarArgBlock |
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
1391 |
"return true, if this block accepts a variable number of arguments" |
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
1392 |
|
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
1393 |
^ false |
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
1394 |
|
d54ee88b8106
VarArgBlock as a private subclass
Claus Gittinger <cg@exept.de>
parents:
2201
diff
changeset
|
1395 |
"Created: 23.1.1997 / 04:59:51 / cg" |
623 | 1396 |
! ! |
1397 |
||
1398 |
!Block methodsFor:'unwinding'! |
|
1399 |
||
1400 |
valueNowOrOnUnwindDo:aBlock |
|
1401 |
"evaluate the receiver - after that, or when some method sent within unwinds (i.e. does |
|
1402 |
a long return), evaluate the argument, aBlock. |
|
1403 |
This is used to make certain that cleanup actions (for example closing files etc.) are |
|
1404 |
executed regardless of error actions" |
|
1405 |
||
1406 |
|v| |
|
1407 |
||
1408 |
thisContext markForUnwind. |
|
1409 |
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
|
1410 |
thisContext unmarkForUnwind. |
623 | 1411 |
aBlock value. |
1412 |
^ v |
|
1413 |
||
1414 |
" |
|
1415 |
in the following example, f will be closed even if the block |
|
1416 |
returns with 'oops'. There are many more applications of this kind |
|
1417 |
found in the system. |
|
1418 |
" |
|
1419 |
" |
|
1420 |
|f| |
|
1421 |
||
1422 |
f := 'Makefile' asFilename readStream. |
|
1423 |
[ |
|
2238 | 1424 |
l := f nextLine. |
1425 |
l isNil ifTrue:[^ 'oops'] |
|
623 | 1426 |
] valueNowOrOnUnwindDo:[ |
2238 | 1427 |
f close |
623 | 1428 |
] |
1429 |
" |
|
1187
619ff79bc665
valueNowOrOnUnwindDo: don't execute unwind block when block is currently executed 'now'.
Stefan Vogel <sv@exept.de>
parents:
1181
diff
changeset
|
1430 |
|
619ff79bc665
valueNowOrOnUnwindDo: don't execute unwind block when block is currently executed 'now'.
Stefan Vogel <sv@exept.de>
parents:
1181
diff
changeset
|
1431 |
"Modified: 16.4.1996 / 11:05:26 / stefan" |
623 | 1432 |
! |
1433 |
||
1434 |
valueOnUnwindDo:aBlock |
|
1435 |
"evaluate the receiver - when some method sent within unwinds (i.e. does |
|
1436 |
a long return), evaluate the argument, aBlock. |
|
1437 |
This is used to make certain that cleanup actions (for example closing files etc.) are |
|
1438 |
executed regardless of error actions" |
|
1439 |
||
2286 | 1440 |
|v| |
1441 |
||
623 | 1442 |
thisContext markForUnwind. |
2286 | 1443 |
v := self value. "the real logic is in Context>>unwind" |
1444 |
thisContext unmarkForUnwind. |
|
1445 |
^ v |
|
1446 |
||
1447 |
"Modified: 27.1.1997 / 23:47:40 / cg" |
|
623 | 1448 |
! ! |
1449 |
||
1773
442d1b73ecb9
no longer allow Blocks with a dynamic-bit to be created
Claus Gittinger <cg@exept.de>
parents:
1672
diff
changeset
|
1450 |
!Block class methodsFor:'documentation'! |
1181
6637fee79d7b
only Block & CheapBlock are fixed - subclasses may look different
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1451 |
|
6637fee79d7b
only Block & CheapBlock are fixed - subclasses may look different
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1452 |
version |
2301 | 1453 |
^ '$Header: /cvs/stx/stx/libbasic/Block.st,v 1.71 1997-01-29 14:26:05 cg Exp $' |
1181
6637fee79d7b
only Block & CheapBlock are fixed - subclasses may look different
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
1454 |
! ! |
623 | 1455 |
Block initialize! |