Skip to content

Commit d7d5dc7

Browse files
mit-mitdcharkes
andauthored
[ffi] Merge null safety branch to master (dart-archive/ffi#62)
* enable using package in NNBD * Update to null safety in 2.12 (dart-archive/ffi#60) Co-authored-by: Daco Harkes <[email protected]>
1 parent f458f37 commit d7d5dc7

File tree

8 files changed

+42
-32
lines changed

8 files changed

+42
-32
lines changed

pkgs/ffi/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 0.2.0-nullsafety.0
4+
5+
Pre-release (non-stable) release supporting null safety.
6+
Requires Dart 2.12.0 or greater.
7+
38
## 0.1.3
49

510
Stable release incorporating all the previous dev release changes.

pkgs/ffi/analysis_options.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
include: package:pedantic/analysis_options.yaml
2+
3+
linter:
4+
rules:
5+
omit_local_variable_types: false

pkgs/ffi/example/main.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import 'dart:ffi';
22

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

5-
main() {
5+
void main() {
66
// Allocate and free some native memory with malloc and free.
77
final pointer = allocate<Uint8>();
88
pointer.value = 3;
99
print(pointer.value);
1010
free(pointer);
1111

1212
// Use the Utf8 helper to encode null-terminated Utf8 strings in native memory.
13-
final String myString = "😎👿💬";
13+
final String myString = '😎👿💬';
1414
final Pointer<Utf8> charPointer = Utf8.toUtf8(myString);
15-
print("First byte is: ${charPointer.cast<Uint8>().value}");
15+
print('First byte is: ${charPointer.cast<Uint8>().value}');
1616
print(Utf8.fromUtf8(charPointer));
1717
free(charPointer);
1818
}

pkgs/ffi/lib/src/allocation.dart

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,34 @@ import 'dart:io';
77

88
// Note that kernel32.dll is the correct name in both 32-bit and 64-bit.
99
final DynamicLibrary stdlib = Platform.isWindows
10-
? DynamicLibrary.open("kernel32.dll")
10+
? DynamicLibrary.open('kernel32.dll')
1111
: DynamicLibrary.process();
1212

1313
typedef PosixMallocNative = Pointer Function(IntPtr);
1414
typedef PosixMalloc = Pointer Function(int);
1515
final PosixMalloc posixMalloc =
16-
stdlib.lookupFunction<PosixMallocNative, PosixMalloc>("malloc");
16+
stdlib.lookupFunction<PosixMallocNative, PosixMalloc>('malloc');
1717

1818
typedef PosixFreeNative = Void Function(Pointer);
1919
typedef PosixFree = void Function(Pointer);
2020
final PosixFree posixFree =
21-
stdlib.lookupFunction<PosixFreeNative, PosixFree>("free");
21+
stdlib.lookupFunction<PosixFreeNative, PosixFree>('free');
2222

2323
typedef WinGetProcessHeapFn = Pointer Function();
2424
final WinGetProcessHeapFn winGetProcessHeap = stdlib
25-
.lookupFunction<WinGetProcessHeapFn, WinGetProcessHeapFn>("GetProcessHeap");
25+
.lookupFunction<WinGetProcessHeapFn, WinGetProcessHeapFn>('GetProcessHeap');
2626
final Pointer processHeap = winGetProcessHeap();
2727

2828
typedef WinHeapAllocNative = Pointer Function(Pointer, Uint32, IntPtr);
2929
typedef WinHeapAlloc = Pointer Function(Pointer, int, int);
3030
final WinHeapAlloc winHeapAlloc =
31-
stdlib.lookupFunction<WinHeapAllocNative, WinHeapAlloc>("HeapAlloc");
31+
stdlib.lookupFunction<WinHeapAllocNative, WinHeapAlloc>('HeapAlloc');
3232

3333
typedef WinHeapFreeNative = Int32 Function(
3434
Pointer heap, Uint32 flags, Pointer memory);
3535
typedef WinHeapFree = int Function(Pointer heap, int flags, Pointer memory);
3636
final WinHeapFree winHeapFree =
37-
stdlib.lookupFunction<WinHeapFreeNative, WinHeapFree>("HeapFree");
37+
stdlib.lookupFunction<WinHeapFreeNative, WinHeapFree>('HeapFree');
3838

3939
/// Allocates memory on the native heap.
4040
///
@@ -52,7 +52,7 @@ Pointer<T> allocate<T extends NativeType>({int count = 1}) {
5252
result = posixMalloc(totalSize).cast();
5353
}
5454
if (result.address == 0) {
55-
throw ArgumentError("Could not allocate $totalSize bytes.");
55+
throw ArgumentError('Could not allocate $totalSize bytes.');
5656
}
5757
return result;
5858
}
@@ -70,7 +70,7 @@ Pointer<T> allocate<T extends NativeType>({int count = 1}) {
7070
void free(Pointer pointer) {
7171
if (Platform.isWindows) {
7272
if (winHeapFree(processHeap, /*flags=*/ 0, pointer) == 0) {
73-
throw ArgumentError("Could not free $pointer.");
73+
throw ArgumentError('Could not free $pointer.');
7474
}
7575
} else {
7676
posixFree(pointer);

pkgs/ffi/lib/src/utf8.dart

+1
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,6 @@ class Utf8 extends Struct {
5959
return result.cast();
6060
}
6161

62+
@override
6263
String toString() => fromUtf8(addressOf);
6364
}

pkgs/ffi/pubspec.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
name: ffi
2-
version: 0.1.3+1
2+
version: 0.2.0-nullsafety.0
33
homepage: https://github.com/dart-lang/ffi
44
description: Utilities for working with Foreign Function Interface (FFI) code.
55

66
environment:
7-
sdk: '>=2.6.0 <3.0.0'
7+
sdk: '>=2.12.0-0 <3.0.0'
88

99
# dependencies:
1010

1111
dev_dependencies:
12-
pedantic: ^1.0.0
13-
test: ^1.6.8
12+
pedantic: ^1.10.0-nullsafety
13+
test: ^1.16.0-nullsafety

pkgs/ffi/test/utf16_test.dart

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
import 'dart:ffi';
66
import 'dart:typed_data';
77

8-
import 'package:test/test.dart';
98
import 'package:ffi/ffi.dart';
9+
import 'package:test/test.dart';
1010

11-
main() {
12-
test("toUtf16 ASCII", () {
13-
final String start = "Hello World!\n";
11+
void main() {
12+
test('toUtf16 ASCII', () {
13+
final String start = 'Hello World!\n';
1414
final Pointer<Uint16> converted = Utf16.toUtf16(start).cast();
1515
final Uint16List end = converted.asTypedList(start.codeUnits.length + 1);
1616
final matcher = equals(start.codeUnits.toList()..add(0));
1717
expect(end, matcher);
1818
free(converted);
1919
});
2020

21-
test("toUtf16 emoji", () {
22-
final String start = "😎";
21+
test('toUtf16 emoji', () {
22+
final String start = '😎';
2323
final Pointer<Utf16> converted = Utf16.toUtf16(start).cast();
2424
final int length = start.codeUnits.length;
2525
final Uint16List end = converted.cast<Uint16>().asTypedList(length + 1);

pkgs/ffi/test/utf8_test.dart

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import 'dart:ffi';
66
import 'dart:typed_data';
77

8-
import 'package:test/test.dart';
98
import 'package:ffi/ffi.dart';
9+
import 'package:test/test.dart';
1010

1111
Pointer<Uint8> _bytesFromList(List<int> ints) {
1212
final Pointer<Uint8> ptr = allocate(count: ints.length);
@@ -15,9 +15,9 @@ Pointer<Uint8> _bytesFromList(List<int> ints) {
1515
return ptr;
1616
}
1717

18-
main() {
19-
test("toUtf8 ASCII", () {
20-
final String start = "Hello World!\n";
18+
void main() {
19+
test('toUtf8 ASCII', () {
20+
final String start = 'Hello World!\n';
2121
final Pointer<Uint8> converted = Utf8.toUtf8(start).cast();
2222
final Uint8List end = converted.asTypedList(start.length + 1);
2323
final matcher =
@@ -26,15 +26,15 @@ main() {
2626
free(converted);
2727
});
2828

29-
test("fromUtf8 ASCII", () {
29+
test('fromUtf8 ASCII', () {
3030
final Pointer<Utf8> utf8 = _bytesFromList(
3131
[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 10, 0]).cast();
3232
final String end = Utf8.fromUtf8(utf8);
33-
expect(end, "Hello World!\n");
33+
expect(end, 'Hello World!\n');
3434
});
3535

36-
test("toUtf8 emoji", () {
37-
final String start = "😎👿💬";
36+
test('toUtf8 emoji', () {
37+
final String start = '😎👿💬';
3838
final Pointer<Utf8> converted = Utf8.toUtf8(start).cast();
3939
final int length = Utf8.strlen(converted);
4040
final Uint8List end = converted.cast<Uint8>().asTypedList(length + 1);
@@ -44,14 +44,14 @@ main() {
4444
free(converted);
4545
});
4646

47-
test("formUtf8 emoji", () {
47+
test('formUtf8 emoji', () {
4848
final Pointer<Utf8> utf8 = _bytesFromList(
4949
[240, 159, 152, 142, 240, 159, 145, 191, 240, 159, 146, 172, 0]).cast();
5050
final String end = Utf8.fromUtf8(utf8);
51-
expect(end, "😎👿💬");
51+
expect(end, '😎👿💬');
5252
});
5353

54-
test("fromUtf8 invalid", () {
54+
test('fromUtf8 invalid', () {
5555
final Pointer<Utf8> utf8 = _bytesFromList([0x80, 0x00]).cast();
5656
expect(() => Utf8.fromUtf8(utf8), throwsA(isFormatException));
5757
});

0 commit comments

Comments
 (0)