author | Stefan Vogel <sv@exept.de> |
Mon, 09 Oct 2017 17:48:33 +0200 | |
changeset 22296 | 5abec1c29d53 |
parent 21100 | 6cd737df7462 |
child 22310 | 7c2de8ff3d04 |
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:[ |
|
22296 | 172 |
^ methods anElement |
21100 | 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>" |
22296 | 206 |
"Modified: / 09-10-2017 / 17:19:51 / stefan" |
13403 | 207 |
! ! |
208 |
||
209 |
!NamespaceAwareLookup methodsFor:'lookup - helpers'! |
|
210 |
||
18359 | 211 |
lookupMethodsForSelector: selector directedTo: initialSearchClass |
13403 | 212 |
"Searches initialSearchClass for a methods with in any namespace" |
18359 | 213 |
|
214 |
^self |
|
215 |
lookupMethodsForSelector: selector |
|
216 |
directedTo: initialSearchClass |
|
217 |
suchThat:[:sel :mthd|true]. |
|
13403 | 218 |
|
219 |
"Created: / 19-07-2010 / 15:37:06 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
220 |
! |
|
221 |
||
222 |
lookupMethodsForSelector: selector directedTo: initialSearchClass inNamespaces: namespaces |
|
18359 | 223 |
"Searches initialSearchClass for a methods with given selector in given namespaces." |
13403 | 224 |
|
18359 | 225 |
^self |
226 |
lookupMethodsForSelector: selector |
|
227 |
directedTo: initialSearchClass |
|
228 |
suchThat:[:sel :mthd|namespaces includes: mthd nameSpace]. |
|
13403 | 229 |
|
230 |
"Created: / 19-07-2010 / 15:13:59 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
231 |
! |
|
232 |
||
233 |
lookupMethodsForSelector: selector directedTo: initialSearchClass suchThat: block |
|
18359 | 234 |
"Searches initialSearchClass for a method with given selector in given nameSpace. |
13403 | 235 |
|
236 |
if no method in given namespace is found, returns nil" |
|
237 |
||
238 |
| searchClass methods seen | |
|
239 |
||
240 |
searchClass := initialSearchClass. |
|
241 |
methods := Set new. |
|
242 |
seen := OrderedCollection new. |
|
18359 | 243 |
[ searchClass notNil ] whileTrue:[ |
244 |
searchClass selectorsAndMethodsDo:[:sel :mthd| |
|
245 |
(sel selector = selector |
|
246 |
and:[ (seen includes: mthd nameSpace) not |
|
247 |
and:[ block value: sel value: mthd]] |
|
248 |
) ifTrue:[ |
|
249 |
methods add: mthd. |
|
250 |
seen add: mthd nameSpace |
|
251 |
] |
|
252 |
]. |
|
253 |
searchClass := searchClass superclass |
|
254 |
]. |
|
13403 | 255 |
^methods |
256 |
||
257 |
"Created: / 19-07-2010 / 15:34:10 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
258 |
"Modified: / 20-07-2010 / 10:42:52 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
259 |
! ! |
|
260 |
||
261 |
!NamespaceAwareLookup methodsFor:'trampolines'! |
|
262 |
||
263 |
ambiguousMessageSend |
|
264 |
||
265 |
^self ambiguousMessage: |
|
18359 | 266 |
(Message |
267 |
selector: #__placeholder__ |
|
268 |
arguments: #() |
|
269 |
) |
|
13403 | 270 |
|
271 |
"Created: / 19-08-2010 / 22:05:48 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
272 |
! |
|
273 |
||
274 |
ambiguousMessageSend: selector withArgs: argArrayOrNil |
|
275 |
||
276 |
| trampoline | |
|
277 |
||
278 |
trampoline := self class methodDictionary at: |
|
18359 | 279 |
(#(" 0"ambiguousMessageSend |
280 |
" 1"ambiguousMessageSendWith: |
|
281 |
" 2"ambiguousMessageSendWith:with: |
|
282 |
" 3"ambiguousMessageSendWith:with:with: |
|
283 |
" 4"ambiguousMessageSendWith:with:with:with: |
|
284 |
" 5"ambiguousMessageSendWith:with:with:with:with: |
|
285 |
" 6"ambiguousMessageSendWith:with:with:with:with:with: |
|
286 |
" 7"ambiguousMessageSendWith:with:with:with:with:with:with: |
|
287 |
" 8"ambiguousMessageSendWith:with:with:with:with:with:with:with: |
|
288 |
) |
|
289 |
at: argArrayOrNil size + 1). |
|
13403 | 290 |
trampoline := trampoline asByteCodeMethod. |
18359 | 291 |
1 to: trampoline numLiterals do: [:litNr| |
292 |
(trampoline literalAt: litNr) == #__placeholder__ ifTrue:[ |
|
293 |
(trampoline literalAt: litNr put: selector) |
|
294 |
] |
|
295 |
]. |
|
13403 | 296 |
^trampoline |
297 |
||
298 |
"Created: / 19-08-2010 / 22:09:24 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
299 |
! |
|
300 |
||
301 |
ambiguousMessageSendWith: a1 |
|
302 |
||
303 |
^self ambiguousMessage: |
|
18359 | 304 |
(Message |
305 |
selector: #__placeholder__ |
|
306 |
arguments: (Array with: a1) |
|
307 |
) |
|
13403 | 308 |
|
309 |
"Created: / 19-08-2010 / 22:06:08 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
310 |
! |
|
311 |
||
312 |
ambiguousMessageSendWith: a1 with: a2 |
|
313 |
||
314 |
^self ambiguousMessage: |
|
18359 | 315 |
(Message |
316 |
selector: #__placeholder__ |
|
317 |
arguments: (Array with: a1 with: a2) |
|
318 |
) |
|
13403 | 319 |
|
320 |
"Created: / 19-08-2010 / 22:06:37 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
321 |
! |
|
322 |
||
323 |
ambiguousMessageSendWith: a1 with: a2 with: a3 |
|
324 |
||
325 |
^self ambiguousMessage: |
|
18359 | 326 |
(Message |
327 |
selector: #__placeholder__ |
|
328 |
arguments: (Array with: a1 with: a2 with: a3) |
|
329 |
) |
|
13403 | 330 |
|
331 |
"Created: / 19-08-2010 / 22:06:48 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
332 |
! |
|
333 |
||
334 |
ambiguousMessageSendWith: a1 with: a2 with: a3 with: a4 |
|
335 |
||
336 |
^self ambiguousMessage: |
|
18359 | 337 |
(Message |
338 |
selector: #__placeholder__ |
|
339 |
arguments: (Array with: a1 with: a2 with: a3 with: a4) |
|
340 |
) |
|
13403 | 341 |
|
342 |
"Created: / 19-08-2010 / 22:06:56 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
343 |
! |
|
344 |
||
345 |
ambiguousMessageSendWith: a1 with: a2 with: a3 with: a4 |
|
18359 | 346 |
with: a5 |
13403 | 347 |
|
348 |
^self ambiguousMessage: |
|
18359 | 349 |
(Message |
350 |
selector: #__placeholder__ |
|
351 |
arguments: (Array with: a1 with: a2 with: a3 with: a4 |
|
352 |
with: a5) |
|
353 |
) |
|
13403 | 354 |
|
355 |
"Created: / 19-08-2010 / 22:07:15 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
356 |
! |
|
357 |
||
358 |
ambiguousMessageSendWith: a1 with: a2 with: a3 with: a4 |
|
18359 | 359 |
with: a5 with: a6 |
13403 | 360 |
|
361 |
^self ambiguousMessage: |
|
18359 | 362 |
(Message |
363 |
selector: #__placeholder__ |
|
364 |
arguments: (Array with: a1 with: a2 with: a3 with: a4 |
|
365 |
with: a5 with: a6) |
|
366 |
) |
|
13403 | 367 |
|
368 |
"Created: / 19-08-2010 / 22:07:23 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
369 |
! |
|
370 |
||
371 |
ambiguousMessageSendWith: a1 with: a2 with: a3 with: a4 |
|
18359 | 372 |
with: a5 with: a6 with: a7 |
13403 | 373 |
|
374 |
^self ambiguousMessage: |
|
18359 | 375 |
(Message |
376 |
selector: #__placeholder__ |
|
377 |
arguments: (Array with: a1 with: a2 with: a3 with: a4 |
|
378 |
with: a5 with: a6 with: a7) |
|
379 |
) |
|
13403 | 380 |
|
381 |
"Created: / 19-08-2010 / 22:07:37 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
382 |
! |
|
383 |
||
384 |
ambiguousMessageSendWith: a1 with: a2 with: a3 with: a4 |
|
18359 | 385 |
with: a5 with: a6 with: a7 with: a8 |
13403 | 386 |
|
387 |
^self ambiguousMessage: |
|
18359 | 388 |
(Message |
389 |
selector: #__placeholder__ |
|
390 |
arguments: (Array with: a1 with: a2 with: a3 with: a4 |
|
391 |
with: a5 with: a6 with: a7 with: a8) |
|
392 |
) |
|
13403 | 393 |
|
394 |
"Created: / 19-08-2010 / 22:08:03 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
395 |
! ! |
|
396 |
||
397 |
!NamespaceAwareLookup class methodsFor:'documentation'! |
|
398 |
||
13478 | 399 |
version_CVS |
21100 | 400 |
^ '$Header$' |
13478 | 401 |
! |
402 |
||
13403 | 403 |
version_SVN |
21100 | 404 |
^ '$Id$' |
13403 | 405 |
! ! |
406 |
||
16080
e12fe7f621a8
class: NamespaceAwareLookup
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13478
diff
changeset
|
407 |
|
13403 | 408 |
NamespaceAwareLookup initialize! |