Skip to content

Commit 7fbbe4f

Browse files
dcharkescommit-bot@chromium.org
authored andcommitted
[vm/ffi] Migrate off .addressOf in tests
Removes invocations of `.addressOf` in tests. Removes the constructor from the Coordinate testing class which has become useless with no access to the underlying pointer. Removes the bare variant of Coordinate because it is identical to the non-bare variant. Issue: #40667 Change-Id: Ie26fa3a9cac38e794e93f5fadfec2562a814c1ae Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/181403 Reviewed-by: Aske Simon Christensen <[email protected]> Commit-Queue: Daco Harkes <[email protected]>
1 parent 24a7e43 commit 7fbbe4f

14 files changed

+123
-217
lines changed

tests/ffi/coordinate.dart

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
library FfiTest;
66

77
import 'dart:ffi';
8-
import "package:ffi/ffi.dart";
98

109
/// Sample struct for dart:ffi library.
1110
class Coordinate extends Struct {
@@ -16,12 +15,4 @@ class Coordinate extends Struct {
1615
external double y;
1716

1817
external Pointer<Coordinate> next;
19-
20-
factory Coordinate.allocate(
21-
Allocator allocator, double x, double y, Pointer<Coordinate> next) {
22-
return allocator<Coordinate>().ref
23-
..x = x
24-
..y = y
25-
..next = next;
26-
}
2718
}

tests/ffi/coordinate_bare.dart

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
4+
//
5+
// Dart test program for testing getters/setters in structs rather than fields.
46

57
library FfiTest;
68

79
import 'dart:ffi';
8-
import "package:ffi/ffi.dart";
910

1011
/// Sample struct for dart:ffi library.
1112
class Coordinate extends Struct {
@@ -19,12 +20,4 @@ class Coordinate extends Struct {
1920

2021
external Pointer<Coordinate> get next;
2122
external set next(Pointer<Coordinate> v);
22-
23-
factory Coordinate.allocate(
24-
Allocator allocator, double x, double y, Pointer<Coordinate> next) {
25-
return allocator<Coordinate>().ref
26-
..x = x
27-
..y = y
28-
..next = next;
29-
}
3023
}

tests/ffi/function_callbacks_structs_by_value_test.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ void main() {
2424
}
2525

2626
void recursiveTest(int recursionCounter) {
27-
final struct = calloc<Struct20BytesHomogeneousInt32>().ref;
27+
final pointer = calloc<Struct20BytesHomogeneousInt32>();
28+
final struct = pointer.ref;
2829
struct.a0 = 1;
2930
struct.a1 = 2;
3031
struct.a2 = 3;
@@ -36,7 +37,7 @@ void recursiveTest(int recursionCounter) {
3637
Expect.equals(struct.a2, result.a2);
3738
Expect.equals(struct.a3, result.a3);
3839
Expect.equals(struct.a4, result.a4);
39-
calloc.free(struct.addressOf);
40+
calloc.free(pointer);
4041
}
4142

4243
Struct20BytesHomogeneousInt32 dartPassStructRecursive(
@@ -94,7 +95,8 @@ void testCopyLogic() {
9495
_invokeReceiveStructByValue(_receiveStructByValuePointer);
9596
Expect.isTrue(typedDataBackedStructSet);
9697

97-
final pointerBackedStruct = calloc<Struct8BytesNestedInt>().ref;
98+
final pointer = calloc<Struct8BytesNestedInt>();
99+
final pointerBackedStruct = pointer.ref;
98100

99101
void reset() {
100102
pointerBackedStruct.a0.a0 = 1;
@@ -131,5 +133,5 @@ void testCopyLogic() {
131133
Expect.equals(5, typedDataBackedStruct.a1.a0);
132134
Expect.equals(6, typedDataBackedStruct.a1.a1);
133135

134-
calloc.free(pointerBackedStruct.addressOf);
136+
calloc.free(pointer);
135137
}

tests/ffi/function_structs_test.dart

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99

1010
import 'dart:ffi';
1111

12-
import 'dylib_utils.dart';
13-
1412
import "package:expect/expect.dart";
1513
import "package:ffi/ffi.dart";
1614

1715
import 'calloc.dart';
1816
import 'coordinate.dart';
17+
import 'dylib_utils.dart';
1918
import 'very_large_struct.dart';
2019

2120
typedef NativeCoordinateOp = Pointer<Coordinate> Function(Pointer<Coordinate>);
@@ -34,10 +33,13 @@ void testFunctionWithStruct() {
3433
ffiTestFunctions.lookup("TransposeCoordinate");
3534
NativeCoordinateOp f1 = p1.asFunction();
3635

37-
Pointer<Coordinate> c1 =
38-
Coordinate.allocate(calloc, 10.0, 20.0, nullptr).addressOf;
39-
Pointer<Coordinate> c2 =
40-
Coordinate.allocate(calloc, 42.0, 84.0, c1).addressOf;
36+
final c1 = calloc<Coordinate>()
37+
..ref.x = 10.0
38+
..ref.y = 20.0;
39+
final c2 = calloc<Coordinate>()
40+
..ref.x = 42.0
41+
..ref.y = 84.0
42+
..ref.next = c1;
4143
c1.ref.next = c2;
4244

4345
Coordinate result = f1(c1).ref;
@@ -58,24 +60,25 @@ void testFunctionWithStructArray() {
5860
ffiTestFunctions.lookup("CoordinateElemAt1");
5961
NativeCoordinateOp f1 = p1.asFunction();
6062

61-
Coordinate c1 = calloc<Coordinate>(3).ref;
62-
Coordinate c2 = c1.addressOf[1];
63-
Coordinate c3 = c1.addressOf[2];
63+
final coordinateArray = calloc<Coordinate>(3);
64+
Coordinate c1 = coordinateArray[0];
65+
Coordinate c2 = coordinateArray[1];
66+
Coordinate c3 = coordinateArray[2];
6467
c1.x = 10.0;
6568
c1.y = 10.0;
66-
c1.next = c3.addressOf;
69+
c1.next = coordinateArray.elementAt(2);
6770
c2.x = 20.0;
6871
c2.y = 20.0;
69-
c2.next = c1.addressOf;
72+
c2.next = coordinateArray.elementAt(0);
7073
c3.x = 30.0;
7174
c3.y = 30.0;
72-
c3.next = c2.addressOf;
75+
c3.next = coordinateArray.elementAt(1);
7376

74-
Coordinate result = f1(c1.addressOf).ref;
77+
Coordinate result = f1(coordinateArray.elementAt(0)).ref;
7578
Expect.approxEquals(20.0, result.x);
7679
Expect.approxEquals(20.0, result.y);
7780

78-
calloc.free(c1.addressOf);
81+
calloc.free(coordinateArray);
7982
}
8083

8184
typedef VeryLargeStructSum = int Function(Pointer<VeryLargeStruct>);
@@ -86,8 +89,9 @@ void testFunctionWithVeryLargeStruct() {
8689
ffiTestFunctions.lookup("SumVeryLargeStruct");
8790
VeryLargeStructSum f = p1.asFunction();
8891

89-
VeryLargeStruct vls1 = calloc<VeryLargeStruct>(2).ref;
90-
VeryLargeStruct vls2 = vls1.addressOf[1];
92+
final vlsArray = calloc<VeryLargeStruct>(2);
93+
VeryLargeStruct vls1 = vlsArray[0];
94+
VeryLargeStruct vls2 = vlsArray[1];
9195
List<VeryLargeStruct> structs = [vls1, vls2];
9296
for (VeryLargeStruct struct in structs) {
9397
struct.a = 1;
@@ -103,19 +107,19 @@ void testFunctionWithVeryLargeStruct() {
103107
struct.k = 1024;
104108
struct.smallLastField = 1;
105109
}
106-
vls1.parent = vls2.addressOf;
110+
vls1.parent = vlsArray.elementAt(1);
107111
vls1.numChildren = 2;
108-
vls1.children = vls1.addressOf;
109-
vls2.parent = vls2.addressOf;
112+
vls1.children = vlsArray.elementAt(0);
113+
vls2.parent = vlsArray.elementAt(1);
110114
vls2.parent = nullptr;
111115
vls2.numChildren = 0;
112116
vls2.children = nullptr;
113117

114-
int result = f(vls1.addressOf);
118+
int result = f(vlsArray.elementAt(0));
115119
Expect.equals(2051, result);
116120

117-
result = f(vls2.addressOf);
121+
result = f(vlsArray.elementAt(1));
118122
Expect.equals(2048, result);
119123

120-
calloc.free(vls1.addressOf);
124+
calloc.free(vlsArray);
121125
}

tests/ffi/structs_nested_test.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,8 @@ void testRead() {
6161
final p = calloc<Struct8BytesNestedInt>();
6262
print(p);
6363
print(p.ref.runtimeType);
64-
print(p.ref.addressOf);
65-
print(p.ref.addressOf.address);
64+
print(p.address);
6665
print(p.ref.a0.runtimeType);
67-
print(p.ref.a0.addressOf);
6866
print(p.ref.a0.a0);
6967
calloc.free(p);
7068
print("read");

tests/ffi/structs_nnbd_workaround_test.dart

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44
//
5-
// Dart test program for testing dart:ffi struct pointers.
5+
// Dart test program for testing getters/setters in structs rather than fields.
66
//
77
// VMOptions=--deterministic --optimization-counter-threshold=50
88

@@ -26,12 +26,17 @@ void main() {
2626

2727
/// allocates each coordinate separately in c memory
2828
void testStructAllocate() {
29-
Pointer<Coordinate> c1 =
30-
Coordinate.allocate(calloc, 10.0, 10.0, nullptr).addressOf;
31-
Pointer<Coordinate> c2 =
32-
Coordinate.allocate(calloc, 20.0, 20.0, c1).addressOf;
33-
Pointer<Coordinate> c3 =
34-
Coordinate.allocate(calloc, 30.0, 30.0, c2).addressOf;
29+
final c1 = calloc<Coordinate>()
30+
..ref.x = 10.0
31+
..ref.y = 10.0;
32+
final c2 = calloc<Coordinate>()
33+
..ref.x = 20.0
34+
..ref.y = 20.0
35+
..ref.next = c1;
36+
final c3 = calloc<Coordinate>()
37+
..ref.x = 30.0
38+
..ref.y = 30.0
39+
..ref.next = c2;
3540
c1.ref.next = c3;
3641

3742
Coordinate currentCoordinate = c1.ref;
@@ -79,8 +84,9 @@ void testStructFromAddress() {
7984
}
8085

8186
void testStructWithNulls() {
82-
Pointer<Coordinate> coordinate =
83-
Coordinate.allocate(calloc, 10.0, 10.0, nullptr).addressOf;
87+
final coordinate = calloc<Coordinate>()
88+
..ref.x = 10.0
89+
..ref.y = 10.0;
8490
Expect.equals(coordinate.ref.next, nullptr);
8591
coordinate.ref.next = coordinate;
8692
Expect.notEquals(coordinate.ref.next, nullptr);
@@ -90,10 +96,12 @@ void testStructWithNulls() {
9096
}
9197

9298
void testTypeTest() {
93-
Coordinate c = Coordinate.allocate(calloc, 10, 10, nullptr);
99+
final pointer = calloc<Coordinate>();
100+
Coordinate c = pointer.ref;
94101
Expect.isTrue(c is Struct);
102+
// TODO(https://dartbug.com/40667): Remove support for this.
95103
Expect.isTrue(c.addressOf is Pointer<Coordinate>);
96-
calloc.free(c.addressOf);
104+
calloc.free(pointer);
97105
}
98106

99107
void testUtf8() {

tests/ffi/structs_test.dart

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,21 @@
44
//
55
// Dart test program for testing dart:ffi struct pointers.
66
//
7-
// VMOptions=--deterministic --optimization-counter-threshold=50
7+
// VMOptions=--deterministic --optimization-counter-threshold=50 --enable-inlining-annotations
88

99
import 'dart:ffi';
1010

1111
import "package:expect/expect.dart";
1212
import "package:ffi/ffi.dart";
1313

1414
import 'calloc.dart';
15-
import 'coordinate_bare.dart' as bare;
1615
import 'coordinate.dart';
17-
import 'ffi_test_helpers.dart';
1816

1917
void main() {
2018
for (int i = 0; i < 100; i++) {
2119
testStructAllocate();
2220
testStructFromAddress();
2321
testStructWithNulls();
24-
testBareStruct();
2522
testTypeTest();
2623
testUtf8();
2724
testDotDotRef();
@@ -30,12 +27,17 @@ void main() {
3027

3128
/// allocates each coordinate separately in c memory
3229
void testStructAllocate() {
33-
Pointer<Coordinate> c1 =
34-
Coordinate.allocate(calloc, 10.0, 10.0, nullptr).addressOf;
35-
Pointer<Coordinate> c2 =
36-
Coordinate.allocate(calloc, 20.0, 20.0, c1).addressOf;
37-
Pointer<Coordinate> c3 =
38-
Coordinate.allocate(calloc, 30.0, 30.0, c2).addressOf;
30+
final c1 = calloc<Coordinate>()
31+
..ref.x = 10.0
32+
..ref.y = 10.0;
33+
final c2 = calloc<Coordinate>()
34+
..ref.x = 20.0
35+
..ref.y = 20.0
36+
..ref.next = c1;
37+
final c3 = calloc<Coordinate>()
38+
..ref.x = 30.0
39+
..ref.y = 30.0
40+
..ref.next = c2;
3941
c1.ref.next = c3;
4042

4143
Coordinate currentCoordinate = c1.ref;
@@ -83,8 +85,9 @@ void testStructFromAddress() {
8385
}
8486

8587
void testStructWithNulls() {
86-
Pointer<Coordinate> coordinate =
87-
Coordinate.allocate(calloc, 10.0, 10.0, nullptr).addressOf;
88+
final coordinate = calloc<Coordinate>()
89+
..ref.x = 10.0
90+
..ref.y = 10.0;
8891
Expect.equals(coordinate.ref.next, nullptr);
8992
coordinate.ref.next = coordinate;
9093
Expect.notEquals(coordinate.ref.next, nullptr);
@@ -93,41 +96,13 @@ void testStructWithNulls() {
9396
calloc.free(coordinate);
9497
}
9598

96-
void testBareStruct() {
97-
int structSize = sizeOf<Double>() * 2 + sizeOf<IntPtr>();
98-
bare.Coordinate c1 =
99-
calloc<Uint8>(structSize * 3).cast<bare.Coordinate>().ref;
100-
bare.Coordinate c2 =
101-
c1.addressOf.offsetBy(structSize).cast<bare.Coordinate>().ref;
102-
bare.Coordinate c3 =
103-
c1.addressOf.offsetBy(structSize * 2).cast<bare.Coordinate>().ref;
104-
c1.x = 10.0;
105-
c1.y = 10.0;
106-
c1.next = c3.addressOf;
107-
c2.x = 20.0;
108-
c2.y = 20.0;
109-
c2.next = c1.addressOf;
110-
c3.x = 30.0;
111-
c3.y = 30.0;
112-
c3.next = c2.addressOf;
113-
114-
bare.Coordinate currentCoordinate = c1;
115-
Expect.equals(10.0, currentCoordinate.x);
116-
currentCoordinate = currentCoordinate.next.ref;
117-
Expect.equals(30.0, currentCoordinate.x);
118-
currentCoordinate = currentCoordinate.next.ref;
119-
Expect.equals(20.0, currentCoordinate.x);
120-
currentCoordinate = currentCoordinate.next.ref;
121-
Expect.equals(10.0, currentCoordinate.x);
122-
123-
calloc.free(c1.addressOf);
124-
}
125-
12699
void testTypeTest() {
127-
Coordinate c = Coordinate.allocate(calloc, 10, 10, nullptr);
100+
final pointer = calloc<Coordinate>();
101+
Coordinate c = pointer.ref;
128102
Expect.isTrue(c is Struct);
103+
// TODO(https://dartbug.com/40667): Remove support for this.
129104
Expect.isTrue(c.addressOf is Pointer<Coordinate>);
130-
calloc.free(c.addressOf);
105+
calloc.free(pointer);
131106
}
132107

133108
void testUtf8() {

0 commit comments

Comments
 (0)