cinvoke-svn
[Top][All Lists]
Advanced

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

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


From: will
Subject: [cinvoke-svn] r68 - in trunk/cinvoke/bindings/java: . org/cinvoke
Date: 4 Jul 2006 00:17:50 -0400

Author: will
Date: 2006-07-04 00:17:40 -0400 (Tue, 04 Jul 2006)
New Revision: 68

Added:
   trunk/cinvoke/bindings/java/org/cinvoke/CBThunk.java
   trunk/cinvoke/bindings/java/org/cinvoke/CInvProxy.java
   trunk/cinvoke/bindings/java/org/cinvoke/CInvokeException.java
   trunk/cinvoke/bindings/java/org/cinvoke/Natives.java
   trunk/cinvoke/bindings/java/org_cinvoke_Natives.cpp
   trunk/cinvoke/bindings/java/org_cinvoke_Natives.h
Removed:
   trunk/cinvoke/bindings/java/org_cinvoke_CInvoke.cpp
   trunk/cinvoke/bindings/java/org_cinvoke_CInvoke.h
Modified:
   trunk/cinvoke/bindings/java/Makefile
   trunk/cinvoke/bindings/java/Test.java
   trunk/cinvoke/bindings/java/org/cinvoke/CInvoke.java
Log:
java binding passes first test


Modified: trunk/cinvoke/bindings/java/Makefile
===================================================================
--- trunk/cinvoke/bindings/java/Makefile        2006-07-03 16:08:41 UTC (rev 67)
+++ trunk/cinvoke/bindings/java/Makefile        2006-07-04 04:17:40 UTC (rev 68)
@@ -3,7 +3,7 @@
 all: $(TARGET)
        javac org/cinvoke/*.java
 
-SRCS = org_cinvoke_CInvoke.cpp
+SRCS = org_cinvoke_Natives.cpp
 OBJS = $(SRCS:.cpp=.o)
 
 $(TARGET): $(OBJS)
@@ -12,10 +12,10 @@
 .cpp.o:
        g++ -Wall -Werror -c $<
 
-org_cinvoke_CInvoke.o: org_cinvoke_CInvoke.cpp org_cinvoke_CInvoke.h
+org_cinvoke_Natives.o: org_cinvoke_Natives.cpp org_cinvoke_Natives.h
 
 header:
-       javah org.cinvoke.CInvoke
+       javah org.cinvoke.Natives
 
 clean:
        rm -f org/cinvoke/*.class *.o *.so

Modified: trunk/cinvoke/bindings/java/Test.java
===================================================================
--- trunk/cinvoke/bindings/java/Test.java       2006-07-03 16:08:41 UTC (rev 67)
+++ trunk/cinvoke/bindings/java/Test.java       2006-07-04 04:17:40 UTC (rev 68)
@@ -6,8 +6,12 @@
        }
        
        public static void main(String[] args) {
-               libc c = (libc)CInvoke.load("/lib/libc.so.6", libc.class);
+               try {
+                       libc c = (libc)CInvoke.load("/lib/libc.so.6", 
libc.class);
 
-               System.out.println("You entered: " + c.getpass("Enter password: 
"));
+                       System.out.println("You entered: " + c.getpass("Enter 
password: "));
+               } catch (CInvokeException ex) {
+                       System.out.println(ex);
+               }
        }
 }

Added: trunk/cinvoke/bindings/java/org/cinvoke/CBThunk.java
===================================================================
--- trunk/cinvoke/bindings/java/org/cinvoke/CBThunk.java                        
        (rev 0)
+++ trunk/cinvoke/bindings/java/org/cinvoke/CBThunk.java        2006-07-04 
04:17:40 UTC (rev 68)
@@ -0,0 +1,13 @@
+package org.cinvoke;
+
+class CBThunk {
+       public CBThunk() {
+               // XXX
+       }
+
+       public Object cbfunc(Object[] params) {
+               // XXX
+               return null;
+       }
+}
+

Added: trunk/cinvoke/bindings/java/org/cinvoke/CInvProxy.java
===================================================================
--- trunk/cinvoke/bindings/java/org/cinvoke/CInvProxy.java                      
        (rev 0)
+++ trunk/cinvoke/bindings/java/org/cinvoke/CInvProxy.java      2006-07-04 
04:17:40 UTC (rev 68)
@@ -0,0 +1,372 @@
+package org.cinvoke;
+import java.lang.reflect.*;
+import java.util.*;
+
+class CInvProxy implements InvocationHandler {
+       private long _ctx = 0;
+       private long _lib = 0;
+       private int _cc;
+       private int _encoding;
+       private HashMap _functions;
+       private HashMap _structs;
+
+       public CInvProxy(String libname, int cc, int encoding)
+               throws CInvokeException {
+               _ctx = Natives.createContext();
+               if (_ctx == 0) throw new OutOfMemoryError();
+               _lib = Natives.createLibrary(_ctx, libname);
+               if (_lib == 0) {
+                       try {
+                               fail();
+                       } finally {
+                               Natives.deleteContext(_ctx);
+                               _ctx = 0;
+                       }
+               }
+
+               _cc = cc;
+               _encoding = encoding;
+               _functions = new HashMap();
+               _structs = new HashMap();
+       }
+
+       protected void finalize() throws Throwable {
+               close();
+       }
+
+       public void close() {
+               for (Iterator i = _functions.values().iterator(); i.hasNext();)
+                       ((NativeMethod)i.next()).close();
+               for (Iterator i = _structs.values().iterator(); i.hasNext();) 
+                       ((NativeStruct)i.next()).close();
+               if (_ctx != 0) {
+                       if (_lib != 0) {
+                               Natives.deleteLibrary(_ctx, _lib);
+                               _lib = 0;
+                       }
+                       Natives.deleteContext(_ctx);
+                       _ctx = 0;
+               }
+       }
+
+       private void fail() throws CInvokeException {
+               throw new CInvokeException(Natives.getError(_ctx));
+       }
+
+       class NativeStruct {
+               public NativeStruct(long ctx, long st) {
+                       _ctx = ctx;
+                       this.st = st;
+               }
+
+               public void close() {
+                       if (st != 0) {
+                               Natives.deleteStruct(_ctx, st);
+                               st = 0;
+                       }
+               }
+
+               public long st;
+               private long _ctx;
+       }
+
+       class NativeMethod {
+               public NativeMethod(long ctx, long func, long ep, int[] types,
+                       boolean hasret, int rettype) {
+                       this.func = func;
+                       this.ep = ep;
+                       this.types = types;
+                       this.hasret = hasret;
+                       this.rettype = rettype;
+                       _ctx = ctx;
+               }
+
+               public void close() {
+                       if (func != 0) {
+                               Natives.deleteFunction(_ctx, func);
+                               func = 0;
+                       }
+               }
+
+               private long _ctx;
+               public long func;
+               public long ep;
+               public int[] types;
+               public boolean hasret;
+               public int rettype;
+       }
+       
+       public Object invoke(Object proxy, Method method, Object[] args)
+               throws Throwable {
+               NativeMethod meth = (NativeMethod)_functions.get(method);
+               if (meth == null) {
+                       meth = createNative(method);
+                       _functions.put(method, meth);
+               }
+               Class returncls = method.getReturnType();
+               if (returncls.equals(Void.TYPE))
+                       returncls = null;
+               Class realretcls = returncls;
+               if (returncls.equals(String.class))
+                       realretcls = Ptr.class;
+
+               Object[] params = marshalParams(method, args);
+               try {
+                       Object ret;
+                       try {
+                               ret = Natives.invokeFunction(_ctx, meth.func, 
meth.ep,
+                                       params, meth.types, realretcls, 
meth.rettype);
+                       } catch (Exception ex) {
+                               if (ex.getMessage().equals("invoke failed")) {
+                                       fail();
+                                       return null;
+                               } else
+                                       throw ex;
+                       }
+
+                       if (returncls != null)
+                               return unmarshalReturnValue(ret, returncls);
+                       else
+                               return null;
+               } finally {
+                       freeParams(method, params);
+               }
+       }
+
+       private int gettypeint(Class cls, boolean thrw) throws CInvokeException 
{
+               if (cls.isArray()) {
+                       if (cls.isInterface())
+                               throw new CInvokeException("Arrays of callbacks 
not supported");
+                       return Natives.T_PTR;
+               } else {
+                       if (cls.isInterface())
+                               return Natives.T_PTR;
+                       else if (cls.equals(String.class))
+                               return Natives.T_PTR;
+                       else if (cls.equals(Byte.class))
+                               return Natives.T_CHAR;
+                       else if (cls.equals(Short.class))
+                               return Natives.T_JSHORT;
+                       else if (cls.equals(Integer.class))
+                               return Natives.T_JINT;
+                       else if (cls.equals(Long.class))
+                               return Natives.T_JLONG;
+                       else if (cls.equals(Float.class))
+                               return Natives.T_FLOAT;
+                       else if (cls.equals(Double.class))
+                               return Natives.T_DOUBLE;
+                       else if (cls.equals(NativeShort.class))
+                               return Natives.T_SHORT;
+                       else if (cls.equals(NativeInt.class))
+                               return Natives.T_INT;
+                       else if (cls.equals(NativeLong.class))
+                               return Natives.T_LONG;
+                       else if (cls.equals(NativeLongLong.class))
+                               return Natives.T_EXTRALONG;
+                       else if (cls.equals(Ptr.class))
+                               return Natives.T_PTR;
+                       else {
+                               if (thrw)
+                                       throw new CInvokeException(
+                                       "Passing or returning structures by 
value not supported");
+                               else
+                                       return -999;
+                       }
+               }
+       }
+       private char gettypechar(int type) {
+               switch (type) {
+               case Natives.T_JSHORT:
+                       return '2';
+               case Natives.T_JINT:
+                       return '4';
+               case Natives.T_JLONG:
+                       return '8';
+               case Natives.T_CHAR:
+                       return 'c';
+               case Natives.T_SHORT:
+                       return 's';
+               case Natives.T_INT:
+                       return 'i';
+               case Natives.T_LONG:
+                       return 'l';
+               case Natives.T_EXTRALONG:
+                       return 'e';
+               case Natives.T_FLOAT:
+                       return 'f';
+               case Natives.T_DOUBLE:
+                       return 'd';
+               case Natives.T_PTR:
+                       return 'p';
+               default:
+                       throw new UnknownError("unknown type");
+               }
+       }
+
+       private NativeMethod createNative(Method method) 
+               throws CInvokeException {
+               long ep = Natives.loadEPLibrary(_ctx, _lib, method.getName());
+               if (ep == 0)
+                       fail();
+               StringBuffer retfmt = new StringBuffer();
+               StringBuffer parmfmt = new StringBuffer();
+               boolean hasret = false;
+               int rettype = 0;
+               Class retcls = method.getReturnType();
+               if (!retcls.equals(Void.TYPE)) {
+                       if (retcls.isArray())
+                               throw new CInvokeException("returning arrays 
not supported");
+                       if (retcls.isInterface())
+                               throw new CInvokeException("returning callbacks 
not supported");
+                       hasret = true;
+                       rettype = gettypeint(retcls, true);
+                       retfmt.append(gettypechar(rettype));
+               }
+
+               Class[] pclasses = method.getParameterTypes();
+               int[] types = new int[pclasses.length];
+               for (int i = 0; i < types.length; i++) {
+                       types[i] = gettypeint(pclasses[i], true);
+                       parmfmt.append(gettypechar(types[i]));
+               }
+
+               long func = Natives.createFunction(_ctx, _cc, retfmt.toString(),
+                       parmfmt.toString());
+               if (func == 0)
+                       fail();
+               return new NativeMethod(_ctx, func, ep, types, hasret, rettype);
+       }
+
+       private Object[] marshalParams(Method method, Object[] rawparams) {
+               Object[] ret = new Object[rawparams.length];
+               Class[] pclasses = method.getParameterTypes();
+
+               for (int i = 0; i < ret.length; i++) {
+                       if (pclasses[i].isArray()) {
+                               // XXX
+                       } else if (pclasses[i].isInterface()) {
+                               // XXX create callback
+                       } else if (pclasses[i].equals(String.class)) {
+                               long l;
+                               if (_encoding == CInvoke.ENC.UNICODE)
+                                       l = 
Natives.stringToPtrUnicode((String)rawparams[i]);
+                               else 
+                                       l = 
Natives.stringToPtrUTF8((String)rawparams[i]);
+                               if (l == 0)
+                                       throw new OutOfMemoryError();
+                               ret[i] = new Ptr(l);
+                       } else
+                               ret[i] = rawparams[i];
+               }
+
+               return ret;
+       }
+
+       private void freeParams(Method method, Object[] params) {
+               Class[] pclasses = method.getParameterTypes();
+               for (int i = 0; i < params.length; i++) {
+                       if (pclasses[i].isArray() || 
pclasses[i].equals(String.class)) {
+                               Natives.free(((Ptr)params[i]).longValue());
+                       } else if (pclasses[i].isInterface()) {
+                               // XXX delete callback
+                       }
+               }
+       }
+
+       private Object unmarshalReturnValue(Object ret, Class type) {
+               if (type.equals(String.class)) {
+                       if (_encoding == CInvoke.ENC.UNICODE)
+                               return 
Natives.ptrToStringUnicode(((Ptr)ret).longValue(), -1);
+                       else
+                               return 
Natives.ptrToStringUTF8(((Ptr)ret).longValue());
+               } else
+                       return ret;
+       }
+
+       private void unmarshalArray(Ptr ptr, Object[] arr, Class type) {
+               // XXX
+       }
+
+       private NativeStruct getNativeStruct(Class cls) throws CInvokeException 
{
+               NativeStruct ret = (NativeStruct)_structs.get(cls);
+
+               if (ret == null) {
+                       long st = Natives.createStruct(_ctx);
+                       if (st == 0)
+                               fail();
+                       Field[] fields = cls.getFields();
+                       for (int i = 0; i < fields.length; i++) {
+                               Class tcls = fields[i].getType();
+                               if (tcls.isArray()) {
+                                       Natives.deleteStruct(_ctx, st);
+                                       throw new CInvokeException(
+                                               "Array structure members not 
supported");
+                               }
+                               if (tcls.isInterface()) {
+                                       Natives.deleteStruct(_ctx, st);
+                                       throw new CInvokeException(
+                                               "Callback structure members not 
supported");
+                               }
+                               int type = gettypeint(tcls, false);
+                               if (type == -999) {
+                                       if (Natives.addStructMemberStruct(_ctx, 
st,
+                                               fields[i].getName(),
+                                               getNativeStruct(tcls).st) == 0) 
{
+                                               try { fail(); }
+                                               finally { 
Natives.deleteStruct(_ctx, st); }
+                                       }
+                               } else {
+                                       if (Natives.addValueMemberStruct(_ctx, 
st,
+                                               fields[i].getName(), type) == 
0) {
+                                               try { fail(); }
+                                               finally { 
Natives.deleteStruct(_ctx, st); }
+                                       }
+                               }
+                       }
+                       if (Natives.finishStruct(_ctx, st) == 0) {
+                               try { fail(); } finally { 
Natives.deleteStruct(_ctx, st); }
+                       }
+                       ret = new NativeStruct(_ctx, st);
+                       _structs.put(cls, ret);
+               }
+               
+               return ret;
+       }
+
+       public String ptrToString(Ptr ptr, int numchars) {
+               if (_encoding == CInvoke.ENC.UNICODE)
+                       return Natives.ptrToStringUnicode(ptr.longValue(), 
numchars);
+               else
+                       return Natives.ptrToStringUTF8(ptr.longValue());
+       }
+       public Object[] ptrToArray(Ptr ptr, Class type, int num) 
+               throws CInvokeException {
+               if (type.isInterface())
+                       throw new CInvokeException("Arrays of callbacks not 
supported");
+
+               Object[] ret = (Object[])Array.newInstance(type, num);
+
+               unmarshalArray(ptr, ret, type);
+
+               return ret;
+       }
+       public int sizeof(Class type) throws CInvokeException {
+               int itype = gettypeint(type, false);
+               if (itype == -999) {
+                       int ret = Natives.sizeStruct(_ctx, 
getNativeStruct(type).st);
+                       if (ret == -1)
+                               fail();
+                       return ret;
+               } else
+                       return Natives.sizeofBasic(itype);
+       }
+       public int sizeof(Object obj) throws CInvokeException {
+               Class cls = obj.getClass();
+               if (cls.isArray()) {
+                       if (cls.isInterface())
+                               throw new CInvokeException("Arrays of callbacks 
not supported");
+                       return Array.getLength(obj) * 
sizeof(cls.getComponentType());
+               } else
+                       return sizeof(cls);
+       }
+}

Modified: trunk/cinvoke/bindings/java/org/cinvoke/CInvoke.java
===================================================================
--- trunk/cinvoke/bindings/java/org/cinvoke/CInvoke.java        2006-07-03 
16:08:41 UTC (rev 67)
+++ trunk/cinvoke/bindings/java/org/cinvoke/CInvoke.java        2006-07-04 
04:17:40 UTC (rev 68)
@@ -1,57 +1,7 @@
 package org.cinvoke;
+import java.lang.reflect.*;
 
 public final class CInvoke {
-       static {
-               System.loadLibrary("cinvoke_java");
-       }
-
-       private static native long createContext();
-       private static native String getError(long ctx);
-       private static native int deleteContext(long ctx);
-       private static native long createLibrary(long ctx, String path);
-       private static native long loadEPLibrary(long ctx, long lib, String 
path);
-       private static native int deleteLibrary(long ctx, long lib);
-       private static native long createFunction(long ctx, int cc, String 
retfmt,
-               String paramfmt);
-       private static native Object invokeFunction(long ctx, long func, long 
ep,
-               Object[] params, int[] types, Class retcls, int rettype);
-       private static native int deleteFunction(long ctx, long func);
-       private static native long createStruct(long ctx);
-       private static native int addValueMemberStruct(long ctx, long strct,
-               String name, int type);
-       private static native int addStructMemberStruct(long ctx, long strct,
-               String name, long type);
-       private static native long alloc(int sz);
-       private static native void free(long m);
-       private static native int sizeofBasic(int type);
-       private static native void writeValue(long m, Object val, int type);
-       private static native Object readValue(long m, Class cls, int type);
-       private static native int setMemberValueStruct(long ctx, long strct,
-               long m, String name, Object val, int type);
-       private static native Object getMemberValueStruct(long ctx, long strct,
-               long m, String name, Class cls, int type);
-       private static native int finishStruct(long ctx, long strct);
-       private static native int sizeStruct(long ctx, long strct);
-       private static native int deleteStruct(long ctx, long strct);
-       private static native long createCallback(long ctx, long func,
-               CBThunk cbcls, Class[] pclasses, int[] ptypes, boolean hasret,
-               int rettype);
-       private static native long getEPCallback(long ctx, long cb);
-       private static native int deleteCallback(long ctx, long cb); 
-       private static native String ptrToStringUTF8(long ptr);
-       private static native String ptrToStringUnicode(long ptr, int numchars);
-
-       private class CBThunk {
-               public CBThunk() {
-                       // XXX
-               }
-
-               public Object cbfunc(Object[] params) {
-                       // XXX
-                       return null;
-               }
-       }
-
        public class CC {
                public static final int DEFAULT = -1;
                public static final int CDECL = 0;
@@ -62,37 +12,19 @@
                public static final int UTF8 = 0;
                public static final int UNICODE = 1;
        }
-       private static final int T_JSHORT = -1;
-       private static final int T_JINT = -2;
-       private static final int T_JLONG = -3;
 
-       public static Object load(String libname, Class iface) {
+       public static Object load(String libname, Class iface)
+               throws CInvokeException {
                return load(libname, iface, CC.DEFAULT);
        }
-       public static Object load(String libname, Class iface, int callconv) {
+       public static Object load(String libname, Class iface, int callconv)
+               throws CInvokeException {
                return load(libname, iface, CC.DEFAULT, ENC.UTF8);
        }
        public static Object load(String libname, Class iface, int callconv,
-               int encoding) {
-               // XXX
-               return null;
+               int encoding)
+               throws CInvokeException {
+               return Proxy.newProxyInstance(iface.getClassLoader(),
+                       new Class[] { iface }, new CInvProxy(libname, callconv, 
encoding));
        }
-       public static String ptrToStringUTF8(Ptr ptr) {
-               return ptrToStringUTF8(ptr.longValue());
-       }
-       public static String ptrToStringUnicode(Ptr ptr, int numchars) {
-               return ptrToStringUnicode(ptr.longValue(), numchars);
-       }
-       public static Object[] ptrToArray(Ptr ptr, Class type, int num) {
-               // XXX
-               return null;
-       }
-       public static int sizeof(Class type) {
-               // XXX
-               return 0;
-       }
-       public static int sizeof(Object obj) {
-               // XXX
-               return 0;
-       }
 }

Added: trunk/cinvoke/bindings/java/org/cinvoke/CInvokeException.java
===================================================================
--- trunk/cinvoke/bindings/java/org/cinvoke/CInvokeException.java               
                (rev 0)
+++ trunk/cinvoke/bindings/java/org/cinvoke/CInvokeException.java       
2006-07-04 04:17:40 UTC (rev 68)
@@ -0,0 +1,8 @@
+package org.cinvoke;
+
+public class CInvokeException extends Exception {
+       public CInvokeException(String msg) {
+               super(msg);
+       }
+       private static final long serialVersionUID = 1;
+}

Added: trunk/cinvoke/bindings/java/org/cinvoke/Natives.java
===================================================================
--- trunk/cinvoke/bindings/java/org/cinvoke/Natives.java                        
        (rev 0)
+++ trunk/cinvoke/bindings/java/org/cinvoke/Natives.java        2006-07-04 
04:17:40 UTC (rev 68)
@@ -0,0 +1,57 @@
+package org.cinvoke;
+
+class Natives {
+       static {
+               System.loadLibrary("cinvoke_java");
+       }
+
+       public static native long createContext();
+       public static native String getError(long ctx);
+       public static native int deleteContext(long ctx);
+       public static native long createLibrary(long ctx, String path);
+       public static native long loadEPLibrary(long ctx, long lib, String 
name);
+       public static native int deleteLibrary(long ctx, long lib);
+       public static native long createFunction(long ctx, int cc, String 
retfmt,
+               String paramfmt);
+       public static native Object invokeFunction(long ctx, long func, long ep,
+               Object[] params, int[] types, Class retcls, int rettype);
+       public static native int deleteFunction(long ctx, long func);
+       public static native long createStruct(long ctx);
+       public static native int addValueMemberStruct(long ctx, long strct,
+               String name, int type);
+       public static native int addStructMemberStruct(long ctx, long strct,
+               String name, long type);
+       public static native long alloc(int sz);
+       public static native void free(long m);
+       public static native int sizeofBasic(int type);
+       public static native void writeValue(long m, Object val, int type);
+       public static native Object readValue(long m, Class cls, int type);
+       public static native int setMemberValueStruct(long ctx, long strct,
+               long m, String name, Object val, int type);
+       public static native Object getMemberValueStruct(long ctx, long strct,
+               long m, String name, Class cls, int type);
+       public static native int finishStruct(long ctx, long strct);
+       public static native int sizeStruct(long ctx, long strct);
+       public static native int deleteStruct(long ctx, long strct);
+       public static native long createCallback(long ctx, long func,
+               CBThunk cbcls, Class[] pclasses, int[] ptypes, boolean hasret,
+               int rettype);
+       public static native long getEPCallback(long ctx, long cb);
+       public static native int deleteCallback(long ctx, long cb); 
+       public static native String ptrToStringUTF8(long ptr);
+       public static native String ptrToStringUnicode(long ptr, int numchars);
+       public static native long stringToPtrUTF8(String str);
+       public static native long stringToPtrUnicode(String str);
+
+       public static final int T_JSHORT = -1;
+       public static final int T_JINT = -2;
+       public static final int T_JLONG = -3;
+       public static final int T_CHAR = 0;
+       public static final int T_SHORT = 1;
+       public static final int T_INT = 2;
+       public static final int T_LONG = 3;
+       public static final int T_EXTRALONG = 4;
+       public static final int T_FLOAT = 5;
+       public static final int T_DOUBLE = 6;
+       public static final int T_PTR = 7;
+}

Deleted: trunk/cinvoke/bindings/java/org_cinvoke_CInvoke.cpp
===================================================================
--- trunk/cinvoke/bindings/java/org_cinvoke_CInvoke.cpp 2006-07-03 16:08:41 UTC 
(rev 67)
+++ trunk/cinvoke/bindings/java/org_cinvoke_CInvoke.cpp 2006-07-04 04:17:40 UTC 
(rev 68)
@@ -1,597 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <cinvoke.h>
-#include "org_cinvoke_CInvoke.h"
-
-#define T_JSHORT -1
-#define T_JINT -2
-#define T_JLONG -3
-
-JNIEXPORT jlong JNICALL Java_org_cinvoke_CInvoke_createContext(
-       JNIEnv *env, jclass) {
-       return (jlong)cinv_context_create();
-}
-JNIEXPORT jstring JNICALL Java_org_cinvoke_CInvoke_getError(
-       JNIEnv *env, jclass, jlong c) {
-       CInvContext *ctx = (CInvContext *)c;
-       return env->NewStringUTF(cinv_context_geterrormsg(ctx));
-}
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_deleteContext(
-       JNIEnv *env, jclass, jlong c) {
-       CInvContext *ctx = (CInvContext *)c;
-       return cinv_context_delete(ctx);
-}
-JNIEXPORT jlong JNICALL Java_org_cinvoke_CInvoke_createLibrary(
-       JNIEnv *env, jclass, jlong c, jstring libname) {
-       CInvContext *ctx = (CInvContext *)c;
-       const char *chrs = env->GetStringUTFChars(libname, NULL);
-       if (chrs == NULL) return 0;
-
-       jlong ret = (jlong)cinv_library_create(ctx, chrs);
-
-       env->ReleaseStringUTFChars(libname, chrs);
-       return ret;
-}
-JNIEXPORT jlong JNICALL Java_org_cinvoke_CInvoke_loadEPLibrary(
-       JNIEnv *env, jclass, jlong c, jlong l, jstring name) {
-       CInvContext *ctx = (CInvContext *)c;
-       CInvLibrary *lib = (CInvLibrary *)l;
-       const char *chrs = env->GetStringUTFChars(name, NULL);
-       if (chrs == NULL) return 0;
-
-       jlong ret = (jlong)cinv_library_load_entrypoint(ctx, lib, chrs);
-
-       env->ReleaseStringUTFChars(name, chrs);
-       return ret;
-}
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_deleteLibrary(
-       JNIEnv *env, jclass, jlong c, jlong l) {
-       CInvContext *ctx = (CInvContext *)c;
-       CInvLibrary *lib = (CInvLibrary *)l;
-       
-       return cinv_library_delete(ctx, lib);
-}
-JNIEXPORT jlong JNICALL Java_org_cinvoke_CInvoke_createFunction(
-       JNIEnv *env, jclass, jlong c, jint cc, jstring retfmt, jstring parmfmt) 
{
-       CInvContext *ctx = (CInvContext *)c;
-       const char *parmchrs = env->GetStringUTFChars(parmfmt, NULL);
-       if (parmchrs == NULL) return 0;
-       const char *retchrs = env->GetStringUTFChars(retfmt, NULL);
-       if (retchrs == NULL) return 0;
-
-       if (cc == -1)
-               cc = CINV_CC_DEFAULT;
-
-       jlong ret = (jlong)cinv_function_create(ctx, (cinv_callconv_t)cc,
-               retchrs, parmchrs);
-
-       env->ReleaseStringUTFChars(parmfmt, parmchrs);
-       env->ReleaseStringUTFChars(retfmt, retchrs);
-       return ret;
-}
-void *alloc(int type) {
-       return malloc(Java_org_cinvoke_CInvoke_sizeofBasic(NULL, NULL, type));
-}
-void fail(JNIEnv *env) {
-       jclass ex = env->FindClass("java/lang/Exception");
-       env->ThrowNew(ex, "invoke failed");
-}
-JNIEXPORT jobject JNICALL Java_org_cinvoke_CInvoke_invokeFunction(
-       JNIEnv *env, jclass, jlong c, jlong f, jlong e, jobjectArray params,
-       jintArray types, jclass retcls, jint rettype) {
-       CInvContext *ctx = (CInvContext *)c;
-       CInvFunction *func = (CInvFunction *)f;
-       void *ep = (void *)e;
-       jobject ret = NULL;
-       void *retp = NULL;
-       void **pp = NULL;
-       jint *tarr = NULL;
-       int numparms = 0, np = env->GetArrayLength(params);
-       if (env->ExceptionOccurred()) {
-               fail(env);
-               goto out;
-       }
-       tarr = env->GetIntArrayElements(types, NULL);
-       if (tarr == NULL) {
-               fail(env);
-               goto out;
-       }
-       
-       if (retcls) {
-               retp = alloc(rettype);
-               if (!retp) {
-                       fail(env);
-                       goto out;
-               }
-       }
-       pp = (void **)malloc(sizeof(void*) * np);
-       if (!pp) {
-               fail(env);
-               goto out;
-       }
-       for (int i = 0; i < np; i++)
-               pp[i] = NULL;
-       for (int i = 0; i < np; i++) {
-               jobject parm = env->GetObjectArrayElement(params, i);
-               if (env->ExceptionOccurred()) {
-                       fail(env);
-                       goto out;
-               }
-               pp[i] = alloc(tarr[i]);
-               if (!pp[i]) {
-                       fail(env);
-                       goto out;
-               }
-               Java_org_cinvoke_CInvoke_writeValue(env, NULL, (jlong)pp[i], 
parm,
-                       tarr[i]);
-       }
-       numparms = np;
-       
-       if (!cinv_function_invoke(ctx, func, ep, retp, pp)) {
-               fail(env);
-               goto out;
-       }
-       
-       if (retp)
-               ret = Java_org_cinvoke_CInvoke_readValue(env, NULL,
-                       (jlong)retp, retcls, rettype);
-out:
-       if (tarr != NULL)
-               env->ReleaseIntArrayElements(types, tarr, 0);
-       for (int i = 0; i < numparms; i++)
-               free(pp[i]);
-       free(pp);
-       free(retp);
-       return ret;
-}
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_deleteFunction(
-       JNIEnv *env, jclass, jlong c, jlong f) {
-       CInvContext *ctx = (CInvContext *)c;
-       CInvFunction *func = (CInvFunction *)f;
-
-       return cinv_function_delete(ctx, func);
-}
-JNIEXPORT jlong JNICALL Java_org_cinvoke_CInvoke_createStruct(
-       JNIEnv *env, jclass, jlong c) {
-       CInvContext *ctx = (CInvContext *)c;
-       
-       return (jlong)cinv_structure_create(ctx);
-}
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_addValueMemberStruct(
-       JNIEnv *env, jclass, jlong c, jlong s, jstring name, jint type) {
-       CInvContext *ctx = (CInvContext *)c;
-       CInvStructure *st = (CInvStructure *)s;
-       const char *chrs = env->GetStringUTFChars(name, NULL);
-       if (chrs == NULL) return 0;
-
-       jint ret = cinv_structure_addmember_value(ctx, st, chrs, 
(cinv_type_t)type);
-
-       env->ReleaseStringUTFChars(name, chrs);
-       return ret;
-}
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_addStructMemberStruct(
-       JNIEnv *env, jclass, jlong c, jlong s, jstring name, jlong t) {
-       CInvContext *ctx = (CInvContext *)c;
-       CInvStructure *st = (CInvStructure *)s;
-       CInvStructure *type = (CInvStructure *)t;
-       const char *chrs = env->GetStringUTFChars(name, NULL);
-       if (chrs == NULL) return 0;
-
-       jint ret = cinv_structure_addmember_struct(ctx, st, chrs, type);
-
-       env->ReleaseStringUTFChars(name, chrs);
-       return ret;
-}
-JNIEXPORT jlong JNICALL Java_org_cinvoke_CInvoke_alloc(
-       JNIEnv *env, jclass, jint sz) {
-       return (jlong)malloc(sz);
-}
-JNIEXPORT void JNICALL Java_org_cinvoke_CInvoke_free(
-       JNIEnv *env, jclass, jlong p) {
-       void *ptr = (void *)p;
-       free(ptr);
-}
-JNIEXPORT void JNICALL Java_org_cinvoke_CInvoke_writeValue(
-       JNIEnv *env, jclass, jlong p, jobject val, jint type) {
-       jclass cls = env->GetObjectClass(val);
-       if (!cls) return;
-
-       jmethodID meth;
-       jbyte b; jshort s; jint i; jlong l; jfloat f; jdouble d;
-
-       switch (type) {
-       case CINV_T_CHAR:
-               meth = env->GetMethodID(cls, "byteValue", "()B");
-               if (!meth) return;
-               b = env->CallByteMethod(val, meth);
-               break;
-       case CINV_T_SHORT:
-       case T_JINT:
-               meth = env->GetMethodID(cls, "intValue", "()I");
-               if (!meth) return;
-               i = env->CallIntMethod(val, meth);
-               break;
-       case CINV_T_INT:
-       case CINV_T_LONG:
-       case CINV_T_EXTRALONG:
-       case CINV_T_PTR:
-       case T_JLONG:
-               meth = env->GetMethodID(cls, "longValue", "()J");
-               if (!meth) return;
-               l = env->CallLongMethod(val, meth);
-               break;
-       case CINV_T_FLOAT:
-               meth = env->GetMethodID(cls, "floatValue", "()F");
-               if (!meth) return;
-               f = env->CallFloatMethod(val, meth);
-               break;
-       case CINV_T_DOUBLE:
-               meth = env->GetMethodID(cls, "doubleValue", "()D");
-               if (!meth) return;
-               d = env->CallDoubleMethod(val, meth);
-               break;
-       case T_JSHORT:
-               meth = env->GetMethodID(cls, "shortValue", "()S");
-               if (!meth) return;
-               s = env->CallShortMethod(val, meth);
-               break;
-       }
-
-       if (env->ExceptionOccurred()) return;
-
-       switch (type) {
-       case CINV_T_CHAR:
-               *(char *)p = (char)b;
-               break;
-       case CINV_T_SHORT:
-               *(short *)p = (short)i;
-               break;
-       case CINV_T_INT:
-               *(int *)p = (int)l;
-               break;
-       case CINV_T_LONG:
-               *(long *)p = (long)l;
-               break;
-       case CINV_T_EXTRALONG:
-               *(long long *)p = (long long)l;
-               break;
-       case CINV_T_PTR: 
-               *(void* *)p = (void*)l;
-               break;
-       case CINV_T_FLOAT:
-               *(float *)p = (float)f;
-               break;
-       case CINV_T_DOUBLE:
-               *(double *)p = (double)d;
-               break;
-       case T_JSHORT:
-               *(cinv_int16_t *)p = (cinv_int16_t)s;
-               break;
-       case T_JINT:
-               *(cinv_int32_t *)p = (cinv_int32_t)i;
-               break;
-       case T_JLONG:
-               *(cinv_int64_t *)p = (cinv_int64_t)l;
-               break;
-       }
-}
-JNIEXPORT jobject JNICALL Java_org_cinvoke_CInvoke_readValue(
-       JNIEnv *env, jclass, jlong p, jclass cls, jint type) {
-       jbyte b; jshort s; jint i; jlong l; jfloat f; jdouble d;
-       switch (type) {
-       case CINV_T_CHAR:
-               b = (jbyte)*(char *)p;
-               break;
-       case CINV_T_SHORT:
-               i = (jint)*(short *)p;
-               break;
-       case CINV_T_INT:
-               l = (jlong)*(int *)p;
-               break;
-       case CINV_T_LONG:
-               l = (jlong)*(long *)p;
-               break;
-       case CINV_T_EXTRALONG:
-               l = (jlong)*(long long *)p;
-               break;
-       case CINV_T_PTR: 
-               l = (jlong)*(void* *)p;
-               break;
-       case CINV_T_FLOAT:
-               f = (jfloat)*(float *)p;
-               break;
-       case CINV_T_DOUBLE:
-               d = (jdouble)*(double *)p;
-               break;
-       case T_JSHORT:
-               s = (jshort)*(cinv_int16_t *)p;
-               break;
-       case T_JINT:
-               i = (jint)*(cinv_int32_t *)p;
-               break;
-       case T_JLONG:
-               l = (jlong)*(cinv_int64_t *)p;
-               break;
-       }
-       
-       jmethodID meth;
-       switch (type) {
-       case CINV_T_CHAR:
-               meth = env->GetMethodID(cls, "<init>", "(B)V");
-               if (!meth) return NULL;
-               return env->NewObject(cls, meth, b);
-       case CINV_T_SHORT:
-       case T_JINT:
-               meth = env->GetMethodID(cls, "<init>", "(I)V");
-               if (!meth) return NULL;
-               return env->NewObject(cls, meth, i);
-       case CINV_T_INT:
-       case CINV_T_LONG:
-       case CINV_T_EXTRALONG:
-       case CINV_T_PTR:
-       case T_JLONG:
-               meth = env->GetMethodID(cls, "<init>", "(J)V");
-               if (!meth) return NULL;
-               return env->NewObject(cls, meth, l);
-       case CINV_T_FLOAT:
-               meth = env->GetMethodID(cls, "<init>", "(F)V");
-               if (!meth) return NULL;
-               return env->NewObject(cls, meth, f);
-       case CINV_T_DOUBLE:
-               meth = env->GetMethodID(cls, "<init>", "(D)V");
-               if (!meth) return NULL;
-               return env->NewObject(cls, meth, d);
-       case T_JSHORT:
-               meth = env->GetMethodID(cls, "<init>", "(S)V");
-               if (!meth) return NULL;
-               return env->NewObject(cls, meth, s);
-       }
-       
-       return NULL;
-}
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_setMemberValueStruct(
-       JNIEnv *env, jclass, jlong c, jlong s, jlong i, jstring name, jobject 
val,
-       jint type) {
-       CInvContext *ctx = (CInvContext *)c;
-       CInvStructure *st = (CInvStructure *)s;
-       void *inst = (void *)i;
-       const char *chrs = env->GetStringUTFChars(name, NULL);
-       if (chrs == NULL) return 0;
-
-       void *p = cinv_structure_instance_getvalue(ctx, st, inst, chrs);
-       if (p == NULL) return 0;
-
-       env->ReleaseStringUTFChars(name, chrs);
-       
-       Java_org_cinvoke_CInvoke_writeValue(env, NULL, (jlong)p, val, type);
-
-       return CINV_SUCCESS;
-}
-JNIEXPORT jobject JNICALL Java_org_cinvoke_CInvoke_getMemberValueStruct(
-       JNIEnv *env, jclass, jlong c, jlong s, jlong i, jstring name, jclass 
cls,
-       jint type) {
-       CInvContext *ctx = (CInvContext *)c;
-       CInvStructure *st = (CInvStructure *)s;
-       void *inst = (void *)i;
-       const char *chrs = env->GetStringUTFChars(name, NULL);
-       if (chrs == NULL) return NULL;
-
-       void *p = cinv_structure_instance_getvalue(ctx, st, inst, chrs);
-       if (p == NULL) return NULL;
-
-       env->ReleaseStringUTFChars(name, chrs);
-       
-       return Java_org_cinvoke_CInvoke_readValue(env, NULL, (jlong)p, cls, 
type);
-}
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_finishStruct(
-       JNIEnv *env, jclass, jlong c, jlong s) {
-       CInvContext *ctx = (CInvContext *)c;
-       CInvStructure *st = (CInvStructure *)s;
-
-       return cinv_structure_finish(ctx, st);
-}
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_sizeStruct(
-       JNIEnv *env, jclass, jlong c, jlong s) {
-       CInvContext *ctx = (CInvContext *)c;
-       CInvStructure *st = (CInvStructure *)s;
-
-       int ret;
-       if (!cinv_structure_getsize(ctx, st, &ret))
-               return -1;
-       return ret;
-}
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_deleteStruct(
-       JNIEnv *env, jclass, jlong c, jlong s) {
-       CInvContext *ctx = (CInvContext *)c;
-       CInvStructure *st = (CInvStructure *)s;
-
-       return cinv_structure_delete(ctx, st);
-}
-
-struct ud {
-       JNIEnv *env;
-       jobject ref;
-       int numparms;
-       jclass *pclasses;
-       int *ptypes;
-       int rettype;
-       bool hasretval;
-};
-
-void cbfunc(CInvFunction *f, void *parameters[],
-       void *returnout, void *userdata) {
-       ud *u = (ud *)userdata;
-
-       JNIEnv *env = u->env;
-       jobject val = u->ref;
-       jclass *pclasses = u->pclasses;
-       int *ptypes = u->ptypes;
-       int numparms = u->numparms;
-       int rettype = u->rettype;
-       bool hasretval = u->hasretval;
-
-       jclass cls = env->GetObjectClass(val);
-       if (!cls) return;
-       jclass objcls = env->FindClass("java/lang/Object");
-       if (!objcls) return;
-
-       jmethodID meth = env->GetMethodID(cls, "cbfunc",
-               "([Ljava/lang/Object;)Ljava/lang/Object;");
-       if (!meth) return;
-
-       jobjectArray parr = env->NewObjectArray(numparms, objcls, NULL);
-       if (!parr) return;
-       for (int i = 0; i < numparms; i++) {
-               env->SetObjectArrayElement(parr, i,
-                       Java_org_cinvoke_CInvoke_readValue(env, NULL, 
(jlong)parameters[i],
-                               pclasses[i], ptypes[i]));
-               if (env->ExceptionOccurred()) return;
-       }
-
-       jobject retval = env->CallObjectMethod(val, meth, parr);
-       if (env->ExceptionOccurred())
-               return;
-
-       if (hasretval)
-               Java_org_cinvoke_CInvoke_writeValue(env, NULL, (jlong)returnout,
-                       retval, rettype);
-}
-
-JNIEXPORT jlong JNICALL Java_org_cinvoke_CInvoke_createCallback(
-       JNIEnv *env, jclass, jlong c, jlong f, jobject cbthunk,
-       jobjectArray pclasses, jintArray ptypes, jboolean hasret, jint rettype) 
{
-       CInvContext *ctx = (CInvContext *)c;
-       CInvFunction *func = (CInvFunction *)f;
-       jobject ref = NULL;
-       ud *u = NULL;
-       int numparms = 0;
-       int *typearr = NULL;
-       jclass *clsarr = NULL;
-
-       ref = env->NewGlobalRef(cbthunk);
-       if (!ref)
-               goto error;
-       numparms = env->GetArrayLength(pclasses);
-       if (env->ExceptionOccurred())
-               goto error;
-       typearr = (int *)malloc(sizeof(int) * numparms);
-       if (!typearr)
-               goto error;
-       jint *tel = env->GetIntArrayElements(ptypes, NULL);
-       if (!tel)
-               goto error;
-       for (int i = 0; i < numparms; i++)
-               typearr[i] = tel[i];
-       env->ReleaseIntArrayElements(ptypes, tel, 0);
-       clsarr = (jclass*)malloc(sizeof(jclass) * numparms);
-       if (!clsarr)
-               goto error;
-       for (int i = 0; i < numparms; i++)
-               clsarr[i] = NULL;
-       for (int i = 0; i < numparms; i++) {
-               jclass cls = (jclass)env->GetObjectArrayElement(pclasses, i);
-               if (!cls)
-                       goto error;
-               clsarr[i] = (jclass)env->NewGlobalRef(cls);
-               if (!clsarr[i])
-                       goto error;
-       }
-       u = new ud();
-       if (!u)
-               goto error;
-
-       u->env = env;
-       u->ref = ref;
-       u->numparms = numparms;
-       u->rettype = rettype;
-       u->hasretval = hasret;
-       u->ptypes = typearr;
-       u->pclasses = clsarr;
-       return (jlong)cinv_callback_create(ctx, func, u, cbfunc);
-error:
-       if (ref)
-               env->DeleteGlobalRef(ref);
-       free(typearr);
-       if (clsarr) {
-               for (int i = 0; i < u->numparms; i++) {
-                       if (clsarr[i])
-                               env->DeleteGlobalRef(clsarr[i]);
-               }
-               free(clsarr);
-       }
-       delete u;
-       return 0;
-}
-JNIEXPORT jlong JNICALL Java_org_cinvoke_CInvoke_getEPCallback(
-       JNIEnv *env, jclass, jlong c, jlong b) {
-       CInvContext *ctx = (CInvContext *)c;
-       CInvCallback *cb = (CInvCallback *)b;
-
-       return (jlong)cinv_callback_getentrypoint(ctx, cb);
-}
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_deleteCallback(
-       JNIEnv *env, jclass, jlong c, jlong b) {
-       CInvContext *ctx = (CInvContext *)c;
-       CInvCallback *cb = (CInvCallback *)b;
-
-       ud *u = (ud *)cb->userdata;
-       u->env->DeleteGlobalRef(u->ref);
-       for (int i = 0; i < u->numparms; i++)
-               u->env->DeleteGlobalRef(u->pclasses[i]);
-       free(u->pclasses);
-       free(u->ptypes);
-       free(u);
-
-       return cinv_callback_delete(ctx, cb);
-}
-
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_sizeofBasic(
-       JNIEnv *env, jclass, jint type) {
-       int sz = 0;
-
-       switch (type) {
-       case CINV_T_CHAR:
-               sz = 1;
-               break;
-       case CINV_T_SHORT:
-               sz = sizeof(short);
-               break;
-       case CINV_T_INT:
-               sz = sizeof(int);
-               break;
-       case CINV_T_LONG:
-               sz = sizeof(long);
-               break;
-       case CINV_T_EXTRALONG:
-               sz = sizeof(long long);
-               break;
-       case CINV_T_PTR: 
-               sz = sizeof(void *);
-               break;
-       case CINV_T_FLOAT:
-               sz = sizeof(float);
-               break;
-       case CINV_T_DOUBLE:
-               sz = sizeof(double);
-               break;
-       case T_JSHORT:
-               sz = sizeof(jshort);
-               break;
-       case T_JINT:
-               sz = sizeof(jint);
-               break;
-       case T_JLONG:
-               sz = sizeof(jlong);
-               break;
-       }
-
-       return sz;
-}
-JNIEXPORT jstring JNICALL Java_org_cinvoke_CInvoke_ptrToStringUTF8(
-       JNIEnv *env, jclass, jlong ptr) {
-       return env->NewStringUTF((char *)ptr);
-}
-JNIEXPORT jstring JNICALL Java_org_cinvoke_CInvoke_ptrToStringUnicode(
-       JNIEnv *env, jclass, jlong ptr, jint len) {
-       return env->NewString((jchar *)ptr, len);
-}

Deleted: trunk/cinvoke/bindings/java/org_cinvoke_CInvoke.h
===================================================================
--- trunk/cinvoke/bindings/java/org_cinvoke_CInvoke.h   2006-07-03 16:08:41 UTC 
(rev 67)
+++ trunk/cinvoke/bindings/java/org_cinvoke_CInvoke.h   2006-07-04 04:17:40 UTC 
(rev 68)
@@ -1,51 +0,0 @@
-/* DO NOT EDIT THIS FILE - it is machine generated */
-
-#ifndef __org_cinvoke_CInvoke__
-#define __org_cinvoke_CInvoke__
-
-#include <jni.h>
-
-#ifdef __cplusplus
-extern "C"
-{
-#endif
-
-JNIEXPORT jlong JNICALL Java_org_cinvoke_CInvoke_createContext (JNIEnv *env, 
jclass);
-JNIEXPORT jstring JNICALL Java_org_cinvoke_CInvoke_getError (JNIEnv *env, 
jclass, jlong);
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_deleteContext (JNIEnv *env, 
jclass, jlong);
-JNIEXPORT jlong JNICALL Java_org_cinvoke_CInvoke_createLibrary (JNIEnv *env, 
jclass, jlong, jstring);
-JNIEXPORT jlong JNICALL Java_org_cinvoke_CInvoke_loadEPLibrary (JNIEnv *env, 
jclass, jlong, jlong, jstring);
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_deleteLibrary (JNIEnv *env, 
jclass, jlong, jlong);
-JNIEXPORT jlong JNICALL Java_org_cinvoke_CInvoke_createFunction (JNIEnv *env, 
jclass, jlong, jint, jstring, jstring);
-JNIEXPORT jobject JNICALL Java_org_cinvoke_CInvoke_invokeFunction (JNIEnv 
*env, jclass, jlong, jlong, jlong, jobjectArray, jintArray, jclass, jint);
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_deleteFunction (JNIEnv *env, 
jclass, jlong, jlong);
-JNIEXPORT jlong JNICALL Java_org_cinvoke_CInvoke_createStruct (JNIEnv *env, 
jclass, jlong);
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_addValueMemberStruct (JNIEnv 
*env, jclass, jlong, jlong, jstring, jint);
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_addStructMemberStruct (JNIEnv 
*env, jclass, jlong, jlong, jstring, jlong);
-JNIEXPORT jlong JNICALL Java_org_cinvoke_CInvoke_alloc (JNIEnv *env, jclass, 
jint);
-JNIEXPORT void JNICALL Java_org_cinvoke_CInvoke_free (JNIEnv *env, jclass, 
jlong);
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_sizeofBasic (JNIEnv *env, 
jclass, jint);
-JNIEXPORT void JNICALL Java_org_cinvoke_CInvoke_writeValue (JNIEnv *env, 
jclass, jlong, jobject, jint);
-JNIEXPORT jobject JNICALL Java_org_cinvoke_CInvoke_readValue (JNIEnv *env, 
jclass, jlong, jclass, jint);
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_setMemberValueStruct (JNIEnv 
*env, jclass, jlong, jlong, jlong, jstring, jobject, jint);
-JNIEXPORT jobject JNICALL Java_org_cinvoke_CInvoke_getMemberValueStruct 
(JNIEnv *env, jclass, jlong, jlong, jlong, jstring, jclass, jint);
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_finishStruct (JNIEnv *env, 
jclass, jlong, jlong);
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_sizeStruct (JNIEnv *env, 
jclass, jlong, jlong);
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_deleteStruct (JNIEnv *env, 
jclass, jlong, jlong);
-JNIEXPORT jlong JNICALL Java_org_cinvoke_CInvoke_createCallback (JNIEnv *env, 
jclass, jlong, jlong, jobject, jobjectArray, jintArray, jboolean, jint);
-JNIEXPORT jlong JNICALL Java_org_cinvoke_CInvoke_getEPCallback (JNIEnv *env, 
jclass, jlong, jlong);
-JNIEXPORT jint JNICALL Java_org_cinvoke_CInvoke_deleteCallback (JNIEnv *env, 
jclass, jlong, jlong);
-JNIEXPORT jstring JNICALL Java_org_cinvoke_CInvoke_ptrToStringUTF8 (JNIEnv 
*env, jclass, jlong);
-JNIEXPORT jstring JNICALL Java_org_cinvoke_CInvoke_ptrToStringUnicode (JNIEnv 
*env, jclass, jlong, jint);
-#undef org_cinvoke_CInvoke_T_JSHORT
-#define org_cinvoke_CInvoke_T_JSHORT -1L
-#undef org_cinvoke_CInvoke_T_JINT
-#define org_cinvoke_CInvoke_T_JINT -2L
-#undef org_cinvoke_CInvoke_T_JLONG
-#define org_cinvoke_CInvoke_T_JLONG -3L
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* __org_cinvoke_CInvoke__ */

Copied: trunk/cinvoke/bindings/java/org_cinvoke_Natives.cpp (from rev 67, 
trunk/cinvoke/bindings/java/org_cinvoke_CInvoke.cpp)
===================================================================
--- trunk/cinvoke/bindings/java/org_cinvoke_Natives.cpp                         
(rev 0)
+++ trunk/cinvoke/bindings/java/org_cinvoke_Natives.cpp 2006-07-04 04:17:40 UTC 
(rev 68)
@@ -0,0 +1,636 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <cinvoke.h>
+#include "org_cinvoke_Natives.h"
+
+#define T_JSHORT -1
+#define T_JINT -2
+#define T_JLONG -3
+
+JNIEXPORT jlong JNICALL Java_org_cinvoke_Natives_createContext(
+       JNIEnv *env, jclass) {
+       return (jlong)cinv_context_create();
+}
+JNIEXPORT jstring JNICALL Java_org_cinvoke_Natives_getError(
+       JNIEnv *env, jclass, jlong c) {
+       CInvContext *ctx = (CInvContext *)c;
+       return env->NewStringUTF(cinv_context_geterrormsg(ctx));
+}
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_deleteContext(
+       JNIEnv *env, jclass, jlong c) {
+       CInvContext *ctx = (CInvContext *)c;
+       return cinv_context_delete(ctx);
+}
+JNIEXPORT jlong JNICALL Java_org_cinvoke_Natives_createLibrary(
+       JNIEnv *env, jclass, jlong c, jstring libname) {
+       CInvContext *ctx = (CInvContext *)c;
+       const char *chrs = env->GetStringUTFChars(libname, NULL);
+       if (chrs == NULL) return 0;
+
+       jlong ret = (jlong)cinv_library_create(ctx, chrs);
+
+       env->ReleaseStringUTFChars(libname, chrs);
+       return ret;
+}
+JNIEXPORT jlong JNICALL Java_org_cinvoke_Natives_loadEPLibrary(
+       JNIEnv *env, jclass, jlong c, jlong l, jstring name) {
+       CInvContext *ctx = (CInvContext *)c;
+       CInvLibrary *lib = (CInvLibrary *)l;
+       const char *chrs = env->GetStringUTFChars(name, NULL);
+       if (chrs == NULL) return 0;
+
+       jlong ret = (jlong)cinv_library_load_entrypoint(ctx, lib, chrs);
+
+       env->ReleaseStringUTFChars(name, chrs);
+       return ret;
+}
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_deleteLibrary(
+       JNIEnv *env, jclass, jlong c, jlong l) {
+       CInvContext *ctx = (CInvContext *)c;
+       CInvLibrary *lib = (CInvLibrary *)l;
+       
+       return cinv_library_delete(ctx, lib);
+}
+JNIEXPORT jlong JNICALL Java_org_cinvoke_Natives_createFunction(
+       JNIEnv *env, jclass, jlong c, jint cc, jstring retfmt, jstring parmfmt) 
{
+       CInvContext *ctx = (CInvContext *)c;
+       const char *parmchrs = env->GetStringUTFChars(parmfmt, NULL);
+       if (parmchrs == NULL) return 0;
+       const char *retchrs = env->GetStringUTFChars(retfmt, NULL);
+       if (retchrs == NULL) return 0;
+
+       if (cc == -1)
+               cc = CINV_CC_DEFAULT;
+
+       jlong ret = (jlong)cinv_function_create(ctx, (cinv_callconv_t)cc,
+               retchrs, parmchrs);
+
+       env->ReleaseStringUTFChars(parmfmt, parmchrs);
+       env->ReleaseStringUTFChars(retfmt, retchrs);
+       return ret;
+}
+void *alloc(int type) {
+       return malloc(Java_org_cinvoke_Natives_sizeofBasic(NULL, NULL, type));
+}
+void fail(JNIEnv *env) {
+       jclass ex = env->FindClass("java/lang/Exception");
+       env->ThrowNew(ex, "invoke failed");
+}
+JNIEXPORT jobject JNICALL Java_org_cinvoke_Natives_invokeFunction(
+       JNIEnv *env, jclass, jlong c, jlong f, jlong e, jobjectArray params,
+       jintArray types, jclass retcls, jint rettype) {
+       CInvContext *ctx = (CInvContext *)c;
+       CInvFunction *func = (CInvFunction *)f;
+       void *ep = (void *)e;
+       jobject ret = NULL;
+       void *retp = NULL;
+       void **pp = NULL;
+       jint *tarr = NULL;
+       int numparms = 0, np = env->GetArrayLength(params);
+       if (env->ExceptionOccurred()) {
+               fail(env);
+               goto out;
+       }
+       tarr = env->GetIntArrayElements(types, NULL);
+       if (tarr == NULL) {
+               fail(env);
+               goto out;
+       }
+       
+       if (retcls) {
+               retp = alloc(rettype);
+               if (!retp) {
+                       fail(env);
+                       goto out;
+               }
+       }
+       pp = (void **)malloc(sizeof(void*) * np);
+       if (!pp) {
+               fail(env);
+               goto out;
+       }
+       for (int i = 0; i < np; i++)
+               pp[i] = NULL;
+       for (int i = 0; i < np; i++) {
+               jobject parm = env->GetObjectArrayElement(params, i);
+               if (env->ExceptionOccurred()) {
+                       fail(env);
+                       goto out;
+               }
+               pp[i] = alloc(tarr[i]);
+               if (!pp[i]) {
+                       fail(env);
+                       goto out;
+               }
+               Java_org_cinvoke_Natives_writeValue(env, NULL, (jlong)pp[i], 
parm,
+                       tarr[i]);
+       }
+       numparms = np;
+       
+       if (!cinv_function_invoke(ctx, func, ep, retp, pp)) {
+               fail(env);
+               goto out;
+       }
+       
+       if (retp)
+               ret = Java_org_cinvoke_Natives_readValue(env, NULL,
+                       (jlong)retp, retcls, rettype);
+out:
+       if (tarr != NULL)
+               env->ReleaseIntArrayElements(types, tarr, 0);
+       for (int i = 0; i < numparms; i++)
+               free(pp[i]);
+       free(pp);
+       free(retp);
+       return ret;
+}
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_deleteFunction(
+       JNIEnv *env, jclass, jlong c, jlong f) {
+       CInvContext *ctx = (CInvContext *)c;
+       CInvFunction *func = (CInvFunction *)f;
+
+       return cinv_function_delete(ctx, func);
+}
+JNIEXPORT jlong JNICALL Java_org_cinvoke_Natives_createStruct(
+       JNIEnv *env, jclass, jlong c) {
+       CInvContext *ctx = (CInvContext *)c;
+       
+       return (jlong)cinv_structure_create(ctx);
+}
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_addValueMemberStruct(
+       JNIEnv *env, jclass, jlong c, jlong s, jstring name, jint type) {
+       CInvContext *ctx = (CInvContext *)c;
+       CInvStructure *st = (CInvStructure *)s;
+       const char *chrs = env->GetStringUTFChars(name, NULL);
+       if (chrs == NULL) return 0;
+
+       jint ret = cinv_structure_addmember_value(ctx, st, chrs, 
(cinv_type_t)type);
+
+       env->ReleaseStringUTFChars(name, chrs);
+       return ret;
+}
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_addStructMemberStruct(
+       JNIEnv *env, jclass, jlong c, jlong s, jstring name, jlong t) {
+       CInvContext *ctx = (CInvContext *)c;
+       CInvStructure *st = (CInvStructure *)s;
+       CInvStructure *type = (CInvStructure *)t;
+       const char *chrs = env->GetStringUTFChars(name, NULL);
+       if (chrs == NULL) return 0;
+
+       jint ret = cinv_structure_addmember_struct(ctx, st, chrs, type);
+
+       env->ReleaseStringUTFChars(name, chrs);
+       return ret;
+}
+JNIEXPORT jlong JNICALL Java_org_cinvoke_Natives_alloc(
+       JNIEnv *env, jclass, jint sz) {
+       return (jlong)malloc(sz);
+}
+JNIEXPORT void JNICALL Java_org_cinvoke_Natives_free(
+       JNIEnv *env, jclass, jlong p) {
+       void *ptr = (void *)p;
+       free(ptr);
+}
+JNIEXPORT void JNICALL Java_org_cinvoke_Natives_writeValue(
+       JNIEnv *env, jclass, jlong p, jobject val, jint type) {
+       jclass cls = env->GetObjectClass(val);
+       if (!cls) return;
+
+       jmethodID meth;
+       jbyte b; jshort s; jint i; jlong l; jfloat f; jdouble d;
+
+       switch (type) {
+       case CINV_T_CHAR:
+               meth = env->GetMethodID(cls, "byteValue", "()B");
+               if (!meth) return;
+               b = env->CallByteMethod(val, meth);
+               break;
+       case CINV_T_SHORT:
+       case T_JINT:
+               meth = env->GetMethodID(cls, "intValue", "()I");
+               if (!meth) return;
+               i = env->CallIntMethod(val, meth);
+               break;
+       case CINV_T_INT:
+       case CINV_T_LONG:
+       case CINV_T_EXTRALONG:
+       case CINV_T_PTR:
+       case T_JLONG:
+               meth = env->GetMethodID(cls, "longValue", "()J");
+               if (!meth) return;
+               l = env->CallLongMethod(val, meth);
+               break;
+       case CINV_T_FLOAT:
+               meth = env->GetMethodID(cls, "floatValue", "()F");
+               if (!meth) return;
+               f = env->CallFloatMethod(val, meth);
+               break;
+       case CINV_T_DOUBLE:
+               meth = env->GetMethodID(cls, "doubleValue", "()D");
+               if (!meth) return;
+               d = env->CallDoubleMethod(val, meth);
+               break;
+       case T_JSHORT:
+               meth = env->GetMethodID(cls, "shortValue", "()S");
+               if (!meth) return;
+               s = env->CallShortMethod(val, meth);
+               break;
+       }
+
+       if (env->ExceptionOccurred()) return;
+
+       switch (type) {
+       case CINV_T_CHAR:
+               *(char *)p = (char)b;
+               break;
+       case CINV_T_SHORT:
+               *(short *)p = (short)i;
+               break;
+       case CINV_T_INT:
+               *(int *)p = (int)l;
+               break;
+       case CINV_T_LONG:
+               *(long *)p = (long)l;
+               break;
+       case CINV_T_EXTRALONG:
+               *(long long *)p = (long long)l;
+               break;
+       case CINV_T_PTR: 
+               *(void* *)p = (void*)l;
+               break;
+       case CINV_T_FLOAT:
+               *(float *)p = (float)f;
+               break;
+       case CINV_T_DOUBLE:
+               *(double *)p = (double)d;
+               break;
+       case T_JSHORT:
+               *(cinv_int16_t *)p = (cinv_int16_t)s;
+               break;
+       case T_JINT:
+               *(cinv_int32_t *)p = (cinv_int32_t)i;
+               break;
+       case T_JLONG:
+               *(cinv_int64_t *)p = (cinv_int64_t)l;
+               break;
+       }
+}
+JNIEXPORT jobject JNICALL Java_org_cinvoke_Natives_readValue(
+       JNIEnv *env, jclass, jlong p, jclass cls, jint type) {
+       jbyte b; jshort s; jint i; jlong l; jfloat f; jdouble d;
+       switch (type) {
+       case CINV_T_CHAR:
+               b = (jbyte)*(char *)p;
+               break;
+       case CINV_T_SHORT:
+               i = (jint)*(short *)p;
+               break;
+       case CINV_T_INT:
+               l = (jlong)*(int *)p;
+               break;
+       case CINV_T_LONG:
+               l = (jlong)*(long *)p;
+               break;
+       case CINV_T_EXTRALONG:
+               l = (jlong)*(long long *)p;
+               break;
+       case CINV_T_PTR: 
+               l = (jlong)*(void* *)p;
+               break;
+       case CINV_T_FLOAT:
+               f = (jfloat)*(float *)p;
+               break;
+       case CINV_T_DOUBLE:
+               d = (jdouble)*(double *)p;
+               break;
+       case T_JSHORT:
+               s = (jshort)*(cinv_int16_t *)p;
+               break;
+       case T_JINT:
+               i = (jint)*(cinv_int32_t *)p;
+               break;
+       case T_JLONG:
+               l = (jlong)*(cinv_int64_t *)p;
+               break;
+       }
+       
+       jmethodID meth;
+       switch (type) {
+       case CINV_T_CHAR:
+               meth = env->GetMethodID(cls, "<init>", "(B)V");
+               if (!meth) return NULL;
+               return env->NewObject(cls, meth, b);
+       case CINV_T_SHORT:
+       case T_JINT:
+               meth = env->GetMethodID(cls, "<init>", "(I)V");
+               if (!meth) return NULL;
+               return env->NewObject(cls, meth, i);
+       case CINV_T_INT:
+       case CINV_T_LONG:
+       case CINV_T_EXTRALONG:
+       case CINV_T_PTR:
+       case T_JLONG:
+               meth = env->GetMethodID(cls, "<init>", "(J)V");
+               if (!meth) return NULL;
+               return env->NewObject(cls, meth, l);
+       case CINV_T_FLOAT:
+               meth = env->GetMethodID(cls, "<init>", "(F)V");
+               if (!meth) return NULL;
+               return env->NewObject(cls, meth, f);
+       case CINV_T_DOUBLE:
+               meth = env->GetMethodID(cls, "<init>", "(D)V");
+               if (!meth) return NULL;
+               return env->NewObject(cls, meth, d);
+       case T_JSHORT:
+               meth = env->GetMethodID(cls, "<init>", "(S)V");
+               if (!meth) return NULL;
+               return env->NewObject(cls, meth, s);
+       }
+       
+       return NULL;
+}
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_setMemberValueStruct(
+       JNIEnv *env, jclass, jlong c, jlong s, jlong i, jstring name, jobject 
val,
+       jint type) {
+       CInvContext *ctx = (CInvContext *)c;
+       CInvStructure *st = (CInvStructure *)s;
+       void *inst = (void *)i;
+       const char *chrs = env->GetStringUTFChars(name, NULL);
+       if (chrs == NULL) return 0;
+
+       void *p = cinv_structure_instance_getvalue(ctx, st, inst, chrs);
+       if (p == NULL) return 0;
+
+       env->ReleaseStringUTFChars(name, chrs);
+       
+       Java_org_cinvoke_Natives_writeValue(env, NULL, (jlong)p, val, type);
+
+       return CINV_SUCCESS;
+}
+JNIEXPORT jobject JNICALL Java_org_cinvoke_Natives_getMemberValueStruct(
+       JNIEnv *env, jclass, jlong c, jlong s, jlong i, jstring name, jclass 
cls,
+       jint type) {
+       CInvContext *ctx = (CInvContext *)c;
+       CInvStructure *st = (CInvStructure *)s;
+       void *inst = (void *)i;
+       const char *chrs = env->GetStringUTFChars(name, NULL);
+       if (chrs == NULL) return NULL;
+
+       void *p = cinv_structure_instance_getvalue(ctx, st, inst, chrs);
+       if (p == NULL) return NULL;
+
+       env->ReleaseStringUTFChars(name, chrs);
+       
+       return Java_org_cinvoke_Natives_readValue(env, NULL, (jlong)p, cls, 
type);
+}
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_finishStruct(
+       JNIEnv *env, jclass, jlong c, jlong s) {
+       CInvContext *ctx = (CInvContext *)c;
+       CInvStructure *st = (CInvStructure *)s;
+
+       return cinv_structure_finish(ctx, st);
+}
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_sizeStruct(
+       JNIEnv *env, jclass, jlong c, jlong s) {
+       CInvContext *ctx = (CInvContext *)c;
+       CInvStructure *st = (CInvStructure *)s;
+
+       int ret;
+       if (!cinv_structure_getsize(ctx, st, &ret))
+               return -1;
+       return ret;
+}
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_deleteStruct(
+       JNIEnv *env, jclass, jlong c, jlong s) {
+       CInvContext *ctx = (CInvContext *)c;
+       CInvStructure *st = (CInvStructure *)s;
+
+       return cinv_structure_delete(ctx, st);
+}
+
+struct ud {
+       JNIEnv *env;
+       jobject ref;
+       int numparms;
+       jclass *pclasses;
+       int *ptypes;
+       int rettype;
+       bool hasretval;
+};
+
+void cbfunc(CInvFunction *f, void *parameters[],
+       void *returnout, void *userdata) {
+       ud *u = (ud *)userdata;
+
+       JNIEnv *env = u->env;
+       jobject val = u->ref;
+       jclass *pclasses = u->pclasses;
+       int *ptypes = u->ptypes;
+       int numparms = u->numparms;
+       int rettype = u->rettype;
+       bool hasretval = u->hasretval;
+
+       jclass cls = env->GetObjectClass(val);
+       if (!cls) return;
+       jclass objcls = env->FindClass("java/lang/Object");
+       if (!objcls) return;
+
+       jmethodID meth = env->GetMethodID(cls, "cbfunc",
+               "([Ljava/lang/Object;)Ljava/lang/Object;");
+       if (!meth) return;
+
+       jobjectArray parr = env->NewObjectArray(numparms, objcls, NULL);
+       if (!parr) return;
+       for (int i = 0; i < numparms; i++) {
+               env->SetObjectArrayElement(parr, i,
+                       Java_org_cinvoke_Natives_readValue(env, NULL, 
(jlong)parameters[i],
+                               pclasses[i], ptypes[i]));
+               if (env->ExceptionOccurred()) return;
+       }
+
+       jobject retval = env->CallObjectMethod(val, meth, parr);
+       if (env->ExceptionOccurred())
+               return;
+
+       if (hasretval)
+               Java_org_cinvoke_Natives_writeValue(env, NULL, (jlong)returnout,
+                       retval, rettype);
+}
+
+JNIEXPORT jlong JNICALL Java_org_cinvoke_Natives_createCallback(
+       JNIEnv *env, jclass, jlong c, jlong f, jobject cbthunk,
+       jobjectArray pclasses, jintArray ptypes, jboolean hasret, jint rettype) 
{
+       CInvContext *ctx = (CInvContext *)c;
+       CInvFunction *func = (CInvFunction *)f;
+       jobject ref = NULL;
+       ud *u = NULL;
+       int numparms = 0;
+       int *typearr = NULL;
+       jclass *clsarr = NULL;
+
+       ref = env->NewGlobalRef(cbthunk);
+       if (!ref)
+               goto error;
+       numparms = env->GetArrayLength(pclasses);
+       if (env->ExceptionOccurred())
+               goto error;
+       typearr = (int *)malloc(sizeof(int) * numparms);
+       if (!typearr)
+               goto error;
+       jint *tel = env->GetIntArrayElements(ptypes, NULL);
+       if (!tel)
+               goto error;
+       for (int i = 0; i < numparms; i++)
+               typearr[i] = tel[i];
+       env->ReleaseIntArrayElements(ptypes, tel, 0);
+       clsarr = (jclass*)malloc(sizeof(jclass) * numparms);
+       if (!clsarr)
+               goto error;
+       for (int i = 0; i < numparms; i++)
+               clsarr[i] = NULL;
+       for (int i = 0; i < numparms; i++) {
+               jclass cls = (jclass)env->GetObjectArrayElement(pclasses, i);
+               if (!cls)
+                       goto error;
+               clsarr[i] = (jclass)env->NewGlobalRef(cls);
+               if (!clsarr[i])
+                       goto error;
+       }
+       u = new ud();
+       if (!u)
+               goto error;
+
+       u->env = env;
+       u->ref = ref;
+       u->numparms = numparms;
+       u->rettype = rettype;
+       u->hasretval = hasret;
+       u->ptypes = typearr;
+       u->pclasses = clsarr;
+       return (jlong)cinv_callback_create(ctx, func, u, cbfunc);
+error:
+       if (ref)
+               env->DeleteGlobalRef(ref);
+       free(typearr);
+       if (clsarr) {
+               for (int i = 0; i < u->numparms; i++) {
+                       if (clsarr[i])
+                               env->DeleteGlobalRef(clsarr[i]);
+               }
+               free(clsarr);
+       }
+       delete u;
+       return 0;
+}
+JNIEXPORT jlong JNICALL Java_org_cinvoke_Natives_getEPCallback(
+       JNIEnv *env, jclass, jlong c, jlong b) {
+       CInvContext *ctx = (CInvContext *)c;
+       CInvCallback *cb = (CInvCallback *)b;
+
+       return (jlong)cinv_callback_getentrypoint(ctx, cb);
+}
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_deleteCallback(
+       JNIEnv *env, jclass, jlong c, jlong b) {
+       CInvContext *ctx = (CInvContext *)c;
+       CInvCallback *cb = (CInvCallback *)b;
+
+       ud *u = (ud *)cb->userdata;
+       u->env->DeleteGlobalRef(u->ref);
+       for (int i = 0; i < u->numparms; i++)
+               u->env->DeleteGlobalRef(u->pclasses[i]);
+       free(u->pclasses);
+       free(u->ptypes);
+       free(u);
+
+       return cinv_callback_delete(ctx, cb);
+}
+
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_sizeofBasic(
+       JNIEnv *env, jclass, jint type) {
+       int sz = 0;
+
+       switch (type) {
+       case CINV_T_CHAR:
+               sz = 1;
+               break;
+       case CINV_T_SHORT:
+               sz = sizeof(short);
+               break;
+       case CINV_T_INT:
+               sz = sizeof(int);
+               break;
+       case CINV_T_LONG:
+               sz = sizeof(long);
+               break;
+       case CINV_T_EXTRALONG:
+               sz = sizeof(long long);
+               break;
+       case CINV_T_PTR: 
+               sz = sizeof(void *);
+               break;
+       case CINV_T_FLOAT:
+               sz = sizeof(float);
+               break;
+       case CINV_T_DOUBLE:
+               sz = sizeof(double);
+               break;
+       case T_JSHORT:
+               sz = sizeof(jshort);
+               break;
+       case T_JINT:
+               sz = sizeof(jint);
+               break;
+       case T_JLONG:
+               sz = sizeof(jlong);
+               break;
+       }
+
+       return sz;
+}
+JNIEXPORT jstring JNICALL Java_org_cinvoke_Natives_ptrToStringUTF8(
+       JNIEnv *env, jclass, jlong ptr) {
+       return env->NewStringUTF((char *)ptr);
+}
+JNIEXPORT jstring JNICALL Java_org_cinvoke_Natives_ptrToStringUnicode(
+       JNIEnv *env, jclass, jlong ptr, jint len) {
+       if (len < 0) {
+               jchar *t = (jchar *)ptr;
+               int i = 0;
+               while (*t) { i++; t++; }
+               return env->NewString((jchar *)ptr, i);
+       } else
+               return env->NewString((jchar *)ptr, len);
+}
+JNIEXPORT jlong JNICALL Java_org_cinvoke_Natives_stringToPtrUTF8(
+       JNIEnv *env, jclass, jstring str) {
+       jsize len = env->GetStringUTFLength(str);
+       if (env->ExceptionOccurred()) return 0;
+       char *ret = (char *)malloc(len + 1);
+       if (!ret) return 0;
+       const char *chrs = env->GetStringUTFChars(str, NULL);
+       if (!chrs) {
+               free(ret);
+               return 0;
+       }
+       memcpy(ret, chrs, len);
+       env->ReleaseStringUTFChars(str, chrs);
+       ret[len] = 0;
+       return (jlong)ret;
+}
+JNIEXPORT jlong JNICALL Java_org_cinvoke_Natives_stringToPtrUnicode(
+       JNIEnv *env, jclass, jstring str) {
+       jsize len = env->GetStringLength(str);
+       if (env->ExceptionOccurred()) return 0;
+       jchar *ret = (jchar *)malloc((len + 1) * sizeof(jchar));
+       if (!ret) return 0;
+       const jchar *chrs = env->GetStringChars(str, NULL);
+       if (!chrs) {
+               free(ret);
+               return 0;
+       }
+       memcpy(ret, chrs, len * sizeof(jchar));
+       env->ReleaseStringChars(str, chrs);
+       ret[len] = 0;
+       return (jlong)ret;
+}

Copied: trunk/cinvoke/bindings/java/org_cinvoke_Natives.h (from rev 66, 
trunk/cinvoke/bindings/java/org_cinvoke_CInvoke.h)
===================================================================
--- trunk/cinvoke/bindings/java/org_cinvoke_Natives.h                           
(rev 0)
+++ trunk/cinvoke/bindings/java/org_cinvoke_Natives.h   2006-07-04 04:17:40 UTC 
(rev 68)
@@ -0,0 +1,69 @@
+/* DO NOT EDIT THIS FILE - it is machine generated */
+
+#ifndef __org_cinvoke_Natives__
+#define __org_cinvoke_Natives__
+
+#include <jni.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+JNIEXPORT jlong JNICALL Java_org_cinvoke_Natives_createContext (JNIEnv *env, 
jclass);
+JNIEXPORT jstring JNICALL Java_org_cinvoke_Natives_getError (JNIEnv *env, 
jclass, jlong);
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_deleteContext (JNIEnv *env, 
jclass, jlong);
+JNIEXPORT jlong JNICALL Java_org_cinvoke_Natives_createLibrary (JNIEnv *env, 
jclass, jlong, jstring);
+JNIEXPORT jlong JNICALL Java_org_cinvoke_Natives_loadEPLibrary (JNIEnv *env, 
jclass, jlong, jlong, jstring);
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_deleteLibrary (JNIEnv *env, 
jclass, jlong, jlong);
+JNIEXPORT jlong JNICALL Java_org_cinvoke_Natives_createFunction (JNIEnv *env, 
jclass, jlong, jint, jstring, jstring);
+JNIEXPORT jobject JNICALL Java_org_cinvoke_Natives_invokeFunction (JNIEnv 
*env, jclass, jlong, jlong, jlong, jobjectArray, jintArray, jclass, jint);
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_deleteFunction (JNIEnv *env, 
jclass, jlong, jlong);
+JNIEXPORT jlong JNICALL Java_org_cinvoke_Natives_createStruct (JNIEnv *env, 
jclass, jlong);
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_addValueMemberStruct (JNIEnv 
*env, jclass, jlong, jlong, jstring, jint);
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_addStructMemberStruct (JNIEnv 
*env, jclass, jlong, jlong, jstring, jlong);
+JNIEXPORT jlong JNICALL Java_org_cinvoke_Natives_alloc (JNIEnv *env, jclass, 
jint);
+JNIEXPORT void JNICALL Java_org_cinvoke_Natives_free (JNIEnv *env, jclass, 
jlong);
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_sizeofBasic (JNIEnv *env, 
jclass, jint);
+JNIEXPORT void JNICALL Java_org_cinvoke_Natives_writeValue (JNIEnv *env, 
jclass, jlong, jobject, jint);
+JNIEXPORT jobject JNICALL Java_org_cinvoke_Natives_readValue (JNIEnv *env, 
jclass, jlong, jclass, jint);
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_setMemberValueStruct (JNIEnv 
*env, jclass, jlong, jlong, jlong, jstring, jobject, jint);
+JNIEXPORT jobject JNICALL Java_org_cinvoke_Natives_getMemberValueStruct 
(JNIEnv *env, jclass, jlong, jlong, jlong, jstring, jclass, jint);
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_finishStruct (JNIEnv *env, 
jclass, jlong, jlong);
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_sizeStruct (JNIEnv *env, 
jclass, jlong, jlong);
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_deleteStruct (JNIEnv *env, 
jclass, jlong, jlong);
+JNIEXPORT jlong JNICALL Java_org_cinvoke_Natives_createCallback (JNIEnv *env, 
jclass, jlong, jlong, jobject, jobjectArray, jintArray, jboolean, jint);
+JNIEXPORT jlong JNICALL Java_org_cinvoke_Natives_getEPCallback (JNIEnv *env, 
jclass, jlong, jlong);
+JNIEXPORT jint JNICALL Java_org_cinvoke_Natives_deleteCallback (JNIEnv *env, 
jclass, jlong, jlong);
+JNIEXPORT jstring JNICALL Java_org_cinvoke_Natives_ptrToStringUTF8 (JNIEnv 
*env, jclass, jlong);
+JNIEXPORT jstring JNICALL Java_org_cinvoke_Natives_ptrToStringUnicode (JNIEnv 
*env, jclass, jlong, jint);
+JNIEXPORT jlong JNICALL Java_org_cinvoke_Natives_stringToPtrUTF8 (JNIEnv *env, 
jclass, jstring);
+JNIEXPORT jlong JNICALL Java_org_cinvoke_Natives_stringToPtrUnicode (JNIEnv 
*env, jclass, jstring);
+#undef org_cinvoke_Natives_T_JSHORT
+#define org_cinvoke_Natives_T_JSHORT -1L
+#undef org_cinvoke_Natives_T_JINT
+#define org_cinvoke_Natives_T_JINT -2L
+#undef org_cinvoke_Natives_T_JLONG
+#define org_cinvoke_Natives_T_JLONG -3L
+#undef org_cinvoke_Natives_T_CHAR
+#define org_cinvoke_Natives_T_CHAR 0L
+#undef org_cinvoke_Natives_T_SHORT
+#define org_cinvoke_Natives_T_SHORT 1L
+#undef org_cinvoke_Natives_T_INT
+#define org_cinvoke_Natives_T_INT 2L
+#undef org_cinvoke_Natives_T_LONG
+#define org_cinvoke_Natives_T_LONG 3L
+#undef org_cinvoke_Natives_T_EXTRALONG
+#define org_cinvoke_Natives_T_EXTRALONG 4L
+#undef org_cinvoke_Natives_T_FLOAT
+#define org_cinvoke_Natives_T_FLOAT 5L
+#undef org_cinvoke_Natives_T_DOUBLE
+#define org_cinvoke_Natives_T_DOUBLE 6L
+#undef org_cinvoke_Natives_T_PTR
+#define org_cinvoke_Natives_T_PTR 7L
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __org_cinvoke_Natives__ */





reply via email to

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