cinvoke-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cinvoke-svn] r80 - in trunk/cinvoke: . bindings/java bindings/java/org/


From: will
Subject: [cinvoke-svn] r80 - in trunk/cinvoke: . bindings/java bindings/java/org/cinvoke lib lib/arch
Date: 8 Jul 2006 01:45:24 -0400

Author: will
Date: 2006-07-08 01:45:23 -0400 (Sat, 08 Jul 2006)
New Revision: 80

Modified:
   trunk/cinvoke/README.txt
   trunk/cinvoke/bindings/java/Makefile
   trunk/cinvoke/bindings/java/Test.java
   trunk/cinvoke/bindings/java/org/cinvoke/CBThunk.java
   trunk/cinvoke/bindings/java/org/cinvoke/CInvProxy.java
   trunk/cinvoke/bindings/java/org/cinvoke/CInvoke.java
   trunk/cinvoke/bindings/java/org/cinvoke/NativeInt.java
   trunk/cinvoke/bindings/java/org/cinvoke/NativeLong.java
   trunk/cinvoke/bindings/java/org/cinvoke/NativeLongLong.java
   trunk/cinvoke/bindings/java/org/cinvoke/NativeShort.java
   trunk/cinvoke/bindings/java/org/cinvoke/Natives.java
   trunk/cinvoke/lib/arch/gcc_ppc_osx.c
   trunk/cinvoke/lib/arch/gcc_x64_unix.c
   trunk/cinvoke/lib/arch/gcc_x86_unix.c
   trunk/cinvoke/lib/cinvoke.c
Log:
many bugfixes in the library and the java binding


Modified: trunk/cinvoke/README.txt
===================================================================
--- trunk/cinvoke/README.txt    2006-07-07 19:36:23 UTC (rev 79)
+++ trunk/cinvoke/README.txt    2006-07-08 05:45:23 UTC (rev 80)
@@ -4,10 +4,13 @@
 
 See LICENSE.txt for license info.
 
-The centralized page for C/Invoke development is:
-http://savannah.nongnu.org/projects/cinvoke
+The C/Invoke homepage is:
+http://www.nongnu.org/cinvoke/
 
 Please subscribe to the cinvoke-dev mailing list, more
 information at the above URL.
 
-To build and install the library, try make && make install.
+To build and install the library, run configure.sh to see a list of
+available platforms, then run it again to create the Makefiles.  Then
+enter 'make', then edit the top-level Makefile to change the install path
+if neccessary, and enter 'make install'.

Modified: trunk/cinvoke/bindings/java/Makefile
===================================================================
--- trunk/cinvoke/bindings/java/Makefile        2006-07-07 19:36:23 UTC (rev 79)
+++ trunk/cinvoke/bindings/java/Makefile        2006-07-08 05:45:23 UTC (rev 80)
@@ -18,7 +18,7 @@
        javah org.cinvoke.Natives
 
 clean:
-       rm -f org/cinvoke/*.class *.o *.so
+       rm -f org/cinvoke/*.class *.class *.o *.so
 
 test:
        javac Test.java

Modified: trunk/cinvoke/bindings/java/Test.java
===================================================================
--- trunk/cinvoke/bindings/java/Test.java       2006-07-07 19:36:23 UTC (rev 79)
+++ trunk/cinvoke/bindings/java/Test.java       2006-07-08 05:45:23 UTC (rev 80)
@@ -3,11 +3,18 @@
 class Test {
        interface libc {
                String getpass(String prompt);
+               Ptr strcpy(byte[] dest, String src);
        }
-       
+
        public static void main(String[] args) {
-               libc c = (libc)CInvoke.load("/lib/libc.so.6", libc.class);
+               libc c = (libc)CInvoke.load("libc.so.6", libc.class);
 
                System.out.println("You entered: " + c.getpass("Enter password: 
"));
+
+               byte[] arr = new byte[] { 'a', 'b', 'c', 'd' };
+               c.strcpy(arr, "ef");
+               for (int i = 0; i < arr.length; i++)
+                       System.out.print(arr[i] + " ");
+               System.out.println();
        }
 }

Modified: trunk/cinvoke/bindings/java/org/cinvoke/CBThunk.java
===================================================================
--- trunk/cinvoke/bindings/java/org/cinvoke/CBThunk.java        2006-07-07 
19:36:23 UTC (rev 79)
+++ trunk/cinvoke/bindings/java/org/cinvoke/CBThunk.java        2006-07-08 
05:45:23 UTC (rev 80)
@@ -42,6 +42,6 @@
                throws IllegalAccessException,
                IllegalArgumentException,
                InvocationTargetException {
-               return _method.invoke(_method, params);
+               return _method.invoke(_inst, params);
        }
 }

Modified: trunk/cinvoke/bindings/java/org/cinvoke/CInvProxy.java
===================================================================
--- trunk/cinvoke/bindings/java/org/cinvoke/CInvProxy.java      2006-07-07 
19:36:23 UTC (rev 79)
+++ trunk/cinvoke/bindings/java/org/cinvoke/CInvProxy.java      2006-07-08 
05:45:23 UTC (rev 80)
@@ -64,10 +64,10 @@
                        Natives.NativeMethod meth = 
_n.createNativeMethod(method, _lib,
                                _cc);
                        Class returncls = method.getReturnType();
-                       if (returncls.equals(Void.class))
+                       if (returncls.equals(Void.TYPE))
                                returncls = null;
                        Class realretcls = returncls;
-                       if (returncls.equals(String.class))
+                       if (String.class.equals(returncls))
                                realretcls = Ptr.class;
 
                        Class[] pclasses = method.getParameterTypes();

Modified: trunk/cinvoke/bindings/java/org/cinvoke/CInvoke.java
===================================================================
--- trunk/cinvoke/bindings/java/org/cinvoke/CInvoke.java        2006-07-07 
19:36:23 UTC (rev 79)
+++ trunk/cinvoke/bindings/java/org/cinvoke/CInvoke.java        2006-07-08 
05:45:23 UTC (rev 80)
@@ -155,7 +155,31 @@
         * or address@hidden ptrToArray(Ptr, Class, int) ptrToArray} instead).
         * <li> <i>Any Class</i>: Class types other than those listed above are
         * marshaled as C structures, with each public field of a class
-        * corresponding to a structure member.  Structures cannot contain 
members
+        * corresponding to a structure member.  Unfortunately, there is a 
twist to
+        * declaring classes which are compatible with C structures: in C, the 
order
+        * of the structure members is very important, but Java provides no 
method
+        * of determining the order class fields are declared in.  Thus, in 
order
+        * to correctly marshal C structures, classes should be declared with 
the
+        * names of the fields in the alphabetical order which matches the 
order in
+        * the c struct. For example, to marshal the following structure 
definition:
+        * <pre>
+struct st {
+       short mys;
+       int myi
+       float myfl;
+};
+        * </pre>
+        * the Java class should be declared:
+        * <pre>
+class st {
+       public NativeShort a_mys;
+       public NativeInt b_myi;
+       public float c_myf;
+}
+        * </pre>
+        * The prefixes on the members ensure that the alphabetic order of the
+        * class fields corresponds to the C structure member order.
+        * <p>Structures cannot contain members
         * that are arrays, Strings, or interfaces.  Members which have class 
types
         * are treated as embedded structure values.  Structures can never be
         * passed or returned by value, only inside arrays.  To pass a pointer 
to

Modified: trunk/cinvoke/bindings/java/org/cinvoke/NativeInt.java
===================================================================
--- trunk/cinvoke/bindings/java/org/cinvoke/NativeInt.java      2006-07-07 
19:36:23 UTC (rev 79)
+++ trunk/cinvoke/bindings/java/org/cinvoke/NativeInt.java      2006-07-08 
05:45:23 UTC (rev 80)
@@ -49,7 +49,7 @@
         * @return The marshaled value.  Note that not all the bits in the long
         * value are guaranteed to be used by the called platform.
         */
-       public long longVal() {
+       public long longValue() {
                return _val;
        }
 
@@ -63,6 +63,9 @@
        public int hashCode() {
                return new Long(_val).hashCode();
        }
+       public String toString() {
+               return new Long(_val).toString();
+       }
        private long _val;
 
        private static final long serialVersionUID = 1;

Modified: trunk/cinvoke/bindings/java/org/cinvoke/NativeLong.java
===================================================================
--- trunk/cinvoke/bindings/java/org/cinvoke/NativeLong.java     2006-07-07 
19:36:23 UTC (rev 79)
+++ trunk/cinvoke/bindings/java/org/cinvoke/NativeLong.java     2006-07-08 
05:45:23 UTC (rev 80)
@@ -49,7 +49,7 @@
         * @return The marshaled value.  Note that not all the bits in the long
         * value are guaranteed to be used by the called platform.
         */
-       public long toLong() {
+       public long longValue() {
                return _val;
        }
 
@@ -63,6 +63,9 @@
        public int hashCode() {
                return new Long(_val).hashCode();
        }
+       public String toString() {
+               return new Long(_val).toString();
+       }
        private long _val;
 
        private static final long serialVersionUID = 1;

Modified: trunk/cinvoke/bindings/java/org/cinvoke/NativeLongLong.java
===================================================================
--- trunk/cinvoke/bindings/java/org/cinvoke/NativeLongLong.java 2006-07-07 
19:36:23 UTC (rev 79)
+++ trunk/cinvoke/bindings/java/org/cinvoke/NativeLongLong.java 2006-07-08 
05:45:23 UTC (rev 80)
@@ -64,6 +64,9 @@
        public int hashCode() {
                return new Long(_val).hashCode();
        }
+       public String toString() {
+               return new Long(_val).toString();
+       }
        private long _val;
 
        private static final long serialVersionUID = 1;

Modified: trunk/cinvoke/bindings/java/org/cinvoke/NativeShort.java
===================================================================
--- trunk/cinvoke/bindings/java/org/cinvoke/NativeShort.java    2006-07-07 
19:36:23 UTC (rev 79)
+++ trunk/cinvoke/bindings/java/org/cinvoke/NativeShort.java    2006-07-08 
05:45:23 UTC (rev 80)
@@ -64,6 +64,9 @@
        public int hashCode() {
                return new Integer(_val).hashCode();
        }
+       public String toString() {
+               return new Integer(_val).toString();
+       }
        private int _val;
 
        private static final long serialVersionUID = 1;

Modified: trunk/cinvoke/bindings/java/org/cinvoke/Natives.java
===================================================================
--- trunk/cinvoke/bindings/java/org/cinvoke/Natives.java        2006-07-07 
19:36:23 UTC (rev 79)
+++ trunk/cinvoke/bindings/java/org/cinvoke/Natives.java        2006-07-08 
05:45:23 UTC (rev 80)
@@ -183,17 +183,17 @@
                                return T_PTR;
                        else if (cls.equals(String.class))
                                return T_PTR;
-                       else if (cls.equals(Byte.class))
+                       else if (cls.equals(Byte.class) || 
cls.equals(Byte.TYPE))
                                return T_CHAR;
-                       else if (cls.equals(Short.class))
+                       else if (cls.equals(Short.class) || 
cls.equals(Short.TYPE))
                                return T_JSHORT;
-                       else if (cls.equals(Integer.class))
+                       else if (cls.equals(Integer.class) || 
cls.equals(Integer.TYPE))
                                return T_JINT;
-                       else if (cls.equals(Long.class))
+                       else if (cls.equals(Long.class) || 
cls.equals(Long.TYPE))
                                return T_JLONG;
-                       else if (cls.equals(Float.class))
+                       else if (cls.equals(Float.class) || 
cls.equals(Float.TYPE))
                                return T_FLOAT;
-                       else if (cls.equals(Double.class))
+                       else if (cls.equals(Double.class) || 
cls.equals(Double.TYPE))
                                return T_DOUBLE;
                        else if (cls.equals(NativeShort.class))
                                return T_SHORT;
@@ -256,7 +256,7 @@
                boolean hasret = false;
                int rettype = 0;
                Class retcls = method.getReturnType();
-               if (!retcls.equals(Void.class)) {
+               if (!retcls.equals(Void.TYPE)) {
                        if (retcls.isArray())
                                throw new CInvokeError("returning arrays not 
supported");
                        if (retcls.isInterface())
@@ -302,9 +302,16 @@
                        return ptrToStringUTF8(p);
        }
 
+       private class FieldComparator implements Comparator {
+               public int compare(Object o1, Object o2) {
+                       return 
((Field)o1).getName().compareTo(((Field)o2).getName());
+               }
+       }
+
        private Object unmarshalStruct(long ptr, Class cls) {
                long st = getNativeStruct(cls).st;
                Field[] fields = cls.getFields();
+               Arrays.sort(fields, new FieldComparator());
                Object ret;
 
                try {
@@ -328,7 +335,7 @@
                                val = unmarshalStruct(p, tcls);
                        } else {
                                val = getMemberValueStruct(_ctx, st, ptr,
-                                       fld.getName(), tcls, type);
+                                       fld.getName(), filterType(tcls), type);
                        }
                        try {
                                fld.set(ret, val);
@@ -344,9 +351,9 @@
        private long marshalStruct(long outp, Object s, Class cls) {
                if (s == null)
                        throw new CInvokeError("Invalid null value");
-
                long st = getNativeStruct(cls).st;
                Field[] fields = cls.getFields();
+               Arrays.sort(fields, new FieldComparator());
                for (int i = 0; i < fields.length; i++) {
                        Field fld = fields[i];
                        Class tcls = fld.getType();
@@ -413,7 +420,7 @@
                long r = ret;
                for (int i = 0; i < numels; i++) {
                        Object o = Array.get(val, i);
-                       if (itype == -909)
+                       if (itype == STRUCT_TYPE)
                                r = marshalStruct(r, o, eltype);
                        else
                                r = writeValue(r, marshalBasic(o, eltype), 
itype);
@@ -425,6 +432,7 @@
        public void unmarshalArray(long ptr, Object arr, Class eltype) {
                if (ptr == 0)
                        throw new CInvokeError("Reading array from null 
pointer");
+               eltype = filterType(eltype);
                int numels = Array.getLength(arr);
                
                int itype = gettypeint(eltype, false);
@@ -456,6 +464,7 @@
                                if (st == 0)
                                        fail();
                                Field[] fields = cls.getFields();
+                               Arrays.sort(fields, new FieldComparator());
                                for (int i = 0; i < fields.length; i++) {
                                        Class tcls = fields[i].getType();
                                        if (tcls.isArray()) {
@@ -503,6 +512,23 @@
                return ret;
        }
 
+       private Class filterType(Class t) {
+               if (t.equals(Byte.TYPE))
+                       return Byte.class;
+               else if (t.equals(Short.TYPE))
+                       return Short.class;
+               else if (t.equals(Integer.TYPE))
+                       return Integer.class;
+               else if (t.equals(Long.TYPE))
+                       return Long.class;
+               else if (t.equals(Float.TYPE))
+                       return Float.class;
+               else if (t.equals(Double.TYPE))
+                       return Double.class;
+               else
+                       return t;
+       }
+
        public long createCB(Object obj, Class iface, int cc) {
                Method[] methods = iface.getDeclaredMethods();
                if (methods.length != 1)
@@ -512,9 +538,12 @@
                Method method = methods[0];
                NativeMethod meth = createNativeMethod(method, 0, cc);
 
+               Class[] cls = method.getParameterTypes();
+               for (int i = 0; i < cls.length; i++)
+                       cls[i] = filterType(cls[i]);
+
                long cb = createCallback(_ctx, meth.func, new CBThunk(obj, 
method),
-                               method.getParameterTypes(), meth.types, 
meth.hasret,
-                               meth.rettype);
+                               cls, meth.types, meth.hasret, meth.rettype);
                if (cb == 0)
                        fail();
        

Modified: trunk/cinvoke/lib/arch/gcc_ppc_osx.c
===================================================================
--- trunk/cinvoke/lib/arch/gcc_ppc_osx.c        2006-07-07 19:36:23 UTC (rev 79)
+++ trunk/cinvoke/lib/arch/gcc_ppc_osx.c        2006-07-08 05:45:23 UTC (rev 80)
@@ -41,13 +41,15 @@
 #include <sys/mman.h>
 #include <unistd.h>
 
-void arch_free_errstr(char *str) {}
+void arch_free_errstr(char *str) {
+       free(str);
+}
 
 cinv_status_t arch_library_create(CInvContext *context, const char *path,
        ArchLibrary *library_out) {
        void *dl = dlopen(path, RTLD_LAZY);
        if (!dl) {
-               context_set_error(context, -1, (char*)dlerror(), 0);
+               context_set_error(context, -1, strdup(dlerror()), 1);
                return CINV_ERROR;
        }
                
@@ -59,7 +61,7 @@
        ArchLibrary *library, const char *name, void **entrypoint_out) {
        void *sym = dlsym(library->dl, name);
        if (!sym) {
-               context_set_error(context, -1, (char*)dlerror(), 0);
+               context_set_error(context, -1, strdup(dlerror()), 1);
                return CINV_ERROR;
        }
 
@@ -69,7 +71,7 @@
 }
 cinv_status_t arch_library_delete(CInvContext *context, ArchLibrary *library) {
        if (dlclose(library->dl)) {
-               context_set_error(context, -1, (char*)dlerror(), 0);
+               context_set_error(context, -1, strdup(dlerror()), 1);
                return CINV_ERROR;
        }
 

Modified: trunk/cinvoke/lib/arch/gcc_x64_unix.c
===================================================================
--- trunk/cinvoke/lib/arch/gcc_x64_unix.c       2006-07-07 19:36:23 UTC (rev 79)
+++ trunk/cinvoke/lib/arch/gcc_x64_unix.c       2006-07-08 05:45:23 UTC (rev 80)
@@ -41,13 +41,15 @@
 #include <sys/mman.h>
 #include <unistd.h>
 
-void arch_free_errstr(char *str) {}
+void arch_free_errstr(char *str) {
+       free(str);
+}
 
 cinv_status_t arch_library_create(CInvContext *context, const char *path,
        ArchLibrary *library_out) {
        void *dl = dlopen(path, RTLD_LAZY);
        if (!dl) {
-               context_set_error(context, -1, (char*)dlerror(), 0);
+               context_set_error(context, -1, strdup(dlerror()), 1);
                return CINV_ERROR;
        }
                
@@ -59,7 +61,7 @@
        ArchLibrary *library, const char *name, void **entrypoint_out) {
        void *sym = dlsym(library->dl, name);
        if (!sym) {
-               context_set_error(context, -1, (char*)dlerror(), 0);
+               context_set_error(context, -1, strdup(dlerror()), 1);
                return CINV_ERROR;
        }
 
@@ -69,7 +71,7 @@
 }
 cinv_status_t arch_library_delete(CInvContext *context, ArchLibrary *library) {
        if (dlclose(library->dl)) {
-               context_set_error(context, -1, (char*)dlerror(), 0);
+               context_set_error(context, -1, strdup(dlerror()), 1);
                return CINV_ERROR;
        }
 

Modified: trunk/cinvoke/lib/arch/gcc_x86_unix.c
===================================================================
--- trunk/cinvoke/lib/arch/gcc_x86_unix.c       2006-07-07 19:36:23 UTC (rev 79)
+++ trunk/cinvoke/lib/arch/gcc_x86_unix.c       2006-07-08 05:45:23 UTC (rev 80)
@@ -41,13 +41,15 @@
 #include <sys/mman.h>
 #include <unistd.h>
 
-void arch_free_errstr(char *str) {}
+void arch_free_errstr(char *str) {
+       free(str);
+}
 
 cinv_status_t arch_library_create(CInvContext *context, const char *path,
        ArchLibrary *library_out) {
        void *dl = dlopen(path, RTLD_LAZY);
        if (!dl) {
-               context_set_error(context, -1, (char*)dlerror(), 0);
+               context_set_error(context, -1, strdup(dlerror()), 1);
                return CINV_ERROR;
        }
                
@@ -59,7 +61,7 @@
        ArchLibrary *library, const char *name, void **entrypoint_out) {
        void *sym = dlsym(library->dl, name);
        if (!sym) {
-               context_set_error(context, -1, (char*)dlerror(), 0);
+               context_set_error(context, -1, strdup(dlerror()), 1);
                return CINV_ERROR;
        }
 
@@ -69,7 +71,7 @@
 }
 cinv_status_t arch_library_delete(CInvContext *context, ArchLibrary *library) {
        if (dlclose(library->dl)) {
-               context_set_error(context, -1, (char*)dlerror(), 0);
+               context_set_error(context, -1, strdup(dlerror()), 1);
                return CINV_ERROR;
        }
 

Modified: trunk/cinvoke/lib/cinvoke.c
===================================================================
--- trunk/cinvoke/lib/cinvoke.c 2006-07-07 19:36:23 UTC (rev 79)
+++ trunk/cinvoke/lib/cinvoke.c 2006-07-08 05:45:23 UTC (rev 80)
@@ -117,12 +117,18 @@
 cinv_status_t parse_type(char fmt, cinv_type_t *typeout) {
        int i;
        char lower = tolower(fmt);
-       if (lower == '2')
-               return CINV_T_2BYTE;
-       if (lower == '4')
-               return CINV_T_4BYTE;
-       if (lower == '8')
-               return CINV_T_8BYTE;
+       if (lower == '2') {
+               *typeout = CINV_T_2BYTE;
+               return CINV_SUCCESS;
+       }
+       if (lower == '4') {
+               *typeout = CINV_T_4BYTE;
+               return CINV_SUCCESS;
+       }
+       if (lower == '8') {
+               *typeout = CINV_T_8BYTE;
+               return CINV_SUCCESS;
+       }
        for (i = 0; i < CINV_NUM_TYPES; i++) {
                if (_typeformats[i] == lower) {
                        *typeout = (cinv_type_t)i;
@@ -150,11 +156,10 @@
        sizefunc func;
        *stackalign = 1;
        *structalign = 1;
+       if (type < 0 || type > CINV_NUM_TYPES - 1)
+               fprintf(stderr, "cinvoke developer error: unknown type %d\n", 
type);
        func = _sizefuncs[type];
-       if (!func)
-               fprintf(stderr, "cinvoke developer error: unknown type\n");
-       else
-               func(stacksize, structsize, stackalign, structalign);
+       func(stacksize, structsize, stackalign, structalign);
 }
 
 typedef void (*pullfunc)(ArchRetValue*, void*);
@@ -171,11 +176,11 @@
 };
 
 void pull_value(ArchRetValue *val, cinv_type_t type, void *valout) {
-       pullfunc func = _pullfuncs[type];
-       if (!func)
-               fprintf(stderr, "cinvoke developer error: unknown type\n");
-       else
-               func(val, valout);
+       pullfunc func;
+       if (type < 0 || type > CINV_NUM_TYPES - 1)
+               fprintf(stderr, "cinvoke developer error: unknown type %d\n", 
type);
+       func = _pullfuncs[type];
+       func(val, valout);
 }
 
 void set_value(ArchRetValue *archval, cinv_type_t type, void *val) {
@@ -205,7 +210,7 @@
                arch_setval_ptr(archval, *(void **)val);
                break;
        default:
-               fprintf(stderr, "cinvoke developer error: unknown type\n");
+               fprintf(stderr, "cinvoke developer error: unknown type %d\n", 
type);
                break;
        }
 }





reply via email to

[Prev in Thread] Current Thread [Next in Thread]