Skip to content

Commit b300dea

Browse files
committed
Create HybridObject::shared()
1 parent 43fe4b6 commit b300dea

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

package/cpp/jsi/HybridObject.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace margelo {
1515

1616
using namespace facebook;
1717

18-
class HybridObject : public jsi::HostObject {
18+
class HybridObject : public jsi::HostObject, std::enable_shared_from_this<HybridObject> {
1919
public:
2020
struct HybridFunction {
2121
jsi::HostFunctionType function;
@@ -29,6 +29,14 @@ class HybridObject : public jsi::HostObject {
2929
jsi::Value get(facebook::jsi::Runtime& runtime, const facebook::jsi::PropNameID& propName) override;
3030
std::vector<jsi::PropNameID> getPropertyNames(facebook::jsi::Runtime& runtime) override;
3131

32+
/**
33+
* Get the `std::shared_ptr` instance of this HybridObject.
34+
* The HybridObject must be managed inside a `shared_ptr` already, otherwise this will fail.
35+
*/
36+
std::shared_ptr<HybridObject> shared() {
37+
return shared_from_this();
38+
}
39+
3240
/**
3341
* Loads all native methods of this `HybridObject` to be exposed to JavaScript.
3442
* Example:
@@ -73,7 +81,6 @@ class HybridObject : public jsi::HostObject {
7381

7482
template <typename Derived, typename ReturnType, typename... Args>
7583
jsi::HostFunctionType createHybridMethod(ReturnType (Derived::*method)(Args...), Derived* derivedInstance) {
76-
// TODO(marc): Use std::shared_ptr<T> instead of T* to keep a strong reference of derivedClass.
7784
return [this, derivedInstance, method](jsi::Runtime& runtime, const jsi::Value& thisVal, const jsi::Value* args,
7885
size_t count) -> jsi::Value {
7986
// Call the actual method with JSI values as arguments and return a JSI value again.

0 commit comments

Comments
 (0)