Skip to content

[vm/ffi] Struct.addressOf Pointer<Struct>.load() signatures #37284

Closed
@dcharkes

Description

@dcharkes

What should the API be to get from a Struct to a Pointer and vice versa?

Struct to Pointer: Struct.addressOf

Field of struct

abstract class Struct<S extends NativeType> extends NativeType {
  // If the struct is backed by native memory, returns the address of the
  // reference. If the struct is backed by Dart-allocated memory, returns null.
  final Pointer<S> addressOf;
}

class TestStruct4 extends ffi.Struct<TestStruct4> {
  @ffi.Double()
  double z;
}

Pros:

  • concise access through . notation
  • no specification of type argument on invocation

Con:

  • a type argument for Struct

Top level function

Pointer<S> addressOf<S extends Struct>(S s) {
  // ...
}

Pro:

  • no type argument on Struct
  • specify type argument at every invocation

Con:

  • no . notation

Method

abstract class Struct extends NativeType {
  // If the struct is backed by native memory, returns the address of the
  // reference. If the struct is backed by Dart-allocated memory, returns null.
 Pointer<S> addressOf<S>();
}

Pro:

  • no type argument on Struct
  • . notation

Con:

  • specify type argument at every invocation

Extension method

extension MyExtension on S extends Struct {
  Pointer<S> addressOf() {
    // ...
  }
}

@lrhn @leafpetersen is this supported by our design for extension methods?

Pro:

  • no type argument on Struct
  • . notation
  • no type argument on invocation

This has the least amount of cons, if this would be supported.

Pointer to Struct: Pointer.load

Similar argument for getting from a Pointer to a Struct by using load.

Extension method

extension MyExtension on Pointer<S extends Struct> {
  S load();
}

Pro:

  • No type argument on invacation

Method

This is the current API.

Pointer<N extends NativeType> {
  // T = int when N = Int32 etc
  // T = N when N is a subtype of Struct
  T load<T>();
}

Con:

  • specifying type argument on invocation

Pre-extension methods

If the extension methods will be supported in the future, what should we decide for now - with the easiest path of migrating to extension methods later?

Metadata

Metadata

Labels

area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.library-ffitype-design

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions