Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Casting to pointer isn't passing by reference #576

Closed
ghost opened this issue Jun 3, 2022 · 7 comments
Closed

Casting to pointer isn't passing by reference #576

ghost opened this issue Jun 3, 2022 · 7 comments
Labels

Comments

@ghost
Copy link

ghost commented Jun 3, 2022

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);

@saudet saudet added the bug label Jun 4, 2022
saudet added a commit that referenced this issue Jun 4, 2022
@saudet
Copy link
Member

saudet commented Jun 4, 2022

I've fixed that in commit 23664f6. Please give it a try with the snapshots: http://bytedeco.org/builds/

@ghost
Copy link
Author

ghost commented Jun 5, 2022

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;
}

saudet added a commit that referenced this issue Jun 6, 2022
@saudet
Copy link
Member

saudet commented Jun 6, 2022

Sure thing, done in commit 307eb90. Enjoy!

@ghost
Copy link
Author

ghost commented Jun 6, 2022

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

@saudet
Copy link
Member

saudet commented Jun 6, 2022

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*?

@ghost
Copy link
Author

ghost commented Jun 6, 2022

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.

@saudet
Copy link
Member

saudet commented Nov 3, 2022

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

@saudet saudet closed this as completed Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant