author | Stefan Vogel <sv@exept.de> |
Fri, 27 Oct 2017 16:14:37 +0200 | |
branch | expecco_2_11_1_branch |
changeset 22329 | 20662662693b |
parent 18359 | b7bce2595753 |
child 18366 | a6e62e167c32 |
child 21100 | 6cd737df7462 |
permissions | -rw-r--r-- |
13403 | 1 |
" |
13478 | 2 |
COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague |
18359 | 3 |
All Rights Reserved |
13403 | 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 |
18359 | 40 |
All Rights Reserved |
13403 | 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 |
||
18359 | 86 |
lookupMethodForSelector: selector directedTo: initialSearchClass for: receiver withArguments: argArrayOrNil from: sendingContext |
13403 | 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 |
|
18359 | 93 |
sends (> 1000 receiver classes per send-site). |
13403 | 94 |
" |
95 |
||
18359 | 96 |
%{ |
13403 | 97 |
OBJ sendingMthd = __ContextInstPtr(sendingContext)->c_method; |
18359 | 98 |
if (__Class(sendingMthd) == Method && |
99 |
__MethodInstPtr(sendingMthd)->m_annotation == nil) { |
|
100 |
OBJ m = __lookup(initialSearchClass, selector); |
|
101 |
if (m != nil) RETURN ( m ); |
|
102 |
} |
|
13403 | 103 |
%}. |
104 |
||
105 |
^Instance lookupMethodForSelector: selector directedTo: initialSearchClass |
|
18359 | 106 |
for: receiver withArguments: argArrayOrNil |
107 |
from: sendingContext |
|
13403 | 108 |
|
109 |
! ! |
|
110 |
||
111 |
!NamespaceAwareLookup methodsFor:'lookup'! |
|
112 |
||
18359 | 113 |
lookupMethodForSelector: selector directedTo: initialSearchClass for: receiver withArguments: argArrayOrNil from: sendingContext ilc: ilcCache |
13403 | 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 |
||
18359 | 117 |
| sendingNs sendingMthd queue seen namespaces methods imports numMethods| |
13403 | 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 |
|
18359 | 124 |
sends (> 1000 receiver classes per send-site). |
125 |
" |
|
126 |
%{ |
|
13403 | 127 |
sendingMthd = __ContextInstPtr(sendingContext)->c_method; |
18359 | 128 |
if (__Class(sendingMthd) == Method && |
129 |
__MethodInstPtr(sendingMthd)->m_annotation == nil) { |
|
130 |
OBJ m = __lookup(initialSearchClass, selector); |
|
131 |
if (m != nil) { |
|
132 |
if (ilcCache != nil) __ilcBind(ilcCache, initialSearchClass, m, selector); |
|
133 |
RETURN ( m ); |
|
134 |
} |
|
135 |
} |
|
13403 | 136 |
%}. |
137 |
"If you remove C code above, uncomment the line below." |
|
138 |
"sendingMthd := sendingContext method." |
|
139 |
sendingNs := sendingMthd isNil |
|
18359 | 140 |
ifTrue:[nil] |
141 |
ifFalse:[sendingMthd nameSpace]. |
|
13403 | 142 |
|
18359 | 143 |
"Second chance to speed up things (in case sending method |
144 |
has resource or so)" |
|
13403 | 145 |
%{ |
16080
e12fe7f621a8
class: NamespaceAwareLookup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13478
diff
changeset
|
146 |
if (sendingNs == nil) { |
18359 | 147 |
OBJ m = __lookup(initialSearchClass, selector); |
148 |
if (m != nil) { |
|
149 |
if (ilcCache != nil) __ilcBind(ilcCache, initialSearchClass, m, selector); |
|
150 |
RETURN ( m ); |
|
151 |
} |
|
13403 | 152 |
} |
153 |
%}. |
|
154 |
" |
|
155 |
Stderr |
|
18359 | 156 |
show: 'sel='; show: selector; show: ' ns='; show: sendingNs printString; |
157 |
show: ' method=', sendingMthd printString; cr. |
|
158 |
" |
|
159 |
||
13403 | 160 |
sendingNs notNil ifTrue: [ |
18359 | 161 |
seen := Set new. |
162 |
namespaces := Array with: sendingNs. |
|
13403 | 163 |
|
18359 | 164 |
[namespaces notEmpty] whileTrue:[ |
165 |
methods := self |
|
166 |
lookupMethodsForSelector: selector |
|
167 |
directedTo: initialSearchClass |
|
168 |
inNamespaces: namespaces. |
|
169 |
numMethods := methods size. |
|
170 |
numMethods == 1 ifTrue:[ |
|
171 |
^ methods anyOne |
|
172 |
]. |
|
173 |
numMethods > 1 ifTrue:[ |
|
174 |
^self ambiguousMessageSend: selector withArgs: argArrayOrNil |
|
175 |
]. |
|
176 |
"No method found" |
|
177 |
seen addAll: namespaces. |
|
178 |
imports := Set new. |
|
179 |
namespaces do:[:namespace| |
|
180 |
namespace notNil ifTrue:[ |
|
181 |
namespace imports do:[:import| |
|
182 |
(seen includes: import) ifFalse: [ |
|
183 |
imports add: import |
|
184 |
] |
|
185 |
] |
|
186 |
] |
|
187 |
]. |
|
188 |
namespaces := imports |
|
189 |
]. |
|
190 |
]. |
|
191 |
||
192 |
methods := self lookupMethodsForSelector: selector |
|
193 |
directedTo: initialSearchClass. |
|
16080
e12fe7f621a8
class: NamespaceAwareLookup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13478
diff
changeset
|
194 |
methods size == 1 ifTrue:[ |
18359 | 195 |
| m | |
16080
e12fe7f621a8
class: NamespaceAwareLookup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13478
diff
changeset
|
196 |
|
18359 | 197 |
m := methods anyOne. |
198 |
ilcCache notNil ifTrue:[ ilcCache bindTo: m forClass: initialSearchClass ]. |
|
199 |
^ m |
|
16080
e12fe7f621a8
class: NamespaceAwareLookup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13478
diff
changeset
|
200 |
]. |
18359 | 201 |
|
13403 | 202 |
^nil |
203 |
||
16080
e12fe7f621a8
class: NamespaceAwareLookup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13478
diff
changeset
|
204 |
"Created: / 19-02-2014 / 21:49:59 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
13403 | 205 |
! ! |
206 |
||
207 |
!NamespaceAwareLookup methodsFor:'lookup - helpers'! |
|
208 |
||
18359 | 209 |
lookupMethodsForSelector: selector directedTo: initialSearchClass |
13403 | 210 |
"Searches initialSearchClass for a methods with in any namespace" |
18359 | 211 |
|
212 |
^self |
|
213 |
lookupMethodsForSelector: selector |
|
214 |
directedTo: initialSearchClass |
|
215 |
suchThat:[:sel :mthd|true]. |
|
13403 | 216 |
|
217 |
"Created: / 19-07-2010 / 15:37:06 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
218 |
! |
|
219 |
||
220 |
lookupMethodsForSelector: selector directedTo: initialSearchClass inNamespaces: namespaces |
|
18359 | 221 |
"Searches initialSearchClass for a methods with given selector in given namespaces." |
13403 | 222 |
|
18359 | 223 |
^self |
224 |
lookupMethodsForSelector: selector |
|
225 |
directedTo: initialSearchClass |
|
226 |
suchThat:[:sel :mthd|namespaces includes: mthd nameSpace]. |
|
13403 | 227 |
|
228 |
"Created: / 19-07-2010 / 15:13:59 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
229 |
! |
|
230 |
||
231 |
lookupMethodsForSelector: selector directedTo: initialSearchClass suchThat: block |
|
18359 | 232 |
"Searches initialSearchClass for a method with given selector in given nameSpace. |
13403 | 233 |
|
234 |
if no method in given namespace is found, returns nil" |
|
235 |
||
236 |
| searchClass methods seen | |
|
237 |
||
238 |
searchClass := initialSearchClass. |
|
239 |
methods := Set new. |
|
240 |
seen := OrderedCollection new. |
|
18359 | 241 |
[ searchClass notNil ] whileTrue:[ |
242 |
searchClass selectorsAndMethodsDo:[:sel :mthd| |
|
243 |
(sel selector = selector |
|
244 |
and:[ (seen includes: mthd nameSpace) not |
|
245 |
and:[ block value: sel value: mthd]] |
|
246 |
) ifTrue:[ |
|
247 |
methods add: mthd. |
|
248 |
seen add: mthd nameSpace |
|
249 |
] |
|
250 |
]. |
|
251 |
searchClass := searchClass superclass |
|
252 |
]. |
|
13403 | 253 |
^methods |
254 |
||
255 |
"Created: / 19-07-2010 / 15:34:10 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
256 |
"Modified: / 20-07-2010 / 10:42:52 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
257 |
! ! |
|
258 |
||
259 |
!NamespaceAwareLookup methodsFor:'trampolines'! |
|
260 |
||
261 |
ambiguousMessageSend |
|
262 |
||
263 |
^self ambiguousMessage: |
|
18359 | 264 |
(Message |
265 |
selector: #__placeholder__ |
|
266 |
arguments: #() |
|
267 |
) |
|
13403 | 268 |
|
269 |
"Created: / 19-08-2010 / 22:05:48 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
270 |
! |
|
271 |
||
272 |
ambiguousMessageSend: selector withArgs: argArrayOrNil |
|
273 |
||
274 |
| trampoline | |
|
275 |
||
276 |
trampoline := self class methodDictionary at: |
|
18359 | 277 |
(#(" 0"ambiguousMessageSend |
278 |
" 1"ambiguousMessageSendWith: |
|
279 |
" 2"ambiguousMessageSendWith:with: |
|
280 |
" 3"ambiguousMessageSendWith:with:with: |
|
281 |
" 4"ambiguousMessageSendWith:with:with:with: |
|
282 |
" 5"ambiguousMessageSendWith:with:with:with:with: |
|
283 |
" 6"ambiguousMessageSendWith:with:with:with:with:with: |
|
284 |
" 7"ambiguousMessageSendWith:with:with:with:with:with:with: |
|
285 |
" 8"ambiguousMessageSendWith:with:with:with:with:with:with:with: |
|
286 |
) |
|
287 |
at: argArrayOrNil size + 1). |
|
13403 | 288 |
trampoline := trampoline asByteCodeMethod. |
18359 | 289 |
1 to: trampoline numLiterals do: [:litNr| |
290 |
(trampoline literalAt: litNr) == #__placeholder__ ifTrue:[ |
|
291 |
(trampoline literalAt: litNr put: selector) |
|
292 |
] |
|
293 |
]. |
|
13403 | 294 |
^trampoline |
295 |
||
296 |
"Created: / 19-08-2010 / 22:09:24 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
297 |
! |
|
298 |
||
299 |
ambiguousMessageSendWith: a1 |
|
300 |
||
301 |
^self ambiguousMessage: |
|
18359 | 302 |
(Message |
303 |
selector: #__placeholder__ |
|
304 |
arguments: (Array with: a1) |
|
305 |
) |
|
13403 | 306 |
|
307 |
"Created: / 19-08-2010 / 22:06:08 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
308 |
! |
|
309 |
||
310 |
ambiguousMessageSendWith: a1 with: a2 |
|
311 |
||
312 |
^self ambiguousMessage: |
|
18359 | 313 |
(Message |
314 |
selector: #__placeholder__ |
|
315 |
arguments: (Array with: a1 with: a2) |
|
316 |
) |
|
13403 | 317 |
|
318 |
"Created: / 19-08-2010 / 22:06:37 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
319 |
! |
|
320 |
||
321 |
ambiguousMessageSendWith: a1 with: a2 with: a3 |
|
322 |
||
323 |
^self ambiguousMessage: |
|
18359 | 324 |
(Message |
325 |
selector: #__placeholder__ |
|
326 |
arguments: (Array with: a1 with: a2 with: a3) |
|
327 |
) |
|
13403 | 328 |
|
329 |
"Created: / 19-08-2010 / 22:06:48 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
330 |
! |
|
331 |
||
332 |
ambiguousMessageSendWith: a1 with: a2 with: a3 with: a4 |
|
333 |
||
334 |
^self ambiguousMessage: |
|
18359 | 335 |
(Message |
336 |
selector: #__placeholder__ |
|
337 |
arguments: (Array with: a1 with: a2 with: a3 with: a4) |
|
338 |
) |
|
13403 | 339 |
|
340 |
"Created: / 19-08-2010 / 22:06:56 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
341 |
! |
|
342 |
||
343 |
ambiguousMessageSendWith: a1 with: a2 with: a3 with: a4 |
|
18359 | 344 |
with: a5 |
13403 | 345 |
|
346 |
^self ambiguousMessage: |
|
18359 | 347 |
(Message |
348 |
selector: #__placeholder__ |
|
349 |
arguments: (Array with: a1 with: a2 with: a3 with: a4 |
|
350 |
with: a5) |
|
351 |
) |
|
13403 | 352 |
|
353 |
"Created: / 19-08-2010 / 22:07:15 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
354 |
! |
|
355 |
||
356 |
ambiguousMessageSendWith: a1 with: a2 with: a3 with: a4 |
|
18359 | 357 |
with: a5 with: a6 |
13403 | 358 |
|
359 |
^self ambiguousMessage: |
|
18359 | 360 |
(Message |
361 |
selector: #__placeholder__ |
|
362 |
arguments: (Array with: a1 with: a2 with: a3 with: a4 |
|
363 |
with: a5 with: a6) |
|
364 |
) |
|
13403 | 365 |
|
366 |
"Created: / 19-08-2010 / 22:07:23 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
367 |
! |
|
368 |
||
369 |
ambiguousMessageSendWith: a1 with: a2 with: a3 with: a4 |
|
18359 | 370 |
with: a5 with: a6 with: a7 |
13403 | 371 |
|
372 |
^self ambiguousMessage: |
|
18359 | 373 |
(Message |
374 |
selector: #__placeholder__ |
|
375 |
arguments: (Array with: a1 with: a2 with: a3 with: a4 |
|
376 |
with: a5 with: a6 with: a7) |
|
377 |
) |
|
13403 | 378 |
|
379 |
"Created: / 19-08-2010 / 22:07:37 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
380 |
! |
|
381 |
||
382 |
ambiguousMessageSendWith: a1 with: a2 with: a3 with: a4 |
|
18359 | 383 |
with: a5 with: a6 with: a7 with: a8 |
13403 | 384 |
|
385 |
^self ambiguousMessage: |
|
18359 | 386 |
(Message |
387 |
selector: #__placeholder__ |
|
388 |
arguments: (Array with: a1 with: a2 with: a3 with: a4 |
|
389 |
with: a5 with: a6 with: a7 with: a8) |
|
390 |
) |
|
13403 | 391 |
|
392 |
"Created: / 19-08-2010 / 22:08:03 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
393 |
! ! |
|
394 |
||
395 |
!NamespaceAwareLookup class methodsFor:'documentation'! |
|
396 |
||
13478 | 397 |
version_CVS |
18359 | 398 |
^ '$Header: /cvs/stx/stx/libbasic/NamespaceAwareLookup.st,v 1.4 2015-05-18 00:05:38 cg Exp $' |
13478 | 399 |
! |
400 |
||
13403 | 401 |
version_SVN |
18359 | 402 |
^ '$Id: NamespaceAwareLookup.st,v 1.4 2015-05-18 00:05:38 cg Exp $' |
13403 | 403 |
! ! |
404 |
||
16080
e12fe7f621a8
class: NamespaceAwareLookup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13478
diff
changeset
|
405 |
|
13403 | 406 |
NamespaceAwareLookup initialize! |