Skip to content

[vm/ffi] Allocate Array with TypedData #54512

Open
@dcharkes

Description

@dcharkes

In [vm/ffi] TypedData constructors for Struct and Union addressing #45697, structs and unions can are automatically backed by a typed data on the default constructor.

We should consider doing similar for fixed-size arrays.

Things to consider:

  • We already use the Array constructor for annotations.

API sketch:

void main() {
  // Allocates a typed data which fits `Coordinate[4]`.
  final a = Array<Coordinate>.typedData(4);

  // Allocates a typed data which fits `Coordinate[2][2]`.
  final a = Array<Array<Coordinate>>.typedData(2, 2);
}

final class Coordinate extends Struct {
  // ...
}

If we could somehow distinguish annotation call sites from normal code, we could possibly reuse the existing constructor:

void main() {
  // Allocates a typed data which fits `Coordinate[4]`.
  final a = Array<Coordinate>(4);

  // Allocates a typed data which fits `Coordinate[2][2]`.
  final a = Array<Array<Coordinate>>(2, 2);
}

cc @mkustermann

Workaround for compile-time length arrays

After https://dart-review.googlesource.com/c/sdk/+/342763 lands.

Define a wrapper struct, allocate that as typed data, and read the array out:

main() {
  final Array<MyStruct> a = HelperStruct().inlineArray;
}

class HelperStruct extends Struct {
  @Array(10)
  external Array<Struct> inlineArray;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.library-ffitriagedIssue has been triaged by sub teamtype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions