author | Claus Gittinger <cg@exept.de> |
Tue, 14 Nov 1995 20:02:34 +0100 | |
changeset 544 | d78012b20769 |
parent 528 | a083413dfbe8 |
child 620 | c7353f86a302 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 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 |
||
13 |
Object subclass:#Behavior |
|
357 | 14 |
instanceVariableNames:'superclass flags selectorArray methodArray |
15 |
otherSuperclasses instSize' |
|
308 | 16 |
classVariableNames:'SubclassInfo' |
1 | 17 |
poolDictionaries:'' |
18 |
category:'Kernel-Classes' |
|
19 |
! |
|
20 |
||
47 | 21 |
!Behavior class methodsFor:'documentation'! |
22 |
||
88 | 23 |
copyright |
24 |
" |
|
25 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
154 | 26 |
All Rights Reserved |
88 | 27 |
|
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
37 |
version |
|
528
a083413dfbe8
converted version methods from comment-only to returning-a-string
Claus Gittinger <cg@exept.de>
parents:
492
diff
changeset
|
38 |
^ '$Header: /cvs/stx/stx/libbasic/Behavior.st,v 1.52 1995-11-11 14:26:59 cg Exp $' |
88 | 39 |
! |
40 |
||
47 | 41 |
documentation |
42 |
" |
|
68 | 43 |
Every class in the system inherits from Behavior (via Class, ClassDescription); |
44 |
so here is where most of the class messages end up being implemented. |
|
45 |
(to answer a FAQ: 'Point basicNew' will be done here :-) |
|
46 |
||
356 | 47 |
Beginners should keep in mind, that all classes are instances (of subclasses) |
91 | 48 |
of Behavior, therefore you will find the above mentioned 'basicNew:' method |
49 |
under the 'instance'-methods of Behavior - NOT under the class methods |
|
154 | 50 |
('Behavior new' will create and return a new class, while sending 'new' to |
51 |
any instance of Behavior (i.e. any class) will return an instance of that class). |
|
68 | 52 |
|
53 |
Behavior provides minimum support for all classes - additional stuff is |
|
77 | 54 |
found in ClassDescription and Class. Behaviors provides all mechanisms needed |
55 |
to create instances, and send messages to those. However, Behavior does not provide |
|
56 |
all the (symbolic) information needed to compile methods for a class or to get |
|
57 |
useful information in inspectors. |
|
47 | 58 |
|
68 | 59 |
In contrast to other ST implementations, the methods have been separated |
60 |
from the selectors (there is no Dictionary, but two separate Arrays) |
|
61 |
- this avoids the need for knowledge about Dictionaries in the runtime library (VM) |
|
62 |
(lookup and search in these is seldom anyway, so the added benefit from using a |
|
356 | 63 |
hashed dictionary is almost void). |
64 |
For ST-80 compatibility, this will be replaced by a single instance of |
|
362 | 65 |
MethodDictionary (which will NOT be a true dictionary, but an Array with |
356 | 66 |
alternating selector/method entries). |
67 |
To be prepared for this change, please do NOT directly use the methodArray |
|
68 |
and selectorArray instVars. |
|
1 | 69 |
|
68 | 70 |
Instance variables: |
1 | 71 |
|
154 | 72 |
superclass <Class> the receivers superclass |
356 | 73 |
|
154 | 74 |
otherSuperclasses <Array of Class> experimental: other superclasses |
356 | 75 |
a hook for experimental multiple inheritance |
76 |
implementations |
|
77 |
||
154 | 78 |
selectorArray <Array of Symbol> the selectors for which inst-methods are defined here |
356 | 79 |
|
154 | 80 |
methodArray <Array of Method> the inst-methods corresponding to the selectors |
356 | 81 |
|
154 | 82 |
instSize <SmallInteger> the number of instance variables |
356 | 83 |
|
154 | 84 |
flags <SmallInteger> special flag bits coded in a number |
356 | 85 |
not for application use |
1 | 86 |
|
81 | 87 |
flag bits (see stc.h): |
47 | 88 |
|
68 | 89 |
NOTICE: layout known by compiler and runtime system; be careful when changing |
47 | 90 |
" |
356 | 91 |
! |
92 |
||
93 |
virtualMachineRelationship |
|
94 |
" |
|
357 | 95 |
NOTICE: |
96 |
the stuff described below may not be available on other |
|
97 |
Smalltalk implementations; be aware that these error mechanisms |
|
98 |
are ST/X specials and applications using these (tricks) may |
|
99 |
not be portable to other systems. |
|
100 |
||
101 |
WARNING: |
|
102 |
do not try the examples below on (some) other smalltalk systems; |
|
103 |
it has been reported, that some crash badly when doing this .... ;-) |
|
104 |
||
356 | 105 |
Instances of Behavior and subclasses (i.e. in sloppy words: classes) |
106 |
play a special role w.r.t. the VM. Only objects whose class-slot is marked |
|
107 |
as being behaviorLike (in the flag-instvar) are considered to be classLike |
|
108 |
and a message lookup will be done for it in the well known way. |
|
109 |
Thus, if an object has a class for which its class does NOT have |
|
362 | 110 |
this flag bit set, the VM will trigger an error on a message send. |
356 | 111 |
|
112 |
Why is this so: |
|
113 |
||
114 |
the above lets every object play the role of a class, |
|
115 |
which has been flagged as behaviorLike in its class's flag. |
|
116 |
Thus, you can create arbitrary new classLike objects and have the VM |
|
117 |
play with them. |
|
118 |
This may offer the flexibility to create a totally different object scheme |
|
362 | 119 |
on top of ST/X (for example: Self like objects) where any object can play |
120 |
a classRole for another object. |
|
356 | 121 |
|
357 | 122 |
However, the VM trusts the isBehaviorLike flag - if it is set for some |
362 | 123 |
object, it expects the object selector and methodDictionaries to be found |
124 |
at the instance positions as defined here. |
|
125 |
(i.e. instanceVariables with contents and semantic corresponding to |
|
126 |
superclass flags selectorArray methodArray |
|
127 |
must be present and have the same instVar-index as here). |
|
128 |
||
356 | 129 |
The VM (and the system) may crash badly, if this is not the case. |
362 | 130 |
|
356 | 131 |
Since every class in the system derives from Behavior, the flag setting |
357 | 132 |
(and instance variable layout) is correct for all 'normal' classes. |
133 |
If you experiment by creating new behaviorLike objects, please take |
|
134 |
care of this flag. If you want to use the VM's lookup function, the |
|
135 |
instVars 'superclass', 'selectorArray' and 'methodArray' are required |
|
136 |
and have to be at the same instVar index. |
|
362 | 137 |
(we suggest, you subclass Behavior, to make certain) |
357 | 138 |
|
139 |
You do not have to care about the above details if you are a 'normal' |
|
140 |
ST-programmer, though. |
|
141 |
||
356 | 142 |
|
143 |
Examples (only of theoretical interrest): |
|
144 |
take away the behaviorLike-flag from a class. |
|
145 |
-> The instances will not understand any messages, since the VM will |
|
146 |
not recognize its class as being a class ... |
|
147 |
||
148 |
|newMeta notRecognizedAsClass someInstance| |
|
149 |
||
150 |
newMeta := Metaclass new. |
|
151 |
newMeta flags:0. |
|
152 |
||
153 |
notRecognizedAsClass := newMeta new. |
|
154 |
||
155 |
someInstance := notRecognizedAsClass new. |
|
156 |
someInstance perform:#isNil |
|
157 |
||
357 | 158 |
|
159 |
Of course, this is an exception which can be handled ...: |
|
160 |
Example: |
|
161 |
||
162 |
|newMeta notRecognizedAsClass someInstance| |
|
163 |
||
164 |
newMeta := Metaclass new. |
|
165 |
newMeta flags:0. |
|
166 |
||
167 |
notRecognizedAsClass := newMeta new. |
|
168 |
||
169 |
someInstance := notRecognizedAsClass new. |
|
170 |
Object errorSignal handle:[:ex | |
|
171 |
ex return |
|
172 |
] do:[ |
|
173 |
someInstance perform:#isNil |
|
174 |
] |
|
175 |
||
176 |
||
177 |
likewise, a doesNotUnderstand-notUnderstood can be handled: |
|
178 |
Example: |
|
179 |
||
180 |
|newMeta funnyClass someInstance| |
|
181 |
||
182 |
newMeta := Metaclass new. |
|
183 |
||
184 |
funnyClass := newMeta new. |
|
185 |
funnyClass setSuperclass:nil. |
|
186 |
||
187 |
someInstance := funnyClass new. |
|
188 |
Object errorSignal handle:[:ex | |
|
189 |
ex return |
|
190 |
] do:[ |
|
191 |
someInstance perform:#isNil |
|
192 |
] |
|
193 |
||
194 |
||
362 | 195 |
more examples, which try to trick the VM ;-): |
357 | 196 |
badly playing around with a classes internals ... |
197 |
||
198 |
|newClass someInstance| |
|
199 |
||
200 |
newClass := Class new. |
|
201 |
newClass setSelectorArray:nil. |
|
202 |
someInstance := newClass new. |
|
203 |
someInstance inspect |
|
204 |
||
205 |
||
206 |
|newClass someInstance| |
|
207 |
||
208 |
newClass := Class new. |
|
209 |
newClass setSuperclass:nil. |
|
210 |
someInstance := newClass new. |
|
211 |
someInstance inspect |
|
212 |
||
213 |
||
214 |
|newClass someInstance| |
|
215 |
||
216 |
newClass := Class new. |
|
217 |
newClass setSuperclass:newClass. |
|
218 |
someInstance := newClass new. |
|
219 |
someInstance inspect |
|
220 |
||
221 |
||
222 |
|newClass someInstance| |
|
223 |
||
224 |
newClass := Class new. |
|
225 |
newClass setSuperclass:1. |
|
226 |
someInstance := newClass new. |
|
227 |
someInstance inspect |
|
228 |
||
229 |
||
356 | 230 |
Example: |
231 |
creating totally anonymous classes: |
|
232 |
||
233 |
|newClass someInstance| |
|
234 |
||
235 |
newClass := Class new. |
|
236 |
someInstance := newClass new. |
|
237 |
someInstance inspect |
|
238 |
||
357 | 239 |
|
356 | 240 |
Example: |
357 | 241 |
creating totally anonymous metaclasses: |
356 | 242 |
|
243 |
|newMeta newClass someInstance| |
|
244 |
||
245 |
newMeta := Metaclass new. |
|
246 |
newClass := newMeta new. |
|
247 |
someInstance := newClass new. |
|
248 |
someInstance inspect |
|
357 | 249 |
|
250 |
PS: if you experiment with new behaviorLike objects, you may want |
|
251 |
to turn off the VM's debugPrintouts |
|
252 |
with: |
|
253 |
'Smalltalk debugPrinting:false' |
|
254 |
and: |
|
255 |
'Smalltalk infoPrinting:false' |
|
356 | 256 |
" |
47 | 257 |
! ! |
1 | 258 |
|
3 | 259 |
!Behavior class methodsFor:'queries'! |
260 |
||
261 |
isBuiltInClass |
|
262 |
"this class is known by the run-time-system" |
|
263 |
||
264 |
^ true |
|
265 |
! ! |
|
266 |
||
1 | 267 |
!Behavior class methodsFor:'creating new classes'! |
268 |
||
269 |
new |
|
356 | 270 |
"creates and return a new behavior (which is like a class, |
271 |
but without the symbolic & name information). |
|
272 |
Not for normal applications. |
|
273 |
Sending the returned behavior the #new message gives you |
|
274 |
an instance if it. |
|
275 |
||
276 |
Notice: the returned class is given a superclass of Object; |
|
277 |
this allows for its new instances to be inspected and the like." |
|
1 | 278 |
|
279 |
|newClass| |
|
280 |
||
281 |
newClass := self basicNew. |
|
282 |
newClass setSuperclass:Object |
|
345 | 283 |
selectors:#() "/ (Array new:0) |
284 |
methods:#() "/ (Array new:0) |
|
154 | 285 |
instSize:0 |
345 | 286 |
flags:(self flagBehavior). |
1 | 287 |
^ newClass |
356 | 288 |
|
289 |
" |
|
290 |
Behavior new <- a new behavior |
|
291 |
Behavior new new <- an instance of it |
|
292 |
ClassDescription new <- a new classDescription |
|
293 |
ClassDescription new new <- an instance of it |
|
294 |
Class new <- a new class |
|
295 |
Class new new <- an instance of it |
|
296 |
Metaclass new <- a new metaclass |
|
297 |
Metaclass new new <- an instance (i.e. a class) of it |
|
298 |
Metaclass new new new <- an instance of this new class |
|
299 |
" |
|
1 | 300 |
! ! |
301 |
||
308 | 302 |
!Behavior class methodsFor:'private '! |
303 |
||
304 |
subclassInfo |
|
357 | 305 |
|d| |
308 | 306 |
|
307 |
SubclassInfo notNil ifTrue:[^ SubclassInfo]. |
|
308 |
||
309 |
d := IdentityDictionary new. |
|
310 |
Smalltalk allClassesDo:[:aClass | |
|
311 |
|superCls| |
|
312 |
||
313 |
aClass isMeta not ifTrue:[ |
|
314 |
superCls := aClass superclass. |
|
315 |
superCls notNil ifTrue:[ |
|
316 |
(d includesKey: superCls) ifFalse:[ |
|
317 |
d at:superCls put:(Set with:aClass). |
|
318 |
] ifTrue:[ |
|
319 |
(d at:superCls ) add:aClass |
|
320 |
] |
|
321 |
] |
|
322 |
]. |
|
323 |
]. |
|
324 |
SubclassInfo := d. |
|
325 |
^ d |
|
326 |
||
327 |
" |
|
328 |
Class subclassInfo |
|
329 |
" |
|
330 |
! |
|
331 |
||
332 |
flushSubclassInfo |
|
333 |
SubclassInfo := nil. |
|
334 |
||
335 |
" |
|
336 |
Class flushSubclassInfo |
|
337 |
" |
|
338 |
! ! |
|
339 |
||
1 | 340 |
!Behavior methodsFor:'initialization'! |
341 |
||
342 |
initialize |
|
442 | 343 |
"initialize is sent to a class either during startup, |
344 |
(for all statically compiled-in classes) or after a class |
|
345 |
has been loaded into the system (either bytecodes or machinecode). |
|
346 |
The default action here is to do nothing." |
|
1 | 347 |
|
348 |
^ self |
|
349 |
! |
|
350 |
||
328 | 351 |
postAutoload |
442 | 352 |
"postAutoload is sent to a class after it has been autoloaded. |
353 |
This gives it a chance to arrange for automatic unloading to be done |
|
354 |
after a while ... |
|
355 |
This is NOT sent to statically compiled in or explicitely filedIn |
|
356 |
classes. |
|
357 |
The default action here is to do nothing." |
|
328 | 358 |
|
359 |
^ self |
|
360 |
! |
|
361 |
||
1 | 362 |
reinitialize |
442 | 363 |
"reinitialize is sent to a class when an image has been restarted. |
364 |
I.e. when the system is restarted. |
|
365 |
This gives classes a chance to flush any device dependent or otherwise |
|
366 |
obsolete data which may be a leftover from the previous live. |
|
367 |
The default action here is to do nothing." |
|
368 |
||
369 |
^ self |
|
370 |
! |
|
371 |
||
372 |
deinitialize |
|
373 |
"deinitialize is sent to a class before it is physically unloaded. |
|
374 |
This is only done with classes which have been loaded in from a binary |
|
375 |
file. Classes may release any primitive memory or other stuff which is |
|
376 |
not visible to smalltalk (for example, release internal memory). |
|
377 |
The default action here is to do nothing." |
|
1 | 378 |
|
379 |
^ self |
|
380 |
! ! |
|
381 |
||
11 | 382 |
!Behavior methodsFor:'copying'! |
383 |
||
384 |
deepCopy |
|
385 |
"return a deep copy of the receiver |
|
386 |
- return the receiver here - time will show if this is ok" |
|
387 |
||
388 |
^ self |
|
389 |
! |
|
390 |
||
391 |
deepCopyUsing:aDictionary |
|
392 |
"return a deep copy of the receiver |
|
393 |
- return the receiver here - time will show if this is ok" |
|
394 |
||
395 |
^ self |
|
396 |
! |
|
397 |
||
398 |
simpleDeepCopy |
|
399 |
"return a deep copy of the receiver |
|
400 |
- return the receiver here - time will show if this is ok" |
|
401 |
||
402 |
^ self |
|
403 |
! ! |
|
404 |
||
492
fa23b14f444b
renamed instance creation category
Claus Gittinger <cg@exept.de>
parents:
457
diff
changeset
|
405 |
!Behavior methodsFor:'instance creation'! |
1 | 406 |
|
407 |
uninitializedNew |
|
308 | 408 |
"create an instance of myself with uninitialized contents. |
457
41c73cbee305
change classes versionString when class changes (prepare for sourceCode system)
Claus Gittinger <cg@exept.de>
parents:
442
diff
changeset
|
409 |
For all classes except ByteArray, this is the same as #basicNew." |
1 | 410 |
|
411 |
^ self basicNew |
|
412 |
! |
|
413 |
||
414 |
uninitializedNew:anInteger |
|
308 | 415 |
"create an instance of myself with uninitialized contents. |
457
41c73cbee305
change classes versionString when class changes (prepare for sourceCode system)
Claus Gittinger <cg@exept.de>
parents:
442
diff
changeset
|
416 |
For all classes except ByteArray, this is the same as #basicNew:." |
1 | 417 |
|
418 |
^ self basicNew:anInteger |
|
419 |
! |
|
420 |
||
127 | 421 |
niceBasicNew:anInteger |
422 |
"same as basicNew:anInteger, but tries to avoid long pauses |
|
423 |
due to garbage collection. This method checks to see if |
|
424 |
allocation is possible without a pause, and does a background |
|
425 |
incremental garbage collect first if there is not enough memory |
|
426 |
available at the moment for fast allocation. |
|
427 |
This is useful in low-priority background processes which like to |
|
428 |
avoid disturbing any higher priority foreground process while allocating |
|
429 |
big amounts of memory. Of course, using this method only makes |
|
430 |
sense for big or huge objects (say > 200k). |
|
431 |
||
432 |
EXPERIMENTAL: this is a non-standard interface and should only |
|
433 |
be used for special applications. There is no guarantee, that this |
|
434 |
method will be available in future ST/X releases." |
|
435 |
||
436 |
|size| |
|
437 |
||
438 |
size := self sizeOfInst:anInteger. |
|
439 |
(ObjectMemory checkForFastNew:size) ifFalse:[ |
|
154 | 440 |
" |
441 |
incrementally collect garbage |
|
442 |
" |
|
443 |
ObjectMemory incrementalGC. |
|
127 | 444 |
]. |
445 |
^ self basicNew:anInteger |
|
446 |
! |
|
447 |
||
1 | 448 |
new |
449 |
"return an instance of myself without indexed variables" |
|
450 |
||
451 |
^ self basicNew |
|
452 |
! |
|
453 |
||
454 |
new:anInteger |
|
455 |
"return an instance of myself with anInteger indexed variables" |
|
456 |
||
457 |
^ self basicNew:anInteger |
|
458 |
! |
|
459 |
||
460 |
basicNew |
|
54 | 461 |
"return an instance of myself without indexed variables. |
1 | 462 |
If the receiver-class has indexed instvars, the new object will have |
91 | 463 |
a basicSize of zero - |
464 |
i.e. 'aClass basicNew' is equivalent to 'aClass basicNew:0'. |
|
54 | 465 |
|
1 | 466 |
** Do not redefine this method in any class **" |
467 |
||
468 |
%{ /* NOCONTEXT */ |
|
81 | 469 |
REGISTER OBJ newobj; |
470 |
REGISTER char *nextPtr; |
|
91 | 471 |
unsigned int instsize; |
472 |
REGISTER unsigned int nInstVars; |
|
369 | 473 |
extern OBJ __new(); |
1 | 474 |
|
54 | 475 |
/* |
369 | 476 |
* the following ugly code is nothing more than a __new() followed |
54 | 477 |
* by a nilling of the new instance. |
91 | 478 |
* Unrolled for a bit more speed since this is one of the central object |
479 |
* allocation methods in the system |
|
54 | 480 |
*/ |
1 | 481 |
nInstVars = _intVal(_INST(instSize)); |
92 | 482 |
instsize = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
54 | 483 |
|
77 | 484 |
newobj = (OBJ) newNextPtr; |
81 | 485 |
nextPtr = ((char *)newobj) + instsize; |
486 |
||
487 |
/* |
|
488 |
* dont argue about the goto and the arrangement below - it saves |
|
489 |
* an extra nil-compare and branch in the common case ... |
|
91 | 490 |
* (i.e. if no GC is needed, we fall through without a branch) |
81 | 491 |
*/ |
82 | 492 |
if (nextPtr < newEndPtr) { |
154 | 493 |
_objPtr(newobj)->o_size = instsize; |
494 |
/* o_allFlags(newobj) = 0; */ |
|
495 |
/* _objPtr(newobj)->o_space = newSpace; */ |
|
496 |
o_setAllFlags(newobj, newSpace); |
|
91 | 497 |
#ifdef ALIGN4 |
154 | 498 |
newNextPtr = nextPtr; |
91 | 499 |
#else |
154 | 500 |
if (instsize & (ALIGN-1)) { |
501 |
newNextPtr = (char *)newobj + (instsize & ~(ALIGN-1)) + ALIGN; |
|
502 |
} else { |
|
503 |
newNextPtr = nextPtr; |
|
504 |
} |
|
91 | 505 |
#endif |
82 | 506 |
|
507 |
ok: |
|
154 | 508 |
_InstPtr(newobj)->o_class = self; |
369 | 509 |
__qSTORE(newobj, self); |
82 | 510 |
|
154 | 511 |
if (nInstVars) { |
357 | 512 |
#if defined(FAST_OBJECT_MEMSET4) || defined(FAST_MEMSET4) |
154 | 513 |
memset4(_InstPtr(newobj)->i_instvars, nil, nInstVars); |
82 | 514 |
#else |
154 | 515 |
REGISTER OBJ *op; |
82 | 516 |
|
154 | 517 |
op = _InstPtr(newobj)->i_instvars; |
82 | 518 |
|
91 | 519 |
# if !defined(NEGATIVE_ADDRESSES) |
154 | 520 |
/* |
521 |
* knowing that nil is 0 |
|
522 |
*/ |
|
91 | 523 |
# if defined(FAST_OBJECT_MEMSET_DOUBLES_UNROLLED) |
154 | 524 |
if (nInstVars > 8) { |
525 |
*op++ = nil; /* for alignment */ |
|
526 |
nInstVars--; |
|
527 |
while (nInstVars >= 8) { |
|
528 |
*(double *)op = 0.0; |
|
529 |
((double *)op)[1] = 0.0; |
|
530 |
((double *)op)[2] = 0.0; |
|
531 |
((double *)op)[3] = 0.0; |
|
532 |
op += 8; |
|
533 |
nInstVars -= 8; |
|
534 |
} |
|
535 |
} |
|
536 |
while (nInstVars != 0) { |
|
537 |
*op++ = 0; |
|
538 |
nInstVars--; |
|
539 |
} |
|
82 | 540 |
# else |
91 | 541 |
# if defined(FAST_OBJECT_MEMSET_LONGLONG_UNROLLED) |
154 | 542 |
if (nInstVars > 8) { |
543 |
*op++ = nil; /* for alignment */ |
|
544 |
nInstVars--; |
|
545 |
while (nInstVars >= 8) { |
|
546 |
*(long long *)op = 0; |
|
547 |
((long long *)op)[1] = 0; |
|
548 |
((long long *)op)[2] = 0; |
|
549 |
((long long *)op)[3] = 0; |
|
550 |
op += 8; |
|
551 |
nInstVars -= 8; |
|
552 |
} |
|
553 |
} |
|
554 |
while (nInstVars != 0) { |
|
555 |
*op++ = 0; |
|
556 |
nInstVars--; |
|
557 |
} |
|
91 | 558 |
|
559 |
# else |
|
560 |
# if defined(FAST_OBJECT_MEMSET_WORDS_UNROLLED) |
|
154 | 561 |
while (nInstVars >= 8) { |
562 |
*op = nil; |
|
563 |
*(op+1) = nil; |
|
564 |
*(op+2) = nil; |
|
565 |
*(op+3) = nil; |
|
566 |
*(op+4) = nil; |
|
567 |
*(op+5) = nil; |
|
568 |
*(op+6) = nil; |
|
569 |
*(op+7) = nil; |
|
570 |
op += 8; |
|
571 |
nInstVars -= 8; |
|
572 |
} |
|
573 |
while (nInstVars != 0) { |
|
574 |
*op++ = nil; |
|
575 |
nInstVars--; |
|
576 |
} |
|
91 | 577 |
# else |
578 |
# if defined(FAST_MEMSET) |
|
154 | 579 |
memset(_InstPtr(newobj)->i_instvars, 0, instsize-OHDR_SIZE); |
91 | 580 |
# else |
154 | 581 |
do { |
582 |
*op++ = nil; |
|
583 |
nInstVars--; |
|
584 |
} while (nInstVars != 0); |
|
91 | 585 |
# endif |
82 | 586 |
# endif |
587 |
# endif |
|
588 |
# endif |
|
589 |
# else /* nil could be ~~ 0 */ |
|
154 | 590 |
while (nInstVars >= 8) { |
591 |
*op = nil; |
|
592 |
*(op+1) = nil; |
|
593 |
*(op+2) = nil; |
|
594 |
*(op+3) = nil; |
|
595 |
*(op+4) = nil; |
|
596 |
*(op+5) = nil; |
|
597 |
*(op+6) = nil; |
|
598 |
*(op+7) = nil; |
|
599 |
op += 8; |
|
600 |
nInstVars -= 8; |
|
601 |
} |
|
602 |
while (nInstVars != 0) { |
|
603 |
*op++ = nil; |
|
604 |
nInstVars--; |
|
605 |
} |
|
82 | 606 |
# endif |
607 |
#endif |
|
154 | 608 |
} |
609 |
RETURN ( newobj ); |
|
81 | 610 |
} |
54 | 611 |
|
92 | 612 |
/* |
613 |
* the slow case - a GC will occur |
|
614 |
*/ |
|
82 | 615 |
PROTECT_CONTEXT |
369 | 616 |
newobj = __new(instsize); |
82 | 617 |
UNPROTECT_CONTEXT |
618 |
if (newobj != nil) goto ok; |
|
1 | 619 |
%} |
2 | 620 |
. |
68 | 621 |
" |
622 |
memory allocation failed. |
|
623 |
When we arrive here, there was no memory, even after |
|
165 | 624 |
a garbage collect. |
625 |
This means, that the VM wanted to get some more memory from the |
|
626 |
Operatingsystem, which was not kind enough to give it. |
|
627 |
Bad luck - you should increase the swap space on your machine. |
|
68 | 628 |
" |
127 | 629 |
^ ObjectMemory allocationFailureSignal raise. |
1 | 630 |
! |
631 |
||
632 |
basicNew:anInteger |
|
633 |
"return an instance of myself with anInteger indexed variables. |
|
634 |
If the receiver-class has no indexed instvars, this is only allowed |
|
635 |
if the argument, anInteger is zero. |
|
636 |
** Do not redefine this method in any class **" |
|
637 |
||
638 |
%{ /* NOCONTEXT */ |
|
639 |
||
640 |
OBJ newobj; |
|
154 | 641 |
unsigned INT instsize, nInstVars; |
642 |
INT nindexedinstvars; |
|
92 | 643 |
unsigned INT flags; |
644 |
#if ! defined(FAST_ARRAY_MEMSET) || defined(NEGATIVE_ADDRESSES) |
|
1 | 645 |
REGISTER char *cp; |
646 |
short *sp; |
|
647 |
long *lp; |
|
648 |
#endif |
|
649 |
REGISTER OBJ *op; |
|
650 |
float *fp; |
|
651 |
double *dp; |
|
652 |
||
249 | 653 |
if (__isSmallInteger(anInteger)) { |
154 | 654 |
nindexedinstvars = _intVal(anInteger); |
655 |
if (nindexedinstvars >= 0) { |
|
656 |
nInstVars = _intVal(_INST(instSize)); |
|
657 |
flags = _intVal(_INST(flags)) & ARRAYMASK; |
|
658 |
switch (flags) { |
|
659 |
case BYTEARRAY: |
|
660 |
instsize = OHDR_SIZE + nindexedinstvars; |
|
661 |
if (nInstVars == 0) { |
|
662 |
if (_CanDoQuickNew(instsize)) { |
|
663 |
/* |
|
664 |
* the most common case |
|
665 |
*/ |
|
666 |
_qCheckedNew(newobj, instsize); |
|
667 |
_InstPtr(newobj)->o_class = self; |
|
357 | 668 |
#if defined(FAST_ARRAY_MEMSET4) || defined(FAST_MEMSET4) |
359 | 669 |
nInstVars = nindexedinstvars >> 2; |
670 |
if (nindexedinstvars & 3) nInstVars++; |
|
671 |
memset4(_InstPtr(newobj)->i_instvars, 0, nInstVars); |
|
357 | 672 |
#else |
673 |
# if defined(FAST_ARRAY_MEMSET) && ! defined(NEGATIVE_ADDRESSES) |
|
154 | 674 |
memset(_InstPtr(newobj)->i_instvars, 0, nindexedinstvars); |
357 | 675 |
# else |
154 | 676 |
cp = (char *)_InstPtr(newobj)->i_instvars; |
677 |
while (nindexedinstvars >= sizeof(long)) { |
|
678 |
*(long *)cp = 0; |
|
679 |
cp += sizeof(long); |
|
680 |
nindexedinstvars -= sizeof(long); |
|
681 |
} |
|
682 |
while (nindexedinstvars--) |
|
683 |
*cp++ = '\0'; |
|
357 | 684 |
# endif |
92 | 685 |
#endif |
154 | 686 |
RETURN ( newobj ); |
687 |
} |
|
688 |
} else { |
|
689 |
instsize += __OBJS2BYTES__(nInstVars); |
|
690 |
} |
|
691 |
PROTECT_CONTEXT |
|
692 |
_qNew(newobj, instsize, SENDER); |
|
693 |
UNPROTECT_CONTEXT |
|
694 |
if (newobj == nil) { |
|
695 |
break; |
|
696 |
} |
|
697 |
_InstPtr(newobj)->o_class = self; |
|
369 | 698 |
__qSTORE(newobj, self); |
699 |
||
357 | 700 |
#if defined(FAST_ARRAY_MEMSET4) || defined(FAST_MEMSET4) |
701 |
nInstVars = (instsize-OHDR_SIZE) >> 2; |
|
702 |
if (instsize & 3) nInstVars++; |
|
703 |
memset4(_InstPtr(newobj)->i_instvars, 0, nInstVars); |
|
704 |
#else |
|
705 |
# if defined(FAST_ARRAY_MEMSET) && ! defined(NEGATIVE_ADDRESSES) |
|
154 | 706 |
/* |
707 |
* knowing that nil is 0 |
|
708 |
*/ |
|
709 |
memset(_InstPtr(newobj)->i_instvars, 0, instsize-OHDR_SIZE); |
|
357 | 710 |
# else |
154 | 711 |
op = _InstPtr(newobj)->i_instvars; |
712 |
while (nInstVars--) |
|
713 |
*op++ = nil; |
|
714 |
cp = (char *)op; |
|
715 |
while (nindexedinstvars >= sizeof(long)) { |
|
716 |
*(long *)cp = 0; |
|
717 |
cp += sizeof(long); |
|
718 |
nindexedinstvars -= sizeof(long); |
|
719 |
} |
|
720 |
while (nindexedinstvars--) |
|
721 |
*cp++ = '\0'; |
|
357 | 722 |
# endif |
1 | 723 |
#endif |
154 | 724 |
RETURN ( newobj ); |
725 |
break; |
|
1 | 726 |
|
154 | 727 |
case WORDARRAY: |
728 |
instsize = OHDR_SIZE + |
|
729 |
__OBJS2BYTES__(nInstVars) + |
|
730 |
nindexedinstvars * sizeof(short); |
|
731 |
PROTECT_CONTEXT |
|
732 |
_qNew(newobj, instsize, SENDER); |
|
733 |
UNPROTECT_CONTEXT |
|
734 |
if (newobj == nil) { |
|
735 |
break; |
|
736 |
} |
|
737 |
_InstPtr(newobj)->o_class = self; |
|
369 | 738 |
__qSTORE(newobj, self); |
739 |
||
92 | 740 |
#if defined(FAST_ARRAY_MEMSET) && ! defined(NEGATIVE_ADDRESSES) |
154 | 741 |
/* |
742 |
* knowing that nil is 0 |
|
743 |
*/ |
|
744 |
memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE); |
|
1 | 745 |
#else |
154 | 746 |
op = _InstPtr(newobj)->i_instvars; |
747 |
while (nInstVars--) |
|
748 |
*op++ = nil; |
|
749 |
sp = (short *)op; |
|
750 |
while (nindexedinstvars--) |
|
751 |
*sp++ = 0; |
|
1 | 752 |
#endif |
154 | 753 |
RETURN ( newobj ); |
754 |
break; |
|
1 | 755 |
|
154 | 756 |
case LONGARRAY: |
757 |
instsize = OHDR_SIZE + |
|
758 |
__OBJS2BYTES__(nInstVars) + |
|
759 |
nindexedinstvars * sizeof(long); |
|
760 |
PROTECT_CONTEXT |
|
761 |
_qAlignedNew(newobj, instsize, SENDER); |
|
762 |
UNPROTECT_CONTEXT |
|
763 |
if (newobj == nil) { |
|
764 |
break; |
|
765 |
} |
|
766 |
_InstPtr(newobj)->o_class = self; |
|
369 | 767 |
__qSTORE(newobj, self); |
768 |
||
357 | 769 |
#if defined(FAST_ARRAY_MEMSET4) || defined(FAST_MEMSET4) |
154 | 770 |
/* |
771 |
* knowing that nil is 0 |
|
772 |
*/ |
|
773 |
memset4(_InstPtr(newobj)->i_instvars, 0, nInstVars + nindexedinstvars); |
|
1 | 774 |
#else |
92 | 775 |
# if defined(FAST_ARRAY_MEMSET) && ! defined(NEGATIVE_ADDRESSES) |
154 | 776 |
/* |
777 |
* knowing that nil is 0 |
|
778 |
*/ |
|
779 |
memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE); |
|
1 | 780 |
# else |
154 | 781 |
op = _InstPtr(newobj)->i_instvars; |
782 |
while (nInstVars--) |
|
783 |
*op++ = nil; |
|
784 |
lp = (long *)op; |
|
785 |
while (nindexedinstvars--) |
|
786 |
*lp++ = 0; |
|
1 | 787 |
# endif |
788 |
#endif |
|
154 | 789 |
RETURN ( newobj ); |
790 |
break; |
|
1 | 791 |
|
154 | 792 |
case FLOATARRAY: |
325 | 793 |
instsize = sizeof(struct __floatArray) + |
154 | 794 |
__OBJS2BYTES__(nInstVars) + |
795 |
(nindexedinstvars - 1) * sizeof(float); |
|
54 | 796 |
|
154 | 797 |
PROTECT_CONTEXT |
798 |
_qNew(newobj, instsize, SENDER); |
|
799 |
UNPROTECT_CONTEXT |
|
800 |
if (newobj == nil) { |
|
801 |
break; |
|
802 |
} |
|
803 |
_InstPtr(newobj)->o_class = self; |
|
369 | 804 |
__qSTORE(newobj, self); |
805 |
||
154 | 806 |
op = _InstPtr(newobj)->i_instvars; |
92 | 807 |
# if defined(mips) /* knowin that float 0.0 is all-zeros */ |
154 | 808 |
memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE); |
92 | 809 |
# else |
154 | 810 |
while (nInstVars--) |
811 |
*op++ = nil; |
|
812 |
fp = (float *)op; |
|
813 |
while (nindexedinstvars--) |
|
814 |
*fp++ = 0.0; |
|
92 | 815 |
# endif |
154 | 816 |
RETURN ( newobj ); |
817 |
break; |
|
1 | 818 |
|
154 | 819 |
case DOUBLEARRAY: |
325 | 820 |
instsize = sizeof(struct __doubleArray) + |
154 | 821 |
__OBJS2BYTES__(nInstVars) + |
822 |
(nindexedinstvars - 1) * sizeof(double); |
|
54 | 823 |
|
154 | 824 |
PROTECT_CONTEXT |
825 |
_qAlignedNew(newobj, instsize, SENDER); |
|
826 |
UNPROTECT_CONTEXT |
|
827 |
if (newobj == nil) { |
|
828 |
break; |
|
829 |
} |
|
830 |
_InstPtr(newobj)->o_class = self; |
|
369 | 831 |
__qSTORE(newobj, self); |
832 |
||
154 | 833 |
op = _InstPtr(newobj)->i_instvars; |
834 |
while (nInstVars--) |
|
835 |
*op++ = nil; |
|
54 | 836 |
#ifdef NEED_DOUBLE_ALIGN |
154 | 837 |
/* |
838 |
* care for double alignment |
|
839 |
*/ |
|
840 |
if ((INT)op & (ALIGN-1)) { |
|
841 |
*op++ = nil; |
|
842 |
} |
|
54 | 843 |
#endif |
154 | 844 |
dp = (double *)op; |
845 |
while (nindexedinstvars--) |
|
846 |
*dp++ = 0.0; |
|
847 |
RETURN ( newobj ); |
|
848 |
break; |
|
1 | 849 |
|
154 | 850 |
case WKPOINTERARRAY: |
851 |
case POINTERARRAY: |
|
852 |
nInstVars += nindexedinstvars; |
|
853 |
instsize = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
|
854 |
PROTECT_CONTEXT |
|
855 |
_qAlignedNew(newobj, instsize, SENDER); |
|
856 |
UNPROTECT_CONTEXT |
|
857 |
if (newobj == nil) { |
|
858 |
break; |
|
859 |
} |
|
860 |
_InstPtr(newobj)->o_class = self; |
|
369 | 861 |
__qSTORE(newobj, self); |
862 |
||
357 | 863 |
#if defined(FAST_ARRAY_MEMSET4) || defined(FAST_MEMSET4) |
154 | 864 |
memset4(_InstPtr(newobj)->i_instvars, nil, nInstVars); |
1 | 865 |
#else |
82 | 866 |
# if !defined(NEGATIVE_ADDRESSES) |
154 | 867 |
/* |
868 |
* knowing that nil is 0 |
|
869 |
*/ |
|
360 | 870 |
#ifdef XXmips |
92 | 871 |
# undef FAST_ARRAY_MEMSET_DOUBLES_UNROLLED |
872 |
# undef FAST_ARRAY_MEMSET_LONGLONG_UNROLLED |
|
198 | 873 |
/* seems to be slightly faster */ |
92 | 874 |
# define FAST_ARRAY_MEMSET |
875 |
#endif |
|
876 |
#ifdef sparc |
|
877 |
# define FAST_ARRAY_MEMSET_DOUBLES_UNROLLED |
|
878 |
#endif |
|
879 |
||
91 | 880 |
# if defined(FAST_ARRAY_MEMSET_DOUBLES_UNROLLED) |
154 | 881 |
op = _InstPtr(newobj)->i_instvars; |
882 |
if (nInstVars > 8) { |
|
883 |
*op++ = nil; /* for alignment */ |
|
884 |
nInstVars--; |
|
885 |
while (nInstVars >= 8) { |
|
886 |
*(double *)op = 0.0; |
|
887 |
((double *)op)[1] = 0.0; |
|
888 |
((double *)op)[2] = 0.0; |
|
889 |
((double *)op)[3] = 0.0; |
|
890 |
op += 8; |
|
891 |
nInstVars -= 8; |
|
892 |
} |
|
893 |
} |
|
894 |
while (nInstVars) { |
|
895 |
*op++ = 0; |
|
896 |
nInstVars--; |
|
897 |
} |
|
82 | 898 |
# else |
91 | 899 |
# if defined(FAST_ARRAY_MEMSET_LONGLONG_UNROLLED) |
154 | 900 |
op = _InstPtr(newobj)->i_instvars; |
901 |
if (nInstVars > 8) { |
|
902 |
*op++ = nil; /* for alignment */ |
|
903 |
nInstVars--; |
|
904 |
while (nInstVars >= 8) { |
|
905 |
*(long long *)op = 0; |
|
906 |
((long long *)op)[1] = 0; |
|
907 |
((long long *)op)[2] = 0; |
|
908 |
((long long *)op)[3] = 0; |
|
909 |
op += 8; |
|
910 |
nInstVars -= 8; |
|
911 |
} |
|
912 |
} |
|
913 |
while (nInstVars) { |
|
914 |
*op++ = 0; |
|
915 |
nInstVars--; |
|
916 |
} |
|
91 | 917 |
# else |
918 |
# if defined(FAST_ARRAY_MEMSET) |
|
154 | 919 |
memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE); |
91 | 920 |
# else |
154 | 921 |
op = _InstPtr(newobj)->i_instvars; |
922 |
while (nInstVars--) |
|
923 |
*op++ = nil; |
|
91 | 924 |
# endif |
82 | 925 |
# endif |
926 |
# endif |
|
927 |
# else |
|
154 | 928 |
op = _InstPtr(newobj)->i_instvars; |
929 |
while (nInstVars--) |
|
930 |
*op++ = nil; |
|
1 | 931 |
# endif |
932 |
#endif |
|
154 | 933 |
RETURN ( newobj ); |
934 |
break; |
|
1 | 935 |
|
154 | 936 |
default: |
937 |
/* |
|
938 |
* new:n for non-variable classes only allowed if |
|
939 |
* n == 0 |
|
940 |
*/ |
|
941 |
if (nindexedinstvars == 0) { |
|
942 |
instsize = OHDR_SIZE + __OBJS2BYTES__(nInstVars); |
|
943 |
PROTECT_CONTEXT |
|
944 |
_qAlignedNew(newobj, instsize, SENDER); |
|
945 |
UNPROTECT_CONTEXT |
|
946 |
if (newobj == nil) { |
|
947 |
break; |
|
948 |
} |
|
949 |
_InstPtr(newobj)->o_class = self; |
|
369 | 950 |
__qSTORE(newobj, self); |
951 |
||
154 | 952 |
if (nInstVars) { |
357 | 953 |
#if defined(FAST_OBJECT_MEMSET4) || defined(FAST_MEMSET4) |
154 | 954 |
memset4(_InstPtr(newobj)->i_instvars, nil, nInstVars); |
1 | 955 |
#else |
956 |
# if defined(FAST_MEMSET) && ! defined(NEGATIVE_ADDRESSES) |
|
154 | 957 |
/* |
958 |
* knowing that nil is 0 |
|
959 |
*/ |
|
960 |
memset(_InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE); |
|
1 | 961 |
# else |
154 | 962 |
op = _InstPtr(newobj)->i_instvars; |
963 |
do { |
|
964 |
*op++ = nil; |
|
965 |
} while (--nInstVars); |
|
1 | 966 |
# endif |
967 |
#endif |
|
154 | 968 |
} |
969 |
RETURN ( newobj ); |
|
970 |
} |
|
971 |
break; |
|
972 |
} |
|
973 |
} |
|
1 | 974 |
} |
68 | 975 |
%}. |
976 |
" |
|
977 |
arrive here if something went wrong ... |
|
978 |
figure out what it was |
|
979 |
" |
|
11 | 980 |
|
1 | 981 |
(anInteger isMemberOf:SmallInteger) ifFalse:[ |
154 | 982 |
" |
983 |
the argument is either not an integer, |
|
984 |
or a LargeInteger (which means that its definitely too big) |
|
985 |
" |
|
986 |
self error:'argument to new: must be Integer'. |
|
987 |
^ nil |
|
68 | 988 |
]. |
989 |
(anInteger < 0) ifTrue:[ |
|
154 | 990 |
" |
991 |
the argument is negative, |
|
992 |
" |
|
993 |
self error:'bad (negative) argument to new:'. |
|
994 |
^ nil |
|
68 | 995 |
]. |
996 |
self isVariable ifFalse:[ |
|
154 | 997 |
" |
998 |
this class does not have any indexed instance variables |
|
999 |
" |
|
1000 |
self error:'class has no indexed instvars - cannot create with new:'. |
|
1001 |
^ nil |
|
68 | 1002 |
]. |
1003 |
" |
|
1004 |
memory allocation failed. |
|
1005 |
When we arrive here, there was no memory, even after |
|
165 | 1006 |
a garbage collect. |
1007 |
This means, that the VM wanted to get some more memory from the |
|
1008 |
Operatingsystem, which was not kind enough to give it. |
|
1009 |
Bad luck - you should increase the swap space on your machine. |
|
68 | 1010 |
" |
127 | 1011 |
^ ObjectMemory allocationFailureSignal raise. |
11 | 1012 |
! |
1013 |
||
1014 |
readFrom:aStream |
|
43 | 1015 |
"read an objects printed representation from the argument, aStream |
1016 |
and return it. |
|
154 | 1017 |
The read object must be a kind of myself if its not, an error is raised. |
11 | 1018 |
This is the reverse operation to 'storeOn:'. |
43 | 1019 |
|
68 | 1020 |
WARNING: storeOn: does not handle circular references and multiple |
198 | 1021 |
references to the same object. |
1022 |
Use #storeBinary:/readBinaryFrom: for this." |
|
11 | 1023 |
|
345 | 1024 |
^ self readFrom:aStream onError:[self error:'expected: ' , self name] |
11 | 1025 |
|
91 | 1026 |
" |
1027 |
|s| |
|
11 | 1028 |
s := WriteStream on:String new. |
1029 |
#(1 2 3 4) storeOn:s. |
|
1030 |
Object readFrom:(ReadStream on:s contents) |
|
1031 |
" |
|
1032 |
! |
|
1033 |
||
198 | 1034 |
readFrom:aStream onError:exceptionBlock |
1035 |
"read an objects printed representation from the argument, aStream |
|
1036 |
and return it (i.e. the stream should contain some representation of |
|
1037 |
the object which was created using #storeOn:). |
|
1038 |
The read object must be a kind of myself if its not, the value of |
|
1039 |
exceptionBlock is returned. |
|
1040 |
To get any object, use 'Object readFrom:...', |
|
1041 |
To get any number, use 'Number readFrom:...' and so on. |
|
1042 |
This is the reverse operation to 'storeOn:'. |
|
1043 |
||
1044 |
WARNING: storeOn: does not handle circular references and multiple |
|
1045 |
references to the same object. |
|
1046 |
Use #storeBinary:/readBinaryFrom: for this." |
|
1047 |
||
1048 |
|newObject| |
|
1049 |
||
340 | 1050 |
ErrorSignal handle:[:ex | |
1051 |
ex return |
|
1052 |
] do:[ |
|
345 | 1053 |
newObject := self evaluatorClass evaluate:aStream. |
340 | 1054 |
]. |
198 | 1055 |
(newObject isKindOf:self) ifFalse:[^ exceptionBlock value]. |
1056 |
^ newObject |
|
1057 |
||
1058 |
" |
|
1059 |
|s| |
|
1060 |
s := WriteStream on:String new. |
|
1061 |
#(1 2 3 4) storeOn:s. |
|
293 | 1062 |
Transcript showCr:( |
1063 |
Array readFrom:(ReadStream on:s contents) onError:'not an Array' |
|
1064 |
) |
|
198 | 1065 |
" |
1066 |
" |
|
1067 |
|s| |
|
1068 |
s := WriteStream on:String new. |
|
1069 |
#[1 2 3 4] storeOn:s. |
|
293 | 1070 |
Transcript showCr:( |
1071 |
Array readFrom:(ReadStream on:s contents) onError:'not an Array' |
|
345 | 1072 |
) |
198 | 1073 |
" |
1074 |
! |
|
1075 |
||
11 | 1076 |
readFromString:aString |
275 | 1077 |
"create an object from its printed representation. |
1078 |
For most classes, the string is expected to be in a format created by |
|
1079 |
storeOn: or storeString; however, some (Time, Date) expect a user |
|
1080 |
readable string here. |
|
1081 |
See comments in Behavior>>readFromString:onError:, |
|
1082 |
Behavior>>readFrom: and Behavior>>readFrom:onError:" |
|
11 | 1083 |
|
345 | 1084 |
^ self readFromString:aString onError:[self error:'expected: ' , self name] |
91 | 1085 |
|
1086 |
" |
|
1087 |
Integer readFromString:'12345678901234567890' |
|
1088 |
Point readFromString:'1@2' |
|
345 | 1089 |
Point readFromString:'1' |
91 | 1090 |
" |
198 | 1091 |
! |
1092 |
||
1093 |
readFromString:aString onError:exceptionBlock |
|
275 | 1094 |
"create an object from its printed representation. |
1095 |
Here, the string is expected to be in a format created by |
|
1096 |
storeOn: or storeString; however, some classes (Time, Date) may redefine |
|
1097 |
it to expect a user readable string here. |
|
198 | 1098 |
See comments in Behavior>>readFrom: and Behavior>>readFrom:onError:" |
1099 |
||
1100 |
^ self readFrom:(ReadStream on:aString) onError:exceptionBlock |
|
1101 |
||
1102 |
" |
|
1103 |
Integer readFromString:'12345678901234567890' |
|
275 | 1104 |
Integer readFromString:'abc' |
1105 |
Integer readFromString:'abc' onError:0 |
|
198 | 1106 |
Point readFromString:'1@2' |
345 | 1107 |
Point readFromString:'0' |
275 | 1108 |
Point readFromString:'0' onError:[0@0] |
198 | 1109 |
" |
1 | 1110 |
! ! |
1111 |
||
10 | 1112 |
!Behavior methodsFor:'autoload check'! |
1113 |
||
1114 |
isLoaded |
|
1115 |
"return true, if the class has been loaded; |
|
1116 |
redefined in Autoload; see comment there" |
|
1117 |
||
1118 |
^ true |
|
1119 |
! |
|
1120 |
||
1121 |
autoload |
|
1122 |
"force autoloading - do nothing here; |
|
1123 |
redefined in Autoload; see comment there" |
|
1124 |
||
1125 |
^ self |
|
1126 |
! ! |
|
1127 |
||
91 | 1128 |
!Behavior methodsFor:'snapshots'! |
1129 |
||
1130 |
preSnapshot |
|
1131 |
"sent by ObjectMemory, before a snapshot is written. |
|
1132 |
Nothing done here." |
|
1133 |
! |
|
1134 |
||
1135 |
postSnapshot |
|
1136 |
"sent by ObjectMemory, after a snapshot has been written. |
|
1137 |
Nothing done here." |
|
1138 |
! ! |
|
1139 |
||
68 | 1140 |
!Behavior class methodsFor:'flag bit constants'! |
1141 |
||
1142 |
flagNotIndexed |
|
356 | 1143 |
"return the flag code for non-indexed instances. |
1144 |
You have to mask the flag value with indexMask when comparing |
|
1145 |
it with flagNotIndexed." |
|
68 | 1146 |
|
1147 |
^ 0 |
|
1148 |
! |
|
1149 |
||
1150 |
flagBytes |
|
356 | 1151 |
"return the flag code for byte-valued indexed instances. |
1152 |
You have to mask the flag value with indexMask when comparing |
|
1153 |
it with flagBytes." |
|
68 | 1154 |
|
1155 |
%{ /* NOCONTEXT */ |
|
1156 |
/* this is defined as a primitive to get defines from stc.h */ |
|
1157 |
||
92 | 1158 |
RETURN ( _MKSMALLINT(BYTEARRAY) ); |
68 | 1159 |
%} |
293 | 1160 |
" |
1161 |
Behavior flagBytes |
|
1162 |
" |
|
68 | 1163 |
! |
1164 |
||
1165 |
flagWords |
|
356 | 1166 |
"return the flag code for word-valued indexed instances (i.e. 2-byte). |
1167 |
You have to mask the flag value with indexMask when comparing |
|
1168 |
it with flagWords." |
|
68 | 1169 |
|
1170 |
%{ /* NOCONTEXT */ |
|
1171 |
/* this is defined as a primitive to get defines from stc.h */ |
|
1172 |
||
92 | 1173 |
RETURN ( _MKSMALLINT(WORDARRAY) ); |
68 | 1174 |
%} |
293 | 1175 |
" |
1176 |
Behavior flagWords |
|
1177 |
" |
|
68 | 1178 |
! |
1179 |
||
1180 |
flagLongs |
|
356 | 1181 |
"return the flag code for long-valued indexed instances (i.e. 4-byte). |
1182 |
You have to mask the flag value with indexMask when comparing |
|
1183 |
it with flagLongs." |
|
68 | 1184 |
|
1185 |
%{ /* NOCONTEXT */ |
|
1186 |
/* this is defined as a primitive to get defines from stc.h */ |
|
1187 |
||
92 | 1188 |
RETURN ( _MKSMALLINT(LONGARRAY) ); |
68 | 1189 |
%} |
293 | 1190 |
" |
1191 |
Behavior flagLongs |
|
1192 |
" |
|
68 | 1193 |
! |
1194 |
||
1195 |
flagFloats |
|
356 | 1196 |
"return the flag code for float-valued indexed instances (i.e. 4-byte reals). |
1197 |
You have to mask the flag value with indexMask when comparing |
|
1198 |
it with flagFloats." |
|
68 | 1199 |
|
1200 |
%{ /* NOCONTEXT */ |
|
1201 |
/* this is defined as a primitive to get defines from stc.h */ |
|
1202 |
||
92 | 1203 |
RETURN ( _MKSMALLINT(FLOATARRAY) ); |
68 | 1204 |
%} |
293 | 1205 |
" |
1206 |
Behavior flagFloats |
|
1207 |
" |
|
68 | 1208 |
! |
1209 |
||
1210 |
flagDoubles |
|
356 | 1211 |
"return the flag code for double-valued indexed instances (i.e. 8-byte reals). |
1212 |
You have to mask the flag value with indexMask when comparing |
|
1213 |
it with flagDoubles." |
|
68 | 1214 |
|
1215 |
%{ /* NOCONTEXT */ |
|
1216 |
/* this is defined as a primitive to get defines from stc.h */ |
|
1217 |
||
92 | 1218 |
RETURN ( _MKSMALLINT(DOUBLEARRAY) ); |
68 | 1219 |
%} |
293 | 1220 |
" |
1221 |
Behavior flagDoubles |
|
1222 |
" |
|
68 | 1223 |
! |
1224 |
||
1225 |
flagPointers |
|
356 | 1226 |
"return the flag code for pointer indexed instances (i.e. Array of object). |
1227 |
You have to mask the flag value with indexMask when comparing |
|
1228 |
it with flagPointers." |
|
68 | 1229 |
|
1230 |
%{ /* NOCONTEXT */ |
|
1231 |
/* this is defined as a primitive to get defines from stc.h */ |
|
1232 |
||
92 | 1233 |
RETURN ( _MKSMALLINT(POINTERARRAY) ); |
1234 |
%} |
|
293 | 1235 |
" |
1236 |
Behavior flagPointers |
|
1237 |
" |
|
92 | 1238 |
! |
1239 |
||
1240 |
flagWeakPointers |
|
356 | 1241 |
"return the flag code for weak pointer indexed instances (i.e. WeakArray). |
1242 |
You have to mask the flag value with indexMask when comparing |
|
1243 |
it with flagWeakPointers." |
|
92 | 1244 |
|
1245 |
%{ /* NOCONTEXT */ |
|
1246 |
/* this is defined as a primitive to get defines from stc.h */ |
|
1247 |
||
1248 |
RETURN ( _MKSMALLINT(WKPOINTERARRAY) ); |
|
68 | 1249 |
%} |
1250 |
! |
|
1251 |
||
1252 |
maskIndexType |
|
1253 |
"return a mask to extract all index-type bits" |
|
1254 |
||
1255 |
%{ /* NOCONTEXT */ |
|
1256 |
/* this is defined as a primitive to get defines from stc.h */ |
|
1257 |
||
92 | 1258 |
RETURN ( _MKSMALLINT(ARRAYMASK) ); |
68 | 1259 |
%} |
1260 |
! |
|
1261 |
||
345 | 1262 |
flagBehavior |
356 | 1263 |
"return the flag code which marks Behavior-like instances. |
1264 |
You have to check this single bit in the flag value when |
|
1265 |
checking for behaviors." |
|
345 | 1266 |
|
1267 |
%{ /* NOCONTEXT */ |
|
1268 |
/* this is defined as a primitive to get defines from stc.h */ |
|
1269 |
||
1270 |
RETURN ( _MKSMALLINT(BEHAVIOR_INSTS) ); |
|
1271 |
%} |
|
356 | 1272 |
|
1273 |
"consistency check: |
|
1274 |
all class-entries must be behaviors; |
|
1275 |
all behaviors must be flagged so (in its class's flags) |
|
1276 |
(otherwise, VM will bark) |
|
1277 |
all non-behaviors may not be flagged |
|
1278 |
||
1279 |
|bit| |
|
1280 |
bit := Class flagBehavior. |
|
1281 |
||
1282 |
ObjectMemory allObjectsDo:[:o| |
|
1283 |
o isBehavior ifTrue:[ |
|
1284 |
(o class flags bitTest:bit) ifFalse:[ |
|
1285 |
self halt |
|
1286 |
]. |
|
1287 |
] ifFalse:[ |
|
1288 |
(o class flags bitTest:bit) ifTrue:[ |
|
1289 |
self halt |
|
1290 |
]. |
|
1291 |
]. |
|
1292 |
o class isBehavior ifFalse:[ |
|
1293 |
self halt |
|
1294 |
] ifTrue:[ |
|
1295 |
(o class class flags bitTest:bit) ifFalse:[ |
|
1296 |
self halt |
|
1297 |
] |
|
1298 |
] |
|
1299 |
] |
|
1300 |
" |
|
345 | 1301 |
! |
1302 |
||
68 | 1303 |
flagBlock |
356 | 1304 |
"return the flag code which marks Block-like instances. |
1305 |
You have to check this single bit in the flag value when |
|
1306 |
checking for blocks." |
|
68 | 1307 |
|
1308 |
%{ /* NOCONTEXT */ |
|
1309 |
/* this is defined as a primitive to get defines from stc.h */ |
|
1310 |
||
92 | 1311 |
RETURN ( _MKSMALLINT(BLOCK_INSTS) ); |
68 | 1312 |
%} |
1313 |
! |
|
1314 |
||
1315 |
flagMethod |
|
356 | 1316 |
"return the flag code which marks Method-like instances. |
1317 |
You have to check this single bit in the flag value when |
|
1318 |
checking for methods." |
|
68 | 1319 |
|
1320 |
%{ /* NOCONTEXT */ |
|
1321 |
/* this is defined as a primitive to get defines from stc.h */ |
|
1322 |
||
92 | 1323 |
RETURN ( _MKSMALLINT(METHOD_INSTS) ); |
68 | 1324 |
%} |
1325 |
! |
|
1326 |
||
357 | 1327 |
flagNonObjectInst |
1328 |
"return the flag code which marks instances which have a |
|
362 | 1329 |
non-object instance variable (in slot 1). |
357 | 1330 |
(these are ignored by the garbage collector)" |
1331 |
||
1332 |
%{ /* NOCONTEXT */ |
|
1333 |
/* this is defined as a primitive to get defines from stc.h */ |
|
1334 |
||
1335 |
RETURN ( _MKSMALLINT(NONOBJECT_INSTS) ); |
|
1336 |
%} |
|
1337 |
! |
|
1338 |
||
68 | 1339 |
flagContext |
356 | 1340 |
"return the flag code which marks Context-like instances. |
1341 |
You have to check this single bit in the flag value when |
|
1342 |
checking for contexts." |
|
68 | 1343 |
|
1344 |
%{ /* NOCONTEXT */ |
|
1345 |
/* this is defined as a primitive to get defines from stc.h */ |
|
1346 |
||
92 | 1347 |
RETURN ( _MKSMALLINT(CONTEXT_INSTS) ); |
68 | 1348 |
%} |
1349 |
! |
|
1350 |
||
1351 |
flagBlockContext |
|
356 | 1352 |
"return the flag code which marks BlockContext-like instances. |
1353 |
You have to check this single bit in the flag value when |
|
1354 |
checking for blockContexts." |
|
68 | 1355 |
|
1356 |
%{ /* NOCONTEXT */ |
|
1357 |
/* this is defined as a primitive to get defines from stc.h */ |
|
1358 |
||
92 | 1359 |
RETURN ( _MKSMALLINT(BCONTEXT_INSTS) ); |
1360 |
%} |
|
1361 |
! |
|
1362 |
||
1363 |
flagFloat |
|
356 | 1364 |
"return the flag code which marks Float-like instances. |
1365 |
You have to check this single bit in the flag value when |
|
1366 |
checking for floats." |
|
92 | 1367 |
|
1368 |
%{ /* NOCONTEXT */ |
|
1369 |
/* this is defined as a primitive to get defines from stc.h */ |
|
1370 |
||
1371 |
RETURN ( _MKSMALLINT(FLOAT_INSTS) ); |
|
1372 |
%} |
|
1373 |
! |
|
1374 |
||
1375 |
flagSymbol |
|
356 | 1376 |
"return the flag code which marks Symbol-like instances. |
1377 |
You have to check this single bit in the flag value when |
|
1378 |
checking for symbols." |
|
92 | 1379 |
|
1380 |
%{ /* NOCONTEXT */ |
|
1381 |
/* this is defined as a primitive to get defines from stc.h */ |
|
1382 |
||
1383 |
RETURN ( _MKSMALLINT(SYMBOL_INSTS) ); |
|
68 | 1384 |
%} |
1385 |
! ! |
|
1386 |
||
1 | 1387 |
!Behavior methodsFor:'accessing'! |
1388 |
||
91 | 1389 |
name |
1390 |
"although behaviors have no name, we return something |
|
1391 |
useful here - there are many places (inspectors) where |
|
1392 |
a classes name is asked for. |
|
1393 |
Implementing this message here allows anonymous classes |
|
151 | 1394 |
and instances of them to be inspected." |
91 | 1395 |
|
1396 |
^ 'someBehavior' |
|
1397 |
! |
|
1398 |
||
356 | 1399 |
displayString |
1400 |
"although behaviors have no name, we return something |
|
1401 |
useful here - there are many places (inspectors) where |
|
1402 |
a classes name is asked for. |
|
1403 |
Implementing this message here allows instances of anonymous classes |
|
1404 |
to show a reasonable name." |
|
1405 |
||
1406 |
^ 'someBehavior' |
|
1407 |
! |
|
1408 |
||
1409 |
category |
|
1410 |
"return the category of the class. |
|
1411 |
Returning nil here, since Behavior does not define a category |
|
1412 |
(only ClassDescriptions do)." |
|
1413 |
||
1414 |
^ nil |
|
1415 |
||
1416 |
" |
|
1417 |
Point category |
|
1418 |
Behavior new category |
|
1419 |
" |
|
1420 |
! |
|
1421 |
||
1 | 1422 |
superclass |
1423 |
"return the receivers superclass" |
|
1424 |
||
1425 |
^ superclass |
|
1426 |
! |
|
1427 |
||
68 | 1428 |
selectorArray |
1429 |
"return the receivers selector array. |
|
1430 |
Notice: this is not compatible with ST-80." |
|
1 | 1431 |
|
91 | 1432 |
^ selectorArray |
1 | 1433 |
! |
1434 |
||
362 | 1435 |
selectors |
457
41c73cbee305
change classes versionString when class changes (prepare for sourceCode system)
Claus Gittinger <cg@exept.de>
parents:
442
diff
changeset
|
1436 |
"return the receivers selector array as an orderedCollection. |
41c73cbee305
change classes versionString when class changes (prepare for sourceCode system)
Claus Gittinger <cg@exept.de>
parents:
442
diff
changeset
|
1437 |
Notice: this may not be compatible with ST-80. |
362 | 1438 |
(should we return a Set ?)" |
1439 |
||
1440 |
^ selectorArray asOrderedCollection |
|
1441 |
! |
|
1442 |
||
68 | 1443 |
methodArray |
1444 |
"return the receivers method array. |
|
1445 |
Notice: this is not compatible with ST-80." |
|
1 | 1446 |
|
91 | 1447 |
^ methodArray |
1 | 1448 |
! |
1449 |
||
1450 |
methodDictionary |
|
77 | 1451 |
"return the receivers method dictionary. |
1452 |
Since no dictionary is actually present, create one for ST-80 compatibility." |
|
68 | 1453 |
|
345 | 1454 |
|dict n "{ Class: SmallInteger }"| |
1 | 1455 |
|
68 | 1456 |
dict := IdentityDictionary new. |
345 | 1457 |
n := selectorArray size. |
1458 |
1 to:n do:[:index | |
|
154 | 1459 |
dict at:(selectorArray at:index) put:(methodArray at:index) |
68 | 1460 |
]. |
1461 |
^ dict |
|
1 | 1462 |
! |
1463 |
||
362 | 1464 |
implicit_methodDict |
1465 |
"ST-80 compatibility. |
|
1466 |
This allows subclasses to assume there is an instance variable |
|
1467 |
named methodDict." |
|
1468 |
||
1469 |
^ self methodDictionary |
|
1470 |
! |
|
1471 |
||
1472 |
implicit_methodDict:aDictionary |
|
1473 |
"ST-80 compatibility. |
|
1474 |
This allows subclasses to assume there is an instance variable |
|
1475 |
named methodDict." |
|
1476 |
||
1477 |
^ self error:'not allowed to set the methodDictionary' |
|
1478 |
! |
|
1479 |
||
1 | 1480 |
instSize |
68 | 1481 |
"return the number of instance variables of the receiver. |
1482 |
This includes all superclass instance variables." |
|
1 | 1483 |
|
1484 |
^ instSize |
|
1485 |
! |
|
1486 |
||
1487 |
flags |
|
1488 |
"return the receivers flag bits" |
|
1489 |
||
1490 |
^ flags |
|
1491 |
! |
|
1492 |
||
216 | 1493 |
superclass:aClass |
1494 |
"set the superclass - this actually creates a new class, |
|
1495 |
recompiling all methods for the new one. The receiving class stays |
|
1496 |
around anonymous to allow existing instances some life. |
|
1497 |
This may change in the future (adjusting existing instances)" |
|
1498 |
||
308 | 1499 |
SubclassInfo := nil. |
1500 |
||
216 | 1501 |
"must flush caches since lookup chain changes" |
1502 |
ObjectMemory flushCaches. |
|
1503 |
||
1504 |
" |
|
1505 |
superclass := aClass |
|
1506 |
" |
|
1507 |
"for correct recompilation, just create a new class ..." |
|
1508 |
||
1509 |
aClass subclass:(self name) |
|
1510 |
instanceVariableNames:(self instanceVariableString) |
|
1511 |
classVariableNames:(self classVariableString) |
|
1512 |
poolDictionaries:'' |
|
1513 |
category:self category |
|
1514 |
! |
|
1515 |
||
1516 |
addSuperclass:aClass |
|
1517 |
"EXPERIMENTAL MI support: add aClass to the set of classes, from which instances |
|
1518 |
inherit protocol." |
|
1519 |
||
1520 |
"first, check if the class is abstract - |
|
1521 |
allows abstract mixins are allowed in the current implementation" |
|
1522 |
||
1523 |
aClass instSize == 0 ifFalse:[ |
|
1524 |
self error:'only abstract mixins allowed'. |
|
1525 |
^ self |
|
1526 |
]. |
|
1527 |
otherSuperclasses isNil ifTrue:[ |
|
1528 |
otherSuperclasses := Array with:aClass |
|
1529 |
] ifFalse:[ |
|
1530 |
otherSuperclasses := otherSuperclasses copyWith:aClass |
|
1531 |
]. |
|
308 | 1532 |
SubclassInfo := nil. |
216 | 1533 |
ObjectMemory flushCaches |
1534 |
! |
|
1535 |
||
1536 |
removeSuperclass:aClass |
|
1537 |
"EXPERIMENTAL MI support: remove aClass from the set of classes, from which instances |
|
1538 |
inherit protocol." |
|
1539 |
||
1540 |
otherSuperclasses notNil ifTrue:[ |
|
1541 |
otherSuperclasses := otherSuperclasses copyWithout:aClass. |
|
1542 |
otherSuperclasses isEmpty ifTrue:[ |
|
1543 |
otherSuperclasses := nil |
|
1544 |
]. |
|
308 | 1545 |
SubclassInfo := nil. |
216 | 1546 |
ObjectMemory flushCaches |
1547 |
]. |
|
1548 |
! |
|
1549 |
||
1550 |
selectors:newSelectors methods:newMethods |
|
1551 |
"set both selector array and method array of the receiver, |
|
1552 |
and flush caches" |
|
1553 |
||
1554 |
ObjectMemory flushCaches. |
|
1555 |
selectorArray := newSelectors. |
|
1556 |
methodArray := newMethods |
|
1557 |
! |
|
1558 |
||
1559 |
addSelector:newSelector withMethod:newMethod |
|
1560 |
"add the method given by 2nd argument under the selector given by |
|
1561 |
1st argument to the methodDictionary. Flush all caches." |
|
1562 |
||
1563 |
|nargs| |
|
1564 |
||
249 | 1565 |
(self primAddSelector:newSelector withMethod:newMethod) ifFalse:[^ false]. |
216 | 1566 |
self changed:#methodDictionary with:newSelector. |
1567 |
||
1568 |
" |
|
1569 |
if I have no subclasses, all we have to flush is cached |
|
1570 |
data for myself ... (actually, in any case all that needs |
|
1571 |
to be flushed is info for myself and all of my subclasses) |
|
1572 |
" |
|
1573 |
" |
|
1574 |
problem: this is slower; since looking for all subclasses is (currently) |
|
1575 |
a bit slow :-( |
|
1576 |
We need the hasSubclasses-info bit in Behavior; now |
|
1577 |
||
1578 |
self withAllSubclassesDo:[:aClass | |
|
1579 |
ObjectMemory flushInlineCachesFor:aClass withArgs:nargs. |
|
1580 |
ObjectMemory flushMethodCacheFor:aClass |
|
1581 |
]. |
|
1582 |
" |
|
1583 |
||
1584 |
" |
|
1585 |
actually, we would do better with less flushing ... |
|
1586 |
" |
|
1587 |
nargs := newSelector numArgs. |
|
1588 |
||
1589 |
ObjectMemory flushMethodCache. |
|
1590 |
ObjectMemory flushInlineCachesWithArgs:nargs. |
|
1591 |
||
1592 |
^ true |
|
1593 |
! |
|
1594 |
||
1595 |
addSelector:newSelector withLazyMethod:newMethod |
|
1596 |
"add the method given by 2nd argument under the selector given by |
|
1597 |
1st argument to the methodDictionary. Since it does not flush |
|
1598 |
any caches, this is only allowed for lazy methods." |
|
1599 |
||
1600 |
newMethod isLazyMethod ifFalse:[ |
|
1601 |
self error:'operation only allowed for lazy methods'. |
|
1602 |
^ false |
|
1603 |
]. |
|
362 | 1604 |
"/ oops: we must flush, if this method already exists ... |
1605 |
(selectorArray includes:newSelector) ifTrue:[ |
|
1606 |
ObjectMemory flushCaches |
|
1607 |
]. |
|
249 | 1608 |
(self primAddSelector:newSelector withMethod:newMethod) ifTrue:[ |
216 | 1609 |
self changed:#methodDictionary with:newSelector. |
1610 |
^ true |
|
1611 |
]. |
|
1612 |
^ false |
|
1613 |
! |
|
1614 |
||
1615 |
removeSelector:aSelector |
|
1616 |
"remove the selector, aSelector and its associated method |
|
1617 |
from the methodDictionary" |
|
1618 |
||
1619 |
|index oldSelectorArray oldMethodArray newSelectorArray newMethodArray| |
|
1620 |
||
1621 |
index := selectorArray identityIndexOf:aSelector startingAt:1. |
|
1622 |
(index == 0) ifTrue:[^ false]. |
|
1623 |
||
1624 |
newSelectorArray := selectorArray copyWithoutIndex:index. |
|
1625 |
newMethodArray := methodArray copyWithoutIndex:index. |
|
1626 |
oldSelectorArray := selectorArray. |
|
1627 |
oldMethodArray := methodArray. |
|
1628 |
selectorArray := newSelectorArray. |
|
1629 |
methodArray := newMethodArray. |
|
1630 |
" |
|
1631 |
[ |
|
1632 |
|nargs| |
|
1633 |
nargs := aSelector numArgs. |
|
1634 |
ObjectMemory flushMethodCache. |
|
1635 |
ObjectMemory flushInlineCachesWithArgs:nargs. |
|
1636 |
] value |
|
1637 |
" |
|
1638 |
" |
|
1639 |
actually, we would do better with less flushing ... |
|
1640 |
" |
|
1641 |
ObjectMemory flushCaches. |
|
1642 |
^ true |
|
1643 |
! ! |
|
1644 |
||
1645 |
!Behavior methodsFor:'queries'! |
|
1646 |
||
127 | 1647 |
sizeOfInst:n |
1648 |
"return the number of bytes required for an instance of |
|
1649 |
myself with n indexed instance variables. The argument n |
|
1650 |
should be zero for classes without indexed instance variables. |
|
1651 |
See Behavior>>niceNew: for an application of this." |
|
216 | 1652 |
|
127 | 1653 |
|nInstvars| |
1654 |
||
1655 |
nInstvars := self instSize. |
|
1656 |
%{ |
|
1657 |
int nBytes; |
|
1658 |
||
1659 |
nBytes = _intVal(nInstvars) * sizeof(OBJ) + OHDR_SIZE; |
|
249 | 1660 |
if (__isSmallInteger(n)) { |
154 | 1661 |
int nIndex; |
127 | 1662 |
|
154 | 1663 |
nIndex = _intVal(n); |
1664 |
switch (_intVal(_INST(flags)) & ARRAYMASK) { |
|
1665 |
case BYTEARRAY: |
|
1666 |
nBytes += nIndex; |
|
1667 |
if (nBytes & (ALIGN - 1)) { |
|
1668 |
nBytes = (nBytes & ~(ALIGN - 1)) + ALIGN; |
|
1669 |
} |
|
1670 |
break; |
|
127 | 1671 |
|
154 | 1672 |
case WORDARRAY: |
1673 |
nBytes += nIndex * sizeof(short); |
|
1674 |
if (nBytes & (ALIGN - 1)) { |
|
1675 |
nBytes = (nBytes & ~(ALIGN - 1)) + ALIGN; |
|
1676 |
} |
|
1677 |
break; |
|
127 | 1678 |
|
154 | 1679 |
case LONGARRAY: |
1680 |
nBytes += nIndex * sizeof(long); |
|
1681 |
break; |
|
127 | 1682 |
|
154 | 1683 |
case FLOATARRAY: |
1684 |
nBytes += nIndex * sizeof(float); |
|
1685 |
break; |
|
127 | 1686 |
|
154 | 1687 |
case DOUBLEARRAY: |
1688 |
nBytes += nIndex * sizeof(double); |
|
1689 |
break; |
|
127 | 1690 |
|
154 | 1691 |
default: |
1692 |
nBytes += nIndex * sizeof(OBJ); |
|
1693 |
break; |
|
1694 |
} |
|
127 | 1695 |
} |
1696 |
RETURN (_MKSMALLINT(nBytes)); |
|
1697 |
%} |
|
1698 |
! |
|
1699 |
||
1 | 1700 |
isVariable |
1701 |
"return true, if instances have indexed instance variables" |
|
1702 |
||
362 | 1703 |
"this could also be defined as: |
154 | 1704 |
^ (flags bitAnd:(Behavior maskIndexType)) ~~ 0 |
68 | 1705 |
" |
1 | 1706 |
|
1707 |
%{ /* NOCONTEXT */ |
|
1708 |
||
1709 |
RETURN ( (_intVal(_INST(flags)) & ARRAYMASK) ? true : false ); |
|
1710 |
%} |
|
1711 |
! |
|
1712 |
||
1713 |
isFixed |
|
1714 |
"return true, if instances do not have indexed instance variables" |
|
1715 |
||
362 | 1716 |
"this could also be defined as: |
154 | 1717 |
^ self isVariable not |
68 | 1718 |
" |
1719 |
||
1 | 1720 |
%{ /* NOCONTEXT */ |
1721 |
||
1722 |
RETURN ( (_intVal(_INST(flags)) & ARRAYMASK) ? false : true ); |
|
1723 |
%} |
|
1724 |
! |
|
1725 |
||
1726 |
isBits |
|
1727 |
"return true, if instances have indexed byte or short instance variables. |
|
68 | 1728 |
Ignore long, float and double arrays, since ST-80 code using isBits are probably |
1 | 1729 |
not prepared to handle them correctly." |
1730 |
||
1731 |
%{ /* NOCONTEXT */ |
|
1732 |
||
1733 |
REGISTER int flags; |
|
1734 |
||
1735 |
RETURN ( (((flags = (_intVal(_INST(flags)) & ARRAYMASK)) == BYTEARRAY) |
|
154 | 1736 |
|| (flags == WORDARRAY)) ? true : false ); |
1 | 1737 |
%} |
1738 |
! |
|
1739 |
||
1740 |
isBytes |
|
1741 |
"return true, if instances have indexed byte instance variables" |
|
1742 |
||
362 | 1743 |
"this could also be defined as: |
154 | 1744 |
^ (flags bitAnd:(Behavior maskIndexType)) == Behavior flagBytes |
68 | 1745 |
" |
1 | 1746 |
%{ /* NOCONTEXT */ |
1747 |
||
1748 |
RETURN ( ((_intVal(_INST(flags)) & ARRAYMASK) == BYTEARRAY) ? true : false ); |
|
1749 |
%} |
|
1750 |
! |
|
1751 |
||
1752 |
isWords |
|
1753 |
"return true, if instances have indexed short instance variables" |
|
1754 |
||
362 | 1755 |
"this could also be defined as: |
154 | 1756 |
^ (flags bitAnd:(Behavior maskIndexType)) == Behavior flagWords |
68 | 1757 |
" |
1 | 1758 |
%{ /* NOCONTEXT */ |
1759 |
||
1760 |
RETURN ( ((_intVal(_INST(flags)) & ARRAYMASK) == WORDARRAY) ? true : false ); |
|
1761 |
%} |
|
1762 |
! |
|
1763 |
||
1764 |
isLongs |
|
1765 |
"return true, if instances have indexed long instance variables" |
|
1766 |
||
362 | 1767 |
"this could also be defined as: |
154 | 1768 |
^ (flags bitAnd:(Behavior maskIndexType)) == Behavior flagLongs |
68 | 1769 |
" |
1 | 1770 |
%{ /* NOCONTEXT */ |
1771 |
||
1772 |
RETURN ( ((_intVal(_INST(flags)) & ARRAYMASK) == LONGARRAY) ? true : false ); |
|
1773 |
%} |
|
1774 |
! |
|
1775 |
||
1776 |
isFloats |
|
1777 |
"return true, if instances have indexed float instance variables" |
|
1778 |
||
362 | 1779 |
"this could also be defined as: |
154 | 1780 |
^ (flags bitAnd:(Behavior maskIndexType)) == Behavior flagFloats |
68 | 1781 |
" |
1 | 1782 |
%{ /* NOCONTEXT */ |
68 | 1783 |
|
1 | 1784 |
RETURN ( ((_intVal(_INST(flags)) & ARRAYMASK) == FLOATARRAY) ? true : false ); |
1785 |
%} |
|
1786 |
! |
|
1787 |
||
1788 |
isDoubles |
|
1789 |
"return true, if instances have indexed double instance variables" |
|
1790 |
||
362 | 1791 |
"this could also be defined as: |
154 | 1792 |
^ (flags bitAnd:(Behavior maskIndexType)) == Behavior flagDoubles |
68 | 1793 |
" |
1 | 1794 |
%{ /* NOCONTEXT */ |
68 | 1795 |
|
1 | 1796 |
RETURN ( ((_intVal(_INST(flags)) & ARRAYMASK) == DOUBLEARRAY) ? true : false ); |
1797 |
%} |
|
1798 |
! |
|
1799 |
||
1800 |
isPointers |
|
2 | 1801 |
"return true, if instances have pointer instance variables |
1802 |
i.e. are either non-indexed or have indexed pointer variables" |
|
1 | 1803 |
|
68 | 1804 |
"QUESTION: should we ignore WeakPointers ?" |
1805 |
||
1 | 1806 |
%{ /* NOCONTEXT */ |
68 | 1807 |
|
1 | 1808 |
REGISTER int flags; |
1809 |
||
2 | 1810 |
flags = _intVal(_INST(flags)) & ARRAYMASK; |
1811 |
switch (flags) { |
|
154 | 1812 |
default: |
1813 |
/* normal objects */ |
|
1814 |
RETURN ( true ); |
|
2 | 1815 |
|
154 | 1816 |
case BYTEARRAY: |
1817 |
case WORDARRAY: |
|
1818 |
case LONGARRAY: |
|
1819 |
case FLOATARRAY: |
|
1820 |
case DOUBLEARRAY: |
|
1821 |
RETURN (false ); |
|
2 | 1822 |
|
154 | 1823 |
case WKPOINTERARRAY: |
1824 |
/* what about those ? */ |
|
1825 |
RETURN (true ); |
|
2 | 1826 |
} |
1 | 1827 |
%} |
2 | 1828 |
! |
1829 |
||
1 | 1830 |
isBehavior |
47 | 1831 |
"return true, if the receiver is describing another objects behavior, |
45 | 1832 |
i.e. is a class. Defined to avoid the need to use isKindOf:" |
1 | 1833 |
|
1834 |
^ true |
|
45 | 1835 |
|
91 | 1836 |
" |
1837 |
True isBehavior |
|
1838 |
true isBehavior |
|
1839 |
" |
|
1 | 1840 |
! |
1841 |
||
47 | 1842 |
canBeSubclassed |
1843 |
"return true, if its allowed to create subclasses of the receiver. |
|
1844 |
This method is redefined in SmallInteger and UndefinedObject, since |
|
68 | 1845 |
instances are detected by their pointer-fields, i.e. they do not have |
1846 |
a class entry (you dont have to understand this :-)" |
|
47 | 1847 |
|
1848 |
^ true |
|
1849 |
! |
|
1850 |
||
1 | 1851 |
hasMultipleSuperclasses |
68 | 1852 |
"Return true, if this class inherits from other classes |
1853 |
(beside its primary superclass). |
|
1854 |
This method is a preparation for a future multiple inheritance extension |
|
1855 |
- currently it is not supported by the VM" |
|
1 | 1856 |
|
68 | 1857 |
^ otherSuperclasses notNil |
1 | 1858 |
! |
1859 |
||
1860 |
superclasses |
|
68 | 1861 |
"return a collection of the receivers immediate superclasses. |
1862 |
This method is a preparation for a future multiple inheritance extension |
|
1863 |
- currently it is not supported by the VM" |
|
1 | 1864 |
|
345 | 1865 |
|a| |
1866 |
||
1867 |
a := Array with:superclass. |
|
68 | 1868 |
otherSuperclasses notNil ifTrue:[ |
345 | 1869 |
^ a , otherSuperclasses |
68 | 1870 |
]. |
345 | 1871 |
^ a |
45 | 1872 |
|
91 | 1873 |
" |
1874 |
String superclasses |
|
1875 |
" |
|
1 | 1876 |
! |
1877 |
||
1878 |
allSuperclasses |
|
1879 |
"return a collection of the receivers accumulated superclasses" |
|
1880 |
||
1881 |
|aCollection theSuperClass| |
|
1882 |
||
1883 |
theSuperClass := superclass. |
|
1884 |
theSuperClass notNil ifTrue:[ |
|
154 | 1885 |
aCollection := OrderedCollection new. |
1886 |
[theSuperClass notNil] whileTrue:[ |
|
1887 |
aCollection add:theSuperClass. |
|
1888 |
theSuperClass := theSuperClass superclass |
|
1889 |
] |
|
1 | 1890 |
]. |
1891 |
^ aCollection |
|
45 | 1892 |
|
91 | 1893 |
" |
1894 |
String allSuperclasses |
|
1895 |
" |
|
1 | 1896 |
! |
1897 |
||
1898 |
withAllSuperclasses |
|
1899 |
"return a collection containing the receiver and all |
|
1900 |
of the receivers accumulated superclasses" |
|
1901 |
||
1902 |
|aCollection theSuperClass| |
|
1903 |
||
1904 |
aCollection := OrderedCollection with:self. |
|
1905 |
theSuperClass := superclass. |
|
1906 |
[theSuperClass notNil] whileTrue:[ |
|
154 | 1907 |
aCollection add:theSuperClass. |
1908 |
theSuperClass := theSuperClass superclass |
|
1 | 1909 |
]. |
1910 |
^ aCollection |
|
45 | 1911 |
|
91 | 1912 |
" |
1913 |
String withAllSuperclasses |
|
1914 |
" |
|
1 | 1915 |
! |
1916 |
||
1917 |
subclasses |
|
1918 |
"return a collection of the direct subclasses of the receiver" |
|
1919 |
||
1920 |
|newColl| |
|
1921 |
||
308 | 1922 |
SubclassInfo notNil ifTrue:[ |
1923 |
newColl := SubclassInfo at:self ifAbsent:nil. |
|
1924 |
newColl notNil ifTrue:[^ newColl asOrderedCollection] |
|
1925 |
]. |
|
1926 |
||
1 | 1927 |
newColl := OrderedCollection new. |
1928 |
self subclassesDo:[:aClass | |
|
154 | 1929 |
newColl add:aClass |
1 | 1930 |
]. |
1931 |
^ newColl |
|
45 | 1932 |
|
91 | 1933 |
" |
1934 |
Collection subclasses |
|
1935 |
" |
|
1 | 1936 |
! |
1937 |
||
1938 |
allSubclasses |
|
1939 |
"return a collection of all subclasses (direct AND indirect) of |
|
45 | 1940 |
the receiver. There will be no specific order, in which entries |
1941 |
are returned." |
|
1 | 1942 |
|
1943 |
|newColl| |
|
1944 |
||
1945 |
newColl := OrderedCollection new. |
|
1946 |
self allSubclassesDo:[:aClass | |
|
154 | 1947 |
newColl add:aClass |
1 | 1948 |
]. |
1949 |
^ newColl |
|
45 | 1950 |
|
91 | 1951 |
" |
1952 |
Collection allSubclasses |
|
1953 |
" |
|
45 | 1954 |
! |
1955 |
||
1956 |
allSubclassesInOrder |
|
1957 |
"return a collection of all subclasses (direct AND indirect) of |
|
1958 |
the receiver. Higher level subclasses will come before lower ones." |
|
1959 |
||
1960 |
|newColl| |
|
1961 |
||
1962 |
newColl := OrderedCollection new. |
|
1963 |
self allSubclassesInOrderDo:[:aClass | |
|
154 | 1964 |
newColl add:aClass |
45 | 1965 |
]. |
1966 |
^ newColl |
|
1967 |
||
91 | 1968 |
" |
1969 |
Collection allSubclassesInOrder |
|
1970 |
" |
|
1 | 1971 |
! |
1972 |
||
1973 |
withAllSubclasses |
|
1974 |
"return a collection containing the receiver and |
|
1975 |
all subclasses (direct AND indirect) of the receiver" |
|
1976 |
||
1977 |
|newColl| |
|
1978 |
||
1979 |
newColl := OrderedCollection with:self. |
|
1980 |
self allSubclassesDo:[:aClass | |
|
154 | 1981 |
newColl add:aClass |
1 | 1982 |
]. |
1983 |
^ newColl |
|
45 | 1984 |
|
91 | 1985 |
" |
1986 |
Collection withAllSubclasses |
|
1987 |
" |
|
1 | 1988 |
! |
1989 |
||
1990 |
isSubclassOf:aClass |
|
1991 |
"return true, if I am a subclass of the argument, aClass" |
|
1992 |
||
1993 |
|theClass| |
|
2 | 1994 |
|
1 | 1995 |
theClass := superclass. |
1996 |
[theClass notNil] whileTrue:[ |
|
154 | 1997 |
(theClass == aClass) ifTrue:[^ true]. |
375 | 1998 |
%{ |
1999 |
if (__isBehaviorLike(theClass)) { |
|
2000 |
theClass = __ClassInstPtr(theClass)->c_superclass; |
|
2001 |
} else { |
|
2002 |
theClass = nil; |
|
2003 |
} |
|
2004 |
%}. |
|
2005 |
"/ theClass := theClass superclass. |
|
1 | 2006 |
]. |
2007 |
^ false |
|
45 | 2008 |
|
91 | 2009 |
" |
2010 |
String isSubclassOf:Collection |
|
2011 |
LinkedList isSubclassOf:Array |
|
2012 |
1 isSubclassOf:Number <- will fail since 1 is no class |
|
2013 |
" |
|
1 | 2014 |
! |
2015 |
||
151 | 2016 |
allInstVarNames |
2017 |
"return a collection of all the instance variable name-strings |
|
2018 |
this includes all superclass-instance variables. |
|
2019 |
Since Behavior has no idea of instvar-names, return an empty collection |
|
2020 |
here. Redefined in ClassDescription." |
|
2021 |
||
2022 |
^ #() |
|
2023 |
! |
|
2024 |
||
2025 |
allClassVarNames |
|
2026 |
"return a collection of all the class variable name-strings |
|
2027 |
this includes all superclass-class variables. |
|
2028 |
Since Behavior has no idea of classvar-names, return an empty collection |
|
2029 |
here. Redefined in ClassDescription." |
|
2030 |
||
2031 |
^ #() |
|
2032 |
! |
|
2033 |
||
1 | 2034 |
allInstances |
2035 |
"return a collection of all my instances" |
|
2036 |
||
362 | 2037 |
"Read the documentation on why there seem to be no |
2038 |
instances of SmallInteger and UndefinedObject" |
|
2039 |
||
1 | 2040 |
|coll| |
2041 |
||
165 | 2042 |
coll := OrderedCollection new:100. |
45 | 2043 |
self allInstancesDo:[:anObject | |
154 | 2044 |
coll add:anObject |
1 | 2045 |
]. |
68 | 2046 |
^ coll |
25 | 2047 |
|
91 | 2048 |
" |
2049 |
ScrollBar allInstances |
|
2050 |
" |
|
25 | 2051 |
! |
2052 |
||
328 | 2053 |
allSubInstances |
25 | 2054 |
"return a collection of all instances of myself and |
328 | 2055 |
instances of all subclasses of myself." |
25 | 2056 |
|
2057 |
|coll| |
|
2058 |
||
165 | 2059 |
coll := OrderedCollection new:100. |
331 | 2060 |
self allSubInstancesDo:[:anObject | |
154 | 2061 |
(anObject isKindOf:self) ifTrue:[ |
2062 |
coll add:anObject |
|
2063 |
] |
|
25 | 2064 |
]. |
68 | 2065 |
^ coll |
25 | 2066 |
|
91 | 2067 |
" |
328 | 2068 |
View allSubInstances |
91 | 2069 |
" |
1 | 2070 |
! |
2071 |
||
328 | 2072 |
allDerivedInstances |
2073 |
"return a collection of all instances of myself and |
|
2074 |
instances of all subclasses of myself. |
|
2075 |
This method is going to be removed for protocol compatibility with |
|
2076 |
other STs; use allSubInstances" |
|
2077 |
||
345 | 2078 |
self obsoleteMethodWarning:'please use #allSubInstances'. |
328 | 2079 |
^ self allSubInstances |
2080 |
! |
|
2081 |
||
154 | 2082 |
hasInstances |
2083 |
"return true, if there are any instances of myself" |
|
2084 |
||
362 | 2085 |
"Read the documentation on why there seem to be no |
2086 |
instances of SmallInteger and UndefinedObject" |
|
2087 |
||
369 | 2088 |
"/ ObjectMemory allObjectsDo:[:anObject | |
2089 |
"/ (anObject class == self) ifTrue:[ |
|
2090 |
"/ ^ true |
|
2091 |
"/ ] |
|
2092 |
"/ ]. |
|
2093 |
ObjectMemory allInstancesOf:self do:[:anObject | |
|
154 | 2094 |
^ true |
2095 |
]. |
|
2096 |
^ false |
|
2097 |
||
2098 |
" |
|
2099 |
Object hasInstances |
|
2100 |
SequenceableCollection hasInstances |
|
2101 |
Float hasInstances |
|
362 | 2102 |
SmallInteger hasInstances |
154 | 2103 |
" |
2104 |
! |
|
2105 |
||
1 | 2106 |
instanceCount |
362 | 2107 |
"return the number of instances of myself." |
2108 |
||
2109 |
"Read the documentation on why there seem to be no |
|
2110 |
instances of SmallInteger and UndefinedObject" |
|
1 | 2111 |
|
2112 |
|count| |
|
2113 |
||
2114 |
count := 0. |
|
369 | 2115 |
"/ ObjectMemory allObjectsDo:[:anObject | |
2116 |
"/ (anObject class == self) ifTrue:[ |
|
2117 |
"/ count := count + 1 |
|
2118 |
"/ ] |
|
2119 |
"/ ]. |
|
2120 |
ObjectMemory allInstancesOf:self do:[:anObject | |
|
2121 |
count := count + 1 |
|
1 | 2122 |
]. |
2123 |
^ count |
|
45 | 2124 |
|
91 | 2125 |
" |
2126 |
View instanceCount |
|
154 | 2127 |
Object instanceCount |
2128 |
Float instanceCount |
|
2129 |
SequenceableCollection instanceCount |
|
362 | 2130 |
SmallInteger instanceCount .... mhmh - hear, hear |
91 | 2131 |
" |
45 | 2132 |
! |
2133 |
||
2134 |
derivedInstanceCount |
|
2135 |
"return the number of instances of myself and of subclasses" |
|
2136 |
||
2137 |
|count| |
|
2138 |
||
2139 |
count := 0. |
|
2140 |
ObjectMemory allObjectsDo:[:anObject | |
|
154 | 2141 |
(anObject isKindOf:self) ifTrue:[ |
2142 |
count := count + 1 |
|
2143 |
] |
|
45 | 2144 |
]. |
2145 |
^ count |
|
2146 |
||
91 | 2147 |
" |
2148 |
View derivedInstanceCount |
|
154 | 2149 |
SequenceableCollection derivedInstanceCount |
91 | 2150 |
" |
2 | 2151 |
! |
2152 |
||
2153 |
selectorIndex:aSelector |
|
2154 |
"return the index in the arrays for given selector aSelector" |
|
2155 |
||
91 | 2156 |
^ selectorArray identityIndexOf:aSelector startingAt:1 |
2 | 2157 |
! |
2158 |
||
295 | 2159 |
includesSelector:aSelector |
2160 |
"for ST-80 compatibility" |
|
2161 |
||
2162 |
^ self implements:aSelector |
|
2163 |
! |
|
2164 |
||
2 | 2165 |
compiledMethodAt:aSelector |
68 | 2166 |
"return the method for given selector aSelector or nil. |
2167 |
Only methods in the receiver - not in the superclass chain are tested." |
|
2 | 2168 |
|
2169 |
|index| |
|
2170 |
||
328 | 2171 |
selectorArray isNil ifTrue:[ |
2172 |
('oops: nil selectorArray in ' , self name) errorPrintNL. |
|
2173 |
^ nil |
|
2174 |
]. |
|
2175 |
||
91 | 2176 |
index := selectorArray identityIndexOf:aSelector startingAt:1. |
2 | 2177 |
(index == 0) ifTrue:[^ nil]. |
91 | 2178 |
^ methodArray at:index |
45 | 2179 |
|
91 | 2180 |
" |
2181 |
Object compiledMethodAt:#== |
|
2182 |
(Object compiledMethodAt:#==) category |
|
2183 |
" |
|
2 | 2184 |
! |
2185 |
||
2186 |
sourceCodeAt:aSelector |
|
68 | 2187 |
"return the methods source for given selector aSelector or nil. |
2188 |
Only methods in the receiver - not in the superclass chain are tested." |
|
2 | 2189 |
|
3 | 2190 |
|method| |
2 | 2191 |
|
3 | 2192 |
method := self compiledMethodAt:aSelector. |
2193 |
method isNil ifTrue:[^ nil]. |
|
2194 |
^ method source |
|
2 | 2195 |
|
91 | 2196 |
" |
2197 |
True sourceCodeAt:#ifTrue: |
|
2198 |
Object sourceCodeAt:#== |
|
2199 |
Behavior sourceCodeAt:#sourceCodeAt: |
|
2200 |
" |
|
2 | 2201 |
! |
2202 |
||
68 | 2203 |
lookupMethodFor:aSelector |
2204 |
"return the method, which would be executed if aSelector was sent to |
|
2205 |
an instance of the receiver. I.e. the selector arrays of the receiver |
|
2206 |
and all of its superclasses are searched for aSelector. |
|
2207 |
Return the method, or nil if instances do not understand aSelector. |
|
2208 |
EXPERIMENTAL: take care of multiple superclasses." |
|
2209 |
||
2210 |
|m cls| |
|
2211 |
||
2212 |
cls := self. |
|
2213 |
[cls notNil] whileTrue:[ |
|
154 | 2214 |
m := cls compiledMethodAt:aSelector. |
2215 |
m notNil ifTrue:[^ m]. |
|
2216 |
cls hasMultipleSuperclasses ifTrue:[ |
|
2217 |
cls superclasses do:[:aSuperClass | |
|
2218 |
m := aSuperClass lookupMethodFor:aSelector. |
|
2219 |
m notNil ifTrue:[^ m]. |
|
2220 |
]. |
|
2221 |
^ nil |
|
2222 |
] ifFalse:[ |
|
2223 |
cls := cls superclass |
|
2224 |
] |
|
68 | 2225 |
]. |
2226 |
^ nil |
|
2227 |
! |
|
2228 |
||
2229 |
cachedLookupMethodFor:aSelector |
|
2230 |
"return the method, which would be executed if aSelector was sent to |
|
2231 |
an instance of the receiver. I.e. the selector arrays of the receiver |
|
2232 |
and all of its superclasses are searched for aSelector. |
|
2233 |
Return the method, or nil if instances do not understand aSelector. |
|
2234 |
This interface provides exactly the same information as #lookupMethodFor:, |
|
2235 |
but uses the lookup-cache in the VM for faster search. |
|
2236 |
However, keep in mind, that doing a lookup through the cache also adds new |
|
2237 |
entries and can thus slow down the system by polluting the cache with |
|
2238 |
irrelevant entries. (do NOT loop over all objects calling this method). |
|
2239 |
Does NOT (currently) handle MI" |
|
2240 |
||
2241 |
%{ /* NOCONTEXT */ |
|
362 | 2242 |
extern OBJ __lookup(); |
2243 |
||
2244 |
RETURN ( __lookup(self, aSelector, SENDER) ); |
|
68 | 2245 |
%} |
2246 |
||
91 | 2247 |
" |
2248 |
String cachedLookupMethodFor:#= |
|
2249 |
String cachedLookupMethodFor:#asOrderedCollection |
|
2250 |
" |
|
68 | 2251 |
! |
2252 |
||
2 | 2253 |
hasMethods |
2254 |
"return true, if there are any (local) methods in this class" |
|
2255 |
||
91 | 2256 |
^ (methodArray size ~~ 0) |
10 | 2257 |
|
91 | 2258 |
" |
2259 |
True hasMethods |
|
2260 |
True class hasMethods |
|
2261 |
" |
|
2 | 2262 |
! |
2263 |
||
2264 |
implements:aSelector |
|
68 | 2265 |
"return true, if the receiver implements aSelector. |
2266 |
(i.e. implemented in THIS class - NOT in a superclass). |
|
10 | 2267 |
Dont use this method to check if someone responds to a message - |
45 | 2268 |
use #canUnderstand: on the class or #respondsTo: on the instance |
10 | 2269 |
to do this." |
2 | 2270 |
|
91 | 2271 |
^ (selectorArray identityIndexOf:aSelector startingAt:1) ~~ 0 |
2 | 2272 |
|
91 | 2273 |
" |
2274 |
True implements:#ifTrue: |
|
2275 |
True implements:#== |
|
2276 |
" |
|
2 | 2277 |
! |
2278 |
||
2279 |
canUnderstand:aSelector |
|
68 | 2280 |
"return true, if the receiver or one of its superclasses implements aSelector. |
2281 |
(i.e. true if my instances understand aSelector)" |
|
2 | 2282 |
|
68 | 2283 |
^ (self lookupMethodFor:aSelector) notNil |
10 | 2284 |
|
91 | 2285 |
" |
2286 |
True canUnderstand:#ifTrue: |
|
2287 |
True canUnderstand:#== |
|
2288 |
True canUnderstand:#do: |
|
2289 |
" |
|
2 | 2290 |
! |
2291 |
||
2292 |
whichClassImplements:aSelector |
|
328 | 2293 |
"obsolete interface; |
2294 |
use whichClassIncludesSelector: for ST-80 compatibility." |
|
2295 |
||
2296 |
^ self whichClassIncludesSelector:aSelector |
|
2297 |
! |
|
2298 |
||
2299 |
whichClassIncludesSelector:aSelector |
|
68 | 2300 |
"return the class in the inheritance chain, which implements the method |
2301 |
for aSelector; return nil if none. |
|
2302 |
EXPERIMENTAL: handle multiple superclasses" |
|
2303 |
||
2304 |
|cls| |
|
2 | 2305 |
|
68 | 2306 |
cls := self. |
2307 |
[cls notNil] whileTrue:[ |
|
154 | 2308 |
(cls implements:aSelector) ifTrue:[^ cls]. |
2309 |
cls hasMultipleSuperclasses ifTrue:[ |
|
2310 |
cls superclasses do:[:aSuperClass | |
|
2311 |
|implementingClass| |
|
2 | 2312 |
|
328 | 2313 |
implementingClass := aSuperClass whichClassIncludesSelector:aSelector. |
154 | 2314 |
implementingClass notNil ifTrue:[^ implementingClass]. |
2315 |
]. |
|
2316 |
^ nil |
|
2317 |
] ifFalse:[ |
|
2318 |
cls := cls superclass |
|
2319 |
] |
|
2 | 2320 |
]. |
2321 |
^ nil |
|
2322 |
||
91 | 2323 |
" |
328 | 2324 |
String whichClassIncludesSelector:#== |
2325 |
String whichClassIncludesSelector:#collect: |
|
91 | 2326 |
" |
2 | 2327 |
! |
2328 |
||
2329 |
inheritsFrom:aClass |
|
68 | 2330 |
"return true, if the receiver inherits methods from aClass" |
2 | 2331 |
|
2332 |
^ self isSubclassOf:aClass |
|
45 | 2333 |
|
91 | 2334 |
" |
2335 |
True inheritsFrom:Object |
|
2336 |
LinkedList inheritsFrom:Array |
|
2337 |
" |
|
2 | 2338 |
! |
2339 |
||
154 | 2340 |
selectorAtMethod:aMethod ifAbsent:failBlock |
2341 |
"return the selector for given method aMethod |
|
2342 |
or the value of failBlock, if not found." |
|
2 | 2343 |
|
2344 |
|index| |
|
2345 |
||
91 | 2346 |
index := methodArray identityIndexOf:aMethod startingAt:1. |
154 | 2347 |
(index == 0) ifTrue:[^ failBlock value]. |
91 | 2348 |
^ selectorArray at:index |
154 | 2349 |
|
2350 |
" |
|
2351 |
|m| |
|
2352 |
||
2353 |
m := Object compiledMethodAt:#copy. |
|
328 | 2354 |
Object selectorAtMethod:m ifAbsent:['oops']. |
154 | 2355 |
" |
2356 |
" |
|
2357 |
|m| |
|
2358 |
||
2359 |
m := Object compiledMethodAt:#copy. |
|
328 | 2360 |
Fraction selectorAtMethod:m ifAbsent:['oops']. |
154 | 2361 |
" |
2362 |
! |
|
2363 |
||
328 | 2364 |
selectorAtMethod:aMethod |
154 | 2365 |
"Return the selector for given method aMethod." |
2366 |
||
2367 |
^ self selectorAtMethod:aMethod ifAbsent:[nil] |
|
328 | 2368 |
|
2369 |
" |
|
2370 |
|m| |
|
2371 |
||
2372 |
m := Object compiledMethodAt:#copy. |
|
2373 |
Fraction selectorAtMethod:m. |
|
2374 |
" |
|
2375 |
" |
|
2376 |
|m| |
|
2377 |
||
2378 |
m := Object compiledMethodAt:#copy. |
|
2379 |
Object selectorAtMethod:m. |
|
2380 |
" |
|
2 | 2381 |
! |
2382 |
||
2383 |
containsMethod:aMethod |
|
77 | 2384 |
"Return true, if the argument, aMethod is a method of myself" |
2 | 2385 |
|
91 | 2386 |
methodArray isNil ifTrue:[^ false]. "degenerated class" |
2387 |
^ (methodArray identityIndexOf:aMethod startingAt:1) ~~ 0 |
|
1 | 2388 |
! ! |
2389 |
||
2390 |
!Behavior methodsFor:'private accessing'! |
|
2391 |
||
2392 |
setSuperclass:sup selectors:sels methods:m instSize:i flags:f |
|
45 | 2393 |
"set some inst vars. |
77 | 2394 |
this method is for special uses only - there will be no recompilation |
198 | 2395 |
and no change record is written here. Also, if the receiver class has |
2396 |
already been in use, future operation of the system is not guaranteed to |
|
2397 |
be correct, since no caches are flushed. |
|
2398 |
Therefore: do NOT use it; use Behavior>>superclass: (or flush the caches, at least)" |
|
1 | 2399 |
|
308 | 2400 |
SubclassInfo := nil. |
1 | 2401 |
superclass := sup. |
91 | 2402 |
selectorArray := sels. |
2403 |
methodArray := m. |
|
1 | 2404 |
instSize := i. |
2405 |
flags := f |
|
2406 |
! |
|
2407 |
||
2408 |
setSuperclass:aClass |
|
45 | 2409 |
"set the superclass of the receiver. |
77 | 2410 |
this method is for special uses only - there will be no recompilation |
198 | 2411 |
and no change record written here. Also, if the receiver class has |
2412 |
already been in use, future operation of the system is not guaranteed to |
|
2413 |
be correct, since no caches are flushed. |
|
2414 |
Therefore: do NOT use it; use Behavior>>superclass: (or flush the caches, at least)" |
|
1 | 2415 |
|
308 | 2416 |
SubclassInfo := nil. |
1 | 2417 |
superclass := aClass |
2418 |
! |
|
2419 |
||
68 | 2420 |
setOtherSuperclasses:anArrayOfClasses |
2421 |
"EXPERIMENTAL: set the other superclasses of the receiver. |
|
77 | 2422 |
this method is for special uses only - there will be no recompilation |
68 | 2423 |
and no change record written here; |
2424 |
Do NOT use it." |
|
2425 |
||
308 | 2426 |
SubclassInfo := nil. |
68 | 2427 |
otherSuperclasses := anArrayOfClasses |
2428 |
! |
|
2429 |
||
1 | 2430 |
instSize:aNumber |
45 | 2431 |
"set the instance size. |
77 | 2432 |
this method is for special uses only - there will be no recompilation |
68 | 2433 |
and no change record written here; |
2434 |
Do NOT use it." |
|
1 | 2435 |
|
2436 |
instSize := aNumber |
|
2437 |
! |
|
2438 |
||
2439 |
flags:aNumber |
|
45 | 2440 |
"set the flags. |
77 | 2441 |
this method is for special uses only - there will be no recompilation |
68 | 2442 |
and no change record written here; |
2443 |
Do NOT use it." |
|
1 | 2444 |
|
2445 |
flags := aNumber |
|
2446 |
! |
|
2447 |
||
154 | 2448 |
setSelectors:sels methods:m |
2449 |
"set some inst vars. |
|
2450 |
this method is for special uses only - there will be no recompilation |
|
2451 |
and no change record written here; |
|
2452 |
Do NOT use it." |
|
2453 |
||
2454 |
selectorArray := sels. |
|
2455 |
methodArray := m. |
|
2456 |
! |
|
2457 |
||
77 | 2458 |
setSelectorArray:anArray |
45 | 2459 |
"set the selector array of the receiver. |
77 | 2460 |
this method is for special uses only - there will be no recompilation |
2461 |
and no change record written here. |
|
2462 |
NOT for general use." |
|
1 | 2463 |
|
91 | 2464 |
selectorArray := anArray |
1 | 2465 |
! |
2466 |
||
77 | 2467 |
setMethodArray:anArray |
45 | 2468 |
"set the method array of the receiver. |
77 | 2469 |
this method is for special uses only - there will be no recompilation |
2470 |
and no change record written here. |
|
2471 |
NOT for general use." |
|
1 | 2472 |
|
91 | 2473 |
methodArray := anArray |
77 | 2474 |
! |
2475 |
||
2476 |
setMethodDictionary:aDictionary |
|
2477 |
"set the receivers method dictionary. |
|
2478 |
Since no dictionary is actually used, decompose into selector- and |
|
2479 |
method arrays and set those. For ST-80 compatibility. |
|
2480 |
NOT for general use." |
|
2481 |
||
91 | 2482 |
|n newSelectorArray newMethodArray idx| |
77 | 2483 |
|
2484 |
n := aDictionary size. |
|
359 | 2485 |
newSelectorArray := Array basicNew:n. |
2486 |
newMethodArray := Array basicNew:n. |
|
77 | 2487 |
idx := 1. |
2488 |
aDictionary keysAndValuesDo:[:sel :method | |
|
154 | 2489 |
newSelectorArray at:idx put:sel. |
2490 |
newMethodArray at:idx put:method. |
|
2491 |
idx := idx + 1 |
|
77 | 2492 |
]. |
91 | 2493 |
selectorArray := newSelectorArray. |
2494 |
methodArray := newMethodArray |
|
2495 |
! |
|
2496 |
||
249 | 2497 |
primAddSelector:newSelector withMethod:newMethod |
91 | 2498 |
"add the method given by 2nd argument under the selector given by |
2499 |
the 1st argument to the methodDictionary. |
|
249 | 2500 |
Does NOT flush any caches, does NOT write a change record. |
91 | 2501 |
|
2502 |
Do not use this in normal situations, strange behavior will be |
|
2503 |
the consequence. |
|
2504 |
I.e. executing obsolete methods, since the old method will still |
|
2505 |
be executed out of the caches." |
|
2506 |
||
2507 |
|index oldSelectorArray oldMethodArray newSelectorArray newMethodArray| |
|
2508 |
||
2509 |
(newSelector isMemberOf:Symbol) ifFalse:[ |
|
154 | 2510 |
self error:'invalid selector'. |
2511 |
^ false |
|
91 | 2512 |
]. |
2513 |
newMethod isNil ifTrue:[ |
|
154 | 2514 |
self error:'invalid method'. |
2515 |
^ false |
|
91 | 2516 |
]. |
2517 |
||
2518 |
index := selectorArray identityIndexOf:newSelector startingAt:1. |
|
2519 |
(index == 0) ifTrue:[ |
|
154 | 2520 |
" |
2521 |
a new selector |
|
2522 |
" |
|
2523 |
newSelectorArray := selectorArray copyWith:newSelector. |
|
2524 |
newMethodArray := methodArray copyWith:newMethod. |
|
2525 |
" |
|
2526 |
keep a reference so they wont go away ... |
|
2527 |
mhmh: this is no longer needed - try without |
|
2528 |
" |
|
2529 |
oldSelectorArray := selectorArray. |
|
2530 |
oldMethodArray := methodArray. |
|
2531 |
selectorArray := newSelectorArray. |
|
2532 |
methodArray := newMethodArray |
|
91 | 2533 |
] ifFalse:[ |
154 | 2534 |
methodArray at:index put:newMethod |
91 | 2535 |
]. |
2536 |
^ true |
|
1 | 2537 |
! ! |
2538 |
||
68 | 2539 |
!Behavior methodsFor:'compiler interface'! |
2540 |
||
2541 |
compiler |
|
249 | 2542 |
"return the compiler to use for this class. |
345 | 2543 |
OBSOLETE: This is the old ST/X interface, kept for migration. |
2544 |
Dont use it - it will vanish." |
|
249 | 2545 |
|
308 | 2546 |
^ self compilerClass |
249 | 2547 |
! |
2548 |
||
2549 |
compilerClass |
|
68 | 2550 |
"return the compiler to use for this class - |
2551 |
this can be redefined in special classes, to get classes with |
|
2552 |
Lisp, Prolog, ASN1, Basic :-) or whatever syntax." |
|
2553 |
||
2554 |
^ Compiler |
|
345 | 2555 |
! |
2556 |
||
2557 |
evaluatorClass |
|
2558 |
"return the compiler to use for expression evaluation for this class - |
|
2559 |
this can be redefined in special classes, to get classes with |
|
2560 |
Lisp, Prolog, ASN1, Basic :-) or whatever syntax." |
|
2561 |
||
2562 |
^ Compiler |
|
68 | 2563 |
! ! |
2564 |
||
216 | 2565 |
!Behavior methodsFor:'enumerating'! |
1 | 2566 |
|
2567 |
allInstancesDo:aBlock |
|
45 | 2568 |
"evaluate aBlock for all of my instances" |
1 | 2569 |
|
369 | 2570 |
"/ ObjectMemory allObjectsDo:[:anObject | |
2571 |
"/ (anObject class == self) ifTrue:[ |
|
2572 |
"/ aBlock value:anObject |
|
2573 |
"/ ] |
|
2574 |
"/ ] |
|
2575 |
||
2576 |
ObjectMemory allInstancesOf:self do:[:anObject | |
|
2577 |
aBlock value:anObject |
|
1 | 2578 |
] |
45 | 2579 |
|
91 | 2580 |
" |
2581 |
StandardSystemView allInstancesDo:[:v | Transcript showCr:(v name)] |
|
2582 |
" |
|
45 | 2583 |
! |
2584 |
||
2585 |
allDerivedInstancesDo:aBlock |
|
328 | 2586 |
"evaluate aBlock for all of my instances and all instances of subclasses. |
2587 |
This method is going to be removed for protocol compatibility with |
|
2588 |
other STs; use allSubInstancesDo:" |
|
2589 |
||
345 | 2590 |
self obsoleteMethodWarning:'please use #allSubInstancesDo:'. |
328 | 2591 |
self allSubInstancesDo:aBlock |
2592 |
||
2593 |
" |
|
2594 |
StandardSystemView allDerivedInstancesDo:[:v | Transcript showCr:(v name)] |
|
2595 |
" |
|
2596 |
! |
|
2597 |
||
2598 |
allSubInstancesDo:aBlock |
|
45 | 2599 |
"evaluate aBlock for all of my instances and all instances of subclasses" |
2600 |
||
2601 |
ObjectMemory allObjectsDo:[:anObject | |
|
154 | 2602 |
(anObject isKindOf:self) ifTrue:[ |
2603 |
aBlock value:anObject |
|
2604 |
] |
|
45 | 2605 |
] |
2606 |
||
91 | 2607 |
" |
328 | 2608 |
StandardSystemView allSubInstancesDo:[:v | Transcript showCr:(v name)] |
91 | 2609 |
" |
1 | 2610 |
! |
2611 |
||
2612 |
subclassesDo:aBlock |
|
198 | 2613 |
"evaluate the argument, aBlock for all immediate subclasses. |
2614 |
This will only enumerate globally known classes - for anonymous |
|
2615 |
behaviors, you have to walk over all instances of Behavior." |
|
1 | 2616 |
|
308 | 2617 |
|coll| |
2618 |
||
2619 |
SubclassInfo isNil ifTrue:[ |
|
2620 |
Behavior subclassInfo |
|
2621 |
]. |
|
2622 |
SubclassInfo notNil ifTrue:[ |
|
2623 |
coll := SubclassInfo at:self ifAbsent:nil. |
|
2624 |
coll notNil ifTrue:[ |
|
2625 |
coll do:aBlock. |
|
2626 |
]. |
|
2627 |
^ self |
|
2628 |
]. |
|
2629 |
||
2 | 2630 |
Smalltalk allBehaviorsDo:[:aClass | |
154 | 2631 |
(aClass superclass == self) ifTrue:[ |
2632 |
aBlock value:aClass |
|
2633 |
] |
|
1 | 2634 |
] |
45 | 2635 |
|
154 | 2636 |
" |
2637 |
Collection subclassesDo:[:c | Transcript showCr:(c name)] |
|
2638 |
" |
|
1 | 2639 |
! |
2640 |
||
2641 |
allSubclassesDo:aBlock |
|
45 | 2642 |
"evaluate aBlock for all of my subclasses. |
198 | 2643 |
There is no specific order, in which the entries are enumerated. |
2644 |
This will only enumerate globally known classes - for anonymous |
|
2645 |
behaviors, you have to walk over all instances of Behavior." |
|
1 | 2646 |
|
2 | 2647 |
Smalltalk allBehaviorsDo:[:aClass | |
154 | 2648 |
(aClass isSubclassOf:self) ifTrue:[ |
2649 |
aBlock value:aClass |
|
2650 |
] |
|
1 | 2651 |
] |
45 | 2652 |
|
198 | 2653 |
" |
2654 |
Collection allSubclassesDo:[:c | Transcript showCr:(c name)] |
|
2655 |
" |
|
1 | 2656 |
! |
2657 |
||
2658 |
allSubclassesInOrderDo:aBlock |
|
45 | 2659 |
"evaluate aBlock for all of my subclasses. |
198 | 2660 |
Higher level subclasses will be enumerated before the deeper ones, |
2661 |
so the order in which aBlock gets called is ok to fileOut classes in |
|
2662 |
correct order for later fileIn. |
|
2663 |
This will only enumerate globally known classes - for anonymous |
|
2664 |
behaviors, you have to walk over all instances of Behavior" |
|
1 | 2665 |
|
2666 |
self subclassesDo:[:aClass | |
|
154 | 2667 |
aBlock value:aClass. |
2668 |
aClass allSubclassesInOrderDo:aBlock |
|
1 | 2669 |
] |
45 | 2670 |
|
198 | 2671 |
" |
2672 |
Collection allSubclassesInOrderDo:[:c | Transcript showCr:(c name)] |
|
2673 |
" |
|
1 | 2674 |
! |
2675 |
||
2676 |
allSuperclassesDo:aBlock |
|
45 | 2677 |
"evaluate aBlock for all of my superclasses" |
1 | 2678 |
|
2679 |
|theClass| |
|
2680 |
||
2681 |
theClass := superclass. |
|
2682 |
[theClass notNil] whileTrue:[ |
|
154 | 2683 |
aBlock value:theClass. |
2684 |
theClass := theClass superclass |
|
1 | 2685 |
] |
45 | 2686 |
|
198 | 2687 |
" |
2688 |
String allSuperclassesDo:[:c | Transcript showCr:(c name)] |
|
2689 |
" |
|
1 | 2690 |
! ! |
2 | 2691 |
|
68 | 2692 |
!Behavior methodsFor:'binary storage'! |
2 | 2693 |
|
343 | 2694 |
storeBinaryDefinitionOn: stream manager: manager |
2695 |
"binary store of a classes definition. |
|
2696 |
Classes will store the name only and restore by looking for |
|
2697 |
that name in the Smalltalk dictionary." |
|
2698 |
||
2699 |
| myName | |
|
2700 |
||
2701 |
myName := self name. |
|
2702 |
stream nextNumber:4 put:self signature. |
|
2703 |
stream nextNumber:2 put:0. |
|
2704 |
stream nextNumber:2 put:myName size. |
|
2705 |
myName do:[:c| |
|
2706 |
stream nextPut:c asciiValue |
|
2707 |
] |
|
2708 |
||
2709 |
" |
|
2710 |
|s| |
|
2711 |
s := WriteStream on:ByteArray new. |
|
2712 |
#(1 2 3 4) storeBinaryOn:s. |
|
2713 |
Object readBinaryFrom:(ReadStream on:s contents) |
|
2714 |
||
2715 |
|s| |
|
2716 |
s := WriteStream on:ByteArray new. |
|
2717 |
Rectangle storeBinaryOn:s. |
|
2718 |
Object readBinaryFrom:(ReadStream on:s contents) |
|
2719 |
" |
|
2720 |
! |
|
2721 |
||
11 | 2722 |
readBinaryFrom:aStream |
2723 |
"read an objects binary representation from the argument, |
|
2724 |
aStream and return it. |
|
198 | 2725 |
The read object must be a kind of myself, otherwise an error is raised. |
2726 |
To get any object, use 'Object readBinaryFrom:...', |
|
2727 |
To get any number, use 'Number readBinaryFrom:...' and so on. |
|
2728 |
This is the reverse operation to 'storeBinaryOn:'. " |
|
2729 |
||
2730 |
^ self readBinaryFrom:aStream onError:[self error:('expected ' , self name)] |
|
2731 |
||
2732 |
" |
|
2733 |
|s| |
|
2734 |
s := WriteStream on:(ByteArray new). |
|
2735 |
#(1 2 3 4) storeBinaryOn:s. |
|
2736 |
Object readBinaryFrom:(ReadStream on:s contents) |
|
2737 |
" |
|
2738 |
" |
|
2739 |
|s| |
|
2740 |
s := 'testFile' asFilename writeStream binary. |
|
2741 |
#(1 2 3 4) storeBinaryOn:s. |
|
2742 |
'hello world' storeBinaryOn:s. |
|
2743 |
s close. |
|
2744 |
||
2745 |
s := 'testFile' asFilename readStream binary. |
|
2746 |
Transcript showCr:(Object readBinaryFrom:s). |
|
2747 |
Transcript showCr:(Object readBinaryFrom:s). |
|
2748 |
s close. |
|
2749 |
" |
|
2750 |
! |
|
2751 |
||
2752 |
readBinaryFrom:aStream onError:exceptionBlock |
|
2753 |
"read an objects binary representation from the argument, |
|
2754 |
aStream and return it. |
|
2755 |
The read object must be a kind of myself, otherwise the value of |
|
2756 |
the exceptionBlock is returned. |
|
2757 |
To get any object, use 'Object readBinaryFrom:...', |
|
2758 |
To get any number, use 'Number readBinaryFrom:...' and so on. |
|
11 | 2759 |
This is the reverse operation to 'storeBinaryOn:'. " |
2760 |
||
2761 |
|newObject| |
|
2762 |
||
2763 |
newObject := (BinaryInputManager new:1024) readFrom:aStream. |
|
198 | 2764 |
(newObject isKindOf:self) ifFalse:[^ exceptionBlock value]. |
11 | 2765 |
^ newObject |
2766 |
||
91 | 2767 |
" |
2768 |
|s| |
|
198 | 2769 |
s := WriteStream on:(ByteArray new). |
11 | 2770 |
#(1 2 3 4) storeBinaryOn:s. |
198 | 2771 |
Object readBinaryFrom:(ReadStream on:s contents) onError:['oops'] |
2772 |
" |
|
2773 |
" |
|
2774 |
|s| |
|
2775 |
s := WriteStream on:(ByteArray new). |
|
2776 |
#[1 2 3 4] storeBinaryOn:s. |
|
2777 |
Array readBinaryFrom:(ReadStream on:s contents) onError:['oops'] |
|
11 | 2778 |
" |
2779 |
! |
|
2780 |
||
43 | 2781 |
binaryDefinitionFrom:stream manager:manager |
343 | 2782 |
"sent during a binary read by the input manager. |
2783 |
Read the definition on an empty instance (of my class) from stream. |
|
43 | 2784 |
All pointer instances are left nil, while all bits are read in here. |
2785 |
return the new object." |
|
2 | 2786 |
|
43 | 2787 |
|obj t |
2788 |
basicSize "{ Class: SmallInteger }" | |
|
2 | 2789 |
|
2790 |
self isPointers ifTrue: [ |
|
343 | 2791 |
"/ |
2792 |
"/ inst size not needed - if you uncomment the line below, |
|
2793 |
"/ also uncomment the corresponding line in |
|
2794 |
"/ Object>>storeBinaryDefinitionOn:manager: |
|
2795 |
"/ |
|
2796 |
"/ stream next. "skip instSize" |
|
154 | 2797 |
self isVariable ifTrue: [ |
2798 |
^ self basicNew:(stream nextNumber:3) |
|
2799 |
]. |
|
2800 |
^ self basicNew |
|
2 | 2801 |
]. |
2802 |
||
343 | 2803 |
" |
2804 |
an object with bit-valued instance variables. |
|
2805 |
These are read here. |
|
2806 |
" |
|
43 | 2807 |
basicSize := stream nextNumber:4. |
2808 |
obj := self basicNew:basicSize. |
|
2809 |
||
2 | 2810 |
self isBytes ifTrue: [ |
154 | 2811 |
stream nextBytes:basicSize into:obj |
2 | 2812 |
] ifFalse: [ |
154 | 2813 |
self isWords ifTrue: [ |
2814 |
1 to:basicSize do:[:i | |
|
213 | 2815 |
obj basicAt:i put:(stream nextNumber:2) |
154 | 2816 |
] |
2817 |
] ifFalse:[ |
|
2818 |
self isLongs ifTrue: [ |
|
2819 |
1 to:basicSize do:[:i | |
|
213 | 2820 |
obj basicAt:i put:(stream nextNumber:4) |
154 | 2821 |
] |
2822 |
] ifFalse:[ |
|
2823 |
self isFloats ifTrue: [ |
|
2824 |
"could do it in one big read on machines which use IEEE floats ..." |
|
343 | 2825 |
t := Float basicNew. |
154 | 2826 |
1 to:basicSize do:[:i | |
2827 |
Float readBinaryIEEESingleFrom:stream into:t. |
|
2828 |
obj basicAt:i put: t |
|
2829 |
] |
|
2830 |
] ifFalse:[ |
|
2831 |
self isDoubles ifTrue: [ |
|
2832 |
"could do it in one big read on machines which use IEEE doubles ..." |
|
343 | 2833 |
t := Float basicNew. |
154 | 2834 |
1 to:basicSize do:[:i | |
2835 |
Float readBinaryIEEEDoubleFrom:stream into:t. |
|
2836 |
obj basicAt:i put: t |
|
2837 |
] |
|
2838 |
] |
|
2839 |
] |
|
2840 |
] |
|
2841 |
] |
|
2 | 2842 |
]. |
2843 |
^obj |
|
308 | 2844 |
! |
2845 |
||
2846 |
canCloneFrom:anObject |
|
2847 |
"return true, if this class can clone an obsolete object as retrieved |
|
2848 |
by a binary load. Subclasses which do not want to have obsolete objects |
|
343 | 2849 |
be converted, should redefine this method to return false. |
2850 |
(However, conversion is never done silently in a binary load; you |
|
2851 |
have to have a handler for the binaryload errors and for the conversion |
|
2852 |
request signal.)" |
|
308 | 2853 |
|
2854 |
^ true |
|
2855 |
! |
|
2856 |
||
2857 |
cloneFrom:aPrototype |
|
2858 |
"return an instance of myself with variables initialized from |
|
343 | 2859 |
a prototype. This is used when instances of obsolete classes are |
2860 |
binary loaded and a conversion is done on the obsolete object. |
|
2861 |
UserClasses may redefine this for better conversions." |
|
308 | 2862 |
|
2863 |
|newInst indexed myInfo otherInfo varIndexAssoc| |
|
2864 |
||
2865 |
indexed := false. |
|
2866 |
aPrototype class isVariable ifTrue:[ |
|
2867 |
self isVariable ifTrue:[ |
|
2868 |
indexed := true. |
|
2869 |
]. |
|
2870 |
"otherwise, these are lost ..." |
|
2871 |
]. |
|
2872 |
indexed ifTrue:[ |
|
2873 |
newInst := self basicNew:aPrototype basicSize |
|
2874 |
] ifFalse:[ |
|
2875 |
newInst := self basicNew |
|
2876 |
]. |
|
2877 |
||
2878 |
myInfo := self instanceVariableOffsets. |
|
2879 |
otherInfo := aPrototype class instanceVariableOffsets. |
|
2880 |
myInfo keysAndValuesDo:[:name :index | |
|
2881 |
varIndexAssoc := otherInfo at:name ifAbsent:[]. |
|
2882 |
varIndexAssoc notNil ifTrue:[ |
|
2883 |
newInst instVarAt:index put:(aPrototype instVarAt:(varIndexAssoc value)) |
|
2884 |
] |
|
2885 |
]. |
|
2886 |
indexed ifTrue:[ |
|
2887 |
1 to:aPrototype basicSize do:[:index | |
|
2888 |
newInst basicAt:index put:(aPrototype basicAt:index) |
|
2889 |
]. |
|
2890 |
]. |
|
2891 |
^ newInst |
|
2892 |
||
2893 |
" |
|
421 | 2894 |
Class withoutUpdatingChangesDo:[ |
442 | 2895 |
Point subclass:#Point3D |
308 | 2896 |
instanceVariableNames:'z' |
2897 |
classVariableNames:'' |
|
2898 |
poolDictionaries:'' |
|
2899 |
category:'testing'. |
|
442 | 2900 |
(Point3D cloneFrom:1@2) inspect. |
421 | 2901 |
] |
308 | 2902 |
" |
2903 |
||
2904 |
" |
|
421 | 2905 |
Class withoutUpdatingChangesDo:[ |
442 | 2906 |
Point variableSubclass:#Point3D |
308 | 2907 |
instanceVariableNames:'z' |
2908 |
classVariableNames:'' |
|
2909 |
poolDictionaries:'' |
|
2910 |
category:'testing'. |
|
442 | 2911 |
(Point3D cloneFrom:#(1 2 3)) inspect. |
421 | 2912 |
] |
308 | 2913 |
" |
2914 |
||
2915 |
" |
|
2916 |
|someObject| |
|
2917 |
||
421 | 2918 |
Class withoutUpdatingChangesDo:[ |
442 | 2919 |
Object subclass:#TestClass1 |
308 | 2920 |
instanceVariableNames:'foo bar' |
2921 |
classVariableNames:'' |
|
2922 |
poolDictionaries:'' |
|
2923 |
category:'testing'. |
|
442 | 2924 |
someObject := TestClass1 new. |
2925 |
someObject instVarAt:1 put:'foo'; instVarAt:2 put:'bar'. |
|
2926 |
Object subclass:#TestClass2 |
|
308 | 2927 |
instanceVariableNames:'bar foo' |
2928 |
classVariableNames:'' |
|
2929 |
poolDictionaries:'' |
|
2930 |
category:'testing'. |
|
442 | 2931 |
(TestClass2 cloneFrom:someObject) inspect. |
421 | 2932 |
] |
308 | 2933 |
" |
2 | 2934 |
! ! |