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