mirror of
https://github.com/zoriya/nitro.git
synced 2026-10-13 00:05:23 +00:00
feat: Map roundtrip (JS -> C++)
This commit is contained in:
@@ -29,16 +29,19 @@ namespace margelo::nitro::image {
|
||||
std::variant<std::string, double> HybridTestObjectImpl::getVariant() {
|
||||
return std::variant<std::string, double>(13.7);
|
||||
}
|
||||
std::shared_ptr<AnyMap> HybridTestObjectImpl::mapRoundtrip(const std::shared_ptr<AnyMap>& map) {
|
||||
return map;
|
||||
}
|
||||
|
||||
std::shared_ptr<AnyMap> HybridTestObjectImpl::getMap() {
|
||||
auto map = std::make_shared<AnyMap>();
|
||||
map->setNull("null");
|
||||
map->setDouble("double", 55.5);
|
||||
map->setBoolean("bool", true);
|
||||
map->setBigInt("bigint", 225452346346);
|
||||
map->setString("string", "Hello!");
|
||||
map->setObject("object", { {"string", "hello"}, { "double", 55.0 }, { "bool", false } });
|
||||
map->setArray("some-array", { 55.0, "string", false });
|
||||
return map;
|
||||
std::shared_ptr<AnyMap> HybridTestObjectImpl::createMap() {
|
||||
auto result = std::make_shared<AnyMap>();
|
||||
result->setNull("null");
|
||||
result->setDouble("double", 55.5);
|
||||
result->setBoolean("bool", true);
|
||||
result->setBigInt("bigint", 225452346346);
|
||||
result->setString("string", "Hello!");
|
||||
result->setObject("object", { {"string", "hello"}, { "double", 55.0 }, { "bool", false } });
|
||||
result->setArray("some-array", { 55.0, "string", false });
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,8 @@ public:
|
||||
|
||||
std::variant<std::string, double> getVariant() override;
|
||||
|
||||
std::shared_ptr<AnyMap> getMap() override;
|
||||
std::shared_ptr<AnyMap> mapRoundtrip(const std::shared_ptr<AnyMap>& map) override;
|
||||
std::shared_ptr<AnyMap> createMap() override;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
+2
-1
@@ -14,7 +14,8 @@ namespace margelo::nitro::image {
|
||||
// load base methods/properties
|
||||
HybridObject::loadHybridMethods();
|
||||
// load custom methods/properties
|
||||
registerHybridMethod("getMap", &HybridTestObject::getMap, this);
|
||||
registerHybridMethod("createMap", &HybridTestObject::createMap, this);
|
||||
registerHybridMethod("mapRoundtrip", &HybridTestObject::mapRoundtrip, this);
|
||||
registerHybridMethod("getVariant", &HybridTestObject::getVariant, this);
|
||||
registerHybridMethod("getValueFromJsCallback", &HybridTestObject::getValueFromJsCallback, this);
|
||||
}
|
||||
|
||||
+2
-1
@@ -49,7 +49,8 @@ namespace margelo::nitro::image {
|
||||
|
||||
public:
|
||||
// Methods
|
||||
virtual std::shared_ptr<AnyMap> getMap() = 0;
|
||||
virtual std::shared_ptr<AnyMap> createMap() = 0;
|
||||
virtual std::shared_ptr<AnyMap> mapRoundtrip(const std::shared_ptr<AnyMap>& map) = 0;
|
||||
virtual std::variant<std::string, double> getVariant() = 0;
|
||||
virtual std::future<void> getValueFromJsCallback(const Func_std__future_std__string_& callback, const Func_void_std__string& andThenCall) = 0;
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { type HybridObject, type AnyMap } from 'react-native-nitro-modules'
|
||||
|
||||
export interface TestObject extends HybridObject<{ ios: 'c++' }> {
|
||||
getMap(): AnyMap
|
||||
createMap(): AnyMap
|
||||
mapRoundtrip(map: AnyMap): AnyMap
|
||||
getVariant(): number | string
|
||||
|
||||
getValueFromJsCallback(
|
||||
|
||||
@@ -29,8 +29,17 @@ struct AnyValue : std::variant<std::monostate, bool, double, int64_t, std::strin
|
||||
*/
|
||||
class AnyMap {
|
||||
public:
|
||||
/**
|
||||
* Create a new instance of AnyMap.
|
||||
*/
|
||||
explicit AnyMap() {}
|
||||
|
||||
public:
|
||||
/**
|
||||
* Create a new shared_ptr instance of AnyMap.
|
||||
*/
|
||||
static std::shared_ptr<AnyMap> make() { return std::make_shared<AnyMap>(); }
|
||||
|
||||
public:
|
||||
/**
|
||||
* Returns whether the map contains the given key, or not.
|
||||
|
||||
@@ -408,7 +408,38 @@ template<> struct JSIConverter<AnyValue> {
|
||||
// AnyMap <> Record<K, V>
|
||||
template<> struct JSIConverter<std::shared_ptr<AnyMap>> {
|
||||
static inline std::shared_ptr<AnyMap> fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
|
||||
throw std::runtime_error("Cannot convert JS object to AnyMap yet!");
|
||||
jsi::Object object = arg.asObject(runtime);
|
||||
jsi::Array propNames = object.getPropertyNames(runtime);
|
||||
size_t size = propNames.size(runtime);
|
||||
std::shared_ptr<AnyMap> map = AnyMap::make();
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
jsi::String propName = propNames.getValueAtIndex(runtime, i).getString(runtime);
|
||||
std::string key = propName.utf8(runtime);
|
||||
jsi::Value value = object.getProperty(runtime, propName);
|
||||
if (value.isNull()) {
|
||||
map->setNull(key);
|
||||
} else if (value.isBool()) {
|
||||
map->setBoolean(key, value.getBool());
|
||||
} else if (value.isNumber()) {
|
||||
map->setDouble(key, value.getNumber());
|
||||
} else if (value.isString()) {
|
||||
map->setString(key, value.getString(runtime).utf8(runtime));
|
||||
} else if (value.isBigInt()) {
|
||||
map->setBigInt(key, value.getBigInt(runtime).asInt64(runtime));
|
||||
} else if (value.isObject()) {
|
||||
jsi::Object valueObj = value.getObject(runtime);
|
||||
if (valueObj.isArray(runtime)) {
|
||||
// TODO: Convert Array
|
||||
} else {
|
||||
// TODO: Convert object
|
||||
}
|
||||
} else {
|
||||
std::string stringified = arg.toString(runtime).utf8(runtime);
|
||||
throw std::runtime_error("Cannot convert \"" + key + "\" (\"" + stringified +
|
||||
"\") to AnyMap value type!");
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
static inline jsi::Value toJSI(jsi::Runtime& runtime, std::shared_ptr<AnyMap> map) {
|
||||
jsi::Object object(runtime);
|
||||
|
||||
@@ -6,13 +6,14 @@ export type ValueType =
|
||||
| number
|
||||
| boolean
|
||||
| bigint
|
||||
| null
|
||||
| ValueType[]
|
||||
| { [k: string]: ValueType }
|
||||
|
||||
/**
|
||||
* Represents an untyped map, similar to a JSON structure.
|
||||
* Supported types:
|
||||
* - Primitives (`string`, `number`, `boolean`, `bigint`)
|
||||
* - Primitives (`string`, `number`, `boolean`, `bigint`, `null`)
|
||||
* - Arrays of primitives (`ValueType[]`)
|
||||
* - Objects of primitives (`Record<string, ValueType>`)
|
||||
* - Arrays of arrays or objects
|
||||
|
||||
Reference in New Issue
Block a user