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