From 96b990ab3821840b2c4980582243f03847fd92c2 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Thu, 8 Aug 2024 13:08:54 +0200 Subject: [PATCH] [ffi] Use `@Native`s for process lookup --- pkgs/ffi/CHANGELOG.md | 4 +++- pkgs/ffi/lib/src/allocation.dart | 32 ++++++++++++++++---------------- pkgs/ffi/pubspec.yaml | 4 ++-- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/pkgs/ffi/CHANGELOG.md b/pkgs/ffi/CHANGELOG.md index bd369b7fe..fa7df106d 100644 --- a/pkgs/ffi/CHANGELOG.md +++ b/pkgs/ffi/CHANGELOG.md @@ -1,6 +1,8 @@ -## 2.1.3-wip +## 2.1.3 - Use `package:dart_flutter_team_lints`. +- Migrate from `DynamicLibrary.process()` to `@Native external` functions. + https://github.com/dart-lang/native/issues/1401 ## 2.1.2 diff --git a/pkgs/ffi/lib/src/allocation.dart b/pkgs/ffi/lib/src/allocation.dart index a3e5330a8..fc3a4a88b 100644 --- a/pkgs/ffi/lib/src/allocation.dart +++ b/pkgs/ffi/lib/src/allocation.dart @@ -5,37 +5,37 @@ import 'dart:ffi'; import 'dart:io'; -// Note that ole32.dll is the correct name in both 32-bit and 64-bit. -final DynamicLibrary stdlib = Platform.isWindows - ? DynamicLibrary.open('ole32.dll') - : DynamicLibrary.process(); - typedef PosixMallocNative = Pointer Function(IntPtr); -typedef PosixMalloc = Pointer Function(int); -final PosixMalloc posixMalloc = - stdlib.lookupFunction('malloc'); + +@Native(symbol: 'malloc') +external Pointer posixMalloc(int size); typedef PosixCallocNative = Pointer Function(IntPtr num, IntPtr size); -typedef PosixCalloc = Pointer Function(int num, int size); -final PosixCalloc posixCalloc = - stdlib.lookupFunction('calloc'); + +@Native(symbol: 'malloc') +external Pointer posixCalloc(int num, int size); typedef PosixFreeNative = Void Function(Pointer); -typedef PosixFree = void Function(Pointer); + +@Native(symbol: 'free') +external void posixFree(Pointer ptr); + final Pointer> posixFreePointer = - stdlib.lookup('free'); -final PosixFree posixFree = posixFreePointer.asFunction(); + Native.addressOf(posixFree); + +// Note that ole32.dll is the correct name in both 32-bit and 64-bit. +final DynamicLibrary ole32lib = DynamicLibrary.open('ole32.dll'); typedef WinCoTaskMemAllocNative = Pointer Function(Size); typedef WinCoTaskMemAlloc = Pointer Function(int); final WinCoTaskMemAlloc winCoTaskMemAlloc = - stdlib.lookupFunction( + ole32lib.lookupFunction( 'CoTaskMemAlloc'); typedef WinCoTaskMemFreeNative = Void Function(Pointer); typedef WinCoTaskMemFree = void Function(Pointer); final Pointer> winCoTaskMemFreePointer = - stdlib.lookup('CoTaskMemFree'); + ole32lib.lookup('CoTaskMemFree'); final WinCoTaskMemFree winCoTaskMemFree = winCoTaskMemFreePointer.asFunction(); /// Manages memory on the native heap. diff --git a/pkgs/ffi/pubspec.yaml b/pkgs/ffi/pubspec.yaml index fdcf9533b..1a3e9d240 100644 --- a/pkgs/ffi/pubspec.yaml +++ b/pkgs/ffi/pubspec.yaml @@ -1,5 +1,5 @@ name: ffi -version: 2.1.3-wip +version: 2.1.3 description: Utilities for working with Foreign Function Interface (FFI) code. repository: https://github.com/dart-lang/native/tree/main/pkgs/ffi @@ -9,7 +9,7 @@ topics: - codegen environment: - sdk: '>=3.3.0-279.1.beta <4.0.0' + sdk: '>=3.3.0 <4.0.0' dev_dependencies: dart_flutter_team_lints: ^2.0.0