JavaNativeMethod.st
changeset 749 e898eaeff091
parent 748 da0840b7798c
child 2097 f0310d8634d3
--- a/JavaNativeMethod.st	Thu Sep 23 13:52:13 2010 +0000
+++ b/JavaNativeMethod.st	Fri Aug 19 08:58:19 2011 +0000
@@ -1,17 +1,131 @@
+"
+ COPYRIGHT (c) 1996-2011 by Claus Gittinger
+ COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
+                            SWING Research Group, Czech Technical University in Prague
+
+ Parts of the code written by Claus Gittinger are under following
+ license:
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+
+ Parts of the code written at SWING Reasearch Group [1] are MIT licensed:
+
+ Permission is hereby granted, free of charge, to any person
+ obtaining a copy of this software and associated documentation
+ files (the 'Software'), to deal in the Software without
+ restriction, including without limitation the rights to use,
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following
+ conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ OTHER DEALINGS IN THE SOFTWARE.
+
+ [1] Code written at SWING Research Group contain a signature
+     of one of the above copright owners.
+"
 "{ Package: 'stx:libjava' }"
 
 JavaMethodWithHandler variableSubclass:#JavaNativeMethod
 	instanceVariableNames:'nativeImplementation'
-	classVariableNames:''
+	classVariableNames:'CacheNativeImplementation'
 	poolDictionaries:''
 	category:'Languages-Java-Classes'
 !
 
+!JavaNativeMethod class methodsFor:'documentation'!
+
+copyright
+"
+ COPYRIGHT (c) 1996-2011 by Claus Gittinger
+ COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
+                            SWING Research Group, Czech Technical University in Prague
+
+ Parts of the code written by Claus Gittinger are under following
+ license:
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+
+ Parts of the code written at SWING Reasearch Group [1] are MIT licensed:
+
+ Permission is hereby granted, free of charge, to any person
+ obtaining a copy of this software and associated documentation
+ files (the 'Software'), to deal in the Software without
+ restriction, including without limitation the rights to use,
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following
+ conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ OTHER DEALINGS IN THE SOFTWARE.
+
+ [1] Code written at SWING Research Group contain a signature
+     of one of the above copright owners.
+
+"
+! !
 
 !JavaNativeMethod class methodsFor:'initialization'!
 
+cacheNativeImplementation
+
+    "For details, see #cacheNativeImplementation:"
+    
+    ^CacheNativeImplementation
+
+    "Created: / 30-04-2011 / 23:38:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+cacheNativeImplementation: aBoolean
+
+    "If set, native implementations are cached, resulting
+     in better performance when calling native methods.
+     Hower, no change in native method implemenetaion will
+     not be visible then, unless #flushAllCachedNativeMethods
+     is explictely called"
+
+    CacheNativeImplementation := aBoolean
+
+    "Created: / 30-04-2011 / 23:38:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
 initialize
     self flags:(self flags bitOr:Behavior flagJavaMethod).
+
+    "By default, do not cache native impls while developing"
+    CacheNativeImplementation := Smalltalk isStandAloneApp.
+
+    "Modified: / 30-04-2011 / 23:35:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !JavaNativeMethod class methodsFor:'cleanup'!
@@ -45,16 +159,136 @@
     nativeImplementation := something.
 
     "Created: / 25.9.1999 / 23:08:00 / cg"
