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