1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
1
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
Class subclass:#Metaclass
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Kernel-Classes'
|
|
18 |
!
|
|
19 |
|
|
20 |
Metaclass comment:'
|
|
21 |
|
5
|
22 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
1
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
every class-class is a subclass of Metaclass
|
|
26 |
- this adds support for creating new subclasses or changing the definition
|
|
27 |
of an already existing class.
|
|
28 |
|
33
|
29 |
$Header: /cvs/stx/stx/libbasic/Metaclass.st,v 1.7 1994-01-08 16:27:40 claus Exp $
|
1
|
30 |
'!
|
|
31 |
|
|
32 |
!Metaclass methodsFor:'creating classes'!
|
|
33 |
|
|
34 |
name:newName inEnvironment:aSystemDictionary
|
|
35 |
subclassOf:aClass
|
|
36 |
instanceVariableNames:stringOfInstVarNames
|
|
37 |
variable:variableBoolean
|
|
38 |
words:wordsBoolean
|
|
39 |
pointers:pointersBoolean
|
|
40 |
classVariableNames:stringOfClassVarNames
|
|
41 |
poolDictionaries:stringOfPoolNames
|
|
42 |
category:categoryString
|
|
43 |
comment:commentString
|
|
44 |
changed:changed
|
|
45 |
|
13
|
46 |
"this is the main workhorse for installing new classes - special care
|
|
47 |
has to be taken, when changing an existing classes definition. In this
|
|
48 |
case, some or all of the methods and subclasses methods have to be
|
|
49 |
recompiled.
|
|
50 |
Also, the old class(es) are still kept (but not accessable as a global),
|
|
51 |
to allow existing instances some life.
|
|
52 |
This might change in the future.
|
|
53 |
"
|
|
54 |
|
1
|
55 |
|newClass newMetaclass nInstVars nameString classSymbol oldClass
|
|
56 |
allSubclasses classVarChange instVarChange superClassChange newComment
|
10
|
57 |
changeSet1 changeSet2 offset oldOffsets newOffsets addedNames
|
13
|
58 |
anyChange oldInstVars newInstVars oldClassVars newClassVars upd|
|
|
59 |
|
|
60 |
"this method is too complex and should be splitted into managable pieces ..."
|
|
61 |
|
|
62 |
newName = aClass name ifTrue:[
|
|
63 |
self error:'trying to create circular class definition'.
|
|
64 |
^ nil
|
|
65 |
].
|
1
|
66 |
|
|
67 |
nInstVars := stringOfInstVarNames countWords.
|
|
68 |
nameString := newName asString.
|
13
|
69 |
classSymbol := newName asSymbol.
|
1
|
70 |
newComment := commentString.
|
|
71 |
|
13
|
72 |
"look, if it already exists as a class"
|
1
|
73 |
(aSystemDictionary includesKey:classSymbol) ifTrue:[
|
|
74 |
oldClass := aSystemDictionary at:classSymbol.
|
13
|
75 |
(oldClass isBehavior not or:[oldClass name ~= newName]) ifTrue:[
|
|
76 |
oldClass := nil.
|
|
77 |
] ifFalse:[
|
|
78 |
oldClass allSuperclasses do:[:aClass |
|
|
79 |
aClass name = newName ifTrue:[
|
|
80 |
self error:'trying to create circular class definition'.
|
|
81 |
^ nil
|
|
82 |
]
|
|
83 |
].
|
|
84 |
|
|
85 |
newComment isNil ifTrue:[
|
|
86 |
newComment := oldClass comment
|
|
87 |
]
|
1
|
88 |
]
|
|
89 |
].
|
|
90 |
|
|
91 |
"create the metaclass first"
|
|
92 |
newMetaclass := Metaclass new.
|
|
93 |
newMetaclass setSuperclass:(aClass class).
|
|
94 |
newMetaclass instSize:(aClass class instSize).
|
|
95 |
newMetaclass flags:0. "not indexed"
|
|
96 |
newMetaclass setName:(nameString , 'class').
|
|
97 |
newMetaclass classVariableString:'' "stringOfClassVarNames".
|
|
98 |
newMetaclass setComment:newComment category:categoryString.
|
|
99 |
|
|
100 |
newClass := newMetaclass new.
|
|
101 |
newClass setSuperclass:aClass.
|
|
102 |
newClass instSize:(aClass instSize + nInstVars).
|
|
103 |
|
|
104 |
(variableBoolean == true) ifTrue:[
|
|
105 |
pointersBoolean ifTrue:[
|
|
106 |
newClass flags:4 "pointerarray"
|
|
107 |
] ifFalse:[
|
|
108 |
wordsBoolean ifTrue:[
|
|
109 |
newClass flags:2 "wordarray"
|
|
110 |
] ifFalse:[
|
|
111 |
newClass flags:1 "bytearray"
|
|
112 |
]
|
|
113 |
]
|
|
114 |
] ifFalse:[
|
|
115 |
"this is a backward compatible hack"
|
|
116 |
|
|
117 |
(variableBoolean == #float) ifTrue:[
|
|
118 |
newClass flags:6 "float array"
|
|
119 |
] ifFalse:[
|
|
120 |
(variableBoolean == #double) ifTrue:[
|
|
121 |
newClass flags:7 "double array"
|
|
122 |
] ifFalse:[
|
|
123 |
(variableBoolean == #long) ifTrue:[
|
|
124 |
newClass flags:3 "long array"
|
|
125 |
] ifFalse:[
|
|
126 |
newClass flags:0
|
|
127 |
]
|
|
128 |
]
|
|
129 |
].
|
|
130 |
].
|
|
131 |
|
|
132 |
newClass setName:nameString.
|
|
133 |
(nInstVars ~~ 0) ifTrue:[
|
|
134 |
newClass instanceVariableString:stringOfInstVarNames
|
|
135 |
].
|
|
136 |
oldClass notNil ifTrue:[
|
|
137 |
"setting first will make new class clear obsolete classvars"
|
|
138 |
newClass setClassVariableString:(oldClass classVariableString)
|
|
139 |
].
|
|
140 |
newClass classVariableString:stringOfClassVarNames.
|
|
141 |
|
2
|
142 |
oldClass isNil ifTrue:[
|
|
143 |
self addChangeRecordForClass:newClass.
|
1
|
144 |
|
|
145 |
commentString notNil ifTrue:[
|
|
146 |
newClass comment:commentString
|
2
|
147 |
].
|
|
148 |
aSystemDictionary at:classSymbol put:newClass.
|
|
149 |
Smalltalk changed.
|
|
150 |
^ newClass
|
|
151 |
].
|
|
152 |
|
10
|
153 |
|
|
154 |
oldInstVars := oldClass instanceVariableString asCollectionOfWords.
|
|
155 |
newInstVars := newClass instanceVariableString asCollectionOfWords.
|
|
156 |
oldClassVars := oldClass classVariableString asCollectionOfWords.
|
|
157 |
newClassVars := newClass classVariableString asCollectionOfWords.
|
|
158 |
|
2
|
159 |
"if only category/comment has changed, do not recompile .."
|
1
|
160 |
|
2
|
161 |
(oldClass superclass == newClass superclass) ifTrue:[
|
|
162 |
(oldClass instSize == newClass instSize) ifTrue:[
|
|
163 |
(oldClass flags == newClass flags) ifTrue:[
|
|
164 |
(oldClass name = newClass name) ifTrue:[
|
10
|
165 |
(oldInstVars = newInstVars) ifTrue:[
|
|
166 |
(oldClassVars = newClassVars) ifTrue:[
|
|
167 |
anyChange := false.
|
|
168 |
|
|
169 |
|
|
170 |
oldClass instanceVariableString:(newClass instanceVariableString).
|
|
171 |
oldClass setClassVariableString:(newClass classVariableString).
|
|
172 |
|
2
|
173 |
(newComment ~= oldClass comment) ifTrue:[
|
10
|
174 |
oldClass comment:newComment. "already writes change-chunk"
|
2
|
175 |
].
|
|
176 |
oldClass category ~= categoryString ifTrue:[
|
|
177 |
"notify change of organization"
|
10
|
178 |
oldClass category:categoryString.
|
|
179 |
self addChangeRecordForClass:newClass.
|
2
|
180 |
Smalltalk changed
|
|
181 |
].
|
|
182 |
"notify change of class"
|
|
183 |
oldClass changed.
|
|
184 |
^ oldClass
|
33
|
185 |
].
|
|
186 |
|
|
187 |
"when we arrive here, class variables have changed"
|
|
188 |
|
|
189 |
(newComment ~= oldClass comment) ifTrue:[
|
|
190 |
oldClass comment:newComment. "already writes change-chunk"
|
|
191 |
].
|
|
192 |
oldClass category ~= categoryString ifTrue:[
|
|
193 |
"notify change of organization"
|
|
194 |
oldClass category:categoryString.
|
|
195 |
Smalltalk changed
|
|
196 |
].
|
|
197 |
|
|
198 |
oldClass classVariableString:stringOfClassVarNames.
|
|
199 |
|
|
200 |
changeSet1 := Set new.
|
|
201 |
oldClassVars do:[:nm |
|
|
202 |
(newClassVars includes:nm) ifFalse:[
|
|
203 |
changeSet1 add:nm
|
|
204 |
]
|
|
205 |
].
|
|
206 |
newClassVars do:[:nm |
|
|
207 |
(oldClassVars includes:nm) ifFalse:[
|
|
208 |
changeSet1 add:nm
|
|
209 |
]
|
|
210 |
].
|
|
211 |
|
|
212 |
"recompile all methods accessing set of changed classvars
|
|
213 |
here and also in all subclasses ..."
|
|
214 |
|
|
215 |
"dont update change file for the recompilation"
|
|
216 |
upd := Class updateChanges:false.
|
|
217 |
" "
|
|
218 |
Transcript showCr:'recompiling class&inst methods accessing ' , changeSet1 printString.
|
|
219 |
" "
|
|
220 |
oldClass withAllSubclasses do:[:aClass |
|
|
221 |
aClass class recompileMethodsAccessingAny:changeSet1.
|
|
222 |
aClass recompileMethodsAccessingAny:changeSet1.
|
|
223 |
].
|
|
224 |
Class updateChanges:upd.
|
|
225 |
|
|
226 |
"notify change of class"
|
|
227 |
self addChangeRecordForClass:oldClass.
|
|
228 |
oldClass changed.
|
|
229 |
|
|
230 |
^ oldClass
|
1
|
231 |
]
|
|
232 |
]
|
2
|
233 |
]
|
|
234 |
]
|
|
235 |
].
|
|
236 |
|
10
|
237 |
"tell dependents of class ..."
|
|
238 |
oldClass changed.
|
|
239 |
|
2
|
240 |
"catch special case, where superclass changed its layout and thus
|
|
241 |
forced redefinition of this class - this will not be logged here"
|
|
242 |
|
|
243 |
(newComment ~= oldClass comment) ifTrue:[
|
|
244 |
newClass comment:newComment
|
|
245 |
].
|
|
246 |
|
|
247 |
superClassChange := oldClass superclass ~~ newClass superclass.
|
1
|
248 |
|
3
|
249 |
"dont allow built-in classes to be modified"
|
|
250 |
(oldClass notNil and:[oldClass isBuiltInClass and:[superClassChange]]) ifTrue:[
|
|
251 |
self error:'the inheritance of this class is fixed - you cannot change it'.
|
|
252 |
^ oldClass
|
|
253 |
].
|
|
254 |
|
2
|
255 |
(superClassChange
|
33
|
256 |
and:[(oldClass superclass isNil or:[oldClass superclass name = newClass superclass name])
|
|
257 |
and:[(oldClassVars = newClassVars)
|
|
258 |
and:[(oldInstVars = newInstVars)
|
|
259 |
and:[newComment = oldClass comment]]]]) ifFalse:[
|
|
260 |
self addChangeRecordForClass:newClass.
|
2
|
261 |
].
|
|
262 |
|
|
263 |
changeSet1 := Set new.
|
|
264 |
changeSet2 := Set new.
|
1
|
265 |
|
2
|
266 |
classVarChange := false.
|
|
267 |
|
|
268 |
superClassChange ifTrue:[
|
|
269 |
"superclass changed,
|
|
270 |
must recompile all class methods accessing any classvar"
|
1
|
271 |
|
2
|
272 |
oldClass allClassVarNames do:[:nm | changeSet1 add:nm].
|
|
273 |
newClass allClassVarNames do:[:nm | changeSet1 add:nm].
|
|
274 |
|
|
275 |
" "
|
|
276 |
Transcript showCr:'recompiling class methods accessing any classvar'.
|
|
277 |
" "
|
|
278 |
self copyInvalidatedMethodsFrom:(oldClass class) for:newMetaclass accessingAny:changeSet1.
|
|
279 |
newMetaclass recompileInvalidatedMethods.
|
|
280 |
] ifFalse:[
|
|
281 |
"same superclass, find out which classvars have changed"
|
1
|
282 |
|
10
|
283 |
classVarChange := oldClassVars ~= newClassVars.
|
2
|
284 |
classVarChange ifTrue:[
|
10
|
285 |
oldClassVars do:[:nm |
|
|
286 |
(newClassVars includes:nm) ifFalse:[
|
|
287 |
changeSet1 add:nm
|
|
288 |
]
|
|
289 |
].
|
|
290 |
newClassVars do:[:nm |
|
|
291 |
(oldClassVars includes:nm) ifFalse:[
|
|
292 |
changeSet1 add:nm
|
|
293 |
]
|
2
|
294 |
].
|
1
|
295 |
].
|
|
296 |
|
2
|
297 |
" "
|
|
298 |
Transcript showCr:'recompiling class methods accessing ' , changeSet1 printString.
|
|
299 |
" "
|
1
|
300 |
classVarChange ifTrue:[
|
|
301 |
"must recompile class-methods"
|
2
|
302 |
self copyInvalidatedMethodsFrom:(oldClass class) for:newMetaclass accessingAny:changeSet1.
|
|
303 |
newMetaclass recompileInvalidatedMethods.
|
1
|
304 |
] ifFalse:[
|
|
305 |
"class methods still work"
|
|
306 |
self copyMethodsFrom:(oldClass class) for:newMetaclass
|
|
307 |
].
|
2
|
308 |
].
|
1
|
309 |
|
2
|
310 |
superClassChange ifTrue:[
|
|
311 |
"superclass changed,
|
13
|
312 |
must recompile all class methods accessing any class or instvar"
|
|
313 |
|
33
|
314 |
"no, if number of instvars is the same, only the changed ones ..."
|
13
|
315 |
|
|
316 |
"find set of changed instvars"
|
|
317 |
|
|
318 |
offset := 0. oldOffsets := Dictionary new.
|
33
|
319 |
oldClass allInstVarNames do:[:nm | offset := offset + 1. oldOffsets at:nm put:offset].
|
13
|
320 |
offset := 0. newOffsets := Dictionary new.
|
|
321 |
newClass allInstVarNames do:[:nm | offset := offset + 1. newOffsets at:nm put:offset].
|
|
322 |
|
|
323 |
oldOffsets associationsDo:[:a |
|
|
324 |
|k|
|
2
|
325 |
|
13
|
326 |
k := a key.
|
|
327 |
(newOffsets includesKey:k) ifFalse:[
|
|
328 |
changeSet2 add:k
|
|
329 |
] ifTrue:[
|
|
330 |
(a value ~~ (newOffsets at:k)) ifTrue:[
|
|
331 |
changeSet2 add:k
|
|
332 |
]
|
|
333 |
]
|
|
334 |
].
|
|
335 |
newOffsets associationsDo:[:a |
|
|
336 |
|k|
|
|
337 |
|
|
338 |
k := a key.
|
|
339 |
(oldOffsets includesKey:k) ifFalse:[
|
|
340 |
changeSet2 add:k
|
|
341 |
] ifTrue:[
|
|
342 |
(a value ~~ (oldOffsets at:k)) ifTrue:[
|
|
343 |
changeSet2 add:k
|
|
344 |
]
|
|
345 |
]
|
|
346 |
].
|
|
347 |
|
|
348 |
changeSet1 do:[:nm | changeSet2 add:nm].
|
|
349 |
" "
|
|
350 |
Transcript showCr:'recompiling instance methods accessing ' , changeSet2 printString , '
|
|
351 |
...'.
|
|
352 |
" "
|
|
353 |
self copyInvalidatedMethodsFrom:oldClass for:newClass accessingAny:changeSet2.
|
|
354 |
newClass recompileInvalidatedMethods.
|
|
355 |
|
|
356 |
false ifTrue:[
|
2
|
357 |
oldClass allClassVarNames do:[:nm | changeSet1 add:nm].
|
|
358 |
newClass allClassVarNames do:[:nm | changeSet1 add:nm].
|
|
359 |
oldClass allInstVarNames do:[:nm | changeSet1 add:nm].
|
|
360 |
newClass allInstVarNames do:[:nm | changeSet1 add:nm].
|
|
361 |
|
|
362 |
" "
|
|
363 |
Transcript showCr:'recompiling instance methods accessing any class or instvar' .
|
|
364 |
" "
|
|
365 |
self copyInvalidatedMethodsFrom:oldClass for:newClass accessingAny:changeSet1.
|
|
366 |
newClass recompileInvalidatedMethods.
|
13
|
367 |
]
|
2
|
368 |
] ifFalse:[
|
10
|
369 |
instVarChange := oldInstVars ~= newInstVars.
|
2
|
370 |
instVarChange ifFalse:[
|
|
371 |
classVarChange ifTrue:[
|
|
372 |
"recompile all inst methods accessing classvars"
|
1
|
373 |
|
2
|
374 |
" "
|
|
375 |
Transcript showCr:'recompiling instance methods accessing ' , changeSet1 printString , ' ...'.
|
|
376 |
" "
|
|
377 |
self copyInvalidatedMethodsFrom:oldClass for:newClass accessingAny:changeSet1.
|
|
378 |
newClass recompileInvalidatedMethods.
|
|
379 |
]
|
|
380 |
] ifTrue:[
|
10
|
381 |
instVarChange := (oldInstVars ~= newInstVars).
|
3
|
382 |
|
|
383 |
"dont allow built-in classes to be modified"
|
|
384 |
(oldClass notNil and:[oldClass isBuiltInClass and:[instVarChange]]) ifTrue:[
|
|
385 |
self error:'the layout of this class is fixed - you cannot change it'.
|
|
386 |
^ oldClass
|
|
387 |
].
|
|
388 |
|
2
|
389 |
instVarChange ifTrue:[
|
|
390 |
|
10
|
391 |
((oldInstVars size == 0)
|
|
392 |
or:[newInstVars startsWith:oldInstVars]) ifTrue:[
|
2
|
393 |
"new variable(s) has/have been added - old methods still work"
|
|
394 |
|
|
395 |
Transcript showCr:'copying methods ...'.
|
|
396 |
self copyMethodsFrom:oldClass for:newClass.
|
1
|
397 |
|
2
|
398 |
"but have to recompile methods accessing stuff now defined
|
|
399 |
(it might have been a global before ...)"
|
1
|
400 |
|
10
|
401 |
addedNames := newInstVars select:[:nm | (oldInstVars includes:nm) not].
|
2
|
402 |
changeSet1 do:[:nm | addedNames add:nm].
|
1
|
403 |
|
2
|
404 |
" "
|
|
405 |
Transcript showCr:'recompiling instance methods accessing ' , addedNames printString , '...'.
|
|
406 |
" "
|
|
407 |
newClass recompileMethodsAccessingAny:addedNames.
|
|
408 |
] ifFalse:[
|
|
409 |
"find set of changed instvars"
|
1
|
410 |
|
2
|
411 |
offset := 0. oldOffsets := Dictionary new.
|
10
|
412 |
oldInstVars do:[:nm | offset := offset + 1. oldOffsets at:nm put:offset].
|
2
|
413 |
offset := 0. newOffsets := Dictionary new.
|
10
|
414 |
newInstVars do:[:nm | offset := offset + 1. newOffsets at:nm put:offset].
|
2
|
415 |
|
|
416 |
oldOffsets associationsDo:[:a |
|
|
417 |
|k|
|
1
|
418 |
|
2
|
419 |
k := a key.
|
|
420 |
(newOffsets includesKey:k) ifFalse:[
|
|
421 |
changeSet2 add:k
|
|
422 |
] ifTrue:[
|
|
423 |
(a value ~~ (newOffsets at:k)) ifTrue:[
|
|
424 |
changeSet2 add:k
|
|
425 |
]
|
|
426 |
]
|
|
427 |
].
|
|
428 |
newOffsets associationsDo:[:a |
|
|
429 |
|k|
|
1
|
430 |
|
2
|
431 |
k := a key.
|
|
432 |
(oldOffsets includesKey:k) ifFalse:[
|
|
433 |
changeSet2 add:k
|
|
434 |
] ifTrue:[
|
|
435 |
(a value ~~ (oldOffsets at:k)) ifTrue:[
|
|
436 |
changeSet2 add:k
|
|
437 |
]
|
|
438 |
]
|
|
439 |
].
|
1
|
440 |
|
2
|
441 |
changeSet1 do:[:nm | changeSet2 add:nm].
|
|
442 |
" "
|
|
443 |
Transcript showCr:'recompiling instance methods accessing ' , changeSet2 printString , ' ...'.
|
|
444 |
" "
|
|
445 |
self copyInvalidatedMethodsFrom:oldClass for:newClass accessingAny:changeSet2.
|
|
446 |
newClass recompileInvalidatedMethods.
|
|
447 |
|
|
448 |
].
|
|
449 |
].
|
|
450 |
].
|
|
451 |
].
|
1
|
452 |
|
2
|
453 |
"get list of all subclasses - do before superclass is changed"
|
|
454 |
|
|
455 |
allSubclasses := oldClass allSubclasses.
|
|
456 |
|
|
457 |
"update superclass of immediate subclasses - this forces recompilation if needed"
|
|
458 |
|
13
|
459 |
"dont update change file for the subclass changes"
|
|
460 |
upd := Class updateChanges:false.
|
|
461 |
[
|
|
462 |
oldClass subclassesDo:[:aClass |
|
2
|
463 |
" "
|
13
|
464 |
Transcript showCr:'changing superclass of:' , aClass name.
|
2
|
465 |
" "
|
13
|
466 |
aClass superclass:newClass
|
|
467 |
]
|
|
468 |
] valueNowOrOnUnwindDo:[
|
|
469 |
Class updateChanges:upd.
|
2
|
470 |
].
|
|
471 |
|
|
472 |
aSystemDictionary at:classSymbol put:newClass.
|
|
473 |
ObjectMemory flushCaches.
|
1
|
474 |
^ newClass
|
|
475 |
!
|
|
476 |
|
|
477 |
new
|
2
|
478 |
"create & return a new metaclass (a classes class)"
|
|
479 |
|
1
|
480 |
|newClass|
|
|
481 |
|
|
482 |
newClass := self basicNew.
|
|
483 |
newClass setSuperclass:(Object class)
|
|
484 |
selectors:(Array new:0)
|
|
485 |
methods:(Array new:0)
|
|
486 |
instSize:0
|
|
487 |
flags:0.
|
|
488 |
newClass setComment:(self comment) category:(self category).
|
|
489 |
^ newClass
|
|
490 |
! !
|
|
491 |
|
|
492 |
!Metaclass methodsFor:'class instance variables'!
|
|
493 |
|
|
494 |
instanceVariableNames:aString
|
|
495 |
"changing / adding class-inst vars -
|
2
|
496 |
this actually creates a new metaclass and class."
|
1
|
497 |
|
|
498 |
|newClass newMetaclass nClassInstVars oldClass
|
2
|
499 |
allSubclasses t oldVars
|
|
500 |
oldNames newNames addedNames
|
|
501 |
oldOffsets newOffsets offset changeSet delta
|
|
502 |
oldToNew newSubMeta newSub oldSubMeta oldSuper|
|
1
|
503 |
|
|
504 |
oldVars := self instanceVariableString.
|
2
|
505 |
aString = oldVars ifTrue:[
|
|
506 |
"
|
|
507 |
Transcript showCr:'no change (', oldVars , ') -> (', aString , ')'.
|
|
508 |
"
|
|
509 |
^ self
|
|
510 |
].
|
|
511 |
|
|
512 |
oldNames := oldVars asCollectionOfWords.
|
|
513 |
newNames := aString asCollectionOfWords.
|
1
|
514 |
|
2
|
515 |
oldNames = newNames ifTrue:[
|
|
516 |
"
|
|
517 |
Transcript showCr:'no real change'.
|
|
518 |
"
|
|
519 |
"no real change (just formatting)"
|
|
520 |
self instanceVariableString:aString.
|
|
521 |
^ self
|
|
522 |
].
|
|
523 |
|
|
524 |
nClassInstVars := newNames size.
|
|
525 |
|
|
526 |
"
|
|
527 |
Transcript showCr:'create new class/metaclass'.
|
|
528 |
"
|
1
|
529 |
|
|
530 |
"create the new metaclass"
|
|
531 |
newMetaclass := Metaclass new.
|
|
532 |
newMetaclass setSuperclass:superclass.
|
|
533 |
newMetaclass instSize:(superclass instSize + nClassInstVars).
|
|
534 |
(nClassInstVars ~~ 0) ifTrue:[
|
|
535 |
newMetaclass instanceVariableString:aString
|
|
536 |
].
|
|
537 |
newMetaclass flags:0. "not indexed"
|
|
538 |
newMetaclass setName:name.
|
|
539 |
newMetaclass classVariableString:classvars.
|
|
540 |
newMetaclass category:category.
|
|
541 |
newMetaclass setComment:comment.
|
|
542 |
|
|
543 |
"find the class which is my sole instance"
|
|
544 |
|
|
545 |
t := Smalltalk allClasses select:[:element | element class == self].
|
|
546 |
(t size ~~ 1) ifTrue:[
|
|
547 |
self error:'oops - I should have exactly one instance'.
|
|
548 |
^ nil
|
|
549 |
].
|
|
550 |
oldClass := t anElement.
|
|
551 |
|
|
552 |
"create a new class"
|
|
553 |
newClass := newMetaclass new.
|
|
554 |
newClass setSuperclass:(oldClass superclass).
|
|
555 |
newClass instSize:(oldClass instSize).
|
|
556 |
newClass flags:(oldClass flags).
|
|
557 |
newClass setName:(oldClass name).
|
|
558 |
newClass instanceVariableString:(oldClass instanceVariableString).
|
|
559 |
newClass classVariableString:(oldClass classVariableString).
|
2
|
560 |
newClass setComment:(oldClass comment).
|
1
|
561 |
newClass category:(oldClass category).
|
|
562 |
|
2
|
563 |
offset := 0. oldOffsets := Dictionary new.
|
|
564 |
oldNames do:[:nm | offset := offset + 1. oldOffsets at:nm put:offset].
|
|
565 |
offset := 0. newOffsets := Dictionary new.
|
|
566 |
newNames do:[:nm | offset := offset + 1. newOffsets at:nm put:offset].
|
|
567 |
changeSet := Set new.
|
1
|
568 |
|
2
|
569 |
oldOffsets associationsDo:[:a |
|
10
|
570 |
|k|
|
|
571 |
|
|
572 |
k := a key.
|
|
573 |
(newOffsets includesKey:k) ifFalse:[
|
|
574 |
changeSet add:k
|
2
|
575 |
] ifTrue:[
|
10
|
576 |
(a value ~~ (newOffsets at:k)) ifTrue:[
|
|
577 |
changeSet add:k
|
2
|
578 |
]
|
|
579 |
]
|
|
580 |
].
|
|
581 |
newOffsets associationsDo:[:a |
|
10
|
582 |
|k|
|
|
583 |
|
|
584 |
k := a key.
|
|
585 |
(oldOffsets includesKey:k) ifFalse:[
|
|
586 |
changeSet add:k
|
2
|
587 |
] ifTrue:[
|
10
|
588 |
(a value ~~ (oldOffsets at:k)) ifTrue:[
|
|
589 |
changeSet add:k
|
2
|
590 |
]
|
|
591 |
]
|
|
592 |
].
|
1
|
593 |
|
2
|
594 |
((oldNames size == 0)
|
|
595 |
or:[newNames startsWith:oldNames]) ifTrue:[
|
|
596 |
"new variable(s) has/have been added - old methods still work"
|
|
597 |
|
|
598 |
Transcript showCr:'copying methods ...'.
|
1
|
599 |
self copyMethodsFrom:self for:newMetaclass.
|
2
|
600 |
self copyMethodsFrom:oldClass for:newClass.
|
|
601 |
|
|
602 |
"but have to recompile methods accessing stuff now defined
|
|
603 |
(it might have been a global before ...)"
|
|
604 |
|
|
605 |
addedNames := newNames select:[:nm | (oldNames includes:nm) not].
|
|
606 |
"
|
|
607 |
Transcript showCr:'recompiling methods accessing ' ,
|
|
608 |
addedNames printString , '...'.
|
|
609 |
"
|
1
|
610 |
"recompile class-methods"
|
2
|
611 |
newMetaclass recompileMethodsAccessingAny:addedNames.
|
|
612 |
] ifFalse:[
|
|
613 |
"
|
|
614 |
Transcript showCr:'recompiling methods accessing ' ,
|
|
615 |
changeSet printString , ' ...'.
|
|
616 |
"
|
|
617 |
"recompile class-methods"
|
|
618 |
self copyInvalidatedMethodsFrom:self for:newMetaclass accessingAny:changeSet.
|
|
619 |
newMetaclass recompileInvalidatedMethods.
|
1
|
620 |
|
2
|
621 |
self copyMethodsFrom:oldClass for:newClass.
|
1
|
622 |
].
|
|
623 |
|
2
|
624 |
delta := newNames size - oldNames size.
|
|
625 |
|
1
|
626 |
"get list of all subclasses - do before superclass is changed"
|
|
627 |
|
|
628 |
allSubclasses := oldClass allSubclasses.
|
2
|
629 |
allSubclasses := allSubclasses asSortedCollection:[:a :b |
|
|
630 |
b isSubclassOf:a
|
|
631 |
].
|
1
|
632 |
|
2
|
633 |
oldToNew := IdentityDictionary new.
|
|
634 |
|
|
635 |
"create a new class tree, based on new version"
|
|
636 |
|
|
637 |
allSubclasses do:[:aSubclass |
|
|
638 |
oldSuper := aSubclass superclass.
|
|
639 |
oldSubMeta := aSubclass class.
|
1
|
640 |
|
2
|
641 |
newSubMeta := Metaclass new.
|
|
642 |
oldSuper == oldClass ifTrue:[
|
|
643 |
newSubMeta setSuperclass:newMetaclass.
|
|
644 |
] ifFalse:[
|
|
645 |
newSubMeta setSuperclass:(oldToNew at:oldSuper) class.
|
|
646 |
].
|
|
647 |
newSubMeta instSize:(oldSubMeta instSize + delta).
|
|
648 |
newSubMeta flags:(oldSubMeta flags).
|
|
649 |
newSubMeta setName:(oldSubMeta name).
|
|
650 |
newSubMeta classVariableString:(oldSubMeta classVariableString).
|
|
651 |
newSubMeta setComment:(oldSubMeta comment).
|
|
652 |
newSubMeta category:(oldSubMeta category).
|
|
653 |
|
|
654 |
newSub := newSubMeta new.
|
|
655 |
oldSuper == oldClass ifTrue:[
|
|
656 |
newSub setSuperclass:newClass.
|
|
657 |
] ifFalse:[
|
|
658 |
newSub setSuperclass:(oldToNew at:oldSuper).
|
|
659 |
].
|
|
660 |
newSub setSelectors:(aSubclass selectors).
|
|
661 |
newSub setMethodDictionary:(aSubclass methodDictionary).
|
|
662 |
newSub setName:(aSubclass name).
|
|
663 |
newSub classVariableString:(aSubclass classVariableString).
|
|
664 |
newSub setComment:(aSubclass comment).
|
|
665 |
newSub category:(aSubclass category).
|
|
666 |
|
|
667 |
oldToNew at:aSubclass put:newSub.
|
|
668 |
|
|
669 |
aSubclass setName:(aSubclass name , '-old').
|
|
670 |
aSubclass category:'obsolete classes'
|
1
|
671 |
].
|
|
672 |
|
2
|
673 |
"recompile what needs to be"
|
|
674 |
|
|
675 |
delta == 0 ifTrue:[
|
|
676 |
"only have to recompile class methods accessing
|
|
677 |
class instvars from changeset"
|
1
|
678 |
|
2
|
679 |
allSubclasses do:[:aClass |
|
|
680 |
aClass class recompileMethodsAccessingAny:changeSet.
|
|
681 |
]
|
|
682 |
] ifFalse:[
|
|
683 |
"have to recompile all class methods accessing class instvars"
|
|
684 |
|
|
685 |
allSubclasses do:[:aClass |
|
|
686 |
|classInstVars|
|
|
687 |
|
|
688 |
classInstVars := aClass class allInstVarNames.
|
|
689 |
aClass class recompileMethodsAccessingAny:classInstVars.
|
|
690 |
]
|
1
|
691 |
].
|
|
692 |
|
2
|
693 |
self addChangeRecordForClassInstvars:newClass.
|
|
694 |
|
|
695 |
"install all new classes"
|
|
696 |
|
|
697 |
Smalltalk at:(oldClass name asSymbol) put:newClass.
|
|
698 |
ObjectMemory flushCachesFor:oldClass.
|
|
699 |
allSubclasses do:[:aClass |
|
|
700 |
Smalltalk at:(oldToNew at:aClass) name asSymbol put:(oldToNew at:aClass).
|
|
701 |
ObjectMemory flushCachesFor:aClass.
|
|
702 |
].
|
|
703 |
|
10
|
704 |
"tell dependents of class ..."
|
|
705 |
|
2
|
706 |
oldClass changed.
|
|
707 |
self changed.
|
1
|
708 |
^ newMetaclass
|
|
709 |
! !
|
|
710 |
|
|
711 |
!Metaclass methodsFor:'queries'!
|
|
712 |
|
|
713 |
isMeta
|
|
714 |
"return true, if the receiver is some kind of metaclass;
|
|
715 |
true is returned here. Redefines isMeta in Object"
|
|
716 |
|
|
717 |
^ true
|
|
718 |
! !
|
|
719 |
|
|
720 |
!Metaclass methodsFor:'private'!
|
|
721 |
|
|
722 |
copyMethodsFrom:oldClass for:newClass
|
|
723 |
"when a class has changed, but metaclass is unaffected (i.e. classVars
|
|
724 |
have not changed) there is no need to recompile them"
|
|
725 |
|
2
|
726 |
newClass selectors:(oldClass selectors copy) methods:(oldClass methodDictionary copy)
|
1
|
727 |
!
|
|
728 |
|
|
729 |
copyInvalidatedMethodsFrom:oldClass for:newClass
|
|
730 |
"when a class has been changed, copy all old methods into the new class
|
|
731 |
- changing code to a trap method giving an error message;
|
|
732 |
this allows us to keep the source while trapping uncompilable (due to
|
|
733 |
now undefined instvars) methods"
|
|
734 |
|
2
|
735 |
|trap trapCode trapByteCode newMethod oldMethodArray newMethodArray|
|
1
|
736 |
|
|
737 |
trap := Method compiledMethodAt:#invalidMethod.
|
|
738 |
trapCode := trap code.
|
|
739 |
trapByteCode := trap byteCode.
|
|
740 |
|
2
|
741 |
oldMethodArray := oldClass methodDictionary.
|
|
742 |
newMethodArray := Array new:(oldMethodArray size).
|
|
743 |
newClass selectors:(oldClass selectors copy) methods:newMethodArray.
|
|
744 |
1 to:oldMethodArray size do:[:i |
|
|
745 |
newMethod := (oldMethodArray at:i) copy.
|
|
746 |
newMethod code:trapCode.
|
|
747 |
newMethod literals:nil.
|
|
748 |
newMethod byteCode:trapByteCode.
|
|
749 |
newMethodArray at:i put:newMethod
|
|
750 |
]
|
|
751 |
!
|
|
752 |
|
|
753 |
copyInvalidatedMethodsFrom:oldClass for:newClass accessingAny:setOfNames
|
|
754 |
"copy all methods from oldClass to newClass. Those methods accessing
|
|
755 |
a variable in setOfNames will be copied as invalid method, leading to
|
|
756 |
a trap when its executed."
|
|
757 |
|
|
758 |
|trap trapCode trapByteCode p oldMethod newMethod oldMethodArray newMethodArray|
|
|
759 |
|
|
760 |
trap := Method compiledMethodAt:#invalidMethod.
|
|
761 |
trapCode := trap code.
|
|
762 |
trapByteCode := trap byteCode.
|
|
763 |
|
|
764 |
oldMethodArray := oldClass methodDictionary.
|
|
765 |
newMethodArray := Array new:(oldMethodArray size).
|
|
766 |
newClass selectors:(oldClass selectors copy) methods:newMethodArray.
|
|
767 |
1 to:oldMethodArray size do:[:i |
|
|
768 |
oldMethod := oldMethodArray at:i.
|
|
769 |
p := Parser parseMethod:(oldMethod source) in:newClass.
|
|
770 |
(p isNil or:[p usedVars notNil and:[p usedVars includesAny:setOfNames]]) ifTrue:[
|
|
771 |
newMethod := oldMethod copy.
|
|
772 |
newMethod code:trapCode.
|
|
773 |
newMethod literals:nil.
|
|
774 |
newMethod byteCode:trapByteCode
|
|
775 |
] ifFalse:[
|
|
776 |
newMethod := oldMethod.
|
|
777 |
].
|
|
778 |
newMethodArray at:i put:newMethod
|
1
|
779 |
]
|
|
780 |
!
|
|
781 |
|
|
782 |
anyInvalidatedMethodsIn:aClass
|
|
783 |
"return true, if aClass has any invalidated methods in it"
|
|
784 |
|
|
785 |
|trap trapCode trapByteCode|
|
|
786 |
|
|
787 |
trap := Method compiledMethodAt:#invalidMethod.
|
|
788 |
trapCode := trap code.
|
|
789 |
trapByteCode := trap byteCode.
|
|
790 |
|
|
791 |
aClass methodDictionary do:[:aMethod |
|
|
792 |
trapCode notNil ifTrue:[
|
|
793 |
(aMethod code == trapCode) ifTrue:[^ true]
|
|
794 |
].
|
|
795 |
trapByteCode notNil ifTrue:[
|
|
796 |
(aMethod byteCode == trapByteCode) ifTrue:[^ true]
|
|
797 |
]
|
|
798 |
].
|
|
799 |
^ false
|
|
800 |
! !
|