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