+!
+
+sourceLineNumber
+    ForceByteCodeDisplay == true ifTrue:[ ^ 1].
+    lineNumberTable notNil ifTrue:[ ^ lineNumberTable at:2].
+    ^1
+
+
+    "
+    ^ (JavaSourceCodeCache new) 
+        findLineForMethod:(self selector)
+        inClass:javaClass.
+    "
+
+    "Modified: / 13-12-2010 / 13:55:55 / Jan Kurs <kurs.jan@post.cz>"
+    "Modified: / 13-12-2010 / 23:46:30 / Marcel Hlopko <hlopik@gmail.com>"
+    "Created: / 17-12-2010 / 10:34:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaNativeMethod methodsFor:'private'!
+
+compileNativeImplementation: sel dispatchingTo: oldSel
+
+    | src arg converted |
+    src := (JavaVM class compiledMethodAt: oldSel) source.
+    src := src asStringCollection.
+    (src first includesString: 'aJavaContext') ifTrue:[
+        arg := 'aJavaContext'
+    ] ifFalse:[
+        (src first includesString: 'nativeContext') ifTrue:[
+            arg := 'nativeContext'
+        ]
+    ].
+    arg ifNotNil:[
+        src removeFirst asString.
+        converted := true.
+    ] ifNil:[
+        arg := 'nativeContext'.
+        src := '    self breakPoint: #jv info: ''Convert it to new-style natives''.
+
+                ^ self ', oldSel, ' nativeContext'.
+        converted := false.            
+    ].
+
+    (JavaVM class 
+        compile:
+            (self nativeMethodTemplate bindWith:sel with: arg with: src asString)
+        classified:         
+            'native - ', ((javaClass javaPackage upTo:$$) replaceAll:$/ with:$. ))
+        package: JavaVM package.
+
+    converted ifTrue:[
+        (JavaVM class compiledMethodAt: oldSel) category: 'native - old-style (converted)'
+    ] ifFalse:[
+       (JavaVM class compiledMethodAt: oldSel) category:  'native - old-style (FAILED to convert)'
+    ]
+
+    "Created: / 01-05-2011 / 00:08:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 01-05-2011 / 13:15:13 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+compileNativeImplementationStub: sel
+
+    (JavaVM class 
+        compile:
+            (self nativeMethodTemplate bindWith:sel with: 'nativeContext' with:('^ UnimplementedNativeMethodSignal raise'))
+        classified:         
+            'native - ', ((javaClass javaPackage upTo:$$) replaceAll:$/ with:$.))
+        package: JavaVM package
+
+    "Created: / 01-05-2011 / 00:08:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+nativeMethodTemplate
+
+    ^'%1 %2
+
+    <javanative: ''', javaClass name , ''' name: ''', (selector copyWithoutLast:signature size), '''>
+
+    %3'
+
+    "Created: / 01-05-2011 / 00:12:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+searchNativeImplementation
+
+    "Returns a SELECTOR of native method implementation.
+    For now, two naming schemes are used. The onld one uses
+    just a class name and selector as a name for native method impl.
+    The new one uses fully qualified class name.
+    "
+
+    | nm newStyleSel oldStyleSel |
+    nm := selector upTo: $(.
+    newStyleSel := ('_' , ((javaClass name copyReplaceAll:$/ with:$_) replaceAll:$$ with:$_), '_' , nm , ':') asSymbol.    
+    (JavaVM class canUnderstand: newStyleSel) ifTrue:
+        ["Good, a JavaVM understands new style native selectors"
+        ^newStyleSel].
+
+    oldStyleSel := ('_' , (javaClass lastName copyReplaceAll:$$ with:$_) , '_' , nm , ':') asSymbol.
+    (JavaVM class canUnderstand: oldStyleSel) ifTrue:
+        [
+        "Convert method on the fly only if Im Jan Vrany
+         (to avoid confusion of other developers :-)"
+        OperatingSystem getLoginName = 'jv' ifTrue:[
+            "OK, old style method has not yet been converted to a newstyle one.
+            Converts old-style method to a new-style one"
+            self compileNativeImplementation: newStyleSel dispatchingTo: oldStyleSel.
+            ^newStyleSel
+        ] ifFalse:[
+            ^oldStyleSel
+        ]].
+    self compileNativeImplementationStub: newStyleSel.
+    ^newStyleSel
+
+    "Created: / 30-04-2011 / 23:50:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 13-08-2011 / 01:08:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !JavaNativeMethod methodsFor:'vm support'!
 
 nativeMethodInvokation
-    |nm sel mthd sender|
+
+    "Called by the VM when a native method is
+     to be executed"
+
+    | sel mthd sender|
 
     (mthd := nativeImplementation) isNil ifTrue:[
-        nm := selector copyWithoutLast:signature size.
-        sel := ('_' , javaClass lastName , '_' , nm , ':') asSymbol.
+        sel := self searchNativeImplementation.
 
         mthd := (JavaVM class compiledMethodAt:sel).
         (mthd isNil or:[mthd isLazyMethod]) ifTrue:[
@@ -68,7 +302,9 @@
                 perform:sel
                 with:sender.
         ].
-        nativeImplementation := mthd.
+        CacheNativeImplementation ifTrue:[
+            nativeImplementation := mthd.
+        ]
     ].
 
     ^ mthd
@@ -82,13 +318,18 @@
 JavaNativeMethod flushAllCachedNativeMethods
 "
 
-    "Modified: / 27.1.2000 / 13:34:53 / cg"
+    "Modified: / 27-01-2000 / 13:34:53 / cg"
+    "Modified: / 30-04-2011 / 23:52:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !JavaNativeMethod class methodsFor:'documentation'!
 
 version
-    ^ '$Id$'
+    ^ '$Id: JavaNativeMethod.st,v 1.9 2011/08/18 18:42:48 vrany Exp $'
+!
+
+version_SVN
+    ^ '$Id: JavaNativeMethod.st,v 1.9 2011/08/18 18:42:48 vrany Exp $'
 ! !
 
-JavaNativeMethod initialize!
+JavaNativeMethod initialize!
\ No newline at end of file