author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 16 Jan 2013 22:06:41 +0000 | |
branch | refactoring-vmdata |
changeset 1977 | 526315e0a801 |
parent 1975 | 038d40cadeb3 |
child 1985 | 7c5a14d663ba |
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: |
|
1975
038d40cadeb3
Fix in JavaNativeMethod - search natives in 'JavaVM natives'.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
1971
diff
changeset
|
204 |
(self nativeMethodTemplate bindWith:sel with: 'nativeContext' with:('^ JavaVM unimplementedNativeMethodSignal raise')) |
761 | 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>" |
|
1975
038d40cadeb3
Fix in JavaNativeMethod - search natives in 'JavaVM natives'.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
1971
diff
changeset
|
210 |
"Modified: / 16-01-2013 / 21:26:52 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
761 | 211 |
! |
212 |
||
213 |
nativeMethodTemplate |
|
214 |
||
215 |
^'%1 %2 |
|
216 |
||
217 |
<javanative: ''', javaClass name , ''' name: ''', (selector copyWithoutLast:signature size), '''> |
|
218 |
||
219 |
%3' |
|
220 |
||
221 |
"Created: / 01-05-2011 / 00:12:42 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
222 |
! |
|
223 |
||
224 |
searchNativeImplementation |
|
225 |
||
226 |
"Returns a SELECTOR of native method implementation. |
|
227 |
For now, two naming schemes are used. The onld one uses |
|
228 |
just a class name and selector as a name for native method impl. |
|
229 |
The new one uses fully qualified class name. |
|
230 |
" |
|
231 |
||
232 |
| nm newStyleSel oldStyleSel | |
|
912
e651488f5741
Many fixes in reflection, expecially for generic types support
vranyj1
parents:
877
diff
changeset
|
233 |
nm := selector upTo: $(. |
761 | 234 |
newStyleSel := ('_' , ((javaClass name copyReplaceAll:$/ with:$_) replaceAll:$$ with:$_), '_' , nm , ':') asSymbol. |
1975
038d40cadeb3
Fix in JavaNativeMethod - search natives in 'JavaVM natives'.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
1971
diff
changeset
|
235 |
(JavaVM natives class canUnderstand: newStyleSel) ifTrue:[ |
038d40cadeb3
Fix in JavaNativeMethod - search natives in 'JavaVM natives'.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
1971
diff
changeset
|
236 |
"Good, a JavaVM understands new style native selectors" |
038d40cadeb3
Fix in JavaNativeMethod - search natives in 'JavaVM natives'.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
1971
diff
changeset
|
237 |
^newStyleSel |
038d40cadeb3
Fix in JavaNativeMethod - search natives in 'JavaVM natives'.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
1971
diff
changeset
|
238 |
]. |
761 | 239 |
|
240 |
oldStyleSel := ('_' , (javaClass lastName copyReplaceAll:$$ with:$_) , '_' , nm , ':') asSymbol. |
|
1975
038d40cadeb3
Fix in JavaNativeMethod - search natives in 'JavaVM natives'.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
1971
diff
changeset
|
241 |
(JavaVM natives canUnderstand: oldStyleSel) ifTrue:[ |
761 | 242 |
"Convert method on the fly only if Im Jan Vrany |
243 |
(to avoid confusion of other developers :-)" |
|
244 |
OperatingSystem getLoginName = 'jv' ifTrue:[ |
|
245 |
"OK, old style method has not yet been converted to a newstyle one. |
|
246 |
Converts old-style method to a new-style one" |
|
247 |
self compileNativeImplementation: newStyleSel dispatchingTo: oldStyleSel. |
|
248 |
^newStyleSel |
|
249 |
] ifFalse:[ |
|
250 |
^oldStyleSel |
|
1975
038d40cadeb3
Fix in JavaNativeMethod - search natives in 'JavaVM natives'.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
1971
diff
changeset
|
251 |
] |
038d40cadeb3
Fix in JavaNativeMethod - search natives in 'JavaVM natives'.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
1971
diff
changeset
|
252 |
]. |
761 | 253 |
self compileNativeImplementationStub: newStyleSel. |
254 |
^newStyleSel |
|
255 |
||
256 |
"Created: / 30-04-2011 / 23:50:29 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
1975
038d40cadeb3
Fix in JavaNativeMethod - search natives in 'JavaVM natives'.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
1971
diff
changeset
|
257 |
"Modified: / 16-01-2013 / 21:25:39 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
761 | 258 |
! ! |
259 |
||
752 | 260 |
!JavaNativeMethod methodsFor:'vm support'! |
261 |
||
262 |
nativeMethodInvokation |
|
761 | 263 |
"Called by the VM when a native method is |
1748 | 264 |
to be executed - old (slow) implementation" |
265 |
||
266 |
^self nativeMethodInvokation: thisContext sender. |
|
267 |
||
268 |
" |
|
269 |
JavaNativeMethod flushAllCachedNativeMethods" |
|
270 |
||
271 |
"Modified: / 27-01-2000 / 13:34:53 / cg" |
|
272 |
"Modified: / 03-11-2011 / 10:47:48 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
273 |
"Modified: / 27-10-2012 / 15:15:54 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
274 |
! |
|
275 |
||
276 |
nativeMethodInvokation: context |
|
277 |
"Called by the VM when a native method is |
|
278 |
to be executed. |
|
279 |
||
280 |
'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
|
281 |
|
e842ab17fe16
quite big class loaders rewrite.. javavm/groovy boots, almost all tests pass.. hope you will like it..
hlopkmar
parents:
1022
diff
changeset
|
282 |
| sel mthd sender | |
1749 | 283 |
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
|
284 |
(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
|
285 |
sel := self searchNativeImplementation. |
1971
e75c1790521c
Java native implementation refactoring (phase 1)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
1864
diff
changeset
|
286 |
mthd := (JavaVM natives class compiledMethodAt: sel). |
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 |
(mthd isNil or: [ mthd isLazyMethod ]) ifTrue: [ |
1748 | 288 |
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
|
289 |
sender sender selector == #noByteCode ifTrue: [ |
752 | 290 |
sender := sender sender. |
291 |
sender := sender sender. |
|
292 |
sender := sender sender. |
|
293 |
]. |
|
1971
e75c1790521c
Java native implementation refactoring (phase 1)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
1864
diff
changeset
|
294 |
^ JavaVM natives perform: sel with: sender. |
752 | 295 |
]. |
1073
e842ab17fe16
quite big class loaders rewrite.. javavm/groovy boots, almost all tests pass.. hope you will like it..
hlopkmar
parents:
1022
diff
changeset
|
296 |
CacheNativeImplementation ifTrue: [ nativeImplementation := mthd. ] |
752 | 297 |
]. |
1075
927aeb3649fc
Fix in native method imp -> class loading java tests all pass.. I'm getting drunk tonight :)
hlopkmar
parents:
1073
diff
changeset
|
298 |
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
|
299 |
^ mthd |
1971
e75c1790521c
Java native implementation refactoring (phase 1)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
1864
diff
changeset
|
300 |
valueWithReceiver: JavaVM natives |
1748 | 301 |
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
|
302 |
selector: selector |
1971
e75c1790521c
Java native implementation refactoring (phase 1)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
1864
diff
changeset
|
303 |
search: JavaVM natives class |
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 |
sender: nil |
752 | 305 |
|
1073
e842ab17fe16
quite big class loaders rewrite.. javavm/groovy boots, almost all tests pass.. hope you will like it..
hlopkmar
parents:
1022
diff
changeset
|
306 |
" |
e842ab17fe16
quite big class loaders rewrite.. javavm/groovy boots, almost all tests pass.. hope you will like it..
hlopkmar
parents:
1022
diff
changeset
|
307 |
JavaNativeMethod flushAllCachedNativeMethods" |
752 | 308 |
|
1748 | 309 |
"Created: / 27-10-2012 / 15:13:35 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
1971
e75c1790521c
Java native implementation refactoring (phase 1)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
1864
diff
changeset
|
310 |
"Modified: / 16-01-2013 / 20:03:41 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
752 | 311 |
! ! |
312 |
||
313 |
!JavaNativeMethod class methodsFor:'documentation'! |
|
314 |
||
315 |
version |
|
1864 | 316 |
^ '$Header$' |
317 |
! |
|
318 |
||
319 |
version_HG |
|
320 |
||
321 |
^ '$Changeset: <not expanded> $' |
|
752 | 322 |
! |
323 |
||
324 |
version_SVN |
|
1864 | 325 |
^ '§Id§' |
923 | 326 |
|
752 | 327 |
! ! |
328 |
||
329 |
JavaNativeMethod initialize! |