1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1988 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:#Behavior
|
68
|
14 |
instanceVariableNames:'superclass otherSuperclasses
|
|
15 |
selectors methods
|
1
|
16 |
instSize flags'
|
|
17 |
classVariableNames:''
|
|
18 |
poolDictionaries:''
|
|
19 |
category:'Kernel-Classes'
|
|
20 |
!
|
|
21 |
|
|
22 |
Behavior comment:'
|
|
23 |
|
5
|
24 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
1
|
25 |
All Rights Reserved
|
|
26 |
|
47
|
27 |
NOTICE: layout known by compiler and runtime system; be careful when changing
|
|
28 |
|
77
|
29 |
$Header: /cvs/stx/stx/libbasic/Behavior.st,v 1.13 1994-05-17 10:06:34 claus Exp $
|
47
|
30 |
written Dec 88 by claus
|
|
31 |
'!
|
|
32 |
|
|
33 |
!Behavior class methodsFor:'documentation'!
|
|
34 |
|
|
35 |
documentation
|
|
36 |
"
|
68
|
37 |
Every class in the system inherits from Behavior (via Class, ClassDescription);
|
|
38 |
so here is where most of the class messages end up being implemented.
|
|
39 |
(to answer a FAQ: 'Point basicNew' will be done here :-)
|
|
40 |
|
|
41 |
Beginners should keep in mind, that all classes are instances of Behavior
|
|
42 |
thus, you will find the above mentioned 'basicNew:' method under the 'instance'-
|
77
|
43 |
methods of Behavior - NOT under the class methods ('Behavior new' will create
|
|
44 |
a new class).
|
68
|
45 |
|
|
46 |
Behavior provides minimum support for all classes - additional stuff is
|
77
|
47 |
found in ClassDescription and Class. Behaviors provides all mechanisms needed
|
|
48 |
to create instances, and send messages to those. However, Behavior does not provide
|
|
49 |
all the (symbolic) information needed to compile methods for a class or to get
|
|
50 |
useful information in inspectors.
|
47
|
51 |
|
68
|
52 |
In contrast to other ST implementations, the methods have been separated
|
|
53 |
from the selectors (there is no Dictionary, but two separate Arrays)
|
|
54 |
- this avoids the need for knowledge about Dictionaries in the runtime library (VM)
|
|
55 |
(lookup and search in these is seldom anyway, so the added benefit from using a
|
|
56 |
hashed dictionary is almost void).
|
1
|
57 |
|
68
|
58 |
Instance variables:
|
1
|
59 |
|
68
|
60 |
superclass <Class> the receivers superclass
|
|
61 |
otherSuperclasses <Array of Class> experimental: other superclasses
|
|
62 |
selectors <Array of Symbol> the selectors for which inst-methods are defined here
|
|
63 |
methods <Array of Method> the inst-methods corresponding to the selectors
|
|
64 |
instSize <SmallInteger> the number of instance variables
|
|
65 |
flags <SmallInteger> special flag bits coded in a number
|
1
|
66 |
|
68
|
67 |
flag bits:
|
47
|
68 |
|
68
|
69 |
2r 0000 0000 1111 index type:
|
|
70 |
0 - not indexed
|
|
71 |
1 - byte indexed
|
|
72 |
2 - short indexed
|
|
73 |
3 - long indexed
|
|
74 |
4 - pointer indexed
|
|
75 |
5 - weak pointer indexed
|
|
76 |
6 - float indexed
|
|
77 |
7 - double indexed
|
47
|
78 |
|
68
|
79 |
8-15 reserved for future extensions
|
|
80 |
|
|
81 |
2r 0000 0001 0000 reserved for alien pointers extension
|
|
82 |
2r 0000 0010 0000 class has subclasses - not set/used currently
|
1
|
83 |
|
68
|
84 |
2r 0000 0100 0000 instances are kind-of blocks (alien ptr in code)
|
|
85 |
2r 0000 1000 0000 instances are kind-of methods (alien ptr in code)
|
47
|
86 |
|
68
|
87 |
2r 0001 0000 0000 instances are kind-of contexts
|
|
88 |
2r 0010 0000 0000 instances are kind-of block contexts
|
47
|
89 |
|
68
|
90 |
2r 0100 0000 0000 reserved for C++ extension (inst is proxy for alien C++ object)
|
|
91 |
2r 1000 0000 0000 reserved for objectiveC extension (inst is proxy for alien OBJC object)
|
47
|
92 |
|
68
|
93 |
other bits are reserved for future extensions.
|
|
94 |
Dont depend on these bit positions - they may change without notice;
|
|
95 |
use access methods (such as isFloats) to get this kind of information.
|
47
|
96 |
|
68
|
97 |
NOTICE: layout known by compiler and runtime system; be careful when changing
|
47
|
98 |
"
|
|
99 |
! !
|
1
|
100 |
|
3
|
101 |
!Behavior class methodsFor:'queries'!
|
|
102 |
|
|
103 |
isBuiltInClass
|
|
104 |
"this class is known by the run-time-system"
|
|
105 |
|
|
106 |
^ true
|
|
107 |
! !
|
|
108 |
|
1
|
109 |
!Behavior class methodsFor:'creating new classes'!
|
|
110 |
|
|
111 |
new
|
|
112 |
"creates and return a new class"
|
|
113 |
|
|
114 |
|newClass|
|
|
115 |
|
|
116 |
newClass := self basicNew.
|
|
117 |
newClass setSuperclass:Object
|
|
118 |
selectors:(Array new:0)
|
|
119 |
methods:(Array new:0)
|
|
120 |
instSize:0
|
|
121 |
flags:0.
|
|
122 |
^ newClass
|
|
123 |
! !
|
|
124 |
|
|
125 |
!Behavior methodsFor:'initialization'!
|
|
126 |
|
|
127 |
initialize
|
|
128 |
"to catch initialize for classes which do not"
|
|
129 |
|
|
130 |
^ self
|
|
131 |
!
|
|
132 |
|
|
133 |
reinitialize
|
|
134 |
"to catch reinitialize for classes which do not"
|
|
135 |
|
|
136 |
^ self
|
|
137 |
! !
|
|
138 |
|
11
|
139 |
!Behavior methodsFor:'copying'!
|
|
140 |
|
|
141 |
deepCopy
|
|
142 |
"return a deep copy of the receiver
|
|
143 |
- return the receiver here - time will show if this is ok"
|
|
144 |
|
|
145 |
^ self
|
|
146 |
!
|
|
147 |
|
|
148 |
deepCopyUsing:aDictionary
|
|
149 |
"return a deep copy of the receiver
|
|
150 |
- return the receiver here - time will show if this is ok"
|
|
151 |
|
|
152 |
^ self
|
|
153 |
!
|
|
154 |
|
|
155 |
simpleDeepCopy
|
|
156 |
"return a deep copy of the receiver
|
|
157 |
- return the receiver here - time will show if this is ok"
|
|
158 |
|
|
159 |
^ self
|
|
160 |
! !
|
|
161 |
|
1
|
162 |
!Behavior methodsFor:'creating an instance of myself'!
|
|
163 |
|
|
164 |
uninitializedNew
|
3
|
165 |
"same as new - only redefined in ByteArray"
|
1
|
166 |
|
|
167 |
^ self basicNew
|
|
168 |
!
|
|
169 |
|
|
170 |
uninitializedNew:anInteger
|
3
|
171 |
"same as new:anInteger - only redefined in ByteArray"
|
1
|
172 |
|
|
173 |
^ self basicNew:anInteger
|
|
174 |
!
|
|
175 |
|
|
176 |
new
|
|
177 |
"return an instance of myself without indexed variables"
|
|
178 |
|
|
179 |
^ self basicNew
|
|
180 |
!
|
|
181 |
|
|
182 |
new:anInteger
|
|
183 |
"return an instance of myself with anInteger indexed variables"
|
|
184 |
|
|
185 |
^ self basicNew:anInteger
|
|
186 |
!
|
|
187 |
|
|
188 |
basicNew
|
54
|
189 |
"return an instance of myself without indexed variables.
|
1
|
190 |
If the receiver-class has indexed instvars, the new object will have
|
54
|
191 |
a basicSize of zero - i.e. 'aClass basicNew' is equivalent to 'aClass basicNew:0'.
|
|
192 |
|
1
|
193 |
** Do not redefine this method in any class **"
|
|
194 |
|
|
195 |
%{ /* NOCONTEXT */
|
|
196 |
|
11
|
197 |
OBJ new();
|
1
|
198 |
OBJ newobj;
|
|
199 |
int instsize;
|
|
200 |
REGISTER int nInstVars;
|
|
201 |
|
54
|
202 |
/*
|
|
203 |
* the following ugly code is nothing more than a new() followed
|
|
204 |
* by a nilling of the new instance.
|
|
205 |
* unrolled for a bit more speed since this is one the central object allocation
|
|
206 |
* methods in the system
|
|
207 |
*/
|
1
|
208 |
nInstVars = _intVal(_INST(instSize));
|
|
209 |
instsize = OHDR_SIZE + nInstVars * sizeof(OBJ);
|
54
|
210 |
|
77
|
211 |
newobj = (OBJ) newNextPtr;
|
68
|
212 |
if (((char *)newobj + instsize) < newEndPtr) {
|
|
213 |
_objPtr(newobj)->o_size = instsize;
|
77
|
214 |
/* o_allFlags(newobj) = 0; */
|
|
215 |
/* _objPtr(newobj)->o_space = newSpace; */
|
|
216 |
o_setAllFlags(newobj, newSpace);
|
68
|
217 |
if (instsize & (ALIGN-1)) {
|
|
218 |
newNextPtr = (char *)newobj + (instsize & ~(ALIGN-1)) + ALIGN;
|
|
219 |
} else {
|
|
220 |
newNextPtr = (char *)newobj + instsize;
|
|
221 |
}
|
54
|
222 |
|
|
223 |
/*
|
68
|
224 |
* save nil-check in this case
|
|
225 |
* stupid: c-compilers should find this out themselfes ...
|
|
226 |
*/
|
25
|
227 |
goto ok;
|
11
|
228 |
} else {
|
|
229 |
PROTECT_CONTEXT
|
|
230 |
newobj = new(instsize, SENDER);
|
|
231 |
UNPROTECT_CONTEXT
|
|
232 |
}
|
25
|
233 |
|
2
|
234 |
if (newobj != nil) {
|
11
|
235 |
ok:
|
2
|
236 |
_InstPtr(newobj)->o_class = self;
|
1
|
237 |
|
2
|
238 |
if (nInstVars) {
|
1
|
239 |
#if defined(memset4)
|
2
|
240 |
memset4(_InstPtr(newobj)->i_instvars, nil, nInstVars);
|
1
|
241 |
#else
|
|
242 |
# if defined(FAST_MEMSET) && ! defined(NEGATIVE_ADDRESSES)
|
2
|
243 |
/*
|
|
244 |
* knowing that nil is 0
|
|
245 |
*/
|
|
246 |
memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE);
|
1
|
247 |
# else
|
68
|
248 |
REGISTER OBJ *op;
|
54
|
249 |
|
2
|
250 |
op = _InstPtr(newobj)->i_instvars;
|
54
|
251 |
# ifdef FAST_MEMSET_WORDS_UNROLLED
|
68
|
252 |
while (nInstVars > 8) {
|
|
253 |
*op = nil;
|
|
254 |
*(op+1) = nil;
|
|
255 |
*(op+2) = nil;
|
|
256 |
*(op+3) = nil;
|
|
257 |
*(op+4) = nil;
|
|
258 |
*(op+5) = nil;
|
|
259 |
*(op+6) = nil;
|
|
260 |
*(op+7) = nil;
|
|
261 |
op += 8;
|
|
262 |
nInstVars -= 8;
|
|
263 |
}
|
54
|
264 |
# endif
|
2
|
265 |
do {
|
|
266 |
*op++ = nil;
|
|
267 |
} while (--nInstVars);
|
1
|
268 |
# endif
|
|
269 |
#endif
|
2
|
270 |
}
|
|
271 |
RETURN ( newobj );
|
1
|
272 |
}
|
|
273 |
%}
|
2
|
274 |
.
|
68
|
275 |
"
|
|
276 |
memory allocation failed.
|
|
277 |
When we arrive here, there was no memory, even after
|
|
278 |
a garbage collect. This means, that the VM wanted to
|
|
279 |
get some more memory from the Operatingsystem, which
|
|
280 |
was not kind enough to give some
|
|
281 |
"
|
|
282 |
ObjectMemory allocationFailureSignal raise.
|
|
283 |
^ nil
|
1
|
284 |
!
|
|
285 |
|
|
286 |
basicNew:anInteger
|
|
287 |
"return an instance of myself with anInteger indexed variables.
|
|
288 |
If the receiver-class has no indexed instvars, this is only allowed
|
|
289 |
if the argument, anInteger is zero.
|
|
290 |
** Do not redefine this method in any class **"
|
|
291 |
|
|
292 |
%{ /* NOCONTEXT */
|
|
293 |
|
|
294 |
OBJ newobj;
|
|
295 |
INT instsize, nInstVars, nindexedinstvars;
|
|
296 |
INT flags;
|
|
297 |
#if ! defined(FAST_MEMSET) || defined(NEGATIVE_ADDRESSES)
|
|
298 |
REGISTER char *cp;
|
|
299 |
short *sp;
|
|
300 |
long *lp;
|
|
301 |
#endif
|
|
302 |
REGISTER OBJ *op;
|
|
303 |
float *fp;
|
|
304 |
double *dp;
|
|
305 |
extern OBJ new();
|
|
306 |
|
|
307 |
if (_isSmallInteger(anInteger)) {
|
|
308 |
nindexedinstvars = _intVal(anInteger);
|
|
309 |
if (nindexedinstvars >= 0) {
|
25
|
310 |
PROTECT_CONTEXT
|
1
|
311 |
nInstVars = _intVal(_INST(instSize));
|
|
312 |
flags = _intVal(_INST(flags)) & ARRAYMASK;
|
|
313 |
switch (flags) {
|
|
314 |
case BYTEARRAY:
|
|
315 |
instsize = OHDR_SIZE + nInstVars * sizeof(OBJ) + nindexedinstvars * sizeof(char);
|
|
316 |
_qNew(newobj, instsize, SENDER);
|
25
|
317 |
UNPROTECT_CONTEXT
|
3
|
318 |
if (newobj == nil) {
|
|
319 |
break;
|
|
320 |
}
|
1
|
321 |
_InstPtr(newobj)->o_class = self;
|
|
322 |
#if defined(FAST_MEMSET) && ! defined(NEGATIVE_ADDRESSES)
|
|
323 |
/*
|
|
324 |
* knowing that nil is 0
|
|
325 |
*/
|
|
326 |
memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE);
|
|
327 |
#else
|
|
328 |
op = _InstPtr(newobj)->i_instvars;
|
|
329 |
while (nInstVars--)
|
|
330 |
*op++ = nil;
|
|
331 |
cp = (char *)op;
|
|
332 |
while (nindexedinstvars >= sizeof(long)) {
|
|
333 |
*(long *)cp = 0;
|
2
|
334 |
cp += sizeof(long);
|
|
335 |
nindexedinstvars -= sizeof(long);
|
|
336 |
}
|
1
|
337 |
while (nindexedinstvars--)
|
|
338 |
*cp++ = '\0';
|
|
339 |
#endif
|
|
340 |
RETURN ( newobj );
|
|
341 |
break;
|
|
342 |
|
|
343 |
case WORDARRAY:
|
|
344 |
instsize = OHDR_SIZE + nInstVars * sizeof(OBJ) + nindexedinstvars * sizeof(short);
|
|
345 |
_qNew(newobj, instsize, SENDER);
|
25
|
346 |
UNPROTECT_CONTEXT
|
3
|
347 |
if (newobj == nil) {
|
|
348 |
break;
|
|
349 |
}
|
1
|
350 |
_InstPtr(newobj)->o_class = self;
|
|
351 |
#if defined(FAST_MEMSET) && ! defined(NEGATIVE_ADDRESSES)
|
|
352 |
/*
|
|
353 |
* knowing that nil is 0
|
|
354 |
*/
|
|
355 |
memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE);
|
|
356 |
#else
|
|
357 |
op = _InstPtr(newobj)->i_instvars;
|
|
358 |
while (nInstVars--)
|
|
359 |
*op++ = nil;
|
|
360 |
sp = (short *)op;
|
|
361 |
while (nindexedinstvars--)
|
|
362 |
*sp++ = 0;
|
|
363 |
#endif
|
|
364 |
RETURN ( newobj );
|
|
365 |
break;
|
|
366 |
|
|
367 |
case LONGARRAY:
|
|
368 |
instsize = OHDR_SIZE + nInstVars * sizeof(OBJ) + nindexedinstvars * sizeof(long);
|
2
|
369 |
_qAlignedNew(newobj, instsize, SENDER);
|
25
|
370 |
UNPROTECT_CONTEXT
|
3
|
371 |
if (newobj == nil) {
|
|
372 |
break;
|
|
373 |
}
|
1
|
374 |
_InstPtr(newobj)->o_class = self;
|
|
375 |
#if defined(memset4) && ! defined(NEGATIVE_ADDRESSES)
|
|
376 |
/*
|
|
377 |
* knowing that nil is 0
|
|
378 |
*/
|
|
379 |
memset4(_InstPtr(newobj)->i_instvars, 0, nInstVars + nindexedinstvars);
|
|
380 |
#else
|
|
381 |
# if defined(FAST_MEMSET) && ! defined(NEGATIVE_ADDRESSES)
|
|
382 |
/*
|
|
383 |
* knowing that nil is 0
|
|
384 |
*/
|
|
385 |
memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE);
|
|
386 |
# else
|
|
387 |
op = _InstPtr(newobj)->i_instvars;
|
|
388 |
while (nInstVars--)
|
|
389 |
*op++ = nil;
|
|
390 |
lp = (long *)op;
|
|
391 |
while (nindexedinstvars--)
|
|
392 |
*lp++ = 0;
|
|
393 |
# endif
|
|
394 |
#endif
|
|
395 |
RETURN ( newobj );
|
|
396 |
break;
|
|
397 |
|
|
398 |
case FLOATARRAY:
|
68
|
399 |
instsize = sizeof(struct floatArray) +
|
|
400 |
nInstVars * sizeof(OBJ) +
|
|
401 |
(nindexedinstvars - 1) * sizeof(float);
|
54
|
402 |
|
1
|
403 |
_qNew(newobj, instsize, SENDER);
|
25
|
404 |
UNPROTECT_CONTEXT
|
3
|
405 |
if (newobj == nil) {
|
|
406 |
break;
|
|
407 |
}
|
1
|
408 |
_InstPtr(newobj)->o_class = self;
|
|
409 |
op = _InstPtr(newobj)->i_instvars;
|
|
410 |
while (nInstVars--)
|
|
411 |
*op++ = nil;
|
|
412 |
fp = (float *)op;
|
|
413 |
while (nindexedinstvars--)
|
|
414 |
*fp++ = 0.0;
|
|
415 |
RETURN ( newobj );
|
|
416 |
break;
|
|
417 |
|
|
418 |
case DOUBLEARRAY:
|
68
|
419 |
instsize = sizeof(struct doubleArray) +
|
|
420 |
nInstVars * sizeof(OBJ) +
|
|
421 |
(nindexedinstvars - 1) * sizeof(double);
|
54
|
422 |
|
|
423 |
_qAlignedNew(newobj, instsize, SENDER);
|
25
|
424 |
UNPROTECT_CONTEXT
|
3
|
425 |
if (newobj == nil) {
|
|
426 |
break;
|
|
427 |
}
|
1
|
428 |
_InstPtr(newobj)->o_class = self;
|
|
429 |
op = _InstPtr(newobj)->i_instvars;
|
|
430 |
while (nInstVars--)
|
|
431 |
*op++ = nil;
|
54
|
432 |
#ifdef NEED_DOUBLE_ALIGN
|
68
|
433 |
/*
|
|
434 |
* care for double alignment
|
|
435 |
*/
|
|
436 |
if ((INT)op & (ALIGN-1)) {
|
|
437 |
*op++ = nil;
|
|
438 |
}
|
54
|
439 |
#endif
|
1
|
440 |
dp = (double *)op;
|
|
441 |
while (nindexedinstvars--)
|
|
442 |
*dp++ = 0.0;
|
|
443 |
RETURN ( newobj );
|
|
444 |
break;
|
|
445 |
|
|
446 |
case WKPOINTERARRAY:
|
|
447 |
case POINTERARRAY:
|
|
448 |
nInstVars += nindexedinstvars;
|
|
449 |
instsize = OHDR_SIZE + nInstVars * sizeof(OBJ);
|
|
450 |
_qAlignedNew(newobj, instsize, SENDER);
|
25
|
451 |
UNPROTECT_CONTEXT
|
3
|
452 |
if (newobj == nil) {
|
|
453 |
break;
|
|
454 |
}
|
1
|
455 |
_InstPtr(newobj)->o_class = self;
|
|
456 |
#if defined(memset4)
|
|
457 |
memset4(_InstPtr(newobj)->i_instvars, nil, nInstVars);
|
|
458 |
#else
|
|
459 |
# if defined(FAST_MEMSET) && ! defined(NEGATIVE_ADDRESSES)
|
|
460 |
/*
|
|
461 |
* knowing that nil is 0
|
|
462 |
*/
|
|
463 |
memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE);
|
|
464 |
# else
|
|
465 |
op = _InstPtr(newobj)->i_instvars;
|
|
466 |
while (nInstVars--)
|
|
467 |
*op++ = nil;
|
|
468 |
# endif
|
|
469 |
#endif
|
|
470 |
RETURN ( newobj );
|
|
471 |
break;
|
|
472 |
|
|
473 |
default:
|
|
474 |
/*
|
|
475 |
* new:n for non-variable classes only allowed if
|
|
476 |
* n == 0
|
|
477 |
*/
|
|
478 |
if (nindexedinstvars == 0) {
|
|
479 |
instsize = OHDR_SIZE + nInstVars * sizeof(OBJ);
|
|
480 |
_qAlignedNew(newobj, instsize, SENDER);
|
25
|
481 |
UNPROTECT_CONTEXT
|
3
|
482 |
if (newobj == nil) {
|
|
483 |
break;
|
|
484 |
}
|
1
|
485 |
_InstPtr(newobj)->o_class = self;
|
|
486 |
if (nInstVars) {
|
|
487 |
#if defined(memset4)
|
|
488 |
memset4(_InstPtr(newobj)->i_instvars, nil, nInstVars);
|
|
489 |
#else
|
|
490 |
# if defined(FAST_MEMSET) && ! defined(NEGATIVE_ADDRESSES)
|
|
491 |
/*
|
|
492 |
* knowing that nil is 0
|
|
493 |
*/
|
|
494 |
memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE);
|
|
495 |
# else
|
|
496 |
op = _InstPtr(newobj)->i_instvars;
|
|
497 |
do {
|
|
498 |
*op++ = nil;
|
|
499 |
} while (--nInstVars);
|
|
500 |
# endif
|
|
501 |
#endif
|
|
502 |
}
|
|
503 |
RETURN ( newobj );
|
|
504 |
}
|
|
505 |
break;
|
|
506 |
}
|
|
507 |
}
|
|
508 |
}
|
68
|
509 |
%}.
|
|
510 |
"
|
|
511 |
arrive here if something went wrong ...
|
|
512 |
figure out what it was
|
|
513 |
"
|
11
|
514 |
|
1
|
515 |
(anInteger isMemberOf:SmallInteger) ifFalse:[
|
68
|
516 |
self error:'argument to new: must be Integer'.
|
77
|
517 |
^ nil
|
68
|
518 |
].
|
|
519 |
(anInteger < 0) ifTrue:[
|
|
520 |
self error:'bad (negative) argument to new:'.
|
77
|
521 |
^ nil
|
68
|
522 |
].
|
|
523 |
self isVariable ifFalse:[
|
|
524 |
self error:'class has no indexed instvars - cannot create with new:'.
|
77
|
525 |
^ nil
|
68
|
526 |
].
|
|
527 |
"
|
|
528 |
memory allocation failed.
|
|
529 |
When we arrive here, there was no memory, even after
|
|
530 |
a garbage collect. This means, that the VM wanted to
|
|
531 |
get some more memory from the Operatingsystem, which
|
|
532 |
was not kind enough to give some
|
|
533 |
"
|
|
534 |
ObjectMemory allocationFailureSignal raise.
|
|
535 |
^ nil
|
11
|
536 |
!
|
|
537 |
|
|
538 |
readFrom:aStream
|
43
|
539 |
"read an objects printed representation from the argument, aStream
|
|
540 |
and return it.
|
68
|
541 |
|
11
|
542 |
The read object must be a kind of myself
|
43
|
543 |
- to get any object, use 'Object readFrom:...',
|
|
544 |
to get any number, use 'Number readFrom:...' and so on.
|
11
|
545 |
This is the reverse operation to 'storeOn:'.
|
43
|
546 |
|
68
|
547 |
WARNING: storeOn: does not handle circular references and multiple
|
|
548 |
references to the same object - use #storeBinary: for this."
|
11
|
549 |
|
|
550 |
|newObject|
|
|
551 |
|
68
|
552 |
newObject := self compiler evaluate:aStream.
|
11
|
553 |
(newObject isKindOf:self) ifFalse:[
|
|
554 |
self error:('expected ' , self name)
|
|
555 |
].
|
|
556 |
^ newObject
|
|
557 |
|
|
558 |
"|s|
|
|
559 |
s := WriteStream on:String new.
|
|
560 |
#(1 2 3 4) storeOn:s.
|
|
561 |
Object readFrom:(ReadStream on:s contents)
|
|
562 |
"
|
|
563 |
!
|
|
564 |
|
|
565 |
readFromString:aString
|
|
566 |
"create an object from its printed representation
|
68
|
567 |
(i.e. recreate what was stored using storeOn: or storeString).
|
|
568 |
See warning in Behavior>>readFrom:"
|
11
|
569 |
|
|
570 |
^ self readFrom:(ReadStream on:aString)
|
1
|
571 |
! !
|
|
572 |
|
10
|
573 |
!Behavior methodsFor:'autoload check'!
|
|
574 |
|
|
575 |
isLoaded
|
|
576 |
"return true, if the class has been loaded;
|
|
577 |
redefined in Autoload; see comment there"
|
|
578 |
|
|
579 |
^ true
|
|
580 |
!
|
|
581 |
|
|
582 |
autoload
|
|
583 |
"force autoloading - do nothing here;
|
|
584 |
redefined in Autoload; see comment there"
|
|
585 |
|
|
586 |
^ self
|
|
587 |
! !
|
|
588 |
|
68
|
589 |
!Behavior class methodsFor:'flag bit constants'!
|
|
590 |
|
|
591 |
flagNotIndexed
|
|
592 |
"return the flag code for non-indexed instances"
|
|
593 |
|
|
594 |
^ 0
|
|
595 |
!
|
|
596 |
|
|
597 |
flagBytes
|
|
598 |
"return the flag code for byte-valued indexed instances"
|
|
599 |
|
|
600 |
%{ /* NOCONTEXT */
|
|
601 |
/* this is defined as a primitive to get defines from stc.h */
|
|
602 |
|
|
603 |
RETURN ( _MKSMALLINT( BYTEARRAY ) );
|
|
604 |
%}
|
|
605 |
!
|
|
606 |
|
|
607 |
flagWords
|
|
608 |
"return the flag code for word-valued indexed instances (i.e. 2-byte)"
|
|
609 |
|
|
610 |
%{ /* NOCONTEXT */
|
|
611 |
/* this is defined as a primitive to get defines from stc.h */
|
|
612 |
|
|
613 |
RETURN ( _MKSMALLINT( WORDARRAY ) );
|
|
614 |
%}
|
|
615 |
!
|
|
616 |
|
|
617 |
flagLongs
|
|
618 |
"return the flag code for long-valued indexed instances (i.e. 4-byte)"
|
|
619 |
|
|
620 |
%{ /* NOCONTEXT */
|
|
621 |
/* this is defined as a primitive to get defines from stc.h */
|
|
622 |
|
|
623 |
RETURN ( _MKSMALLINT( LONGARRAY ) );
|
|
624 |
%}
|
|
625 |
!
|
|
626 |
|
|
627 |
flagFloats
|
|
628 |
"return the flag code for float-valued indexed instances (i.e. 4-byte reals)"
|
|
629 |
|
|
630 |
%{ /* NOCONTEXT */
|
|
631 |
/* this is defined as a primitive to get defines from stc.h */
|
|
632 |
|
|
633 |
RETURN ( _MKSMALLINT( FLOATARRAY ) );
|
|
634 |
%}
|
|
635 |
!
|
|
636 |
|
|
637 |
flagDoubles
|
|
638 |
"return the flag code for double-valued indexed instances (i.e. 8-byte reals)"
|
|
639 |
|
|
640 |
%{ /* NOCONTEXT */
|
|
641 |
/* this is defined as a primitive to get defines from stc.h */
|
|
642 |
|
|
643 |
RETURN ( _MKSMALLINT( DOUBLEARRAY ) );
|
|
644 |
%}
|
|
645 |
!
|
|
646 |
|
|
647 |
flagPointers
|
|
648 |
"return the flag code for pointer indexed instances (i.e. Array of object)"
|
|
649 |
|
|
650 |
%{ /* NOCONTEXT */
|
|
651 |
/* this is defined as a primitive to get defines from stc.h */
|
|
652 |
|
|
653 |
RETURN ( _MKSMALLINT( POINTERARRAY ) );
|
|
654 |
%}
|
|
655 |
!
|
|
656 |
|
|
657 |
maskIndexType
|
|
658 |
"return a mask to extract all index-type bits"
|
|
659 |
|
|
660 |
%{ /* NOCONTEXT */
|
|
661 |
/* this is defined as a primitive to get defines from stc.h */
|
|
662 |
|
|
663 |
RETURN ( _MKSMALLINT( ARRAYMASK ) );
|
|
664 |
%}
|
|
665 |
!
|
|
666 |
|
|
667 |
flagBlock
|
|
668 |
"return the flag code which marks Block-type instances"
|
|
669 |
|
|
670 |
%{ /* NOCONTEXT */
|
|
671 |
/* this is defined as a primitive to get defines from stc.h */
|
|
672 |
|
|
673 |
RETURN ( _MKSMALLINT( BLOCK_INSTS ) );
|
|
674 |
%}
|
|
675 |
!
|
|
676 |
|
|
677 |
flagMethod
|
|
678 |
"return the flag code which marks Method-type instances"
|
|
679 |
|
|
680 |
%{ /* NOCONTEXT */
|
|
681 |
/* this is defined as a primitive to get defines from stc.h */
|
|
682 |
|
|
683 |
RETURN ( _MKSMALLINT( METHOD_INSTS ) );
|
|
684 |
%}
|
|
685 |
!
|
|
686 |
|
|
687 |
flagContext
|
|
688 |
"return the flag code which marks Context-type instances"
|
|
689 |
|
|
690 |
%{ /* NOCONTEXT */
|
|
691 |
/* this is defined as a primitive to get defines from stc.h */
|
|
692 |
|
|
693 |
RETURN ( _MKSMALLINT( CONTEXT_INSTS ) );
|
|
694 |
%}
|
|
695 |
!
|
|
696 |
|
|
697 |
flagBlockContext
|
|
698 |
"return the flag code which marks BlockContext-type instances"
|
|
699 |
|
|
700 |
%{ /* NOCONTEXT */
|
|
701 |
/* this is defined as a primitive to get defines from stc.h */
|
|
702 |
|
|
703 |
RETURN ( _MKSMALLINT( BCONTEXT_INSTS ) );
|
|
704 |
%}
|
|
705 |
! !
|
|
706 |
|
1
|
707 |
!Behavior methodsFor:'accessing'!
|
|
708 |
|
|
709 |
superclass
|
|
710 |
"return the receivers superclass"
|
|
711 |
|
|
712 |
^ superclass
|
|
713 |
!
|
|
714 |
|
68
|
715 |
selectorArray
|
|
716 |
"return the receivers selector array.
|
|
717 |
Notice: this is not compatible with ST-80."
|
1
|
718 |
|
|
719 |
^ selectors
|
|
720 |
!
|
|
721 |
|
68
|
722 |
methodArray
|
|
723 |
"return the receivers method array.
|
|
724 |
Notice: this is not compatible with ST-80."
|
1
|
725 |
|
|
726 |
^ methods
|
|
727 |
!
|
|
728 |
|
|
729 |
methodDictionary
|
77
|
730 |
"return the receivers method dictionary.
|
|
731 |
Since no dictionary is actually present, create one for ST-80 compatibility."
|
68
|
732 |
|
|
733 |
|dict|
|
1
|
734 |
|
68
|
735 |
dict := IdentityDictionary new.
|
|
736 |
1 to:selectors size do:[:index |
|
|
737 |
dict at:(selectors at:index) put:(methods at:index)
|
|
738 |
].
|
|
739 |
^ dict
|
1
|
740 |
!
|
|
741 |
|
|
742 |
instSize
|
68
|
743 |
"return the number of instance variables of the receiver.
|
|
744 |
This includes all superclass instance variables."
|
1
|
745 |
|
|
746 |
^ instSize
|
|
747 |
!
|
|
748 |
|
|
749 |
flags
|
|
750 |
"return the receivers flag bits"
|
|
751 |
|
|
752 |
^ flags
|
|
753 |
!
|
|
754 |
|
|
755 |
isVariable
|
|
756 |
"return true, if instances have indexed instance variables"
|
|
757 |
|
68
|
758 |
"this could be defined as:
|
|
759 |
^ (flags bitAnd:(Behavior maskIndexType)) ~~ 0
|
|
760 |
"
|
1
|
761 |
|
|
762 |
%{ /* NOCONTEXT */
|
|
763 |
|
|
764 |
RETURN ( (_intVal(_INST(flags)) & ARRAYMASK) ? true : false );
|
|
765 |
%}
|
|
766 |
!
|
|
767 |
|
|
768 |
isFixed
|
|
769 |
"return true, if instances do not have indexed instance variables"
|
|
770 |
|
68
|
771 |
"this could be defined as:
|
|
772 |
^ self isVariable not
|
|
773 |
"
|
|
774 |
|
1
|
775 |
%{ /* NOCONTEXT */
|
|
776 |
|
|
777 |
RETURN ( (_intVal(_INST(flags)) & ARRAYMASK) ? false : true );
|
|
778 |
%}
|
|
779 |
!
|
|
780 |
|
|
781 |
isBits
|
|
782 |
"return true, if instances have indexed byte or short instance variables.
|
68
|
783 |
Ignore long, float and double arrays, since ST-80 code using isBits are probably
|
1
|
784 |
not prepared to handle them correctly."
|
|
785 |
|
|
786 |
%{ /* NOCONTEXT */
|
|
787 |
|
|
788 |
REGISTER int flags;
|
|
789 |
|
|
790 |
RETURN ( (((flags = (_intVal(_INST(flags)) & ARRAYMASK)) == BYTEARRAY)
|
2
|
791 |
|| (flags == WORDARRAY)) ? true : false );
|
1
|
792 |
%}
|
|
793 |
!
|
|
794 |
|
|
795 |
isBytes
|
|
796 |
"return true, if instances have indexed byte instance variables"
|
|
797 |
|
68
|
798 |
"this could be defined as:
|
|
799 |
^ (flags bitAnd:(Behavior maskIndexType)) == Behavior flagBytes
|
|
800 |
"
|
1
|
801 |
%{ /* NOCONTEXT */
|
|
802 |
|
|
803 |
RETURN ( ((_intVal(_INST(flags)) & ARRAYMASK) == BYTEARRAY) ? true : false );
|
|
804 |
%}
|
|
805 |
!
|
|
806 |
|
|
807 |
isWords
|
|
808 |
"return true, if instances have indexed short instance variables"
|
|
809 |
|
68
|
810 |
"this could be defined as:
|
|
811 |
^ (flags bitAnd:(Behavior maskIndexType)) == Behavior flagWords
|
|
812 |
"
|
1
|
813 |
%{ /* NOCONTEXT */
|
|
814 |
|
|
815 |
RETURN ( ((_intVal(_INST(flags)) & ARRAYMASK) == WORDARRAY) ? true : false );
|
|
816 |
%}
|
|
817 |
!
|
|
818 |
|
|
819 |
isLongs
|
|
820 |
"return true, if instances have indexed long instance variables"
|
|
821 |
|
68
|
822 |
"this could be defined as:
|
|
823 |
^ (flags bitAnd:(Behavior maskIndexType)) == Behavior flagLongs
|
|
824 |
"
|
1
|
825 |
%{ /* NOCONTEXT */
|
|
826 |
|
|
827 |
RETURN ( ((_intVal(_INST(flags)) & ARRAYMASK) == LONGARRAY) ? true : false );
|
|
828 |
%}
|
|
829 |
!
|
|
830 |
|
|
831 |
isFloats
|
|
832 |
"return true, if instances have indexed float instance variables"
|
|
833 |
|
68
|
834 |
"this could be defined as:
|
|
835 |
^ (flags bitAnd:(Behavior maskIndexType)) == Behavior flagFloats
|
|
836 |
"
|
1
|
837 |
%{ /* NOCONTEXT */
|
68
|
838 |
|
1
|
839 |
RETURN ( ((_intVal(_INST(flags)) & ARRAYMASK) == FLOATARRAY) ? true : false );
|
|
840 |
%}
|
|
841 |
!
|
|
842 |
|
|
843 |
isDoubles
|
|
844 |
"return true, if instances have indexed double instance variables"
|
|
845 |
|
68
|
846 |
"this could be defined as:
|
|
847 |
^ (flags bitAnd:(Behavior maskIndexType)) == Behavior flagDoubles
|
|
848 |
"
|
1
|
849 |
%{ /* NOCONTEXT */
|
68
|
850 |
|
1
|
851 |
RETURN ( ((_intVal(_INST(flags)) & ARRAYMASK) == DOUBLEARRAY) ? true : false );
|
|
852 |
%}
|
|
853 |
!
|
|
854 |
|
|
855 |
isPointers
|
2
|
856 |
"return true, if instances have pointer instance variables
|
|
857 |
i.e. are either non-indexed or have indexed pointer variables"
|
1
|
858 |
|
68
|
859 |
"QUESTION: should we ignore WeakPointers ?"
|
|
860 |
|
1
|
861 |
%{ /* NOCONTEXT */
|
68
|
862 |
|
1
|
863 |
REGISTER int flags;
|
|
864 |
|
2
|
865 |
flags = _intVal(_INST(flags)) & ARRAYMASK;
|
|
866 |
switch (flags) {
|
|
867 |
default:
|
|
868 |
/* normal objects */
|
|
869 |
RETURN ( true );
|
|
870 |
|
|
871 |
case BYTEARRAY:
|
|
872 |
case WORDARRAY:
|
|
873 |
case LONGARRAY:
|
|
874 |
case FLOATARRAY:
|
|
875 |
case DOUBLEARRAY:
|
|
876 |
RETURN (false );
|
|
877 |
|
|
878 |
case WKPOINTERARRAY:
|
|
879 |
/* what about those ? */
|
|
880 |
RETURN (true );
|
|
881 |
}
|
1
|
882 |
%}
|
2
|
883 |
!
|
|
884 |
|
|
885 |
superclass:aClass
|
|
886 |
"set the superclass - this actually creates a new class,
|
68
|
887 |
recompiling all methods for the new one. The receiving class stays
|
|
888 |
around anonymous to allow existing instances some life.
|
|
889 |
This may change in the future (adjusting existing instances)"
|
2
|
890 |
|
|
891 |
"must flush caches since lookup chain changes"
|
|
892 |
ObjectMemory flushCaches.
|
10
|
893 |
|
2
|
894 |
"
|
|
895 |
superclass := aClass
|
|
896 |
"
|
|
897 |
"for correct recompilation, just create a new class ..."
|
|
898 |
|
|
899 |
aClass subclass:(self name)
|
|
900 |
instanceVariableNames:(self instanceVariableString)
|
|
901 |
classVariableNames:(self classVariableString)
|
|
902 |
poolDictionaries:''
|
|
903 |
category:self category
|
|
904 |
!
|
|
905 |
|
68
|
906 |
addSuperclass:aClass
|
|
907 |
"EXPERIMENTAL: add aClass to the set of classes, from which instances
|
|
908 |
inherit protocol."
|
|
909 |
|
|
910 |
"first, check if the class is abstract -
|
|
911 |
allows abstract mixins are allowed in the current implementation"
|
|
912 |
|
|
913 |
aClass instSize == 0 ifFalse:[
|
|
914 |
self error:'only abstract mixins allowed'.
|
|
915 |
^ self
|
|
916 |
].
|
|
917 |
otherSuperclasses isNil ifTrue:[
|
|
918 |
otherSuperclasses := Array with:aClass
|
|
919 |
] ifFalse:[
|
|
920 |
otherSuperclasses := otherSuperclasses copyWith:aClass
|
|
921 |
].
|
|
922 |
ObjectMemory flushCaches
|
|
923 |
!
|
|
924 |
|
|
925 |
removeSuperclass:aClass
|
|
926 |
"EXPERIMENTAL: add aClass to the set of classes, from which instances
|
|
927 |
inherit protocol."
|
|
928 |
|
|
929 |
otherSuperclasses notNil ifTrue:[
|
|
930 |
otherSuperclasses := otherSuperclasses copyWithout:aClass.
|
|
931 |
otherSuperclasses isEmpty ifTrue:[
|
|
932 |
otherSuperclasses := nil
|
|
933 |
].
|
|
934 |
ObjectMemory flushCaches
|
|
935 |
].
|
|
936 |
!
|
|
937 |
|
2
|
938 |
selectors:selectorArray methods:methodArray
|
|
939 |
"set both selector array and method array of the receiver,
|
|
940 |
and flush caches"
|
|
941 |
|
|
942 |
ObjectMemory flushCaches.
|
|
943 |
selectors := selectorArray.
|
|
944 |
methods := methodArray
|
1
|
945 |
! !
|
|
946 |
|
|
947 |
!Behavior methodsFor:'queries'!
|
|
948 |
|
|
949 |
isBehavior
|
47
|
950 |
"return true, if the receiver is describing another objects behavior,
|
45
|
951 |
i.e. is a class. Defined to avoid the need to use isKindOf:"
|
1
|
952 |
|
|
953 |
^ true
|
45
|
954 |
|
|
955 |
"True isBehavior"
|
|
956 |
"true isBehavior"
|
1
|
957 |
!
|
|
958 |
|
47
|
959 |
canBeSubclassed
|
|
960 |
"return true, if its allowed to create subclasses of the receiver.
|
|
961 |
This method is redefined in SmallInteger and UndefinedObject, since
|
68
|
962 |
instances are detected by their pointer-fields, i.e. they do not have
|
|
963 |
a class entry (you dont have to understand this :-)"
|
47
|
964 |
|
|
965 |
^ true
|
|
966 |
!
|
|
967 |
|
1
|
968 |
hasMultipleSuperclasses
|
68
|
969 |
"Return true, if this class inherits from other classes
|
|
970 |
(beside its primary superclass).
|
|
971 |
This method is a preparation for a future multiple inheritance extension
|
|
972 |
- currently it is not supported by the VM"
|
1
|
973 |
|
68
|
974 |
^ otherSuperclasses notNil
|
1
|
975 |
!
|
|
976 |
|
|
977 |
superclasses
|
68
|
978 |
"return a collection of the receivers immediate superclasses.
|
|
979 |
This method is a preparation for a future multiple inheritance extension
|
|
980 |
- currently it is not supported by the VM"
|
1
|
981 |
|
68
|
982 |
otherSuperclasses notNil ifTrue:[
|
|
983 |
^ (Array with:superclass) , otherSuperclasses
|
|
984 |
].
|
1
|
985 |
^ Array with:superclass
|
45
|
986 |
|
|
987 |
"String superclasses"
|
1
|
988 |
!
|
|
989 |
|
|
990 |
allSuperclasses
|
|
991 |
"return a collection of the receivers accumulated superclasses"
|
|
992 |
|
|
993 |
|aCollection theSuperClass|
|
|
994 |
|
|
995 |
theSuperClass := superclass.
|
|
996 |
theSuperClass notNil ifTrue:[
|
|
997 |
aCollection := OrderedCollection new.
|
|
998 |
[theSuperClass notNil] whileTrue:[
|
|
999 |
aCollection add:theSuperClass.
|
|
1000 |
theSuperClass := theSuperClass superclass
|
|
1001 |
]
|
|
1002 |
].
|
|
1003 |
^ aCollection
|
45
|
1004 |
|
|
1005 |
"String allSuperclasses"
|
1
|
1006 |
!
|
|
1007 |
|
|
1008 |
withAllSuperclasses
|
|
1009 |
"return a collection containing the receiver and all
|
|
1010 |
of the receivers accumulated superclasses"
|
|
1011 |
|
|
1012 |
|aCollection theSuperClass|
|
|
1013 |
|
|
1014 |
aCollection := OrderedCollection with:self.
|
|
1015 |
theSuperClass := superclass.
|
|
1016 |
[theSuperClass notNil] whileTrue:[
|
|
1017 |
aCollection add:theSuperClass.
|
|
1018 |
theSuperClass := theSuperClass superclass
|
|
1019 |
].
|
|
1020 |
^ aCollection
|
45
|
1021 |
|
|
1022 |
"String withAllSuperclasses"
|
1
|
1023 |
!
|
|
1024 |
|
|
1025 |
subclasses
|
|
1026 |
"return a collection of the direct subclasses of the receiver"
|
|
1027 |
|
|
1028 |
|newColl|
|
|
1029 |
|
|
1030 |
newColl := OrderedCollection new.
|
|
1031 |
self subclassesDo:[:aClass |
|
|
1032 |
newColl add:aClass
|
|
1033 |
].
|
|
1034 |
^ newColl
|
45
|
1035 |
|
|
1036 |
"Collection subclasses"
|
1
|
1037 |
!
|
|
1038 |
|
|
1039 |
allSubclasses
|
|
1040 |
"return a collection of all subclasses (direct AND indirect) of
|
45
|
1041 |
the receiver. There will be no specific order, in which entries
|
|
1042 |
are returned."
|
1
|
1043 |
|
|
1044 |
|newColl|
|
|
1045 |
|
|
1046 |
newColl := OrderedCollection new.
|
|
1047 |
self allSubclassesDo:[:aClass |
|
|
1048 |
newColl add:aClass
|
|
1049 |
].
|
|
1050 |
^ newColl
|
45
|
1051 |
|
|
1052 |
"Collection allSubclasses"
|
|
1053 |
!
|
|
1054 |
|
|
1055 |
allSubclassesInOrder
|
|
1056 |
"return a collection of all subclasses (direct AND indirect) of
|
|
1057 |
the receiver. Higher level subclasses will come before lower ones."
|
|
1058 |
|
|
1059 |
|newColl|
|
|
1060 |
|
|
1061 |
newColl := OrderedCollection new.
|
|
1062 |
self allSubclassesInOrderDo:[:aClass |
|
|
1063 |
newColl add:aClass
|
|
1064 |
].
|
|
1065 |
^ newColl
|
|
1066 |
|
|
1067 |
"Collection allSubclassesInOrder"
|
1
|
1068 |
!
|
|
1069 |
|
|
1070 |
withAllSubclasses
|
|
1071 |
"return a collection containing the receiver and
|
|
1072 |
all subclasses (direct AND indirect) of the receiver"
|
|
1073 |
|
|
1074 |
|newColl|
|
|
1075 |
|
|
1076 |
newColl := OrderedCollection with:self.
|
|
1077 |
self allSubclassesDo:[:aClass |
|
|
1078 |
newColl add:aClass
|
|
1079 |
].
|
|
1080 |
^ newColl
|
45
|
1081 |
|
|
1082 |
"Collection withAllSubclasses"
|
1
|
1083 |
!
|
|
1084 |
|
|
1085 |
isSubclassOf:aClass
|
|
1086 |
"return true, if I am a subclass of the argument, aClass"
|
|
1087 |
|
|
1088 |
|theClass|
|
2
|
1089 |
|
1
|
1090 |
theClass := superclass.
|
|
1091 |
[theClass notNil] whileTrue:[
|
|
1092 |
(theClass == aClass) ifTrue:[^ true].
|
|
1093 |
theClass := theClass superclass
|
|
1094 |
].
|
|
1095 |
^ false
|
45
|
1096 |
|
|
1097 |
"String isSubclassOf:Collection"
|
|
1098 |
"LinkedList isSubclassOf:Array"
|
47
|
1099 |
"1 isSubclassOf:Number" "will fail since 1 is no class"
|
1
|
1100 |
!
|
|
1101 |
|
|
1102 |
allInstances
|
|
1103 |
"return a collection of all my instances"
|
|
1104 |
|
|
1105 |
|coll|
|
|
1106 |
|
|
1107 |
coll := OrderedCollection new.
|
45
|
1108 |
self allInstancesDo:[:anObject |
|
|
1109 |
coll add:anObject
|
1
|
1110 |
].
|
68
|
1111 |
^ coll
|
25
|
1112 |
|
|
1113 |
"ScrollBar allInstances"
|
|
1114 |
!
|
|
1115 |
|
|
1116 |
allDerivedInstances
|
|
1117 |
"return a collection of all instances of myself and
|
|
1118 |
instances of all subclasses of myself"
|
|
1119 |
|
|
1120 |
|coll|
|
|
1121 |
|
|
1122 |
coll := OrderedCollection new.
|
45
|
1123 |
self allDerivedInstancesDo:[:anObject |
|
25
|
1124 |
(anObject isKindOf:self) ifTrue:[
|
|
1125 |
coll add:anObject
|
|
1126 |
]
|
|
1127 |
].
|
68
|
1128 |
^ coll
|
25
|
1129 |
|
|
1130 |
"View allDerivedInstances"
|
1
|
1131 |
!
|
|
1132 |
|
|
1133 |
instanceCount
|
|
1134 |
"return the number of instances of myself"
|
|
1135 |
|
|
1136 |
|count|
|
|
1137 |
|
|
1138 |
count := 0.
|
|
1139 |
ObjectMemory allObjectsDo:[:anObject |
|
|
1140 |
(anObject class == self) ifTrue:[
|
|
1141 |
count := count + 1
|
|
1142 |
]
|
|
1143 |
].
|
|
1144 |
^ count
|
45
|
1145 |
|
|
1146 |
"View instanceCount"
|
|
1147 |
!
|
|
1148 |
|
|
1149 |
derivedInstanceCount
|
|
1150 |
"return the number of instances of myself and of subclasses"
|
|
1151 |
|
|
1152 |
|count|
|
|
1153 |
|
|
1154 |
count := 0.
|
|
1155 |
ObjectMemory allObjectsDo:[:anObject |
|
|
1156 |
(anObject isKindOf:self) ifTrue:[
|
|
1157 |
count := count + 1
|
|
1158 |
]
|
|
1159 |
].
|
|
1160 |
^ count
|
|
1161 |
|
|
1162 |
"View derivedInstanceCount"
|
2
|
1163 |
!
|
|
1164 |
|
|
1165 |
selectorIndex:aSelector
|
|
1166 |
"return the index in the arrays for given selector aSelector"
|
|
1167 |
|
|
1168 |
^ selectors identityIndexOf:aSelector startingAt:1
|
|
1169 |
!
|
|
1170 |
|
|
1171 |
compiledMethodAt:aSelector
|
68
|
1172 |
"return the method for given selector aSelector or nil.
|
|
1173 |
Only methods in the receiver - not in the superclass chain are tested."
|
2
|
1174 |
|
|
1175 |
|index|
|
|
1176 |
|
|
1177 |
index := selectors identityIndexOf:aSelector startingAt:1.
|
|
1178 |
(index == 0) ifTrue:[^ nil].
|
|
1179 |
^ methods at:index
|
45
|
1180 |
|
|
1181 |
"Object compiledMethodAt:#=="
|
|
1182 |
"(Object compiledMethodAt:#==) category"
|
2
|
1183 |
!
|
|
1184 |
|
|
1185 |
sourceCodeAt:aSelector
|
68
|
1186 |
"return the methods source for given selector aSelector or nil.
|
|
1187 |
Only methods in the receiver - not in the superclass chain are tested."
|
2
|
1188 |
|
3
|
1189 |
|method|
|
2
|
1190 |
|
3
|
1191 |
method := self compiledMethodAt:aSelector.
|
|
1192 |
method isNil ifTrue:[^ nil].
|
|
1193 |
^ method source
|
2
|
1194 |
|
|
1195 |
"True sourceCodeAt:#ifTrue:"
|
45
|
1196 |
"Object sourceCodeAt:#=="
|
|
1197 |
"Behavior sourceCodeAt:#sourceCodeAt:"
|
2
|
1198 |
!
|
|
1199 |
|
68
|
1200 |
lookupMethodFor:aSelector
|
|
1201 |
"return the method, which would be executed if aSelector was sent to
|
|
1202 |
an instance of the receiver. I.e. the selector arrays of the receiver
|
|
1203 |
and all of its superclasses are searched for aSelector.
|
|
1204 |
Return the method, or nil if instances do not understand aSelector.
|
|
1205 |
EXPERIMENTAL: take care of multiple superclasses."
|
|
1206 |
|
|
1207 |
|m cls|
|
|
1208 |
|
|
1209 |
cls := self.
|
|
1210 |
[cls notNil] whileTrue:[
|
|
1211 |
m := cls compiledMethodAt:aSelector.
|
|
1212 |
m notNil ifTrue:[^ m].
|
|
1213 |
cls hasMultipleSuperclasses ifTrue:[
|
|
1214 |
cls superclasses do:[:aSuperClass |
|
|
1215 |
m := aSuperClass lookupMethodFor:aSelector.
|
|
1216 |
m notNil ifTrue:[^ m].
|
|
1217 |
].
|
|
1218 |
^ nil
|
|
1219 |
] ifFalse:[
|
|
1220 |
cls := cls superclass
|
|
1221 |
]
|
|
1222 |
].
|
|
1223 |
^ nil
|
|
1224 |
!
|
|
1225 |
|
|
1226 |
cachedLookupMethodFor:aSelector
|
|
1227 |
"return the method, which would be executed if aSelector was sent to
|
|
1228 |
an instance of the receiver. I.e. the selector arrays of the receiver
|
|
1229 |
and all of its superclasses are searched for aSelector.
|
|
1230 |
Return the method, or nil if instances do not understand aSelector.
|
|
1231 |
This interface provides exactly the same information as #lookupMethodFor:,
|
|
1232 |
but uses the lookup-cache in the VM for faster search.
|
|
1233 |
However, keep in mind, that doing a lookup through the cache also adds new
|
|
1234 |
entries and can thus slow down the system by polluting the cache with
|
|
1235 |
irrelevant entries. (do NOT loop over all objects calling this method).
|
|
1236 |
Does NOT (currently) handle MI"
|
|
1237 |
|
|
1238 |
%{ /* NOCONTEXT */
|
|
1239 |
extern OBJ lookup();
|
|
1240 |
|
|
1241 |
RETURN ( lookup(self, aSelector, SENDER) );
|
|
1242 |
%}
|
|
1243 |
|
|
1244 |
"String cachedLookupMethodFor:#="
|
|
1245 |
"String cachedLookupMethodFor:#asOrderedCollection"
|
|
1246 |
!
|
|
1247 |
|
2
|
1248 |
hasMethods
|
|
1249 |
"return true, if there are any (local) methods in this class"
|
|
1250 |
|
|
1251 |
^ (methods size ~~ 0)
|
10
|
1252 |
|
|
1253 |
"True hasMethods"
|
|
1254 |
"True class hasMethods"
|
2
|
1255 |
!
|
|
1256 |
|
|
1257 |
implements:aSelector
|
68
|
1258 |
"return true, if the receiver implements aSelector.
|
|
1259 |
(i.e. implemented in THIS class - NOT in a superclass).
|
10
|
1260 |
Dont use this method to check if someone responds to a message -
|
45
|
1261 |
use #canUnderstand: on the class or #respondsTo: on the instance
|
10
|
1262 |
to do this."
|
2
|
1263 |
|
54
|
1264 |
^ (selectors identityIndexOf:aSelector startingAt:1) ~~ 0
|
2
|
1265 |
|
|
1266 |
"True implements:#ifTrue:"
|
|
1267 |
"True implements:#=="
|
|
1268 |
!
|
|
1269 |
|
|
1270 |
canUnderstand:aSelector
|
68
|
1271 |
"return true, if the receiver or one of its superclasses implements aSelector.
|
|
1272 |
(i.e. true if my instances understand aSelector)"
|
2
|
1273 |
|
68
|
1274 |
^ (self lookupMethodFor:aSelector) notNil
|
10
|
1275 |
|
|
1276 |
"True canUnderstand:#ifTrue:"
|
|
1277 |
"True canUnderstand:#=="
|
|
1278 |
"True canUnderstand:#do:"
|
2
|
1279 |
!
|
|
1280 |
|
|
1281 |
whichClassImplements:aSelector
|
68
|
1282 |
"return the class in the inheritance chain, which implements the method
|
|
1283 |
for aSelector; return nil if none.
|
|
1284 |
EXPERIMENTAL: handle multiple superclasses"
|
|
1285 |
|
|
1286 |
|cls|
|
2
|
1287 |
|
68
|
1288 |
cls := self.
|
|
1289 |
[cls notNil] whileTrue:[
|
|
1290 |
(cls implements:aSelector) ifTrue:[^ cls].
|
|
1291 |
cls hasMultipleSuperclasses ifTrue:[
|
|
1292 |
cls superclasses do:[:aSuperClass |
|
|
1293 |
|implementingClass|
|
2
|
1294 |
|
68
|
1295 |
implementingClass := aSuperClass whichClassImplements:aSelector.
|
|
1296 |
implementingClass notNil ifTrue:[^ implementingClass].
|
|
1297 |
].
|
|
1298 |
^ nil
|
|
1299 |
] ifFalse:[
|
|
1300 |
cls := cls superclass
|
|
1301 |
]
|
2
|
1302 |
].
|
|
1303 |
^ nil
|
|
1304 |
|
68
|
1305 |
"String whichClassImplements:#=="
|
|
1306 |
"String whichClassImplements:#collect:"
|
2
|
1307 |
!
|
|
1308 |
|
|
1309 |
inheritsFrom:aClass
|
68
|
1310 |
"return true, if the receiver inherits methods from aClass"
|
2
|
1311 |
|
|
1312 |
^ self isSubclassOf:aClass
|
45
|
1313 |
|
|
1314 |
"True inheritsFrom:Object"
|
|
1315 |
"LinkedList inheritsFrom:Array"
|
2
|
1316 |
!
|
|
1317 |
|
|
1318 |
selectorForMethod:aMethod
|
47
|
1319 |
"Return the selector for given method aMethod."
|
2
|
1320 |
|
|
1321 |
|index|
|
|
1322 |
|
54
|
1323 |
index := methods identityIndexOf:aMethod startingAt:1.
|
2
|
1324 |
(index == 0) ifTrue:[^ nil].
|
|
1325 |
^ selectors at:index
|
|
1326 |
!
|
|
1327 |
|
|
1328 |
containsMethod:aMethod
|
77
|
1329 |
"Return true, if the argument, aMethod is a method of myself"
|
2
|
1330 |
|
54
|
1331 |
^ (methods identityIndexOf:aMethod startingAt:1) ~~ 0
|
1
|
1332 |
! !
|
|
1333 |
|
|
1334 |
!Behavior methodsFor:'private accessing'!
|
|
1335 |
|
|
1336 |
setSuperclass:sup selectors:sels methods:m instSize:i flags:f
|
45
|
1337 |
"set some inst vars.
|
77
|
1338 |
this method is for special uses only - there will be no recompilation
|
68
|
1339 |
and no change record written here;
|
|
1340 |
Do NOT use it."
|
1
|
1341 |
|
|
1342 |
superclass := sup.
|
|
1343 |
selectors := sels.
|
|
1344 |
methods := m.
|
|
1345 |
instSize := i.
|
|
1346 |
flags := f
|
|
1347 |
!
|
|
1348 |
|
|
1349 |
setSuperclass:aClass
|
45
|
1350 |
"set the superclass of the receiver.
|
77
|
1351 |
this method is for special uses only - there will be no recompilation
|
68
|
1352 |
and no change record written here;
|
|
1353 |
Do NOT use it."
|
1
|
1354 |
|
|
1355 |
superclass := aClass
|
|
1356 |
!
|
|
1357 |
|
68
|
1358 |
setOtherSuperclasses:anArrayOfClasses
|
|
1359 |
"EXPERIMENTAL: set the other superclasses of the receiver.
|
77
|
1360 |
this method is for special uses only - there will be no recompilation
|
68
|
1361 |
and no change record written here;
|
|
1362 |
Do NOT use it."
|
|
1363 |
|
|
1364 |
otherSuperclasses := anArrayOfClasses
|
|
1365 |
!
|
|
1366 |
|
1
|
1367 |
instSize:aNumber
|
45
|
1368 |
"set the instance size.
|
77
|
1369 |
this method is for special uses only - there will be no recompilation
|
68
|
1370 |
and no change record written here;
|
|
1371 |
Do NOT use it."
|
1
|
1372 |
|
|
1373 |
instSize := aNumber
|
|
1374 |
!
|
|
1375 |
|
|
1376 |
flags:aNumber
|
45
|
1377 |
"set the flags.
|
77
|
1378 |
this method is for special uses only - there will be no recompilation
|
68
|
1379 |
and no change record written here;
|
|
1380 |
Do NOT use it."
|
1
|
1381 |
|
|
1382 |
flags := aNumber
|
|
1383 |
!
|
|
1384 |
|
77
|
1385 |
setSelectorArray:anArray
|
45
|
1386 |
"set the selector array of the receiver.
|
77
|
1387 |
this method is for special uses only - there will be no recompilation
|
|
1388 |
and no change record written here.
|
|
1389 |
NOT for general use."
|
1
|
1390 |
|
|
1391 |
selectors := anArray
|
|
1392 |
!
|
|
1393 |
|
77
|
1394 |
setMethodArray:anArray
|
45
|
1395 |
"set the method array of the receiver.
|
77
|
1396 |
this method is for special uses only - there will be no recompilation
|
|
1397 |
and no change record written here.
|
|
1398 |
NOT for general use."
|
1
|
1399 |
|
|
1400 |
methods := anArray
|
77
|
1401 |
!
|
|
1402 |
|
|
1403 |
setMethodDictionary:aDictionary
|
|
1404 |
"set the receivers method dictionary.
|
|
1405 |
Since no dictionary is actually used, decompose into selector- and
|
|
1406 |
method arrays and set those. For ST-80 compatibility.
|
|
1407 |
NOT for general use."
|
|
1408 |
|
|
1409 |
|n selArray methodArray idx|
|
|
1410 |
|
|
1411 |
n := aDictionary size.
|
|
1412 |
selArray := Array new:n.
|
|
1413 |
methodArray := Array new:n.
|
|
1414 |
idx := 1.
|
|
1415 |
aDictionary keysAndValuesDo:[:sel :method |
|
|
1416 |
selArray at:idx put:sel.
|
|
1417 |
methodArray at:idx put:method.
|
|
1418 |
idx := idx + 1
|
|
1419 |
].
|
|
1420 |
selectors := selArray.
|
|
1421 |
methods := methodArray
|
1
|
1422 |
! !
|
|
1423 |
|
68
|
1424 |
!Behavior methodsFor:'compiler interface'!
|
|
1425 |
|
|
1426 |
compiler
|
|
1427 |
"return the compiler to use for this class -
|
|
1428 |
this can be redefined in special classes, to get classes with
|
|
1429 |
Lisp, Prolog, ASN1, Basic :-) or whatever syntax."
|
|
1430 |
|
|
1431 |
^ Compiler
|
|
1432 |
! !
|
|
1433 |
|
1
|
1434 |
!Behavior methodsFor:'enumeration'!
|
|
1435 |
|
|
1436 |
allInstancesDo:aBlock
|
45
|
1437 |
"evaluate aBlock for all of my instances"
|
1
|
1438 |
|
|
1439 |
ObjectMemory allObjectsDo:[:anObject |
|
|
1440 |
(anObject class == self) ifTrue:[
|
|
1441 |
aBlock value:anObject
|
|
1442 |
]
|
|
1443 |
]
|
45
|
1444 |
|
|
1445 |
"StandardSystemView allInstancesDo:[:v | Transcript showCr:(v name)]"
|
|
1446 |
!
|
|
1447 |
|
|
1448 |
allDerivedInstancesDo:aBlock
|
|
1449 |
"evaluate aBlock for all of my instances and all instances of subclasses"
|
|
1450 |
|
|
1451 |
ObjectMemory allObjectsDo:[:anObject |
|
|
1452 |
(anObject isKindOf:self) ifTrue:[
|
|
1453 |
aBlock value:anObject
|
|
1454 |
]
|
|
1455 |
]
|
|
1456 |
|
|
1457 |
"StandardSystemView allDerivedInstancesDo:[:v | Transcript showCr:(v name)]"
|
1
|
1458 |
!
|
|
1459 |
|
|
1460 |
subclassesDo:aBlock
|
|
1461 |
"evaluate the argument, aBlock for all immediate subclasses"
|
|
1462 |
|
2
|
1463 |
Smalltalk allBehaviorsDo:[:aClass |
|
1
|
1464 |
(aClass superclass == self) ifTrue:[
|
|
1465 |
aBlock value:aClass
|
|
1466 |
]
|
|
1467 |
]
|
45
|
1468 |
|
|
1469 |
"Collection subclassesDo:[:c | Transcript showCr:(c name)]"
|
1
|
1470 |
!
|
|
1471 |
|
|
1472 |
allSubclassesDo:aBlock
|
45
|
1473 |
"evaluate aBlock for all of my subclasses.
|
|
1474 |
There is no specific order, in which the entries are enumerated."
|
1
|
1475 |
|
2
|
1476 |
Smalltalk allBehaviorsDo:[:aClass |
|
1
|
1477 |
(aClass isSubclassOf:self) ifTrue:[
|
|
1478 |
aBlock value:aClass
|
|
1479 |
]
|
|
1480 |
]
|
45
|
1481 |
|
|
1482 |
"Collection allSubclassesDo:[:c | Transcript showCr:(c name)]"
|
1
|
1483 |
!
|
|
1484 |
|
|
1485 |
allSubclassesInOrderDo:aBlock
|
45
|
1486 |
"evaluate aBlock for all of my subclasses.
|
|
1487 |
Higher level subclasses will be enumerated before the deeper ones"
|
1
|
1488 |
|
|
1489 |
self subclassesDo:[:aClass |
|
|
1490 |
aBlock value:aClass.
|
|
1491 |
aClass allSubclassesInOrderDo:aBlock
|
|
1492 |
]
|
45
|
1493 |
|
|
1494 |
"Collection allSubclassesInOrderDo:[:c | Transcript showCr:(c name)]"
|
1
|
1495 |
!
|
|
1496 |
|
|
1497 |
allSuperclassesDo:aBlock
|
45
|
1498 |
"evaluate aBlock for all of my superclasses"
|
1
|
1499 |
|
|
1500 |
|theClass|
|
|
1501 |
|
|
1502 |
theClass := superclass.
|
|
1503 |
[theClass notNil] whileTrue:[
|
|
1504 |
aBlock value:theClass.
|
|
1505 |
theClass := theClass superclass
|
|
1506 |
]
|
45
|
1507 |
|
|
1508 |
"String allSuperclassesDo:[:c | Transcript showCr:(c name)]"
|
1
|
1509 |
! !
|
2
|
1510 |
|
68
|
1511 |
!Behavior methodsFor:'binary storage'!
|
2
|
1512 |
|
11
|
1513 |
readBinaryFrom:aStream
|
|
1514 |
"read an objects binary representation from the argument,
|
|
1515 |
aStream and return it.
|
|
1516 |
The read object must be a kind of myself
|
43
|
1517 |
- to get any object, use 'Object readBinaryFrom:...',
|
|
1518 |
to get any number, use 'Number readBinaryFrom:...' and so on.
|
11
|
1519 |
This is the reverse operation to 'storeBinaryOn:'. "
|
|
1520 |
|
|
1521 |
|newObject|
|
|
1522 |
|
|
1523 |
newObject := (BinaryInputManager new:1024) readFrom:aStream.
|
|
1524 |
(newObject isKindOf:self) ifFalse:[
|
|
1525 |
self error:('expected ' , self name)
|
|
1526 |
].
|
|
1527 |
^ newObject
|
|
1528 |
|
|
1529 |
"|s|
|
|
1530 |
s := WriteStream on:ByteArray new.
|
|
1531 |
#(1 2 3 4) storeBinaryOn:s.
|
|
1532 |
Object readBinaryFrom:(ReadStream on:s contents)
|
|
1533 |
"
|
|
1534 |
!
|
|
1535 |
|
43
|
1536 |
storeBinaryDefinitionOn: stream manager: manager
|
|
1537 |
"classes will store the name only and restore by looking for
|
|
1538 |
that name in the Smalltalk dictionary."
|
2
|
1539 |
|
43
|
1540 |
| myName |
|
2
|
1541 |
|
43
|
1542 |
myName := self name.
|
77
|
1543 |
stream nextNumber:4 put:self signature.
|
|
1544 |
stream nextNumber:2 put:myName size.
|
43
|
1545 |
myName do:[:c|
|
|
1546 |
stream nextPut:c asciiValue
|
|
1547 |
]
|
2
|
1548 |
!
|
|
1549 |
|
43
|
1550 |
binaryDefinitionFrom:stream manager:manager
|
|
1551 |
"read the definition on an empty instance (of my class) from stream.
|
|
1552 |
All pointer instances are left nil, while all bits are read in here.
|
|
1553 |
return the new object."
|
2
|
1554 |
|
43
|
1555 |
|obj t
|
|
1556 |
basicSize "{ Class: SmallInteger }" |
|
2
|
1557 |
|
|
1558 |
self isPointers ifTrue: [
|
|
1559 |
stream next. "skip instSize"
|
|
1560 |
self isVariable ifTrue: [
|
43
|
1561 |
^ self basicNew:(stream nextNumber:3)
|
2
|
1562 |
].
|
|
1563 |
^ self basicNew
|
|
1564 |
].
|
|
1565 |
|
43
|
1566 |
basicSize := stream nextNumber:4.
|
|
1567 |
obj := self basicNew:basicSize.
|
|
1568 |
|
2
|
1569 |
self isBytes ifTrue: [
|
45
|
1570 |
stream nextBytes:basicSize into:obj
|
43
|
1571 |
"
|
2
|
1572 |
1 to:basicSize do:[:i |
|
43
|
1573 |
obj basicAt:i put:stream next
|
2
|
1574 |
]
|
43
|
1575 |
"
|
2
|
1576 |
] ifFalse: [
|
|
1577 |
self isWords ifTrue: [
|
|
1578 |
1 to:basicSize do:[:i |
|
43
|
1579 |
obj basicAt:i put: stream nextNumber:2
|
2
|
1580 |
]
|
|
1581 |
] ifFalse:[
|
|
1582 |
self isLongs ifTrue: [
|
|
1583 |
1 to:basicSize do:[:i |
|
43
|
1584 |
obj basicAt:i put: stream nextNumber:4
|
2
|
1585 |
]
|
|
1586 |
] ifFalse:[
|
|
1587 |
self isFloats ifTrue: [
|
47
|
1588 |
"could do it in one big read on machines which use IEEE floats ..."
|
2
|
1589 |
1 to:basicSize do:[:i |
|
|
1590 |
t := Float basicNew.
|
43
|
1591 |
Float readBinaryIEEESingleFrom:stream into:t.
|
|
1592 |
obj basicAt:i put: t
|
2
|
1593 |
]
|
|
1594 |
] ifFalse:[
|
47
|
1595 |
self isDoubles ifTrue: [
|
|
1596 |
"could do it in one big read on machines which use IEEE doubles ..."
|
|
1597 |
1 to:basicSize do:[:i |
|
|
1598 |
t := Float basicNew.
|
|
1599 |
Float readBinaryIEEEDoubleFrom:stream into:t.
|
|
1600 |
obj basicAt:i put: t
|
|
1601 |
]
|
2
|
1602 |
]
|
|
1603 |
]
|
|
1604 |
]
|
|
1605 |
]
|
|
1606 |
].
|
|
1607 |
^obj
|
|
1608 |
! !
|