cffi-abcl: global lookups in find-foreign-function and %foreign-symbol-pointer
authorStas Boukarev <stassats@gmail.com>
Sat, 12 Jan 2013 16:50:24 +0000 (20:50 +0400)
committerLuís Oliveira <loliveira@common-lisp.net>
Thu, 21 Feb 2013 22:17:32 +0000 (22:17 +0000)
Fixes issue with ABCL not finding things like "strcmp" when no libraries
are loaded.

src/cffi-abcl.lisp

index 7f61710..c19e3c1 100644 (file)
@@ -296,19 +296,41 @@ WITH-POINTER-TO-VECTOR-DATA."
                val)))
   value)
 
+;;;# Foreign Globals
+
+(defun %foreign-symbol-pointer (name library)
+  "Returns a pointer to a foreign symbol NAME."
+  (flet ((find-it (library)
+           (ignore-errors
+            (make-pointer
+             (jcall 
+              (private-jmethod "com.sun.jna.NativeLibrary" "getSymbolAddress")
+              library name)))))
+    (if (eq library :default)
+        (or (find-it
+             (jstatic "getProcess" "com.sun.jna.NativeLibrary"))
+            ;; The above should find it, but I'm not exactly sure, so
+            ;; let's still do it manually just in case.
+            (loop for lib being the hash-values of *loaded-libraries*
+                  thereis (find-it lib)))
+        (find-it (gethash library *loaded-libraries*)))))
+
 ;;;# Calling Foreign Functions
 
 (defun find-foreign-function (name library)
-  (flet ((find-it (name library)
+  (flet ((find-it (library)
            (ignore-errors
-             (jcall (jmethod "com.sun.jna.NativeLibrary" "getFunction"
-                             "java.lang.String")
-                    library name))))
+            (jcall (jmethod "com.sun.jna.NativeLibrary" "getFunction"
+                            "java.lang.String")
+                   library name))))
     (if (eq library :default)
-        (loop for lib in (hash-table-values *loaded-libraries*)
-              for fn = (find-it name lib)
-              when fn do (return fn))
-        (find-it name (gethash library *loaded-libraries*)))))
+        (or (find-it
+             (jstatic "getProcess" "com.sun.jna.NativeLibrary"))
+            ;; The above should find it, but I'm not exactly sure, so
+            ;; let's still do it manually just in case.
+            (loop for lib being the hash-values of *loaded-libraries*
+                  thereis (find-it lib)))
+        (find-it (gethash library *loaded-libraries*)))))
 
 (defun convert-calling-convention (convention)
   (ecase convention
@@ -573,20 +595,3 @@ interface extends specified as fully qualifed dotted Java names."
 
 (defun native-namestring (pathname)
   (namestring pathname))
-
-;;;# Foreign Globals
-
-(defun %foreign-symbol-pointer (name library)
-  "Returns a pointer to a foreign symbol NAME."
-  (flet ((find-it (name library)
-           (let ((p (ignore-errors
-                      (jcall 
-                       (private-jmethod "com.sun.jna.NativeLibrary" "getSymbolAddress")
-                       library name))))
-             (unless (null p)
-               (make-pointer p)))))
-    (if (eq library :default)
-        (loop for lib in (hash-table-values *loaded-libraries*)
-              for fn = (find-it name lib)
-              when fn do (return fn))
-        (find-it name library))))