1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
3 |
All Rights Reserved
|
|
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 |
|
83
|
13 |
ExecutableCodeObject subclass:#Block
|
|
14 |
instanceVariableNames:'home nargs
|
80
|
15 |
sourcePos initialPC
|
54
|
16 |
selfValue method'
|
1
|
17 |
classVariableNames:'InvalidNewSignal'
|
|
18 |
poolDictionaries:''
|
|
19 |
category:'Kernel-Methods'
|
|
20 |
!
|
|
21 |
|
|
22 |
Block comment:'
|
|
23 |
|
5
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
25 |
All Rights Reserved
|
|
26 |
|
83
|
27 |
$Header: /cvs/stx/stx/libbasic/Block.st,v 1.15 1994-06-02 11:18:43 claus Exp $
|
1
|
28 |
|
|
29 |
written spring 89 by claus
|
|
30 |
'!
|
|
31 |
|
|
32 |
!Block class methodsFor:'documentation'!
|
|
33 |
|
|
34 |
documentation
|
|
35 |
"
|
67
|
36 |
Blocks are pieces of executable code which can be evaluated by sending
|
|
37 |
them a value-message (''value'', ''value:'', ''value:value:'' etc).
|
1
|
38 |
|
67
|
39 |
Blocks with arguments need a message of type ''value:arg1 ... value:argn''
|
|
40 |
for evaluation; the number of arguments passed when evaluating must match
|
|
41 |
the number of arguments the block was declared with otherwise an error is
|
|
42 |
raised. Blocks without args need a ''value'' message for evaluation.
|
1
|
43 |
|
67
|
44 |
Blocks keep a reference to the method context where the block was declared -
|
|
45 |
this allows blocks to access the methods arguments and/or variables.
|
|
46 |
This is also true when the method has already returned - since the
|
|
47 |
block keeps this reference, the methods context will NOT die in this case.
|
1
|
48 |
|
67
|
49 |
A return (via ^-statement) out of a block will force a return from the
|
|
50 |
blocks method context (if it is still living) - this make the implementation
|
|
51 |
of long-jumps and control structures possible.
|
|
52 |
(If the method is not alive (i.e. has already returned), a return out of the block
|
|
53 |
is ignored and a simple return from the block is performed).
|
1
|
54 |
|
67
|
55 |
Long-jump is done by defining a catchBlock as ''[^ self]''
|
|
56 |
somewhere up in the calling-tree. Then, to do the long-jump from out of some
|
|
57 |
deeply nested method, simply do: ''catchBlock value''.
|
1
|
58 |
|
67
|
59 |
Instance variables:
|
1
|
60 |
|
67
|
61 |
code <not_an_object> the function pointer if its a compiled block
|
|
62 |
flags <SmallInteger> special flag bits coded in a number
|
|
63 |
byteCode <ByteArray> bytecode of home method if its an interpreted block
|
|
64 |
home <Context> the context where this block was created (i.e. defined)
|
|
65 |
this may be a BlockContext or a methodContext
|
|
66 |
nargs <SmallInteger> the number of arguments the block expects
|
|
67 |
sourcePos <SmallInteger> the character poistion of its source, in chars
|
|
68 |
relative to methods source beginning
|
|
69 |
initialPC <SmallInteger> the start position within the byteCode
|
|
70 |
for compiled blocks, this is nil.
|
|
71 |
literals <Array> the blocks literal array
|
|
72 |
selfValue <Object> value to use for self if its a copying block
|
|
73 |
method <Method> method where block was created (cheap blocks only)
|
1
|
74 |
|
67
|
75 |
NOTICE: layout known by runtime system and compiler - do not change
|
1
|
76 |
"
|
|
77 |
! !
|
|
78 |
|
|
79 |
!Block class methodsFor:'initialization' !
|
|
80 |
|
|
81 |
initialize
|
|
82 |
"setup the signals"
|
|
83 |
|
48
|
84 |
InvalidNewSignal isNil ifTrue:[
|
|
85 |
InvalidNewSignal := (Signal new).
|
|
86 |
InvalidNewSignal mayProceed:false.
|
|
87 |
InvalidNewSignal notifierString:'blocks are only created by the system'.
|
|
88 |
]
|
1
|
89 |
! !
|
|
90 |
|
3
|
91 |
!Block class methodsFor:'queries'!
|
|
92 |
|
|
93 |
isBuiltInClass
|
|
94 |
"this class is known by the run-time-system"
|
|
95 |
|
|
96 |
^ true
|
|
97 |
! !
|
|
98 |
|
1
|
99 |
!Block class methodsFor:'instance creation'!
|
|
100 |
|
11
|
101 |
code:codeAddress byteCode:bCode nargs:numArgs sourcePosition:sourcePos initialPC:initialPC literals:literals dynamic:dynamic
|
1
|
102 |
"create a new cheap (homeless) block.
|
|
103 |
Not for public use - special hook for the compiler."
|
|
104 |
|
|
105 |
|newBlock|
|
|
106 |
|
11
|
107 |
newBlock := super basicNew code:codeAddress
|
22
|
108 |
byteCode:bCode
|
|
109 |
nargs:numArgs
|
11
|
110 |
sourcePosition:sourcePos
|
22
|
111 |
initialPC:initialPC
|
|
112 |
literals:literals
|
|
113 |
dynamic:dynamic.
|
1
|
114 |
^ newBlock
|
|
115 |
!
|
|
116 |
|
22
|
117 |
new
|
1
|
118 |
"catch creation of blocks - only the system creates blocks"
|
|
119 |
|
|
120 |
InvalidNewSignal raise.
|
|
121 |
^ nil
|
|
122 |
!
|
|
123 |
|
22
|
124 |
new:size
|
1
|
125 |
"catch creation of blocks - only the system creates blocks"
|
|
126 |
|
|
127 |
InvalidNewSignal raise.
|
|
128 |
^ nil
|
|
129 |
! !
|
|
130 |
|
|
131 |
!Block methodsFor:'testing'!
|
|
132 |
|
|
133 |
isBlock
|
48
|
134 |
"return true, if this is a block - yes I am"
|
11
|
135 |
|
1
|
136 |
^ true
|
|
137 |
! !
|
|
138 |
|
|
139 |
!Block methodsFor:'accessing'!
|
|
140 |
|
|
141 |
nargs
|
|
142 |
"return the number of arguments I expect for evaluation"
|
|
143 |
|
|
144 |
^ nargs
|
|
145 |
!
|
|
146 |
|
|
147 |
selfValue
|
|
148 |
"return the copied self"
|
|
149 |
|
|
150 |
^ selfValue
|
|
151 |
! !
|
|
152 |
|
|
153 |
!Block methodsFor:'private accessing'!
|
|
154 |
|
11
|
155 |
code:codeAddress byteCode:bCode nargs:numArgs sourcePosition:srcPos initialPC:iPC literals:lits dynamic:dynamic
|
|
156 |
"set all relevant internals"
|
|
157 |
|
|
158 |
self code:codeAddress.
|
|
159 |
byteCode := bCode.
|
|
160 |
nargs := numArgs.
|
|
161 |
sourcePos := srcPos.
|
|
162 |
initialPC := iPC.
|
|
163 |
literals := lits.
|
|
164 |
self dynamic:dynamic
|
|
165 |
!
|
|
166 |
|
1
|
167 |
nargs:numArgs
|
48
|
168 |
"set the number of arguments I expect for evaluation - DANGER ALERT"
|
1
|
169 |
|
|
170 |
nargs := numArgs
|
|
171 |
!
|
|
172 |
|
|
173 |
sourcePosition:position
|
|
174 |
"set the position of the source within my method"
|
|
175 |
|
|
176 |
sourcePos := position
|
|
177 |
!
|
|
178 |
|
|
179 |
initialPC:initial
|
48
|
180 |
"set the initial pc for evaluation - DANGER ALERT"
|
1
|
181 |
|
|
182 |
initialPC := initial
|
|
183 |
! !
|
|
184 |
|
|
185 |
!Block methodsFor:'error handling'!
|
|
186 |
|
|
187 |
argumentCountError:numberGiven
|
|
188 |
"report that the number of arguments given does not match the number expected"
|
|
189 |
|
|
190 |
self error:('Block got ' , numberGiven printString ,
|
|
191 |
' args while ' , nargs printString , ' where expected')
|
|
192 |
!
|
|
193 |
|
|
194 |
invalidMethod
|
67
|
195 |
"this error is triggered by the interpreter when a non-Block object
|
|
196 |
is about to be executed.
|
|
197 |
In this case, the VM sends this to the bad method (the receiver).
|
|
198 |
Can only happen when the Compiler/runtime system is broken or
|
|
199 |
someone played around."
|
1
|
200 |
|
|
201 |
self error:'invalid block - not executable'
|
|
202 |
!
|
|
203 |
|
67
|
204 |
tooManySendArguments
|
|
205 |
"this error is triggered, when a method tries to perform a send with
|
|
206 |
more arguments than supported by the interpreter - this can only
|
|
207 |
happen, if the compiler has been changed without updating the VM."
|
|
208 |
|
|
209 |
self error:'too many send args - should not happen'
|
|
210 |
!
|
|
211 |
|
|
212 |
tooManyArguments
|
|
213 |
"this error is triggered, when a method is called with more arguments
|
|
214 |
than supported by the interpreter - this can only happen, if the
|
|
215 |
compiler has been changed without updating the VM."
|
|
216 |
|
|
217 |
self error:'block has too many args - should not happen'
|
1
|
218 |
! !
|
|
219 |
|
|
220 |
!Block methodsFor:'evaluation'!
|
|
221 |
|
|
222 |
value
|
|
223 |
"evaluate the receiver with no block args. The receiver must be a block without arguments."
|
|
224 |
|
|
225 |
%{ /* NOCONTEXT */
|
|
226 |
|
|
227 |
REGISTER OBJFUNC thecode;
|
|
228 |
OBJ home;
|
|
229 |
extern OBJ interpret();
|
|
230 |
|
|
231 |
if (_INST(nargs) == _MKSMALLINT(0)) {
|
|
232 |
#if defined(THIS_CONTEXT)
|
|
233 |
if (1 /* _intVal(__pilc->ilc_lineNo) > 0 */)
|
|
234 |
_ContextInstPtr(__thisContext)->c_lineno = __pilc->ilc_lineNo;
|
|
235 |
#endif
|
11
|
236 |
thecode = _BlockInstPtr(self)->b_code;
|
|
237 |
#ifdef NEW_BLOCK_CALL
|
22
|
238 |
if (thecode != (OBJFUNC)nil) {
|
|
239 |
/* compiled machine code */
|
|
240 |
RETURN ( (*thecode)(self, COMMA_SND) );
|
|
241 |
}
|
|
242 |
/* interpreted code */
|
|
243 |
RETURN ( interpret(self, 0, nil, nil COMMA_SND, nil) );
|
11
|
244 |
#else
|
1
|
245 |
home = _BlockInstPtr(self)->b_home;
|
|
246 |
if (thecode != (OBJFUNC)nil) {
|
|
247 |
/* compiled machine code */
|
|
248 |
RETURN ( (*thecode)(home COMMA_SND) );
|
|
249 |
}
|
|
250 |
/* interpreted code */
|
|
251 |
RETURN ( interpret(self, 0, nil, home COMMA_SND, nil) );
|
11
|
252 |
#endif
|
1
|
253 |
}
|
|
254 |
%}
|
|
255 |
.
|
|
256 |
^ self argumentCountError:0
|
|
257 |
!
|
|
258 |
|
|
259 |
value:arg
|
|
260 |
"evaluate the receiver with one argument. The receiver must be a 1-arg block."
|
|
261 |
|
|
262 |
%{ /* NOCONTEXT */
|
|
263 |
|
|
264 |
REGISTER OBJFUNC thecode;
|
|
265 |
OBJ home;
|
|
266 |
extern OBJ interpret();
|
|
267 |
|
|
268 |
if (_INST(nargs) == _MKSMALLINT(1)) {
|
|
269 |
#if defined(THIS_CONTEXT)
|
|
270 |
if (1 /* _intVal(__pilc->ilc_lineNo) > 0 */)
|
|
271 |
_ContextInstPtr(__thisContext)->c_lineno = __pilc->ilc_lineNo;
|
|
272 |
#endif
|
|
273 |
thecode = _BlockInstPtr(self)->b_code;
|
11
|
274 |
#ifdef NEW_BLOCK_CALL
|
1
|
275 |
if (thecode != (OBJFUNC)nil) {
|
11
|
276 |
RETURN ( (*thecode)(self COMMA_SND, arg) );
|
1
|
277 |
}
|
|
278 |
/* interpreted code */
|
11
|
279 |
RETURN ( interpret(self, 1, nil, nil COMMA_SND, nil, arg) );
|
1
|
280 |
#else
|
11
|
281 |
home = _BlockInstPtr(self)->b_home;
|
|
282 |
if (thecode != (OBJFUNC)nil) {
|
|
283 |
RETURN ( (*thecode)(home COMMA_SND, arg) );
|
|
284 |
}
|
|
285 |
/* interpreted code */
|
1
|
286 |
RETURN ( interpret(self, 1, nil, home COMMA_SND, nil, arg) );
|
|
287 |
#endif
|
|
288 |
}
|
|
289 |
%}
|
|
290 |
.
|
|
291 |
^ self argumentCountError:1
|
|
292 |
!
|
|
293 |
|
|
294 |
value:arg1 value:arg2
|
|
295 |
"evaluate the receiver with two arguments. The receiver must be a 2-arg block."
|
|
296 |
|
|
297 |
%{ /* NOCONTEXT */
|
|
298 |
|
|
299 |
REGISTER OBJFUNC thecode;
|
|
300 |
OBJ home;
|
|
301 |
extern OBJ interpret();
|
|
302 |
|
|
303 |
if (_INST(nargs) == _MKSMALLINT(2)) {
|
|
304 |
#if defined(THIS_CONTEXT)
|
|
305 |
if (1 /* _intVal(__pilc->ilc_lineNo) > 0 */)
|
|
306 |
_ContextInstPtr(__thisContext)->c_lineno = __pilc->ilc_lineNo;
|
|
307 |
#endif
|
|
308 |
thecode = _BlockInstPtr(self)->b_code;
|
11
|
309 |
#ifdef NEW_BLOCK_CALL
|
1
|
310 |
if (thecode != (OBJFUNC)nil) {
|
11
|
311 |
RETURN ( (*thecode)(self COMMA_SND, arg1, arg2) );
|
|
312 |
}
|
|
313 |
RETURN ( interpret(self, 2, nil, nil COMMA_SND, nil, arg1, arg2) );
|
1
|
314 |
#else
|
11
|
315 |
home = _BlockInstPtr(self)->b_home;
|
|
316 |
if (thecode != (OBJFUNC)nil) {
|
1
|
317 |
RETURN ( (*thecode)(home COMMA_SND, arg1, arg2) );
|
|
318 |
}
|
|
319 |
RETURN ( interpret(self, 2, nil, home COMMA_SND, nil, arg1, arg2) );
|
|
320 |
#endif
|
|
321 |
}
|
|
322 |
%}
|
|
323 |
.
|
|
324 |
^ self argumentCountError:2
|
|
325 |
!
|
|
326 |
|
|
327 |
value:arg1 value:arg2 value:arg3
|
|
328 |
"evaluate the receiver with three arguments. The receiver must be a 3-arg block."
|
|
329 |
|
|
330 |
%{ /* NOCONTEXT */
|
|
331 |
|
|
332 |
REGISTER OBJFUNC thecode;
|
|
333 |
OBJ home;
|
|
334 |
extern OBJ interpret();
|
|
335 |
|
|
336 |
if (_INST(nargs) == _MKSMALLINT(3)) {
|
|
337 |
#if defined(THIS_CONTEXT)
|
|
338 |
if (1 /* _intVal(__pilc->ilc_lineNo) > 0 */)
|
|
339 |
_ContextInstPtr(__thisContext)->c_lineno = __pilc->ilc_lineNo;
|
|
340 |
#endif
|
|
341 |
thecode = _BlockInstPtr(self)->b_code;
|
11
|
342 |
#ifdef NEW_BLOCK_CALL
|
1
|
343 |
if (thecode != (OBJFUNC)nil) {
|
11
|
344 |
RETURN ( (*thecode)(self COMMA_SND, arg1, arg2, arg3) );
|
|
345 |
}
|
|
346 |
RETURN ( interpret(self, 3, nil, nil COMMA_SND, nil, arg1, arg2, arg3) );
|
1
|
347 |
#else
|
11
|
348 |
home = _BlockInstPtr(self)->b_home;
|
|
349 |
if (thecode != (OBJFUNC)nil) {
|
1
|
350 |
RETURN ( (*thecode)(home COMMA_SND, arg1, arg2, arg3) );
|
|
351 |
}
|
|
352 |
RETURN ( interpret(self, 3, nil, home COMMA_SND, nil, arg1, arg2, arg3) );
|
|
353 |
#endif
|
|
354 |
}
|
|
355 |
%}
|
|
356 |
.
|
|
357 |
^ self argumentCountError:3
|
|
358 |
!
|
|
359 |
|
|
360 |
value:arg1 value:arg2 value:arg3 value:arg4
|
|
361 |
"evaluate the receiver with four arguments. The receiver must be a 4-arg block."
|
|
362 |
|
|
363 |
%{ /* NOCONTEXT */
|
|
364 |
|
|
365 |
REGISTER OBJFUNC thecode;
|
|
366 |
OBJ home;
|
|
367 |
extern OBJ interpret();
|
|
368 |
|
|
369 |
if (_INST(nargs) == _MKSMALLINT(4)) {
|
|
370 |
#if defined(THIS_CONTEXT)
|
|
371 |
if (1 /* _intVal(__pilc->ilc_lineNo) > 0 */)
|
|
372 |
_ContextInstPtr(__thisContext)->c_lineno = __pilc->ilc_lineNo;
|
|
373 |
#endif
|
|
374 |
thecode = _BlockInstPtr(self)->b_code;
|
11
|
375 |
#ifdef NEW_BLOCK_CALL
|
1
|
376 |
if (thecode != (OBJFUNC)nil) {
|
11
|
377 |
RETURN ( (*thecode)(self COMMA_SND, arg1, arg2, arg3, arg4) );
|
|
378 |
}
|
|
379 |
RETURN ( interpret(self, 4, nil, nil COMMA_SND, nil, arg1, arg2, arg3, arg4) );
|
1
|
380 |
#else
|
11
|
381 |
home = _BlockInstPtr(self)->b_home;
|
|
382 |
if (thecode != (OBJFUNC)nil) {
|
1
|
383 |
RETURN ( (*thecode)(home COMMA_SND, arg1, arg2, arg3, arg4) );
|
|
384 |
}
|
|
385 |
RETURN ( interpret(self, 4, nil, home COMMA_SND, nil, arg1, arg2, arg3, arg4) );
|
|
386 |
#endif
|
|
387 |
}
|
|
388 |
%}
|
|
389 |
.
|
|
390 |
^ self argumentCountError:4
|
|
391 |
!
|
|
392 |
|
|
393 |
value:arg1 value:arg2 value:arg3 value:arg4 value:arg5
|
|
394 |
"evaluate the receiver with four arguments. The receiver must be a 5-arg block."
|
|
395 |
|
|
396 |
%{ /* NOCONTEXT */
|
|
397 |
|
|
398 |
REGISTER OBJFUNC thecode;
|
|
399 |
OBJ home;
|
|
400 |
extern OBJ interpret();
|
|
401 |
|
|
402 |
if (_INST(nargs) == _MKSMALLINT(5)) {
|
|
403 |
#if defined(THIS_CONTEXT)
|
|
404 |
if (1 /* _intVal(__pilc->ilc_lineNo) > 0 */)
|
|
405 |
_ContextInstPtr(__thisContext)->c_lineno = __pilc->ilc_lineNo;
|
|
406 |
#endif
|
|
407 |
thecode = _BlockInstPtr(self)->b_code;
|
11
|
408 |
#ifdef NEW_BLOCK_CALL
|
1
|
409 |
if (thecode != (OBJFUNC)nil) {
|
11
|
410 |
RETURN ( (*thecode)(self COMMA_SND, arg1, arg2, arg3, arg4, arg5) );
|
|
411 |
}
|
|
412 |
RETURN ( interpret(self, 5, nil, nil COMMA_SND, nil, arg1, arg2, arg3, arg4, arg5) );
|
1
|
413 |
#else
|
11
|
414 |
home = _BlockInstPtr(self)->b_home;
|
|
415 |
if (thecode != (OBJFUNC)nil) {
|
1
|
416 |
RETURN ( (*thecode)(home COMMA_SND, arg1, arg2, arg3, arg4, arg5) );
|
|
417 |
}
|
|
418 |
RETURN ( interpret(self, 5, nil, home COMMA_SND, nil, arg1, arg2, arg3, arg4, arg5) );
|
|
419 |
#endif
|
|
420 |
}
|
|
421 |
%}
|
|
422 |
.
|
|
423 |
^ self argumentCountError:5
|
|
424 |
!
|
|
425 |
|
|
426 |
value:arg1 value:arg2 value:arg3 value:arg4 value:arg5 value:arg6
|
|
427 |
"evaluate the receiver with four arguments. The receiver must be a 6-arg block."
|
|
428 |
|
|
429 |
%{ /* NOCONTEXT */
|
|
430 |
|
|
431 |
REGISTER OBJFUNC thecode;
|
|
432 |
OBJ home;
|
|
433 |
extern OBJ interpret();
|
|
434 |
|
|
435 |
if (_INST(nargs) == _MKSMALLINT(6)) {
|
|
436 |
#if defined(THIS_CONTEXT)
|
|
437 |
if (1 /* _intVal(__pilc->ilc_lineNo) > 0 */)
|
|
438 |
_ContextInstPtr(__thisContext)->c_lineno = __pilc->ilc_lineNo;
|
|
439 |
#endif
|
|
440 |
thecode = _BlockInstPtr(self)->b_code;
|
11
|
441 |
#ifdef NEW_BLOCK_CALL
|
1
|
442 |
if (thecode != (OBJFUNC)nil) {
|
11
|
443 |
RETURN ( (*thecode)(self COMMA_SND, arg1, arg2, arg3, arg4, arg5, arg6) );
|
|
444 |
}
|
|
445 |
RETURN ( interpret(self, 6, nil, nil COMMA_SND, nil, arg1, arg2, arg3, arg4, arg5, arg6) );
|
1
|
446 |
#else
|
11
|
447 |
home = _BlockInstPtr(self)->b_home;
|
|
448 |
if (thecode != (OBJFUNC)nil) {
|
1
|
449 |
RETURN ( (*thecode)(home COMMA_SND, arg1, arg2, arg3, arg4, arg5, arg6) );
|
|
450 |
}
|
|
451 |
RETURN ( interpret(self, 6, nil, home COMMA_SND, nil, arg1, arg2, arg3, arg4, arg5, arg6) );
|
|
452 |
#endif
|
|
453 |
}
|
|
454 |
%}
|
|
455 |
.
|
|
456 |
^ self argumentCountError:6
|
|
457 |
!
|
|
458 |
|
|
459 |
valueWithArguments:argArray
|
|
460 |
"evaluate the receiver with arguments taken from argArray.
|
|
461 |
The size of the argArray must match the number of arguments the receiver expects."
|
|
462 |
|
|
463 |
|a1 a2 a3 a4 a5 a6 a7|
|
|
464 |
|
|
465 |
(argArray class == Array) ifFalse:[
|
|
466 |
^ self error:'argument must be an array'
|
|
467 |
].
|
|
468 |
(argArray size == nargs) ifFalse:[
|
|
469 |
^ self argumentCountError:(argArray size)
|
|
470 |
].
|
|
471 |
%{
|
|
472 |
|
|
473 |
REGISTER OBJFUNC thecode;
|
|
474 |
OBJ home;
|
|
475 |
extern OBJ interpret();
|
67
|
476 |
REGISTER OBJ *ap;
|
1
|
477 |
|
|
478 |
#if defined(THIS_CONTEXT)
|
|
479 |
if (1 /* _intVal(__pilc->ilc_lineNo) > 0 */)
|
|
480 |
_ContextInstPtr(__thisContext)->c_lineno = __pilc->ilc_lineNo;
|
|
481 |
#endif
|
|
482 |
switch (_intVal(_INST(nargs))) {
|
22
|
483 |
default:
|
|
484 |
goto error;
|
1
|
485 |
case 7:
|
|
486 |
a7 = _ArrayInstPtr(argArray)->a_element[6];
|
|
487 |
case 6:
|
|
488 |
a6 = _ArrayInstPtr(argArray)->a_element[5];
|
|
489 |
case 5:
|
|
490 |
a5 = _ArrayInstPtr(argArray)->a_element[4];
|
|
491 |
case 4:
|
|
492 |
a4 = _ArrayInstPtr(argArray)->a_element[3];
|
|
493 |
case 3:
|
|
494 |
a3 = _ArrayInstPtr(argArray)->a_element[2];
|
|
495 |
case 2:
|
|
496 |
a2 = _ArrayInstPtr(argArray)->a_element[1];
|
|
497 |
case 1:
|
|
498 |
a1 = _ArrayInstPtr(argArray)->a_element[0];
|
|
499 |
case 0:
|
|
500 |
break;
|
|
501 |
}
|
|
502 |
thecode = _BlockInstPtr(self)->b_code;
|
11
|
503 |
#ifdef NEW_BLOCK_CALL
|
1
|
504 |
if (thecode != (OBJFUNC)nil) {
|
11
|
505 |
RETURN ( (*thecode)(self COMMA_SND, a1, a2, a3, a4, a5, a6, a7) );
|
|
506 |
}
|
67
|
507 |
RETURN ( interpret(self, _intVal(_INST(nargs)), nil, nil COMMA_SND, nil,
|
|
508 |
a1, a2, a3, a4, a5, a6, a7) );
|
1
|
509 |
#else
|
11
|
510 |
home = _BlockInstPtr(self)->b_home;
|
|
511 |
if (thecode != (OBJFUNC)nil) {
|
1
|
512 |
RETURN ( (*thecode)(home COMMA_SND, a1, a2, a3, a4, a5, a6, a7) );
|
|
513 |
}
|
67
|
514 |
RETURN ( interpret(self, _intVal(_INST(nargs)), nil, home COMMA_SND, nil,
|
|
515 |
a1, a2, a3, a4, a5, a6, a7) );
|
1
|
516 |
#endif
|
67
|
517 |
|
11
|
518 |
error: ;
|
1
|
519 |
%}
|
11
|
520 |
.
|
|
521 |
self error:'only blocks with up-to 7 arguments supported'
|
1
|
522 |
!
|
|
523 |
|
|
524 |
valueNowOrOnUnwindDo:aBlock
|
|
525 |
"evaluate the receiver - after that, or when some method sent within unwinds (i.e. does
|
|
526 |
a long return), evaluate the argument, aBlock.
|
|
527 |
This is used to make certain that cleanup actions (for example closing files etc.) are
|
|
528 |
executed regardless of error actions"
|
|
529 |
|
|
530 |
|v|
|
|
531 |
|
67
|
532 |
v := self value. "the real logic is in Context>>unwind"
|
1
|
533 |
aBlock value.
|
|
534 |
^ v
|
|
535 |
!
|
|
536 |
|
|
537 |
valueOnUnwindDo:aBlock
|
|
538 |
"evaluate the receiver - when some method sent within unwinds (i.e. does
|
|
539 |
a long return), evaluate the argument, aBlock.
|
|
540 |
This is used to make certain that cleanup actions (for example closing files etc.) are
|
|
541 |
executed regardless of error actions"
|
|
542 |
|
67
|
543 |
^ self value "the real logic is in Context>>unwind"
|
1
|
544 |
! !
|
|
545 |
|
|
546 |
!Block methodsFor:'looping'!
|
|
547 |
|
|
548 |
whileTrue:aBlock
|
|
549 |
"evaluate the argument, aBlock while the receiver evaluates to true.
|
67
|
550 |
- usually open coded by compilers, but needed here for #perform
|
|
551 |
and expression evaluation."
|
|
552 |
|
|
553 |
"now, this implementation is for purists ... :-)"
|
|
554 |
self value ifFalse:[^ nil].
|
|
555 |
aBlock value.
|
|
556 |
thisContext restart
|
|
557 |
|
|
558 |
"
|
|
559 |
|n|
|
|
560 |
|
|
561 |
n := 1.
|
|
562 |
[n <= 10] whileTrue:[
|
|
563 |
n printNewline.
|
|
564 |
n := n + 1
|
|
565 |
]
|
|
566 |
"
|
|
567 |
!
|
1
|
568 |
|
67
|
569 |
whileFalse:aBlock
|
|
570 |
"evaluate the argument, aBlock while the receiver evaluates to false.
|
|
571 |
- usually open coded by compilers, but needed here for #perform
|
|
572 |
and expression evaluation."
|
|
573 |
|
|
574 |
"now, this implementation is for purists ... :-)"
|
|
575 |
self value ifTrue:[^ nil].
|
|
576 |
aBlock value.
|
|
577 |
thisContext restart
|
|
578 |
|
|
579 |
"
|
|
580 |
|n|
|
|
581 |
|
|
582 |
n := 1.
|
|
583 |
[n > 10] whileFalse:[
|
|
584 |
n printNewline.
|
|
585 |
n := n + 1
|
|
586 |
]
|
|
587 |
"
|
1
|
588 |
!
|
|
589 |
|
|
590 |
whileTrue
|
67
|
591 |
"evaluate the receiver while it evaluates to true (ST80 compatibility)"
|
1
|
592 |
|
67
|
593 |
"now, this implementation is for purists ... :-)"
|
|
594 |
self value ifFalse:[^ nil].
|
|
595 |
thisContext restart
|
1
|
596 |
|
67
|
597 |
"
|
|
598 |
|n|
|
1
|
599 |
|
67
|
600 |
n := 1.
|
|
601 |
[n printNewline. (n := n + 1) <= 10] whileTrue
|
|
602 |
"
|
1
|
603 |
!
|
|
604 |
|
|
605 |
whileFalse
|
67
|
606 |
"evaluate the receiver while it evaluates to false (ST80 compatibility)"
|
|
607 |
|
|
608 |
"now, this implementation is for purists ... :-)"
|
|
609 |
self value ifTrue:[^ nil].
|
|
610 |
thisContext restart
|
1
|
611 |
|
67
|
612 |
"
|
|
613 |
|n|
|
|
614 |
|
|
615 |
n := 1.
|
|
616 |
[n printNewline. (n := n + 1) > 10] whileFalse
|
|
617 |
"
|
1
|
618 |
!
|
|
619 |
|
|
620 |
doWhile:aBlock
|
|
621 |
"repeat the receiver block until aBlock evaluates to false.
|
|
622 |
The receiver is evaluated at least once."
|
|
623 |
|
67
|
624 |
"now, this implementation is for purists ... :-)"
|
1
|
625 |
self value.
|
67
|
626 |
aBlock value ifFalse:[^ nil].
|
|
627 |
thisContext restart
|
|
628 |
|
|
629 |
"
|
|
630 |
|n|
|
|
631 |
|
|
632 |
n := 1.
|
68
|
633 |
[n printNewline] doWhile:[ (n := n + 1) <= 5 ]
|
67
|
634 |
"
|
1
|
635 |
!
|
|
636 |
|
|
637 |
doUntil:aBlock
|
|
638 |
"repeat the receiver block until aBlock evaluates to true.
|
67
|
639 |
The receiver is evaluated at least once.
|
|
640 |
This is the same as '... doWhile:[... not]' "
|
1
|
641 |
|
67
|
642 |
"now, this implementation is for purists ... :-)"
|
1
|
643 |
self value.
|
67
|
644 |
aBlock value ifTrue:[^ nil].
|
|
645 |
thisContext restart
|
68
|
646 |
|
|
647 |
"
|
|
648 |
|n|
|
|
649 |
|
|
650 |
n := 1.
|
|
651 |
[n printNewline] doUntil:[ (n := n + 1) > 5 ]
|
|
652 |
"
|
1
|
653 |
!
|
|
654 |
|
|
655 |
loop
|
|
656 |
"repeat the receiver forever (should contain a return somewhere).
|
|
657 |
Inspired by a corresponding Self method."
|
|
658 |
|
67
|
659 |
self value.
|
|
660 |
thisContext restart
|
|
661 |
|
|
662 |
"
|
|
663 |
|n|
|
1
|
664 |
|
67
|
665 |
n := 1.
|
|
666 |
[
|
|
667 |
n printNewline.
|
|
668 |
n >= 10 ifTrue:[^ nil].
|
|
669 |
n := n + 1
|
|
670 |
] loop
|
|
671 |
"
|
1
|
672 |
!
|
|
673 |
|
|
674 |
valueWithExit
|
|
675 |
"the receiver must be a block of one argument. It is evaluated, and is passed a block,
|
|
676 |
which, if sent a value:-message, will exit the receiver block, returning the parameter of the
|
|
677 |
value:-message. Used for premature returns to the caller.
|
|
678 |
Taken from a manchester goody (also appears in Self)."
|
|
679 |
|
|
680 |
^ self value: [:exitValue | ^exitValue]
|
|
681 |
|
|
682 |
"[:exit |
|
|
683 |
1 to:10 do:[:i |
|
48
|
684 |
Transcript showCr:i.
|
1
|
685 |
i == 5 ifTrue:[exit value:'thats it']
|
|
686 |
].
|
|
687 |
'regular block-value; never returned'
|
|
688 |
] valueWithExit"
|
|
689 |
!
|
|
690 |
|
|
691 |
loopWithExit
|
67
|
692 |
"the receiver must be a block of one argument. It is evaluated in a loop forever,
|
|
693 |
and is passed a block, which, if sent a value:-message, will exit the receiver block,
|
|
694 |
returning the parameter of the value:-message. Used for loops with exit in the middle.
|
1
|
695 |
Inspired by a corresponding Self method."
|
|
696 |
|
|
697 |
|exitBlock|
|
|
698 |
|
|
699 |
exitBlock := [:exitValue | ^ exitValue].
|
|
700 |
[true] whileTrue:[self value:exitBlock]
|
|
701 |
|
68
|
702 |
"
|
|
703 |
|i|
|
1
|
704 |
i := 1.
|
|
705 |
[:exit |
|
48
|
706 |
Transcript showCr:i.
|
1
|
707 |
i == 5 ifTrue:[exit value:'thats it'].
|
|
708 |
i := i + 1
|
68
|
709 |
] loopWithExit
|
|
710 |
"
|
67
|
711 |
! !
|
|
712 |
|
1
|
713 |
!Block methodsFor:'process creation'!
|
|
714 |
|
|
715 |
newProcess
|
|
716 |
"create a new (unscheduled) process executing the receiver"
|
|
717 |
|
67
|
718 |
^ Processor newProcessFor:self.
|
1
|
719 |
!
|
|
720 |
|
|
721 |
fork
|
|
722 |
"create a new process executing the receiver"
|
|
723 |
|
|
724 |
^ self newProcess resume
|
|
725 |
!
|
|
726 |
|
|
727 |
forkWith:argumentArray
|
44
|
728 |
"create a new process executing the receiver passing elements
|
|
729 |
in argumentArray to the receiver block"
|
|
730 |
|
1
|
731 |
|b|
|
|
732 |
|
|
733 |
b := [self valueWithArguments:argumentArray].
|
10
|
734 |
^ b fork
|
1
|
735 |
!
|
|
736 |
|
|
737 |
forkAt:priority
|
|
738 |
"create a new process executing the receiver"
|
|
739 |
|
|
740 |
^ (self newProcess priority:priority) resume
|
|
741 |
! !
|
|
742 |
|
|
743 |
!Block methodsFor:'printing'!
|
|
744 |
|
|
745 |
printOn:aStream
|
44
|
746 |
"append a a printed representation of the block to aStream"
|
|
747 |
|
54
|
748 |
|homeClass receiverClass selector|
|
|
749 |
|
|
750 |
"cheap blocks have no home context, but a method instead"
|
|
751 |
|
|
752 |
(home isNil or:[home isContext not]) ifTrue:[
|
|
753 |
aStream nextPutAll:'[] in '.
|
1
|
754 |
|
54
|
755 |
method notNil ifTrue:[
|
|
756 |
"find out, for which class this Method was for ..."
|
|
757 |
receiverClass := method containingClass.
|
|
758 |
receiverClass notNil ifTrue:[
|
|
759 |
selector := receiverClass selectorForMethod:method.
|
|
760 |
aStream nextPutAll:(receiverClass name , '-' , selector).
|
|
761 |
aStream nextPutAll:' (optimized)'.
|
|
762 |
^ self
|
|
763 |
].
|
|
764 |
].
|
|
765 |
"currently, some cheap blocks don't know where they have been created"
|
|
766 |
aStream nextPutAll:' ??? (optimized)'.
|
|
767 |
^ self
|
|
768 |
].
|
|
769 |
|
|
770 |
"a full blown block (with home, but without method)"
|
|
771 |
|
|
772 |
aStream nextPutAll:'[] in '.
|
|
773 |
home receiver class name printOn:aStream.
|
|
774 |
aStream nextPut:$-.
|
|
775 |
home methodHome selector printOn:aStream.
|
|
776 |
|
|
777 |
"
|
1
|
778 |
aStream nextPutAll:'[] in '.
|
|
779 |
homeClass := home containingClass.
|
|
780 |
homeClass notNil ifTrue:[
|
|
781 |
homeClass name printOn:aStream.
|
|
782 |
aStream space.
|
|
783 |
(homeClass selectorForMethod:home) printOn:aStream
|
|
784 |
] ifFalse:[
|
54
|
785 |
aStream nextPutAll:' ???'
|
1
|
786 |
]
|
54
|
787 |
"
|
1
|
788 |
! !
|