From cdaccc583272999acbaac6cc28f6ac6c1f6449d7 Mon Sep 17 00:00:00 2001 From: Craig Labenz Date: Wed, 14 Feb 2024 09:45:50 -0800 Subject: [PATCH] changes from code review --- .../mediapipe-core/lib/src/ffi_utils.dart | 47 +++++++++++-------- .../mediapipe-core/test/ffi_utils_test.dart | 18 +++---- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/packages/mediapipe-core/lib/src/ffi_utils.dart b/packages/mediapipe-core/lib/src/ffi_utils.dart index f14c797..ac3ead4 100644 --- a/packages/mediapipe-core/lib/src/ffi_utils.dart +++ b/packages/mediapipe-core/lib/src/ffi_utils.dart @@ -31,7 +31,7 @@ extension NativeStrings on String { /// /// See also: /// * [toCharsPointerPointer] - Pointer toNative() => toNativeUtf8().cast(); + Pointer copyToNative() => toNativeUtf8().cast(); } /// Helpers to convert between a `List` and a `Pointer>`. @@ -40,10 +40,10 @@ extension NativeListOfStrings on List { /// /// See also: /// * [Pointer.toNative] for a non-list equivalent. - Pointer> toNative() { + Pointer> copyToNative() { final ptrArray = calloc>(length); for (var i = 0; i < length; i++) { - ptrArray[i] = this[i].toNative(); + ptrArray[i] = this[i].copyToNative(); } return ptrArray; } @@ -51,25 +51,28 @@ extension NativeListOfStrings on List { /// Helpers to convert a [Pointer] into a [Uint8List]. extension DartAwareChars on Pointer { - /// Creates a Dart view on a pointer to utf8 chars in native memory, cast as - /// unsigned, 8-bit integers. This method does not copy the data out of native - /// memory. + /// Creates a Dart view on native memory, cast as unsigned, 8-bit integers. + /// This method does not copy the data out of native memory. /// - /// If this is called on a `nullptr`, the method returns `null`. - Uint8List? toUint8List(int length) { - if (isNullPointer) return null; + /// Throws an exception if the method is called on a `nullptr`. + Uint8List toUint8List(int length) { + if (isNullPointer) { + throw Exception('Unexpectedly called `toUint8List` on nullptr'); + } RangeError.checkNotNegative(length, 'length'); return cast().asTypedList(length); } /// Converts the C memory representation of a string into a Dart [String]. /// - /// If this is called on a `nullptr`, the method returns `null`. + /// Throws an exception if the method is called on a `nullptr`. /// /// See also: /// * [toDartStrings] - String? toDartString({int? length}) { - if (isNullPointer) return null; + String toDartString({int? length}) { + if (isNullPointer) { + throw Exception('Unexpectedly called `toDartString` on nullptr'); + } return cast().toDartString(length: length); } } @@ -80,12 +83,14 @@ extension DartAwarePointerChars on Pointer> { /// cast as a `List`. This method does not copy the data out of native /// memory. /// - /// If this is called on a `nullptr`, the method returns `null`. + /// Throws an exception if the method is called on a `nullptr`. /// /// See also: /// * [toDartString], for a non-list equivalent. - List? toDartStrings(int length) { - if (isNullPointer) return null; + List toDartStrings(int length) { + if (isNullPointer) { + throw Exception('Unexpectedly called `toDartStrings` on nullptr'); + } final dartStrings = []; int counter = 0; while (counter < length) { @@ -101,9 +106,11 @@ extension DartAwareFloats on Pointer { /// Creates a Dart view on a pointer to floats in native memory, cast as a /// Float32List. This method does not copy the data out of native memory. /// - /// If this is called on a `nullptr`, the method returns `null`. - Float32List? toFloat32List(int length) { - if (isNullPointer) return null; + /// Throws an exception if the method is called on a `nullptr`. + Float32List toFloat32List(int length) { + if (isNullPointer) { + throw Exception('Unexpectedly called `toFloat32List` on nullptr'); + } RangeError.checkNotNegative(length, 'length'); return cast().asTypedList(length); } @@ -115,7 +122,7 @@ extension NativeFloats on Float32List { /// Copies a [Float32List] into native memory as a `float*`. /// /// Returns an [allocator]-allocated pointer to the result. - Pointer toNative({Allocator allocator = malloc}) { + Pointer copyToNative({Allocator allocator = malloc}) { final Pointer result = allocator(length) ..asTypedList(length).setAll(0, this); return result.cast(); @@ -128,7 +135,7 @@ extension NativeInts on Uint8List { /// Copies a [Uint8List] into native memory as a `char*`. /// /// Returns an [allocator]-allocated pointer to the result. - Pointer toNative({Allocator allocator = malloc}) { + Pointer copyToNative({Allocator allocator = malloc}) { final Pointer result = allocator(length) ..asTypedList(length).setAll(0, this); return result.cast(); diff --git a/packages/mediapipe-core/test/ffi_utils_test.dart b/packages/mediapipe-core/test/ffi_utils_test.dart index fe08867..9b5691d 100644 --- a/packages/mediapipe-core/test/ffi_utils_test.dart +++ b/packages/mediapipe-core/test/ffi_utils_test.dart @@ -55,41 +55,41 @@ void main() { group('Strings should', () { test('be convertable to char*', () { - final Pointer abc = 'abc'.toNative(); + final Pointer abc = 'abc'.copyToNative(); calloc.free(abc); }); test('be round-trippable', () { - expect('abc'.toNative().toDartString(), 'abc'); + expect('abc'.copyToNative().toDartString(), 'abc'); }); }); group('Lists of Strings should', () { test('be convertable to char**', () { - final Pointer> strings = ['abc'].toNative(); + final Pointer> strings = ['abc'].copyToNative(); calloc.free(strings); }); test('be round-trippable', () { - expect(['abc'].toNative().toDartStrings(1), ['abc']); + expect(['abc'].copyToNative().toDartStrings(1), ['abc']); }); }); group('Uint8List should', () { test('be convertable to Pointer', () { - final Pointer ptr = Uint8List.fromList([1, 2, 3]).toNative(); + final Pointer ptr = Uint8List.fromList([1, 2, 3]).copyToNative(); malloc.free(ptr); }); test('be round-trippable', () { expect( - Uint8List.fromList([1, 2, 3]).toNative().toUint8List(3).toList(), + Uint8List.fromList([1, 2, 3]).copyToNative().toUint8List(3).toList(), [1, 2, 3], ); }); test('copy memory with toList', () { - final ptr = Uint8List.fromList([1, 2, 3]).toNative(); + final ptr = Uint8List.fromList([1, 2, 3]).copyToNative(); final List ints = ptr.toUint8List(3).toList(); calloc.free(ptr); expect(ints[0], 1); @@ -101,13 +101,13 @@ void main() { group('Float32List should', () { test('be convertable to Pointer', () { final Pointer ptr = - Float32List.fromList([1.0, 2.0, 3.1]).toNative(); + Float32List.fromList([1.0, 2.0, 3.1]).copyToNative(); malloc.free(ptr); }); test('be round-trippable', () { final doubles = Float32List.fromList([1.1, 2.2, 3.3]) - .toNative() + .copyToNative() .toFloat32List(3) .toList(); expect(doubles[0], closeTo(1.1, 0.0001));