Files
nitro/packages/react-native-nitro-modules/cpp/jsi/JSIConverter+UnorderedMap.hpp
T
Marc RousavyandGitHub 065736dc2e feat: Base for Kotlin modules (#45)
* feat: Base for Kotlin modules

* fix: Properly make sure Nitro C++ is initialized

* Update JNIOnLoad.java

* feat: Set up `initHybrid()` in JHybridObject

* chore: Fix a bunch of linter issues

* fix: Fix fbjni codegen

* Update JHybridObject.hpp

* fix: Make `initHybrid()` JVM static

* feat: Make `mHybridData` private member of each in inheritance chain

* Generate specs

* feat: Set up proper inheritance graph in FBJNI using virtual inheritance `HybridObject` (#46)

* feat: Try to set up proper inheritance in FBJNI

* Virtual inheritance

* Update JHybridImageFactorySpec.cpp

* It works...

* feat: Virtual base

* Update HybridTestObject.hpp

* Update CMakeLists.txt

* Update JHybridObject.hpp

* Virtual dtor

* Dynamic cast to NativeState 🤔

* Virtual proper

* feat: Generate as virtual base?

* chore: Change to `updateNative`

* feat: Cannot default-construct HybridObject

* fix: Use `dynamic` pointer cast for `HybridContext` because `HybridObject` is now a virtual base

* Update README.md
2024-08-19 12:40:44 +02:00

71 lines
2.2 KiB
C++

//
// Created by Marc Rousavy on 21.02.24.
//
#pragma once
// Forward declare a few of the common types that might have cyclic includes.
namespace margelo::nitro {
template <typename T, typename Enable>
struct JSIConverter;
} // namespace margelo::nitro
#include "JSIConverter.hpp"
#include "AnyMap.hpp"
#include "JSIHelpers.hpp"
#include <jsi/jsi.h>
#include <unordered_map>
namespace margelo::nitro {
using namespace facebook;
// std::unordered_map<std::string, T> <> Record<string, T>
template <typename ValueType>
struct JSIConverter<std::unordered_map<std::string, ValueType>> final {
static inline std::unordered_map<std::string, ValueType> fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
jsi::Object object = arg.asObject(runtime);
jsi::Array propertyNames = object.getPropertyNames(runtime);
size_t length = propertyNames.size(runtime);
std::unordered_map<std::string, ValueType> map;
map.reserve(length);
for (size_t i = 0; i < length; ++i) {
std::string key = propertyNames.getValueAtIndex(runtime, i).asString(runtime).utf8(runtime);
jsi::Value value = object.getProperty(runtime, key.c_str());
map.emplace(key, JSIConverter<ValueType>::fromJSI(runtime, value));
}
return map;
}
static inline jsi::Value toJSI(jsi::Runtime& runtime, const std::unordered_map<std::string, ValueType>& map) {
jsi::Object object(runtime);
for (const auto& pair : map) {
jsi::Value value = JSIConverter<ValueType>::toJSI(runtime, pair.second);
object.setProperty(runtime, pair.first.c_str(), std::move(value));
}
return object;
}
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
if (!value.isObject()) {
return false;
}
jsi::Object object = value.getObject(runtime);
if (!isPlainObject(runtime, object)) {
return false;
}
jsi::Array properties = object.getPropertyNames(runtime);
size_t size = properties.size(runtime);
for (size_t i = 0; i < size; i++) {
bool canConvertProp = JSIConverter<ValueType>::canConvert(runtime, properties.getValueAtIndex(runtime, i));
if (!canConvertProp) {
return false;
}
}
return true;
}
};
} // namespace margelo::nitro