@@ -22,6 +22,7 @@ import 'package:_fe_analyzer_shared/src/macros/bootstrap.dart';
22
22
import 'package:_fe_analyzer_shared/src/macros/executor.dart' ;
23
23
import 'package:_fe_analyzer_shared/src/macros/executor_shared/introspection_impls.dart' ;
24
24
import 'package:_fe_analyzer_shared/src/macros/executor_shared/remote_instance.dart' ;
25
+ import 'package:_fe_analyzer_shared/src/macros/executor_shared/serialization.dart' ;
25
26
import 'package:_fe_analyzer_shared/src/macros/isolated_executor/isolated_executor.dart'
26
27
as isolatedExecutor;
27
28
@@ -30,6 +31,9 @@ void _log(String message) {
30
31
print ('${_watch .elapsed }: $message ' );
31
32
}
32
33
34
+ var clientSerializationMode = SerializationMode .jsonClient;
35
+ var serverSerializationMode = SerializationMode .jsonServer;
36
+
33
37
// Run this script to print out the generated augmentation library for an example class.
34
38
void main () async {
35
39
_log ('Preparing to run macros.' );
@@ -39,7 +43,7 @@ void main() async {
39
43
print ('This script must be ran from the `macros` directory.' );
40
44
exit (1 );
41
45
}
42
- var executor = await isolatedExecutor.start ();
46
+ var executor = await isolatedExecutor.start (serverSerializationMode );
43
47
var tmpDir = Directory .systemTemp.createTempSync ('data_class_macro_example' );
44
48
try {
45
49
var macroUri = thisFile.absolute.uri;
@@ -49,7 +53,7 @@ void main() async {
49
53
macroUri.toString (): {
50
54
macroName: ['' ],
51
55
}
52
- });
56
+ }, clientSerializationMode );
53
57
54
58
var bootstrapFile = File (tmpDir.uri.resolve ('main.dart' ).toFilePath ())
55
59
..writeAsStringSync (bootstrapContent);
@@ -73,12 +77,16 @@ void main() async {
73
77
_log ('Loading DataClass macro' );
74
78
var clazzId = await executor.loadMacro (macroUri, macroName,
75
79
precompiledKernelUri: kernelOutputFile.uri);
80
+ _log ('Instantiating macro' );
76
81
var instanceId =
77
82
await executor.instantiateMacro (clazzId, '' , Arguments ([], {}));
78
83
79
84
_log ('Running DataClass macro 100 times...' );
80
85
var results = < MacroExecutionResult > [];
81
- for (var i = 1 ; i < 101 ; i++ ) {
86
+ var macroExecutionStart = _watch.elapsed;
87
+ late Duration firstRunEnd;
88
+ late Duration first11RunsEnd;
89
+ for (var i = 1 ; i <= 111 ; i++ ) {
82
90
var _shouldLog = i == 1 || i == 10 || i == 100 ;
83
91
if (_shouldLog) _log ('Running DataClass macro for the ${i }th time' );
84
92
if (instanceId.shouldExecute (DeclarationKind .clazz, Phase .types)) {
@@ -103,17 +111,34 @@ void main() async {
103
111
if (i == 1 ) results.add (result);
104
112
}
105
113
if (_shouldLog) _log ('Done running DataClass macro for the ${i }th time.' );
114
+
115
+ if (i == 1 ) {
116
+ firstRunEnd = _watch.elapsed;
117
+ } else if (i == 11 ) {
118
+ first11RunsEnd = _watch.elapsed;
119
+ }
106
120
}
121
+ var first111RunsEnd = _watch.elapsed;
107
122
108
123
_log ('Building augmentation library' );
109
124
var library = executor.buildAugmentationLibrary (results, (identifier) {
110
125
if (identifier == boolIdentifier ||
111
126
identifier == objectIdentifier ||
112
127
identifier == stringIdentifier ||
113
128
identifier == intIdentifier) {
114
- return Uri (scheme: 'dart' , path: 'core' );
129
+ return ResolvedIdentifier (
130
+ kind: IdentifierKind .topLevelMember,
131
+ name: identifier.name,
132
+ staticScope: null ,
133
+ uri: null );
115
134
} else {
116
- return File ('example/data_class.dart' ).absolute.uri;
135
+ return ResolvedIdentifier (
136
+ kind: identifier.name == 'MyClass'
137
+ ? IdentifierKind .topLevelMember
138
+ : IdentifierKind .instanceMember,
139
+ name: identifier.name,
140
+ staticScope: null ,
141
+ uri: Platform .script.resolve ('data_class.dart' ));
117
142
}
118
143
});
119
144
executor.close ();
@@ -125,6 +150,11 @@ void main() async {
125
150
.replaceAll ('/*augment*/' , 'augment' );
126
151
127
152
_log ('Macro augmentation library:\n\n $formatted ' );
153
+ _log ('Time for the first run: ${macroExecutionStart - firstRunEnd }' );
154
+ _log ('Average time for the next 10 runs: '
155
+ '${(first11RunsEnd - firstRunEnd ).dividedBy (10 )}' );
156
+ _log ('Average time for the next 100 runs: '
157
+ '${(first111RunsEnd - first11RunsEnd ).dividedBy (100 )}' );
128
158
} finally {
129
159
tmpDir.deleteSync (recursive: true );
130
160
}
@@ -136,7 +166,7 @@ final intIdentifier = IdentifierImpl(id: RemoteInstance.uniqueId, name: 'int');
136
166
final objectIdentifier =
137
167
IdentifierImpl (id: RemoteInstance .uniqueId, name: 'Object' );
138
168
final stringIdentifier =
139
- IdentifierImpl (id: RemoteInstance .uniqueId, name: 'bool ' );
169
+ IdentifierImpl (id: RemoteInstance .uniqueId, name: 'String ' );
140
170
141
171
final boolType = NamedTypeAnnotationImpl (
142
172
id: RemoteInstance .uniqueId,
@@ -189,6 +219,7 @@ final myClassFields = [
189
219
isExternal: false ,
190
220
isFinal: true ,
191
221
isLate: false ,
222
+ isStatic: false ,
192
223
type: stringType),
193
224
FieldDeclarationImpl (
194
225
definingClass: myClassIdentifier,
@@ -197,6 +228,7 @@ final myClassFields = [
197
228
isExternal: false ,
198
229
isFinal: true ,
199
230
isLate: false ,
231
+ isStatic: false ,
200
232
type: boolType),
201
233
];
202
234
@@ -210,6 +242,7 @@ final myClassMethods = [
210
242
isGetter: false ,
211
243
isOperator: true ,
212
244
isSetter: false ,
245
+ isStatic: false ,
213
246
namedParameters: [],
214
247
positionalParameters: [
215
248
ParameterDeclarationImpl (
@@ -236,6 +269,7 @@ final myClassMethods = [
236
269
isOperator: false ,
237
270
isGetter: true ,
238
271
isSetter: false ,
272
+ isStatic: false ,
239
273
namedParameters: [],
240
274
positionalParameters: [],
241
275
returnType: intType,
@@ -250,6 +284,7 @@ final myClassMethods = [
250
284
isGetter: false ,
251
285
isOperator: false ,
252
286
isSetter: false ,
287
+ isStatic: false ,
253
288
namedParameters: [],
254
289
positionalParameters: [],
255
290
returnType: stringType,
@@ -289,3 +324,8 @@ class FakeTypeDeclarationResolver extends Fake
289
324
implements TypeDeclarationResolver {}
290
325
291
326
class FakeTypeResolver extends Fake implements TypeResolver {}
327
+
328
+ extension _ on Duration {
329
+ Duration dividedBy (int amount) =>
330
+ Duration (microseconds: (this .inMicroseconds / amount).round ());
331
+ }
0 commit comments