1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
159
|
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 |
Class subclass:#Metaclass
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Kernel-Classes'
|
|
18 |
!
|
|
19 |
|
|
20 |
Metaclass comment:'
|
88
|
21 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
159
|
22 |
All Rights Reserved
|
93
|
23 |
|
326
|
24 |
$Header: /cvs/stx/stx/libbasic/Metaclass.st,v 1.23 1995-04-11 14:49:45 claus Exp $
|
88
|
25 |
'!
|
1
|
26 |
|
88
|
27 |
!Metaclass class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
159
|
32 |
All Rights Reserved
|
1
|
33 |
|
88
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
1
|
42 |
|
88
|
43 |
version
|
|
44 |
"
|
326
|
45 |
$Header: /cvs/stx/stx/libbasic/Metaclass.st,v 1.23 1995-04-11 14:49:45 claus Exp $
|
88
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
|
51 |
every classes class is a subclass of Metaclass.
|
|
52 |
Metaclass provides support for creating new (sub)classes and/or
|
|
53 |
changing the definition of an already existing class.
|
|
54 |
"
|
|
55 |
! !
|
1
|
56 |
|
|
57 |
!Metaclass methodsFor:'creating classes'!
|
|
58 |
|
|
59 |
name:newName inEnvironment:aSystemDictionary
|
159
|
60 |
subclassOf:aClass
|
|
61 |
instanceVariableNames:stringOfInstVarNames
|
|
62 |
variable:variableBoolean
|
|
63 |
words:wordsBoolean
|
|
64 |
pointers:pointersBoolean
|
|
65 |
classVariableNames:stringOfClassVarNames
|
|
66 |
poolDictionaries:stringOfPoolNames
|
|
67 |
category:categoryString
|
|
68 |
comment:commentString
|
|
69 |
changed:changed
|
1
|
70 |
|
13
|
71 |
"this is the main workhorse for installing new classes - special care
|
|
72 |
has to be taken, when changing an existing classes definition. In this
|
|
73 |
case, some or all of the methods and subclasses methods have to be
|
|
74 |
recompiled.
|
|
75 |
Also, the old class(es) are still kept (but not accessable as a global),
|
|
76 |
to allow existing instances some life.
|
|
77 |
This might change in the future.
|
|
78 |
"
|
|
79 |
|
1
|
80 |
|newClass newMetaclass nInstVars nameString classSymbol oldClass
|
175
|
81 |
classVarChange instVarChange superClassChange newComment
|
308
|
82 |
changeSet1 changeSet2 addedNames
|
326
|
83 |
anyChange oldInstVars newInstVars oldClassVars newClassVars superFlags newFlags
|
|
84 |
currentProject|
|
13
|
85 |
|
68
|
86 |
"NOTICE:
|
|
87 |
this method is too complex and should be splitted into managable pieces ...
|
159
|
88 |
I dont like it anymore :-)
|
|
89 |
(However, its a good test for the compilers ability to handle big, complex methods ;-)
|
68
|
90 |
"
|
13
|
91 |
|
|
92 |
newName = aClass name ifTrue:[
|
159
|
93 |
self error:'trying to create circular class definition'.
|
|
94 |
^ nil
|
13
|
95 |
].
|
1
|
96 |
|
49
|
97 |
"check for invalid subclassing of UndefinedObject and SmallInteger"
|
|
98 |
aClass canBeSubclassed ifFalse:[
|
159
|
99 |
self error:('it is not possible to subclass ' , aClass name).
|
195
|
100 |
^ nil
|
49
|
101 |
].
|
|
102 |
|
1
|
103 |
nInstVars := stringOfInstVarNames countWords.
|
|
104 |
nameString := newName asString.
|
13
|
105 |
classSymbol := newName asSymbol.
|
1
|
106 |
newComment := commentString.
|
|
107 |
|
13
|
108 |
"look, if it already exists as a class"
|
68
|
109 |
oldClass := aSystemDictionary at:classSymbol ifAbsent:[nil].
|
|
110 |
oldClass isBehavior ifFalse:[
|
159
|
111 |
oldClass := nil.
|
68
|
112 |
] ifTrue:[
|
159
|
113 |
oldClass superclass notNil ifTrue:[
|
|
114 |
oldClass allSuperclasses do:[:cls |
|
|
115 |
cls name = nameString ifTrue:[
|
|
116 |
self error:'trying to create circular class definition'.
|
|
117 |
^ nil
|
|
118 |
]
|
|
119 |
]
|
|
120 |
].
|
68
|
121 |
|
159
|
122 |
aClass superclass notNil ifTrue:[
|
|
123 |
aClass allSuperclasses do:[:cls |
|
|
124 |
cls name = nameString ifTrue:[
|
|
125 |
self error:'trying to create circular class definition'.
|
|
126 |
^ nil
|
|
127 |
]
|
|
128 |
].
|
|
129 |
].
|
13
|
130 |
|
159
|
131 |
newComment isNil ifTrue:[
|
|
132 |
newComment := oldClass comment
|
|
133 |
].
|
68
|
134 |
|
159
|
135 |
"
|
|
136 |
warn, if it exists with different category and different instvars,
|
|
137 |
and the existing is not an autoload class.
|
|
138 |
Usually, this indicates that someone wants to create a new class with
|
|
139 |
a name, which already exists (it happened a few times to myself, while
|
|
140 |
I wanted to create a new class called ReturnNode ...).
|
|
141 |
This will be much less of a problem, once multiple name spaces are
|
|
142 |
implemented and classes can be put into separate packages.
|
|
143 |
"
|
|
144 |
oldClass isLoaded ifTrue:[
|
|
145 |
oldClass category ~= categoryString ifTrue:[
|
|
146 |
oldClass instanceVariableString asCollectionOfWords
|
|
147 |
~= stringOfInstVarNames asCollectionOfWords ifTrue:[
|
|
148 |
(self confirm:'a class named ' , oldClass name , ' already exists -
|
68
|
149 |
|
|
150 |
create (i.e. change) anyway ?' withCRs)
|
159
|
151 |
ifFalse:[
|
|
152 |
^ nil
|
|
153 |
]
|
|
154 |
]
|
|
155 |
]
|
|
156 |
]
|
1
|
157 |
].
|
|
158 |
|
68
|
159 |
"
|
|
160 |
Check for some 'considered bad-style' things, like lower case names.
|
|
161 |
But only do these checks for new classes -
|
|
162 |
- thus, once confirmed, the warnings will not come again and again.
|
|
163 |
|
|
164 |
NOTICE:
|
195
|
165 |
I dont like the confirmers there - we need a notifying: argument, to give
|
44
|
166 |
the outer codeview a chance to highlight the error.
|
68
|
167 |
(but thats how its defined in the book - maybe I will change anyway).
|
|
168 |
"
|
44
|
169 |
oldClass isNil ifTrue:[
|
195
|
170 |
(self checkConventionsFor:newName
|
|
171 |
instVarNames:stringOfInstVarNames
|
|
172 |
classVarNames:stringOfClassVarNames) ifFalse:[
|
|
173 |
^ nil
|
159
|
174 |
]
|
44
|
175 |
].
|
|
176 |
|
1
|
177 |
"create the metaclass first"
|
|
178 |
newMetaclass := Metaclass new.
|
|
179 |
newMetaclass setSuperclass:(aClass class).
|
|
180 |
newMetaclass instSize:(aClass class instSize).
|
175
|
181 |
newMetaclass setName:(nameString , 'class') asSymbol.
|
1
|
182 |
newMetaclass classVariableString:'' "stringOfClassVarNames".
|
226
|
183 |
"/ newMetaclass setComment:newComment category:categoryString.
|
326
|
184 |
|
226
|
185 |
Project notNil ifTrue:[
|
326
|
186 |
currentProject := Project current.
|
|
187 |
currentProject notNil ifTrue:[
|
|
188 |
newMetaclass package:(currentProject packageName)
|
|
189 |
]
|
226
|
190 |
].
|
1
|
191 |
|
195
|
192 |
"then let the new meta create the class"
|
1
|
193 |
newClass := newMetaclass new.
|
|
194 |
newClass setSuperclass:aClass.
|
|
195 |
newClass instSize:(aClass instSize + nInstVars).
|
175
|
196 |
newClass setName:classSymbol.
|
|
197 |
newClass setComment:newComment category:categoryString.
|
1
|
198 |
|
68
|
199 |
"
|
|
200 |
Allowing non-booleans as variableBoolean
|
|
201 |
is a hack for backward (ST-80) compatibility:
|
|
202 |
|
|
203 |
ST-80 code will pass true or false as variableBoolean,
|
|
204 |
while ST/X also calls it with symbols such as #float, #double etc.
|
|
205 |
"
|
1
|
206 |
(variableBoolean == true) ifTrue:[
|
159
|
207 |
pointersBoolean ifTrue:[
|
|
208 |
newFlags := Behavior flagPointers
|
|
209 |
] ifFalse:[
|
|
210 |
wordsBoolean ifTrue:[
|
|
211 |
newFlags := Behavior flagWords
|
|
212 |
] ifFalse:[
|
|
213 |
newFlags := Behavior flagBytes
|
|
214 |
]
|
|
215 |
]
|
1
|
216 |
] ifFalse:[
|
159
|
217 |
(variableBoolean == #float) ifTrue:[
|
|
218 |
newFlags := Behavior flagFloats
|
|
219 |
] ifFalse:[
|
|
220 |
(variableBoolean == #double) ifTrue:[
|
|
221 |
newFlags := Behavior flagDoubles
|
|
222 |
] ifFalse:[
|
|
223 |
(variableBoolean == #long) ifTrue:[
|
|
224 |
newFlags := Behavior flagLongs
|
|
225 |
] ifFalse:[
|
|
226 |
newFlags := Behavior flagNotIndexed
|
|
227 |
]
|
|
228 |
]
|
|
229 |
].
|
1
|
230 |
].
|
68
|
231 |
superFlags := aClass flags bitAnd:(Behavior maskIndexType bitInvert). "preserve other bits"
|
|
232 |
oldClass notNil ifTrue:[
|
159
|
233 |
oldClass isBuiltInClass ifTrue:[
|
|
234 |
"
|
|
235 |
special care when redefining Method, Block and other built-in classes,
|
|
236 |
which might have other flag bits ...
|
|
237 |
"
|
49
|
238 |
|
159
|
239 |
newFlags := newFlags bitOr:(oldClass flags bitAnd:(Behavior maskIndexType bitInvert))
|
|
240 |
]
|
68
|
241 |
].
|
|
242 |
newClass flags:(newFlags bitOr:superFlags). "preserve inherited special bits"
|
1
|
243 |
|
|
244 |
(nInstVars ~~ 0) ifTrue:[
|
202
|
245 |
newClass setInstanceVariableString:stringOfInstVarNames
|
1
|
246 |
].
|
|
247 |
oldClass notNil ifTrue:[
|
159
|
248 |
"setting first will make new class clear obsolete classvars"
|
|
249 |
newClass setClassVariableString:(oldClass classVariableString)
|
1
|
250 |
].
|
|
251 |
newClass classVariableString:stringOfClassVarNames.
|
|
252 |
|
68
|
253 |
"
|
|
254 |
for new classes, we are almost done here
|
77
|
255 |
(also for autoloaded classes)
|
68
|
256 |
"
|
77
|
257 |
(oldClass isNil or:[oldClass isLoaded not]) ifTrue:[
|
159
|
258 |
oldClass isNil ifTrue:[
|
|
259 |
self addChangeRecordForClass:newClass.
|
|
260 |
].
|
1
|
261 |
|
159
|
262 |
commentString notNil ifTrue:[
|
|
263 |
newClass comment:commentString
|
|
264 |
].
|
68
|
265 |
|
159
|
266 |
aSystemDictionary at:classSymbol put:newClass.
|
314
|
267 |
|
|
268 |
oldClass isNil ifTrue:[
|
326
|
269 |
Project notNil ifTrue:[
|
|
270 |
currentProject := Project current.
|
|
271 |
currentProject notNil ifTrue:[
|
|
272 |
"
|
|
273 |
new classes get the package assigned
|
|
274 |
"
|
|
275 |
newClass package:(currentProject packageName asSymbol)
|
|
276 |
]
|
314
|
277 |
].
|
|
278 |
].
|
|
279 |
|
159
|
280 |
aSystemDictionary changed:#newClass with:newClass.
|
|
281 |
^ newClass
|
2
|
282 |
].
|
|
283 |
|
10
|
284 |
|
68
|
285 |
"
|
|
286 |
here comes the hard part - we are actually changing the
|
|
287 |
definition of an existing class ....
|
|
288 |
Try hard to get away WITHOUT recompiling, since it makes all
|
|
289 |
compiled code into interpreted ...
|
|
290 |
"
|
|
291 |
|
10
|
292 |
oldInstVars := oldClass instanceVariableString asCollectionOfWords.
|
|
293 |
newInstVars := newClass instanceVariableString asCollectionOfWords.
|
|
294 |
oldClassVars := oldClass classVariableString asCollectionOfWords.
|
|
295 |
newClassVars := newClass classVariableString asCollectionOfWords.
|
|
296 |
|
68
|
297 |
"
|
|
298 |
we are on the bright side of life, if the instance layout and
|
|
299 |
inheritance do not change.
|
|
300 |
In this case, we can go ahead and patch the class object.
|
|
301 |
"
|
2
|
302 |
(oldClass superclass == newClass superclass) ifTrue:[
|
|
303 |
(oldClass instSize == newClass instSize) ifTrue:[
|
159
|
304 |
(oldClass flags == newClass flags) ifTrue:[
|
|
305 |
(oldClass name = newClass name) ifTrue:[
|
|
306 |
(oldInstVars = newInstVars) ifTrue:[
|
68
|
307 |
|
159
|
308 |
(newComment ~= oldClass comment) ifTrue:[
|
|
309 |
oldClass setComment:newComment. "writes a change-chunk"
|
|
310 |
oldClass changed:#comment with:oldClass comment.
|
|
311 |
self addChangeRecordForClassComment:oldClass.
|
|
312 |
].
|
10
|
313 |
|
159
|
314 |
(oldClassVars = newClassVars) ifTrue:[
|
|
315 |
"
|
|
316 |
really no change (just comment and/or category)
|
|
317 |
"
|
|
318 |
anyChange := false.
|
10
|
319 |
|
202
|
320 |
oldClass setInstanceVariableString:(newClass instanceVariableString).
|
159
|
321 |
oldClass setClassVariableString:(newClass classVariableString).
|
68
|
322 |
|
159
|
323 |
oldClass category ~= categoryString ifTrue:[
|
|
324 |
oldClass category:categoryString.
|
|
325 |
self addChangeRecordForClass:newClass.
|
|
326 |
"notify change of organization"
|
202
|
327 |
aSystemDictionary changed:#organization
|
159
|
328 |
].
|
|
329 |
"notify change of class"
|
|
330 |
"/ oldClass changed.
|
|
331 |
^ oldClass
|
|
332 |
].
|
33
|
333 |
|
159
|
334 |
"
|
|
335 |
when we arrive here, class variables have changed
|
|
336 |
"
|
|
337 |
oldClass category ~= categoryString ifTrue:[
|
|
338 |
"notify change of organization"
|
|
339 |
oldClass category:categoryString.
|
|
340 |
"notify change of organization"
|
202
|
341 |
aSystemDictionary changed:#organization
|
159
|
342 |
].
|
33
|
343 |
|
159
|
344 |
"
|
|
345 |
set class variable string;
|
|
346 |
this also updates the set of class variables
|
|
347 |
by creating new / deleting obsolete ones.
|
|
348 |
"
|
|
349 |
oldClass classVariableString:stringOfClassVarNames.
|
33
|
350 |
|
159
|
351 |
"
|
|
352 |
get the set of changed class variables
|
|
353 |
"
|
|
354 |
changeSet1 := Set new.
|
|
355 |
oldClassVars do:[:nm |
|
|
356 |
(newClassVars includes:nm) ifFalse:[
|
|
357 |
changeSet1 add:nm
|
|
358 |
]
|
|
359 |
].
|
|
360 |
newClassVars do:[:nm |
|
|
361 |
(oldClassVars includes:nm) ifFalse:[
|
|
362 |
changeSet1 add:nm
|
|
363 |
]
|
|
364 |
].
|
33
|
365 |
|
159
|
366 |
"
|
|
367 |
recompile all methods accessing set of changed classvars
|
|
368 |
here and also in all subclasses ...
|
|
369 |
"
|
|
370 |
|
|
371 |
"
|
|
372 |
dont update change file for the recompilation
|
|
373 |
"
|
202
|
374 |
Class withoutUpdatingChangesDo:[
|
33
|
375 |
" "
|
159
|
376 |
Transcript showCr:'recompiling class & inst methods accessing ' , changeSet1 printString.
|
|
377 |
Transcript endEntry.
|
33
|
378 |
" "
|
159
|
379 |
oldClass withAllSubclasses do:[:aClass |
|
|
380 |
aClass class recompileMethodsAccessingAny:changeSet1.
|
|
381 |
aClass recompileMethodsAccessingAny:changeSet1.
|
|
382 |
].
|
|
383 |
].
|
33
|
384 |
|
159
|
385 |
"notify change of class"
|
|
386 |
self addChangeRecordForClass:oldClass.
|
|
387 |
oldClass changed:#definition.
|
33
|
388 |
|
159
|
389 |
^ oldClass
|
|
390 |
]
|
|
391 |
]
|
|
392 |
]
|
2
|
393 |
]
|
|
394 |
].
|
|
395 |
|
68
|
396 |
"
|
|
397 |
here we enter the darkness of mordor ...
|
159
|
398 |
since instance variable layout and/or inheritance has changed.
|
68
|
399 |
"
|
2
|
400 |
(newComment ~= oldClass comment) ifTrue:[
|
159
|
401 |
newClass comment:newComment
|
2
|
402 |
].
|
|
403 |
|
|
404 |
superClassChange := oldClass superclass ~~ newClass superclass.
|
1
|
405 |
|
68
|
406 |
"
|
|
407 |
dont allow built-in classes to be modified this way
|
|
408 |
"
|
3
|
409 |
(oldClass notNil and:[oldClass isBuiltInClass and:[superClassChange]]) ifTrue:[
|
159
|
410 |
self error:'the inheritance of this class is fixed - you cannot change it'.
|
|
411 |
^ oldClass
|
3
|
412 |
].
|
|
413 |
|
68
|
414 |
"
|
|
415 |
catch special case, where superclass changed its layout and thus
|
|
416 |
forced redefinition of this class;
|
|
417 |
only log if this is not the case.
|
|
418 |
"
|
2
|
419 |
(superClassChange
|
33
|
420 |
and:[(oldClass superclass isNil or:[oldClass superclass name = newClass superclass name])
|
|
421 |
and:[(oldClassVars = newClassVars)
|
|
422 |
and:[(oldInstVars = newInstVars)
|
|
423 |
and:[newComment = oldClass comment]]]]) ifFalse:[
|
159
|
424 |
self addChangeRecordForClass:newClass.
|
2
|
425 |
].
|
|
426 |
|
68
|
427 |
"
|
|
428 |
care for class methods ...
|
|
429 |
"
|
2
|
430 |
changeSet1 := Set new.
|
1
|
431 |
|
2
|
432 |
classVarChange := false.
|
|
433 |
|
|
434 |
superClassChange ifTrue:[
|
159
|
435 |
"
|
|
436 |
superclass changed:
|
|
437 |
must recompile all class methods accessing ANY classvar
|
|
438 |
(
|
|
439 |
actually, we could be less strict and handle the case where
|
|
440 |
both the old and the new superclass have a common ancestor,
|
|
441 |
and both have no new classvariables in between.
|
|
442 |
This would speedup the case when a class is inserted into
|
|
443 |
the inheritance chain.
|
|
444 |
)
|
|
445 |
"
|
1
|
446 |
|
159
|
447 |
oldClass allClassVarNames do:[:nm | changeSet1 add:nm].
|
|
448 |
newClass allClassVarNames do:[:nm | changeSet1 add:nm].
|
2
|
449 |
|
|
450 |
" "
|
159
|
451 |
Transcript showCr:'recompiling class methods accessing any classvar'.
|
|
452 |
Transcript endEntry.
|
2
|
453 |
" "
|
159
|
454 |
self copyInvalidatedMethodsFrom:(oldClass class)
|
|
455 |
for:newMetaclass
|
|
456 |
accessingAny:changeSet1
|
|
457 |
orSuper:true.
|
|
458 |
newMetaclass recompileInvalidatedMethods:(Metaclass compiledMethodAt:#invalidMethod).
|
2
|
459 |
] ifFalse:[
|
159
|
460 |
"
|
|
461 |
same superclass, find out which classvars have changed
|
|
462 |
"
|
|
463 |
classVarChange := oldClassVars ~= newClassVars.
|
|
464 |
classVarChange ifTrue:[
|
|
465 |
oldClassVars do:[:nm |
|
|
466 |
(newClassVars includes:nm) ifFalse:[
|
|
467 |
changeSet1 add:nm
|
|
468 |
]
|
|
469 |
].
|
|
470 |
newClassVars do:[:nm |
|
|
471 |
(oldClassVars includes:nm) ifFalse:[
|
|
472 |
changeSet1 add:nm
|
|
473 |
]
|
|
474 |
].
|
|
475 |
].
|
1
|
476 |
|
159
|
477 |
classVarChange ifTrue:[
|
|
478 |
"
|
|
479 |
must recompile some class-methods
|
|
480 |
"
|
2
|
481 |
" "
|
159
|
482 |
Transcript showCr:'recompiling class methods accessing ' , changeSet1 printString.
|
|
483 |
Transcript endEntry.
|
68
|
484 |
" "
|
159
|
485 |
self copyInvalidatedMethodsFrom:(oldClass class) for:newMetaclass accessingAny:changeSet1.
|
|
486 |
newMetaclass recompileInvalidatedMethods:(Metaclass compiledMethodAt:#invalidMethod).
|
|
487 |
] ifFalse:[
|
|
488 |
"
|
|
489 |
class methods still work
|
|
490 |
"
|
|
491 |
self copyMethodsFrom:(oldClass class) for:newMetaclass
|
|
492 |
].
|
2
|
493 |
].
|
1
|
494 |
|
68
|
495 |
"
|
|
496 |
care for instance methods ...
|
|
497 |
"
|
|
498 |
|
2
|
499 |
superClassChange ifTrue:[
|
159
|
500 |
"superclass changed,
|
|
501 |
must recompile all methods accessing any class or instvar.
|
|
502 |
If number of instvars (i.e. the instances instSize) is the same,
|
|
503 |
we can limit the set of recompiled instance methods to those methods,
|
|
504 |
which refer to an instvar with a different inst-index
|
|
505 |
"
|
13
|
506 |
|
159
|
507 |
"
|
|
508 |
the changeset consists of instance variables,
|
308
|
509 |
with a different position
|
159
|
510 |
"
|
308
|
511 |
changeSet2 := self differentInstanceVariableOffsetsIn:oldClass and:newClass.
|
13
|
512 |
|
159
|
513 |
"
|
|
514 |
merge in the changed class variables
|
|
515 |
"
|
|
516 |
changeSet1 do:[:nm | changeSet2 add:nm].
|
68
|
517 |
|
13
|
518 |
" "
|
159
|
519 |
Transcript showCr:'recompiling instance methods accessing ' , changeSet2 printString , ' ...'.
|
|
520 |
Transcript endEntry.
|
13
|
521 |
" "
|
159
|
522 |
self copyInvalidatedMethodsFrom:oldClass
|
|
523 |
for:newClass
|
|
524 |
accessingAny:changeSet2
|
|
525 |
orSuper:true.
|
|
526 |
newClass recompileInvalidatedMethods:(Metaclass compiledMethodAt:#invalidMethod).
|
13
|
527 |
|
2
|
528 |
] ifFalse:[
|
159
|
529 |
"
|
|
530 |
same inheritance ...
|
|
531 |
"
|
|
532 |
instVarChange := oldInstVars ~= newInstVars.
|
|
533 |
instVarChange ifFalse:[
|
|
534 |
"
|
|
535 |
same instance variables ...
|
|
536 |
"
|
|
537 |
classVarChange ifTrue:[
|
|
538 |
"recompile all inst methods accessing changed classvars"
|
1
|
539 |
|
2
|
540 |
" "
|
159
|
541 |
Transcript showCr:'recompiling instance methods accessing ' , changeSet1 printString , ' ...'.
|
|
542 |
Transcript endEntry.
|
2
|
543 |
" "
|
159
|
544 |
self copyInvalidatedMethodsFrom:oldClass for:newClass accessingAny:changeSet1.
|
|
545 |
newClass recompileInvalidatedMethods:(Metaclass compiledMethodAt:#invalidMethod).
|
|
546 |
]
|
|
547 |
] ifTrue:[
|
|
548 |
"
|
|
549 |
dont allow built-in classes to be modified
|
|
550 |
"
|
|
551 |
(oldClass notNil and:[oldClass isBuiltInClass and:[instVarChange]]) ifTrue:[
|
|
552 |
self error:'the layout of this class is fixed - you cannot change it'.
|
|
553 |
^ oldClass
|
|
554 |
].
|
3
|
555 |
|
159
|
556 |
((oldInstVars size == 0)
|
|
557 |
or:[newInstVars startsWith:oldInstVars]) ifTrue:[
|
|
558 |
"
|
|
559 |
only new inst variable(s) has/have been added -
|
|
560 |
old methods still work (the existing inst-indices are still valid)
|
|
561 |
"
|
68
|
562 |
" "
|
159
|
563 |
Transcript showCr:'copying methods ...'.
|
|
564 |
Transcript endEntry.
|
68
|
565 |
" "
|
159
|
566 |
self copyMethodsFrom:oldClass for:newClass.
|
2
|
567 |
|
159
|
568 |
"
|
|
569 |
but: we have to recompile all methods accessing new instars
|
|
570 |
(it might have been a classVar/global before ...)
|
|
571 |
"
|
|
572 |
addedNames := newInstVars select:[:nm | (oldInstVars includes:nm) not].
|
|
573 |
"merge in class variables"
|
|
574 |
changeSet1 do:[:nm | addedNames add:nm].
|
1
|
575 |
|
2
|
576 |
" "
|
159
|
577 |
Transcript showCr:'recompiling instance methods accessing ' , addedNames printString , '...'.
|
|
578 |
Transcript endEntry.
|
2
|
579 |
" "
|
159
|
580 |
newClass recompileMethodsAccessingAny:addedNames.
|
|
581 |
] ifFalse:[
|
2
|
582 |
|
159
|
583 |
"
|
|
584 |
the changeset consists of instance variables,
|
308
|
585 |
with a different position
|
159
|
586 |
"
|
308
|
587 |
changeSet2 := self differentInstanceVariableOffsetsIn:oldClass and:newClass.
|
1
|
588 |
|
308
|
589 |
"merge in the class variables"
|
159
|
590 |
changeSet1 do:[:nm | changeSet2 add:nm].
|
2
|
591 |
" "
|
159
|
592 |
Transcript showCr:'recompiling instance methods accessing ' , changeSet2 printString , ' ...'.
|
|
593 |
Transcript endEntry.
|
2
|
594 |
" "
|
159
|
595 |
self copyInvalidatedMethodsFrom:oldClass for:newClass accessingAny:changeSet2.
|
|
596 |
newClass recompileInvalidatedMethods:(Metaclass compiledMethodAt:#invalidMethod).
|
|
597 |
].
|
|
598 |
].
|
2
|
599 |
].
|
1
|
600 |
|
68
|
601 |
"
|
|
602 |
WOW, everything done for this class
|
|
603 |
what about subclasses ?
|
|
604 |
"
|
2
|
605 |
|
68
|
606 |
"
|
|
607 |
update superclass of immediate subclasses -
|
|
608 |
this forces recompilation (recursively) if needed
|
|
609 |
(dont update change file for the subclass changes)
|
|
610 |
"
|
202
|
611 |
Class withoutUpdatingChangesDo:[
|
159
|
612 |
oldClass subclassesDo:[:aClass |
|
2
|
613 |
" "
|
159
|
614 |
Transcript showCr:'changing superclass of:' , aClass name.
|
|
615 |
Transcript endEntry.
|
2
|
616 |
" "
|
159
|
617 |
aClass superclass:newClass
|
|
618 |
]
|
2
|
619 |
].
|
|
620 |
|
68
|
621 |
"
|
|
622 |
change category in oldClass - so we see immediately what it is ...
|
|
623 |
"
|
|
624 |
oldClass category:'obsolete'.
|
|
625 |
oldClass class category:'obsolete'.
|
|
626 |
|
|
627 |
"
|
|
628 |
and make the new class globally known
|
|
629 |
"
|
2
|
630 |
aSystemDictionary at:classSymbol put:newClass.
|
68
|
631 |
|
|
632 |
oldClass category ~= categoryString ifTrue:[
|
159
|
633 |
"notify change of organization"
|
202
|
634 |
aSystemDictionary changed:#organization
|
68
|
635 |
].
|
|
636 |
|
|
637 |
"
|
|
638 |
Not becoming the old class creates some update problems;
|
|
639 |
the browsers must check carefully - a simple identity compare is
|
159
|
640 |
not enough ...
|
68
|
641 |
QUESTION: is this a good idea ?
|
|
642 |
"
|
|
643 |
|
|
644 |
newClass dependents:(oldClass dependents).
|
202
|
645 |
newClass changed:#definition.
|
68
|
646 |
|
159
|
647 |
"just to make certain ... - tell dependents of oldClass, that something changed
|
85
|
648 |
(systemBrowsers will react on this, and update their views)"
|
159
|
649 |
oldClass changed:#definition with:newClass.
|
68
|
650 |
|
2
|
651 |
ObjectMemory flushCaches.
|
68
|
652 |
|
1
|
653 |
^ newClass
|
|
654 |
!
|
|
655 |
|
|
656 |
new
|
2
|
657 |
"create & return a new metaclass (a classes class)"
|
|
658 |
|
1
|
659 |
|newClass|
|
|
660 |
|
|
661 |
newClass := self basicNew.
|
|
662 |
newClass setSuperclass:(Object class)
|
159
|
663 |
selectors:(Array new:0)
|
|
664 |
methods:(Array new:0)
|
|
665 |
instSize:0
|
|
666 |
flags:(Behavior flagNotIndexed).
|
175
|
667 |
"/ newClass setComment:(self comment) category:(self category).
|
1
|
668 |
^ newClass
|
|
669 |
! !
|
|
670 |
|
|
671 |
!Metaclass methodsFor:'class instance variables'!
|
|
672 |
|
|
673 |
instanceVariableNames:aString
|
|
674 |
"changing / adding class-inst vars -
|
251
|
675 |
this actually creates a new metaclass and class, leaving the original
|
|
676 |
classes around as obsolete classes. This may also be true for all subclasses,
|
|
677 |
if class instance variables are added/removed.
|
|
678 |
Existing instances continue to be defined by their original classes.
|
|
679 |
|
|
680 |
Time will show, if this is an acceptable behavior or if we should migrate
|
|
681 |
instances to become insts. of the new classes."
|
1
|
682 |
|
|
683 |
|newClass newMetaclass nClassInstVars oldClass
|
2
|
684 |
allSubclasses t oldVars
|
|
685 |
oldNames newNames addedNames
|
|
686 |
oldOffsets newOffsets offset changeSet delta
|
251
|
687 |
oldToNew newSubMeta newSub oldSubMeta oldSuper
|
|
688 |
commonClassInstVars|
|
1
|
689 |
|
68
|
690 |
"
|
|
691 |
cleanup needed here: extract common things with name:inEnvironment:...
|
|
692 |
and restructure things ... currently way too complex.
|
|
693 |
"
|
44
|
694 |
|
1
|
695 |
oldVars := self instanceVariableString.
|
2
|
696 |
aString = oldVars ifTrue:[
|
|
697 |
"
|
159
|
698 |
Transcript showCr:'no change (', oldVars , ') -> (', aString , ')'.
|
2
|
699 |
"
|
159
|
700 |
^ self
|
2
|
701 |
].
|
|
702 |
|
|
703 |
oldNames := oldVars asCollectionOfWords.
|
|
704 |
newNames := aString asCollectionOfWords.
|
1
|
705 |
|
2
|
706 |
oldNames = newNames ifTrue:[
|
|
707 |
"
|
159
|
708 |
Transcript showCr:'no real change'.
|
2
|
709 |
"
|
159
|
710 |
"no real change (just formatting)"
|
202
|
711 |
self setInstanceVariableString:aString.
|
159
|
712 |
^ self
|
2
|
713 |
].
|
|
714 |
|
251
|
715 |
"/ "
|
|
716 |
"/ let user confirm, if any name is no good (and was good before)
|
|
717 |
"/ "
|
|
718 |
"/ (oldNames inject:true
|
|
719 |
"/ into:[:okSoFar :word |
|
|
720 |
"/ okSoFar and:[word first isUppercase]
|
|
721 |
"/ ])
|
|
722 |
"/ ifTrue:[
|
|
723 |
"/ "was ok before"
|
|
724 |
"/ (newNames inject:true
|
|
725 |
"/ into:[:okSoFar :word |
|
|
726 |
"/ okSoFar and:[word first isUppercase]
|
|
727 |
"/ ])
|
|
728 |
"/ ifFalse:[
|
|
729 |
"/ (self confirm:'class instance variable names should start with an uppercase letter
|
|
730 |
"/(by convention only)
|
|
731 |
"/
|
|
732 |
"/install anyway ?' withCRs)
|
|
733 |
"/ ifFalse:[
|
|
734 |
"/ ^ nil
|
|
735 |
"/ ]
|
|
736 |
"/ ]
|
|
737 |
"/ ].
|
44
|
738 |
|
2
|
739 |
nClassInstVars := newNames size.
|
|
740 |
|
|
741 |
"
|
|
742 |
Transcript showCr:'create new class/metaclass'.
|
|
743 |
"
|
1
|
744 |
|
251
|
745 |
"
|
|
746 |
create the new metaclass
|
|
747 |
"
|
1
|
748 |
newMetaclass := Metaclass new.
|
|
749 |
newMetaclass setSuperclass:superclass.
|
|
750 |
newMetaclass instSize:(superclass instSize + nClassInstVars).
|
|
751 |
(nClassInstVars ~~ 0) ifTrue:[
|
202
|
752 |
newMetaclass setInstanceVariableString:aString
|
1
|
753 |
].
|
93
|
754 |
newMetaclass flags:(Behavior flagNotIndexed).
|
1
|
755 |
newMetaclass setName:name.
|
|
756 |
newMetaclass classVariableString:classvars.
|
|
757 |
newMetaclass category:category.
|
195
|
758 |
newMetaclass setComment:(self comment).
|
1
|
759 |
|
|
760 |
"find the class which is my sole instance"
|
|
761 |
|
|
762 |
t := Smalltalk allClasses select:[:element | element class == self].
|
|
763 |
(t size ~~ 1) ifTrue:[
|
159
|
764 |
self error:'oops - I should have exactly one instance'.
|
|
765 |
^ nil
|
1
|
766 |
].
|
|
767 |
oldClass := t anElement.
|
|
768 |
|
251
|
769 |
"
|
|
770 |
create the new class
|
|
771 |
"
|
1
|
772 |
newClass := newMetaclass new.
|
|
773 |
newClass setSuperclass:(oldClass superclass).
|
|
774 |
newClass instSize:(oldClass instSize).
|
|
775 |
newClass flags:(oldClass flags).
|
|
776 |
newClass setName:(oldClass name).
|
202
|
777 |
newClass setInstanceVariableString:(oldClass instanceVariableString).
|
1
|
778 |
newClass classVariableString:(oldClass classVariableString).
|
2
|
779 |
newClass setComment:(oldClass comment).
|
1
|
780 |
newClass category:(oldClass category).
|
|
781 |
|
2
|
782 |
changeSet := Set new.
|
|
783 |
((oldNames size == 0)
|
|
784 |
or:[newNames startsWith:oldNames]) ifTrue:[
|
159
|
785 |
"new variable(s) has/have been added - old methods still work"
|
2
|
786 |
|
68
|
787 |
" "
|
159
|
788 |
Transcript showCr:'copying methods ...'.
|
|
789 |
Transcript endEntry.
|
68
|
790 |
" "
|
159
|
791 |
self copyMethodsFrom:self for:newMetaclass.
|
|
792 |
self copyMethodsFrom:oldClass for:newClass.
|
2
|
793 |
|
251
|
794 |
"
|
|
795 |
but have to recompile methods accessing stuff now defined
|
|
796 |
(it might have been a global before ...)
|
|
797 |
"
|
2
|
798 |
|
159
|
799 |
addedNames := newNames select:[:nm | (oldNames includes:nm) not].
|
251
|
800 |
" "
|
159
|
801 |
Transcript showCr:'recompiling methods accessing ' , addedNames printString , '...'.
|
|
802 |
Transcript endEntry.
|
251
|
803 |
" "
|
159
|
804 |
"recompile class-methods"
|
|
805 |
newMetaclass recompileMethodsAccessingAny:addedNames.
|
2
|
806 |
] ifFalse:[
|
308
|
807 |
"
|
|
808 |
create the changeSet; thats the set of class instvar names
|
|
809 |
which have changed their position or are new
|
|
810 |
"
|
|
811 |
offset := 0. oldOffsets := Dictionary new.
|
|
812 |
oldNames do:[:nm | offset := offset + 1. oldOffsets at:nm put:offset].
|
|
813 |
offset := 0. newOffsets := Dictionary new.
|
|
814 |
newNames do:[:nm | offset := offset + 1. newOffsets at:nm put:offset].
|
|
815 |
|
|
816 |
oldOffsets associationsDo:[:a |
|
|
817 |
|k|
|
|
818 |
|
|
819 |
k := a key.
|
|
820 |
(newOffsets includesKey:k) ifFalse:[
|
|
821 |
changeSet add:k
|
|
822 |
] ifTrue:[
|
|
823 |
(a value ~~ (newOffsets at:k)) ifTrue:[
|
|
824 |
changeSet add:k
|
|
825 |
]
|
|
826 |
]
|
|
827 |
].
|
|
828 |
newOffsets associationsDo:[:a |
|
|
829 |
|k|
|
|
830 |
|
|
831 |
k := a key.
|
|
832 |
(oldOffsets includesKey:k) ifFalse:[
|
|
833 |
changeSet add:k
|
|
834 |
] ifTrue:[
|
|
835 |
(a value ~~ (oldOffsets at:k)) ifTrue:[
|
|
836 |
changeSet add:k
|
|
837 |
]
|
|
838 |
]
|
|
839 |
].
|
|
840 |
|
251
|
841 |
" "
|
159
|
842 |
Transcript showCr:'recompiling methods accessing ' , changeSet printString , ' ...'.
|
|
843 |
Transcript endEntry.
|
251
|
844 |
" "
|
|
845 |
"
|
|
846 |
recompile class-methods
|
|
847 |
"
|
159
|
848 |
self copyInvalidatedMethodsFrom:self for:newMetaclass accessingAny:changeSet.
|
|
849 |
newMetaclass recompileInvalidatedMethods:(Metaclass compiledMethodAt:#invalidMethod).
|
1
|
850 |
|
159
|
851 |
self copyMethodsFrom:oldClass for:newClass.
|
1
|
852 |
].
|
|
853 |
|
2
|
854 |
delta := newNames size - oldNames size.
|
|
855 |
|
68
|
856 |
"
|
|
857 |
get list of all subclasses - do before superclass is changed
|
|
858 |
"
|
1
|
859 |
allSubclasses := oldClass allSubclasses.
|
2
|
860 |
allSubclasses := allSubclasses asSortedCollection:[:a :b |
|
159
|
861 |
b isSubclassOf:a
|
|
862 |
].
|
1
|
863 |
|
2
|
864 |
oldToNew := IdentityDictionary new.
|
|
865 |
|
251
|
866 |
"
|
|
867 |
create a new class tree, based on new version
|
|
868 |
"
|
2
|
869 |
allSubclasses do:[:aSubclass |
|
159
|
870 |
oldSuper := aSubclass superclass.
|
|
871 |
oldSubMeta := aSubclass class.
|
1
|
872 |
|
159
|
873 |
newSubMeta := Metaclass new.
|
|
874 |
oldSuper == oldClass ifTrue:[
|
|
875 |
newSubMeta setSuperclass:newMetaclass.
|
|
876 |
] ifFalse:[
|
|
877 |
newSubMeta setSuperclass:(oldToNew at:oldSuper) class.
|
|
878 |
].
|
|
879 |
newSubMeta instSize:(oldSubMeta instSize + delta).
|
|
880 |
newSubMeta flags:(oldSubMeta flags).
|
|
881 |
newSubMeta setName:(oldSubMeta name).
|
251
|
882 |
newSubMeta setInstanceVariableString:(oldSubMeta instanceVariableString).
|
159
|
883 |
newSubMeta classVariableString:(oldSubMeta classVariableString).
|
|
884 |
newSubMeta setComment:(oldSubMeta comment).
|
|
885 |
newSubMeta category:(oldSubMeta category).
|
2
|
886 |
|
159
|
887 |
newSub := newSubMeta new.
|
|
888 |
oldSuper == oldClass ifTrue:[
|
|
889 |
newSub setSuperclass:newClass.
|
|
890 |
] ifFalse:[
|
|
891 |
newSub setSuperclass:(oldToNew at:oldSuper).
|
|
892 |
].
|
|
893 |
newSub setSelectorArray:(aSubclass selectorArray).
|
|
894 |
newSub setMethodArray:(aSubclass methodArray).
|
|
895 |
newSub setName:(aSubclass name).
|
|
896 |
newSub classVariableString:(aSubclass classVariableString).
|
|
897 |
newSub setComment:(aSubclass comment).
|
|
898 |
newSub category:(aSubclass category).
|
2
|
899 |
|
159
|
900 |
oldToNew at:aSubclass put:newSub.
|
2
|
901 |
|
159
|
902 |
aSubclass category:'obsolete'.
|
|
903 |
aSubclass class category:'obsolete'.
|
1
|
904 |
].
|
|
905 |
|
2
|
906 |
"recompile what needs to be"
|
|
907 |
|
|
908 |
delta == 0 ifTrue:[
|
159
|
909 |
"only have to recompile class methods accessing
|
|
910 |
class instvars from changeset
|
|
911 |
"
|
1
|
912 |
|
251
|
913 |
allSubclasses do:[:oldSubclass |
|
|
914 |
|newSubclass|
|
|
915 |
|
|
916 |
newSubclass := oldToNew at:oldSubclass.
|
|
917 |
|
|
918 |
Transcript showCr:'recompiling class methods of ' , newSubclass class name ,
|
|
919 |
' accessing any of ' , changeSet printString.
|
|
920 |
|
|
921 |
newSubclass class recompileMethodsAccessingAny:changeSet.
|
159
|
922 |
]
|
2
|
923 |
] ifFalse:[
|
159
|
924 |
"
|
|
925 |
have to recompile all class methods accessing class instvars
|
|
926 |
"
|
251
|
927 |
commonClassInstVars := oldClass class allInstVarNames.
|
|
928 |
changeSet do:[:v |
|
|
929 |
commonClassInstVars remove:v ifAbsent:[]
|
|
930 |
].
|
2
|
931 |
|
251
|
932 |
allSubclasses do:[:oldSubclass |
|
|
933 |
|newSubclass classInstVars|
|
|
934 |
|
|
935 |
newSubclass := oldToNew at:oldSubclass.
|
2
|
936 |
|
251
|
937 |
classInstVars := newSubclass class allInstVarNames asSet.
|
|
938 |
classInstVars removeAll:commonClassInstVars.
|
|
939 |
classInstVars addAll:changeSet.
|
|
940 |
|
|
941 |
Transcript showCr:'recompiling class methods of ' , newSubclass class name ,
|
|
942 |
' accessing any of ' , classInstVars printString.
|
|
943 |
|
|
944 |
newSubclass class recompileMethodsAccessingAny:classInstVars.
|
159
|
945 |
]
|
1
|
946 |
].
|
|
947 |
|
2
|
948 |
self addChangeRecordForClassInstvars:newClass.
|
|
949 |
|
|
950 |
"install all new classes"
|
|
951 |
|
|
952 |
Smalltalk at:(oldClass name asSymbol) put:newClass.
|
|
953 |
ObjectMemory flushCachesFor:oldClass.
|
251
|
954 |
|
|
955 |
allSubclasses do:[:oldClass |
|
|
956 |
|newClass|
|
|
957 |
|
|
958 |
newClass := oldToNew at:oldClass.
|
|
959 |
"
|
|
960 |
Transcript showCr:'install ' , newClass name , '(' , newClass category , ')' ,
|
|
961 |
' as ' , newClass name.
|
|
962 |
"
|
|
963 |
Smalltalk at:newClass name asSymbol put:newClass.
|
|
964 |
ObjectMemory flushCachesFor:oldClass.
|
2
|
965 |
].
|
|
966 |
|
68
|
967 |
"tell dependents ..."
|
10
|
968 |
|
202
|
969 |
oldClass changed:#definition.
|
|
970 |
self changed:#definition.
|
68
|
971 |
|
1
|
972 |
^ newMetaclass
|
|
973 |
! !
|
|
974 |
|
|
975 |
!Metaclass methodsFor:'queries'!
|
|
976 |
|
|
977 |
isMeta
|
|
978 |
"return true, if the receiver is some kind of metaclass;
|
|
979 |
true is returned here. Redefines isMeta in Object"
|
|
980 |
|
|
981 |
^ true
|
|
982 |
! !
|
|
983 |
|
|
984 |
!Metaclass methodsFor:'private'!
|
|
985 |
|
68
|
986 |
invalidMethod
|
|
987 |
"When recompiling classes after a definition-change, all
|
|
988 |
uncompilable methods will be bound to this method here,
|
|
989 |
so that evaluating such an uncompilable method will trigger an error.
|
|
990 |
Can also happen when Compiler/runtime system is broken."
|
|
991 |
|
|
992 |
self error:'invalid method - this method failed to compile when the class was changed'
|
|
993 |
!
|
|
994 |
|
308
|
995 |
differentInstanceVariableOffsetsIn:class1 and:class2
|
|
996 |
"return a set of instance variable names which have different
|
|
997 |
positions in class1 and class2.
|
|
998 |
Also, variables which are only present in one class are returned.
|
|
999 |
This is used to find methods which need recompilation after a
|
|
1000 |
change in the instance variables."
|
|
1001 |
|
|
1002 |
|offsets1 offsets2 changeSet|
|
|
1003 |
|
|
1004 |
changeSet := Set new.
|
|
1005 |
|
|
1006 |
"
|
|
1007 |
collect the instvar-indices in the old and new class
|
|
1008 |
"
|
|
1009 |
offsets1 := class1 instanceVariableOffsets.
|
|
1010 |
offsets2 := class2 instanceVariableOffsets.
|
|
1011 |
|
|
1012 |
"
|
|
1013 |
the changeset consists of instance variables,
|
|
1014 |
with a different position
|
|
1015 |
"
|
|
1016 |
offsets1 keysAndValuesDo:[:varName :varIndex |
|
|
1017 |
(offsets2 includesKey:varName) ifFalse:[
|
|
1018 |
changeSet add:varName
|
|
1019 |
] ifTrue:[
|
|
1020 |
(varIndex ~~ (offsets2 at:varName)) ifTrue:[
|
|
1021 |
changeSet add:varName
|
|
1022 |
]
|
|
1023 |
]
|
|
1024 |
].
|
|
1025 |
offsets2 keysAndValuesDo:[:varName :varIndex |
|
|
1026 |
(offsets1 includesKey:varName) ifFalse:[
|
|
1027 |
changeSet add:varName
|
|
1028 |
] ifTrue:[
|
|
1029 |
(varIndex ~~ (offsets1 at:varName)) ifTrue:[
|
|
1030 |
changeSet add:varName
|
|
1031 |
]
|
|
1032 |
]
|
|
1033 |
].
|
|
1034 |
^ changeSet
|
|
1035 |
|
|
1036 |
"
|
|
1037 |
View class
|
|
1038 |
differentInstanceVariableOffsetsIn:View
|
|
1039 |
and:StandardSystemView
|
|
1040 |
View class
|
|
1041 |
differentInstanceVariableOffsetsIn:Object
|
|
1042 |
and:Point
|
|
1043 |
"
|
|
1044 |
!
|
|
1045 |
|
1
|
1046 |
copyMethodsFrom:oldClass for:newClass
|
159
|
1047 |
"copy all methods from oldClass to newClass.
|
|
1048 |
This is used for class-methods when a class has changed, but metaclass is
|
|
1049 |
unaffected (i.e. classVars/inheritance have not changed) so there is no need
|
|
1050 |
to recompile the class methods."
|
1
|
1051 |
|
68
|
1052 |
newClass selectors:(oldClass selectorArray copy)
|
159
|
1053 |
methods:(oldClass methodArray copy)
|
1
|
1054 |
!
|
|
1055 |
|
|
1056 |
copyInvalidatedMethodsFrom:oldClass for:newClass
|
159
|
1057 |
"copy all methods from oldClass to newClass and change their code
|
|
1058 |
to a trap method reporting an error.
|
|
1059 |
This is used when a class has been changed its layout or inheritance,
|
|
1060 |
for all methods; before recompilation is attempted.
|
|
1061 |
This allows us to keep the source while trapping uncompilable (due to
|
|
1062 |
now undefined instvars) methods. Compilation of these methods will show
|
|
1063 |
an error on the transcript and lead to the debugger once called."
|
1
|
1064 |
|
251
|
1065 |
|trap trapCode trapByteCode oldMethod newMethod oldMethodArray newMethodArray|
|
1
|
1066 |
|
68
|
1067 |
trap := Metaclass compiledMethodAt:#invalidMethod.
|
1
|
1068 |
trapCode := trap code.
|
|
1069 |
trapByteCode := trap byteCode.
|
|
1070 |
|
68
|
1071 |
oldMethodArray := oldClass methodArray.
|
2
|
1072 |
newMethodArray := Array new:(oldMethodArray size).
|
68
|
1073 |
newClass selectors:(oldClass selectorArray copy)
|
159
|
1074 |
methods:newMethodArray.
|
2
|
1075 |
1 to:oldMethodArray size do:[:i |
|
251
|
1076 |
oldMethod := oldMethodArray at:i.
|
|
1077 |
oldMethod isWrapped ifTrue:[
|
|
1078 |
oldMethod := oldMethod originalMethod
|
|
1079 |
].
|
|
1080 |
newMethod := oldMethod copy.
|
159
|
1081 |
newMethod code:trapCode.
|
|
1082 |
newMethod literals:nil.
|
|
1083 |
newMethod byteCode:trapByteCode.
|
|
1084 |
newMethodArray at:i put:newMethod
|
2
|
1085 |
]
|
|
1086 |
!
|
|
1087 |
|
|
1088 |
copyInvalidatedMethodsFrom:oldClass for:newClass accessingAny:setOfNames
|
|
1089 |
"copy all methods from oldClass to newClass. Those methods accessing
|
|
1090 |
a variable in setOfNames will be copied as invalid method, leading to
|
159
|
1091 |
a trap when its executed. This is used when a class has changed its
|
|
1092 |
layout for all methods which are affected by the change."
|
|
1093 |
|
|
1094 |
self copyInvalidatedMethodsFrom:oldClass
|
|
1095 |
for:newClass
|
|
1096 |
accessingAny:setOfNames
|
|
1097 |
orSuper:false
|
|
1098 |
!
|
|
1099 |
|
|
1100 |
copyInvalidatedMethodsFrom:oldClass for:newClass accessingAny:setOfNames orSuper:superBoolean
|
|
1101 |
"copy all methods from oldClass to newClass.
|
|
1102 |
Those methods accessing a variable in setOfNames will be copied as invalid method,
|
|
1103 |
leading to a trap when its executed. If superBoolean is true, this is also done
|
|
1104 |
for methods accessing super. This is used when a class has changed its
|
|
1105 |
layout for all methods which are affected by the change."
|
2
|
1106 |
|
251
|
1107 |
|trap trapCode trapByteCode p source mustInvalidate
|
202
|
1108 |
oldMethod newMethod oldMethodArray newMethodArray|
|
2
|
1109 |
|
68
|
1110 |
trap := Metaclass compiledMethodAt:#invalidMethod.
|
2
|
1111 |
trapCode := trap code.
|
|
1112 |
trapByteCode := trap byteCode.
|
|
1113 |
|
68
|
1114 |
oldMethodArray := oldClass methodArray.
|
2
|
1115 |
newMethodArray := Array new:(oldMethodArray size).
|
68
|
1116 |
newClass selectors:(oldClass selectorArray copy)
|
159
|
1117 |
methods:newMethodArray.
|
2
|
1118 |
1 to:oldMethodArray size do:[:i |
|
159
|
1119 |
oldMethod := oldMethodArray at:i.
|
251
|
1120 |
oldMethod isWrapped ifTrue:[
|
|
1121 |
oldMethod := oldMethod originalMethod
|
|
1122 |
].
|
202
|
1123 |
|
|
1124 |
"before parsing (which may take some time),
|
|
1125 |
do a string search if its only one variable,
|
|
1126 |
we are looking for."
|
|
1127 |
|
251
|
1128 |
source := oldMethod source.
|
202
|
1129 |
((setOfNames size == 1) and:[superBoolean not]) ifTrue:[
|
251
|
1130 |
mustInvalidate := (source findString:(setOfNames first)) ~~ 0.
|
202
|
1131 |
] ifFalse:[
|
251
|
1132 |
((setOfNames size == 0) and:[superBoolean]) ifTrue:[
|
|
1133 |
mustInvalidate := (source findString:'super') ~~ 0.
|
|
1134 |
] ifFalse:[
|
|
1135 |
mustInvalidate := true
|
|
1136 |
].
|
202
|
1137 |
].
|
|
1138 |
|
|
1139 |
mustInvalidate ifTrue:[
|
251
|
1140 |
"we have to parse it ..."
|
|
1141 |
p := Parser parseMethod:source in:newClass.
|
202
|
1142 |
(p isNil
|
|
1143 |
or:[(p usedVars notNil and:[p usedVars includesAny:setOfNames])
|
|
1144 |
or:[superBoolean and:[p usesSuper]]]) ifFalse:[
|
|
1145 |
mustInvalidate := false
|
|
1146 |
]
|
|
1147 |
].
|
|
1148 |
|
|
1149 |
mustInvalidate ifTrue:[
|
159
|
1150 |
newMethod := oldMethod copy.
|
|
1151 |
newMethod code:trapCode.
|
|
1152 |
newMethod literals:nil.
|
|
1153 |
newMethod byteCode:trapByteCode
|
|
1154 |
] ifFalse:[
|
|
1155 |
newMethod := oldMethod.
|
|
1156 |
].
|
|
1157 |
newMethodArray at:i put:newMethod
|
1
|
1158 |
]
|
|
1159 |
!
|
|
1160 |
|
|
1161 |
anyInvalidatedMethodsIn:aClass
|
|
1162 |
"return true, if aClass has any invalidated methods in it"
|
|
1163 |
|
|
1164 |
|trap trapCode trapByteCode|
|
|
1165 |
|
68
|
1166 |
trap := Metaclass compiledMethodAt:#invalidMethod.
|
1
|
1167 |
trapCode := trap code.
|
|
1168 |
trapByteCode := trap byteCode.
|
|
1169 |
|
68
|
1170 |
aClass methodArray do:[:aMethod |
|
159
|
1171 |
trapCode notNil ifTrue:[
|
|
1172 |
(aMethod code == trapCode) ifTrue:[^ true]
|
|
1173 |
].
|
|
1174 |
trapByteCode notNil ifTrue:[
|
|
1175 |
(aMethod byteCode == trapByteCode) ifTrue:[^ true]
|
|
1176 |
]
|
1
|
1177 |
].
|
|
1178 |
^ false
|
195
|
1179 |
!
|
|
1180 |
|
|
1181 |
checkConventionsFor:className instVarNames:instVarNameString classVarNames:classVarNameString
|
|
1182 |
"Check for some 'considered bad-style' things, like lower case names.
|
|
1183 |
NOTICE:
|
|
1184 |
I dont like the confirmers below - we need a notifying: argument, to give
|
|
1185 |
the outer codeview a chance to highlight the error.
|
|
1186 |
(but thats how its defined in the book - maybe I will change it anyway).
|
|
1187 |
"
|
|
1188 |
|
|
1189 |
"let user confirm, if the classname is no good"
|
|
1190 |
className first isUppercase ifFalse:[
|
202
|
1191 |
(self confirm:'classename ''' , className , ''' should start with an uppercase letter
|
195
|
1192 |
(by convention only)
|
|
1193 |
|
|
1194 |
install anyway ?' withCRs)
|
|
1195 |
ifFalse:[
|
|
1196 |
^ false
|
|
1197 |
]
|
|
1198 |
].
|
|
1199 |
|
|
1200 |
"let user confirm, if any instvarname is no good"
|
|
1201 |
(instVarNameString asCollectionOfWords
|
|
1202 |
findFirst:[:word | word first isUppercase]) ~~ 0 ifTrue:[
|
|
1203 |
(self confirm:'instance variable names should start with a lowercase letter
|
|
1204 |
(by convention only)
|
|
1205 |
|
|
1206 |
install anyway ?' withCRs)
|
|
1207 |
ifFalse:[
|
|
1208 |
^ false
|
|
1209 |
]
|
|
1210 |
].
|
|
1211 |
|
|
1212 |
"let user confirm, if any classvarname is no good"
|
|
1213 |
(classVarNameString asCollectionOfWords
|
|
1214 |
findFirst:[:word | word first isLowercase]) ~~ 0 ifTrue:[
|
|
1215 |
(self confirm:'class variable names should start with an uppercase letter
|
|
1216 |
(by convention only)
|
|
1217 |
|
|
1218 |
install anyway ?' withCRs)
|
|
1219 |
ifFalse:[
|
|
1220 |
^ false
|
|
1221 |
].
|
|
1222 |
].
|
|
1223 |
|
|
1224 |
^ true
|
1
|
1225 |
! !
|