-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathJsiConversions.h
More file actions
101 lines (79 loc) · 3.25 KB
/
JsiConversions.h
File metadata and controls
101 lines (79 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#pragma once
#include <set>
#include <type_traits>
#include <jsi/jsi.h>
namespace rnexecutorch::jsiconversion {
using namespace facebook;
// Conversion from jsi to C++ types --------------------------------------------
template <typename T> T getValue(const jsi::Value &val, jsi::Runtime &runtime);
template <>
inline double getValue<double>(const jsi::Value &val, jsi::Runtime &runtime) {
return val.asNumber();
}
template <>
inline bool getValue<bool>(const jsi::Value &val, jsi::Runtime &runtime) {
return val.asBool();
}
template <>
inline std::string getValue<std::string>(const jsi::Value &val,
jsi::Runtime &runtime) {
return val.getString(runtime).utf8(runtime);
}
template <>
inline std::vector<std::string>
getValue<std::vector<std::string>>(const jsi::Value &val,
jsi::Runtime &runtime) {
jsi::Array array = val.asObject(runtime).asArray(runtime);
size_t length = array.size(runtime);
std::vector<std::string> result;
result.reserve(length);
for (size_t i = 0; i < length; ++i) {
jsi::Value element = array.getValueAtIndex(runtime, i);
result.push_back(getValue<std::string>(element, runtime));
}
return result;
}
// C++ set from JS array. Set with heterogenerous look-up (adding std::less<>
// enables querying with std::string_view).
template <>
inline std::set<std::string, std::less<>>
getValue<std::set<std::string, std::less<>>>(const jsi::Value &val,
jsi::Runtime &runtime) {
jsi::Array array = val.asObject(runtime).asArray(runtime);
size_t length = array.size(runtime);
std::set<std::string, std::less<>> result;
for (size_t i = 0; i < length; ++i) {
jsi::Value element = array.getValueAtIndex(runtime, i);
result.insert(getValue<std::string>(element, runtime));
}
return result;
}
// Conversion from C++ types to jsi --------------------------------------------
// Implementation functions might return any type, but in a promise we can only
// return jsi::Value or jsi::Object. For each type being returned
// we add a function here.
inline jsi::Value getJsiValue(std::shared_ptr<jsi::Object> valuePtr,
jsi::Runtime &runtime) {
return std::move(*valuePtr);
}
inline jsi::Value getJsiValue(const std::string &str, jsi::Runtime &runtime) {
return jsi::String::createFromAscii(runtime, str);
}
template <typename Model, typename R, typename... Types>
constexpr std::size_t getArgumentCount(R (Model::*f)(Types...)) {
return sizeof...(Types);
}
template <typename... Types, std::size_t... I>
std::tuple<Types...> fillTupleFromArgs(std::index_sequence<I...>,
const jsi::Value *args,
jsi::Runtime &runtime) {
return std::make_tuple(getValue<Types>(args[I], runtime)...);
}
template <typename Model, typename R, typename... Types>
std::tuple<Types...> createArgsTupleFromJsi(R (Model::*f)(Types...),
const jsi::Value *args,
jsi::Runtime &runtime) {
return fillTupleFromArgs<Types...>(std::index_sequence_for<Types...>{}, args,
runtime);
}
} // namespace rnexecutorch::jsiconversion