Here's small question. I'm trying to consume libcl...
# kotlin-native
r
Here's small question. I'm trying to consume libclang from K/N code. There is a function called
clang_visitChildren
which expects callback having C structs passed by values as parameters. When trying to use it as is, I'm getting the following error:
type CValue<CXCursor> is not supported in callback signature
. Do I understand correctly that the following workaround is the only way to go for now?
s
Unfortunately, yes, in this case you need to add callback wrapper. You can add C code at the end of
.def
file after `---`: https://github.com/JetBrains/kotlin-native/blob/master/INTEROP.md#adding-custom-declarations Also you can make
CallbackWithPtrs
receive some user data using the following:
Copy code
...
// [C++] My wrapper
typedef void (*CallbackWithPtrs) (Input*, void* userData);

struct WrapperCallbackUserData {
    CallbackWithPtrs callback;
    void* userData;
}

void WrapperCallback(Input input, void* userData) {
    WrapperCallbackUserData* data = (WrapperCallbackUserData*) userData;
    CallbackWithPtrs actualCallback = data->callback;
    actualCallback(&input, data->userData);
}

void wrapperVisitor(CallbackWithPtrs callback, void* userData) {
    struct WrapperCallbackUserData data;
    data.callback = callback;
    data.userData = userData;
    visitor(WrapperCallback, (void*) &data);
}         

// [Kotlin/Native] Wrapper usage
wrapperVisitor(staticCFunction({ input: CPointer<Input>, userData: COpaquePointer ->
    // use input.pointed
}))
r
Oh, I forgot about the feature with C code in
.def
file. Yeah, I've decided to skip
userData
from example for simplicity. I'm going to use it with
StableObjPtr
in my own code, of course. Thank you very much!