Mercurial > pub > dyncall > bindings
changeset 21:40a2c4198016
- working jdc (but with memleaks, currently)
- added example
author | cslag |
---|---|
date | Wed, 30 Mar 2016 23:40:52 +0200 |
parents | 77ca609422f1 |
children | 4ee8d6aa7721 |
files | java/jdc/Makefile java/jdc/README.txt java/jdc/examples/UnixMathExample.java java/jdc/org_dyncall_DC.c |
diffstat | 4 files changed, 80 insertions(+), 25 deletions(-) [+] |
line wrap: on
line diff
--- a/java/jdc/Makefile Wed Mar 30 02:40:56 2016 +0200 +++ b/java/jdc/Makefile Wed Mar 30 23:40:52 2016 +0200 @@ -10,10 +10,10 @@ $(SO): org_dyncall_DC.h org_dyncall_DC.c # @@@ support different compilers @@@ esp. on windows - $(CC) -shared -fPIC -o $@ org_dyncall_DC.c + $(CC) -shared -fPIC -o $@ org_dyncall_DC.c -ldyncall_s -ldynload_s org_dyncall_DC.h: org/dyncall/DC.class - javah org.dyncall.DC + javah -jni org.dyncall.DC org/dyncall/DC.class: cd org/dyncall/ && javac DC.java
--- a/java/jdc/README.txt Wed Mar 30 02:40:56 2016 +0200 +++ b/java/jdc/README.txt Wed Mar 30 23:40:52 2016 +0200 @@ -8,3 +8,12 @@ make + +RUNNING EXAMPLES +================ + +For example, having build jdc, build and run UnixMathExample from this folder: + + javac -d . examples/UnixMathExample.java + java -Djava.library.path=. UnixMathExample +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/jdc/examples/UnixMathExample.java Wed Mar 30 23:40:52 2016 +0200 @@ -0,0 +1,28 @@ +import org.dyncall.DC; + +class UnixMathExample +{ + public static void main(String args[]) + { + long lib = DC.load("/usr/lib/libm.so"); + System.out.format("libm.so handle: %x\n", lib); + + long f0 = DC.find(lib, "sqrtf"); + System.out.format("found sqrft at: %x\n", f0); + + long f1 = DC.find(lib, "pow"); + System.out.format("found pow at: %x\n", f1); + + long vm = DC.newCallVM(4096); + DC.argFloat(vm, 36.f); + float r0 = DC.callFloat(vm, f0); + System.out.format("sqrtf(36.f) = %f\n", r0); + + DC.reset(vm); + DC.argDouble(vm, 2.); + DC.argDouble(vm, 10.); + double r1 = DC.callDouble(vm, f1); + System.out.format("pow(2., 10.) = %f\n", r1); + } +} +
--- a/java/jdc/org_dyncall_DC.c Wed Mar 30 02:40:56 2016 +0200 +++ b/java/jdc/org_dyncall_DC.c Wed Mar 30 23:40:52 2016 +0200 @@ -10,13 +10,25 @@ jlong JNICALL Java_org_dyncall_DC_load(JNIEnv *pEnv, jclass clazz, jstring s) { - return (jlong)dlLoadLibrary((const char*)s); + jlong l = 0; + const char *sz = (*pEnv)->GetStringUTFChars(pEnv, s, NULL); + if(sz != NULL) { + l = (jlong)dlLoadLibrary(sz); + (*pEnv)->ReleaseStringUTFChars(pEnv, s, sz); //@@@ free + } + return l; } jlong JNICALL Java_org_dyncall_DC_find(JNIEnv *pEnv, jclass clazz, jlong libhandle, jstring s) { - return (jlong)dlFindSymbol((DLLib*)libhandle, (const char*)s); + jlong l = 0; + const char *sz = (*pEnv)->GetStringUTFChars(pEnv, s, NULL); + if(sz != NULL) { + l = (jlong)dlFindSymbol((DLLib*)libhandle, sz); + (*pEnv)->ReleaseStringUTFChars(pEnv, s, sz); + } + return l; } //jint JNICALL Java_org_dyncall_DC_symsCount(JNIEnv *pEnv, jclass clazz, jlong symshandle) @@ -31,42 +43,43 @@ void JNICALL Java_org_dyncall_DC_mode(JNIEnv *pEnv, jclass clazz, jlong vm, jint i) { - dcMode((DCCallVM*)vm, i); + dcMode((DCCallVM*)vm, i);//@@@test } void JNICALL Java_org_dyncall_DC_reset(JNIEnv *pEnv, jclass clazz, jlong vm) { dcReset((DCCallVM*)vm); +//@@@add cleanup code here for temporary memory held by jni } void JNICALL Java_org_dyncall_DC_argBool(JNIEnv *pEnv, jclass clazz, jlong vm, jboolean b) { - dcArgBool((DCCallVM*)vm, b); + dcArgBool((DCCallVM*)vm, b);//@@@test } void JNICALL Java_org_dyncall_DC_argChar(JNIEnv *pEnv, jclass clazz, jlong vm, jbyte b) { - dcArgChar((DCCallVM*)vm, b); + dcArgChar((DCCallVM*)vm, b);//@@@test } void JNICALL Java_org_dyncall_DC_argShort(JNIEnv *pEnv, jclass clazz, jlong vm, jshort s) { - dcArgShort((DCCallVM*)vm, s); + dcArgShort((DCCallVM*)vm, s);//@@@test } void JNICALL Java_org_dyncall_DC_argInt(JNIEnv *pEnv, jclass clazz, jlong vm, jint i) { - dcArgInt((DCCallVM*)vm, i); + dcArgInt((DCCallVM*)vm, i);//@@@test } void JNICALL Java_org_dyncall_DC_argLong(JNIEnv *pEnv, jclass clazz, jlong vm, jlong l) { - dcArgLong((DCCallVM*)vm, l); + dcArgLong((DCCallVM*)vm, l);//@@@test } void JNICALL Java_org_dyncall_DC_argLongLong(JNIEnv *pEnv, jclass clazz, jlong vm, jlong l) { - dcArgLongLong((DCCallVM*)vm, l); + dcArgLongLong((DCCallVM*)vm, l);//@@@test } void JNICALL Java_org_dyncall_DC_argFloat(JNIEnv *pEnv, jclass clazz, jlong vm, jfloat f) @@ -76,57 +89,62 @@ void JNICALL Java_org_dyncall_DC_argDouble(JNIEnv *pEnv, jclass clazz, jlong vm, jdouble d) { - dcArgDouble((DCCallVM*)vm, d); + dcArgDouble((DCCallVM*)vm, d); } void JNICALL Java_org_dyncall_DC_argPointer__JJ(JNIEnv *pEnv, jclass clazz, jlong vm, jlong l) { - dcArgPointer((DCCallVM*)vm, (DCpointer)l); + dcArgPointer((DCCallVM*)vm, (DCpointer)l);//@@@test } void JNICALL Java_org_dyncall_DC_argPointer__JLjava_lang_Object_2(JNIEnv *pEnv, jclass clazz, jlong vm, jobject o) { - dcArgPointer((DCCallVM*)vm, (DCpointer)o); + dcArgPointer((DCCallVM*)vm, (DCpointer)o);//@@@test } void JNICALL Java_org_dyncall_DC_argString(JNIEnv *pEnv, jclass clazz, jlong vm, jstring s) { - dcArgPointer((DCCallVM*)vm, (DCpointer)s); + const char *sz = (*pEnv)->GetStringUTFChars(pEnv, s, NULL); + if(sz != NULL) { + dcArgPointer((DCCallVM*)vm, (DCpointer)sz); + //(*pEnv)->ReleaseStringUTFChars(pEnv, s, sz); + //@@@ free this string when vm is destroyed, reset + } } void JNICALL Java_org_dyncall_DC_callVoid (JNIEnv *pEnv, jclass clazz, jlong vm, jlong target) { - dcCallVoid((DCCallVM*)vm, (DCpointer)target); + dcCallVoid((DCCallVM*)vm, (DCpointer)target);//@@@test } jboolean JNICALL Java_org_dyncall_DC_callBool(JNIEnv *pEnv, jclass clazz, jlong vm, jlong target) { - return dcCallBool((DCCallVM*)vm, (DCpointer)target); + return dcCallBool((DCCallVM*)vm, (DCpointer)target);//@@@test } jbyte JNICALL Java_org_dyncall_DC_callChar(JNIEnv *pEnv, jclass clazz, jlong vm, jlong target) { - return dcCallChar((DCCallVM*)vm, (DCpointer)target); + return dcCallChar((DCCallVM*)vm, (DCpointer)target);//@@@test } jshort JNICALL Java_org_dyncall_DC_callShort(JNIEnv *pEnv, jclass clazz, jlong vm, jlong target) { - return dcCallShort((DCCallVM*)vm, (DCpointer)target); + return dcCallShort((DCCallVM*)vm, (DCpointer)target);//@@@test } jint JNICALL Java_org_dyncall_DC_callInt(JNIEnv *pEnv, jclass clazz, jlong vm, jlong target) { - return dcCallInt((DCCallVM*)vm, (DCpointer)target); + return dcCallInt((DCCallVM*)vm, (DCpointer)target);//@@@test } jlong JNICALL Java_org_dyncall_DC_callLong(JNIEnv *pEnv, jclass clazz, jlong vm, jlong target) { - return dcCallLong((DCCallVM*)vm, (DCpointer)target); + return dcCallLong((DCCallVM*)vm, (DCpointer)target);//@@@test } jlong JNICALL Java_org_dyncall_DC_callLongLong(JNIEnv *pEnv, jclass clazz, jlong vm, jlong target) { - return dcCallLongLong((DCCallVM*)vm, (DCpointer)target); + return dcCallLongLong((DCCallVM*)vm, (DCpointer)target);//@@@test } jfloat JNICALL Java_org_dyncall_DC_callFloat(JNIEnv *pEnv, jclass clazz, jlong vm, jlong target) @@ -141,16 +159,16 @@ jlong JNICALL Java_org_dyncall_DC_callPointer(JNIEnv *pEnv, jclass clazz, jlong vm, jlong target) { - return (jlong)dcCallPointer((DCCallVM*)vm, (DCpointer)target); + return (jlong)dcCallPointer((DCCallVM*)vm, (DCpointer)target);//@@@test } jstring JNICALL Java_org_dyncall_DC_callString(JNIEnv *pEnv, jclass clazz, jlong vm, jlong target) { - return dcCallPointer((DCCallVM*)vm, (DCpointer)target); + return (*pEnv)->NewStringUTF(pEnv, dcCallPointer((DCCallVM*)vm, (DCpointer)target));//@@@test } jint JNICALL Java_org_dyncall_DC_getError(JNIEnv *pEnv, jclass clazz, jlong vm) { - return dcGetError((DCCallVM*)vm); + return dcGetError((DCCallVM*)vm);//@@@test }