NamespaceAwareLookup.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Thu, 20 May 2010 14:35:23 +0100
branchjv
changeset 17766 0acf634e6550
child 17775 90a5bae0a710
permissions -rw-r--r--
initial implementation of selector namespaces

"
 COPYRIGHT (c) 2006 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libbasic' }"

Lookup subclass:#NamespaceAwareLookup
	instanceVariableNames:''
	classVariableNames:'Instance'
	poolDictionaries:''
	category:'Kernel-Extensions'
!

!NamespaceAwareLookup class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2006 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
! !

!NamespaceAwareLookup class methodsFor:'accessing'!

instance

    Instance ifNil:[Instance := self basicNew].
    ^Instance

    "Created: / 20-05-2010 / 11:18:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!NamespaceAwareLookup methodsFor:'lookup'!

lookupMethodForSelector: selector directedTo: searchClass for: receiver withArguments: argArrayOrNil from: sendingContext 
    "invoked by the VM to ask me for a method to call.
     The arguments are: the selector, receiver and arguments,
     the class to start the search in (for here-, super and directed sends)
     the sending context.

     The returned method object will be put into the inline- and polyCache
     at the call site; it might therefore be called more than once for the
     same receiver-class/selector combination (once for each call site).
     If I return nil, a doesNotUnderstand will be invoked."
    "I implement a selector-namespace-aware lookup"
    
    | method  queue  visited |

    queue := Queue new.
    visited := Set new.
    (method := sendingContext method) 
        ifNotNil: [method nameSpace ifNotNil: [queue add: method nameSpace]].
    [queue notEmpty] whileTrue: 
            [| ns  mthd |

            ns := queue removeFirst.
            visited add: ns.
            mthd := self 
                        lookupMethodForSelector: selector
                        directedTo: searchClass
                        inNamespace: ns.
            mthd ifNotNil: [^ mthd].
            ns imports 
                do: [:importedNs | (visited includes: importedNs) ifFalse: [queue add: importedNs]]].
     "Fallback, lookup for method in no namespace"
    ^ self 
        lookupMethodForSelector: selector
        directedTo: searchClass
        inNamespace: nil

    "Created: / 20-05-2010 / 10:10:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 20-05-2010 / 11:42:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

lookupMethodForSelector: selector directedTo: initialSearchClass inNamespace: nameSpace

    "Searches initialSearchClass for a method with given selector in given nameSpace.
     if no method in given namespace is found, returns nil"

    | searchClass |
    searchClass := initialSearchClass.
    [ searchClass notNil ] whileTrue:        
        [searchClass selectorsAndMethodsDo:
            [:sel :mthd|
            (sel = selector and:[mthd nameSpace = nameSpace]) 
                ifTrue:[^mthd]].
        searchClass := searchClass superclass].
    ^nil

    "Created: / 20-05-2010 / 10:12:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!NamespaceAwareLookup class methodsFor:'documentation'!

version_SVN
    ^ '$Id: NamespaceAwareLookup.st 10524 2010-05-20 13:35:23Z vranyj1 $'
! !