Hello everyone. How to call functions from Kotlin ...
# kotlin-native
d
Hello everyone. How to call functions from Kotlin Native in C++ side that take lambda as arguments? I compiled Kotlin Native code into a static library and included it in my C++ application. In Kotlin, I have this code:
Copy code
actual fun platform() = "Shared Linux X64"

actual fun triggerLambda(callback: () -> Unit) {
    callback()
}
Next, in my C++ application, I added the corresponding libshared.a and libshared_api.h files. In these files, the functions I need have the following structure, I will only show a part of it:
Copy code
typedef void* libshared_KNativePtr;
typedef struct {
  libshared_KNativePtr pinned;
} libshared_kref_kotlin_Function0;
// irrelevant code skipped here
struct {
  struct {
    struct {
      struct {
        // irrelevant code skipped here
        const char* (*platform)();
        void (*triggerLambda)(libshared_kref_kotlin_Function0 callback);
      } root;
  } kotlin;
} libshared_ExportedSymbols;
extern libshared_ExportedSymbols* libshared_symbols(void);
So, in my application, I can easily call the first function, for example like this:
Copy code
auto ktText = libshared_symbols()->kotlin.root.platform();
And it works. But I am having trouble calling the second function:
Copy code
class KotlinNativeVM: //...

  void foo() {
    // works
    auto ktText = libshared_symbols()->kotlin.root.platform();
    
    libshared_kref_kotlin_Function0 callback; 
    auto lambda = []() {
      // ...
    };
    callback.pinned = reinterpret_cast<libshared_KNativePtr>(&lambda);
    // The program crashes
    libshared_symbols()->kotlin.root.triggerLambda(callback);
  }
}
Because such call causing app to crash.