-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[analyzer] Support for RawVoidCallbacks
Pointer.fromFunction returns a native function pointer to a Dart function, but has the restriction that the native code must invoke it on the same thread as the isolate that created it. RawVoidCallbacks is a new API that is similar to Pointer.fromFunction. It returns a native function pointer that can be invoked on any thread, with the restriction that the Dart function must return void. Under the hood we forward the function args over a port to the target isolate. We're not 100% settled on the name of the class, but the overall API design won't change. I'll make sure to get the naming finalized before submitting this CL. Doc with discussion of naming: https://docs.google.com/document/d/1z9Rgahoid2AhC9JXwsDAEODvlJS6dvBHCbcGkOxv_ws/edit?resourcekey=0-TbdNiSL-fdwskla02QaPwg#heading=h.te70ikwelbw8 Bug: #37022 Change-Id: Iba98f6f803c52919b942fa054df1060991574c8c TEST=ffi_async_callback_test.dart Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/308860 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Liam Appelbe <liama@google.com> Reviewed-by: Marya Belanger <mbelanger@google.com> Reviewed-by: Daco Harkes <dacoharkes@google.com>
- Loading branch information
1 parent
c24c41e
commit 583da43
Showing
9 changed files
with
246 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
pkg/analyzer/test/src/diagnostics/ffi_async_callback_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:analyzer/src/dart/error/ffi_code.dart'; | ||
import 'package:test_reflective_loader/test_reflective_loader.dart'; | ||
|
||
import '../dart/resolution/context_collection_resolution.dart'; | ||
|
||
main() { | ||
defineReflectiveSuite(() { | ||
defineReflectiveTests(FfiRawVoidCallbacksMustReturnVoid); | ||
}); | ||
} | ||
|
||
@reflectiveTest | ||
class FfiRawVoidCallbacksMustReturnVoid extends PubPackageResolutionTest { | ||
test_RawVoidCallback_inferred() async { | ||
await assertErrorsInCode(r''' | ||
import 'dart:ffi'; | ||
void f(int i) => i * 2; | ||
void g() { | ||
RawVoidCallback<Void Function(Int32)>? callback; | ||
callback = RawVoidCallback(f); | ||
callback.close(); | ||
} | ||
''', []); | ||
} | ||
|
||
test_RawVoidCallback_mustBeANativeFunctionType() async { | ||
await assertErrorsInCode(r''' | ||
import 'dart:ffi'; | ||
void f(int i) => i * 2; | ||
void g() { | ||
RawVoidCallback<void Function(int)>(f); | ||
} | ||
''', [ | ||
error(FfiCode.MUST_BE_A_NATIVE_FUNCTION_TYPE, 56, 35), | ||
]); | ||
} | ||
|
||
test_RawVoidCallback_mustBeASubtype() async { | ||
await assertErrorsInCode(r''' | ||
import 'dart:ffi'; | ||
void f(int i) => i * 2; | ||
void g() { | ||
RawVoidCallback<Void Function(Double)>(f); | ||
} | ||
''', [ | ||
error(FfiCode.MUST_BE_A_SUBTYPE, 95, 1), | ||
]); | ||
} | ||
|
||
test_RawVoidCallback_mustHaveTypeArgs() async { | ||
await assertErrorsInCode(r''' | ||
import 'dart:ffi'; | ||
int f(int i) => i * 2; | ||
void g() { | ||
RawVoidCallback(f); | ||
} | ||
''', [ | ||
error(FfiCode.MUST_BE_A_NATIVE_FUNCTION_TYPE, 55, 15), | ||
]); | ||
} | ||
|
||
test_RawVoidCallback_mustReturnVoid() async { | ||
await assertErrorsInCode(r''' | ||
import 'dart:ffi'; | ||
int f(int i) => i * 2; | ||
void g() { | ||
RawVoidCallback<Int32 Function(Int32)>(f); | ||
} | ||
''', [ | ||
error(FfiCode.MUST_RETURN_VOID, 94, 1), | ||
]); | ||
} | ||
|
||
test_RawVoidCallback_ok() async { | ||
await assertErrorsInCode(r''' | ||
import 'dart:ffi'; | ||
void f(int i) => i * 2; | ||
void g() { | ||
RawVoidCallback<Void Function(Int32)>(f); | ||
} | ||
''', []); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters