[pigeon] Fix async handling in C++ generator (#3040)

* Add unit test

* Switch all errors to direct return

* Make 'wrapped' entirely local

* Switch reply to copy instead of reference

* Version bump
This commit is contained in:
stuartmorgan
2023-01-11 19:04:18 -08:00
committed by GitHub
parent a9a2564bb2
commit 56c6ce5550
14 changed files with 360 additions and 309 deletions

View File

@ -1,3 +1,7 @@
## 5.0.1
* [c++] Fixes undefined behavior in `@async` methods.
## 5.0.0 ## 5.0.0
* Creates new Generator classes for each language. * Creates new Generator classes for each language.

View File

@ -498,9 +498,9 @@ void _writeHostApiHeader(Indent indent, Api api, Root root) {
indent.writeln( indent.writeln(
'static void SetUp(flutter::BinaryMessenger* binary_messenger, ${api.name}* api);'); 'static void SetUp(flutter::BinaryMessenger* binary_messenger, ${api.name}* api);');
indent.writeln( indent.writeln(
'static flutter::EncodableList WrapError(std::string_view error_message);'); 'static flutter::EncodableValue WrapError(std::string_view error_message);');
indent.writeln( indent.writeln(
'static flutter::EncodableList WrapError(const FlutterError& error);'); 'static flutter::EncodableValue WrapError(const FlutterError& error);');
}); });
indent.scoped(' protected:', '', () { indent.scoped(' protected:', '', () {
indent.writeln('${api.name}() = default;'); indent.writeln('${api.name}() = default;');
@ -541,7 +541,6 @@ const flutter::StandardMessageCodec& ${api.name}::GetCodec() {
indent.write( indent.write(
'channel->SetMessageHandler([api](const flutter::EncodableValue& message, const flutter::MessageReply<flutter::EncodableValue>& reply) '); 'channel->SetMessageHandler([api](const flutter::EncodableValue& message, const flutter::MessageReply<flutter::EncodableValue>& reply) ');
indent.scoped('{', '});', () { indent.scoped('{', '});', () {
indent.writeln('flutter::EncodableList wrapped;');
indent.write('try '); indent.write('try ');
indent.scoped('{', '}', () { indent.scoped('{', '}', () {
final List<String> methodArgument = <String>[]; final List<String> methodArgument = <String>[];
@ -627,7 +626,7 @@ const flutter::StandardMessageCodec& ${api.name}::GetCodec() {
indent.write('if ($encodableArgName.IsNull()) '); indent.write('if ($encodableArgName.IsNull()) ');
indent.scoped('{', '}', () { indent.scoped('{', '}', () {
indent.writeln( indent.writeln(
'reply(flutter::EncodableValue(WrapError("$argName unexpectedly null.")));'); 'reply(WrapError("$argName unexpectedly null."));');
indent.writeln('return;'); indent.writeln('return;');
}); });
} }
@ -637,17 +636,16 @@ const flutter::StandardMessageCodec& ${api.name}::GetCodec() {
}); });
} }
String wrapResponse(String reply, TypeDeclaration returnType) { String wrapResponse(TypeDeclaration returnType,
String elseBody = ''; {String prefix = ''}) {
final String ifCondition; final String nonErrorPath;
final String errorCondition;
final String errorGetter; final String errorGetter;
final String prefix = (reply != '') ? '\t' : '';
const String nullValue = 'flutter::EncodableValue()'; const String nullValue = 'flutter::EncodableValue()';
if (returnType.isVoid) { if (returnType.isVoid) {
elseBody = nonErrorPath = '${prefix}wrapped.push_back($nullValue);';
'$prefix\twrapped.push_back($nullValue);${indent.newline}'; errorCondition = 'output.has_value()';
ifCondition = 'output.has_value()';
errorGetter = 'value'; errorGetter = 'value';
} else { } else {
final HostDatatype hostType = getHostDatatype( final HostDatatype hostType = getHostDatatype(
@ -662,26 +660,32 @@ const flutter::StandardMessageCodec& ${api.name}::GetCodec() {
if (returnType.isNullable) { if (returnType.isNullable) {
// The value is a std::optional, so needs an extra layer of // The value is a std::optional, so needs an extra layer of
// handling. // handling.
elseBody = ''' nonErrorPath = '''
$prefix\tauto output_optional = $extractedValue; ${prefix}auto output_optional = $extractedValue;
$prefix\tif (output_optional) { ${prefix}if (output_optional) {
$prefix\t\twrapped.push_back($wrapperType(std::move(output_optional).value())); $prefix\twrapped.push_back($wrapperType(std::move(output_optional).value()));
$prefix\t} else { $prefix} else {
$prefix\t\twrapped.push_back($nullValue); $prefix\twrapped.push_back($nullValue);
$prefix\t}${indent.newline}'''; $prefix}''';
} else { } else {
elseBody = nonErrorPath =
'$prefix\twrapped.push_back($wrapperType($extractedValue));${indent.newline}'; '${prefix}wrapped.push_back($wrapperType($extractedValue));';
} }
ifCondition = 'output.has_error()'; errorCondition = 'output.has_error()';
errorGetter = 'error'; errorGetter = 'error';
} }
return '${prefix}if ($ifCondition) {${indent.newline}' // Ideally this code would use an initializer list to create
'$prefix\twrapped = WrapError(output.$errorGetter());${indent.newline}' // an EncodableList inline, which would be less code. However,
'$prefix} else {${indent.newline}' // that would always copy the element, so the slightly more
'$elseBody' // verbose create-and-push approach is used instead.
'$prefix}' return '''
'$prefix$reply'; ${prefix}if ($errorCondition) {
$prefix\treply(WrapError(output.$errorGetter()));
$prefix\treturn;
$prefix}
${prefix}flutter::EncodableList wrapped;
$nonErrorPath
${prefix}reply(flutter::EncodableValue(std::move(wrapped)));''';
} }
final HostDatatype returnType = getHostDatatype( final HostDatatype returnType = getHostDatatype(
@ -692,8 +696,8 @@ $prefix\t}${indent.newline}''';
final String returnTypeName = _apiReturnType(returnType); final String returnTypeName = _apiReturnType(returnType);
if (method.isAsynchronous) { if (method.isAsynchronous) {
methodArgument.add( methodArgument.add(
'[&wrapped, &reply]($returnTypeName&& output) {${indent.newline}' '[reply]($returnTypeName&& output) {${indent.newline}'
'${wrapResponse('\treply(flutter::EncodableValue(std::move(wrapped)));${indent.newline}', method.returnType)}' '${wrapResponse(method.returnType, prefix: '\t')}${indent.newline}'
'}', '}',
); );
} }
@ -703,21 +707,21 @@ $prefix\t}${indent.newline}''';
indent.format('$call;'); indent.format('$call;');
} else { } else {
indent.writeln('$returnTypeName output = $call;'); indent.writeln('$returnTypeName output = $call;');
indent.format(wrapResponse('', method.returnType)); indent.format(wrapResponse(method.returnType));
} }
}); });
indent.write('catch (const std::exception& exception) '); indent.write('catch (const std::exception& exception) ');
indent.scoped('{', '}', () { indent.scoped('{', '}', () {
indent.writeln('wrapped = WrapError(exception.what());'); // There is a potential here for `reply` to be called twice, which
if (method.isAsynchronous) { // is a violation of the API contract, because there's no way of
indent.writeln( // knowing whether or not the plugin code called `reply` before
'reply(flutter::EncodableValue(std::move(wrapped)));'); // throwing. Since use of `@async` suggests that the reply is
} // probably not sent within the scope of the stack, err on the
// side of potential double-call rather than no call (which is
// also an API violation) so that unexpected errors have a better
// chance of being caught and handled in a useful way.
indent.writeln('reply(WrapError(exception.what()));');
}); });
if (!method.isAsynchronous) {
indent.writeln(
'reply(flutter::EncodableValue(std::move(wrapped)));');
}
}); });
}); });
indent.scoped(null, '}', () { indent.scoped(null, '}', () {
@ -1187,15 +1191,15 @@ void generateCppSource(CppOptions options, Root root, StringSink sink) {
indent.addln(''); indent.addln('');
indent.format(''' indent.format('''
flutter::EncodableList ${api.name}::WrapError(std::string_view error_message) { flutter::EncodableValue ${api.name}::WrapError(std::string_view error_message) {
\treturn flutter::EncodableList({ \treturn flutter::EncodableValue(flutter::EncodableList{
\t\tflutter::EncodableValue(std::string(error_message)), \t\tflutter::EncodableValue(std::string(error_message)),
\t\tflutter::EncodableValue("Error"), \t\tflutter::EncodableValue("Error"),
\t\tflutter::EncodableValue() \t\tflutter::EncodableValue()
\t}); \t});
} }
flutter::EncodableList ${api.name}::WrapError(const FlutterError& error) { flutter::EncodableValue ${api.name}::WrapError(const FlutterError& error) {
\treturn flutter::EncodableList({ \treturn flutter::EncodableValue(flutter::EncodableList{
\t\tflutter::EncodableValue(error.message()), \t\tflutter::EncodableValue(error.message()),
\t\tflutter::EncodableValue(error.code()), \t\tflutter::EncodableValue(error.code()),
\t\terror.details() \t\terror.details()

View File

@ -9,7 +9,7 @@ import 'dart:mirrors';
import 'ast.dart'; import 'ast.dart';
/// The current version of pigeon. This must match the version in pubspec.yaml. /// The current version of pigeon. This must match the version in pubspec.yaml.
const String pigeonVersion = '5.0.0'; const String pigeonVersion = '5.0.1';
/// Read all the content from [stdin] to a String. /// Read all the content from [stdin] to a String.
String readStdin() { String readStdin() {

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
// Autogenerated from Pigeon (v5.0.0), do not edit directly. // Autogenerated from Pigeon (v5.0.1), do not edit directly.
// See also: https://pub.dev/packages/pigeon // See also: https://pub.dev/packages/pigeon
package com.example.alternate_language_test_plugin; package com.example.alternate_language_test_plugin;

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
// Autogenerated from Pigeon (v5.0.0), do not edit directly. // Autogenerated from Pigeon (v5.0.1), do not edit directly.
// See also: https://pub.dev/packages/pigeon // See also: https://pub.dev/packages/pigeon
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
@protocol FlutterBinaryMessenger; @protocol FlutterBinaryMessenger;

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
// Autogenerated from Pigeon (v5.0.0), do not edit directly. // Autogenerated from Pigeon (v5.0.1), do not edit directly.
// See also: https://pub.dev/packages/pigeon // See also: https://pub.dev/packages/pigeon
#import "CoreTests.gen.h" #import "CoreTests.gen.h"
#import <Flutter/Flutter.h> #import <Flutter/Flutter.h>

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
// Autogenerated from Pigeon (v5.0.0), do not edit directly. // Autogenerated from Pigeon (v5.0.1), do not edit directly.
// See also: https://pub.dev/packages/pigeon // See also: https://pub.dev/packages/pigeon
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import // ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import
import 'dart:async'; import 'dart:async';

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
// Autogenerated from Pigeon (v5.0.0), do not edit directly. // Autogenerated from Pigeon (v5.0.1), do not edit directly.
// See also: https://pub.dev/packages/pigeon // See also: https://pub.dev/packages/pigeon
package com.example.test_plugin package com.example.test_plugin

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
// Autogenerated from Pigeon (v5.0.0), do not edit directly. // Autogenerated from Pigeon (v5.0.1), do not edit directly.
// See also: https://pub.dev/packages/pigeon // See also: https://pub.dev/packages/pigeon
import Foundation import Foundation

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
// Autogenerated from Pigeon (v5.0.0), do not edit directly. // Autogenerated from Pigeon (v5.0.1), do not edit directly.
// See also: https://pub.dev/packages/pigeon // See also: https://pub.dev/packages/pigeon
import Foundation import Foundation

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
// Autogenerated from Pigeon (v5.0.0), do not edit directly. // Autogenerated from Pigeon (v5.0.1), do not edit directly.
// See also: https://pub.dev/packages/pigeon // See also: https://pub.dev/packages/pigeon
#undef _HAS_EXCEPTIONS #undef _HAS_EXCEPTIONS
@ -553,18 +553,18 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
std::optional<FlutterError> output = api->Noop(); std::optional<FlutterError> output = api->Noop();
if (output.has_value()) { if (output.has_value()) {
wrapped = WrapError(output.value()); reply(WrapError(output.value()));
} else { return;
wrapped.push_back(flutter::EncodableValue());
} }
flutter::EncodableList wrapped;
wrapped.push_back(flutter::EncodableValue());
reply(flutter::EncodableValue(std::move(wrapped)));
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
} }
reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} else { } else {
channel->SetMessageHandler(nullptr); channel->SetMessageHandler(nullptr);
@ -580,13 +580,11 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
const auto& args = std::get<flutter::EncodableList>(message); const auto& args = std::get<flutter::EncodableList>(message);
const auto& encodable_everything_arg = args.at(0); const auto& encodable_everything_arg = args.at(0);
if (encodable_everything_arg.IsNull()) { if (encodable_everything_arg.IsNull()) {
reply(flutter::EncodableValue( reply(WrapError("everything_arg unexpectedly null."));
WrapError("everything_arg unexpectedly null.")));
return; return;
} }
const auto& everything_arg = std::any_cast<const AllTypes&>( const auto& everything_arg = std::any_cast<const AllTypes&>(
@ -594,15 +592,16 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
encodable_everything_arg)); encodable_everything_arg));
ErrorOr<AllTypes> output = api->EchoAllTypes(everything_arg); ErrorOr<AllTypes> output = api->EchoAllTypes(everything_arg);
if (output.has_error()) { if (output.has_error()) {
wrapped = WrapError(output.error()); reply(WrapError(output.error()));
} else { return;
wrapped.push_back(flutter::CustomEncodableValue(
std::move(output).TakeValue()));
} }
flutter::EncodableList wrapped;
wrapped.push_back(
flutter::CustomEncodableValue(std::move(output).TakeValue()));
reply(flutter::EncodableValue(std::move(wrapped)));
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
} }
reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} else { } else {
channel->SetMessageHandler(nullptr); channel->SetMessageHandler(nullptr);
@ -618,7 +617,6 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
const auto& args = std::get<flutter::EncodableList>(message); const auto& args = std::get<flutter::EncodableList>(message);
const auto& encodable_everything_arg = args.at(0); const auto& encodable_everything_arg = args.at(0);
@ -629,20 +627,21 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
ErrorOr<std::optional<AllNullableTypes>> output = ErrorOr<std::optional<AllNullableTypes>> output =
api->EchoAllNullableTypes(everything_arg); api->EchoAllNullableTypes(everything_arg);
if (output.has_error()) { if (output.has_error()) {
wrapped = WrapError(output.error()); reply(WrapError(output.error()));
} else { return;
auto output_optional = std::move(output).TakeValue();
if (output_optional) {
wrapped.push_back(flutter::CustomEncodableValue(
std::move(output_optional).value()));
} else {
wrapped.push_back(flutter::EncodableValue());
}
} }
flutter::EncodableList wrapped;
auto output_optional = std::move(output).TakeValue();
if (output_optional) {
wrapped.push_back(flutter::CustomEncodableValue(
std::move(output_optional).value()));
} else {
wrapped.push_back(flutter::EncodableValue());
}
reply(flutter::EncodableValue(std::move(wrapped)));
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
} }
reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} else { } else {
channel->SetMessageHandler(nullptr); channel->SetMessageHandler(nullptr);
@ -658,18 +657,18 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
std::optional<FlutterError> output = api->ThrowError(); std::optional<FlutterError> output = api->ThrowError();
if (output.has_value()) { if (output.has_value()) {
wrapped = WrapError(output.value()); reply(WrapError(output.value()));
} else { return;
wrapped.push_back(flutter::EncodableValue());
} }
flutter::EncodableList wrapped;
wrapped.push_back(flutter::EncodableValue());
reply(flutter::EncodableValue(std::move(wrapped)));
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
} }
reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} else { } else {
channel->SetMessageHandler(nullptr); channel->SetMessageHandler(nullptr);
@ -684,27 +683,26 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
const auto& args = std::get<flutter::EncodableList>(message); const auto& args = std::get<flutter::EncodableList>(message);
const auto& encodable_an_int_arg = args.at(0); const auto& encodable_an_int_arg = args.at(0);
if (encodable_an_int_arg.IsNull()) { if (encodable_an_int_arg.IsNull()) {
reply(flutter::EncodableValue( reply(WrapError("an_int_arg unexpectedly null."));
WrapError("an_int_arg unexpectedly null.")));
return; return;
} }
const int64_t an_int_arg = encodable_an_int_arg.LongValue(); const int64_t an_int_arg = encodable_an_int_arg.LongValue();
ErrorOr<int64_t> output = api->EchoInt(an_int_arg); ErrorOr<int64_t> output = api->EchoInt(an_int_arg);
if (output.has_error()) { if (output.has_error()) {
wrapped = WrapError(output.error()); reply(WrapError(output.error()));
} else { return;
wrapped.push_back(
flutter::EncodableValue(std::move(output).TakeValue()));
} }
flutter::EncodableList wrapped;
wrapped.push_back(
flutter::EncodableValue(std::move(output).TakeValue()));
reply(flutter::EncodableValue(std::move(wrapped)));
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
} }
reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} else { } else {
channel->SetMessageHandler(nullptr); channel->SetMessageHandler(nullptr);
@ -720,28 +718,27 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
const auto& args = std::get<flutter::EncodableList>(message); const auto& args = std::get<flutter::EncodableList>(message);
const auto& encodable_a_double_arg = args.at(0); const auto& encodable_a_double_arg = args.at(0);
if (encodable_a_double_arg.IsNull()) { if (encodable_a_double_arg.IsNull()) {
reply(flutter::EncodableValue( reply(WrapError("a_double_arg unexpectedly null."));
WrapError("a_double_arg unexpectedly null.")));
return; return;
} }
const auto& a_double_arg = const auto& a_double_arg =
std::get<double>(encodable_a_double_arg); std::get<double>(encodable_a_double_arg);
ErrorOr<double> output = api->EchoDouble(a_double_arg); ErrorOr<double> output = api->EchoDouble(a_double_arg);
if (output.has_error()) { if (output.has_error()) {
wrapped = WrapError(output.error()); reply(WrapError(output.error()));
} else { return;
wrapped.push_back(
flutter::EncodableValue(std::move(output).TakeValue()));
} }
flutter::EncodableList wrapped;
wrapped.push_back(
flutter::EncodableValue(std::move(output).TakeValue()));
reply(flutter::EncodableValue(std::move(wrapped)));
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
} }
reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} else { } else {
channel->SetMessageHandler(nullptr); channel->SetMessageHandler(nullptr);
@ -756,27 +753,26 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
const auto& args = std::get<flutter::EncodableList>(message); const auto& args = std::get<flutter::EncodableList>(message);
const auto& encodable_a_bool_arg = args.at(0); const auto& encodable_a_bool_arg = args.at(0);
if (encodable_a_bool_arg.IsNull()) { if (encodable_a_bool_arg.IsNull()) {
reply(flutter::EncodableValue( reply(WrapError("a_bool_arg unexpectedly null."));
WrapError("a_bool_arg unexpectedly null.")));
return; return;
} }
const auto& a_bool_arg = std::get<bool>(encodable_a_bool_arg); const auto& a_bool_arg = std::get<bool>(encodable_a_bool_arg);
ErrorOr<bool> output = api->EchoBool(a_bool_arg); ErrorOr<bool> output = api->EchoBool(a_bool_arg);
if (output.has_error()) { if (output.has_error()) {
wrapped = WrapError(output.error()); reply(WrapError(output.error()));
} else { return;
wrapped.push_back(
flutter::EncodableValue(std::move(output).TakeValue()));
} }
flutter::EncodableList wrapped;
wrapped.push_back(
flutter::EncodableValue(std::move(output).TakeValue()));
reply(flutter::EncodableValue(std::move(wrapped)));
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
} }
reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} else { } else {
channel->SetMessageHandler(nullptr); channel->SetMessageHandler(nullptr);
@ -792,28 +788,27 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
const auto& args = std::get<flutter::EncodableList>(message); const auto& args = std::get<flutter::EncodableList>(message);
const auto& encodable_a_string_arg = args.at(0); const auto& encodable_a_string_arg = args.at(0);
if (encodable_a_string_arg.IsNull()) { if (encodable_a_string_arg.IsNull()) {
reply(flutter::EncodableValue( reply(WrapError("a_string_arg unexpectedly null."));
WrapError("a_string_arg unexpectedly null.")));
return; return;
} }
const auto& a_string_arg = const auto& a_string_arg =
std::get<std::string>(encodable_a_string_arg); std::get<std::string>(encodable_a_string_arg);
ErrorOr<std::string> output = api->EchoString(a_string_arg); ErrorOr<std::string> output = api->EchoString(a_string_arg);
if (output.has_error()) { if (output.has_error()) {
wrapped = WrapError(output.error()); reply(WrapError(output.error()));
} else { return;
wrapped.push_back(
flutter::EncodableValue(std::move(output).TakeValue()));
} }
flutter::EncodableList wrapped;
wrapped.push_back(
flutter::EncodableValue(std::move(output).TakeValue()));
reply(flutter::EncodableValue(std::move(wrapped)));
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
} }
reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} else { } else {
channel->SetMessageHandler(nullptr); channel->SetMessageHandler(nullptr);
@ -829,13 +824,11 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
const auto& args = std::get<flutter::EncodableList>(message); const auto& args = std::get<flutter::EncodableList>(message);
const auto& encodable_a_uint8_list_arg = args.at(0); const auto& encodable_a_uint8_list_arg = args.at(0);
if (encodable_a_uint8_list_arg.IsNull()) { if (encodable_a_uint8_list_arg.IsNull()) {
reply(flutter::EncodableValue( reply(WrapError("a_uint8_list_arg unexpectedly null."));
WrapError("a_uint8_list_arg unexpectedly null.")));
return; return;
} }
const auto& a_uint8_list_arg = const auto& a_uint8_list_arg =
@ -843,15 +836,16 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
ErrorOr<std::vector<uint8_t>> output = ErrorOr<std::vector<uint8_t>> output =
api->EchoUint8List(a_uint8_list_arg); api->EchoUint8List(a_uint8_list_arg);
if (output.has_error()) { if (output.has_error()) {
wrapped = WrapError(output.error()); reply(WrapError(output.error()));
} else { return;
wrapped.push_back(
flutter::EncodableValue(std::move(output).TakeValue()));
} }
flutter::EncodableList wrapped;
wrapped.push_back(
flutter::EncodableValue(std::move(output).TakeValue()));
reply(flutter::EncodableValue(std::move(wrapped)));
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
} }
reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} else { } else {
channel->SetMessageHandler(nullptr); channel->SetMessageHandler(nullptr);
@ -867,28 +861,27 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
const auto& args = std::get<flutter::EncodableList>(message); const auto& args = std::get<flutter::EncodableList>(message);
const auto& encodable_an_object_arg = args.at(0); const auto& encodable_an_object_arg = args.at(0);
if (encodable_an_object_arg.IsNull()) { if (encodable_an_object_arg.IsNull()) {
reply(flutter::EncodableValue( reply(WrapError("an_object_arg unexpectedly null."));
WrapError("an_object_arg unexpectedly null.")));
return; return;
} }
const auto& an_object_arg = encodable_an_object_arg; const auto& an_object_arg = encodable_an_object_arg;
ErrorOr<flutter::EncodableValue> output = ErrorOr<flutter::EncodableValue> output =
api->EchoObject(an_object_arg); api->EchoObject(an_object_arg);
if (output.has_error()) { if (output.has_error()) {
wrapped = WrapError(output.error()); reply(WrapError(output.error()));
} else { return;
wrapped.push_back(
flutter::EncodableValue(std::move(output).TakeValue()));
} }
flutter::EncodableList wrapped;
wrapped.push_back(
flutter::EncodableValue(std::move(output).TakeValue()));
reply(flutter::EncodableValue(std::move(wrapped)));
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
} }
reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} else { } else {
channel->SetMessageHandler(nullptr); channel->SetMessageHandler(nullptr);
@ -904,13 +897,11 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
const auto& args = std::get<flutter::EncodableList>(message); const auto& args = std::get<flutter::EncodableList>(message);
const auto& encodable_wrapper_arg = args.at(0); const auto& encodable_wrapper_arg = args.at(0);
if (encodable_wrapper_arg.IsNull()) { if (encodable_wrapper_arg.IsNull()) {
reply(flutter::EncodableValue( reply(WrapError("wrapper_arg unexpectedly null."));
WrapError("wrapper_arg unexpectedly null.")));
return; return;
} }
const auto& wrapper_arg = const auto& wrapper_arg =
@ -920,20 +911,21 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
ErrorOr<std::optional<std::string>> output = ErrorOr<std::optional<std::string>> output =
api->ExtractNestedNullableString(wrapper_arg); api->ExtractNestedNullableString(wrapper_arg);
if (output.has_error()) { if (output.has_error()) {
wrapped = WrapError(output.error()); reply(WrapError(output.error()));
} else { return;
auto output_optional = std::move(output).TakeValue();
if (output_optional) {
wrapped.push_back(flutter::EncodableValue(
std::move(output_optional).value()));
} else {
wrapped.push_back(flutter::EncodableValue());
}
} }
flutter::EncodableList wrapped;
auto output_optional = std::move(output).TakeValue();
if (output_optional) {
wrapped.push_back(flutter::EncodableValue(
std::move(output_optional).value()));
} else {
wrapped.push_back(flutter::EncodableValue());
}
reply(flutter::EncodableValue(std::move(wrapped)));
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
} }
reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} else { } else {
channel->SetMessageHandler(nullptr); channel->SetMessageHandler(nullptr);
@ -949,7 +941,6 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
const auto& args = std::get<flutter::EncodableList>(message); const auto& args = std::get<flutter::EncodableList>(message);
const auto& encodable_nullable_string_arg = args.at(0); const auto& encodable_nullable_string_arg = args.at(0);
@ -958,15 +949,16 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
ErrorOr<AllNullableTypesWrapper> output = ErrorOr<AllNullableTypesWrapper> output =
api->CreateNestedNullableString(nullable_string_arg); api->CreateNestedNullableString(nullable_string_arg);
if (output.has_error()) { if (output.has_error()) {
wrapped = WrapError(output.error()); reply(WrapError(output.error()));
} else { return;
wrapped.push_back(flutter::CustomEncodableValue(
std::move(output).TakeValue()));
} }
flutter::EncodableList wrapped;
wrapped.push_back(
flutter::CustomEncodableValue(std::move(output).TakeValue()));
reply(flutter::EncodableValue(std::move(wrapped)));
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
} }
reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} else { } else {
channel->SetMessageHandler(nullptr); channel->SetMessageHandler(nullptr);
@ -982,7 +974,6 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
const auto& args = std::get<flutter::EncodableList>(message); const auto& args = std::get<flutter::EncodableList>(message);
const auto& encodable_a_nullable_bool_arg = args.at(0); const auto& encodable_a_nullable_bool_arg = args.at(0);
@ -1004,15 +995,16 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
a_nullable_bool_arg, a_nullable_int_arg, a_nullable_bool_arg, a_nullable_int_arg,
a_nullable_string_arg); a_nullable_string_arg);
if (output.has_error()) { if (output.has_error()) {
wrapped = WrapError(output.error()); reply(WrapError(output.error()));
} else { return;
wrapped.push_back(flutter::CustomEncodableValue(
std::move(output).TakeValue()));
} }
flutter::EncodableList wrapped;
wrapped.push_back(
flutter::CustomEncodableValue(std::move(output).TakeValue()));
reply(flutter::EncodableValue(std::move(wrapped)));
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
} }
reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} else { } else {
channel->SetMessageHandler(nullptr); channel->SetMessageHandler(nullptr);
@ -1028,7 +1020,6 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
const auto& args = std::get<flutter::EncodableList>(message); const auto& args = std::get<flutter::EncodableList>(message);
const auto& encodable_a_nullable_int_arg = args.at(0); const auto& encodable_a_nullable_int_arg = args.at(0);
@ -1043,20 +1034,21 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
ErrorOr<std::optional<int64_t>> output = ErrorOr<std::optional<int64_t>> output =
api->EchoNullableInt(a_nullable_int_arg); api->EchoNullableInt(a_nullable_int_arg);
if (output.has_error()) { if (output.has_error()) {
wrapped = WrapError(output.error()); reply(WrapError(output.error()));
} else { return;
auto output_optional = std::move(output).TakeValue();
if (output_optional) {
wrapped.push_back(flutter::EncodableValue(
std::move(output_optional).value()));
} else {
wrapped.push_back(flutter::EncodableValue());
}
} }
flutter::EncodableList wrapped;
auto output_optional = std::move(output).TakeValue();
if (output_optional) {
wrapped.push_back(flutter::EncodableValue(
std::move(output_optional).value()));
} else {
wrapped.push_back(flutter::EncodableValue());
}
reply(flutter::EncodableValue(std::move(wrapped)));
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
} }
reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} else { } else {
channel->SetMessageHandler(nullptr); channel->SetMessageHandler(nullptr);
@ -1072,7 +1064,6 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
const auto& args = std::get<flutter::EncodableList>(message); const auto& args = std::get<flutter::EncodableList>(message);
const auto& encodable_a_nullable_double_arg = args.at(0); const auto& encodable_a_nullable_double_arg = args.at(0);
@ -1081,20 +1072,21 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
ErrorOr<std::optional<double>> output = ErrorOr<std::optional<double>> output =
api->EchoNullableDouble(a_nullable_double_arg); api->EchoNullableDouble(a_nullable_double_arg);
if (output.has_error()) { if (output.has_error()) {
wrapped = WrapError(output.error()); reply(WrapError(output.error()));
} else { return;
auto output_optional = std::move(output).TakeValue();
if (output_optional) {
wrapped.push_back(flutter::EncodableValue(
std::move(output_optional).value()));
} else {
wrapped.push_back(flutter::EncodableValue());
}
} }
flutter::EncodableList wrapped;
auto output_optional = std::move(output).TakeValue();
if (output_optional) {
wrapped.push_back(flutter::EncodableValue(
std::move(output_optional).value()));
} else {
wrapped.push_back(flutter::EncodableValue());
}
reply(flutter::EncodableValue(std::move(wrapped)));
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
} }
reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} else { } else {
channel->SetMessageHandler(nullptr); channel->SetMessageHandler(nullptr);
@ -1110,7 +1102,6 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
const auto& args = std::get<flutter::EncodableList>(message); const auto& args = std::get<flutter::EncodableList>(message);
const auto& encodable_a_nullable_bool_arg = args.at(0); const auto& encodable_a_nullable_bool_arg = args.at(0);
@ -1119,20 +1110,21 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
ErrorOr<std::optional<bool>> output = ErrorOr<std::optional<bool>> output =
api->EchoNullableBool(a_nullable_bool_arg); api->EchoNullableBool(a_nullable_bool_arg);
if (output.has_error()) { if (output.has_error()) {
wrapped = WrapError(output.error()); reply(WrapError(output.error()));
} else { return;
auto output_optional = std::move(output).TakeValue();
if (output_optional) {
wrapped.push_back(flutter::EncodableValue(
std::move(output_optional).value()));
} else {
wrapped.push_back(flutter::EncodableValue());
}
} }
flutter::EncodableList wrapped;
auto output_optional = std::move(output).TakeValue();
if (output_optional) {
wrapped.push_back(flutter::EncodableValue(
std::move(output_optional).value()));
} else {
wrapped.push_back(flutter::EncodableValue());
}
reply(flutter::EncodableValue(std::move(wrapped)));
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
} }
reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} else { } else {
channel->SetMessageHandler(nullptr); channel->SetMessageHandler(nullptr);
@ -1148,7 +1140,6 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
const auto& args = std::get<flutter::EncodableList>(message); const auto& args = std::get<flutter::EncodableList>(message);
const auto& encodable_a_nullable_string_arg = args.at(0); const auto& encodable_a_nullable_string_arg = args.at(0);
@ -1157,20 +1148,21 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
ErrorOr<std::optional<std::string>> output = ErrorOr<std::optional<std::string>> output =
api->EchoNullableString(a_nullable_string_arg); api->EchoNullableString(a_nullable_string_arg);
if (output.has_error()) { if (output.has_error()) {
wrapped = WrapError(output.error()); reply(WrapError(output.error()));
} else { return;
auto output_optional = std::move(output).TakeValue();
if (output_optional) {
wrapped.push_back(flutter::EncodableValue(
std::move(output_optional).value()));
} else {
wrapped.push_back(flutter::EncodableValue());
}
} }
flutter::EncodableList wrapped;
auto output_optional = std::move(output).TakeValue();
if (output_optional) {
wrapped.push_back(flutter::EncodableValue(
std::move(output_optional).value()));
} else {
wrapped.push_back(flutter::EncodableValue());
}
reply(flutter::EncodableValue(std::move(wrapped)));
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
} }
reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} else { } else {
channel->SetMessageHandler(nullptr); channel->SetMessageHandler(nullptr);
@ -1186,7 +1178,6 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
const auto& args = std::get<flutter::EncodableList>(message); const auto& args = std::get<flutter::EncodableList>(message);
const auto& encodable_a_nullable_uint8_list_arg = args.at(0); const auto& encodable_a_nullable_uint8_list_arg = args.at(0);
@ -1196,20 +1187,21 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
ErrorOr<std::optional<std::vector<uint8_t>>> output = ErrorOr<std::optional<std::vector<uint8_t>>> output =
api->EchoNullableUint8List(a_nullable_uint8_list_arg); api->EchoNullableUint8List(a_nullable_uint8_list_arg);
if (output.has_error()) { if (output.has_error()) {
wrapped = WrapError(output.error()); reply(WrapError(output.error()));
} else { return;
auto output_optional = std::move(output).TakeValue();
if (output_optional) {
wrapped.push_back(flutter::EncodableValue(
std::move(output_optional).value()));
} else {
wrapped.push_back(flutter::EncodableValue());
}
} }
flutter::EncodableList wrapped;
auto output_optional = std::move(output).TakeValue();
if (output_optional) {
wrapped.push_back(flutter::EncodableValue(
std::move(output_optional).value()));
} else {
wrapped.push_back(flutter::EncodableValue());
}
reply(flutter::EncodableValue(std::move(wrapped)));
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
} }
reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} else { } else {
channel->SetMessageHandler(nullptr); channel->SetMessageHandler(nullptr);
@ -1225,7 +1217,6 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
const auto& args = std::get<flutter::EncodableList>(message); const auto& args = std::get<flutter::EncodableList>(message);
const auto& encodable_a_nullable_object_arg = args.at(0); const auto& encodable_a_nullable_object_arg = args.at(0);
@ -1234,20 +1225,21 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
ErrorOr<std::optional<flutter::EncodableValue>> output = ErrorOr<std::optional<flutter::EncodableValue>> output =
api->EchoNullableObject(a_nullable_object_arg); api->EchoNullableObject(a_nullable_object_arg);
if (output.has_error()) { if (output.has_error()) {
wrapped = WrapError(output.error()); reply(WrapError(output.error()));
} else { return;
auto output_optional = std::move(output).TakeValue();
if (output_optional) {
wrapped.push_back(flutter::EncodableValue(
std::move(output_optional).value()));
} else {
wrapped.push_back(flutter::EncodableValue());
}
} }
flutter::EncodableList wrapped;
auto output_optional = std::move(output).TakeValue();
if (output_optional) {
wrapped.push_back(flutter::EncodableValue(
std::move(output_optional).value()));
} else {
wrapped.push_back(flutter::EncodableValue());
}
reply(flutter::EncodableValue(std::move(wrapped)));
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
} }
reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} else { } else {
channel->SetMessageHandler(nullptr); channel->SetMessageHandler(nullptr);
@ -1262,20 +1254,18 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
api->NoopAsync( api->NoopAsync([reply](std::optional<FlutterError>&& output) {
[&wrapped, &reply](std::optional<FlutterError>&& output) { if (output.has_value()) {
if (output.has_value()) { reply(WrapError(output.value()));
wrapped = WrapError(output.value()); return;
} else { }
wrapped.push_back(flutter::EncodableValue()); flutter::EncodableList wrapped;
} wrapped.push_back(flutter::EncodableValue());
reply(flutter::EncodableValue(std::move(wrapped))); reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
reply(flutter::EncodableValue(std::move(wrapped)));
} }
}); });
} else { } else {
@ -1292,31 +1282,28 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
const auto& args = std::get<flutter::EncodableList>(message); const auto& args = std::get<flutter::EncodableList>(message);
const auto& encodable_a_string_arg = args.at(0); const auto& encodable_a_string_arg = args.at(0);
if (encodable_a_string_arg.IsNull()) { if (encodable_a_string_arg.IsNull()) {
reply(flutter::EncodableValue( reply(WrapError("a_string_arg unexpectedly null."));
WrapError("a_string_arg unexpectedly null.")));
return; return;
} }
const auto& a_string_arg = const auto& a_string_arg =
std::get<std::string>(encodable_a_string_arg); std::get<std::string>(encodable_a_string_arg);
api->EchoAsyncString( api->EchoAsyncString(
a_string_arg, a_string_arg, [reply](ErrorOr<std::string>&& output) {
[&wrapped, &reply](ErrorOr<std::string>&& output) {
if (output.has_error()) { if (output.has_error()) {
wrapped = WrapError(output.error()); reply(WrapError(output.error()));
} else { return;
wrapped.push_back(flutter::EncodableValue(
std::move(output).TakeValue()));
} }
flutter::EncodableList wrapped;
wrapped.push_back(
flutter::EncodableValue(std::move(output).TakeValue()));
reply(flutter::EncodableValue(std::move(wrapped))); reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
reply(flutter::EncodableValue(std::move(wrapped)));
} }
}); });
} else { } else {
@ -1333,20 +1320,19 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
api->CallFlutterNoop( api->CallFlutterNoop(
[&wrapped, &reply](std::optional<FlutterError>&& output) { [reply](std::optional<FlutterError>&& output) {
if (output.has_value()) { if (output.has_value()) {
wrapped = WrapError(output.value()); reply(WrapError(output.value()));
} else { return;
wrapped.push_back(flutter::EncodableValue());
} }
flutter::EncodableList wrapped;
wrapped.push_back(flutter::EncodableValue());
reply(flutter::EncodableValue(std::move(wrapped))); reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
reply(flutter::EncodableValue(std::move(wrapped)));
} }
}); });
} else { } else {
@ -1363,31 +1349,28 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
const auto& args = std::get<flutter::EncodableList>(message); const auto& args = std::get<flutter::EncodableList>(message);
const auto& encodable_a_string_arg = args.at(0); const auto& encodable_a_string_arg = args.at(0);
if (encodable_a_string_arg.IsNull()) { if (encodable_a_string_arg.IsNull()) {
reply(flutter::EncodableValue( reply(WrapError("a_string_arg unexpectedly null."));
WrapError("a_string_arg unexpectedly null.")));
return; return;
} }
const auto& a_string_arg = const auto& a_string_arg =
std::get<std::string>(encodable_a_string_arg); std::get<std::string>(encodable_a_string_arg);
api->CallFlutterEchoString( api->CallFlutterEchoString(
a_string_arg, a_string_arg, [reply](ErrorOr<std::string>&& output) {
[&wrapped, &reply](ErrorOr<std::string>&& output) {
if (output.has_error()) { if (output.has_error()) {
wrapped = WrapError(output.error()); reply(WrapError(output.error()));
} else { return;
wrapped.push_back(flutter::EncodableValue(
std::move(output).TakeValue()));
} }
flutter::EncodableList wrapped;
wrapped.push_back(
flutter::EncodableValue(std::move(output).TakeValue()));
reply(flutter::EncodableValue(std::move(wrapped))); reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
reply(flutter::EncodableValue(std::move(wrapped)));
} }
}); });
} else { } else {
@ -1396,17 +1379,17 @@ void HostIntegrationCoreApi::SetUp(flutter::BinaryMessenger* binary_messenger,
} }
} }
flutter::EncodableList HostIntegrationCoreApi::WrapError( flutter::EncodableValue HostIntegrationCoreApi::WrapError(
std::string_view error_message) { std::string_view error_message) {
return flutter::EncodableList( return flutter::EncodableValue(flutter::EncodableList{
{flutter::EncodableValue(std::string(error_message)), flutter::EncodableValue(std::string(error_message)),
flutter::EncodableValue("Error"), flutter::EncodableValue()}); flutter::EncodableValue("Error"), flutter::EncodableValue()});
} }
flutter::EncodableList HostIntegrationCoreApi::WrapError( flutter::EncodableValue HostIntegrationCoreApi::WrapError(
const FlutterError& error) { const FlutterError& error) {
return flutter::EncodableList({flutter::EncodableValue(error.message()), return flutter::EncodableValue(flutter::EncodableList{
flutter::EncodableValue(error.code()), flutter::EncodableValue(error.message()),
error.details()}); flutter::EncodableValue(error.code()), error.details()});
} }
FlutterIntegrationCoreApiCodecSerializer:: FlutterIntegrationCoreApiCodecSerializer::
@ -1561,18 +1544,18 @@ void HostTrivialApi::SetUp(flutter::BinaryMessenger* binary_messenger,
channel->SetMessageHandler( channel->SetMessageHandler(
[api](const flutter::EncodableValue& message, [api](const flutter::EncodableValue& message,
const flutter::MessageReply<flutter::EncodableValue>& reply) { const flutter::MessageReply<flutter::EncodableValue>& reply) {
flutter::EncodableList wrapped;
try { try {
std::optional<FlutterError> output = api->Noop(); std::optional<FlutterError> output = api->Noop();
if (output.has_value()) { if (output.has_value()) {
wrapped = WrapError(output.value()); reply(WrapError(output.value()));
} else { return;
wrapped.push_back(flutter::EncodableValue());
} }
flutter::EncodableList wrapped;
wrapped.push_back(flutter::EncodableValue());
reply(flutter::EncodableValue(std::move(wrapped)));
} catch (const std::exception& exception) { } catch (const std::exception& exception) {
wrapped = WrapError(exception.what()); reply(WrapError(exception.what()));
} }
reply(flutter::EncodableValue(std::move(wrapped)));
}); });
} else { } else {
channel->SetMessageHandler(nullptr); channel->SetMessageHandler(nullptr);
@ -1580,16 +1563,16 @@ void HostTrivialApi::SetUp(flutter::BinaryMessenger* binary_messenger,
} }
} }
flutter::EncodableList HostTrivialApi::WrapError( flutter::EncodableValue HostTrivialApi::WrapError(
std::string_view error_message) { std::string_view error_message) {
return flutter::EncodableList( return flutter::EncodableValue(flutter::EncodableList{
{flutter::EncodableValue(std::string(error_message)), flutter::EncodableValue(std::string(error_message)),
flutter::EncodableValue("Error"), flutter::EncodableValue()}); flutter::EncodableValue("Error"), flutter::EncodableValue()});
} }
flutter::EncodableList HostTrivialApi::WrapError(const FlutterError& error) { flutter::EncodableValue HostTrivialApi::WrapError(const FlutterError& error) {
return flutter::EncodableList({flutter::EncodableValue(error.message()), return flutter::EncodableValue(flutter::EncodableList{
flutter::EncodableValue(error.code()), flutter::EncodableValue(error.message()),
error.details()}); flutter::EncodableValue(error.code()), error.details()});
} }
} // namespace core_tests_pigeontest } // namespace core_tests_pigeontest

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
// Autogenerated from Pigeon (v5.0.0), do not edit directly. // Autogenerated from Pigeon (v5.0.1), do not edit directly.
// See also: https://pub.dev/packages/pigeon // See also: https://pub.dev/packages/pigeon
#ifndef PIGEON_CORE_TESTS_PIGEONTEST_H_ #ifndef PIGEON_CORE_TESTS_PIGEONTEST_H_
@ -336,8 +336,8 @@ class HostIntegrationCoreApi {
// the `binary_messenger`. // the `binary_messenger`.
static void SetUp(flutter::BinaryMessenger* binary_messenger, static void SetUp(flutter::BinaryMessenger* binary_messenger,
HostIntegrationCoreApi* api); HostIntegrationCoreApi* api);
static flutter::EncodableList WrapError(std::string_view error_message); static flutter::EncodableValue WrapError(std::string_view error_message);
static flutter::EncodableList WrapError(const FlutterError& error); static flutter::EncodableValue WrapError(const FlutterError& error);
protected: protected:
HostIntegrationCoreApi() = default; HostIntegrationCoreApi() = default;
@ -405,8 +405,8 @@ class HostTrivialApi {
// `binary_messenger`. // `binary_messenger`.
static void SetUp(flutter::BinaryMessenger* binary_messenger, static void SetUp(flutter::BinaryMessenger* binary_messenger,
HostTrivialApi* api); HostTrivialApi* api);
static flutter::EncodableList WrapError(std::string_view error_message); static flutter::EncodableValue WrapError(std::string_view error_message);
static flutter::EncodableList WrapError(const FlutterError& error); static flutter::EncodableValue WrapError(const FlutterError& error);
protected: protected:
HostTrivialApi() = default; HostTrivialApi() = default;

View File

@ -2,7 +2,7 @@ name: pigeon
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier. description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
repository: https://github.com/flutter/packages/tree/main/packages/pigeon repository: https://github.com/flutter/packages/tree/main/packages/pigeon
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3Apigeon issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3Apigeon
version: 5.0.0 # This must match the version in lib/generator_tools.dart version: 5.0.1 # This must match the version in lib/generator_tools.dart
environment: environment:
sdk: ">=2.12.0 <3.0.0" sdk: ">=2.12.0 <3.0.0"

View File

@ -1300,4 +1300,64 @@ void main() {
expect(code, isNot(contains('reply(wrap'))); expect(code, isNot(contains('reply(wrap')));
expect(code, contains('reply(flutter::EncodableValue(')); expect(code, contains('reply(flutter::EncodableValue('));
}); });
test('does not keep unowned references in async handlers', () {
final Root root = Root(apis: <Api>[
Api(name: 'HostApi', location: ApiLocation.host, methods: <Method>[
Method(
name: 'noop',
arguments: <NamedType>[],
returnType: const TypeDeclaration.voidDeclaration(),
isAsynchronous: true,
),
Method(
name: 'doSomething',
arguments: <NamedType>[
NamedType(
type: const TypeDeclaration(
baseName: 'int',
isNullable: false,
),
name: '')
],
returnType:
const TypeDeclaration(baseName: 'double', isNullable: false),
isAsynchronous: true,
),
]),
Api(name: 'FlutterApi', location: ApiLocation.flutter, methods: <Method>[
Method(
name: 'noop',
arguments: <NamedType>[],
returnType: const TypeDeclaration.voidDeclaration(),
isAsynchronous: true,
),
Method(
name: 'doSomething',
arguments: <NamedType>[
NamedType(
type: const TypeDeclaration(
baseName: 'String',
isNullable: false,
),
name: '')
],
returnType:
const TypeDeclaration(baseName: 'bool', isNullable: false),
isAsynchronous: true,
),
])
], classes: <Class>[], enums: <Enum>[]);
final StringBuffer sink = StringBuffer();
generateCppSource(const CppOptions(), root, sink);
final String code = sink.toString();
// Nothing should be captured by reference for async handlers, since their
// lifetime is unknown (and expected to be longer than the stack's).
expect(code, isNot(contains('&reply')));
expect(code, isNot(contains('&wrapped')));
// Check for the exact capture format that is currently being used, to
// ensure that the negative tests above get updated if there are any
// changes to lambda capture.
expect(code, contains('[reply]('));
});
} }