Skip to content

Commit 9fe8865

Browse files
[vm, bytecode] Implement ParameterMirror and enable mirror tests.
In particular, mirrors_reader_test no longer crashes. Change-Id: I602e7d85755dafd943fe84d20831f0f35484b387 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/108968 Reviewed-by: Alexander Markov <[email protected]>
1 parent 1ac0a36 commit 9fe8865

File tree

13 files changed

+413
-33
lines changed

13 files changed

+413
-33
lines changed

pkg/vm/lib/bytecode/dbc.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ library vm.bytecode.dbc;
1010
/// Before bumping current bytecode version format, make sure that
1111
/// all users have switched to a VM which is able to consume new
1212
/// version of bytecode.
13-
const int currentBytecodeFormatVersion = 16;
13+
const int currentBytecodeFormatVersion = 17;
1414

1515
enum Opcode {
1616
kUnusedOpcode000,

pkg/vm/lib/bytecode/declarations.dart

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class ClassDeclaration {
105105
final ObjectHandle superType;
106106
final List<ObjectHandle> interfaces;
107107
final Members members;
108-
final ObjectHandle annotations;
108+
final AnnotationsDeclaration annotations;
109109

110110
ClassDeclaration(
111111
this.name,
@@ -161,7 +161,7 @@ class ClassDeclaration {
161161
final superType = reader.readPackedObject();
162162
final interfaces = reader.readPackedList<ObjectHandle>();
163163
final annotations = ((flags & hasAnnotationsFlag) != 0)
164-
? reader.readLinkOffset<ObjectHandle>()
164+
? reader.readLinkOffset<AnnotationsDeclaration>()
165165
: null;
166166
final members = reader.readLinkOffset<Members>();
167167
return new ClassDeclaration(
@@ -343,7 +343,7 @@ class FieldDeclaration {
343343
final ObjectHandle getterName;
344344
final ObjectHandle setterName;
345345
final Code initializerCode;
346-
final ObjectHandle annotations;
346+
final AnnotationsDeclaration annotations;
347347

348348
FieldDeclaration(
349349
this.flags,
@@ -409,7 +409,7 @@ class FieldDeclaration {
409409
final setterName =
410410
((flags & hasSetterFlag) != 0) ? reader.readPackedObject() : null;
411411
final annotations = ((flags & hasAnnotationsFlag) != 0)
412-
? reader.readLinkOffset<ObjectHandle>()
412+
? reader.readLinkOffset<AnnotationsDeclaration>()
413413
: null;
414414
return new FieldDeclaration(flags, name, type, value, script, position,
415415
endPosition, getterName, setterName, initializerCode, annotations);
@@ -496,7 +496,7 @@ class FunctionDeclaration {
496496
final ObjectHandle returnType;
497497
final ObjectHandle nativeName;
498498
final Code code;
499-
final ObjectHandle annotations;
499+
final AnnotationsDeclaration annotations;
500500

501501
FunctionDeclaration(
502502
this.flags,
@@ -576,7 +576,7 @@ class FunctionDeclaration {
576576
final code =
577577
((flags & isAbstractFlag) == 0) ? reader.readLinkOffset<Code>() : null;
578578
final annotations = ((flags & hasAnnotationsFlag) != 0)
579-
? reader.readLinkOffset<ObjectHandle>()
579+
? reader.readLinkOffset<AnnotationsDeclaration>()
580580
: null;
581581
return new FunctionDeclaration(
582582
flags,
@@ -717,6 +717,7 @@ class ParameterDeclaration {
717717
// Parameter flags are written separately (in Code).
718718
static const isCovariantFlag = 1 << 0;
719719
static const isGenericCovariantImplFlag = 1 << 1;
720+
static const isFinalFlag = 1 << 2;
720721

721722
final ObjectHandle name;
722723
final ObjectHandle type;
@@ -1117,6 +1118,20 @@ class ClosureCode {
11171118
}
11181119
}
11191120

1121+
class AnnotationsDeclaration {
1122+
final ObjectHandle object;
1123+
1124+
AnnotationsDeclaration(this.object);
1125+
1126+
void write(BufferedWriter writer) {
1127+
writer.writePackedObject(object);
1128+
}
1129+
1130+
factory AnnotationsDeclaration.read(BufferedReader reader) {
1131+
return new AnnotationsDeclaration(reader.readPackedObject());
1132+
}
1133+
}
1134+
11201135
class _Section {
11211136
int numItems;
11221137
int offset;
@@ -1147,7 +1162,7 @@ class Component {
11471162
final List<SourceFile> sourceFiles = <SourceFile>[];
11481163
final Map<Uri, SourceFile> uriToSource = <Uri, SourceFile>{};
11491164
final List<LocalVariableTable> localVariables = <LocalVariableTable>[];
1150-
final List<ObjectHandle> annotations = <ObjectHandle>[];
1165+
final List<AnnotationsDeclaration> annotations = <AnnotationsDeclaration>[];
11511166
ObjectHandle mainLibrary;
11521167

11531168
Component(this.version)
@@ -1163,7 +1178,7 @@ class Component {
11631178
final annotationsWriter = new BufferedWriter.fromWriter(writer);
11641179
for (var annot in annotations) {
11651180
writer.linkWriter.put(annot, annotationsWriter.offset);
1166-
annotationsWriter.writePackedObject(annot);
1181+
annot.write(annotationsWriter);
11671182
}
11681183
BytecodeSizeStatistics.annotationsSize += annotationsWriter.offset;
11691184

@@ -1355,7 +1370,7 @@ class Component {
13551370
reader.offset = annotationsStart;
13561371
for (int i = 0; i < annotationsNum; ++i) {
13571372
int offset = reader.offset - annotationsStart;
1358-
ObjectHandle annot = reader.readPackedObject();
1373+
AnnotationsDeclaration annot = new AnnotationsDeclaration.read(reader);
13591374
reader.linkReader.setOffset(annot, offset);
13601375
annotations.add(annot);
13611376
}

pkg/vm/lib/bytecode/gen_bytecode.dart

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,52 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
383383
}
384384
final object =
385385
objectTable.getHandle(new ListConstant(const DynamicType(), constants));
386-
bytecodeComponent.annotations.add(object);
387-
return new Annotations(object, hasPragma);
386+
final decl = new AnnotationsDeclaration(object);
387+
bytecodeComponent.annotations.add(decl);
388+
return new Annotations(decl, hasPragma);
389+
}
390+
391+
// Insert annotations for the function and its parameters into the annotations
392+
// section. Return the annotations for the function only. The bytecode reader
393+
// will implicitly find the parameter annotations by reading N packed objects
394+
// after reading the function's annotations, one for each parameter.
395+
Annotations getFunctionAnnotations(Member member) {
396+
final functionNodes = member.annotations;
397+
final parameterNodeLists = new List<List<Expression>>();
398+
for (VariableDeclaration variable in member.function.positionalParameters) {
399+
parameterNodeLists.add(variable.annotations);
400+
}
401+
for (VariableDeclaration variable in member.function.namedParameters) {
402+
parameterNodeLists.add(variable.annotations);
403+
}
404+
405+
if (functionNodes.isEmpty &&
406+
parameterNodeLists.every((nodes) => nodes.isEmpty)) {
407+
return const Annotations(null, false);
408+
}
409+
410+
List<Constant> functionConstants = constantEvaluator.withNewEnvironment(
411+
() => functionNodes.map(_evaluateConstantExpression).toList());
412+
bool hasPragma = functionConstants.any(_isPragma);
413+
if (!options.emitAnnotations && !hasPragma) {
414+
return const Annotations(null, false);
415+
}
416+
417+
final functionObject = objectTable
418+
.getHandle(new ListConstant(const DynamicType(), functionConstants));
419+
final functionDecl = new AnnotationsDeclaration(functionObject);
420+
bytecodeComponent.annotations.add(functionDecl);
421+
422+
for (final parameterNodes in parameterNodeLists) {
423+
List<Constant> parameterConstants = constantEvaluator.withNewEnvironment(
424+
() => parameterNodes.map(_evaluateConstantExpression).toList());
425+
final parameterObject = objectTable
426+
.getHandle(new ListConstant(const DynamicType(), parameterConstants));
427+
final parameterDecl = new AnnotationsDeclaration(parameterObject);
428+
bytecodeComponent.annotations.add(parameterDecl);
429+
}
430+
431+
return new Annotations(functionDecl, hasPragma);
388432
}
389433

390434
FieldDeclaration getFieldDeclaration(Field field, Code initializer) {
@@ -548,7 +592,7 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
548592
position = (member as dynamic).startFileOffset;
549593
endPosition = member.fileEndOffset;
550594
}
551-
Annotations annotations = getAnnotations(member.annotations);
595+
Annotations annotations = getFunctionAnnotations(member);
552596
if (annotations.object != null) {
553597
flags |= FunctionDeclaration.hasAnnotationsFlag;
554598
if (annotations.hasPragma) {
@@ -641,6 +685,9 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
641685
if (variable.isGenericCovariantImpl) {
642686
flags |= ParameterDeclaration.isGenericCovariantImplFlag;
643687
}
688+
if (variable.isFinal) {
689+
flags |= ParameterDeclaration.isFinalFlag;
690+
}
644691
return flags;
645692
}
646693

@@ -1408,7 +1455,9 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
14081455
int forwardingStubTargetCpIndex = null;
14091456
int defaultFunctionTypeArgsCpIndex = null;
14101457

1411-
if (node is Procedure) {
1458+
if (node is Constructor) {
1459+
parameterFlags = getParameterFlags(node.function);
1460+
} else if (node is Procedure) {
14121461
parameterFlags = getParameterFlags(node.function);
14131462

14141463
if (node.isForwardingStub) {
@@ -3905,7 +3954,7 @@ class FinallyBlock {
39053954
}
39063955

39073956
class Annotations {
3908-
final ObjectHandle object;
3957+
final AnnotationsDeclaration object;
39093958
final bool hasPragma;
39103959

39113960
const Annotations(this.object, this.hasPragma);

runtime/lib/mirrors.cc

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@
77
#include "lib/invocation_mirror.h"
88
#include "vm/bootstrap_natives.h"
99
#include "vm/class_finalizer.h"
10-
#include "vm/compiler/frontend/kernel_to_il.h"
11-
#include "vm/compiler/jit/compiler.h"
1210
#include "vm/dart_api_impl.h"
1311
#include "vm/dart_entry.h"
1412
#include "vm/exceptions.h"
15-
#include "vm/flags.h"
13+
#include "vm/kernel.h"
1614
#include "vm/object_store.h"
1715
#include "vm/parser.h"
1816
#include "vm/port.h"
19-
#include "vm/resolver.h"
2017
#include "vm/symbols.h"
2118

2219
namespace dart {
@@ -132,20 +129,14 @@ static RawInstance* CreateParameterMirrorList(const Function& func,
132129
// hence do not have a token position, and therefore cannot be reparsed.
133130
has_extra_parameter_info = false;
134131
}
135-
if (func.is_declared_in_bytecode()) {
136-
// Anonymous closures in bytecode cannot be reparsed.
137-
has_extra_parameter_info = false;
138-
}
139132

140133
Array& param_descriptor = Array::Handle();
141134
if (has_extra_parameter_info) {
142135
// Reparse the function for the following information:
143136
// * The default value of a parameter.
144137
// * Whether a parameters has been declared as final.
145138
// * Any metadata associated with the parameter.
146-
Object& result = Object::Handle();
147-
ASSERT(func.kernel_offset() > 0);
148-
result = kernel::BuildParameterDescriptor(func);
139+
Object& result = Object::Handle(kernel::BuildParameterDescriptor(func));
149140
if (result.IsError()) {
150141
Exceptions::PropagateError(Error::Cast(result));
151142
UNREACHABLE();

0 commit comments

Comments
 (0)