-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend benchmark generator to apply arbitrary macros to classes.
- Loading branch information
1 parent
a005818
commit 04fb59d
Showing
8 changed files
with
141 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:benchmark_generator/workspace.dart'; | ||
import 'package:dart_model/dart_model.dart'; | ||
|
||
class ClassesAndFieldsInputGenerator { | ||
final List<QualifiedName> macros; | ||
final int fieldsPerClass; | ||
final int classesPerLibrary; | ||
final int librariesPerCycle; | ||
|
||
ClassesAndFieldsInputGenerator({ | ||
required this.macros, | ||
required this.fieldsPerClass, | ||
required this.classesPerLibrary, | ||
required this.librariesPerCycle, | ||
}); | ||
|
||
void generate(Workspace workspace) { | ||
for (var i = 0; i != librariesPerCycle; ++i) { | ||
workspace.write('a$i.dart', source: _generateLibrary(i)); | ||
} | ||
} | ||
|
||
String _generateLibrary(int index) { | ||
final buffer = StringBuffer(); | ||
|
||
for (final qualifiedName in macros) { | ||
buffer.writeln("import '${qualifiedName.uri}';"); | ||
} | ||
|
||
if (librariesPerCycle != 1) { | ||
final nextLibrary = (index + 1) % librariesPerCycle; | ||
buffer.writeln('import "a$nextLibrary.dart" as next_in_cycle;'); | ||
buffer.writeln('next_in_cycle.A0? referenceOther;'); | ||
} | ||
|
||
for (var j = 0; j != classesPerLibrary; ++j) { | ||
buffer.write(_generateClass(index, j)); | ||
} | ||
|
||
return buffer.toString(); | ||
} | ||
|
||
String _generateClass(int libraryIndex, int index) { | ||
final className = 'A$index'; | ||
String fieldName(int fieldIndex) { | ||
if (libraryIndex == 0 && index == 0 && fieldIndex == 0) { | ||
return 'aCACHEBUSTER'; | ||
} | ||
return 'a$fieldIndex'; | ||
} | ||
|
||
final result = StringBuffer(); | ||
for (final qualifiedName in macros) { | ||
result.writeln('@${qualifiedName.name}()'); | ||
} | ||
|
||
result.writeln('class $className {'); | ||
|
||
if (macros.any( | ||
(m) => m.asString == 'package:_test_macros/json_codable.dart#JsonCodable', | ||
)) { | ||
result.writeln(''' | ||
// TODO(davidmorgan): see https://github.com/dart-lang/macros/issues/80. | ||
external $className.fromJson(Map<String, Object?> json); | ||
external Map<String, Object?> toJson();'''); | ||
} | ||
|
||
for (var i = 0; i != fieldsPerClass; ++i) { | ||
result.writeln('int? ${fieldName(i)};'); | ||
} | ||
|
||
result.writeln('}'); | ||
return result.toString(); | ||
} | ||
} |
Oops, something went wrong.