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

fix: Move ExtendableHybridClass to it's own file with explanation #493

Merged
merged 3 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import androidx.annotation.Keep
import com.facebook.jni.HybridData
import com.facebook.proguard.annotations.DoNotStrip

interface ExtendableHybridClass {
fun updateNative(hybridData: HybridData)
}

/**
* A base class for all Kotlin-based HybridObjects.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.margelo.nitro.core

import com.facebook.jni.HybridData

/**
* Represents an fbjni Hybrid Class (aka a Java class with an accompanying C++ class)
* that supports inheritance.
*
* Each fbjni Hybrid Class initializes it's `mHybridData` using an `initHybrid()` method,
* which initializes the respective C++ class this was bound to.

* Assuming we have Java class `B` which extends Java class `A`, and a C++
* inheritance chain that reflects the Java inheritance chain (C++ class `B` which extends C++ class `A`),
* we would have two `initHybrid()` methods - one for `A` and one for `B`.
* - When you initialize an instance of `A`, it will initialize `A`'s C++ part.
* - When you initialize an instance of `B`, it will initialize `B`'s C++ part, as well as `A`'s C++ part, since
* the native `A.initHybrid()` method cannot be overridden in `B` - both have their own `initHybrid()` methods!
*
* To fix this issue, we have `ExtendableHybridClass`. In an extendable HybridClass, we initialize `mHybridData`,
* but we pass it upwards the inheritance chain after initializing self using `updateNative(mHybridData)`.
* This way the actual `mHybridData` instance is the lowest class in the chain - `B`.
*/
interface ExtendableHybridClass {
/**
* Update the `hybridData` (C++ part) this Hybrid Class is referencing.
* Call this with `hybridData` from a subclass to properly fill in the inheritance chain.
*/
fun updateNative(hybridData: HybridData)
}
Loading