|
1 " |
|
2 COPYRIGHT (c) 2006 by eXept Software AG |
|
3 All Rights Reserved |
|
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 "{ Package: 'stx:libbasic' }" |
|
13 |
|
14 Lookup subclass:#NamespaceAwareLookup |
|
15 instanceVariableNames:'' |
|
16 classVariableNames:'Instance' |
|
17 poolDictionaries:'' |
|
18 category:'Kernel-Extensions' |
|
19 ! |
|
20 |
|
21 !NamespaceAwareLookup class methodsFor:'documentation'! |
|
22 |
|
23 copyright |
|
24 " |
|
25 COPYRIGHT (c) 2006 by eXept Software AG |
|
26 All Rights Reserved |
|
27 |
|
28 This software is furnished under a license and may be used |
|
29 only in accordance with the terms of that license and with the |
|
30 inclusion of the above copyright notice. This software may not |
|
31 be provided or otherwise made available to, or used by, any |
|
32 other person. No title to or ownership of the software is |
|
33 hereby transferred. |
|
34 " |
|
35 ! ! |
|
36 |
|
37 !NamespaceAwareLookup class methodsFor:'accessing'! |
|
38 |
|
39 instance |
|
40 |
|
41 Instance ifNil:[Instance := self basicNew]. |
|
42 ^Instance |
|
43 |
|
44 "Created: / 20-05-2010 / 11:18:20 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
45 ! ! |
|
46 |
|
47 !NamespaceAwareLookup methodsFor:'lookup'! |
|
48 |
|
49 lookupMethodForSelector: selector directedTo: searchClass for: receiver withArguments: argArrayOrNil from: sendingContext |
|
50 "invoked by the VM to ask me for a method to call. |
|
51 The arguments are: the selector, receiver and arguments, |
|
52 the class to start the search in (for here-, super and directed sends) |
|
53 the sending context. |
|
54 |
|
55 The returned method object will be put into the inline- and polyCache |
|
56 at the call site; it might therefore be called more than once for the |
|
57 same receiver-class/selector combination (once for each call site). |
|
58 If I return nil, a doesNotUnderstand will be invoked." |
|
59 "I implement a selector-namespace-aware lookup" |
|
60 |
|
61 | method queue visited | |
|
62 |
|
63 queue := Queue new. |
|
64 visited := Set new. |
|
65 (method := sendingContext method) |
|
66 ifNotNil: [method nameSpace ifNotNil: [queue add: method nameSpace]]. |
|
67 [queue notEmpty] whileTrue: |
|
68 [| ns mthd | |
|
69 |
|
70 ns := queue removeFirst. |
|
71 visited add: ns. |
|
72 mthd := self |
|
73 lookupMethodForSelector: selector |
|
74 directedTo: searchClass |
|
75 inNamespace: ns. |
|
76 mthd ifNotNil: [^ mthd]. |
|
77 ns imports |
|
78 do: [:importedNs | (visited includes: importedNs) ifFalse: [queue add: importedNs]]]. |
|
79 "Fallback, lookup for method in no namespace" |
|
80 ^ self |
|
81 lookupMethodForSelector: selector |
|
82 directedTo: searchClass |
|
83 inNamespace: nil |
|
84 |
|
85 "Created: / 20-05-2010 / 10:10:31 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
86 "Modified: / 20-05-2010 / 11:42:30 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
87 ! |
|
88 |
|
89 lookupMethodForSelector: selector directedTo: initialSearchClass inNamespace: nameSpace |
|
90 |
|
91 "Searches initialSearchClass for a method with given selector in given nameSpace. |
|
92 if no method in given namespace is found, returns nil" |
|
93 |
|
94 | searchClass | |
|
95 searchClass := initialSearchClass. |
|
96 [ searchClass notNil ] whileTrue: |
|
97 [searchClass selectorsAndMethodsDo: |
|
98 [:sel :mthd| |
|
99 (sel = selector and:[mthd nameSpace = nameSpace]) |
|
100 ifTrue:[^mthd]]. |
|
101 searchClass := searchClass superclass]. |
|
102 ^nil |
|
103 |
|
104 "Created: / 20-05-2010 / 10:12:18 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
105 ! ! |
|
106 |
|
107 !NamespaceAwareLookup class methodsFor:'documentation'! |
|
108 |
|
109 version_SVN |
|
110 ^ '$Id: NamespaceAwareLookup.st 10524 2010-05-20 13:35:23Z vranyj1 $' |
|
111 ! ! |