Method.st
changeset 5324 5c9959ce98af
parent 5322 411b6c0f7250
child 5339 c922450b57b5
equal deleted inserted replaced
5323:f4c3a230f42f 5324:5c9959ce98af
   413     source notNil ifTrue:[
   413     source notNil ifTrue:[
   414 	sourcePosition notNil ifTrue:[
   414 	sourcePosition notNil ifTrue:[
   415 	    self source:(self source)
   415 	    self source:(self source)
   416 	]
   416 	]
   417     ].
   417     ].
       
   418 !
       
   419 
       
   420 mclass:aClass
       
   421     mclass notNil ifTrue:[
       
   422         'Method [warning]: mclass already set' errorPrintCR.
       
   423     ].
       
   424     mclass := aClass.
   418 !
   425 !
   419 
   426 
   420 package
   427 package
   421     "return the package-symbol"
   428     "return the package-symbol"
   422 
   429 
  1782 
  1789 
  1783     "based on who, which has been added for ST-80 compatibility"
  1790     "based on who, which has been added for ST-80 compatibility"
  1784 
  1791 
  1785     |who|
  1792     |who|
  1786 
  1793 
       
  1794     mclass notNil ifTrue:[^ mclass].
       
  1795 
  1787     who := self who.
  1796     who := self who.
  1788     who notNil ifTrue:[^ who methodClass].
  1797     who notNil ifTrue:[^ who methodClass].
  1789     "
  1798     "
  1790      none found - sorry
  1799      none found - sorry
  1791     "
  1800     "
  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
  2295     "untypical situation: an anonymous class"
  2311     "untypical situation: an anonymous class"
  2296     "
  2312     "
  2297      |m cls|
  2313      |m cls|
  2298 
  2314 
  2299      Object 
  2315      Object 
  2300 	subclass:#FunnyClass 
  2316         subclass:#FunnyClass 
  2301 	instanceVariableNames:'foo'
  2317         instanceVariableNames:'foo'
  2302 	classVariableNames:''
  2318         classVariableNames:''
  2303 	poolDictionaries:''
  2319         poolDictionaries:''
  2304 	category:'testing'.
  2320         category:'testing'.
  2305      cls := Smalltalk at:#FunnyClass.
  2321      cls := Smalltalk at:#FunnyClass.
  2306      Smalltalk removeClass:cls.
  2322      Smalltalk removeClass:cls.
  2307 
  2323 
  2308      cls compile:'testMethod1:arg foo:=arg'.
  2324      cls compile:'testMethod1:arg foo:=arg'.
  2309      cls compile:'testMethod2 ^ foo'.
  2325      cls compile:'testMethod2 ^ foo'.
  2507 ! !
  2523 ! !
  2508 
  2524 
  2509 !Method class methodsFor:'documentation'!
  2525 !Method class methodsFor:'documentation'!
  2510 
  2526 
  2511 version
  2527 version
  2512     ^ '$Header: /cvs/stx/stx/libbasic/Method.st,v 1.186 2000-03-24 11:54:40 cg Exp $'
  2528     ^ '$Header: /cvs/stx/stx/libbasic/Method.st,v 1.187 2000-03-24 12:53:35 cg Exp $'
  2513 ! !
  2529 ! !
  2514 Method initialize!
  2530 Method initialize!