@@ -782,7 +782,7 @@ class BinaryBuilder {
782
782
var name = readName ();
783
783
var annotations = readAnnotationList (node);
784
784
debugPath.add (node.name? .name ?? 'constructor' );
785
- var function = readFunctionNode ();
785
+ var function = readFunctionNode (false );
786
786
pushVariableDeclarations (function.positionalParameters);
787
787
pushVariableDeclarations (function.namedParameters);
788
788
if (shouldWriteData) {
@@ -829,12 +829,8 @@ class BinaryBuilder {
829
829
bool readFunctionNodeNow =
830
830
(kind == ProcedureKind .Factory && functionNodeSize <= 50 ) ||
831
831
_disableLazyReading;
832
- var function;
833
- var transformerFlags;
834
- if (readFunctionNodeNow) {
835
- function = readFunctionNodeOption ();
836
- transformerFlags = getAndResetTransformerFlags ();
837
- }
832
+ var function = readFunctionNodeOption (! readFunctionNodeNow);
833
+ var transformerFlags = getAndResetTransformerFlags ();
838
834
debugPath.removeLast ();
839
835
if (shouldWriteData) {
840
836
node.fileOffset = fileOffset;
@@ -844,25 +840,9 @@ class BinaryBuilder {
844
840
node.name = name;
845
841
node.fileUri = fileUri;
846
842
node.annotations = annotations;
847
- if (readFunctionNodeNow) {
848
- node.function = function;
849
- function? .parent = node;
850
- node.transformerFlags = transformerFlags;
851
- } else {
852
- int offset = _byteOffset;
853
- int programStartOffset = _programStartOffset;
854
- List <TypeParameter > typeParameters = typeParameterStack.toList ();
855
- node.lazyBuilder = () {
856
- _byteOffset = offset;
857
- typeParameterStack.clear ();
858
- typeParameterStack.addAll (typeParameters);
859
- _programStartOffset = programStartOffset;
860
- FunctionNode functionNode = readFunctionNodeOption ();
861
- node.function = functionNode;
862
- functionNode? .parent = node;
863
- node.transformerFlags = getAndResetTransformerFlags ();
864
- };
865
- }
843
+ node.function = function;
844
+ function? .parent = node;
845
+ node.transformerFlags = transformerFlags;
866
846
}
867
847
_byteOffset = endOffset;
868
848
return node;
@@ -894,11 +874,11 @@ class BinaryBuilder {
894
874
}
895
875
}
896
876
897
- FunctionNode readFunctionNodeOption () {
898
- return readAndCheckOptionTag () ? readFunctionNode () : null ;
877
+ FunctionNode readFunctionNodeOption (bool lazyLoadBody ) {
878
+ return readAndCheckOptionTag () ? readFunctionNode (lazyLoadBody ) : null ;
899
879
}
900
880
901
- FunctionNode readFunctionNode () {
881
+ FunctionNode readFunctionNode (bool lazyLoadBody ) {
902
882
int tag = readByte ();
903
883
assert (tag == Tag .FunctionNode );
904
884
int offset = readOffset ();
@@ -914,12 +894,14 @@ class BinaryBuilder {
914
894
var named = readAndPushVariableDeclarationList ();
915
895
var returnType = readDartType ();
916
896
int oldLabelStackBase = labelStackBase;
917
- labelStackBase = labelStack.length;
918
- var body = readStatementOption ();
919
- labelStackBase = oldLabelStackBase;
920
- variableStack.length = variableStackHeight;
921
- typeParameterStack.length = typeParameterStackHeight;
922
- return new FunctionNode (body,
897
+
898
+ var body;
899
+ if (! lazyLoadBody) {
900
+ labelStackBase = labelStack.length;
901
+ body = readStatementOption ();
902
+ }
903
+
904
+ FunctionNode result = new FunctionNode (body,
923
905
typeParameters: typeParameters,
924
906
requiredParameterCount: requiredParameterCount,
925
907
positionalParameters: positional,
@@ -929,6 +911,37 @@ class BinaryBuilder {
929
911
dartAsyncMarker: dartAsyncMarker)
930
912
..fileOffset = offset
931
913
..fileEndOffset = endOffset;
914
+
915
+ if (lazyLoadBody) {
916
+ final int savedByteOffset = _byteOffset;
917
+ final int programStartOffset = _programStartOffset;
918
+ final List <TypeParameter > typeParameters = typeParameterStack.toList ();
919
+ final List <VariableDeclaration > variables = variableStack.toList ();
920
+ result.lazyBuilder = () {
921
+ _byteOffset = savedByteOffset;
922
+ typeParameterStack.clear ();
923
+ typeParameterStack.addAll (typeParameters);
924
+ variableStack.clear ();
925
+ variableStack.addAll (variables);
926
+ _programStartOffset = programStartOffset;
927
+
928
+ result.body = readStatementOption ();
929
+ result.body? .parent = result;
930
+ labelStackBase = oldLabelStackBase;
931
+ variableStack.length = variableStackHeight;
932
+ typeParameterStack.length = typeParameterStackHeight;
933
+ if (result.parent is Procedure ) {
934
+ Procedure parent = result.parent;
935
+ parent.transformerFlags | = getAndResetTransformerFlags ();
936
+ }
937
+ };
938
+ }
939
+
940
+ labelStackBase = oldLabelStackBase;
941
+ variableStack.length = variableStackHeight;
942
+ typeParameterStack.length = typeParameterStackHeight;
943
+
944
+ return result;
932
945
}
933
946
934
947
void pushVariableDeclaration (VariableDeclaration variable) {
@@ -1165,7 +1178,8 @@ class BinaryBuilder {
1165
1178
return new AwaitExpression (readExpression ());
1166
1179
case Tag .FunctionExpression :
1167
1180
int offset = readOffset ();
1168
- return new FunctionExpression (readFunctionNode ())..fileOffset = offset;
1181
+ return new FunctionExpression (readFunctionNode (false ))
1182
+ ..fileOffset = offset;
1169
1183
case Tag .Let :
1170
1184
var variable = readVariableDeclaration ();
1171
1185
int stackHeight = variableStack.length;
@@ -1329,7 +1343,7 @@ class BinaryBuilder {
1329
1343
int offset = readOffset ();
1330
1344
var variable = readVariableDeclaration ();
1331
1345
variableStack.add (variable); // Will be popped by the enclosing scope.
1332
- var function = readFunctionNode ();
1346
+ var function = readFunctionNode (false );
1333
1347
return new FunctionDeclaration (variable, function)..fileOffset = offset;
1334
1348
default :
1335
1349
throw fail ('Invalid statement tag: $tag ' );
@@ -1737,9 +1751,9 @@ class BinaryBuilderWithMetadata extends BinaryBuilder implements BinarySource {
1737
1751
}
1738
1752
1739
1753
@override
1740
- FunctionNode readFunctionNode () {
1754
+ FunctionNode readFunctionNode (bool lazyLoadBody ) {
1741
1755
final nodeOffset = _byteOffset;
1742
- final result = super .readFunctionNode ();
1756
+ final result = super .readFunctionNode (lazyLoadBody );
1743
1757
return _associateMetadata (result, nodeOffset);
1744
1758
}
1745
1759
0 commit comments