p-hatan/ mirror

pub repository

mirror.p-hatan.com

record_use

1.1.1

Dart API to access `@RecordUse()` recorded usages in link hooks.

Cached in Iran · Upstream SHA-256 matched

Download archive

Readme

Dart API to access @RecordUse() recorded usages in link hooks.

During compilation, usages of declarations annotated with @RecordUse() in reachable code are recorded, and information about these usages is made available to post-compile steps (such as link hooks). Usages in unreachable code are not recorded.

  • If placed on a statically resolved function or member (such as a top-level function, static method, non-redirecting factory constructor, getter, setter, operator, extension method, or extension type method), all calls to or tear-offs of that member in reachable code will be recorded, along with arguments passed to the call as far as they can be evaluated as constant expressions at compile time. Generative constructors and redirecting factory constructors cannot be annotated directly.
  • If placed on a final class or enum:
    • For a final class: any constant instance of the class (including instances created via const redirecting factory constructors), any non-const generative constructor invocation, and any generative constructor tear-off in reachable code will be recorded. Calls to non-const factory constructors are not recorded directly by annotating the class; rather, any generative constructor invocation within the factory body will be recorded.
    • For an enum: any constant enum element in reachable code will be recorded.
    • The @RecordUse() annotation cannot be placed directly on an extension type to record instances.

Only usages in executable code are recorded. Usages appearing within metadata (annotations) are ignored.

The @RecordUse() annotation is only allowed on declarations within a package's lib/ directory.

Example

void main() {
  PirateTranslator.speak('Hello');
  print(const PirateShip('Black Pearl', 50));
}

abstract class PirateTranslator {
  @RecordUse()
  static String speak(String english) => 'Ahoy $english';
}

@RecordUse()
final class PirateShip {
  final String name;
  final int cannons;

  const PirateShip(this.name, this.cannons);
}

This code will generate a data file that contains both the field values of the PirateShip instances, as well as the arguments for the speak method annotated with @RecordUse().

This information can then be accessed in a link hook as follows:

void main(List<String> arguments) {
  link(arguments, (input, output) async {
    final uses = input.recordedUses;
    if (uses == null) return;

    final calls = uses.calls[methodId] ?? [];
    for (final call in calls) {
      switch (call) {
        case CallWithArguments(
          positionalArguments: [StringConstant(value: final english), ...],
        ):
          // Shrink a translations file based on all the different translation
          // keys.
          print('Translating to pirate: $english');
        case _:
          print('Cannot determine which translations are used.');
      }
    }

    final ships = uses.instances[classId] ?? [];
    for (final ship in ships) {
      switch (ship) {
        case InstanceConstantReference(
          instanceConstant: InstanceConstant(
            fields: {'name': StringConstant(value: final name)},
          ),
        ):
          // Include the 3d model for this ship in the application but not
          // bundle the other ships.
          print('Pirate ship found: $name');
        case _:
          print('Cannot determine which ships are used.');
      }
    }
  });
}

For complete end-to-end examples combining link hooks (hook/link.dart) and package:record_use to tree-shake native C libraries (LinkerOptions.treeshake), see the examples index.

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

Changelog

1.1.1

  • Fix quadratic deserialization performance and index corruption in Recordings.fromJson.

1.1.0

  • Added Recordings.+ to combine two recordings.

1.0.0

  • Stable release.

0.6.0

  • Breaking Change: Changed the JSON format and Dart API in various ways.

0.5.0

  • Complete reimplementation of the package. The Dart API changed completely, the underlying JSON format changed completely, the internal architecture changed completely to be based on a JSON schema, the format can now express many more recordings, the code samples in the doc comments are now tested and up to date, and many more changes. This is by no means a final version, but will enable people to play with the current state of the implementation.

0.4.2

  • Fix empty instance parsing.

0.4.1

  • Fix bug in signature parsing.

0.4.0

  • Update SDK constraint to ^3.5.0.
  • Rewrite API to expose less symbols.
  • Remove locations for easier caching.

0.3.0

  • Make InstanceConstant a Constant.
  • Separate import from location uri.

0.2.0

  • Use maps instead of lists in serialization.

0.1.1

  • Fix repository link.

0.1.0

  • Initial version.

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.com
flutter pub add record_use
pubspec.yaml
name: record_use
description: >
  Dart API to access `@RecordUse()` recorded usages in link hooks.
version: 1.1.1
repository: https://github.com/dart-lang/native/tree/main/pkgs/record_use

environment:
  sdk: '>=3.10.0 <4.0.0'

resolution: workspace

dependencies:
  collection: ^1.19.1
  meta: ^1.19.0
  pub_semver: ^2.2.0

dev_dependencies:
  dart_flutter_team_lints: ^3.5.2
  json_schema: ^5.2.2
  json_syntax_generator: ^0.1.0-wip
  native_test_helpers: ^0.1.0-wip
  test: ^1.25.15

Version history

VersionPublishedArchiveSHA-256
0.1.02024-09-06T13:16:07.619797ZCached3aa9785ab984e967c6ab50bf1b55d18cdea258c2b9a7c0a275bd1cdd0a18ba41
0.2.02024-09-13T08:33:42.350999ZCachedaa2630e495bf522df65248c2488b4b05790b9356743b41d5b24d39f47f6d6085
0.3.02024-09-13T09:43:31.776788ZCached7c8e4f5ae82350f12d328e37892ca5874b69ab9393d14210d781ccce2e9a98f6
0.4.02025-05-05T11:25:28.405679ZCached67febd9224ec96fc73522ffe3037e96a74589f6a11a6c99100b1850f159402b1
0.4.12025-06-24T10:35:55.925846ZCachede2e0d735e843f81a8ab28572fcff9f30aeb0a5870b6450d4bc6778cbc919c8e8
0.4.22025-09-29T15:03:11.895281ZCachedd3d59b18ca7aa1ce689bb3b3fd982bbebdfcd9e89f977576486a55e3b0f19a27
0.5.02026-03-06T11:26:11.668488ZCachedbd4b242e3e16ad772db86581b40b3a5e0d159205211b02acff15974ac6c3b939
0.6.02026-03-25T12:19:08.173499ZCached2551bd8eecfe95d14ae75f6021ad0248be5c27f138c2ec12fcb52b500b3ba1ed
1.0.02026-07-13T14:23:16.395334ZCachedaf37186ff9ede46fa32f152526c48f46c5b3ad7d3d5a566140cb5df3dc4ed737
1.1.02026-08-10T09:34:54.558113ZCached3b4ab682aff40175afca6ff090cc5aa7ce9dafe8dfbae1537a6c678e6678baee
1.1.12026-08-18T07:25:17.066900ZCached1cb8564af8d43b464294411db9217f5ec04891c6f22ee2c32d73ae05e88a6bd2

Dependencies

collection "^1.19.1"

meta "^1.19.0"

pub_semver "^2.2.0"