-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
NitroModules.box(...)
to support using Nitro Modules from…
… any Runtime/Worklets context
- Loading branch information
Showing
5 changed files
with
122 additions
and
5 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
packages/react-native-nitro-modules/cpp/core/BoxedHybridObject.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// BoxedHybridObject.cpp | ||
// NitroModules | ||
// | ||
// Created by Marc Rousavy on 17.09.24. | ||
// | ||
|
||
#include "BoxedHybridObject.hpp" | ||
|
||
namespace margelo::nitro { | ||
|
||
jsi::Value BoxedHybridObject::get(jsi::Runtime& runtime, const jsi::PropNameID& propName) { | ||
std::string name = propName.utf8(runtime); | ||
|
||
if (name == "unbox") { | ||
return jsi::Function::createFromHostFunction( | ||
runtime, jsi::PropNameID::forUtf8(runtime, "unbox"), 0, | ||
[hybridObject = _hybridObject](jsi::Runtime& runtime, const jsi::Value& thisArg, const jsi::Value* args, | ||
size_t count) -> jsi::Value { return hybridObject->toObject(runtime); }); | ||
} | ||
|
||
return jsi::Value::undefined(); | ||
} | ||
|
||
} // namespace margelo::nitro |
35 changes: 35 additions & 0 deletions
35
packages/react-native-nitro-modules/cpp/core/BoxedHybridObject.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// Created by Marc Rousavy on 21.02.24. | ||
// | ||
|
||
#pragma once | ||
|
||
#include "HybridObject.hpp" | ||
#include <jsi/jsi.h> | ||
#include <memory> | ||
|
||
namespace margelo::nitro { | ||
|
||
using namespace facebook; | ||
|
||
/** | ||
* Represents a `HybridObject` that has been boxed into a `jsi::HostObject`. | ||
* | ||
* While `HybridObject`s are runtime agnostic, some threading/worklet libraries do not support copying over objects | ||
* with `jsi::NativeState` and a prototype chain (which is what a `HybridObject` is), so Nitro offers support for | ||
* boxing those `HybridObject`s into a type that those libraries support - which is a `jsi::HostObject`. | ||
* | ||
* Simply call `unbox()` on this `jsi::HostObject` from the new Runtime/context to get the `HybridObject` again. | ||
*/ | ||
class BoxedHybridObject : public jsi::HostObject { | ||
public: | ||
explicit BoxedHybridObject(const std::shared_ptr<HybridObject>& hybridObject) : _hybridObject(hybridObject) {} | ||
|
||
public: | ||
jsi::Value get(jsi::Runtime& runtime, const jsi::PropNameID& propName) override; | ||
|
||
private: | ||
std::shared_ptr<HybridObject> _hybridObject; | ||
}; | ||
|
||
} // namespace margelo::nitro |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters