author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 02 Jan 2013 18:51:13 +0100 | |
branch | development |
changeset 1922 | 7fa6a6a8f1d8 |
parent 1864 | 60a8dc26c8c6 |
child 1971 | e75c1790521c |
permissions | -rw-r--r-- |
877 | 1 |
" |
2 |
COPYRIGHT (c) 1996-2011 by Claus Gittinger |
|
1152 | 3 |
|
1155 | 4 |
New code and modifications done at SWING Research Group [1]: |
1152 | 5 |
|
877 | 6 |
COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko |
7 |
SWING Research Group, Czech Technical University in Prague |
|
8 |
||
9 |
This software is furnished under a license and may be used |
|
10 |
only in accordance with the terms of that license and with the |
|
11 |
inclusion of the above copyright notice. This software may not |
|
12 |
be provided or otherwise made available to, or used by, any |
|
13 |
other person. No title to or ownership of the software is |
|
14 |
hereby transferred. |
|
15 |
||
1155 | 16 |
[1] Code written at SWING Research Group contains a signature |
17 |
of one of the above copright owners. For exact set of such code, |
|
18 |
see the differences between this version and version stx:libjava |
|
1152 | 19 |
as of 1.9.2010 |
877 | 20 |
" |
752 | 21 |
"{ Package: 'stx:libjava' }" |
22 |
||
23 |
JavaMethodWithHandler variableSubclass:#JavaNativeMethod |
|
1749 | 24 |
instanceVariableNames:'nativeImplementation nCalls' |
1075
927aeb3649fc
Fix in native method imp -> class loading java tests all pass.. I'm getting drunk tonight :)
hlopkmar
parents:
1073
diff
changeset
|
25 |
classVariableNames:'CacheNativeImplementation Verbose' |
752 | 26 |
poolDictionaries:'' |
27 |
category:'Languages-Java-Classes' |
|
28 |
! |
|
29 |
||
877 | 30 |
!JavaNativeMethod class methodsFor:'documentation'! |
31 |
||
32 |
copyright |
|
33 |
" |
|
34 |
COPYRIGHT (c) 1996-2011 by Claus Gittinger |
|
1152 | 35 |
|
1155 | 36 |
New code and modifications done at SWING Research Group [1]: |
1152 | 37 |
|
877 | 38 |
COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko |
39 |
SWING Research Group, Czech Technical University in Prague |
|
40 |
||
41 |
This software is furnished under a license and may be used |
|
42 |
only in accordance with the terms of that license and with the |
|
43 |
inclusion of the above copyright notice. This software may not |
|
44 |
be provided or otherwise made available to, or used by, any |
|
45 |
other person. No title to or ownership of the software is |
|
46 |
hereby transferred. |
|
47 |
||
1155 | 48 |
[1] Code written at SWING Research Group contains a signature |
49 |
of one of the above copright owners. For exact set of such code, |
|
50 |
see the differences between this version and version stx:libjava |
|
1152 | 51 |
as of 1.9.2010 |
877 | 52 |
|
53 |
" |
|
54 |
! ! |
|
752 | 55 |
|
56 |
!JavaNativeMethod class methodsFor:'initialization'! |
|
57 |
||
761 | 58 |
cacheNativeImplementation |
59 |
||
60 |
"For details, see #cacheNativeImplementation:" |
|
61 |
||
62 |
^CacheNativeImplementation |
|
63 |
||
64 |
"Created: / 30-04-2011 / 23:38:21 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
65 |
! |
|
66 |
||
67 |
cacheNativeImplementation: aBoolean |
|
68 |
||
69 |
"If set, native implementations are cached, resulting |
|
70 |
in better performance when calling native methods. |
|
71 |
Hower, no change in native method implemenetaion will |
|
72 |
not be visible then, unless #flushAllCachedNativeMethods |
|
73 |
is explictely called" |
|
74 |
||
75 |
CacheNativeImplementation := aBoolean |
|
76 |
||
77 |
"Created: / 30-04-2011 / 23:38:10 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
78 |
! |
|
79 |
||
752 | 80 |
initialize |
1075
927aeb3649fc
Fix in native method imp -> class loading java tests all pass.. I'm getting drunk tonight :)
hlopkmar
parents:
1073
diff
changeset
|
81 |
self flags: (self flags bitOr: Behavior flagJavaMethod). |
927aeb3649fc
Fix in native method imp -> class loading java tests all pass.. I'm getting drunk tonight :)
hlopkmar
parents:
1073
diff
changeset
|
82 |
"By default, do not cache native impls while developing" |
761 | 83 |
CacheNativeImplementation := Smalltalk isStandAloneApp. |
1075
927aeb3649fc
Fix in native method imp -> class loading java tests all pass.. I'm getting drunk tonight :)
hlopkmar
parents:
1073
diff
changeset
|
84 |
Verbose := false. |
927aeb3649fc
Fix in native method imp -> class loading java tests all pass.. I'm getting drunk tonight :)
hlopkmar
parents:
1073
diff
changeset
|
85 |
" |
927aeb3649fc
Fix in native method imp -> class loading java tests all pass.. I'm getting drunk tonight :)
hlopkmar
parents:
1073
diff
changeset
|
86 |
Verbose := true. |
927aeb3649fc
Fix in native method imp -> class loading java tests all pass.. I'm getting drunk tonight :)
hlopkmar
parents:
1073
diff
changeset
|
87 |
" |
761 | 88 |
|
89 |
"Modified: / 30-04-2011 / 23:35:40 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
1075
927aeb3649fc
Fix in native method imp -> class loading java tests all pass.. I'm getting drunk tonight :)
hlopkmar
parents:
1073
diff
changeset
|
90 |
"Modified (comment): / 03-11-2011 / 10:48:12 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
752 | 91 |
! ! |
92 |
||
93 |
!JavaNativeMethod class methodsFor:'cleanup'! |
|
94 |
||
95 |
flushAllCachedNativeMethods |
|
96 |
self allInstancesDo:[:aNativeMethod | |
|
97 |
aNativeMethod nativeImplementation:nil |
|
98 |
]. |
|
99 |
||
100 |
" |
|
101 |
self flushAllCachedNativeMethods |
|
102 |
" |
|
103 |
||
104 |
"Created: / 24.12.1999 / 03:10:38 / cg" |
|
105 |
"Modified: / 24.12.1999 / 03:10:51 / cg" |
|
106 |
! ! |
|
107 |
||
108 |
!JavaNativeMethod methodsFor:'accessing'! |
|
109 |
||
1749 | 110 |
nCalls |
111 |
^ nCalls ? 0 |
|
112 |
||
113 |
"Modified: / 27-10-2012 / 18:05:49 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
114 |
! |
|
115 |
||
1754
b3cde0c1c2b1
- do not enforce Java native code optimization off
vranyj1
parents:
1749
diff
changeset
|
116 |
nCallsReset |
b3cde0c1c2b1
- do not enforce Java native code optimization off
vranyj1
parents:
1749
diff
changeset
|
117 |
nCalls := 0 |
b3cde0c1c2b1
- do not enforce Java native code optimization off
vranyj1
parents:
1749
diff
changeset
|
118 |
|
b3cde0c1c2b1
- do not enforce Java native code optimization off
vranyj1
parents:
1749
diff
changeset
|
119 |
"Modified: / 27-10-2012 / 18:05:49 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
b3cde0c1c2b1
- do not enforce Java native code optimization off
vranyj1
parents:
1749
diff
changeset
|
120 |
! |
b3cde0c1c2b1
- do not enforce Java native code optimization off
vranyj1
parents:
1749
diff
changeset
|
121 |
|
752 | 122 |
nativeImplementation |
123 |
"return the value of the instance variable 'nativeImplementation' (automatically generated)" |
|
124 |
||
125 |
^ nativeImplementation |
|
126 |
||
127 |
"Created: / 25.9.1999 / 23:08:00 / cg" |
|
128 |
! |
|
129 |
||
130 |
nativeImplementation:something |
|
131 |
"set the value of the instance variable 'nativeImplementation' (automatically generated)" |
|
132 |
||
133 |
nativeImplementation := something. |
|
134 |
||
135 |
"Created: / 25.9.1999 / 23:08:00 / cg" |
|
136 |
! |
|
137 |
||
138 |
sourceLineNumber |
|
1213
fc90769151e8
- fix in reflection over methods (signature is nil for non-generic methods)
vranyj1
parents:
1155
diff
changeset
|
139 |
|
fc90769151e8
- fix in reflection over methods (signature is nil for non-generic methods)
vranyj1
parents:
1155
diff
changeset
|
140 |
^super sourceLineNumber |
fc90769151e8
- fix in reflection over methods (signature is nil for non-generic methods)
vranyj1
parents:
1155
diff
changeset
|
141 |
|
fc90769151e8
- fix in reflection over methods (signature is nil for non-generic methods)
vranyj1
parents:
1155
diff
changeset
|
142 |
"/ ForceByteCodeDisplay == true ifTrue:[ ^ 1]. |
fc90769151e8
- fix in reflection over methods (signature is nil for non-generic methods)
vranyj1
parents:
1155
diff
changeset
|
143 |
"/ lineNumberTable notNil ifTrue:[ ^ lineNumberTable at:2]. |
fc90769151e8
- fix in reflection over methods (signature is nil for non-generic methods)
vranyj1
parents:
1155
diff
changeset
|
144 |
"/ ^1 |
752 | 145 |
|
146 |
||
147 |
" |
|
148 |
^ (JavaSourceCodeCache new) |
|
149 |
findLineForMethod:(self selector) |
|
150 |
inClass:javaClass. |
|
151 |
" |
|
152 |
||
153 |
"Modified: / 13-12-2010 / 13:55:55 / Jan Kurs <kurs.jan@post.cz>" |
|
154 |
"Modified: / 13-12-2010 / 23:46:30 / Marcel Hlopko <hlopik@gmail.com>" |
|
155 |
"Created: / 17-12-2010 / 10:34:43 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
156 |
! ! |
|
157 |
||
761 | 158 |
!JavaNativeMethod methodsFor:'private'! |
159 |
||
160 |
compileNativeImplementation: sel dispatchingTo: oldSel |
|
161 |
||
162 |
| src arg converted | |
|
163 |
src := (JavaVM class compiledMethodAt: oldSel) source. |
|
164 |
src := src asStringCollection. |
|
165 |
(src first includesString: 'aJavaContext') ifTrue:[ |
|
166 |
arg := 'aJavaContext' |
|
167 |
] ifFalse:[ |
|
168 |
(src first includesString: 'nativeContext') ifTrue:[ |
|
169 |
arg := 'nativeContext' |
|
170 |
] |
|
171 |
]. |
|
172 |
arg ifNotNil:[ |
|
173 |
src removeFirst asString. |
|
174 |
converted := true. |
|
175 |
] ifNil:[ |
|
176 |
arg := 'nativeContext'. |
|
177 |
src := ' self breakPoint: #jv info: ''Convert it to new-style natives''. |
|
178 |
||
179 |
^ self ', oldSel, ' nativeContext'. |
|
180 |
converted := false. |
|
181 |
]. |
|
182 |
||
183 |
(JavaVM class |
|
184 |
compile: |
|
185 |
(self nativeMethodTemplate bindWith:sel with: arg with: src asString) |
|
186 |
classified: |
|
187 |
'native - ', ((javaClass javaPackage upTo:$$) replaceAll:$/ with:$. )) |
|
188 |
package: JavaVM package. |
|
189 |
||
190 |
converted ifTrue:[ |
|
191 |
(JavaVM class compiledMethodAt: oldSel) category: 'native - old-style (converted)' |
|
192 |
] ifFalse:[ |
|
193 |
(JavaVM class compiledMethodAt: oldSel) category: 'native - old-style (FAILED to convert)' |
|
194 |
] |
|
195 |
||
196 |
"Created: / 01-05-2011 / 00:08:59 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
197 |
"Modified: / 01-05-2011 / 13:15:13 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
198 |
! |
|
199 |
||
200 |
compileNativeImplementationStub: sel |
|
201 |
||
202 |
(JavaVM class |
|
203 |
compile: |
|
204 |
(self nativeMethodTemplate bindWith:sel with: 'nativeContext' with:('^ UnimplementedNativeMethodSignal raise')) |
|
205 |
classified: |
|
206 |
'native - ', ((javaClass javaPackage upTo:$$) replaceAll:$/ with:$.)) |
|
207 |
package: JavaVM package |
|
208 |
||
209 |
"Created: / 01-05-2011 / 00:08:59 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
210 |
! |
|
211 |
||
212 |
nativeMethodTemplate |
|
213 |
||
214 |
^'%1 %2 |
|
215 |
||
216 |
<javanative: ''', javaClass name , ''' name: ''', (selector copyWithoutLast:signature size), '''> |
|
217 |
||
218 |
%3' |
|
219 |
||
220 |
"Created: / 01-05-2011 / 00:12:42 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
221 |
! |
|
222 |
||
223 |
searchNativeImplementation |
|
224 |
||
225 |
"Returns a SELECTOR of native method implementation. |
|
226 |
For now, two naming schemes are used. The onld one uses |
|
227 |
just a class name and selector as a name for native method impl. |
|
228 |
The new one uses fully qualified class name. |
|
229 |
" |
|
230 |
||
231 |
| nm newStyleSel oldStyleSel | |
|
912
e651488f5741
Many fixes in reflection, expecially for generic types support
vranyj1
parents:
877
diff
changeset
|
232 |
nm := selector upTo: $(. |
761 | 233 |
newStyleSel := ('_' , ((javaClass name copyReplaceAll:$/ with:$_) replaceAll:$$ with:$_), '_' , nm , ':') asSymbol. |
234 |
(JavaVM class canUnderstand: newStyleSel) ifTrue: |
|
235 |
["Good, a JavaVM understands new style native selectors" |
|
236 |
^newStyleSel]. |
|
237 |
||
238 |
oldStyleSel := ('_' , (javaClass lastName copyReplaceAll:$$ with:$_) , '_' , nm , ':') asSymbol. |
|
239 |
(JavaVM class canUnderstand: oldStyleSel) ifTrue: |
|
240 |
[ |
|
241 |
"Convert method on the fly only if Im Jan Vrany |
|
242 |
(to avoid confusion of other developers :-)" |
|
243 |
OperatingSystem getLoginName = 'jv' ifTrue:[ |
|
244 |
"OK, old style method has not yet been converted to a newstyle one. |
|
245 |
Converts old-style method to a new-style one" |
|
246 |
self compileNativeImplementation: newStyleSel dispatchingTo: oldStyleSel. |
|
247 |
^newStyleSel |
|
248 |
] ifFalse:[ |
|
249 |
^oldStyleSel |
|
250 |
]]. |
|
251 |
self compileNativeImplementationStub: newStyleSel. |
|
252 |
^newStyleSel |
|
253 |
||
254 |
"Created: / 30-04-2011 / 23:50:29 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
912
e651488f5741
Many fixes in reflection, expecially for generic types support
vranyj1
parents:
877
diff
changeset
|
255 |
"Modified: / 13-08-2011 / 01:08:04 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
761 | 256 |
! ! |
257 |
||
752 | 258 |
!JavaNativeMethod methodsFor:'vm support'! |
259 |
||
260 |
nativeMethodInvokation |
|
761 | 261 |
"Called by the VM when a native method is |
1748 | 262 |
to be executed - old (slow) implementation" |
263 |
||
264 |
^self nativeMethodInvokation: thisContext sender. |
|
265 |
||
266 |
" |
|
267 |
JavaNativeMethod flushAllCachedNativeMethods" |
|
268 |
||
269 |
"Modified: / 27-01-2000 / 13:34:53 / cg" |
|
270 |
"Modified: / 03-11-2011 / 10:47:48 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
271 |
"Modified: / 27-10-2012 / 15:15:54 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
272 |
! |
|
273 |
||
274 |
nativeMethodInvokation: context |
|
275 |
"Called by the VM when a native method is |
|
276 |
to be executed. |
|
277 |
||
278 |
'context' is the context of being-invoked native method" |
|
1073
e842ab17fe16
quite big class loaders rewrite.. javavm/groovy boots, almost all tests pass.. hope you will like it..
hlopkmar
parents:
1022
diff
changeset
|
279 |
|
e842ab17fe16
quite big class loaders rewrite.. javavm/groovy boots, almost all tests pass.. hope you will like it..
hlopkmar
parents:
1022
diff
changeset
|
280 |
| sel mthd sender | |
1749 | 281 |
nCalls := (nCalls ? 0) + 1. |
1073
e842ab17fe16
quite big class loaders rewrite.. javavm/groovy boots, almost all tests pass.. hope you will like it..
hlopkmar
parents:
1022
diff
changeset
|
282 |
(mthd := nativeImplementation) isNil ifTrue: [ |
1075
927aeb3649fc
Fix in native method imp -> class loading java tests all pass.. I'm getting drunk tonight :)
hlopkmar
parents:
1073
diff
changeset
|
283 |
sel := self searchNativeImplementation. |
1073
e842ab17fe16
quite big class loaders rewrite.. javavm/groovy boots, almost all tests pass.. hope you will like it..
hlopkmar
parents:
1022
diff
changeset
|
284 |
mthd := (JavaVM class compiledMethodAt: sel). |
e842ab17fe16
quite big class loaders rewrite.. javavm/groovy boots, almost all tests pass.. hope you will like it..
hlopkmar
parents:
1022
diff
changeset
|
285 |
(mthd isNil or: [ mthd isLazyMethod ]) ifTrue: [ |
1748 | 286 |
sender := context. |
1073
e842ab17fe16
quite big class loaders rewrite.. javavm/groovy boots, almost all tests pass.. hope you will like it..
hlopkmar
parents:
1022
diff
changeset
|
287 |
sender sender selector == #noByteCode ifTrue: [ |
752 | 288 |
sender := sender sender. |
289 |
sender := sender sender. |
|
290 |
sender := sender sender. |
|
291 |
]. |
|
1073
e842ab17fe16
quite big class loaders rewrite.. javavm/groovy boots, almost all tests pass.. hope you will like it..
hlopkmar
parents:
1022
diff
changeset
|
292 |
^ JavaVM perform: sel with: sender. |
752 | 293 |
]. |
1073
e842ab17fe16
quite big class loaders rewrite.. javavm/groovy boots, almost all tests pass.. hope you will like it..
hlopkmar
parents:
1022
diff
changeset
|
294 |
CacheNativeImplementation ifTrue: [ nativeImplementation := mthd. ] |
752 | 295 |
]. |
1075
927aeb3649fc
Fix in native method imp -> class loading java tests all pass.. I'm getting drunk tonight :)
hlopkmar
parents:
1073
diff
changeset
|
296 |
Verbose ifTrue: [Logger log: 'Native method invokation: ' , sel severity: #debug facility: #JVM]. |
1073
e842ab17fe16
quite big class loaders rewrite.. javavm/groovy boots, almost all tests pass.. hope you will like it..
hlopkmar
parents:
1022
diff
changeset
|
297 |
^ mthd |
e842ab17fe16
quite big class loaders rewrite.. javavm/groovy boots, almost all tests pass.. hope you will like it..
hlopkmar
parents:
1022
diff
changeset
|
298 |
valueWithReceiver: JavaVM |
1748 | 299 |
arguments: (Array with: context) |
1073
e842ab17fe16
quite big class loaders rewrite.. javavm/groovy boots, almost all tests pass.. hope you will like it..
hlopkmar
parents:
1022
diff
changeset
|
300 |
selector: selector |
e842ab17fe16
quite big class loaders rewrite.. javavm/groovy boots, almost all tests pass.. hope you will like it..
hlopkmar
parents:
1022
diff
changeset
|
301 |
search: JavaVM class |
e842ab17fe16
quite big class loaders rewrite.. javavm/groovy boots, almost all tests pass.. hope you will like it..
hlopkmar
parents:
1022
diff
changeset
|
302 |
sender: nil |
752 | 303 |
|
1073
e842ab17fe16
quite big class loaders rewrite.. javavm/groovy boots, almost all tests pass.. hope you will like it..
hlopkmar
parents:
1022
diff
changeset
|
304 |
" |
e842ab17fe16
quite big class loaders rewrite.. javavm/groovy boots, almost all tests pass.. hope you will like it..
hlopkmar
parents:
1022
diff
changeset
|
305 |
JavaNativeMethod flushAllCachedNativeMethods" |
752 | 306 |
|
1748 | 307 |
"Created: / 27-10-2012 / 15:13:35 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
1749 | 308 |
"Modified: / 27-10-2012 / 18:02:57 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
752 | 309 |
! ! |
310 |
||
311 |
!JavaNativeMethod class methodsFor:'documentation'! |
|
312 |
||
313 |
version |
|
1864 | 314 |
^ '$Header$' |
315 |
! |
|
316 |
||
317 |
version_HG |
|
318 |
||
319 |
^ '$Changeset: <not expanded> $' |
|
752 | 320 |
! |
321 |
||
322 |
version_SVN |
|
1864 | 323 |
^ '§Id§' |
923 | 324 |
|
752 | 325 |
! ! |
326 |
||
327 |
JavaNativeMethod initialize! |