Skip to content

Commit

Permalink
[vm/ffi] Migrate off .addressOf in samples
Browse files Browse the repository at this point in the history
Removes invocations of `.addressOf` in samples.

Removes the constructor from the Coordinate testing class which
has become useless with no access to the underlying pointer.

Issue: #40667


Change-Id: I1e55a4b159662654e1d14b2d51b0b43d734d5010
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/181405
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
  • Loading branch information
dcharkes authored and commit-bot@chromium.org committed Jan 28, 2021
1 parent 2525e44 commit 24a7e43
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 66 deletions.
10 changes: 0 additions & 10 deletions samples/ffi/coordinate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import 'dart:ffi';

import "package:ffi/ffi.dart";

/// Sample struct for dart:ffi library.
class Coordinate extends Struct {
@Double()
Expand All @@ -15,12 +13,4 @@ class Coordinate extends Struct {
external double y;

external Pointer<Coordinate> next;

factory Coordinate.allocate(
Allocator allocator, double x, double y, Pointer<Coordinate> next) {
return allocator<Coordinate>().ref
..x = x
..y = y
..next = next;
}
}
10 changes: 6 additions & 4 deletions samples/ffi/sample_ffi_functions_callbacks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ main() {
Pointer<NativeFunction<CoordinateTrice>> p2 =
ffiTestFunctions.lookup("CoordinateUnOpTrice");
CoordinateTrice coordinateUnOpTrice = p2.asFunction();
Coordinate c1 = Coordinate.allocate(calloc, 10.0, 20.0, nullptr);
c1.next = c1.addressOf;
Coordinate result =
coordinateUnOpTrice(transposeCoordinatePointer, c1.addressOf).ref;
final c1 = calloc<Coordinate>()
..ref.x = 10.0
..ref.y = 20.0;
c1.ref.next = c1;
Coordinate result = coordinateUnOpTrice(transposeCoordinatePointer, c1).ref;
print(result.runtimeType);
print(result.x);
print(result.y);
calloc.free(c1);
}

{
Expand Down
17 changes: 11 additions & 6 deletions samples/ffi/sample_ffi_functions_structs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@ main() {
ffiTestFunctions.lookup("TransposeCoordinate");
NativeCoordinateOp f1 = p1.asFunction();

Coordinate c1 = Coordinate.allocate(calloc, 10.0, 20.0, nullptr);
Coordinate c2 = Coordinate.allocate(calloc, 42.0, 84.0, c1.addressOf);
c1.next = c2.addressOf;
final c1 = calloc<Coordinate>()
..ref.x = 10.0
..ref.y = 20.0;
final c2 = calloc<Coordinate>()
..ref.x = 42.0
..ref.y = 84.0
..ref.next = c1;
c1.ref.next = c2;

Coordinate result = f1(c1.addressOf).ref;
Coordinate result = f1(c1).ref;

print(c1.x);
print(c1.y);
print(c1.ref.x);
print(c1.ref.y);

print(result.runtimeType);

Expand Down
35 changes: 22 additions & 13 deletions samples/ffi/sample_ffi_structs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,28 @@ main() {

{
// Allocates each coordinate separately in c memory.
Coordinate c1 = Coordinate.allocate(calloc, 10.0, 10.0, nullptr);
Coordinate c2 = Coordinate.allocate(calloc, 20.0, 20.0, c1.addressOf);
Coordinate c3 = Coordinate.allocate(calloc, 30.0, 30.0, c2.addressOf);
c1.next = c3.addressOf;
final c1 = calloc<Coordinate>()
..ref.x = 10.0
..ref.y = 10.0;
final c2 = calloc<Coordinate>()
..ref.x = 20.0
..ref.y = 20.0
..ref.next = c1;
final c3 = calloc<Coordinate>()
..ref.x = 30.0
..ref.y = 30.0
..ref.next = c2;
c1.ref.next = c3;

Coordinate currentCoordinate = c1;
Coordinate currentCoordinate = c1.ref;
for (var i in [0, 1, 2, 3, 4]) {
currentCoordinate = currentCoordinate.next.ref;
print("${currentCoordinate.x}; ${currentCoordinate.y}");
}

calloc.free(c1.addressOf);
calloc.free(c2.addressOf);
calloc.free(c3.addressOf);
calloc.free(c1);
calloc.free(c2);
calloc.free(c3);
}

{
Expand Down Expand Up @@ -55,11 +63,12 @@ main() {
}

{
Coordinate c = Coordinate.allocate(calloc, 10, 10, nullptr);
print(c is Coordinate);
print(c is Pointer<Void>);
print(c is Pointer);
calloc.free(c.addressOf);
// Allocating in native memory returns a pointer.
final c = calloc<Coordinate>();
print(c is Pointer<Coordinate>);
// `.ref` returns a reference which gives access to the fields.
print(c.ref is Coordinate);
calloc.free(c);
}

print("end main");
Expand Down
10 changes: 0 additions & 10 deletions samples_2/ffi/coordinate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

import 'dart:ffi';

import "package:ffi/ffi.dart";

/// Sample struct for dart:ffi library.
class Coordinate extends Struct {
@Double()
Expand All @@ -17,12 +15,4 @@ class Coordinate extends Struct {
double y;

Pointer<Coordinate> next;

factory Coordinate.allocate(
Allocator allocator, double x, double y, Pointer<Coordinate> next) {
return allocator<Coordinate>().ref
..x = x
..y = y
..next = next;
}
}
10 changes: 6 additions & 4 deletions samples_2/ffi/sample_ffi_functions_callbacks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ main() {
Pointer<NativeFunction<CoordinateTrice>> p2 =
ffiTestFunctions.lookup("CoordinateUnOpTrice");
CoordinateTrice coordinateUnOpTrice = p2.asFunction();
Coordinate c1 = Coordinate.allocate(calloc, 10.0, 20.0, nullptr);
c1.next = c1.addressOf;
Coordinate result =
coordinateUnOpTrice(transposeCoordinatePointer, c1.addressOf).ref;
final c1 = calloc<Coordinate>()
..ref.x = 10.0
..ref.y = 20.0;
c1.ref.next = c1;
Coordinate result = coordinateUnOpTrice(transposeCoordinatePointer, c1).ref;
print(result.runtimeType);
print(result.x);
print(result.y);
calloc.free(c1);
}

{
Expand Down
17 changes: 11 additions & 6 deletions samples_2/ffi/sample_ffi_functions_structs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ main() {
ffiTestFunctions.lookup("TransposeCoordinate");
NativeCoordinateOp f1 = p1.asFunction();

Coordinate c1 = Coordinate.allocate(calloc, 10.0, 20.0, nullptr);
Coordinate c2 = Coordinate.allocate(calloc, 42.0, 84.0, c1.addressOf);
c1.next = c2.addressOf;
final c1 = calloc<Coordinate>()
..ref.x = 10.0
..ref.y = 20.0;
final c2 = calloc<Coordinate>()
..ref.x = 42.0
..ref.y = 84.0
..ref.next = c1;
c1.ref.next = c2;

Coordinate result = f1(c1.addressOf).ref;
Coordinate result = f1(c1).ref;

print(c1.x);
print(c1.y);
print(c1.ref.x);
print(c1.ref.y);

print(result.runtimeType);

Expand Down
35 changes: 22 additions & 13 deletions samples_2/ffi/sample_ffi_structs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,28 @@ main() {

{
// Allocates each coordinate separately in c memory.
Coordinate c1 = Coordinate.allocate(calloc, 10.0, 10.0, nullptr);
Coordinate c2 = Coordinate.allocate(calloc, 20.0, 20.0, c1.addressOf);
Coordinate c3 = Coordinate.allocate(calloc, 30.0, 30.0, c2.addressOf);
c1.next = c3.addressOf;
final c1 = calloc<Coordinate>()
..ref.x = 10.0
..ref.y = 10.0;
final c2 = calloc<Coordinate>()
..ref.x = 20.0
..ref.y = 20.0
..ref.next = c1;
final c3 = calloc<Coordinate>()
..ref.x = 30.0
..ref.y = 30.0
..ref.next = c2;
c1.ref.next = c3;

Coordinate currentCoordinate = c1;
Coordinate currentCoordinate = c1.ref;
for (var i in [0, 1, 2, 3, 4]) {
currentCoordinate = currentCoordinate.next.ref;
print("${currentCoordinate.x}; ${currentCoordinate.y}");
}

calloc.free(c1.addressOf);
calloc.free(c2.addressOf);
calloc.free(c3.addressOf);
calloc.free(c1);
calloc.free(c2);
calloc.free(c3);
}

{
Expand Down Expand Up @@ -57,11 +65,12 @@ main() {
}

{
Coordinate c = Coordinate.allocate(calloc, 10, 10, nullptr);
print(c is Coordinate);
print(c is Pointer<Void>);
print(c is Pointer);
calloc.free(c.addressOf);
// Allocating in native memory returns a pointer.
final c = calloc<Coordinate>();
print(c is Pointer<Coordinate>);
// `.ref` returns a reference which gives access to the fields.
print(c.ref is Coordinate);
calloc.free(c);
}

print("end main");
Expand Down

0 comments on commit 24a7e43

Please sign in to comment.