Skip to content

Commit 4dc1901

Browse files
alexmarkovcommit-bot@chromium.org
authored andcommitted
[vm,bytecode] Write closure attributes separately
There are could be conflicts in inferred types attributes between function and its closures, so attributes for closures should be written separately. Change-Id: Iae395d73fdc2dfcb6b55137176f198f02e9b079b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/121907 Reviewed-by: Ryan Macnak <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
1 parent ecd7977 commit 4dc1901

File tree

3 files changed

+62
-16
lines changed

3 files changed

+62
-16
lines changed

pkg/vm/lib/bytecode/declarations.dart

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -949,16 +949,17 @@ class Code {
949949
}
950950

951951
class ClosureDeclaration {
952-
static const int hasOptionalPositionalParamsFlag = 1 << 0;
953-
static const int hasOptionalNamedParamsFlag = 1 << 1;
954-
static const int hasTypeParamsFlag = 1 << 2;
955-
static const int hasSourcePositionsFlag = 1 << 3;
956-
static const int isAsyncFlag = 1 << 4;
957-
static const int isAsyncStarFlag = 1 << 5;
958-
static const int isSyncStarFlag = 1 << 6;
959-
static const int isDebuggableFlag = 1 << 7;
960-
961-
final int flags;
952+
static const hasOptionalPositionalParamsFlag = 1 << 0;
953+
static const hasOptionalNamedParamsFlag = 1 << 1;
954+
static const hasTypeParamsFlag = 1 << 2;
955+
static const hasSourcePositionsFlag = 1 << 3;
956+
static const isAsyncFlag = 1 << 4;
957+
static const isAsyncStarFlag = 1 << 5;
958+
static const isSyncStarFlag = 1 << 6;
959+
static const isDebuggableFlag = 1 << 7;
960+
static const hasAttributesFlag = 1 << 8;
961+
962+
int flags;
962963
final ObjectHandle parent;
963964
final ObjectHandle name;
964965
final int position;
@@ -968,6 +969,7 @@ class ClosureDeclaration {
968969
final int numNamedParams;
969970
final List<NameAndType> parameters;
970971
final ObjectHandle returnType;
972+
ObjectHandle attributes;
971973
ClosureCode code;
972974

973975
ClosureDeclaration(
@@ -980,7 +982,8 @@ class ClosureDeclaration {
980982
this.numRequiredParams,
981983
this.numNamedParams,
982984
this.parameters,
983-
this.returnType);
985+
this.returnType,
986+
[this.attributes]);
984987

985988
void write(BufferedWriter writer) {
986989
writer.writePackedUInt30(flags);
@@ -1012,6 +1015,9 @@ class ClosureDeclaration {
10121015
writer.writePackedObject(param.type);
10131016
}
10141017
writer.writePackedObject(returnType);
1018+
if ((flags & hasAttributesFlag) != 0) {
1019+
writer.writePackedObject(attributes);
1020+
}
10151021
}
10161022

10171023
factory ClosureDeclaration.read(BufferedReader reader) {
@@ -1051,8 +1057,20 @@ class ClosureDeclaration {
10511057
(_) => new NameAndType(
10521058
reader.readPackedObject(), reader.readPackedObject()));
10531059
final returnType = reader.readPackedObject();
1054-
return new ClosureDeclaration(flags, parent, name, position, endPosition,
1055-
typeParams, numRequiredParams, numNamedParams, parameters, returnType);
1060+
final attributes =
1061+
((flags & hasAttributesFlag) != 0) ? reader.readPackedObject() : null;
1062+
return new ClosureDeclaration(
1063+
flags,
1064+
parent,
1065+
name,
1066+
position,
1067+
endPosition,
1068+
typeParams,
1069+
numRequiredParams,
1070+
numNamedParams,
1071+
parameters,
1072+
returnType,
1073+
attributes);
10561074
}
10571075

10581076
@override
@@ -1088,6 +1106,9 @@ class ClosureDeclaration {
10881106
}
10891107
sb.write(') -> ');
10901108
sb.writeln(returnType);
1109+
if ((flags & hasAttributesFlag) != 0) {
1110+
sb.write(' attributes $attributes\n');
1111+
}
10911112
if (code != null) {
10921113
sb.write(code.toString());
10931114
}

pkg/vm/lib/bytecode/gen_bytecode.dart

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
433433
return new Annotations(decl, hasPragma);
434434
}
435435

436-
ObjectHandle getMemberAttributes(Member member) {
436+
ObjectHandle getMemberAttributes() {
437437
if (procedureAttributesMetadata == null && inferredTypesAttribute == null) {
438438
return null;
439439
}
@@ -453,6 +453,17 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
453453
return objectTable.getHandle(ListConstant(const DynamicType(), attrs));
454454
}
455455

456+
ObjectHandle getClosureAttributes() {
457+
if (inferredTypesAttribute == null) {
458+
return null;
459+
}
460+
final attrs = <Constant>[
461+
StringConstant(InferredTypeMetadataRepository.repositoryTag),
462+
ListConstant(const DynamicType(), inferredTypesAttribute),
463+
];
464+
return objectTable.getHandle(ListConstant(const DynamicType(), attrs));
465+
}
466+
456467
// Insert annotations for the function and its parameters into the annotations
457468
// section. Return the annotations for the function only. The bytecode reader
458469
// will implicitly find the parameter annotations by reading N packed objects
@@ -621,7 +632,7 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
621632
flags |= FieldDeclaration.hasPragmaFlag;
622633
}
623634
}
624-
final ObjectHandle attributes = getMemberAttributes(field);
635+
final ObjectHandle attributes = getMemberAttributes();
625636
if (attributes != null) {
626637
flags |= FieldDeclaration.hasAttributesFlag;
627638
}
@@ -737,7 +748,7 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
737748
flags |= FunctionDeclaration.hasPragmaFlag;
738749
}
739750
}
740-
final ObjectHandle attributes = getMemberAttributes(member);
751+
final ObjectHandle attributes = getMemberAttributes();
741752
if (attributes != null) {
742753
flags |= FunctionDeclaration.hasAttributesFlag;
743754
}
@@ -2354,6 +2365,8 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
23542365
enclosingFunction = function;
23552366
final savedLoopDepth = currentLoopDepth;
23562367
currentLoopDepth = 0;
2368+
final savedInferredTypesAttribute = inferredTypesAttribute;
2369+
inferredTypesAttribute = null;
23572370

23582371
if (function.typeParameters.isNotEmpty) {
23592372
functionTypeParameters ??= new List<TypeParameter>();
@@ -2431,6 +2444,13 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
24312444
isClosure = savedIsClosure;
24322445
currentLoopDepth = savedLoopDepth;
24332446

2447+
final attributes = getClosureAttributes();
2448+
if (attributes != null) {
2449+
closure.attributes = attributes;
2450+
closure.flags |= ClosureDeclaration.hasAttributesFlag;
2451+
}
2452+
inferredTypesAttribute = savedInferredTypesAttribute;
2453+
24342454
locals.leaveScope();
24352455

24362456
closure.code = new ClosureCode(asm.bytecode, asm.exceptionsTable,

runtime/vm/compiler/frontend/bytecode_reader.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ void BytecodeReaderHelper::ReadClosureDeclaration(const Function& function,
457457
const int kIsAsyncStarFlag = 1 << 5;
458458
const int kIsSyncStarFlag = 1 << 6;
459459
const int kIsDebuggableFlag = 1 << 7;
460+
const int kHasAttributesFlag = 1 << 8;
460461

461462
const intptr_t flags = reader_.ReadUInt();
462463

@@ -509,6 +510,10 @@ void BytecodeReaderHelper::ReadClosureDeclaration(const Function& function,
509510

510511
closure.SetSignatureType(signature_type);
511512

513+
if ((flags & kHasAttributesFlag) != 0) {
514+
ReadAttributes(closure);
515+
}
516+
512517
I->AddClosureFunction(closure);
513518
}
514519

0 commit comments

Comments
 (0)