Skip to content

Commit

Permalink
Add GraphNode example for Cxx TMs (facebook#41766)
Browse files Browse the repository at this point in the history
Summary:

Changelog: [Internal]

Adds a simple example showing a recursive node, stored inside a collection in a Cxx TM.

Currently we can't auto-generate [the necessary C++ Types](https://reactnative.dev/docs/next/the-new-architecture/cxx-custom-types#struct-generator) - but we can add it later if this scenarios becomes really common.

Differential Revision: D51783974
  • Loading branch information
christophpurrer authored and facebook-github-bot committed Dec 2, 2023
1 parent 3854735 commit 6339474
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ std::string NativeCxxModuleExample::consumeCustomHostObject(
return value->a_ + std::to_string(value->b_);
}

GraphNode NativeCxxModuleExample::getGraphNode(
jsi::Runtime& rt,
GraphNode arg) {
if (arg.neighbors) {
arg.neighbors->emplace_back(GraphNode{.label = "top"});
arg.neighbors->emplace_back(GraphNode{.label = "down"});
}
return arg;
}

NativeCxxModuleExampleCxxEnumFloat NativeCxxModuleExample::getNumEnum(
jsi::Runtime& rt,
NativeCxxModuleExampleCxxEnumInt arg) {
Expand Down
38 changes: 38 additions & 0 deletions packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,42 @@ struct CustomHostObjectRef {

using CustomHostObject = HostObjectWrapper<CustomHostObjectRef>;

#pragma mark - recursive objects
struct GraphNode {
std::string label;
std::optional<std::vector<GraphNode>> neighbors;
};

template <>
struct Bridging<GraphNode> {
static GraphNode fromJs(
jsi::Runtime& rt,
const jsi::Object& value,
const std::shared_ptr<CallInvoker>& jsInvoker) {
GraphNode result{
bridging::fromJs<std::string>(
rt, value.getProperty(rt, "label"), jsInvoker),
bridging::fromJs<std::optional<std::vector<GraphNode>>>(
rt, value.getProperty(rt, "neighbors"), jsInvoker)};
return result;
}

static jsi::Object toJs(
jsi::Runtime& rt,
const GraphNode value,
const std::shared_ptr<CallInvoker>& jsInvoker) {
auto result = facebook::jsi::Object(rt);
result.setProperty(rt, "label", bridging::toJs(rt, value.label, jsInvoker));
if (value.neighbors) {
result.setProperty(
rt,
"neighbors",
bridging::toJs(rt, value.neighbors.value(), jsInvoker));
}
return result;
}
};

#pragma mark - implementation
class NativeCxxModuleExample
: public NativeCxxModuleExampleCxxSpec<NativeCxxModuleExample> {
Expand Down Expand Up @@ -120,6 +156,8 @@ class NativeCxxModuleExample
jsi::Runtime& rt,
std::shared_ptr<CustomHostObject> arg);

GraphNode getGraphNode(jsi::Runtime& rt, GraphNode arg);

NativeCxxModuleExampleCxxEnumFloat getNumEnum(
jsi::Runtime& rt,
NativeCxxModuleExampleCxxEnumInt arg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,19 @@ export type ValueStruct = {|

export type CustomHostObject = {};

export type GraphNode = {
label: string,
neighbors?: Array<GraphNode>,
};

export interface Spec extends TurboModule {
+getArray: (arg: Array<ObjectStruct | null>) => Array<ObjectStruct | null>;
+getBool: (arg: boolean) => boolean;
+getConstants: () => ConstantsStruct;
+getCustomEnum: (arg: EnumInt) => EnumInt;
+getCustomHostObject: () => CustomHostObject;
+consumeCustomHostObject: (customHostObject: CustomHostObject) => string;
+getGraphNode: (arg: GraphNode) => GraphNode;
+getNumEnum: (arg: EnumInt) => EnumFloat;
+getStrEnum: (arg: EnumNone) => EnumStr;
+getMap: (arg: {[key: string]: ?number}) => {[key: string]: ?number};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type Examples =
| 'getBool'
| 'getConstants'
| 'getCustomEnum'
| 'getCustomHostObject'
| 'getGraphNode'
| 'getNumEnum'
| 'getStrEnum'
| 'getMap'
Expand Down Expand Up @@ -102,6 +104,11 @@ class NativeCxxModuleExampleExample extends React.Component<{||}, State> {
NativeCxxModuleExample?.consumeCustomHostObject(
NativeCxxModuleExample?.getCustomHostObject(),
),
getGraphNode: () =>
NativeCxxModuleExample?.getGraphNode({
label: 'root',
neighbors: [{label: 'left'}, {label: 'right'}],
}),
getNumEnum: () => NativeCxxModuleExample?.getNumEnum(EnumInt.IB),
getStrEnum: () => NativeCxxModuleExample?.getStrEnum(EnumNone.NB),
getNumber: () => NativeCxxModuleExample?.getNumber(99.95),
Expand Down

0 comments on commit 6339474

Please sign in to comment.