mirror of
https://github.com/status-im/react-native.git
synced 2025-02-23 22:58:19 +00:00
Summary: This is only the core C++ part of TurboModule - moving to github to make integration with existing NativeModules system slightly easier. Other bindings for iOS/Android are not yet ready to move. Notes: * TurboModules is not ready to use at the moment. * Build configuration is not yet provided (cocoapods/.xcodeproj/gradle), just like Fabric. * No effort was done to make this lib C++17 strictly compliant yet (there will be in the future). Reviewed By: RSNara Differential Revision: D13551211 fbshipit-source-id: cd3b458e6746ee9218451962ca65b1ad641a32db
99 lines
2.8 KiB
C++
99 lines
2.8 KiB
C++
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#include "TurboModuleUtils.h"
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
static jsi::Value deepCopyJSIValue(jsi::Runtime &rt, const jsi::Value &value) {
|
|
if (value.isNull()) {
|
|
return jsi::Value::null();
|
|
}
|
|
|
|
if (value.isBool()) {
|
|
return jsi::Value(value.getBool());
|
|
}
|
|
|
|
if (value.isNumber()) {
|
|
return jsi::Value(value.getNumber());
|
|
}
|
|
|
|
if (value.isString()) {
|
|
return value.getString(rt);
|
|
}
|
|
|
|
if (value.isObject()) {
|
|
jsi::Object o = value.getObject(rt);
|
|
if (o.isArray(rt)) {
|
|
return deepCopyJSIArray(rt, o.getArray(rt));
|
|
}
|
|
if (o.isFunction(rt)) {
|
|
return o.getFunction(rt);
|
|
}
|
|
return deepCopyJSIObject(rt, o);
|
|
}
|
|
|
|
return jsi::Value::undefined();
|
|
}
|
|
|
|
jsi::Object deepCopyJSIObject(jsi::Runtime &rt, const jsi::Object &obj) {
|
|
jsi::Object copy(rt);
|
|
jsi::Array propertyNames = obj.getPropertyNames(rt);
|
|
size_t size = propertyNames.size(rt);
|
|
for (size_t i = 0; i < size; i++) {
|
|
jsi::String name = propertyNames.getValueAtIndex(rt, i).getString(rt);
|
|
jsi::Value value = obj.getProperty(rt, name);
|
|
copy.setProperty(rt, name, deepCopyJSIValue(rt, value));
|
|
}
|
|
return copy;
|
|
}
|
|
|
|
jsi::Array deepCopyJSIArray(jsi::Runtime &rt, const jsi::Array &arr) {
|
|
size_t size = arr.size(rt);
|
|
jsi::Array copy(rt, size);
|
|
for (size_t i = 0; i < size; i++) {
|
|
copy.setValueAtIndex(rt, i, deepCopyJSIValue(rt, arr.getValueAtIndex(rt, i)));
|
|
}
|
|
return copy;
|
|
}
|
|
|
|
Promise::Promise(jsi::Runtime &rt, jsi::Function resolve, jsi::Function reject)
|
|
: runtime_(rt),
|
|
resolve_(std::move(resolve)),
|
|
reject_(std::move(reject)) {}
|
|
|
|
void Promise::resolve(const jsi::Value &result) {
|
|
resolve_.call(runtime_, result);
|
|
}
|
|
|
|
void Promise::reject(const std::string &message) {
|
|
jsi::Object error(runtime_);
|
|
error.setProperty(runtime_, "message", jsi::String::createFromUtf8(runtime_, message));
|
|
reject_.call(runtime_, error);
|
|
}
|
|
|
|
jsi::Value createPromiseAsJSIValue(jsi::Runtime &rt, const PromiseSetupFunctionType func) {
|
|
jsi::Function JSPromise = rt.global().getPropertyAsFunction(rt, "Promise");
|
|
jsi::Function fn = jsi::Function::createFromHostFunction(
|
|
rt,
|
|
jsi::PropNameID::forAscii(rt, "fn"),
|
|
2,
|
|
[func](jsi::Runtime &rt2, const jsi::Value &thisVal, const jsi::Value *args, size_t count) {
|
|
jsi::Function resolve = args[0].getObject(rt2).getFunction(rt2);
|
|
jsi::Function reject = args[1].getObject(rt2).getFunction(rt2);
|
|
auto wrapper = std::make_shared<Promise>(rt2, std::move(resolve), std::move(reject));
|
|
func(rt2, wrapper);
|
|
return jsi::Value::undefined();
|
|
});
|
|
|
|
return JSPromise.callAsConstructor(rt, fn);
|
|
}
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|