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

feat: New Callback implementation that extends () -> T in Kotlin (faster!) #470

Merged
merged 21 commits into from
Jan 9, 2025

Conversation

mrousavy
Copy link
Owner

@mrousavy mrousavy commented Jan 9, 2025

This PR changes the way how functions work in Kotlin/Android.
Previously we only had native functions that can be passed to Kotlin.
Now we can also have actual Kotlin functions (lambdas) that can be passed to C++.

So for a Func_void, we now have

  • JFunc_void: base interface with an invoke() method
  • JFunc_void_cxx: The C++-based implementation that holds a std::function<void()> - most likely from a jsi::Function (JNI)
  • JFunc_void_java: The Java based implementation that holds a () -> Unit, which can be called from C++ using JNI

Also, instead of explicitly converting a Func_void to a () -> Unit (toLambda()), it now no longer needs to be converted to one since it always is one. The Java interface Func_void **inherits from () -> Unit.

fun interface Func_void: () -> Unit {
  //                     ^ cool part
  override fun invoke() {
    // ...
  }
}

This way Func_void is always a callable.


Also I added these two tests to ensure this won't break again:

  • optionalCallback?: (number) => void test (callback property, instead of method)
  • complexReturn(): T to harden requiresSpecialBridging for Kotlin

This PR fixes a few bugs around callbacks and structs, e.g.:

  • Structs that hold a Callback couldn't be passed from JS -> C++ -> Kotlin
  • Callbacks couldn't be passed from Kotlin -> C++ -> JS

Copy link

vercel bot commented Jan 9, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
nitro-docs ⬜️ Skipped (Inspect) Jan 9, 2025 1:45pm

@mrousavy mrousavy changed the title feat: Add optionalCallback property test feat: New Callback implementation that extends () -> T in Kotlin (faster!) Jan 9, 2025
@mrousavy mrousavy merged commit 07d96a0 into main Jan 9, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant