Skip to content

Commit

Permalink
fix(core): callbacks with C long return types
Browse files Browse the repository at this point in the history
  • Loading branch information
Spasi committed Aug 31, 2019
1 parent a6d7321 commit d836bdf
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ val int = IntegerType("int", PrimitiveMapping.INT)
val intb = PrimitiveType("int", PrimitiveMapping.BOOLEAN4) // integer mapped to boolean
val unsigned = IntegerType("unsigned", PrimitiveMapping.INT, unsigned = true)
val unsignedb = PrimitiveType("unsigned", PrimitiveMapping.BOOLEAN4)
val long = IntegerType("long", PrimitiveMapping.CLONG)
val long_long = IntegerType("long long", PrimitiveMapping.LONG)
val float = PrimitiveType("float", PrimitiveMapping.FLOAT)
val double = PrimitiveType("double", PrimitiveMapping.DOUBLE)
Expand All @@ -39,6 +40,7 @@ val unsigned_char = IntegerType("unsigned char", PrimitiveMapping.BYTE, unsigned
val unsigned_short = IntegerType("unsigned short", PrimitiveMapping.SHORT, unsigned = true)
val unsigned_int = IntegerType("unsigned int", PrimitiveMapping.INT, unsigned = true)
val unsigned_intb = PrimitiveType("unsigned int", PrimitiveMapping.BOOLEAN4)
val unsigned_long = IntegerType("unsigned long", PrimitiveMapping.CLONG, unsigned = true)
val unsigned_long_long = IntegerType("unsigned long long", PrimitiveMapping.LONG, unsigned = true)

// strings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_system_dyncall_DynCall_ndcArgInt(JNIEnv *_
dcArgInt(vm, (DCint)value);
}

