javacpp icon indicating copy to clipboard operation
javacpp copied to clipboard

Casting to pointer isn't passing by reference

Open unit-a-user opened this issue 2 years ago • 6 comments

I would like to let the address from LWJGL into JavaCpp of the long type.

What we doing: public native int add(@ByRef @Cast("vk::DescriptorImageInfo*") long ptr);

Result CPP code:

JNIEXPORT jint JNICALL Java_org_helixd2s_yavulkanmod_wrapper_Alter_00024BucketOfDescriptorImageInfo_add(JNIEnv* env, jobject obj, jlong arg0) {
    ::cpp21::bucket<vk::DescriptorImageInfo>* ptr = (::cpp21::bucket<vk::DescriptorImageInfo>*)jlong_to_ptr(env->GetLongField(obj, JavaCPP_addressFID));
    if (ptr == NULL) {
        env->ThrowNew(JavaCPP_getClass(env, 9), "This pointer address is NULL.");
        return 0;
    }
    jlong position = env->GetLongField(obj, JavaCPP_positionFID);
    ptr += position;
    jint rarg = 0;
    jthrowable exc = NULL;
    try {
        int rval = ptr->add((vk::DescriptorImageInfo*)arg0);
        rarg = (jint)rval;
    } catch (...) {
        exc = JavaCPP_handleException(env, 8);
    }

    if (exc != NULL) {
        env->Throw(exc);
    }
    return rarg;
}

Compiler error:

Severity    Code    Description    Project    File    Line    Suppression State
Error    C2664    'uintptr_t cpp21::bucket_<vk::DescriptorImageInfo,std::vector,cpp21::shared_vector>::add(const T &)': cannot convert argument 1 from 'vk::DescriptorImageInfo *' to 'const T &'    YAV    C:\VULKAN\YetAnotherVulkanMod\src\main\cpp\jniAlter.cpp    1582

Should to be line: int rval = ptr->add(*(vk::DescriptorImageInfo*)arg0);

unit-a-user avatar Jun 03 '22 10:06 unit-a-user

I've fixed that in commit https://github.com/bytedeco/javacpp/commit/23664f6f832efa56185d46de62334f0b9d70e0ee. Please give it a try with the snapshots: http://bytedeco.org/builds/

saudet avatar Jun 04 '22 13:06 saudet

I have similar issue, but for getters. Needs reversal. I copy content from that issue:

I expected line such as (uintptr_t)&ptr->extent;

        @Name("extent")
        @MemberGetter @Cast("uintptr_t") @ByRef public native long getExtent();

But getting wrong code:

JNIEXPORT jlong JNICALL Java_org_helixd2s_yavulkanmod_alter_header_CreateInfo_00024ImageCreateInfo_getExtent(JNIEnv* env, jobject obj) {
    ::alter::ImageCreateInfo* ptr = (::alter::ImageCreateInfo*)jlong_to_ptr(env->GetLongField(obj, JavaCPP_addressFID));
    if (ptr == NULL) {
        env->ThrowNew(JavaCPP_getClass(env, 7), "This pointer address is NULL.");
        return 0;
    }
    jlong position = env->GetLongField(obj, JavaCPP_positionFID);
    ptr += position;
    jlong rarg = 0;
    jthrowable exc = NULL;
    try {
        uintptr_t rval = (uintptr_t)ptr->extent;
        rarg = (jlong)rval;
    } catch (...) {
        exc = JavaCPP_handleException(env, 9);
    }

    if (exc != NULL) {
        env->Throw(exc);
    }
    return rarg;
}

ghost avatar Jun 05 '22 18:06 ghost

Sure thing, done in commit https://github.com/bytedeco/javacpp/commit/307eb9097b779ee2281b18b1b30024e43ddda71e. Enjoy!

saudet avatar Jun 06 '22 11:06 saudet

Something go wrong: uintptr_t rval = (uintptr_t)ptr->extent;, needs with &. I tried to update dependencies. You sure about && cast.endsWith("*)")? How about uintptr_t or intptr_t? Look again to @Cast: @Name("extent") @MemberGetter @Cast("uintptr_t") @ByRef public native long getExtent(); Which means should to be: (uintptr_t)&ptr->extent;, not somebody like (some*) I looked to commit

ghost avatar Jun 06 '22 13:06 ghost

uintptr_t isn't a pointer value, it's an integer value, just like long, so that seems correct to me. What's the reason you cannot use something like void*?

saudet avatar Jun 06 '22 13:06 saudet

The void* pointer is not so clear-cut. The point is that it can only be converted to long explicitly. But I noticed that the generator substitutes rarg = (jlong)rval; after this cast. Other than that, I don't think there are any questions yet.

ghost avatar Jun 06 '22 13:06 ghost

Fixes for this issue have been released with JavaCPP 1.5.8. Thanks for reporting and for testing!

saudet avatar Nov 03 '22 04:11 saudet