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