Properly throw Dart error from PluginUtilities.getCallbackHandle FFI (flutter/engine#36032)

This commit is contained in:
Callum Moffat
2022-09-16 13:11:08 -04:00
committed by GitHub
parent 552dab9cf0
commit 0a5737e441
3 changed files with 104 additions and 1 deletions

View File

@@ -259,7 +259,9 @@ Dart_Handle DartRuntimeHooks::GetCallbackHandle(Dart_Handle func) {
}
Dart_Handle DartRuntimeHooks::GetCallbackFromHandle(int64_t handle) {
return DartCallbackCache::GetCallback(handle);
Dart_Handle result = DartCallbackCache::GetCallback(handle);
PropagateIfError(result);
return result;
}
void DartPluginRegistrant_EnsureInitialized() {

View File

@@ -364,3 +364,74 @@ Future<void> toImageSync() async {
picture2.dispose();
notifyNative();
}
@pragma('vm:entry-point')
Future<void> included() async {
}
Future<void> excluded() async {
}
class IsolateParam {
const IsolateParam(this.sendPort, this.rawHandle);
final SendPort sendPort;
final int rawHandle;
}
@pragma('vm:entry-point')
Future<void> runCallback(IsolateParam param) async {
try {
final Future<dynamic> Function() func = PluginUtilities.getCallbackFromHandle(
CallbackHandle.fromRawHandle(param.rawHandle)
)! as Future<dynamic> Function();
await func.call();
param.sendPort.send(true);
}
on NoSuchMethodError {
param.sendPort.send(false);
}
}
@pragma('vm:entry-point')
void notifyNativeBool(bool value) native 'NotifyNativeBool';
@pragma('vm:entry-point')
Future<void> testPluginUtilitiesCallbackHandle() async {
ReceivePort port = ReceivePort();
await Isolate.spawn(
runCallback,
IsolateParam(
port.sendPort,
PluginUtilities.getCallbackHandle(included)!.toRawHandle()
),
onError: port.sendPort
);
final dynamic result1 = await port.first;
if (result1 != true) {
print('Expected $result1 to == true');
notifyNativeBool(false);
return;
}
port.close();
if (const bool.fromEnvironment('dart.vm.product')) {
port = ReceivePort();
await Isolate.spawn(
runCallback,
IsolateParam(
port.sendPort,
PluginUtilities.getCallbackHandle(excluded)!.toRawHandle()
),
onError: port.sendPort
);
final dynamic result2 = await port.first;
if (result2 != false) {
print('Expected $result2 to == false');
notifyNativeBool(false);
return;
}
port.close();
}
notifyNativeBool(true);
}

View File

@@ -3850,6 +3850,36 @@ TEST_F(ShellTest, PictureToImageSync) {
DestroyShell(std::move(shell));
}
TEST_F(ShellTest, PluginUtilitiesCallbackHandleErrorHandling) {
auto settings = CreateSettingsForFixture();
std::unique_ptr<Shell> shell =
CreateShell(settings, GetTaskRunnersForFixture());
fml::AutoResetWaitableEvent latch;
bool test_passed;
AddNativeCallback("NotifyNativeBool", CREATE_NATIVE_ENTRY([&](auto args) {
Dart_Handle exception = nullptr;
test_passed = tonic::DartConverter<bool>::FromArguments(
args, 0, exception);
latch.Signal();
}));
ASSERT_NE(shell, nullptr);
ASSERT_TRUE(shell->IsSetup());
auto configuration = RunConfiguration::InferFromSettings(settings);
PlatformViewNotifyCreated(shell.get());
configuration.SetEntrypoint("testPluginUtilitiesCallbackHandle");
RunEngine(shell.get(), std::move(configuration));
PumpOneFrame(shell.get());
latch.Wait();
ASSERT_TRUE(test_passed);
PlatformViewNotifyDestroyed(shell.get());
DestroyShell(std::move(shell));
}
} // namespace testing
} // namespace flutter