mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: Adds `RootTag` as a valid type for arguments and return types in TurboModules (on both Android and iOS). This will enable us to change `RootTag` into an opaque type. There are two compelling reasons to do this: - JavaScript will no longer be able to safely depend on `RootTag` being a number (which means we can change this in the future). - Call sites using `unstable_RootTagContext` will can get a `RootTag`, but call sites using the legacy `context.rootTag` will not. This means the opaque type will give us a strategy for migrating away from legacy context and eventually making `unstable_RootTagContext` the only way to access `RootTag`. Changelog: [Internal] (Note: this ignores all push blocking failures!) Reviewed By: RSNara Differential Revision: D21127170 fbshipit-source-id: baec9d7ad17b2f8c4527f1a84f604fc0d28b97eb
159 lines
4.2 KiB
C++
159 lines
4.2 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 "SampleTurboCxxModuleLegacyImpl.h"
|
|
|
|
#include <cxxreact/JsArgumentHelpers.h>
|
|
|
|
using namespace facebook::xplat::module;
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
SampleTurboCxxModuleLegacyImpl::SampleTurboCxxModuleLegacyImpl() {}
|
|
|
|
std::string SampleTurboCxxModuleLegacyImpl::getName() {
|
|
return "SampleTurboCxxModule_v2";
|
|
}
|
|
|
|
std::map<std::string, folly::dynamic>
|
|
SampleTurboCxxModuleLegacyImpl::getConstants() {
|
|
return {
|
|
{"const1", true},
|
|
{"const2", 375},
|
|
{"const3", "something"},
|
|
};
|
|
};
|
|
|
|
std::vector<CxxModule::Method> SampleTurboCxxModuleLegacyImpl::getMethods() {
|
|
return {
|
|
CxxModule::Method(
|
|
"voidFunc", [this](folly::dynamic args) { voidFunc(); }),
|
|
CxxModule::Method(
|
|
"getBool",
|
|
[this](folly::dynamic args) {
|
|
return getBool(xplat::jsArgAsBool(args, 0));
|
|
},
|
|
CxxModule::SyncTag),
|
|
CxxModule::Method(
|
|
"getNumber",
|
|
[this](folly::dynamic args) {
|
|
return getNumber(xplat::jsArgAsDouble(args, 0));
|
|
},
|
|
CxxModule::SyncTag),
|
|
CxxModule::Method(
|
|
"getString",
|
|
[this](folly::dynamic args) {
|
|
return getString(xplat::jsArgAsString(args, 0));
|
|
},
|
|
CxxModule::SyncTag),
|
|
CxxModule::Method(
|
|
"getString",
|
|
[this](folly::dynamic args) {
|
|
return getString(xplat::jsArgAsString(args, 0));
|
|
},
|
|
CxxModule::SyncTag),
|
|
CxxModule::Method(
|
|
"getArray",
|
|
[this](folly::dynamic args) {
|
|
return getArray(xplat::jsArgAsArray(args, 0));
|
|
},
|
|
CxxModule::SyncTag),
|
|
CxxModule::Method(
|
|
"getObject",
|
|
[this](folly::dynamic args) {
|
|
return getObject(xplat::jsArgAsObject(args, 0));
|
|
},
|
|
CxxModule::SyncTag),
|
|
CxxModule::Method(
|
|
"getRootTag",
|
|
[this](folly::dynamic args) {
|
|
return getNumber(xplat::jsArgAsDouble(args, 0));
|
|
},
|
|
CxxModule::SyncTag),
|
|
CxxModule::Method(
|
|
"getValue",
|
|
[this](folly::dynamic args) {
|
|
return getValue(
|
|
xplat::jsArgAsDouble(args, 0),
|
|
xplat::jsArgAsString(args, 1),
|
|
xplat::jsArgAsObject(args, 2));
|
|
},
|
|
CxxModule::SyncTag),
|
|
CxxModule::Method(
|
|
"getValueWithCallback",
|
|
[this](folly::dynamic args, CxxModule::Callback callback) {
|
|
getValueWithCallback(callback);
|
|
}),
|
|
CxxModule::Method(
|
|
"getValueWithPromise",
|
|
[this](
|
|
folly::dynamic args,
|
|
CxxModule::Callback resolve,
|
|
CxxModule::Callback reject) {
|
|
getValueWithPromise(xplat::jsArgAsBool(args, 0), resolve, reject);
|
|
}),
|
|
};
|
|
};
|
|
|
|
void SampleTurboCxxModuleLegacyImpl::voidFunc() {
|
|
// Do nothing.
|
|
}
|
|
|
|
bool SampleTurboCxxModuleLegacyImpl::getBool(bool arg) {
|
|
return arg;
|
|
}
|
|
|
|
double SampleTurboCxxModuleLegacyImpl::getNumber(double arg) {
|
|
return arg;
|
|
}
|
|
|
|
std::string SampleTurboCxxModuleLegacyImpl::getString(const std::string &arg) {
|
|
return arg;
|
|
}
|
|
|
|
folly::dynamic SampleTurboCxxModuleLegacyImpl::getArray(
|
|
const folly::dynamic &arg) {
|
|
return arg;
|
|
}
|
|
|
|
folly::dynamic SampleTurboCxxModuleLegacyImpl::getObject(
|
|
const folly::dynamic &arg) {
|
|
return arg;
|
|
}
|
|
|
|
double SampleTurboCxxModuleLegacyImpl::getRootTag(double arg) {
|
|
return arg;
|
|
}
|
|
|
|
folly::dynamic SampleTurboCxxModuleLegacyImpl::getValue(
|
|
double x,
|
|
const std::string &y,
|
|
const folly::dynamic &z) {
|
|
return folly::dynamic::object("x", x)("y", y)("z", z);
|
|
}
|
|
|
|
void SampleTurboCxxModuleLegacyImpl::getValueWithCallback(
|
|
const CxxModule::Callback &callback) {
|
|
callback({"value from callback!"});
|
|
}
|
|
|
|
void SampleTurboCxxModuleLegacyImpl::getValueWithPromise(
|
|
bool error,
|
|
const CxxModule::Callback &resolve,
|
|
const CxxModule::Callback &reject) {
|
|
if (!error) {
|
|
resolve({"result!"});
|
|
} else {
|
|
reject(
|
|
{folly::dynamic::object("message", "intentional promise rejection")});
|
|
}
|
|
}
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|