author | Claus Gittinger <cg@exept.de> |
Mon, 22 Jan 1996 14:20:03 +0100 | |
changeset 886 | 62c66d49077d |
parent 860 | b3ffea40999a |
child 921 | 4db7603f9506 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1991 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 |
||
620 | 13 |
nil subclass:#Autoload |
14 |
instanceVariableNames:'' |
|
15 |
classVariableNames:'LazyLoading AutoloadFailedSignal LoadedClasses' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Kernel-Classes' |
|
1 | 18 |
! |
19 |
||
68 | 20 |
!Autoload class methodsFor:'documentation'! |
21 |
||
88 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1991 by Claus Gittinger |
|
154 | 25 |
All Rights Reserved |
88 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
68 | 36 |
documentation |
37 |
" |
|
38 |
In memory limited systems (as my 8Mb 386 is) all seldom-used classes are made |
|
39 |
subclasses of this class. Autoload catches all messages and |
|
40 |
files-In the corresponding code when first used. Then the cought message |
|
41 |
is resent to the (now existing) class. |
|
42 |
||
43 |
Late addition: above comment is no longer true - I have made now almost |
|
44 |
all Demos & Goodies be autoloaded ... even for big systems. |
|
45 |
||
46 |
class variables: |
|
92 | 47 |
|
154 | 48 |
LazyLoading <Boolean> if true, the loaded classes |
49 |
methods will NOT be compiled at |
|
50 |
autoload time, but instead when |
|
51 |
first called. This allows for a |
|
52 |
faster load. However, expect short |
|
53 |
pauses later when the methods are |
|
54 |
first executed. |
|
126 | 55 |
|
154 | 56 |
AutoloadFailedSignal <Signal> signal raised if an autoloaded |
57 |
classes source is not available. |
|
68 | 58 |
" |
59 |
! ! |
|
60 |
||
126 | 61 |
!Autoload class methodsFor:'initialization'! |
62 |
||
63 |
initialize |
|
64 |
AutoloadFailedSignal isNil ifTrue:[ |
|
624
f09ad5cf21d4
cannot access Objects classVars directly in nil-subclass
Claus Gittinger <cg@exept.de>
parents:
620
diff
changeset
|
65 |
AutoloadFailedSignal := Object errorSignal newSignalMayProceed:true. |
154 | 66 |
AutoloadFailedSignal nameClass:self message:#autoloadFailedSignal. |
67 |
AutoloadFailedSignal notifierString:'autoload failed '. |
|
197 | 68 |
|
69 |
self setSuperclass:nil. |
|
70 |
ObjectMemory flushCaches. |
|
126 | 71 |
] |
72 |
! ! |
|
73 |
||
345 | 74 |
!Autoload class methodsFor:'Signal constants'! |
126 | 75 |
|
76 |
autoloadFailedSignal |
|
77 |
"return the signal raised when an autoload fails" |
|
78 |
||
79 |
^ AutoloadFailedSignal |
|
80 |
! ! |
|
81 |
||
359 | 82 |
!Autoload class methodsFor:'adding/removing autoloaded classes'! |
83 |
||
216 | 84 |
addClass:aClassName |
85 |
self addClass:aClassName inCategory:'autoloaded-Classes' |
|
86 |
||
87 |
" |
|
88 |
Autoload addClass:'Clock' |
|
89 |
" |
|
90 |
! |
|
91 |
||
92 |
addClass:aClassName inCategory:aCategory |
|
93 |
|nameSymbol| |
|
94 |
||
95 |
nameSymbol := aClassName asSymbol. |
|
96 |
(Smalltalk at:nameSymbol) isNil ifTrue:[ |
|
97 |
Autoload subclass:nameSymbol |
|
98 |
instanceVariableNames:'' |
|
99 |
classVariableNames:'' |
|
100 |
poolDictionaries:'' |
|
101 |
category:aCategory. |
|
102 |
] |
|
103 |
" |
|
104 |
Autoload addClass:'Clock' inCategory:'autoloaded-Demos' |
|
105 |
" |
|
620 | 106 |
! |
107 |
||
108 |
removeClass:aClass |
|
109 |
LoadedClasses remove:aClass ifAbsent:[] |
|
110 |
! ! |
|
111 |
||
112 |
!Autoload class methodsFor:'fileout'! |
|
113 |
||
114 |
fileOutDefinitionOn:aStream |
|
115 |
"print an expression to define myself on aStream. |
|
116 |
Since autoloaded classes dont know their real definition, simply |
|
117 |
output some comment string making things clear in the browser." |
|
118 |
||
709
c31cbd5e5323
more info for autoloaded classes (for browsers definition text)
Claus Gittinger <cg@exept.de>
parents:
661
diff
changeset
|
119 |
|myName fileName nm mgr classFileName packageDir| |
620 | 120 |
|
121 |
(self == Autoload) ifTrue:[^ super fileOutDefinitionOn:aStream]. |
|
122 |
||
123 |
myName := self name. |
|
124 |
aStream nextPutAll:'"' ; nextPutAll:'Notice from Autoload:'; cr; cr; |
|
739
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
125 |
spaces:4; nextPutAll:myName , ' is not yet loaded.'; cr; cr. |
620 | 126 |
aStream nextPutAll:'to load, execute: '. |
127 |
aStream cr; cr; spaces:4; nextPutAll:myName , ' autoload'; cr. |
|
128 |
||
129 |
" |
|
130 |
the following is simply informative ... |
|
131 |
actually, its a hack & kludge - there ought to be a method for this |
|
132 |
in Smalltalk |
|
133 |
(knowing the details of loading here is no good coding style) |
|
134 |
" |
|
135 |
fileName := Smalltalk fileNameForClass:myName. |
|
136 |
(ObjectFileLoader notNil and:[Smalltalk loadBinaries]) ifTrue:[ |
|
739
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
137 |
(nm := Smalltalk libraryFileNameOfClass:myName) notNil ifTrue:[ |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
138 |
nm := nm , ' (a classLibrary, possibly including more classes)' |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
139 |
] ifFalse:[ |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
140 |
nm := Smalltalk getBinaryFileName:(fileName , '.so'). |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
141 |
nm isNil ifTrue:[ |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
142 |
nm := Smalltalk getBinaryFileName:(fileName , '.o') |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
143 |
]. |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
144 |
nm notNil ifTrue:[ |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
145 |
nm := nm , ' (a classBinary)' |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
146 |
] |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
147 |
]. |
620 | 148 |
]. |
149 |
nm isNil ifTrue:[ |
|
739
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
150 |
nm := Smalltalk getFileInFileName:(fileName , '.st'). |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
151 |
nm isNil ifTrue:[ |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
152 |
nm := Smalltalk getSourceFileName:(fileName , '.st'). |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
153 |
]. |
620 | 154 |
]. |
155 |
nm notNil ifTrue:[ |
|
739
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
156 |
aStream cr; nextPutAll:'When accessed, ' , myName , ' will automatically be loaded'; cr. |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
157 |
aStream nextPutAll:'from: '; cr; spaces:4; nextPutAll:nm. |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
158 |
nm asFilename isSymbolicLink ifTrue:[ |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
159 |
aStream cr; cr. |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
160 |
aStream nextPutAll:'which is a link to: '; cr; spaces:4; |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
161 |
nextPutAll:(nm asFilename linkInfo at:#path). |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
162 |
] |
620 | 163 |
] ifFalse:[ |
739
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
164 |
aStream cr; nextPutAll:'There is currently no file to load ' , myName , ' from.'; cr; cr. |
709
c31cbd5e5323
more info for autoloaded classes (for browsers definition text)
Claus Gittinger <cg@exept.de>
parents:
661
diff
changeset
|
165 |
|
739
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
166 |
(mgr := Smalltalk at:#SourceCodeManager) notNil ifTrue:[ |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
167 |
classFileName := Smalltalk fileNameForClass:myName. |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
168 |
packageDir := Smalltalk sourceDirectoryNameOfClass:myName. |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
169 |
]. |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
170 |
(classFileName notNil and:[packageDir notNil]) ifTrue:[ |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
171 |
aStream nextPutAll:'When accessed, I''ll ask the sourceCodeManager to load the code |
709
c31cbd5e5323
more info for autoloaded classes (for browsers definition text)
Claus Gittinger <cg@exept.de>
parents:
661
diff
changeset
|
172 |
from "' , classFileName , '.st" in the "' , packageDir , '" package.'. |
739
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
173 |
] ifFalse:[ |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
174 |
aStream nextPutAll:'When accessed, an error will be reported.'. |
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
175 |
] |
620 | 176 |
]. |
177 |
aStream cr; nextPutAll:'"'. |
|
709
c31cbd5e5323
more info for autoloaded classes (for browsers definition text)
Claus Gittinger <cg@exept.de>
parents:
661
diff
changeset
|
178 |
|
c31cbd5e5323
more info for autoloaded classes (for browsers definition text)
Claus Gittinger <cg@exept.de>
parents:
661
diff
changeset
|
179 |
"Created: 8.12.1995 / 00:31:53 / cg" |
727
475d224376d3
careful with empty source info in descriptive message
Claus Gittinger <cg@exept.de>
parents:
709
diff
changeset
|
180 |
"Modified: 9.12.1995 / 22:31:02 / cg" |
620 | 181 |
! ! |
182 |
||
183 |
!Autoload class methodsFor:'lazy compilation'! |
|
184 |
||
185 |
compileLazy |
|
186 |
"return the lazy loading flag - if on, fileIn is much faster, |
|
187 |
but pauses are to be expected later, since methods are compiled |
|
188 |
when first executed." |
|
189 |
||
190 |
^ LazyLoading |
|
191 |
! |
|
192 |
||
193 |
compileLazy:aBoolean |
|
194 |
"turn on/off lazy loading - if on, fileIn is much faster, |
|
195 |
but pauses are to be expected later, since methods are compiled |
|
196 |
when first executed. |
|
197 |
If you like it, add a line to your startup file." |
|
198 |
||
199 |
LazyLoading := aBoolean |
|
216 | 200 |
! ! |
201 |
||
1 | 202 |
!Autoload class methodsFor:'loading'! |
203 |
||
204 |
autoload |
|
205 |
"use this to force loading |
|
206 |
- it is defined a noop in all non-autoloading clases" |
|
207 |
||
254 | 208 |
|mySelf myName newClass| |
1 | 209 |
|
210 |
mySelf := self. |
|
164 | 211 |
myName := self name asSymbol. |
1 | 212 |
|
213 |
"remove myself - to avoid recompilation" |
|
164 | 214 |
Smalltalk at:myName put:nil. |
1 | 215 |
|
2 | 216 |
"load it" |
68 | 217 |
Transcript showCr:('autoloading ', myName , ' ...'); endEntry. |
1 | 218 |
|
860
b3ffea40999a
use new becomeSameAs: to migrate to new class.
Claus Gittinger <cg@exept.de>
parents:
789
diff
changeset
|
219 |
[ |
b3ffea40999a
use new becomeSameAs: to migrate to new class.
Claus Gittinger <cg@exept.de>
parents:
789
diff
changeset
|
220 |
Smalltalk fileInClass:myName initialize:false lazy:LazyLoading. |
b3ffea40999a
use new becomeSameAs: to migrate to new class.
Claus Gittinger <cg@exept.de>
parents:
789
diff
changeset
|
221 |
] valueOnUnwindDo:[ |
b3ffea40999a
use new becomeSameAs: to migrate to new class.
Claus Gittinger <cg@exept.de>
parents:
789
diff
changeset
|
222 |
Smalltalk at:myName put:mySelf. |
b3ffea40999a
use new becomeSameAs: to migrate to new class.
Claus Gittinger <cg@exept.de>
parents:
789
diff
changeset
|
223 |
]. |
1 | 224 |
|
225 |
"did it work ?" |
|
164 | 226 |
newClass := Smalltalk at:myName. |
227 |
Smalltalk at:myName put:mySelf. "will be undone by become:" |
|
1 | 228 |
|
138 | 229 |
"no - report the error" |
1 | 230 |
newClass isNil ifTrue:[ |
138 | 231 |
" |
232 |
this signal is raised, if an autoloaded class |
|
233 |
cannot be loaded. Usually, this happends when |
|
154 | 234 |
some sourcefile is missing, not readable or if |
235 |
an entry is missing in the abbreviation file. |
|
236 |
Check for a readable file named <myName>.st |
|
237 |
in the 'source' directory and (if its a long fileName) |
|
238 |
for a corresponding entry in the abbreviation file |
|
239 |
'include/abbrev.stc'. |
|
240 |
Finally, your searchpath could be set wrong - |
|
241 |
both 'source' and 'include' directories must be found in |
|
242 |
one of the directories named in systemPath. |
|
243 |
||
244 |
In the debugger, press 'abort' to continue execution. |
|
138 | 245 |
" |
154 | 246 |
AutoloadFailedSignal |
126 | 247 |
raiseRequestWith:self |
154 | 248 |
errorString:('autoload of ' , myName , ' failed'). |
249 |
^ nil |
|
1 | 250 |
]. |
251 |
||
216 | 252 |
LoadedClasses isNil ifTrue:[ |
253 |
LoadedClasses := IdentitySet new. |
|
254 |
]. |
|
255 |
LoadedClasses add:self. |
|
256 |
||
154 | 257 |
"wow - it worked. now the big trick ..." |
1 | 258 |
|
739
63566c9b691a
oops - after autoloading, the metaClasses soleInstance still pointed to the
Claus Gittinger <cg@exept.de>
parents:
729
diff
changeset
|
259 |
newClass class setSoleInstance:self. "/ will be undone by become ... |
860
b3ffea40999a
use new becomeSameAs: to migrate to new class.
Claus Gittinger <cg@exept.de>
parents:
789
diff
changeset
|
260 |
self becomeSameAs:newClass. |
216 | 261 |
LoadedClasses rehash. |
886 | 262 |
newClass initialize. |
263 |
newClass postAutoload. |
|
264 |
^ newClass |
|
1 | 265 |
! ! |
266 |
||
267 |
!Autoload class methodsFor:'message catching'! |
|
268 |
||
620 | 269 |
basicNew |
270 |
"catch basicNew" |
|
271 |
||
272 |
^ self doesNotUnderstand:(Message selector:#basicNew) |
|
273 |
! |
|
274 |
||
275 |
basicNew:arg |
|
276 |
"catch basicNew:" |
|
277 |
||
278 |
^ self doesNotUnderstand:(Message selector:#basicNew: with:arg) |
|
279 |
! |
|
280 |
||
281 |
comment |
|
282 |
"return the classes comment. |
|
283 |
Autoloaded classes have no comment; but I myself have one" |
|
284 |
||
285 |
(self == Autoload) ifTrue:[^ super comment]. |
|
286 |
^ 'not yet loaded' |
|
287 |
! |
|
288 |
||
1 | 289 |
doesNotUnderstand:aMessage |
290 |
"cought a message; load class and retry" |
|
291 |
||
292 |
|newClass| |
|
293 |
||
164 | 294 |
self ~~ Autoload ifTrue:[ |
295 |
newClass := self autoload. |
|
296 |
newClass notNil ifTrue:[ |
|
297 |
^ newClass perform:(aMessage selector) |
|
298 |
withArguments:(aMessage arguments) |
|
299 |
] |
|
1 | 300 |
]. |
789
155eca125ef0
return value from doesNotUnderstand (so handler can return a value)
Claus Gittinger <cg@exept.de>
parents:
739
diff
changeset
|
301 |
^ super doesNotUnderstand:aMessage |
1 | 302 |
! |
303 |
||
304 |
new |
|
305 |
"catch new" |
|
306 |
||
307 |
^ self doesNotUnderstand:(Message selector:#new) |
|
308 |
! |
|
309 |
||
310 |
new:arg |
|
311 |
"catch new:" |
|
312 |
||
313 |
^ self doesNotUnderstand:(Message selector:#new: with:arg) |
|
314 |
! |
|
315 |
||
316 |
subclass:a1 instanceVariableNames:a2 classVariableNames:a3 poolDictionaries:a4 category:a5 |
|
2 | 317 |
"catch subclass creation - this forces missing superclasses to be |
318 |
loaded first" |
|
319 |
||
68 | 320 |
|newClass sel args| |
1 | 321 |
|
2 | 322 |
"take care: subclassing Autoload must still be possible" |
1 | 323 |
(self == Autoload) ifTrue:[ |
154 | 324 |
^ super |
325 |
subclass:a1 |
|
326 |
instanceVariableNames:a2 |
|
327 |
classVariableNames:a3 |
|
328 |
poolDictionaries:a4 |
|
329 |
category:a5 |
|
1 | 330 |
]. |
331 |
newClass := self autoload. |
|
68 | 332 |
sel := thisContext selector. |
333 |
args := thisContext args. |
|
1 | 334 |
newClass notNil ifTrue:[ |
154 | 335 |
^ newClass perform:sel withArguments:args |
1 | 336 |
]. |
337 |
^ nil |
|
338 |
! ! |
|
11 | 339 |
|
620 | 340 |
!Autoload class methodsFor:'queries'! |
356 | 341 |
|
620 | 342 |
isBehavior |
343 |
"return true if the recevier is some kind of class. |
|
344 |
Autoloaded classes are definitely; therefore return true." |
|
164 | 345 |
|
620 | 346 |
^ true |
347 |
! |
|
356 | 348 |
|
729 | 349 |
isBuiltInClass |
350 |
^ false |
|
351 |
||
352 |
"Created: 10.12.1995 / 00:28:22 / cg" |
|
353 |
! |
|
354 |
||
620 | 355 |
isLoaded |
356 |
"return true, if the class has been loaded; redefined in Autoload; |
|
357 |
see comment there. this allows testing for a class been already loaded." |
|
358 |
||
359 |
^ (self == Autoload) |
|
360 |
! |
|
361 |
||
362 |
wasAutoloaded:aClass |
|
363 |
^ LoadedClasses notNil and:[LoadedClasses includes:aClass] |
|
11 | 364 |
! ! |
620 | 365 |
|
661 | 366 |
!Autoload class methodsFor:'documentation'! |
367 |
||
368 |
version |
|
886 | 369 |
^ '$Header: /cvs/stx/stx/libbasic/Autoload.st,v 1.39 1996-01-22 13:20:03 cg Exp $' |
661 | 370 |
! ! |
620 | 371 |
Autoload initialize! |