749
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1996-2011 by Claus Gittinger
|
|
3 |
COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
|
|
4 |
SWING Research Group, Czech Technical University in Prague
|
|
5 |
|
|
6 |
Parts of the code written by Claus Gittinger are under following
|
|
7 |
license:
|
|
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 |
|
|
16 |
Parts of the code written at SWING Reasearch Group [1] are MIT licensed:
|
|
17 |
|
|
18 |
Permission is hereby granted, free of charge, to any person
|
|
19 |
obtaining a copy of this software and associated documentation
|
|
20 |
files (the 'Software'), to deal in the Software without
|
|
21 |
restriction, including without limitation the rights to use,
|
|
22 |
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
23 |
copies of the Software, and to permit persons to whom the
|
|
24 |
Software is furnished to do so, subject to the following
|
|
25 |
conditions:
|
|
26 |
|
|
27 |
The above copyright notice and this permission notice shall be
|
|
28 |
included in all copies or substantial portions of the Software.
|
|
29 |
|
|
30 |
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
31 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
32 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
33 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
34 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
35 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
36 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
37 |
OTHER DEALINGS IN THE SOFTWARE.
|
|
38 |
|
|
39 |
[1] Code written at SWING Research Group contain a signature
|
|
40 |
of one of the above copright owners.
|
|
41 |
"
|
703
|
42 |
"{ Package: 'stx:libjava' }"
|
|
43 |
|
611
|
44 |
JavaMethodWithHandler variableSubclass:#JavaNativeMethod
|
|
45 |
instanceVariableNames:'nativeImplementation'
|
749
|
46 |
classVariableNames:'CacheNativeImplementation'
|
611
|
47 |
poolDictionaries:''
|
713
|
48 |
category:'Languages-Java-Classes'
|
611
|
49 |
!
|
|
50 |
|
749
|
51 |
!JavaNativeMethod class methodsFor:'documentation'!
|
|
52 |
|
|
53 |
copyright
|
|
54 |
"
|
|
55 |
COPYRIGHT (c) 1996-2011 by Claus Gittinger
|
|
56 |
COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
|
|
57 |
SWING Research Group, Czech Technical University in Prague
|
|
58 |
|
|
59 |
Parts of the code written by Claus Gittinger are under following
|
|
60 |
license:
|
|
61 |
|
|
62 |
This software is furnished under a license and may be used
|
|
63 |
only in accordance with the terms of that license and with the
|
|
64 |
inclusion of the above copyright notice. This software may not
|
|
65 |
be provided or otherwise made available to, or used by, any
|
|
66 |
other person. No title to or ownership of the software is
|
|
67 |
hereby transferred.
|
|
68 |
|
|
69 |
Parts of the code written at SWING Reasearch Group [1] are MIT licensed:
|
|
70 |
|
|
71 |
Permission is hereby granted, free of charge, to any person
|
|
72 |
obtaining a copy of this software and associated documentation
|
|
73 |
files (the 'Software'), to deal in the Software without
|
|
74 |
restriction, including without limitation the rights to use,
|
|
75 |
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
76 |
copies of the Software, and to permit persons to whom the
|
|
77 |
Software is furnished to do so, subject to the following
|
|
78 |
conditions:
|
|
79 |
|
|
80 |
The above copyright notice and this permission notice shall be
|
|
81 |
included in all copies or substantial portions of the Software.
|
|
82 |
|
|
83 |
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
84 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
85 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
86 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
87 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
88 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
89 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
90 |
OTHER DEALINGS IN THE SOFTWARE.
|
|
91 |
|
|
92 |
[1] Code written at SWING Research Group contain a signature
|
|
93 |
of one of the above copright owners.
|
|
94 |
|
|
95 |
"
|
|
96 |
! !
|
642
|
97 |
|
616
|
98 |
!JavaNativeMethod class methodsFor:'initialization'!
|
|
99 |
|
749
|
100 |
cacheNativeImplementation
|
|
101 |
|
|
102 |
"For details, see #cacheNativeImplementation:"
|
|
103 |
|
|
104 |
^CacheNativeImplementation
|
|
105 |
|
|
106 |
"Created: / 30-04-2011 / 23:38:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
107 |
!
|
|
108 |
|
|
109 |
cacheNativeImplementation: aBoolean
|
|
110 |
|
|
111 |
"If set, native implementations are cached, resulting
|
|
112 |
in better performance when calling native methods.
|
|
113 |
Hower, no change in native method implemenetaion will
|
|
114 |
not be visible then, unless #flushAllCachedNativeMethods
|
|
115 |
is explictely called"
|
|
116 |
|
|
117 |
CacheNativeImplementation := aBoolean
|
|
118 |
|
|
119 |
"Created: / 30-04-2011 / 23:38:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
120 |
!
|
|
121 |
|
616
|
122 |
initialize
|
|
123 |
self flags:(self flags bitOr:Behavior flagJavaMethod).
|
749
|
124 |
|
|
125 |
"By default, do not cache native impls while developing"
|
|
126 |
CacheNativeImplementation := Smalltalk isStandAloneApp.
|
|
127 |
|
|
128 |
"Modified: / 30-04-2011 / 23:35:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
616
|
129 |
! !
|
|
130 |
|
654
|
131 |
!JavaNativeMethod class methodsFor:'cleanup'!
|
|
132 |
|
|
133 |
flushAllCachedNativeMethods
|
|
134 |
self allInstancesDo:[:aNativeMethod |
|
|
135 |
aNativeMethod nativeImplementation:nil
|
|
136 |
].
|
|
137 |
|
|
138 |
"
|
|
139 |
self flushAllCachedNativeMethods
|
|
140 |
"
|
|
141 |
|
|
142 |
"Created: / 24.12.1999 / 03:10:38 / cg"
|
|
143 |
"Modified: / 24.12.1999 / 03:10:51 / cg"
|
|
144 |
! !
|
|
145 |
|
611
|
146 |
!JavaNativeMethod methodsFor:'accessing'!
|
|
147 |
|
|
148 |
nativeImplementation
|
|
149 |
"return the value of the instance variable 'nativeImplementation' (automatically generated)"
|
|
150 |
|
|
151 |
^ nativeImplementation
|
|
152 |
|
|
153 |
"Created: / 25.9.1999 / 23:08:00 / cg"
|
|
154 |
!
|
|
155 |
|
|
156 |
nativeImplementation:something
|
|
157 |
"set the value of the instance variable 'nativeImplementation' (automatically generated)"
|
|
158 |
|
|
159 |
nativeImplementation := something.
|
|
160 |
|
|
161 |
"Created: / 25.9.1999 / 23:08:00 / cg"
|
749
|
162 |
!
|
|
163 |
|
|
164 |
sourceLineNumber
|
|
165 |
ForceByteCodeDisplay == true ifTrue:[ ^ 1].
|
|
166 |
lineNumberTable notNil ifTrue:[ ^ lineNumberTable at:2].
|
|
167 |
^1
|
|
168 |
|
|
169 |
|
|
170 |
"
|
|
171 |
^ (JavaSourceCodeCache new)
|
|
172 |
findLineForMethod:(self selector)
|
|
173 |
inClass:javaClass.
|
|
174 |
"
|
|
175 |
|
|
176 |
"Modified: / 13-12-2010 / 13:55:55 / Jan Kurs <kurs.jan@post.cz>"
|
|
177 |
"Modified: / 13-12-2010 / 23:46:30 / Marcel Hlopko <hlopik@gmail.com>"
|
|
178 |
"Created: / 17-12-2010 / 10:34:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
179 |
! !
|
|
180 |
|
|
181 |
!JavaNativeMethod methodsFor:'private'!
|
|
182 |
|
|
183 |
compileNativeImplementation: sel dispatchingTo: oldSel
|
|
184 |
|
|
185 |
| src arg converted |
|
|
186 |
src := (JavaVM class compiledMethodAt: oldSel) source.
|
|
187 |
src := src asStringCollection.
|
|
188 |
(src first includesString: 'aJavaContext') ifTrue:[
|
|
189 |
arg := 'aJavaContext'
|
|
190 |
] ifFalse:[
|
|
191 |
(src first includesString: 'nativeContext') ifTrue:[
|
|
192 |
arg := 'nativeContext'
|
|
193 |
]
|
|
194 |
].
|
|
195 |
arg ifNotNil:[
|
|
196 |
src removeFirst asString.
|
|
197 |
converted := true.
|
|
198 |
] ifNil:[
|
|
199 |
arg := 'nativeContext'.
|
|
200 |
src := ' self breakPoint: #jv info: ''Convert it to new-style natives''.
|
|
201 |
|
|
202 |
^ self ', oldSel, ' nativeContext'.
|
|
203 |
converted := false.
|
|
204 |
].
|
|
205 |
|
|
206 |
(JavaVM class
|
|
207 |
compile:
|
|
208 |
(self nativeMethodTemplate bindWith:sel with: arg with: src asString)
|
|
209 |
classified:
|
|
210 |
'native - ', ((javaClass javaPackage upTo:$$) replaceAll:$/ with:$. ))
|
|
211 |
package: JavaVM package.
|
|
212 |
|
|
213 |
converted ifTrue:[
|
|
214 |
(JavaVM class compiledMethodAt: oldSel) category: 'native - old-style (converted)'
|
|
215 |
] ifFalse:[
|
|
216 |
(JavaVM class compiledMethodAt: oldSel) category: 'native - old-style (FAILED to convert)'
|
|
217 |
]
|
|
218 |
|
|
219 |
"Created: / 01-05-2011 / 00:08:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
220 |
"Modified: / 01-05-2011 / 13:15:13 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
221 |
!
|
|
222 |
|
|
223 |
compileNativeImplementationStub: sel
|
|
224 |
|
|
225 |
(JavaVM class
|
|
226 |
compile:
|
|
227 |
(self nativeMethodTemplate bindWith:sel with: 'nativeContext' with:('^ UnimplementedNativeMethodSignal raise'))
|
|
228 |
classified:
|
|
229 |
'native - ', ((javaClass javaPackage upTo:$$) replaceAll:$/ with:$.))
|
|
230 |
package: JavaVM package
|
|
231 |
|
|
232 |
"Created: / 01-05-2011 / 00:08:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
233 |
!
|
|
234 |
|
|
235 |
nativeMethodTemplate
|
|
236 |
|
|
237 |
^'%1 %2
|
|
238 |
|
|
239 |
<javanative: ''', javaClass name , ''' name: ''', (selector copyWithoutLast:signature size), '''>
|
|
240 |
|
|
241 |
%3'
|
|
242 |
|
|
243 |
"Created: / 01-05-2011 / 00:12:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
244 |
!
|
|
245 |
|
|
246 |
searchNativeImplementation
|
|
247 |
|
|
248 |
"Returns a SELECTOR of native method implementation.
|
|
249 |
For now, two naming schemes are used. The onld one uses
|
|
250 |
just a class name and selector as a name for native method impl.
|
|
251 |
The new one uses fully qualified class name.
|
|
252 |
"
|
|
253 |
|
|
254 |
| nm newStyleSel oldStyleSel |
|
|
255 |
nm := selector upTo: $(.
|
|
256 |
newStyleSel := ('_' , ((javaClass name copyReplaceAll:$/ with:$_) replaceAll:$$ with:$_), '_' , nm , ':') asSymbol.
|
|
257 |
(JavaVM class canUnderstand: newStyleSel) ifTrue:
|
|
258 |
["Good, a JavaVM understands new style native selectors"
|
|
259 |
^newStyleSel].
|
|
260 |
|
|
261 |
oldStyleSel := ('_' , (javaClass lastName copyReplaceAll:$$ with:$_) , '_' , nm , ':') asSymbol.
|
|
262 |
(JavaVM class canUnderstand: oldStyleSel) ifTrue:
|
|
263 |
[
|
|
264 |
"Convert method on the fly only if Im Jan Vrany
|
|
265 |
(to avoid confusion of other developers :-)"
|
|
266 |
OperatingSystem getLoginName = 'jv' ifTrue:[
|
|
267 |
"OK, old style method has not yet been converted to a newstyle one.
|
|
268 |
Converts old-style method to a new-style one"
|
|
269 |
self compileNativeImplementation: newStyleSel dispatchingTo: oldStyleSel.
|
|
270 |
^newStyleSel
|
|
271 |
] ifFalse:[
|
|
272 |
^oldStyleSel
|
|
273 |
]].
|
|
274 |
self compileNativeImplementationStub: newStyleSel.
|
|
275 |
^newStyleSel
|
|
276 |
|
|
277 |
"Created: / 30-04-2011 / 23:50:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
278 |
"Modified: / 13-08-2011 / 01:08:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
611
|
279 |
! !
|
|
280 |
|
|
281 |
!JavaNativeMethod methodsFor:'vm support'!
|
|
282 |
|
|
283 |
nativeMethodInvokation
|
749
|
284 |
|
|
285 |
"Called by the VM when a native method is
|
|
286 |
to be executed"
|
|
287 |
|
|
288 |
| sel mthd sender|
|
611
|
289 |
|
642
|
290 |
(mthd := nativeImplementation) isNil ifTrue:[
|
749
|
291 |
sel := self searchNativeImplementation.
|
611
|
292 |
|
642
|
293 |
mthd := (JavaVM class compiledMethodAt:sel).
|
|
294 |
(mthd isNil or:[mthd isLazyMethod]) ifTrue:[
|
660
|
295 |
sender := thisContext sender.
|
|
296 |
sender sender selector == #noByteCode ifTrue:[
|
|
297 |
sender := sender sender.
|
663
|
298 |
sender := sender sender.
|
|
299 |
sender := sender sender.
|
660
|
300 |
].
|
642
|
301 |
^ JavaVM
|
|
302 |
perform:sel
|
660
|
303 |
with:sender.
|
642
|
304 |
].
|
749
|
305 |
CacheNativeImplementation ifTrue:[
|
|
306 |
nativeImplementation := mthd.
|
|
307 |
]
|
611
|
308 |
].
|
|
309 |
|
642
|
310 |
^ mthd
|
|
311 |
valueWithReceiver:JavaVM
|
|
312 |
arguments:(Array with:thisContext sender)
|
|
313 |
selector:selector
|
|
314 |
search:JavaVM class
|
|
315 |
sender:nil
|
611
|
316 |
|
703
|
317 |
"
|
|
318 |
JavaNativeMethod flushAllCachedNativeMethods
|
|
319 |
"
|
|
320 |
|
749
|
321 |
"Modified: / 27-01-2000 / 13:34:53 / cg"
|
|
322 |
"Modified: / 30-04-2011 / 23:52:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
611
|
323 |
! !
|
|
324 |
|
|
325 |
!JavaNativeMethod class methodsFor:'documentation'!
|
|
326 |
|
|
327 |
version
|
749
|
328 |
^ '$Id: JavaNativeMethod.st,v 1.9 2011/08/18 18:42:48 vrany Exp $'
|
|
329 |
!
|
|
330 |
|
|
331 |
version_SVN
|
|
332 |
^ '$Id: JavaNativeMethod.st,v 1.9 2011/08/18 18:42:48 vrany Exp $'
|
611
|
333 |
! !
|
713
|
334 |
|
749
|
335 |
JavaNativeMethod initialize! |