author | Claus Gittinger <cg@exept.de> |
Thu, 23 Nov 1995 15:23:19 +0100 | |
changeset 624 | f09ad5cf21d4 |
parent 620 | c7353f86a302 |
child 661 | 7e1c7358c980 |
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 |
" |
620 | 59 |
! |
60 |
||
61 |
version |
|
624
f09ad5cf21d4
cannot access Objects classVars directly in nil-subclass
Claus Gittinger <cg@exept.de>
parents:
620
diff
changeset
|
62 |
^ '$Header: /cvs/stx/stx/libbasic/Autoload.st,v 1.31 1995-11-23 14:23:19 cg Exp $' |
68 | 63 |
! ! |
64 |
||
126 | 65 |
!Autoload class methodsFor:'initialization'! |
66 |
||
67 |
initialize |
|
68 |
AutoloadFailedSignal isNil ifTrue:[ |
|
624
f09ad5cf21d4
cannot access Objects classVars directly in nil-subclass
Claus Gittinger <cg@exept.de>
parents:
620
diff
changeset
|
69 |
AutoloadFailedSignal := Object errorSignal newSignalMayProceed:true. |
154 | 70 |
AutoloadFailedSignal nameClass:self message:#autoloadFailedSignal. |
71 |
AutoloadFailedSignal notifierString:'autoload failed '. |
|
197 | 72 |
|
73 |
self setSuperclass:nil. |
|
74 |
ObjectMemory flushCaches. |
|
126 | 75 |
] |
76 |
! ! |
|
77 |
||
345 | 78 |
!Autoload class methodsFor:'Signal constants'! |
126 | 79 |
|
80 |
autoloadFailedSignal |
|
81 |
"return the signal raised when an autoload fails" |
|
82 |
||
83 |
^ AutoloadFailedSignal |
|
84 |
! ! |
|
85 |
||
359 | 86 |
!Autoload class methodsFor:'adding/removing autoloaded classes'! |
87 |
||
216 | 88 |
addClass:aClassName |
89 |
self addClass:aClassName inCategory:'autoloaded-Classes' |
|
90 |
||
91 |
" |
|
92 |
Autoload addClass:'Clock' |
|
93 |
" |
|
94 |
! |
|
95 |
||
96 |
addClass:aClassName inCategory:aCategory |
|
97 |
|nameSymbol| |
|
98 |
||
99 |
nameSymbol := aClassName asSymbol. |
|
100 |
(Smalltalk at:nameSymbol) isNil ifTrue:[ |
|
101 |
Autoload subclass:nameSymbol |
|
102 |
instanceVariableNames:'' |
|
103 |
classVariableNames:'' |
|
104 |
poolDictionaries:'' |
|
105 |
category:aCategory. |
|
106 |
] |
|
107 |
" |
|
108 |
Autoload addClass:'Clock' inCategory:'autoloaded-Demos' |
|
109 |
" |
|
620 | 110 |
! |
111 |
||
112 |
removeClass:aClass |
|
113 |
LoadedClasses remove:aClass ifAbsent:[] |
|
114 |
! ! |
|
115 |
||
116 |
!Autoload class methodsFor:'fileout'! |
|
117 |
||
118 |
fileOutDefinitionOn:aStream |
|
119 |
"print an expression to define myself on aStream. |
|
120 |
Since autoloaded classes dont know their real definition, simply |
|
121 |
output some comment string making things clear in the browser." |
|
122 |
||
123 |
|myName fileName nm| |
|
124 |
||
125 |
(self == Autoload) ifTrue:[^ super fileOutDefinitionOn:aStream]. |
|
126 |
||
127 |
myName := self name. |
|
128 |
aStream nextPutAll:'"' ; nextPutAll:'Notice from Autoload:'; cr; cr; |
|
129 |
spaces:4; nextPutAll:myName , ' is not yet loaded.'; cr; cr. |
|
130 |
aStream nextPutAll:'to load, execute: '. |
|
131 |
aStream cr; cr; spaces:4; nextPutAll:myName , ' autoload'; cr. |
|
132 |
||
133 |
" |
|
134 |
the following is simply informative ... |
|
135 |
actually, its a hack & kludge - there ought to be a method for this |
|
136 |
in Smalltalk |
|
137 |
(knowing the details of loading here is no good coding style) |
|
138 |
" |
|
139 |
fileName := Smalltalk fileNameForClass:myName. |
|
140 |
(ObjectFileLoader notNil and:[Smalltalk loadBinaries]) ifTrue:[ |
|
141 |
(nm := Smalltalk libraryFileNameOfClass:myName) notNil ifTrue:[ |
|
142 |
nm := nm , ' (a classLibrary, possibly including more classes)' |
|
143 |
] ifFalse:[ |
|
144 |
nm := Smalltalk getBinaryFileName:(fileName , '.so'). |
|
145 |
nm isNil ifTrue:[ |
|
146 |
nm := Smalltalk getBinaryFileName:(fileName , '.o') |
|
147 |
]. |
|
148 |
nm notNil ifTrue:[ |
|
149 |
nm := nm , ' (a classBinary)' |
|
150 |
] |
|
151 |
]. |
|
152 |
]. |
|
153 |
nm isNil ifTrue:[ |
|
154 |
nm := Smalltalk getFileInFileName:(fileName , '.st'). |
|
155 |
nm isNil ifTrue:[ |
|
156 |
nm := Smalltalk getSourceFileName:(fileName , '.st'). |
|
157 |
]. |
|
158 |
]. |
|
159 |
nm notNil ifTrue:[ |
|
160 |
aStream cr; nextPutAll:'When accessed, ' , myName , ' will automatically be loaded'; cr. |
|
161 |
aStream nextPutAll:'from: '; cr; spaces:4; nextPutAll:nm. |
|
162 |
nm asFilename isSymbolicLink ifTrue:[ |
|
163 |
aStream cr; cr. |
|
164 |
aStream nextPutAll:'which is a link to: '; cr; spaces:4; |
|
165 |
nextPutAll:(nm asFilename linkInfo at:#path). |
|
166 |
] |
|
167 |
] ifFalse:[ |
|
168 |
aStream cr; nextPutAll:'there is currently no file to load ' , myName , ' from.'. |
|
169 |
aStream cr; nextPutAll:'When accessed, an error will be reported.'. |
|
170 |
]. |
|
171 |
aStream cr; nextPutAll:'"'. |
|
172 |
! ! |
|
173 |
||
174 |
!Autoload class methodsFor:'lazy compilation'! |
|
175 |
||
176 |
compileLazy |
|
177 |
"return the lazy loading flag - if on, fileIn is much faster, |
|
178 |
but pauses are to be expected later, since methods are compiled |
|
179 |
when first executed." |
|
180 |
||
181 |
^ LazyLoading |
|
182 |
! |
|
183 |
||
184 |
compileLazy:aBoolean |
|
185 |
"turn on/off lazy loading - if on, fileIn is much faster, |
|
186 |
but pauses are to be expected later, since methods are compiled |
|
187 |
when first executed. |
|
188 |
If you like it, add a line to your startup file." |
|
189 |
||
190 |
LazyLoading := aBoolean |
|
216 | 191 |
! ! |
192 |
||
1 | 193 |
!Autoload class methodsFor:'loading'! |
194 |
||
195 |
autoload |
|
196 |
"use this to force loading |
|
197 |
- it is defined a noop in all non-autoloading clases" |
|
198 |
||
254 | 199 |
|mySelf myName newClass| |
1 | 200 |
|
201 |
mySelf := self. |
|
164 | 202 |
myName := self name asSymbol. |
1 | 203 |
|
204 |
"remove myself - to avoid recompilation" |
|
164 | 205 |
Smalltalk at:myName put:nil. |
1 | 206 |
|
2 | 207 |
"load it" |
68 | 208 |
Transcript showCr:('autoloading ', myName , ' ...'); endEntry. |
1 | 209 |
|
216 | 210 |
Smalltalk fileInClass:myName initialize:false lazy:LazyLoading. |
1 | 211 |
|
212 |
"did it work ?" |
|
164 | 213 |
newClass := Smalltalk at:myName. |
214 |
Smalltalk at:myName put:mySelf. "will be undone by become:" |
|
1 | 215 |
|
138 | 216 |
"no - report the error" |
1 | 217 |
newClass isNil ifTrue:[ |
138 | 218 |
" |
219 |
this signal is raised, if an autoloaded class |
|
220 |
cannot be loaded. Usually, this happends when |
|
154 | 221 |
some sourcefile is missing, not readable or if |
222 |
an entry is missing in the abbreviation file. |
|
223 |
Check for a readable file named <myName>.st |
|
224 |
in the 'source' directory and (if its a long fileName) |
|
225 |
for a corresponding entry in the abbreviation file |
|
226 |
'include/abbrev.stc'. |
|
227 |
Finally, your searchpath could be set wrong - |
|
228 |
both 'source' and 'include' directories must be found in |
|
229 |
one of the directories named in systemPath. |
|
230 |
||
231 |
In the debugger, press 'abort' to continue execution. |
|
138 | 232 |
" |
154 | 233 |
AutoloadFailedSignal |
126 | 234 |
raiseRequestWith:self |
154 | 235 |
errorString:('autoload of ' , myName , ' failed'). |
236 |
^ nil |
|
1 | 237 |
]. |
238 |
||
216 | 239 |
LoadedClasses isNil ifTrue:[ |
240 |
LoadedClasses := IdentitySet new. |
|
241 |
]. |
|
242 |
LoadedClasses add:self. |
|
243 |
||
154 | 244 |
"wow - it worked. now the big trick ..." |
1 | 245 |
|
246 |
self become:newClass. |
|
216 | 247 |
LoadedClasses rehash. |
345 | 248 |
self initialize. "/ thats the new class now |
328 | 249 |
self postAutoload. |
154 | 250 |
^ self "this is now the new class - see what doesNotUnderstand: does with it" |
1 | 251 |
! ! |
252 |
||
253 |
!Autoload class methodsFor:'message catching'! |
|
254 |
||
620 | 255 |
basicNew |
256 |
"catch basicNew" |
|
257 |
||
258 |
^ self doesNotUnderstand:(Message selector:#basicNew) |
|
259 |
! |
|
260 |
||
261 |
basicNew:arg |
|
262 |
"catch basicNew:" |
|
263 |
||
264 |
^ self doesNotUnderstand:(Message selector:#basicNew: with:arg) |
|
265 |
! |
|
266 |
||
267 |
comment |
|
268 |
"return the classes comment. |
|
269 |
Autoloaded classes have no comment; but I myself have one" |
|
270 |
||
271 |
(self == Autoload) ifTrue:[^ super comment]. |
|
272 |
^ 'not yet loaded' |
|
273 |
! |
|
274 |
||
1 | 275 |
doesNotUnderstand:aMessage |
276 |
"cought a message; load class and retry" |
|
277 |
||
278 |
|newClass| |
|
279 |
||
164 | 280 |
self ~~ Autoload ifTrue:[ |
281 |
newClass := self autoload. |
|
282 |
newClass notNil ifTrue:[ |
|
283 |
^ newClass perform:(aMessage selector) |
|
284 |
withArguments:(aMessage arguments) |
|
285 |
] |
|
1 | 286 |
]. |
287 |
super doesNotUnderstand:aMessage |
|
288 |
! |
|
289 |
||
290 |
new |
|
291 |
"catch new" |
|
292 |
||
293 |
^ self doesNotUnderstand:(Message selector:#new) |
|
294 |
! |
|
295 |
||
296 |
new:arg |
|
297 |
"catch new:" |
|
298 |
||
299 |
^ self doesNotUnderstand:(Message selector:#new: with:arg) |
|
300 |
! |
|
301 |
||
302 |
subclass:a1 instanceVariableNames:a2 classVariableNames:a3 poolDictionaries:a4 category:a5 |
|
2 | 303 |
"catch subclass creation - this forces missing superclasses to be |
304 |
loaded first" |
|
305 |
||
68 | 306 |
|newClass sel args| |
1 | 307 |
|
2 | 308 |
"take care: subclassing Autoload must still be possible" |
1 | 309 |
(self == Autoload) ifTrue:[ |
154 | 310 |
^ super |
311 |
subclass:a1 |
|
312 |
instanceVariableNames:a2 |
|
313 |
classVariableNames:a3 |
|
314 |
poolDictionaries:a4 |
|
315 |
category:a5 |
|
1 | 316 |
]. |
317 |
newClass := self autoload. |
|
68 | 318 |
sel := thisContext selector. |
319 |
args := thisContext args. |
|
1 | 320 |
newClass notNil ifTrue:[ |
154 | 321 |
^ newClass perform:sel withArguments:args |
1 | 322 |
]. |
323 |
^ nil |
|
324 |
! ! |
|
11 | 325 |
|
620 | 326 |
!Autoload class methodsFor:'queries'! |
356 | 327 |
|
620 | 328 |
isBehavior |
329 |
"return true if the recevier is some kind of class. |
|
330 |
Autoloaded classes are definitely; therefore return true." |
|
164 | 331 |
|
620 | 332 |
^ true |
333 |
! |
|
356 | 334 |
|
620 | 335 |
isLoaded |
336 |
"return true, if the class has been loaded; redefined in Autoload; |
|
337 |
see comment there. this allows testing for a class been already loaded." |
|
338 |
||
339 |
^ (self == Autoload) |
|
340 |
! |
|
341 |
||
342 |
wasAutoloaded:aClass |
|
343 |
^ LoadedClasses notNil and:[LoadedClasses includes:aClass] |
|
11 | 344 |
! ! |
620 | 345 |
|
346 |
Autoload initialize! |