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