-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JSI<->C++ bridging conversion for folly::dynamic (#37237)
Summary: Pull Request resolved: #37237 Enables bridging conversions to/from jsi::Value and folly::dynamic. Changelog: [Internal] - Add JSI<->C++ bridging conversion for folly::dynamic Differential Revision: D45544843 fbshipit-source-id: 7ae55334b013a4718cfe8342f75cbab72bce8ef8
- Loading branch information
1 parent
c8a0f19
commit 630e809
Showing
3 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/react-native/ReactCommon/react/bridging/Dynamic.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <folly/dynamic.h> | ||
#include <jsi/JSIDynamic.h> | ||
|
||
namespace facebook::react { | ||
|
||
template <> | ||
struct Bridging<folly::dynamic> { | ||
static folly::dynamic fromJs(jsi::Runtime &rt, const jsi::Value &value) { | ||
if (value.isNull()) { | ||
return nullptr; | ||
} else if (value.isBool()) { | ||
return value.asBool(); | ||
} else if (value.isNumber()) { | ||
return value.asNumber(); | ||
} else if (value.isString()) { | ||
return value.asString(rt).utf8(rt); | ||
} else if (value.isObject()) { | ||
auto obj = value.asObject(rt); | ||
if (obj.isArray(rt)) { | ||
folly::dynamic result = folly::dynamic::array(); | ||
auto array = obj.asArray(rt); | ||
auto length = array.size(rt); | ||
for (auto i = 0; i < length; ++i) { | ||
result.push_back(Bridging<folly::dynamic>::fromJs( | ||
rt, array.getValueAtIndex(rt, i))); | ||
} | ||
return result; | ||
} else { | ||
folly::dynamic result = folly::dynamic::object(); | ||
auto propertyNames = obj.getPropertyNames(rt); | ||
auto length = propertyNames.length(rt); | ||
for (auto i = 0; i < length; i++) { | ||
auto propertyName = propertyNames.getValueAtIndex(rt, i); | ||
result[Bridging<folly::dynamic>::fromJs(rt, propertyName)] = | ||
Bridging<folly::dynamic>::fromJs( | ||
rt, obj.getProperty(rt, propertyName.asString(rt))); | ||
} | ||
return result; | ||
} | ||
} | ||
return nullptr; | ||
} | ||
|
||
static jsi::Value toJs(jsi::Runtime &rt, folly::dynamic value) { | ||
return jsi::valueFromDynamic(rt, std::move(value)); | ||
} | ||
}; | ||
|
||
} // namespace facebook::react |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters