|
5 | 5 | #pragma once |
6 | 6 |
|
7 | 7 | #include <memory> |
8 | | -#include <array> |
9 | | -#include <future> |
10 | 8 | #include <vector> |
11 | 9 | #include <string> |
12 | 10 | #include <utility> |
13 | 11 | #include <type_traits> |
14 | | -#include <unordered_map> |
15 | 12 | #include <limits> |
16 | 13 | #include <variant> |
17 | 14 | #include <map> |
|
20 | 17 | #include <jsi/jsi.h> |
21 | 18 |
|
22 | 19 | #include "RNFEnumMapper.h" |
23 | | -#include "RNFJSIHelper.h" |
24 | 20 | #include "RNFPromise.h" |
25 | 21 |
|
26 | 22 | #include "Unions.h" |
@@ -275,102 +271,6 @@ template <typename ElementType> struct JSIConverter<std::vector<ElementType>> { |
275 | 271 | } |
276 | 272 | }; |
277 | 273 |
|
278 | | -// std::unordered_map<std::string, T> <> Record<string, T> |
279 | | -template <typename ValueType> struct JSIConverter<std::unordered_map<std::string, ValueType>> { |
280 | | - static std::unordered_map<std::string, ValueType> fromJSI(jsi::Runtime& runtime, const jsi::Value& arg, bool outOfBound) { |
281 | | - jsi::Object object = arg.asObject(runtime); |
282 | | - jsi::Array propertyNames = object.getPropertyNames(runtime); |
283 | | - size_t length = propertyNames.size(runtime); |
284 | | - |
285 | | - std::unordered_map<std::string, ValueType> map; |
286 | | - map.reserve(length); |
287 | | - for (size_t i = 0; i < length; ++i) { |
288 | | - std::string key = propertyNames.getValueAtIndex(runtime, i).asString(runtime).utf8(runtime); |
289 | | - jsi::Value value = object.getProperty(runtime, key.c_str()); |
290 | | - map.emplace(key, JSIConverter<ValueType>::fromJSI(runtime, value, outOfBound)); |
291 | | - } |
292 | | - return map; |
293 | | - } |
294 | | - static jsi::Value toJSI(jsi::Runtime& runtime, const std::unordered_map<std::string, ValueType>& map) { |
295 | | - jsi::Object object(runtime); |
296 | | - for (const auto& pair : map) { |
297 | | - jsi::Value value = JSIConverter<ValueType>::toJSI(runtime, pair.second); |
298 | | - jsi::String key = jsi::String::createFromUtf8(runtime, pair.first); |
299 | | - object.setProperty(runtime, key, std::move(value)); |
300 | | - } |
301 | | - return object; |
302 | | - } |
303 | | -}; |
304 | | - |
305 | | -// HostObject (non-NativeState) <> {} |
306 | | -template <typename T> struct is_shared_ptr_to_host_object : std::false_type {}; |
307 | | - |
308 | | -// Only match HostObject types that are NOT NativeState types |
309 | | -template <typename T> |
310 | | -struct is_shared_ptr_to_host_object<std::shared_ptr<T>> |
311 | | - : std::bool_constant<std::is_base_of_v<jsi::HostObject, T> && |
312 | | - !std::is_base_of_v<jsi::NativeState, T>> {}; |
313 | | - |
314 | | -template <typename T> struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_host_object<T>::value>> { |
315 | | - using TPointee = typename T::element_type; |
316 | | - |
317 | | -#if DEBUG |
318 | | - inline static std::string getFriendlyTypename() { |
319 | | - std::string name = std::string(typeid(TPointee).name()); |
320 | | -#if __has_include(<cxxabi.h>) |
321 | | - int status = 0; |
322 | | - char* demangled_name = abi::__cxa_demangle(name.c_str(), NULL, NULL, &status); |
323 | | - if (status == 0) { |
324 | | - name = demangled_name; |
325 | | - std::free(demangled_name); |
326 | | - } |
327 | | -#endif |
328 | | - return name; |
329 | | - } |
330 | | - |
331 | | - inline static std::string invalidTypeErrorMessage(const std::string& typeDescription, const std::string& reason) { |
332 | | - return "Cannot convert \"" + typeDescription + "\" to HostObject<" + getFriendlyTypename() + ">! " + reason; |
333 | | - } |
334 | | -#endif |
335 | | - |
336 | | - static T fromJSI(jsi::Runtime& runtime, const jsi::Value& arg, bool outOfBound) { |
337 | | -#if DEBUG |
338 | | - if (arg.isUndefined()) { |
339 | | - [[unlikely]]; |
340 | | - throw jsi::JSError(runtime, invalidTypeErrorMessage("undefined", "It is undefined!")); |
341 | | - } |
342 | | - if (!arg.isObject()) { |
343 | | - [[unlikely]]; |
344 | | - std::string stringRepresentation = arg.toString(runtime).utf8(runtime); |
345 | | - throw jsi::JSError(runtime, invalidTypeErrorMessage(stringRepresentation, "It is not an object!")); |
346 | | - } |
347 | | -#endif |
348 | | - jsi::Object object = arg.getObject(runtime); |
349 | | -#if DEBUG |
350 | | - if (!object.isHostObject<TPointee>(runtime)) { |
351 | | - [[unlikely]]; |
352 | | - std::string stringRepresentation = arg.toString(runtime).utf8(runtime); |
353 | | - throw jsi::JSError(runtime, invalidTypeErrorMessage(stringRepresentation, "It is a different HostObject<T>!")); |
354 | | - } |
355 | | -#endif |
356 | | - return object.getHostObject<TPointee>(runtime); |
357 | | - } |
358 | | - static jsi::Value toJSI(jsi::Runtime& runtime, const T& arg) { |
359 | | -#if DEBUG |
360 | | - if (arg == nullptr) { |
361 | | - [[unlikely]]; |
362 | | - throw jsi::JSError(runtime, "Cannot convert nullptr to HostObject<" + getFriendlyTypename() + ">!"); |
363 | | - } |
364 | | -#endif |
365 | | - auto result = jsi::Object::createFromHostObject(runtime, arg); |
366 | | - auto memoryPressure = arg->getMemoryPressure(); |
367 | | - if (memoryPressure > 0) { |
368 | | - result.setExternalMemoryPressure(runtime, memoryPressure); |
369 | | - } |
370 | | - return result; |
371 | | - } |
372 | | -}; |
373 | | - |
374 | 274 | // NativeState <> {} |
375 | 275 | template <typename T> struct is_shared_ptr_to_native_state : std::false_type {}; |
376 | 276 |
|
|
0 commit comments