JNIEXPORT void JNICALL Java_org_lwjgl_system_dyncall_DynCall_ndcArgLong(JNIEnv *__env, jclass clazz, jlong vmAddress, jint value) {
JNIEXPORT void JNICALL Java_org_lwjgl_system_dyncall_DynCall_ndcArgLong(JNIEnv *__env, jclass clazz, jlong vmAddress, jlong value) {
DCCallVM *vm = (DCCallVM *)(intptr_t)vmAddress;
UNUSED_PARAMS(__env, clazz)
dcArgLong(vm, (DClong)value);
Expand Down Expand Up @@ -129,11 +129,11 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_system_dyncall_DynCall_ndcCallInt(JNIEnv *
return (jint)dcCallInt(vm, funcptr);
}

JNIEXPORT jint JNICALL Java_org_lwjgl_system_dyncall_DynCall_ndcCallLong(JNIEnv *__env, jclass clazz, jlong vmAddress, jlong funcptrAddress) {
JNIEXPORT jlong JNICALL Java_org_lwjgl_system_dyncall_DynCall_ndcCallLong(JNIEnv *__env, jclass clazz, jlong vmAddress, jlong funcptrAddress) {
DCCallVM *vm = (DCCallVM *)(intptr_t)vmAddress;
DCpointer funcptr = (DCpointer)(intptr_t)funcptrAddress;
UNUSED_PARAMS(__env, clazz)
return (jint)dcCallLong(vm, funcptr);
return (jlong)dcCallLong(vm, funcptr);
}

JNIEXPORT jlong JNICALL Java_org_lwjgl_system_dyncall_DynCall_ndcCallLongLong(JNIEnv *__env, jclass clazz, jlong vmAddress, jlong funcptrAddress) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_system_dyncall_DynCallback_ndcbArgInt(JNIE
return (jint)dcbArgInt(args);
}

JNIEXPORT jint JNICALL Java_org_lwjgl_system_dyncall_DynCallback_ndcbArgLong(JNIEnv *__env, jclass clazz, jlong argsAddress) {
JNIEXPORT jlong JNICALL Java_org_lwjgl_system_dyncall_DynCallback_ndcbArgLong(JNIEnv *__env, jclass clazz, jlong argsAddress) {
DCArgs *args = (DCArgs *)(intptr_t)argsAddress;
UNUSED_PARAMS(__env, clazz)
return (jint)dcbArgLong(args);
return (jlong)dcbArgLong(args);
}

JNIEXPORT jlong JNICALL Java_org_lwjgl_system_dyncall_DynCallback_ndcbArgLongLong(JNIEnv *__env, jclass clazz, jlong argsAddress) {
Expand All @@ -91,10 +91,10 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_system_dyncall_DynCallback_ndcbArgUInt(JNI
return (jint)dcbArgUInt(args);
}

JNIEXPORT jint JNICALL Java_org_lwjgl_system_dyncall_DynCallback_ndcbArgULong(JNIEnv *__env, jclass clazz, jlong argsAddress) {
JNIEXPORT jlong JNICALL Java_org_lwjgl_system_dyncall_DynCallback_ndcbArgULong(JNIEnv *__env, jclass clazz, jlong argsAddress) {
DCArgs *args = (DCArgs *)(intptr_t)argsAddress;
UNUSED_PARAMS(__env, clazz)
return (jint)dcbArgULong(args);
return (jlong)dcbArgULong(args);
}

JNIEXPORT jlong JNICALL Java_org_lwjgl_system_dyncall_DynCallback_ndcbArgULongLong(JNIEnv *__env, jclass clazz, jlong argsAddress) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,15 @@ public static void dcArgInt(@NativeType("DCCallVM *") long vm, @NativeType("DCin
// --- [ dcArgLong ] ---

/** Unsafe version of: {@link #dcArgLong ArgLong} */
public static native void ndcArgLong(long vm, int value);
public static native void ndcArgLong(long vm, long value);

/**
* Binds a {@code long} argument.
*
* @param vm a {@code CallVM} instance
* @param value the argument value
*/
public static void dcArgLong(@NativeType("DCCallVM *") long vm, @NativeType("DClong") int value) {
public static void dcArgLong(@NativeType("DCCallVM *") long vm, @NativeType("DClong") long value) {
if (CHECKS) {
check(vm);
}
Expand Down Expand Up @@ -476,7 +476,7 @@ public static int dcCallInt(@NativeType("DCCallVM *") long vm, @NativeType("DCpo
// --- [ dcCallLong ] ---

/** Unsafe version of: {@link #dcCallLong CallLong} */
public static native int ndcCallLong(long vm, long funcptr);
public static native long ndcCallLong(long vm, long funcptr);

/**
* Calls the function specified by {@code funcptr} with the arguments bound to the {@code CallVM} and returns.
Expand All @@ -488,7 +488,7 @@ public static int dcCallInt(@NativeType("DCCallVM *") long vm, @NativeType("DCpo
* @param funcptr the function pointer
*/
@NativeType("DClong")
public static int dcCallLong(@NativeType("DCCallVM *") long vm, @NativeType("DCpointer") long funcptr) {
public static long dcCallLong(@NativeType("DCCallVM *") long vm, @NativeType("DCpointer") long funcptr) {
if (CHECKS) {
check(vm);
check(funcptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,15 @@ public static int dcbArgInt(@NativeType("DCArgs *") long args) {
// --- [ dcbArgLong ] ---

/** Unsafe version of: {@link #dcbArgLong ArgLong} */
public static native int ndcbArgLong(long args);
public static native long ndcbArgLong(long args);

/**
* Returns the next {@code long} argument.
*
* @param args the function arguments
*/
@NativeType("DClong")
public static int dcbArgLong(@NativeType("DCArgs *") long args) {
public static long dcbArgLong(@NativeType("DCArgs *") long args) {
if (CHECKS) {
check(args);
}
Expand Down Expand Up @@ -332,15 +332,15 @@ public static int dcbArgUInt(@NativeType("DCArgs *") long args) {
// --- [ dcbArgULong ] ---

/** Unsafe version of: {@link #dcbArgULong ArgULong} */
public static native int ndcbArgULong(long args);
public static native long ndcbArgULong(long args);

/**
* Returns the next {@code unsigned long} argument.
*
* @param args the function arguments
*/
@NativeType("DClong")
public static int dcbArgULong(@NativeType("DCArgs *") long args) {
public static long dcbArgULong(@NativeType("DCArgs *") long args) {
if (CHECKS) {
check(args);
}
Expand Down
9 changes: 6 additions & 3 deletions modules/lwjgl/core/src/main/c/org_lwjgl_system_Callback.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ static jmethodID
javaCallbackS,
javaCallbackI,
javaCallbackJ,
javaCallbackN,
javaCallbackF,
javaCallbackD,
javaCallbackP;
Expand Down Expand Up @@ -83,6 +84,7 @@ DEFINE_CB_HANDLER(B, jbyte, 'c', Byte)
DEFINE_CB_HANDLER(S, jshort, 's', Short)
DEFINE_CB_HANDLER(I, jint, 'i', Int)
DEFINE_CB_HANDLER(J, jlong, 'l', Long)
DEFINE_CB_HANDLER(N, long, 'j', Long)
DEFINE_CB_HANDLER(F, jfloat, 'f', Float)
DEFINE_CB_HANDLER(D, jdouble, 'd', Double)
DEFINE_CB_HANDLER(P, intptr_t, 'p', Long)
Expand All @@ -104,9 +106,10 @@ JNIEXPORT void JNICALL Java_org_lwjgl_system_Callback_getNativeCallbacks(JNIEnv
SETUP_CALLBACK(3, S)
SETUP_CALLBACK(4, I)
SETUP_CALLBACK(5, J)
SETUP_CALLBACK(6, F)
SETUP_CALLBACK(7, D)
SETUP_CALLBACK(8, P)
SETUP_CALLBACK(6, N)
SETUP_CALLBACK(7, F)
SETUP_CALLBACK(8, D)
SETUP_CALLBACK(9, P)
}

EXTERN_C_EXIT
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public abstract class Callback implements Pointer, NativeResource {
SHORT,
INT,
LONG,
CLONG,
FLOAT,
DOUBLE,
PTR;
Expand All @@ -47,6 +48,7 @@ public abstract class Callback implements Pointer, NativeResource {
CallbackI.S.class.getDeclaredMethod("callback", params),
CallbackI.I.class.getDeclaredMethod("callback", params),
CallbackI.J.class.getDeclaredMethod("callback", params),
CallbackI.N.class.getDeclaredMethod("callback", params),
CallbackI.F.class.getDeclaredMethod("callback", params),
CallbackI.D.class.getDeclaredMethod("callback", params),
CallbackI.P.class.getDeclaredMethod("callback", params)
Expand All @@ -62,6 +64,7 @@ public abstract class Callback implements Pointer, NativeResource {
SHORT = callbacks.get();
INT = callbacks.get();
LONG = callbacks.get();
CLONG = callbacks.get();
FLOAT = callbacks.get();
DOUBLE = callbacks.get();
PTR = callbacks.get();
Expand Down Expand Up @@ -150,6 +153,8 @@ private static long getNativeFunction(char type) {
return INT;
case 'l':
return LONG;
case 'j':
return CLONG;
case 'p':
return PTR;
case 'f':
Expand Down
12 changes: 12 additions & 0 deletions modules/lwjgl/core/src/main/java/org/lwjgl/system/CallbackI.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ interface J extends CallbackI {
long callback(long args);
}

/** A {@code Callback} that returns a C long value. */
interface N extends CallbackI {
/**
* Will be called by native code.
*
* @param args pointer to a {@code DCArgs} iterator
*
* @return the value to store to the result {@code DCValue}
*/
long callback(long args);
}

/** A {@code Callback} that returns a float value. */
interface F extends CallbackI {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ val DCshort = typedef(short, "DCshort")
val DCushort = typedef(unsigned_short, "DCushort")
val DCint = typedef(int, "DCint")
val DCuint = typedef(unsigned_int, "DCuint")
val DClong = IntegerType("DClong", PrimitiveMapping.INT)
val DCulong = IntegerType("DCulong", PrimitiveMapping.INT, unsigned = true)
val DClonglong = IntegerType("DClonglong", PrimitiveMapping.LONG)
val DCulonglong = IntegerType("DCulonglong", PrimitiveMapping.LONG, unsigned = true)
val DClong = typedef(long, "DClong")
val DCulong = typedef(unsigned_long, "DCulong")
val DClonglong = typedef(long_long, "DClonglong")
val DCulonglong = typedef(unsigned_long_long, "DCulonglong")
val DCfloat = typedef(float, "DCfloat")
val DCdouble = typedef(double, "DCdouble")
val DCpointer = typedef(opaque_p, "DCpointer")
Expand Down

0 comments on commit d836bdf

Please sign in to comment.