2162 who |
2171 who |
2163 "return the class and selector of where I am defined in; |
2172 "return the class and selector of where I am defined in; |
2164 nil is returned for unbound methods. |
2173 nil is returned for unbound methods. |
2165 |
2174 |
2166 ST/X special notice: |
2175 ST/X special notice: |
2167 returns an instance of MethodWhoInfo, which |
2176 returns an instance of MethodWhoInfo, which |
2168 responds to #methodClass and #methodSelector query messages. |
2177 responds to #methodClass and #methodSelector query messages. |
2169 For backward- (& ST-80) compatibility, the returned object also |
2178 For backward- (& ST-80) compatibility, the returned object also |
2170 responds to #at:1 and #at:2 messages. |
2179 responds to #at:1 and #at:2 messages. |
2171 |
2180 |
2172 Implementation notice: |
2181 Implementation notice: |
2173 Since there is no information of the containing class |
2182 Since there is no information of the containing class |
2174 in the method, we have to do a search here. |
2183 in the method, we have to do a search here. |
2175 |
2184 |
2176 Normally, this is not a problem, except when a method is |
2185 Normally, this is not a problem, except when a method is |
2177 accepted in the debugger or redefined from within a method |
2186 accepted in the debugger or redefined from within a method |
2178 (maybe done indirectly, if #doIt is done recursively) |
2187 (maybe done indirectly, if #doIt is done recursively) |
2179 - the information about which class the original method was |
2188 - the information about which class the original method was |
2180 defined in is lost in this case. |
2189 defined in is lost in this case. |
2181 |
2190 |
2182 Problem: |
2191 Problem: |
2183 this is heavily called for in the debugger to create |
2192 this is heavily called for in the debugger to create |
2184 a readable context walkback. For unbound methods, it is |
2193 a readable context walkback. For unbound methods, it is |
2185 slow, since the search (over all classes) will always fail. |
2194 slow, since the search (over all classes) will always fail. |
2186 |
2195 |
2187 Q: should we add a backref from the method to the class |
2196 Q: should we add a backref from the method to the class |
2188 and/or add a subclass of Method for unbound ones ? |
2197 and/or add a subclass of Method for unbound ones ? |
2189 Q2: if so, what about the bad guy then, who copies methods around to |
2198 Q2: if so, what about the bad guy then, who copies methods around to |
2190 other classes ?" |
2199 other classes ?" |
2191 |
2200 |
2192 |classes cls sel fn clsName| |
2201 |classes cls sel fn clsName| |
|
2202 |
|
2203 mclass notNil ifTrue:[ |
|
2204 sel := mclass selectorAtMethod:self. |
|
2205 sel notNil ifTrue:[ |
|
2206 ^ MethodWhoInfo class:mclass selector:sel |
|
2207 ]. |
|
2208 ]. |
2193 |
2209 |
2194 " |
2210 " |
2195 speedup kludge: if my sourceFileName is valid, |
2211 speedup kludge: if my sourceFileName is valid, |
2196 extract the className from it and try that class first. |
2212 extract the className from it and try that class first. |
2197 " |
2213 " |
2198 (fn := self sourceFilename) notNil ifTrue:[ |
2214 (fn := self sourceFilename) notNil ifTrue:[ |
2199 clsName := fn asFilename withoutSuffix name. |
2215 clsName := fn asFilename withoutSuffix name. |
2200 clsName := clsName asSymbolIfInterned. |
2216 clsName := clsName asSymbolIfInterned. |
2201 clsName notNil ifTrue:[ |
2217 clsName notNil ifTrue:[ |
2202 cls := Smalltalk at:clsName ifAbsent:nil. |
2218 cls := Smalltalk at:clsName ifAbsent:nil. |
2203 cls notNil ifTrue:[ |
2219 cls notNil ifTrue:[ |
2204 sel := cls selectorAtMethod:self. |
2220 sel := cls selectorAtMethod:self. |
2205 sel notNil ifTrue:[ |
2221 sel notNil ifTrue:[ |
2206 ^ MethodWhoInfo class:cls selector:sel |
2222 ^ MethodWhoInfo class:cls selector:sel |
2207 ]. |
2223 ]. |
2208 |
2224 |
2209 cls := cls class. |
2225 cls := cls class. |
2210 sel := cls selectorAtMethod:self. |
2226 sel := cls selectorAtMethod:self. |
2211 sel notNil ifTrue:[ |
2227 sel notNil ifTrue:[ |
2212 ^ MethodWhoInfo class:cls selector:sel |
2228 ^ MethodWhoInfo class:cls selector:sel |
2213 ]. |
2229 ]. |
2214 ] |
2230 ] |
2215 ]. |
2231 ]. |
2216 ]. |
2232 ]. |
2217 |
2233 |
2218 " |
2234 " |
2219 then, look in the class we found something the last time |
2235 then, look in the class we found something the last time |
2220 this may often give a hit, when asking who repeatingly for |
2236 this may often give a hit, when asking who repeatingly for |
2221 a context chain. (keep last by its name, to not keep classes from |
2237 a context chain. (keep last by its name, to not keep classes from |
2222 being garbage collected) |
2238 being garbage collected) |
2223 " |
2239 " |
2224 LastWhoClass notNil ifTrue:[ |
2240 LastWhoClass notNil ifTrue:[ |
2225 cls := Smalltalk at:LastWhoClass ifAbsent:nil. |
2241 cls := Smalltalk at:LastWhoClass ifAbsent:nil. |
2226 cls notNil ifTrue:[ |
2242 cls notNil ifTrue:[ |
2227 sel := cls selectorAtMethod:self. |
2243 sel := cls selectorAtMethod:self. |
2228 sel notNil ifTrue:[ |
2244 sel notNil ifTrue:[ |
2229 ^ MethodWhoInfo class:cls selector:sel |
2245 ^ MethodWhoInfo class:cls selector:sel |
2230 ]. |
2246 ]. |
2231 |
2247 |
2232 cls := cls class. |
2248 cls := cls class. |
2233 sel := cls selectorAtMethod:self. |
2249 sel := cls selectorAtMethod:self. |
2234 sel notNil ifTrue:[ |
2250 sel notNil ifTrue:[ |
2235 ^ MethodWhoInfo class:cls selector:sel |
2251 ^ MethodWhoInfo class:cls selector:sel |
2236 ]. |
2252 ]. |
2237 ] |
2253 ] |
2238 ]. |
2254 ]. |
2239 |
2255 |
2240 " |
2256 " |
2241 first, limit the search to global classes only - |
2257 first, limit the search to global classes only - |
2242 since probability is high, that the receiver is found in there ... |
2258 since probability is high, that the receiver is found in there ... |
2244 classes := Smalltalk allClasses. |
2260 classes := Smalltalk allClasses. |
2245 " |
2261 " |
2246 instance methods are usually more common - search those first |
2262 instance methods are usually more common - search those first |
2247 " |
2263 " |
2248 classes do:[:aClass | |
2264 classes do:[:aClass | |
2249 |sel| |
2265 |sel| |
2250 |
2266 |
2251 sel := aClass selectorAtMethod:self ifAbsent:nil. |
2267 sel := aClass selectorAtMethod:self ifAbsent:nil. |
2252 sel notNil ifTrue:[ |
2268 sel notNil ifTrue:[ |
2253 LastWhoClass := aClass theNonMetaclass name. |
2269 LastWhoClass := aClass theNonMetaclass name. |
2254 ^ MethodWhoInfo class:aClass selector:sel |
2270 ^ MethodWhoInfo class:aClass selector:sel |
2255 ]. |
2271 ]. |
2256 ]. |
2272 ]. |
2257 |
2273 |
2258 classes do:[:aClass | |
2274 classes do:[:aClass | |
2259 |sel| |
2275 |sel| |
2260 |
2276 |
2261 sel := aClass class selectorAtMethod:self. |
2277 sel := aClass class selectorAtMethod:self. |
2262 sel notNil ifTrue:[ |
2278 sel notNil ifTrue:[ |
2263 LastWhoClass := aClass theNonMetaclass name. |
2279 LastWhoClass := aClass theNonMetaclass name. |
2264 ^ MethodWhoInfo class:aClass class selector:sel |
2280 ^ MethodWhoInfo class:aClass class selector:sel |
2265 ]. |
2281 ]. |
2266 ]. |
2282 ]. |
2267 |
2283 |
2268 LastWhoClass := nil. |
2284 LastWhoClass := nil. |
2269 " |
2285 " |
2270 mhmh - must be a method of some anonymous class (i.e. one not |
2286 mhmh - must be a method of some anonymous class (i.e. one not |
2271 in the Smalltalk dictionary). Search all instances of Behavior |
2287 in the Smalltalk dictionary). Search all instances of Behavior |
2272 " |
2288 " |
2273 Behavior allSubInstancesDo:[:someClass | |
2289 Behavior allSubInstancesDo:[:someClass | |
2274 |sel| |
2290 |sel| |
2275 |
2291 |
2276 (classes includes:someClass) ifFalse:[ |
2292 (classes includes:someClass) ifFalse:[ |
2277 sel := someClass selectorAtMethod:self. |
2293 sel := someClass selectorAtMethod:self. |
2278 sel notNil ifTrue:[ |
2294 sel notNil ifTrue:[ |
2279 ^ MethodWhoInfo class:someClass selector:sel |
2295 ^ MethodWhoInfo class:someClass selector:sel |
2280 ] |
2296 ] |
2281 ] |
2297 ] |
2282 ]. |
2298 ]. |
2283 " |
2299 " |
2284 none found - sorry |
2300 none found - sorry |
2285 " |
2301 " |
2286 ^ nil |
2302 ^ nil |