[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[cinvoke-svn] r65 - in trunk/cinvoke/bindings/java: . org/cinvoke
From: |
will |
Subject: |
[cinvoke-svn] r65 - in trunk/cinvoke/bindings/java: . org/cinvoke |
Date: |
2 Jul 2006 23:43:37 -0400 |
Author: will
Date: 2006-07-02 23:43:37 -0400 (Sun, 02 Jul 2006)
New Revision: 65
Modified:
trunk/cinvoke/bindings/java/org/cinvoke/CInvoke.java
trunk/cinvoke/bindings/java/org/cinvoke/NativeInt.java
trunk/cinvoke/bindings/java/org/cinvoke/NativeLongLong.java
trunk/cinvoke/bindings/java/org/cinvoke/NativeShort.java
trunk/cinvoke/bindings/java/org/cinvoke/Ptr.java
trunk/cinvoke/bindings/java/org_cinvoke_CInvoke.cpp
trunk/cinvoke/bindings/java/org_cinvoke_CInvoke.h
Log:
implemented some native methods
Modified: trunk/cinvoke/bindings/java/org/cinvoke/CInvoke.java
===================================================================
--- trunk/cinvoke/bindings/java/org/cinvoke/CInvoke.java 2006-07-01
22:35:13 UTC (rev 64)
+++ trunk/cinvoke/bindings/java/org/cinvoke/CInvoke.java 2006-07-03
03:43:37 UTC (rev 65)
@@ -9,13 +9,12 @@
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 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);
+ 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,
@@ -25,11 +24,11 @@
private static native long alloc(int sz);
private static native void free(long m);
private static native void writeValue(long m, Object val, int type);
- private static native Object readValue(long m, 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, int type);
+ 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);
@@ -43,7 +42,7 @@
// XXX
}
- public Object cbfunc(long f, Object[] params) {
+ public Object cbfunc(Object[] params) {
// XXX
return null;
}
@@ -55,18 +54,25 @@
public static final int STDCALL = 1;
public static final int FASTCALL = 2;
}
+ public class ENC {
+ 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) {
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) {
+ return load(libname, iface, CC.DEFAULT, ENC.UTF8);
+ }
+ public static Object load(String libname, Class iface, int callconv,
+ int encoding) {
// XXX
return null;
}
- public static void dispose(Object library) {
- // XXX
- }
public static String ptrToStringUTF8(Ptr ptr) {
// XXX
return null;
@@ -75,7 +81,7 @@
// XXX
return null;
}
- public static Object[] ptrToStruct(Ptr ptr, Class type) {
+ public static Object[] ptrToArray(Ptr ptr, Class type, int num) {
// XXX
return null;
}
Modified: trunk/cinvoke/bindings/java/org/cinvoke/NativeInt.java
===================================================================
--- trunk/cinvoke/bindings/java/org/cinvoke/NativeInt.java 2006-07-01
22:35:13 UTC (rev 64)
+++ trunk/cinvoke/bindings/java/org/cinvoke/NativeInt.java 2006-07-03
03:43:37 UTC (rev 65)
@@ -4,7 +4,7 @@
public NativeInt(long val) {
_val = val;
}
- public long toLong() {
+ public long longVal() {
return _val;
}
Modified: trunk/cinvoke/bindings/java/org/cinvoke/NativeLongLong.java
===================================================================
--- trunk/cinvoke/bindings/java/org/cinvoke/NativeLongLong.java 2006-07-01
22:35:13 UTC (rev 64)
+++ trunk/cinvoke/bindings/java/org/cinvoke/NativeLongLong.java 2006-07-03
03:43:37 UTC (rev 65)
@@ -4,7 +4,7 @@
public NativeLongLong(long val) {
_val = val;
}
- public long toLong() {
+ public long longValue() {
return _val;
}
Modified: trunk/cinvoke/bindings/java/org/cinvoke/NativeShort.java
===================================================================
--- trunk/cinvoke/bindings/java/org/cinvoke/NativeShort.java 2006-07-01
22:35:13 UTC (rev 64)
+++ trunk/cinvoke/bindings/java/org/cinvoke/NativeShort.java 2006-07-03
03:43:37 UTC (rev 65)
@@ -4,7 +4,7 @@
public NativeShort(int val) {
_val = val;
}
- public int toLong() {
+ public int intValue() {
return _val;
}
Modified: trunk/cinvoke/bindings/java/org/cinvoke/Ptr.java
===================================================================
--- trunk/cinvoke/bindings/java/org/cinvoke/Ptr.java 2006-07-01 22:35:13 UTC
(rev 64)
+++ trunk/cinvoke/bindings/java/org/cinvoke/Ptr.java 2006-07-03 03:43:37 UTC
(rev 65)
@@ -4,7 +4,7 @@
public Ptr(long val) {
_val = val;
}
- public long toLong() {
+ public long longValue() {
return _val;
}
Modified: trunk/cinvoke/bindings/java/org_cinvoke_CInvoke.cpp
===================================================================
--- trunk/cinvoke/bindings/java/org_cinvoke_CInvoke.cpp 2006-07-01 22:35:13 UTC
(rev 64)
+++ trunk/cinvoke/bindings/java/org_cinvoke_CInvoke.cpp 2006-07-03 03:43:37 UTC
(rev 65)
@@ -3,16 +3,9 @@
#include <cinvoke.h>
#include "org_cinvoke_CInvoke.h"
-cinv_type_t gettype(jint jt) {
- if (jt == -1)
- return CINV_T_2BYTE;
- else if (jt == -2)
- return CINV_T_4BYTE;
- else if (jt == -3)
- return CINV_T_8BYTE;
- else
- return (cinv_type_t)jt;
-}
+#define T_JSHORT -1
+#define T_JINT -2
+#define T_JLONG -3
JNIEXPORT jlong JNICALL Java_org_cinvoke_CInvoke_createContext(
JNIEnv *env, jclass) {
@@ -76,11 +69,115 @@
env->ReleaseStringUTFChars(retfmt, retchrs);
return ret;
}
+void *alloc(int 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 malloc(sz);
+}
+void fail(JNIEnv *env) {
+ // XXX
+}
JNIEXPORT jobject JNICALL Java_org_cinvoke_CInvoke_invokeFunction(
JNIEnv *env, jclass, jlong c, jlong f, jlong e, jobjectArray params,
- jintArray types) {
- // XXX
- return NULL;
+ 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;
+ }
+
+
+ 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;
+ }
+
+ 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) {
@@ -131,23 +228,195 @@
}
JNIEXPORT void JNICALL Java_org_cinvoke_CInvoke_writeValue(
JNIEnv *env, jclass, jlong p, jobject val, jint type) {
- // XXX
+ 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, jint type) {
- // XXX
+ 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) {
- // XXX
- return 0;
+ 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, jint
type) {
- // XXX
- return NULL;
+ 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) {
@@ -173,13 +442,25 @@
return cinv_structure_delete(ctx, st);
}
+
+void cbfunc(CInvFunction *f, void *parameters[],
+ void *returnout, void *userdata) {
+/*
+ JNIEnv *env = (JNIEnv *)userdata;
+ // XXX store object in global reference
+ jclass cls = env->GetObjectClass(val);
+ if (!cls) return;
+
+ jmethodID meth = env->GetMethodID(cls, "cbfunc",
+ "([Ljava/lang/Object;)Ljava/lang/Object;");*/
+}
+
JNIEXPORT jlong JNICALL Java_org_cinvoke_CInvoke_createCallback(
JNIEnv *env, jclass, jlong c, jlong f, jobject cbthunk) {
- //CInvContext *ctx = (CInvContext *)c;
- //CInvFunction *func = (CInvFunction *)f;
+ CInvContext *ctx = (CInvContext *)c;
+ CInvFunction *func = (CInvFunction *)f;
- // XXX
- return 0;
+ return (jlong)cinv_callback_create(ctx, func, env, cbfunc);
}
JNIEXPORT jlong JNICALL Java_org_cinvoke_CInvoke_getEPCallback(
JNIEnv *env, jclass, jlong c, jlong b) {
@@ -192,6 +473,6 @@
JNIEnv *env, jclass, jlong c, jlong b) {
CInvContext *ctx = (CInvContext *)c;
CInvCallback *cb = (CInvCallback *)b;
-
+
return cinv_callback_delete(ctx, cb);
}
Modified: trunk/cinvoke/bindings/java/org_cinvoke_CInvoke.h
===================================================================
--- trunk/cinvoke/bindings/java/org_cinvoke_CInvoke.h 2006-07-01 22:35:13 UTC
(rev 64)
+++ trunk/cinvoke/bindings/java/org_cinvoke_CInvoke.h 2006-07-03 03:43:37 UTC
(rev 65)
@@ -17,7 +17,7 @@
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);
+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);
@@ -25,9 +25,9 @@
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 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, 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, 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);
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [cinvoke-svn] r65 - in trunk/cinvoke/bindings/java: . org/cinvoke,
will <=