13403
|
1 |
"
|
13478
|
2 |
COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague
|
13403
|
3 |
All Rights Reserved
|
|
4 |
|
13478
|
5 |
Permission is hereby granted, free of charge, to any person
|
|
6 |
obtaining a copy of this software and associated documentation
|
|
7 |
files (the 'Software'), to deal in the Software without
|
|
8 |
restriction, including without limitation the rights to use,
|
|
9 |
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10 |
copies of the Software, and to permit persons to whom the
|
|
11 |
Software is furnished to do so, subject to the following
|
|
12 |
conditions:
|
|
13 |
|
|
14 |
The above copyright notice and this permission notice shall be
|
|
15 |
included in all copies or substantial portions of the Software.
|
|
16 |
|
|
17 |
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
18 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
19 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
20 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
21 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
22 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
23 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
24 |
OTHER DEALINGS IN THE SOFTWARE.
|
13403
|
25 |
"
|
|
26 |
"{ Package: 'stx:libbasic' }"
|
|
27 |
|
|
28 |
Lookup subclass:#NamespaceAwareLookup
|
|
29 |
instanceVariableNames:''
|
|
30 |
classVariableNames:'Instance'
|
|
31 |
poolDictionaries:''
|
|
32 |
category:'Kernel-Extensions'
|
|
33 |
!
|
|
34 |
|
|
35 |
!NamespaceAwareLookup class methodsFor:'documentation'!
|
|
36 |
|
|
37 |
copyright
|
|
38 |
"
|
13478
|
39 |
COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague
|
13403
|
40 |
All Rights Reserved
|
|
41 |
|
13478
|
42 |
Permission is hereby granted, free of charge, to any person
|
|
43 |
obtaining a copy of this software and associated documentation
|
|
44 |
files (the 'Software'), to deal in the Software without
|
|
45 |
restriction, including without limitation the rights to use,
|
|
46 |
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
47 |
copies of the Software, and to permit persons to whom the
|
|
48 |
Software is furnished to do so, subject to the following
|
|
49 |
conditions:
|
|
50 |
|
|
51 |
The above copyright notice and this permission notice shall be
|
|
52 |
included in all copies or substantial portions of the Software.
|
|
53 |
|
|
54 |
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
55 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
56 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
57 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
58 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
59 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
60 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
61 |
OTHER DEALINGS IN THE SOFTWARE.
|
13403
|
62 |
"
|
|
63 |
! !
|
|
64 |
|
|
65 |
!NamespaceAwareLookup class methodsFor:'initialization'!
|
|
66 |
|
|
67 |
initialize
|
|
68 |
|
|
69 |
Instance := self basicNew
|
|
70 |
|
|
71 |
"Created: / 10-07-2010 / 21:12:34 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
72 |
! !
|
|
73 |
|
|
74 |
!NamespaceAwareLookup class methodsFor:'accessing'!
|
|
75 |
|
|
76 |
instance
|
|
77 |
|
|
78 |
^Instance
|
|
79 |
|
|
80 |
"Created: / 20-05-2010 / 11:18:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
81 |
"Modified: / 10-07-2010 / 21:12:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
82 |
! !
|
|
83 |
|
|
84 |
!NamespaceAwareLookup class methodsFor:'lookup'!
|
|
85 |
|
|
86 |
lookupMethodForSelector: selector directedTo: initialSearchClass for: receiver withArguments: argArrayOrNil from: sendingContext
|
|
87 |
|
|
88 |
"JV @ 2010-07-24
|
|
89 |
Following C code is just a performance optimization.
|
|
90 |
It is not neccessary, however it speeds things in most
|
|
91 |
cases. Such optimization significantly speeds up the IDE
|
|
92 |
since class browser involves dozens of super-polymorphic
|
|
93 |
sends (> 1000 receiver classes per send-site).
|
|
94 |
"
|
|
95 |
|
|
96 |
%{
|
|
97 |
OBJ sendingMthd = __ContextInstPtr(sendingContext)->c_method;
|
|
98 |
if (__Class(sendingMthd) == Method &&
|
|
99 |
__MethodInstPtr(sendingMthd)->m_annotation == nil) {
|
|
100 |
OBJ m = __lookup(initialSearchClass, selector);
|
|
101 |
if (m != nil) RETURN ( m );
|
|
102 |
}
|
|
103 |
%}.
|
|
104 |
|
|
105 |
^Instance lookupMethodForSelector: selector directedTo: initialSearchClass
|
|
106 |
for: receiver withArguments: argArrayOrNil
|
|
107 |
from: sendingContext
|
|
108 |
|
|
109 |
! !
|
|
110 |
|
|
111 |
!NamespaceAwareLookup methodsFor:'lookup'!
|
|
112 |
|
|
113 |
lookupMethodForSelector: selector directedTo: initialSearchClass for: receiver withArguments: argArrayOrNil from: sendingContext
|
|
114 |
"Invoked by the VM to ask me for a method to fire.
|
|
115 |
For details, see comment inLookup>>lookupMethodForSelector:directedTo:for:withArguments:from:"
|
|
116 |
|
|
117 |
| sendingNs sendingMthd queue seen namespaces methods imports |
|
|
118 |
|
|
119 |
"JV @ 2010-07-24
|
|
120 |
Following C code is just a performance optimization.
|
|
121 |
It is not neccessary, however it speeds things in most
|
|
122 |
cases. Such optimization significantly speeds up the IDE
|
|
123 |
since class browser involves dozens of super-polymorphic
|
|
124 |
sends (> 1000 receiver classes per send-site).
|
|
125 |
"
|
|
126 |
%{
|
|
127 |
sendingMthd = __ContextInstPtr(sendingContext)->c_method;
|
|
128 |
if (__Class(sendingMthd) == Method &&
|
|
129 |
__MethodInstPtr(sendingMthd)->m_annotation == nil) {
|
|
130 |
OBJ m = __lookup(initialSearchClass, selector);
|
|
131 |
if (m != nil) RETURN ( m );
|
|
132 |
}
|
|
133 |
%}.
|
|
134 |
"If you remove C code above, uncomment the line below."
|
|
135 |
"sendingMthd := sendingContext method."
|
|
136 |
sendingNs := sendingMthd isNil
|
|
137 |
ifTrue:[nil]
|
|
138 |
ifFalse:[sendingMthd nameSpace].
|
|
139 |
sendingNs := sendingNs.
|
|
140 |
|
|
141 |
"Second chance to speed up things (in case sending method
|
|
142 |
has resource or so)"
|
|
143 |
%{
|
|
144 |
if (sendingNs == nil) {
|
|
145 |
OBJ m = __lookup(initialSearchClass, selector);
|
|
146 |
if (m != nil) RETURN ( m );
|
|
147 |
}
|
|
148 |
%}.
|
|
149 |
"
|
|
150 |
Stderr
|
|
151 |
show: 'sel='; show: selector; show: ' ns='; show: sendingNs printString;
|
|
152 |
show: ' method=', sendingMthd printString; cr.
|
|
153 |
"
|
|
154 |
|
|
155 |
sendingNs notNil ifTrue: [
|
|
156 |
|
|
157 |
seen := Set new.
|
|
158 |
namespaces := Array with: sendingNs.
|
|
159 |
|
|
160 |
[namespaces notEmpty] whileTrue:
|
|
161 |
[
|
|
162 |
methods := self
|
|
163 |
lookupMethodsForSelector: selector
|
|
164 |
directedTo: initialSearchClass
|
|
165 |
inNamespaces: namespaces.
|
|
166 |
methods size == 1 ifTrue:
|
|
167 |
[^methods anyOne].
|
|
168 |
methods size > 1 ifTrue:
|
|
169 |
[^self ambiguousMessageSend: selector
|
|
170 |
withArgs: argArrayOrNil].
|
|
171 |
"No method found"
|
|
172 |
seen addAll: namespaces.
|
|
173 |
imports := Set new.
|
|
174 |
namespaces do:
|
|
175 |
[:namespace|
|
|
176 |
namespace notNil ifTrue:
|
|
177 |
[namespace imports do:
|
|
178 |
[:import|
|
|
179 |
(seen includes: import) ifFalse:
|
|
180 |
[imports add: import]]]].
|
|
181 |
namespaces := imports].
|
|
182 |
].
|
|
183 |
|
|
184 |
methods := self lookupMethodsForSelector: selector
|
|
185 |
directedTo: initialSearchClass.
|
|
186 |
methods size == 1 ifTrue:[^methods anyOne].
|
|
187 |
|
|
188 |
^nil
|
|
189 |
|
|
190 |
"Modified: / 21-07-2010 / 17:11:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
191 |
! !
|
|
192 |
|
|
193 |
!NamespaceAwareLookup methodsFor:'lookup - helpers'!
|
|
194 |
|
|
195 |
lookupMethodsForSelector: selector directedTo: initialSearchClass
|
|
196 |
|
|
197 |
"Searches initialSearchClass for a methods with in any namespace"
|
|
198 |
^self
|
|
199 |
lookupMethodsForSelector: selector
|
|
200 |
directedTo: initialSearchClass
|
|
201 |
suchThat:[:sel :mthd|true].
|
|
202 |
|
|
203 |
"Created: / 19-07-2010 / 15:37:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
204 |
!
|
|
205 |
|
|
206 |
lookupMethodsForSelector: selector directedTo: initialSearchClass inNamespaces: namespaces
|
|
207 |
|
|
208 |
"Searches initialSearchClass for a methods with given selector in given namespaces."
|
|
209 |
^self
|
|
210 |
lookupMethodsForSelector: selector
|
|
211 |
directedTo: initialSearchClass
|
|
212 |
suchThat:[:sel :mthd|namespaces includes: mthd nameSpace].
|
|
213 |
|
|
214 |
"Created: / 19-07-2010 / 15:13:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
215 |
!
|
|
216 |
|
|
217 |
lookupMethodsForSelector: selector directedTo: initialSearchClass suchThat: block
|
|
218 |
|
|
219 |
"Searches initialSearchClass for a method with given selector in given nameSpace.
|
|
220 |
if no method in given namespace is found, returns nil"
|
|
221 |
|
|
222 |
| searchClass methods seen |
|
|
223 |
|
|
224 |
searchClass := initialSearchClass.
|
|
225 |
methods := Set new.
|
|
226 |
seen := OrderedCollection new.
|
|
227 |
[ searchClass notNil ] whileTrue:
|
|
228 |
[searchClass selectorsAndMethodsDo:
|
|
229 |
[:sel :mthd|
|
|
230 |
(sel selector = selector
|
|
231 |
and:[(seen includes: mthd nameSpace) not
|
|
232 |
and:[block value: sel value: mthd]]) ifTrue:
|
|
233 |
[methods add: mthd.
|
|
234 |
seen add: mthd nameSpace]].
|
|
235 |
searchClass := searchClass superclass].
|
|
236 |
^methods
|
|
237 |
|
|
238 |
"Created: / 19-07-2010 / 15:34:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
239 |
"Modified: / 20-07-2010 / 10:42:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
240 |
! !
|
|
241 |
|
|
242 |
!NamespaceAwareLookup methodsFor:'trampolines'!
|
|
243 |
|
|
244 |
ambiguousMessageSend
|
|
245 |
|
|
246 |
^self ambiguousMessage:
|
|
247 |
(Message
|
|
248 |
selector: #__placeholder__
|
|
249 |
arguments: #()
|
|
250 |
)
|
|
251 |
|
|
252 |
"Created: / 19-08-2010 / 22:05:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
253 |
!
|
|
254 |
|
|
255 |
ambiguousMessageSend: selector withArgs: argArrayOrNil
|
|
256 |
|
|
257 |
| trampoline |
|
|
258 |
|
|
259 |
trampoline := self class methodDictionary at:
|
|
260 |
(#(" 0"ambiguousMessageSend
|
|
261 |
" 1"ambiguousMessageSendWith:
|
|
262 |
" 2"ambiguousMessageSendWith:with:
|
|
263 |
" 3"ambiguousMessageSendWith:with:with:
|
|
264 |
" 4"ambiguousMessageSendWith:with:with:with:
|
|
265 |
" 5"ambiguousMessageSendWith:with:with:with:with:
|
|
266 |
" 6"ambiguousMessageSendWith:with:with:with:with:with:
|
|
267 |
" 7"ambiguousMessageSendWith:with:with:with:with:with:with:
|
|
268 |
" 8"ambiguousMessageSendWith:with:with:with:with:with:with:with:
|
|
269 |
)
|
|
270 |
at: argArrayOrNil size + 1).
|
|
271 |
trampoline := trampoline asByteCodeMethod.
|
|
272 |
1 to: trampoline numLiterals do:
|
|
273 |
[:litNr|
|
|
274 |
(trampoline literalAt: litNr) == #__placeholder__
|
|
275 |
ifTrue:[(trampoline literalAt: litNr put: selector)]].
|
|
276 |
^trampoline
|
|
277 |
|
|
278 |
"Created: / 19-08-2010 / 22:09:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
279 |
!
|
|
280 |
|
|
281 |
ambiguousMessageSendWith: a1
|
|
282 |
|
|
283 |
^self ambiguousMessage:
|
|
284 |
(Message
|
|
285 |
selector: #__placeholder__
|
|
286 |
arguments: (Array with: a1)
|
|
287 |
)
|
|
288 |
|
|
289 |
"Created: / 19-08-2010 / 22:06:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
290 |
!
|
|
291 |
|
|
292 |
ambiguousMessageSendWith: a1 with: a2
|
|
293 |
|
|
294 |
^self ambiguousMessage:
|
|
295 |
(Message
|
|
296 |
selector: #__placeholder__
|
|
297 |
arguments: (Array with: a1 with: a2)
|
|
298 |
)
|
|
299 |
|
|
300 |
"Created: / 19-08-2010 / 22:06:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
301 |
!
|
|
302 |
|
|
303 |
ambiguousMessageSendWith: a1 with: a2 with: a3
|
|
304 |
|
|
305 |
^self ambiguousMessage:
|
|
306 |
(Message
|
|
307 |
selector: #__placeholder__
|
|
308 |
arguments: (Array with: a1 with: a2 with: a3)
|
|
309 |
)
|
|
310 |
|
|
311 |
"Created: / 19-08-2010 / 22:06:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
312 |
!
|
|
313 |
|
|
314 |
ambiguousMessageSendWith: a1 with: a2 with: a3 with: a4
|
|
315 |
|
|
316 |
^self ambiguousMessage:
|
|
317 |
(Message
|
|
318 |
selector: #__placeholder__
|
|
319 |
arguments: (Array with: a1 with: a2 with: a3 with: a4)
|
|
320 |
)
|
|
321 |
|
|
322 |
"Created: / 19-08-2010 / 22:06:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
323 |
!
|
|
324 |
|
|
325 |
ambiguousMessageSendWith: a1 with: a2 with: a3 with: a4
|
|
326 |
with: a5
|
|
327 |
|
|
328 |
^self ambiguousMessage:
|
|
329 |
(Message
|
|
330 |
selector: #__placeholder__
|
|
331 |
arguments: (Array with: a1 with: a2 with: a3 with: a4
|
|
332 |
with: a5)
|
|
333 |
)
|
|
334 |
|
|
335 |
"Created: / 19-08-2010 / 22:07:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
336 |
!
|
|
337 |
|
|
338 |
ambiguousMessageSendWith: a1 with: a2 with: a3 with: a4
|
|
339 |
with: a5 with: a6
|
|
340 |
|
|
341 |
^self ambiguousMessage:
|
|
342 |
(Message
|
|
343 |
selector: #__placeholder__
|
|
344 |
arguments: (Array with: a1 with: a2 with: a3 with: a4
|
|
345 |
with: a5 with: a6)
|
|
346 |
)
|
|
347 |
|
|
348 |
"Created: / 19-08-2010 / 22:07:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
349 |
!
|
|
350 |
|
|
351 |
ambiguousMessageSendWith: a1 with: a2 with: a3 with: a4
|
|
352 |
with: a5 with: a6 with: a7
|
|
353 |
|
|
354 |
^self ambiguousMessage:
|
|
355 |
(Message
|
|
356 |
selector: #__placeholder__
|
|
357 |
arguments: (Array with: a1 with: a2 with: a3 with: a4
|
|
358 |
with: a5 with: a6 with: a7)
|
|
359 |
)
|
|
360 |
|
|
361 |
"Created: / 19-08-2010 / 22:07:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
362 |
!
|
|
363 |
|
|
364 |
ambiguousMessageSendWith: a1 with: a2 with: a3 with: a4
|
|
365 |
with: a5 with: a6 with: a7 with: a8
|
|
366 |
|
|
367 |
^self ambiguousMessage:
|
|
368 |
(Message
|
|
369 |
selector: #__placeholder__
|
|
370 |
arguments: (Array with: a1 with: a2 with: a3 with: a4
|
|
371 |
with: a5 with: a6 with: a7 with: a8)
|
|
372 |
)
|
|
373 |
|
|
374 |
"Created: / 19-08-2010 / 22:08:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
375 |
! !
|
|
376 |
|
|
377 |
!NamespaceAwareLookup class methodsFor:'documentation'!
|
|
378 |
|
13478
|
379 |
version_CVS
|
|
380 |
^ '$Header: /cvs/stx/stx/libbasic/NamespaceAwareLookup.st,v 1.2 2011-07-03 15:00:20 cg Exp $'
|
|
381 |
!
|
|
382 |
|
13403
|
383 |
version_SVN
|
13478
|
384 |
^ '§Id: NamespaceAwareLookup.st,v 1.1 2011/06/28 11:04:04 vrany Exp §'
|
13403
|
385 |
! !
|
|
386 |
|
|
387 |
NamespaceAwareLookup initialize!
|