mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Move TurboModule Core from ReactCommon/turbomodule to ReactCommon/react/nativemodule
Summary: This diff moves the code of TurboModule Core from ReactCommon/turbomodule to ReactCommon/react/nativemodule For iOS: Pod spec name stays as "ReactCommon/turbomodule/..." for now, only the source/header location is affected. The target will be renamed/restructured closer to TurboModule rollout. changelog: [internal] Internal Reviewed By: RSNara Differential Revision: D23362253 fbshipit-source-id: c2c8207578e50821c7573255d4319b9051b58a37
This commit is contained in:
committed by
Facebook GitHub Bot
parent
57dd48b246
commit
7c93f5b001
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* 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
|
||||
Reference in New Issue
Block a user