mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
0a8164d993
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/36574 We would previously generate the following codegen for optional args ``` return static_cast<NativeAudioModuleCxxSpecJSI *>(&turboModule)->playAudio( rt, args[0].asString(rt), args[1].isNull() || args[1].isUndefined() ? std::nullopt : std::make_optional(args[1].asString(rt)), args[2].isNull() || args[2].isUndefined() ? std::nullopt : std::make_optional(args[2].asString(rt)), args[3].asNumber(), count < 4 || args[4].isNull() || args[4].isUndefined() ? std::nullopt : std::make_optional(args[4].asObject(rt)), count < 5 || args[5].isNull() || args[5].isUndefined() ? std::nullopt : std::make_optional(args[5].asObject(rt)), count < 6 || args[6].isNull() || args[6].isUndefined() ? std::nullopt : std::make_optional(args[6].asBool()) ); ``` However, the counts checked are off-by-one, causing us to incorrectly process args. Changelog: [General][Fixed] Issue with TurboModule C++ codegen with optional args Differential Revision: D44299193 fbshipit-source-id: f00b9f5e09c2f524f9393137346c256d8b6b2979
146 lines
3.7 KiB
C++
146 lines
3.7 KiB
C++
/*
|
|
* 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.
|
|
*/
|
|
|
|
#include "NativeCxxModuleExample.h"
|
|
|
|
namespace facebook::react {
|
|
|
|
NativeCxxModuleExample::NativeCxxModuleExample(
|
|
std::shared_ptr<CallInvoker> jsInvoker)
|
|
: NativeCxxModuleExampleCxxSpec(std::move(jsInvoker)) {}
|
|
|
|
void NativeCxxModuleExample::getValueWithCallback(
|
|
jsi::Runtime &rt,
|
|
AsyncCallback<std::string> callback) {
|
|
callback({"value from callback!"});
|
|
}
|
|
|
|
std::vector<std::optional<ObjectStruct>> NativeCxxModuleExample::getArray(
|
|
jsi::Runtime &rt,
|
|
std::vector<std::optional<ObjectStruct>> arg) {
|
|
return arg;
|
|
}
|
|
|
|
bool NativeCxxModuleExample::getBool(jsi::Runtime &rt, bool arg) {
|
|
return arg;
|
|
}
|
|
|
|
ConstantsStruct NativeCxxModuleExample::getConstants(jsi::Runtime &rt) {
|
|
return ConstantsStruct{true, 69, "react-native"};
|
|
}
|
|
|
|
CustomEnumInt NativeCxxModuleExample::getCustomEnum(
|
|
jsi::Runtime &rt,
|
|
CustomEnumInt arg) {
|
|
return arg;
|
|
}
|
|
|
|
NativeCxxModuleExampleCxxEnumFloat NativeCxxModuleExample::getNumEnum(
|
|
jsi::Runtime &rt,
|
|
NativeCxxModuleExampleCxxEnumInt arg) {
|
|
return NativeCxxModuleExampleCxxEnumFloat::FB;
|
|
}
|
|
|
|
NativeCxxModuleExampleCxxEnumStr NativeCxxModuleExample::getStrEnum(
|
|
jsi::Runtime &rt,
|
|
NativeCxxModuleExampleCxxEnumNone arg) {
|
|
return NativeCxxModuleExampleCxxEnumStr::SB;
|
|
}
|
|
|
|
std::map<std::string, std::optional<int32_t>> NativeCxxModuleExample::getMap(
|
|
jsi::Runtime &rt,
|
|
std::map<std::string, std::optional<int32_t>> arg) {
|
|
return arg;
|
|
}
|
|
|
|
double NativeCxxModuleExample::getNumber(jsi::Runtime &rt, double arg) {
|
|
return arg;
|
|
}
|
|
|
|
ObjectStruct NativeCxxModuleExample::getObject(
|
|
jsi::Runtime &rt,
|
|
ObjectStruct arg) {
|
|
return arg;
|
|
}
|
|
|
|
std::set<float> NativeCxxModuleExample::getSet(
|
|
jsi::Runtime &rt,
|
|
std::set<float> arg) {
|
|
return arg;
|
|
}
|
|
|
|
std::string NativeCxxModuleExample::getString(
|
|
jsi::Runtime &rt,
|
|
std::string arg) {
|
|
return arg;
|
|
}
|
|
|
|
std::string NativeCxxModuleExample::getUnion(
|
|
jsi::Runtime &rt,
|
|
float x,
|
|
std::string y,
|
|
jsi::Object z) {
|
|
std::string result = "x: " + std::to_string(x) + ", y: " + y + ", z: { ";
|
|
if (z.hasProperty(rt, "value")) {
|
|
result += "value: ";
|
|
result += std::to_string(z.getProperty(rt, "value").getNumber());
|
|
} else if (z.hasProperty(rt, "low")) {
|
|
result += "low: ";
|
|
result += z.getProperty(rt, "low").getString(rt).utf8(rt);
|
|
}
|
|
result += " }";
|
|
return result;
|
|
}
|
|
|
|
ValueStruct NativeCxxModuleExample::getValue(
|
|
jsi::Runtime &rt,
|
|
double x,
|
|
std::string y,
|
|
ObjectStruct z) {
|
|
ValueStruct result{x, y, z};
|
|
return result;
|
|
}
|
|
|
|
AsyncPromise<std::string> NativeCxxModuleExample::getValueWithPromise(
|
|
jsi::Runtime &rt,
|
|
bool error) {
|
|
auto promise = AsyncPromise<std::string>(rt, jsInvoker_);
|
|
if (error) {
|
|
promise.reject("intentional promise rejection");
|
|
} else {
|
|
promise.resolve("result!");
|
|
}
|
|
return promise;
|
|
}
|
|
|
|
bool NativeCxxModuleExample::getWithWithOptionalArgs(
|
|
jsi::Runtime &rt,
|
|
std::optional<bool> optionalArg) {
|
|
return optionalArg.value_or(false);
|
|
}
|
|
|
|
void NativeCxxModuleExample::voidFunc(jsi::Runtime &rt) {
|
|
// Nothing to do
|
|
}
|
|
|
|
void NativeCxxModuleExample::emitCustomDeviceEvent(
|
|
jsi::Runtime &rt,
|
|
jsi::String eventName) {
|
|
// Test emitting device events (RCTDeviceEventEmitter.emit) from C++
|
|
// TurboModule with arbitrary arguments
|
|
emitDeviceEvent(
|
|
rt,
|
|
eventName.utf8(rt).c_str(),
|
|
[](jsi::Runtime &rt, std::vector<jsi::Value> &args) {
|
|
args.emplace_back(jsi::Value(true));
|
|
args.emplace_back(jsi::Value(42));
|
|
args.emplace_back(jsi::String::createFromAscii(rt, "stringArg"));
|
|
});
|
|
}
|
|
|
|
} // namespace facebook::react
|