author | Claus Gittinger <cg@exept.de> |
Wed, 26 Jun 2019 22:06:12 +0200 | |
changeset 24369 | e796791ea838 |
parent 23386 | e7d124bf6282 |
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; |
22310 | 100 |
if (__Class(sendingMthd) == Method && __MethodInstPtr(sendingMthd)->m_annotation == nil) { |
101 |
OBJ m = __lookup(initialSearchClass, selector); |
|
102 |
if (m != nil) RETURN ( m ); |
|
18359 | 103 |
} |
13403 | 104 |
%}. |
105 |
||
106 |
^Instance lookupMethodForSelector: selector directedTo: initialSearchClass |
|
21100 | 107 |
for: receiver withArguments: argArrayOrNil |
108 |
from: sendingContext |
|
22310 | 109 |
! |
110 |
||
111 |
lookupMethodForSelector: selector directedTo: initialSearchClass for: receiver withArguments: argArrayOrNil from: sendingContext ilc:ilc |
|
112 |
||
113 |
"JV @ 2010-07-24 |
|
114 |
Following C code is just a performance optimization. |
|
115 |
It is not necessary, however it speeds things in most |
|
116 |
cases. Such optimization significantly speeds up the IDE |
|
117 |
since class browser involves dozens of super-polymorphic |
|
118 |
sends (> 1000 receiver classes per send-site). |
|
119 |
" |
|
120 |
||
121 |
%{ |
|
122 |
OBJ sendingMthd = __ContextInstPtr(sendingContext)->c_method; |
|
123 |
if (__Class(sendingMthd) == Method && __MethodInstPtr(sendingMthd)->m_annotation == nil) { |
|
124 |
OBJ m = __lookup(initialSearchClass, selector); |
|
125 |
if (m != nil) RETURN ( m ); |
|
126 |
} |
|
127 |
%}. |
|
128 |
||
129 |
^Instance lookupMethodForSelector: selector directedTo: initialSearchClass |
|
130 |
for: receiver withArguments: argArrayOrNil |
|
131 |
from: sendingContext ilc:ilc |
|
13403 | 132 |
! ! |
133 |
||
134 |
!NamespaceAwareLookup methodsFor:'lookup'! |
|
135 |
||
18359 | 136 |
lookupMethodForSelector: selector directedTo: initialSearchClass for: receiver withArguments: argArrayOrNil from: sendingContext ilc: ilcCache |
13403 | 137 |
"Invoked by the VM to ask me for a method to fire. |
138 |
For details, see comment inLookup>>lookupMethodForSelector:directedTo:for:withArguments:from:" |
|
139 |
||
18359 | 140 |
| sendingNs sendingMthd queue seen namespaces methods imports numMethods| |
13403 | 141 |
|
142 |
"JV @ 2010-07-24 |
|
143 |
Following C code is just a performance optimization. |
|
21100 | 144 |
It is not necessary, however it speeds things in most |
13403 | 145 |
cases. Such optimization significantly speeds up the IDE |
146 |
since class browser involves dozens of super-polymorphic |
|
18359 | 147 |
sends (> 1000 receiver classes per send-site). |
148 |
" |
|
149 |
%{ |
|
13403 | 150 |
sendingMthd = __ContextInstPtr(sendingContext)->c_method; |
18359 | 151 |
if (__Class(sendingMthd) == Method && |
21100 | 152 |
__MethodInstPtr(sendingMthd)->m_annotation == nil) { |
153 |
OBJ m = __lookup(initialSearchClass, selector); |
|
154 |
if (m != nil) { |
|
155 |
if (ilcCache != nil) __ilcBind(ilcCache, initialSearchClass, m, selector); |
|
156 |
RETURN ( m ); |
|
157 |
} |
|
18359 | 158 |
} |
13403 | 159 |
%}. |
160 |
"If you remove C code above, uncomment the line below." |
|
161 |
"sendingMthd := sendingContext method." |
|
162 |
sendingNs := sendingMthd isNil |
|
21100 | 163 |
ifTrue:[nil] |
164 |
ifFalse:[sendingMthd nameSpace]. |
|
13403 | 165 |
|
21100 | 166 |
"Second chance to speed up things (in case sending method |
167 |
has resource or so)" |
|
13403 | 168 |
%{ |
16080
e12fe7f621a8
class: NamespaceAwareLookup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13478
diff
changeset
|
169 |
if (sendingNs == nil) { |
21100 | 170 |
OBJ m = __lookup(initialSearchClass, selector); |
171 |
if (m != nil) { |
|
172 |
if (ilcCache != nil) __ilcBind(ilcCache, initialSearchClass, m, selector); |
|
173 |
RETURN ( m ); |
|
174 |
} |
|
13403 | 175 |
} |
176 |
%}. |
|
177 |
" |
|
178 |
Stderr |
|
21100 | 179 |
show: 'sel='; show: selector; show: ' ns='; show: sendingNs printString; |
180 |
show: ' method=', sendingMthd printString; cr. |
|
181 |
" |
|
18359 | 182 |
|
13403 | 183 |
sendingNs notNil ifTrue: [ |
21100 | 184 |
seen := Set new. |
185 |
namespaces := Array with: sendingNs. |
|
13403 | 186 |
|
21100 | 187 |
[namespaces notEmpty] whileTrue:[ |
188 |
methods := self |
|
189 |
lookupMethodsForSelector: selector |
|
190 |
directedTo: initialSearchClass |
|
191 |
inNamespaces: namespaces. |
|
192 |
numMethods := methods size. |
|
193 |
numMethods == 1 ifTrue:[ |
|
22296 | 194 |
^ methods anElement |
21100 | 195 |
]. |
196 |
numMethods > 1 ifTrue:[ |
|
197 |
^self ambiguousMessageSend: selector withArgs: argArrayOrNil |
|
198 |
]. |
|
199 |
"No method found" |
|
200 |
seen addAll: namespaces. |
|
201 |
imports := Set new. |
|
202 |
namespaces do:[:namespace| |
|
203 |
namespace notNil ifTrue:[ |
|
204 |
namespace imports do:[:import| |
|
205 |
(seen includes: import) ifFalse: [ |
|
206 |
imports add: import |
|
207 |
] |
|
208 |
] |
|
209 |
] |
|
210 |
]. |
|
211 |
namespaces := imports |
|
212 |
]. |
|
18359 | 213 |
]. |
214 |
||
215 |
methods := self lookupMethodsForSelector: selector |
|
21100 | 216 |
directedTo: initialSearchClass. |
16080
e12fe7f621a8
class: NamespaceAwareLookup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13478
diff
changeset
|
217 |
methods size == 1 ifTrue:[ |
21100 | 218 |
| m | |
16080
e12fe7f621a8
class: NamespaceAwareLookup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13478
diff
changeset
|
219 |
|
21100 | 220 |
m := methods anyOne. |
221 |
ilcCache notNil ifTrue:[ ilcCache bindTo: m forClass: initialSearchClass ]. |
|
222 |
^ m |
|
16080
e12fe7f621a8
class: NamespaceAwareLookup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13478
diff
changeset
|
223 |
]. |
18359 | 224 |
|
13403 | 225 |
^nil |
226 |
||
16080
e12fe7f621a8
class: NamespaceAwareLookup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13478
diff
changeset
|
227 |
"Created: / 19-02-2014 / 21:49:59 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
22296 | 228 |
"Modified: / 09-10-2017 / 17:19:51 / stefan" |
13403 | 229 |
! ! |
230 |
||
231 |
!NamespaceAwareLookup methodsFor:'lookup - helpers'! |
|
232 |
||
18359 | 233 |
lookupMethodsForSelector: selector directedTo: initialSearchClass |
13403 | 234 |
"Searches initialSearchClass for a methods with in any namespace" |
18359 | 235 |
|
236 |
^self |
|
237 |
lookupMethodsForSelector: selector |
|
238 |
directedTo: initialSearchClass |
|
239 |
suchThat:[:sel :mthd|true]. |
|
13403 | 240 |
|
241 |
"Created: / 19-07-2010 / 15:37:06 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
242 |
! |
|
243 |
||
244 |
lookupMethodsForSelector: selector directedTo: initialSearchClass inNamespaces: namespaces |
|
18359 | 245 |
"Searches initialSearchClass for a methods with given selector in given namespaces." |
13403 | 246 |
|
18359 | 247 |
^self |
248 |
lookupMethodsForSelector: selector |
|
249 |
directedTo: initialSearchClass |
|
250 |
suchThat:[:sel :mthd|namespaces includes: mthd nameSpace]. |
|
13403 | 251 |
|
252 |
"Created: / 19-07-2010 / 15:13:59 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
253 |
! |
|
254 |
||
255 |
lookupMethodsForSelector: selector directedTo: initialSearchClass suchThat: block |
|
18359 | 256 |
"Searches initialSearchClass for a method with given selector in given nameSpace. |
13403 | 257 |
|
258 |
if no method in given namespace is found, returns nil" |
|
259 |
||
260 |
| searchClass methods seen | |
|
261 |
||
262 |
searchClass := initialSearchClass. |
|
263 |
methods := Set new. |
|
264 |
seen := OrderedCollection new. |
|
18359 | 265 |
[ searchClass notNil ] whileTrue:[ |
23386 | 266 |
searchClass selectorsAndMethodsDo:[:sel :mthd| |
267 |
(sel selectorWithoutNameSpace = selector |
|
268 |
and:[ (seen includes: mthd nameSpace) not |
|
269 |
and:[ block value: sel value: mthd]] |
|
270 |
) ifTrue:[ |
|
271 |
methods add: mthd. |
|
272 |
seen add: mthd nameSpace |
|
273 |
] |
|
274 |
]. |
|
275 |
searchClass := searchClass superclass |
|
18359 | 276 |
]. |
13403 | 277 |
^methods |
278 |
||
279 |
"Created: / 19-07-2010 / 15:34:10 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
280 |
"Modified: / 20-07-2010 / 10:42:52 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
23386 | 281 |
"Modified: / 24-09-2018 / 21:17:59 / Claus Gittinger" |
13403 | 282 |
! ! |
283 |
||
284 |
!NamespaceAwareLookup methodsFor:'trampolines'! |
|
285 |
||
286 |
ambiguousMessageSend |
|
287 |
||
288 |
^self ambiguousMessage: |
|
18359 | 289 |
(Message |
290 |
selector: #__placeholder__ |
|
291 |
arguments: #() |
|
292 |
) |
|
13403 | 293 |
|
294 |
"Created: / 19-08-2010 / 22:05:48 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
295 |
! |
|
296 |
||
297 |
ambiguousMessageSend: selector withArgs: argArrayOrNil |
|
298 |
||
299 |
| trampoline | |
|
300 |
||
301 |
trampoline := self class methodDictionary at: |
|
18359 | 302 |
(#(" 0"ambiguousMessageSend |
303 |
" 1"ambiguousMessageSendWith: |
|
304 |
" 2"ambiguousMessageSendWith:with: |
|
305 |
" 3"ambiguousMessageSendWith:with:with: |
|
306 |
" 4"ambiguousMessageSendWith:with:with:with: |
|
307 |
" 5"ambiguousMessageSendWith:with:with:with:with: |
|
308 |
" 6"ambiguousMessageSendWith:with:with:with:with:with: |
|
309 |
" 7"ambiguousMessageSendWith:with:with:with:with:with:with: |
|
310 |
" 8"ambiguousMessageSendWith:with:with:with:with:with:with:with: |
|
311 |
) |
|
312 |
at: argArrayOrNil size + 1). |
|
13403 | 313 |
trampoline := trampoline asByteCodeMethod. |
18359 | 314 |
1 to: trampoline numLiterals do: [:litNr| |
315 |
(trampoline literalAt: litNr) == #__placeholder__ ifTrue:[ |
|
316 |
(trampoline literalAt: litNr put: selector) |
|
317 |
] |
|
318 |
]. |
|
13403 | 319 |
^trampoline |
320 |
||
321 |
"Created: / 19-08-2010 / 22:09:24 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
322 |
! |
|
323 |
||
324 |
ambiguousMessageSendWith: a1 |
|
325 |
||
326 |
^self ambiguousMessage: |
|
18359 | 327 |
(Message |
328 |
selector: #__placeholder__ |
|
329 |
arguments: (Array with: a1) |
|
330 |
) |
|
13403 | 331 |
|
332 |
"Created: / 19-08-2010 / 22:06:08 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
333 |
! |
|
334 |
||
335 |
ambiguousMessageSendWith: a1 with: a2 |
|
336 |
||
337 |
^self ambiguousMessage: |
|
18359 | 338 |
(Message |
339 |
selector: #__placeholder__ |
|
340 |
arguments: (Array with: a1 with: a2) |
|
341 |
) |
|
13403 | 342 |
|
343 |
"Created: / 19-08-2010 / 22:06:37 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
344 |
! |
|
345 |
||
346 |
ambiguousMessageSendWith: a1 with: a2 with: a3 |
|
347 |
||
348 |
^self ambiguousMessage: |
|
18359 | 349 |
(Message |
350 |
selector: #__placeholder__ |
|
351 |
arguments: (Array with: a1 with: a2 with: a3) |
|
352 |
) |
|
13403 | 353 |
|
354 |
"Created: / 19-08-2010 / 22:06:48 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
355 |
! |
|
356 |
||
357 |
ambiguousMessageSendWith: a1 with: a2 with: a3 with: a4 |
|
358 |
||
359 |
^self ambiguousMessage: |
|
18359 | 360 |
(Message |
361 |
selector: #__placeholder__ |
|
362 |
arguments: (Array with: a1 with: a2 with: a3 with: a4) |
|
363 |
) |
|
13403 | 364 |
|
365 |
"Created: / 19-08-2010 / 22:06:56 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
366 |
! |
|
367 |
||
368 |
ambiguousMessageSendWith: a1 with: a2 with: a3 with: a4 |
|
18359 | 369 |
with: a5 |
13403 | 370 |
|
371 |
^self ambiguousMessage: |
|
18359 | 372 |
(Message |
373 |
selector: #__placeholder__ |
|
374 |
arguments: (Array with: a1 with: a2 with: a3 with: a4 |
|
375 |
with: a5) |
|
376 |
) |
|
13403 | 377 |
|
378 |
"Created: / 19-08-2010 / 22:07:15 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
379 |
! |
|
380 |
||
381 |
ambiguousMessageSendWith: a1 with: a2 with: a3 with: a4 |
|
18359 | 382 |
with: a5 with: a6 |
13403 | 383 |
|
384 |
^self ambiguousMessage: |
|
18359 | 385 |
(Message |
386 |
selector: #__placeholder__ |
|
387 |
arguments: (Array with: a1 with: a2 with: a3 with: a4 |
|
388 |
with: a5 with: a6) |
|
389 |
) |
|
13403 | 390 |
|
391 |
"Created: / 19-08-2010 / 22:07:23 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
392 |
! |
|
393 |
||
394 |
ambiguousMessageSendWith: a1 with: a2 with: a3 with: a4 |
|
18359 | 395 |
with: a5 with: a6 with: a7 |
13403 | 396 |
|
397 |
^self ambiguousMessage: |
|
18359 | 398 |
(Message |
399 |
selector: #__placeholder__ |
|
400 |
arguments: (Array with: a1 with: a2 with: a3 with: a4 |
|
401 |
with: a5 with: a6 with: a7) |
|
402 |
) |
|
13403 | 403 |
|
404 |
"Created: / 19-08-2010 / 22:07:37 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
405 |
! |
|
406 |
||
407 |
ambiguousMessageSendWith: a1 with: a2 with: a3 with: a4 |
|
18359 | 408 |
with: a5 with: a6 with: a7 with: a8 |
13403 | 409 |
|
410 |
^self ambiguousMessage: |
|
18359 | 411 |
(Message |
412 |
selector: #__placeholder__ |
|
413 |
arguments: (Array with: a1 with: a2 with: a3 with: a4 |
|
414 |
with: a5 with: a6 with: a7 with: a8) |
|
415 |
) |
|
13403 | 416 |
|
417 |
"Created: / 19-08-2010 / 22:08:03 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
418 |
! ! |
|
419 |
||
420 |
!NamespaceAwareLookup class methodsFor:'documentation'! |
|
421 |
||
13478 | 422 |
version_CVS |
21100 | 423 |
^ '$Header$' |
13478 | 424 |
! |
425 |
||
13403 | 426 |
version_SVN |
21100 | 427 |
^ '$Id$' |
13403 | 428 |
! ! |
429 |
||
16080
e12fe7f621a8
class: NamespaceAwareLookup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13478
diff
changeset
|
430 |
|
13403 | 431 |
NamespaceAwareLookup initialize! |