Skip to content

Commit

Permalink
Switch to package:dart_flutter_team_lints (#1272)
Browse files Browse the repository at this point in the history
---

<details>
  <summary>Contribution guidelines:</summary><br>

- See our [contributor guide](https://github.com/dart-lang/.github/blob/main/CONTRIBUTING.md) for general expectations for PRs.
- Larger or significant changes should be discussed in an issue before creating a PR.
- Contributions to our repos should follow the [Dart style guide](https://dart.dev/guides/language/effective-dart) and use `dart format`.
- Most changes should add an entry to the changelog and may need to [rev the pubspec package version](https://github.com/dart-lang/sdk/blob/main/docs/External-Package-Maintenance.md#making-a-change).
- Changes to packages require [corresponding tests](https://github.com/dart-lang/.github/blob/main/CONTRIBUTING.md#Testing).

Note that many Dart repos have a weekly cadence for reviewing PRs - please allow for some latency before initial review feedback.
</details>
  • Loading branch information
mosuem committed Jul 9, 2024
1 parent b054170 commit 6013cb7
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 107 deletions.
4 changes: 4 additions & 0 deletions pkgs/ffi/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.1.3-wip

- Use `package:dart_flutter_team_lints`.

## 2.1.2

- Update repository to point to dart-lang/native.
Expand Down
6 changes: 5 additions & 1 deletion pkgs/ffi/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
include: package:lints/recommended.yaml
include: package:dart_flutter_team_lints/analysis_options.yaml

analyzer:
language:
strict-casts: true
strict-inference: true

linter:
rules:
- prefer_final_locals
11 changes: 8 additions & 3 deletions pkgs/ffi/example/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2024, 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 'dart:ffi';

import 'package:ffi/ffi.dart';
Expand All @@ -9,9 +13,10 @@ void main() {
print(pointer.value);
calloc.free(pointer);

// Use the Utf8 helper to encode zero-terminated UTF-8 strings in native memory.
final String myString = '😎👿💬';
final Pointer<Utf8> charPointer = myString.toNativeUtf8();
// Use the Utf8 helper to encode zero-terminated UTF-8 strings in native
// memory.
final myString = '😎👿💬';
final charPointer = myString.toNativeUtf8();
print('First byte is: ${charPointer.cast<Uint8>().value}');
print(charPointer.toDartString());
calloc.free(charPointer);
Expand Down
2 changes: 1 addition & 1 deletion pkgs/ffi/lib/ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

export 'src/allocation.dart' show calloc, malloc;
export 'src/arena.dart';
export 'src/utf8.dart';
export 'src/utf16.dart';
export 'src/utf8.dart';
4 changes: 2 additions & 2 deletions pkgs/ffi/lib/src/allocation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ final class MallocAllocator implements Allocator {

/// Returns a pointer to a native free function.
///
/// This function can be used to release memory allocated by [allocated]
/// This function can be used to release memory allocated by [allocate]
/// from the native side. It can also be used as a finalization callback
/// passed to `NativeFinalizer` constructor or `Pointer.atTypedList`
/// method.
Expand Down Expand Up @@ -189,7 +189,7 @@ final class CallocAllocator implements Allocator {

/// Returns a pointer to a native free function.
///
/// This function can be used to release memory allocated by [allocated]
/// This function can be used to release memory allocated by [allocate]
/// from the native side. It can also be used as a finalization callback
/// passed to `NativeFinalizer` constructor or `Pointer.atTypedList`
/// method.
Expand Down
14 changes: 6 additions & 8 deletions pkgs/ffi/lib/src/arena.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import 'dart:async';
import 'dart:ffi';

import 'package:ffi/ffi.dart';
import '../ffi.dart';

/// An [Allocator] which frees all allocations at the same time.
///
Expand Down Expand Up @@ -119,12 +119,12 @@ class Arena implements Allocator {
R using<R>(R Function(Arena) computation,
[Allocator wrappedAllocator = calloc]) {
final arena = Arena(wrappedAllocator);
bool isAsync = false;
var isAsync = false;
try {
final result = computation(arena);
if (result is Future) {
isAsync = true;
return (result.whenComplete(arena.releaseAll) as R);
return result.whenComplete(arena.releaseAll) as R;
}
return result;
} finally {
Expand All @@ -143,16 +143,14 @@ R using<R>(R Function(Arena) computation,
R withZoneArena<R>(R Function() computation,
[Allocator wrappedAllocator = calloc]) {
final arena = Arena(wrappedAllocator);
var arenaHolder = [arena];
bool isAsync = false;
final arenaHolder = [arena];
var isAsync = false;
try {
return runZoned(() {
final result = computation();
if (result is Future) {
isAsync = true;
return result.whenComplete(() {
arena.releaseAll();
}) as R;
return result.whenComplete(arena.releaseAll) as R;
}
return result;
}, zoneValues: {#_arena: arenaHolder});
Expand Down
7 changes: 3 additions & 4 deletions pkgs/ffi/lib/src/utf16.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:ffi';
import 'dart:typed_data';

import 'package:ffi/ffi.dart';
import '../ffi.dart';

/// The contents of a native zero-terminated array of UTF-16 code units.
///
Expand Down Expand Up @@ -92,8 +91,8 @@ extension StringUtf16Pointer on String {
/// Returns an [allocator]-allocated pointer to the result.
Pointer<Utf16> toNativeUtf16({Allocator allocator = malloc}) {
final units = codeUnits;
final Pointer<Uint16> result = allocator<Uint16>(units.length + 1);
final Uint16List nativeString = result.asTypedList(units.length + 1);
final result = allocator<Uint16>(units.length + 1);
final nativeString = result.asTypedList(units.length + 1);
nativeString.setRange(0, units.length, units);
nativeString[units.length] = 0;
return result.cast();
Expand Down
7 changes: 3 additions & 4 deletions pkgs/ffi/lib/src/utf8.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

import 'dart:convert';
import 'dart:ffi';
import 'dart:typed_data';

import 'package:ffi/ffi.dart';
import '../ffi.dart';

/// The contents of a native zero-terminated array of UTF-8 code units.
///
Expand Down Expand Up @@ -80,8 +79,8 @@ extension StringUtf8Pointer on String {
/// Returns an [allocator]-allocated pointer to the result.
Pointer<Utf8> toNativeUtf8({Allocator allocator = malloc}) {
final units = utf8.encode(this);
final Pointer<Uint8> result = allocator<Uint8>(units.length + 1);
final Uint8List nativeString = result.asTypedList(units.length + 1);
final result = allocator<Uint8>(units.length + 1);
final nativeString = result.asTypedList(units.length + 1);
nativeString.setAll(0, units);
nativeString[units.length] = 0;
return result.cast();
Expand Down
4 changes: 2 additions & 2 deletions pkgs/ffi/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: ffi
version: 2.1.2
version: 2.1.3-wip
description: Utilities for working with Foreign Function Interface (FFI) code.
repository: https://github.com/dart-lang/native/tree/main/pkgs/ffi

Expand All @@ -12,5 +12,5 @@ environment:
sdk: '>=3.3.0-279.1.beta <4.0.0'

dev_dependencies:
dart_flutter_team_lints: ^2.0.0
test: ^1.21.2
lints: ^2.0.0
6 changes: 3 additions & 3 deletions pkgs/ffi/test/allocation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ void main() async {
for (var i = 0; i < testRuns; i++) {
final allocBytes = Random().nextInt(1000);
final mem = calloc<Uint8>(allocBytes);
expect(mem.asTypedList(allocBytes).where(((element) => element != 0)),
expect(mem.asTypedList(allocBytes).where((element) => element != 0),
isEmpty);
calloc.free(mem);
}
});

test('testPointerAllocateTooLarge', () {
// Try to allocate something that doesn't fit in 64 bit address space.
int maxInt = 9223372036854775807; // 2^63 - 1
final maxInt = 9223372036854775807; // 2^63 - 1
expect(() => calloc<Uint8>(maxInt), throwsA(isA<ArgumentError>()));

// Try to allocate almost the full 64 bit address space.
int maxInt1_8 = 1152921504606846975; // 2^60 -1
final maxInt1_8 = 1152921504606846975; // 2^60 -1
expect(() => calloc<Uint8>(maxInt1_8), throwsA(isA<ArgumentError>()));
});

Expand Down
24 changes: 12 additions & 12 deletions pkgs/ffi/test/arena_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:test/test.dart';

void main() async {
test('sync', () async {
List<int> freed = [];
final freed = <int>[];
void freeInt(int i) {
freed.add(i);
}
Expand All @@ -25,12 +25,12 @@ void main() async {
test('async', () async {
/// Calling [using] waits with releasing its resources until after
/// [Future]s complete.
List<int> freed = [];
final freed = <int>[];
void freeInt(int i) {
freed.add(i);
}

Future<int> myFutureInt = using((Arena arena) {
final Future<int> myFutureInt = using((Arena arena) {
return Future.microtask(() {
arena.using(1234, freeInt);
return 1;
Expand All @@ -45,7 +45,7 @@ void main() async {
test('throw', () {
/// [using] waits with releasing its resources until after [Future]s
/// complete.
List<int> freed = [];
final freed = <int>[];
void freeInt(int i) {
freed.add(i);
}
Expand Down Expand Up @@ -80,7 +80,7 @@ void main() async {

test('allocate throw', () {
// Resources are freed also when abnormal control flow occurs.
bool didThrow = false;
var didThrow = false;
final countingAllocator = CountingAllocator();
try {
using((Arena arena) {
Expand All @@ -105,7 +105,7 @@ void main() async {
});

test('zone', () async {
List<int> freed = [];
final freed = <int>[];
void freeInt(int i) {
freed.add(i);
}
Expand All @@ -121,12 +121,12 @@ void main() async {
test('zone async', () async {
/// [using] waits with releasing its resources until after [Future]s
/// complete.
List<int> freed = [];
final freed = <int>[];
void freeInt(int i) {
freed.add(i);
}

Future<int> myFutureInt = withZoneArena(() {
final Future<int> myFutureInt = withZoneArena(() {
return Future.microtask(() {
zoneArena.using(1234, freeInt);
return 1;
Expand All @@ -142,13 +142,13 @@ void main() async {
test('zone throw', () {
/// [using] waits with releasing its resources until after [Future]s
/// complete.
List<int> freed = [];
final freed = <int>[];
void freeInt(int i) {
freed.add(i);
}

// Resources are freed also when abnormal control flow occurs.
bool didThrow = false;
var didThrow = false;
try {
withZoneArena(() {
zoneArena.using(1234, freeInt);
Expand All @@ -164,8 +164,8 @@ void main() async {
});

test('zone future error', () async {
bool caughtError = false;
bool uncaughtError = false;
var caughtError = false;
var uncaughtError = false;

Future<int> asyncFunction() async {
throw Exception('Exception 4');
Expand Down
29 changes: 9 additions & 20 deletions pkgs/ffi/test/utf16_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:ffi';
import 'dart:typed_data';

import 'package:ffi/ffi.dart';
import 'package:test/test.dart';

void main() {
test('toUtf16 ASCII', () {
final String start = 'Hello World!\n';
final Pointer<Uint16> converted = start.toNativeUtf16().cast();
final Uint16List end = converted.asTypedList(start.codeUnits.length + 1);
final start = 'Hello World!\n';
final converted = start.toNativeUtf16().cast<Uint16>();
final end = converted.asTypedList(start.codeUnits.length + 1);
final matcher = equals(start.codeUnits.toList()..add(0));
expect(end, matcher);
calloc.free(converted);
});

test('toUtf16 emoji', () {
final String start = '😎';
final Pointer<Utf16> converted = start.toNativeUtf16().cast();
final int length = start.codeUnits.length;
final Uint16List end = converted.cast<Uint16>().asTypedList(length + 1);
final start = '😎';
final converted = start.toNativeUtf16().cast();
final length = start.codeUnits.length;
final end = converted.cast<Uint16>().asTypedList(length + 1);
final matcher = equals(start.codeUnits.toList()..add(0));
expect(end, matcher);
calloc.free(converted);
Expand Down Expand Up @@ -68,21 +67,11 @@ void main() {

test('nullptr.toDartString()', () {
final Pointer<Utf16> utf16 = nullptr;
try {
utf16.toDartString();
} on UnsupportedError {
return;
}
fail('Expected an error.');
expect(utf16.toDartString, throwsUnsupportedError);
});

test('nullptr.length', () {
final Pointer<Utf16> utf16 = nullptr;
try {
utf16.length;
} on UnsupportedError {
return;
}
fail('Expected an error.');
expect(() => utf16.length, throwsUnsupportedError);
});
}
Loading

0 comments on commit 6013cb7

Please sign in to comment.