File tree Expand file tree Collapse file tree 9 files changed +74
-0
lines changed Expand file tree Collapse file tree 9 files changed +74
-0
lines changed Original file line number Diff line number Diff line change 66// the patches are applied.
77import "dart:_internal" show patch;
88import 'dart:typed_data' ;
9+ import 'dart:isolate' ;
910
1011DynamicLibrary _open (String name) native "Ffi_dl_open" ;
1112DynamicLibrary _processLibrary () native "Ffi_dl_processLibrary" ;
Original file line number Diff line number Diff line change 66// the patches are applied.
77import "dart:_internal" show patch;
88import 'dart:typed_data' ;
9+ import 'dart:isolate' ;
910
1011// NativeType is not private, because it is used in type arguments.
1112// NativeType is abstract because it not used with const constructors in
Original file line number Diff line number Diff line change 66// the patches are applied.
77import "dart:_internal" show patch;
88import 'dart:typed_data' ;
9+ import 'dart:isolate' ;
910
1011const Map <Type , int > _knownSizes = {
1112 Int8 : 1 ,
@@ -442,3 +443,8 @@ extension StructPointer<T extends Struct> on Pointer<T> {
442443 @patch
443444 T operator [](int index) => _loadStruct (this , index);
444445}
446+
447+ extension NativePort on SendPort {
448+ @patch
449+ int get nativePort native "SendPortImpl_get_id" ;
450+ }
Original file line number Diff line number Diff line change 1515library dart.ffi;
1616
1717import 'dart:typed_data' ;
18+ import 'dart:isolate' ;
1819
1920part "native_type.dart" ;
2021part "annotations.dart" ;
@@ -609,3 +610,13 @@ extension StructPointer<T extends Struct> on Pointer<T> {
609610 /// the platform.
610611 external T operator [](int index);
611612}
613+
614+ /// Extension to retrieve the native `Dart_Port` from a [SendPort] .
615+ extension NativePort on SendPort {
616+ /// The native port of this [SendPort] .
617+ ///
618+ /// The returned native port can for example be used by C code to post
619+ /// messages to the connected [ReceivePort] via `Dart_PostCObject()` - see
620+ /// `dart_native_api.h` .
621+ external int get nativePort;
622+ }
Original file line number Diff line number Diff line change 88// the patches are applied.
99import "dart:_internal" show patch;
1010import 'dart:typed_data' ;
11+ import 'dart:isolate' ;
1112
1213DynamicLibrary _open (String name) native "Ffi_dl_open" ;
1314DynamicLibrary _processLibrary () native "Ffi_dl_processLibrary" ;
Original file line number Diff line number Diff line change 88// the patches are applied.
99import "dart:_internal" show patch;
1010import 'dart:typed_data' ;
11+ import 'dart:isolate' ;
1112
1213// NativeType is not private, because it is used in type arguments.
1314// NativeType is abstract because it not used with const constructors in
Original file line number Diff line number Diff line change 88// the patches are applied.
99import "dart:_internal" show patch;
1010import 'dart:typed_data' ;
11+ import 'dart:isolate' ;
1112
1213const Map <Type , int > _knownSizes = {
1314 Int8 : 1 ,
@@ -444,3 +445,8 @@ extension StructPointer<T extends Struct> on Pointer<T> {
444445 @patch
445446 T operator [](int index) => _loadStruct (this , index);
446447}
448+
449+ extension NativePort on SendPort {
450+ @patch
451+ int get nativePort native "SendPortImpl_get_id" ;
452+ }
Original file line number Diff line number Diff line change @@ -611,3 +611,13 @@ extension StructPointer<T extends Struct> on Pointer<T> {
611611 /// the platform.
612612 external T operator [](int index);
613613}
614+
615+ /// Extension to retrieve the native `Dart_Port` from a [SendPort] .
616+ extension NativePort on SendPort {
617+ /// The native port of this [SendPort] .
618+ ///
619+ /// The returned native port can for example be used by C code to post
620+ /// messages to the connected [ReceivePort] via `Dart_PostCObject()` - see
621+ /// `dart_native_api.h` .
622+ external int get nativePort;
623+ }
Original file line number Diff line number Diff line change 1+ // Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+ // for details. All rights reserved. Use of this source code is governed by a
3+ // BSD-style license that can be found in the LICENSE file.
4+
5+ import 'dart:async' ;
6+ import 'dart:ffi' ;
7+ import 'dart:isolate' ;
8+
9+ import 'package:expect/expect.dart' ;
10+
11+ typedef Dart_PostIntegerNFT = IntPtr Function (Int64 port, Int64 message);
12+ typedef Dart_PostIntegerFT = int Function (int port, int message);
13+
14+ main () async {
15+ const int message = 112344556677888 ;
16+
17+ final completer = Completer ();
18+
19+ final receivePort = ReceivePort ()
20+ ..listen ((receivedMessage) => completer.complete (receivedMessage));
21+
22+ final executableSymbols = DynamicLibrary .executable ();
23+
24+ final postInteger =
25+ executableSymbols.lookupFunction <Dart_PostIntegerNFT , Dart_PostIntegerFT >(
26+ "Dart_PostInteger" );
27+
28+ // Issue(dartbug.com/38545): The dart:ffi doesn't have a bool type yet.
29+ final bool success =
30+ postInteger (receivePort.sendPort.nativePort, message) != 0 ;
31+ Expect .isTrue (success);
32+
33+ final postedMessage = await completer.future;
34+ Expect .equals (message, postedMessage);
35+
36+ receivePort.close ();
37+ }
You can’t perform that action at this time.
0 commit comments