Skip to content

Commit f5c0606

Browse files
Add jsi::HostObject example to rn-tester (facebook#36909)
Summary: Pull Request resolved: facebook#36909 Changelog: [Internal] Reviewed By: javache Differential Revision: D44978689 fbshipit-source-id: 0e31b5c37a7890744694706994ea672c3e619341
1 parent adb2103 commit f5c0606

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ CustomEnumInt NativeCxxModuleExample::getCustomEnum(
4040
return arg;
4141
}
4242

43+
std::shared_ptr<CustomHostObject> NativeCxxModuleExample::getCustomHostObject(
44+
jsi::Runtime &rt) {
45+
return std::make_shared<CustomHostObject>(
46+
std::make_shared<CustomHostObjectRef>("answer", 42));
47+
}
48+
49+
std::string NativeCxxModuleExample::consumeCustomHostObject(
50+
jsi::Runtime &rt,
51+
std::shared_ptr<CustomHostObject> arg) {
52+
auto value = arg->getValue();
53+
return value->a_ + std::to_string(value->b_);
54+
}
55+
4356
NativeCxxModuleExampleCxxEnumFloat NativeCxxModuleExample::getNumEnum(
4457
jsi::Runtime &rt,
4558
NativeCxxModuleExampleCxxEnumInt arg) {

packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,30 @@ struct Bridging<CustomEnumInt> {
7373
}
7474
};
7575

76+
#pragma mark - jsi::HostObjects
77+
template <typename T>
78+
class HostObjectWrapper : public jsi::HostObject {
79+
public:
80+
HostObjectWrapper(std::shared_ptr<T> value) : value_(std::move(value)) {}
81+
82+
std::shared_ptr<T> getValue() const {
83+
return value_;
84+
}
85+
86+
~HostObjectWrapper() override = default;
87+
88+
private:
89+
std::shared_ptr<T> value_;
90+
};
91+
92+
struct CustomHostObjectRef {
93+
CustomHostObjectRef(std::string a, int32_t b) : a_(a), b_(b) {}
94+
std::string a_;
95+
int32_t b_;
96+
};
97+
98+
using CustomHostObject = HostObjectWrapper<CustomHostObjectRef>;
99+
76100
#pragma mark - implementation
77101
class NativeCxxModuleExample
78102
: public NativeCxxModuleExampleCxxSpec<NativeCxxModuleExample> {
@@ -93,6 +117,12 @@ class NativeCxxModuleExample
93117

94118
CustomEnumInt getCustomEnum(jsi::Runtime &rt, CustomEnumInt arg);
95119

120+
std::shared_ptr<CustomHostObject> getCustomHostObject(jsi::Runtime &rt);
121+
122+
std::string consumeCustomHostObject(
123+
jsi::Runtime &rt,
124+
std::shared_ptr<CustomHostObject> arg);
125+
96126
NativeCxxModuleExampleCxxEnumFloat getNumEnum(
97127
jsi::Runtime &rt,
98128
NativeCxxModuleExampleCxxEnumInt arg);

packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,15 @@ export type ValueStruct = {|
5454
z: ObjectStruct,
5555
|};
5656

57+
export type CustomHostObject = {};
58+
5759
export interface Spec extends TurboModule {
5860
+getArray: (arg: Array<ObjectStruct | null>) => Array<ObjectStruct | null>;
5961
+getBool: (arg: boolean) => boolean;
6062
+getConstants: () => ConstantsStruct;
6163
+getCustomEnum: (arg: EnumInt) => EnumInt;
64+
+getCustomHostObject: () => CustomHostObject;
65+
+consumeCustomHostObject: (customHostObject: CustomHostObject) => string;
6266
+getNumEnum: (arg: EnumInt) => EnumFloat;
6367
+getStrEnum: (arg: EnumNone) => EnumStr;
6468
+getMap: (arg: {[key: string]: ?number}) => {[key: string]: ?number};

packages/rn-tester/js/examples/TurboModule/NativeCxxModuleExampleExample.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ class NativeCxxModuleExampleExample extends React.Component<{||}, State> {
8787
getBool: () => NativeCxxModuleExample?.getBool(true),
8888
getConstants: () => NativeCxxModuleExample?.getConstants(),
8989
getCustomEnum: () => NativeCxxModuleExample?.getCustomEnum(EnumInt.IB),
90+
getCustomHostObject: () =>
91+
NativeCxxModuleExample?.consumeCustomHostObject(
92+
NativeCxxModuleExample?.getCustomHostObject(),
93+
),
9094
getNumEnum: () => NativeCxxModuleExample?.getNumEnum(EnumInt.IB),
9195
getStrEnum: () => NativeCxxModuleExample?.getStrEnum(EnumNone.NB),
9296
getNumber: () => NativeCxxModuleExample?.getNumber(99.95),

0 commit comments

Comments
 (0)