@@ -15,7 +15,7 @@ namespace margelo {
15
15
16
16
using namespace facebook ;
17
17
18
- class HybridObject : public jsi ::HostObject {
18
+ class HybridObject : public jsi ::HostObject, std::enable_shared_from_this<HybridObject> {
19
19
public:
20
20
struct HybridFunction {
21
21
jsi::HostFunctionType function;
@@ -29,6 +29,14 @@ class HybridObject : public jsi::HostObject {
29
29
jsi::Value get (facebook::jsi::Runtime& runtime, const facebook::jsi::PropNameID& propName) override ;
30
30
std::vector<jsi::PropNameID> getPropertyNames (facebook::jsi::Runtime& runtime) override ;
31
31
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
+
32
40
/* *
33
41
* Loads all native methods of this `HybridObject` to be exposed to JavaScript.
34
42
* Example:
@@ -73,7 +81,6 @@ class HybridObject : public jsi::HostObject {
73
81
74
82
template <typename Derived, typename ReturnType, typename ... Args>
75
83
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.
77
84
return [this , derivedInstance, method](jsi::Runtime& runtime, const jsi::Value& thisVal, const jsi::Value* args,
78
85
size_t count) -> jsi::Value {
79
86
// Call the actual method with JSI values as arguments and return a JSI value again.
0 commit comments