Skip to content

Commit c30f97b

Browse files
committed
feat: Add std::optional<T> to JSIConverter
1 parent c93a117 commit c30f97b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

package/cpp/jsi/JSIConverter.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@ template <> struct JSIConverter<bool> {
7373
}
7474
};
7575

76+
template <typename TInner> struct JSIConverter<std::optional<TInner>> {
77+
static std::optional<TInner> fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
78+
if (arg.isUndefined() || arg.isNull()) {
79+
return std::nullopt;
80+
} else {
81+
return JSIConverter<TInner>::fromJSI(runtime, std::move(arg));
82+
}
83+
}
84+
static jsi::Value toJSI(jsi::Runtime& runtime, std::optional<TInner> arg) {
85+
if (arg == std::nullopt) {
86+
return jsi::Value::undefined();
87+
} else {
88+
return JSIConverter<TInner>::toJSI(runtime, arg);
89+
}
90+
}
91+
};
92+
7693
template <> struct JSIConverter<std::string> {
7794
static std::string fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
7895
return arg.asString(runtime).utf8(runtime);

0 commit comments

Comments
 (0)