Skip to content

Commit e5d3128

Browse files
alexmarkovcommit-bot@chromium.org
authored andcommitted
[vm] Support Dart metadata on libraries and library dependencies in Dart 2 mode
Fixes #33774 Change-Id: I0b34cdf2bee099076e29bec6cfb849b0b250f690 Reviewed-on: https://dart-review.googlesource.com/64245 Reviewed-by: Ryan Macnak <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
1 parent d4df9b9 commit e5d3128

11 files changed

+83
-37
lines changed

runtime/vm/compiler/frontend/kernel_binary_flowgraph.cc

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7283,26 +7283,34 @@ RawObject* StreamingFlowGraphBuilder::BuildAnnotations() {
72837283
return metadata_values.raw();
72847284
}
72857285

7286-
RawObject* StreamingFlowGraphBuilder::EvaluateMetadata(intptr_t kernel_offset) {
7286+
RawObject* StreamingFlowGraphBuilder::EvaluateMetadata(
7287+
intptr_t kernel_offset,
7288+
bool is_annotations_offset) {
72877289
SetOffset(kernel_offset);
7288-
const Tag tag = PeekTag();
72897290

72907291
ASSERT(active_class() != NULL);
72917292

7292-
if (tag == kClass) {
7293-
ClassHelper class_helper(this);
7294-
class_helper.ReadUntilExcluding(ClassHelper::kAnnotations);
7295-
} else if (tag == kProcedure) {
7296-
ProcedureHelper procedure_helper(this);
7297-
procedure_helper.ReadUntilExcluding(ProcedureHelper::kAnnotations);
7298-
} else if (tag == kField) {
7299-
FieldHelper field_helper(this);
7300-
field_helper.ReadUntilExcluding(FieldHelper::kAnnotations);
7301-
} else if (tag == kConstructor) {
7302-
ConstructorHelper constructor_helper(this);
7303-
constructor_helper.ReadUntilExcluding(ConstructorHelper::kAnnotations);
7304-
} else {
7305-
FATAL("No support for metadata on this type of kernel node\n");
7293+
// Library and LibraryDependency objects do not have a tag in kernel binary.
7294+
// Synthetic metadata fields corresponding to these objects keep kernel
7295+
// offset of annotations list instead of annotated object.
7296+
if (!is_annotations_offset) {
7297+
const Tag tag = PeekTag();
7298+
7299+
if (tag == kClass) {
7300+
ClassHelper class_helper(this);
7301+
class_helper.ReadUntilExcluding(ClassHelper::kAnnotations);
7302+
} else if (tag == kProcedure) {
7303+
ProcedureHelper procedure_helper(this);
7304+
procedure_helper.ReadUntilExcluding(ProcedureHelper::kAnnotations);
7305+
} else if (tag == kField) {
7306+
FieldHelper field_helper(this);
7307+
field_helper.ReadUntilExcluding(FieldHelper::kAnnotations);
7308+
} else if (tag == kConstructor) {
7309+
ConstructorHelper constructor_helper(this);
7310+
constructor_helper.ReadUntilExcluding(ConstructorHelper::kAnnotations);
7311+
} else {
7312+
FATAL("No support for metadata on this type of kernel node\n");
7313+
}
73067314
}
73077315

73087316
return BuildAnnotations();

runtime/vm/compiler/frontend/kernel_binary_flowgraph.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ class StreamingFlowGraphBuilder : public KernelReaderHelper {
275275
Fragment BuildStatementAt(intptr_t kernel_offset);
276276
RawObject* BuildParameterDescriptor(intptr_t kernel_offset);
277277
RawObject* BuildAnnotations();
278-
RawObject* EvaluateMetadata(intptr_t kernel_offset);
278+
RawObject* EvaluateMetadata(intptr_t kernel_offset,
279+
bool is_annotations_offset);
279280
void CollectTokenPositionsFor(
280281
intptr_t script_index,
281282
intptr_t initial_script_index,

runtime/vm/compiler/frontend/kernel_to_il.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,7 +2058,8 @@ JoinEntryInstr* BaseFlowGraphBuilder::BuildThrowNoSuchMethod() {
20582058
return nsm;
20592059
}
20602060

2061-
RawObject* EvaluateMetadata(const Field& metadata_field) {
2061+
RawObject* EvaluateMetadata(const Field& metadata_field,
2062+
bool is_annotations_offset) {
20622063
LongJumpScope jump;
20632064
if (setjmp(*jump.Set()) == 0) {
20642065
Thread* thread = Thread::Current();
@@ -2076,7 +2077,7 @@ RawObject* EvaluateMetadata(const Field& metadata_field) {
20762077
ExternalTypedData::Handle(Z, metadata_field.KernelData()),
20772078
metadata_field.KernelDataProgramOffset(), &active_class);
20782079
return streaming_flow_graph_builder.EvaluateMetadata(
2079-
metadata_field.kernel_offset());
2080+
metadata_field.kernel_offset(), is_annotations_offset);
20802081
} else {
20812082
Thread* thread = Thread::Current();
20822083
Error& error = Error::Handle();

runtime/vm/compiler/frontend/kernel_to_il.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,8 @@ class CatchBlock {
690690
intptr_t catch_try_index_;
691691
};
692692

693-
RawObject* EvaluateMetadata(const Field& metadata_field);
693+
RawObject* EvaluateMetadata(const Field& metadata_field,
694+
bool is_annotations_offset);
694695
RawObject* BuildParameterDescriptor(const Function& function);
695696
void CollectTokenPositionsFor(const Script& script);
696697

runtime/vm/compiler/frontend/kernel_translation_helper.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,10 @@ void LibraryDependencyHelper::ReadUntilExcluding(Field field) {
13041304
}
13051305
/* Falls through */
13061306
case kAnnotations: {
1307-
helper_->SkipListOfExpressions();
1307+
annotation_count_ = helper_->ReadListLength();
1308+
for (intptr_t i = 0; i < annotation_count_; ++i) {
1309+
helper_->SkipExpression(); // read ith expression.
1310+
}
13081311
if (++next_read_ == field) return;
13091312
}
13101313
/* Falls through */

runtime/vm/compiler/frontend/kernel_translation_helper.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ class LibraryDependencyHelper {
729729
};
730730

731731
explicit LibraryDependencyHelper(KernelReaderHelper* helper)
732-
: helper_(helper), next_read_(kFileOffset) {}
732+
: annotation_count_(0), helper_(helper), next_read_(kFileOffset) {}
733733

734734
void ReadUntilIncluding(Field field) {
735735
ReadUntilExcluding(static_cast<Field>(static_cast<int>(field) + 1));
@@ -740,6 +740,7 @@ class LibraryDependencyHelper {
740740
uint8_t flags_;
741741
StringIndex name_index_;
742742
NameIndex target_library_canonical_name_;
743+
intptr_t annotation_count_;
743744

744745
private:
745746
KernelReaderHelper* helper_;

runtime/vm/kernel_loader.cc

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,8 @@ RawLibrary* KernelLoader::LoadLibrary(intptr_t index) {
762762
Z, ScriptAt(library_helper.source_uri_index_, import_uri_index));
763763

764764
library_helper.ReadUntilExcluding(LibraryHelper::kAnnotations);
765+
intptr_t annotations_kernel_offset =
766+
builder_.ReaderOffset() - correction_offset_;
765767
intptr_t annotation_count = builder_.ReadListLength(); // read list length.
766768
if (annotation_count > 0) {
767769
EnsurePotentialExtensionLibraries();
@@ -772,17 +774,17 @@ RawLibrary* KernelLoader::LoadLibrary(intptr_t index) {
772774
}
773775
library_helper.SetJustRead(LibraryHelper::kAnnotations);
774776

775-
library_helper.ReadUntilExcluding(LibraryHelper::kDependencies);
776-
LoadLibraryImportsAndExports(&library);
777-
library_helper.SetJustRead(LibraryHelper::kDependencies);
778-
779777
// Setup toplevel class (which contains library fields/procedures).
780778
Class& toplevel_class =
781779
Class::Handle(Z, Class::New(library, Symbols::TopLevel(), script,
782780
TokenPosition::kNoSource));
783781
toplevel_class.set_is_cycle_free();
784782
library.set_toplevel_class(toplevel_class);
785783

784+
library_helper.ReadUntilExcluding(LibraryHelper::kDependencies);
785+
LoadLibraryImportsAndExports(&library, toplevel_class);
786+
library_helper.SetJustRead(LibraryHelper::kDependencies);
787+
786788
const GrowableObjectArray& classes =
787789
GrowableObjectArray::Handle(Z, I->object_store()->pending_classes());
788790

@@ -851,14 +853,21 @@ RawLibrary* KernelLoader::LoadLibrary(intptr_t index) {
851853
LoadProcedure(library, toplevel_class, false, next_procedure_offset);
852854
}
853855

856+
if (FLAG_enable_mirrors && annotation_count > 0) {
857+
ASSERT(annotations_kernel_offset > 0);
858+
library.AddLibraryMetadata(toplevel_class, TokenPosition::kNoSource,
859+
annotations_kernel_offset);
860+
}
861+
854862
toplevel_class.SetFunctions(Array::Handle(MakeFunctionsArray()));
855863
classes.Add(toplevel_class, Heap::kOld);
856864
if (!library.Loaded()) library.SetLoaded();
857865

858866
return library.raw();
859867
}
860868

861-
void KernelLoader::LoadLibraryImportsAndExports(Library* library) {
869+
void KernelLoader::LoadLibraryImportsAndExports(Library* library,
870+
const Class& toplevel_class) {
862871
GrowableObjectArray& show_list = GrowableObjectArray::Handle(Z);
863872
GrowableObjectArray& hide_list = GrowableObjectArray::Handle(Z);
864873
Array& show_names = Array::Handle(Z);
@@ -869,6 +878,11 @@ void KernelLoader::LoadLibraryImportsAndExports(Library* library) {
869878
const intptr_t deps_count = builder_.ReadListLength();
870879
for (intptr_t dep = 0; dep < deps_count; ++dep) {
871880
LibraryDependencyHelper dependency_helper(&builder_);
881+
882+
dependency_helper.ReadUntilExcluding(LibraryDependencyHelper::kAnnotations);
883+
intptr_t annotations_kernel_offset =
884+
builder_.ReaderOffset() - correction_offset_;
885+
872886
dependency_helper.ReadUntilExcluding(LibraryDependencyHelper::kCombinators);
873887

874888
// Ignore the dependency if the target library is invalid.
@@ -937,6 +951,11 @@ void KernelLoader::LoadLibraryImportsAndExports(Library* library) {
937951
}
938952
}
939953
}
954+
if (FLAG_enable_mirrors && dependency_helper.annotation_count_ > 0) {
955+
ASSERT(annotations_kernel_offset > 0);
956+
ns.AddMetadata(toplevel_class, TokenPosition::kNoSource,
957+
annotations_kernel_offset);
958+
}
940959
}
941960
}
942961

runtime/vm/kernel_loader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ class KernelLoader : public ValueObject {
249249
const Function& function,
250250
const AbstractType& field_type);
251251

252-
void LoadLibraryImportsAndExports(Library* library);
252+
void LoadLibraryImportsAndExports(Library* library,
253+
const Class& toplevel_class);
253254

254255
Library& LookupLibraryOrNull(NameIndex library);
255256
Library& LookupLibrary(NameIndex library);

runtime/vm/object.cc

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10626,8 +10626,9 @@ void Library::AddTypeParameterMetadata(const TypeParameter& param,
1062610626
}
1062710627

1062810628
void Library::AddLibraryMetadata(const Object& tl_owner,
10629-
TokenPosition token_pos) const {
10630-
AddMetadata(tl_owner, Symbols::TopLevel(), token_pos);
10629+
TokenPosition token_pos,
10630+
intptr_t kernel_offset) const {
10631+
AddMetadata(tl_owner, Symbols::TopLevel(), token_pos, kernel_offset);
1063110632
}
1063210633

1063310634
RawString* Library::MakeMetadataName(const Object& obj) const {
@@ -10695,7 +10696,8 @@ RawObject* Library::GetMetadata(const Object& obj) const {
1069510696
metadata = field.StaticValue();
1069610697
if (field.StaticValue() == Object::empty_array().raw()) {
1069710698
if (field.kernel_offset() > 0) {
10698-
metadata = kernel::EvaluateMetadata(field);
10699+
metadata = kernel::EvaluateMetadata(
10700+
field, /* is_annotations_offset = */ obj.IsLibrary());
1069910701
} else {
1070010702
metadata = Parser::ParseMetadata(field);
1070110703
}
@@ -12287,7 +12289,9 @@ void Namespace::set_metadata_field(const Field& value) const {
1228712289
StorePointer(&raw_ptr()->metadata_field_, value.raw());
1228812290
}
1228912291

12290-
void Namespace::AddMetadata(const Object& owner, TokenPosition token_pos) {
12292+
void Namespace::AddMetadata(const Object& owner,
12293+
TokenPosition token_pos,
12294+
intptr_t kernel_offset) {
1229112295
ASSERT(Field::Handle(metadata_field()).IsNull());
1229212296
Field& field = Field::Handle(Field::NewTopLevel(Symbols::TopLevel(),
1229312297
false, // is_final
@@ -12296,6 +12300,7 @@ void Namespace::AddMetadata(const Object& owner, TokenPosition token_pos) {
1229612300
field.set_is_reflectable(false);
1229712301
field.SetFieldType(Object::dynamic_type());
1229812302
field.SetStaticValue(Array::empty_array(), true);
12303+
field.set_kernel_offset(kernel_offset);
1229912304
set_metadata_field(field);
1230012305
}
1230112306

@@ -12311,7 +12316,12 @@ RawObject* Namespace::GetMetadata() const {
1231112316
Object& metadata = Object::Handle();
1231212317
metadata = field.StaticValue();
1231312318
if (field.StaticValue() == Object::empty_array().raw()) {
12314-
metadata = Parser::ParseMetadata(field);
12319+
if (field.kernel_offset() > 0) {
12320+
metadata =
12321+
kernel::EvaluateMetadata(field, /* is_annotations_offset = */ true);
12322+
} else {
12323+
metadata = Parser::ParseMetadata(field);
12324+
}
1231512325
if (metadata.IsArray()) {
1231612326
ASSERT(Array::Cast(metadata).raw() != Object::empty_array().raw());
1231712327
field.SetStaticValue(Array::Cast(metadata), true);

runtime/vm/object.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3849,7 +3849,8 @@ class Library : public Object {
38493849
TokenPosition token_pos,
38503850
intptr_t kernel_offset = 0) const;
38513851
void AddLibraryMetadata(const Object& tl_owner,
3852-
TokenPosition token_pos) const;
3852+
TokenPosition token_pos,
3853+
intptr_t kernel_offset = 0) const;
38533854
void AddTypeParameterMetadata(const TypeParameter& param,
38543855
TokenPosition token_pos) const;
38553856
void CloneMetadataFrom(const Library& from_library,
@@ -4065,7 +4066,9 @@ class Namespace : public Object {
40654066
RawArray* show_names() const { return raw_ptr()->show_names_; }
40664067
RawArray* hide_names() const { return raw_ptr()->hide_names_; }
40674068

4068-
void AddMetadata(const Object& owner, TokenPosition token_pos);
4069+
void AddMetadata(const Object& owner,
4070+
TokenPosition token_pos,
4071+
intptr_t kernel_offset = 0);
40694072
RawObject* GetMetadata() const;
40704073

40714074
static intptr_t InstanceSize() {

tests/lib_2/lib_2_kernel.status

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ mirrors/constructor_kinds_test/none: RuntimeError
116116
mirrors/constructor_private_name_test: RuntimeError # Issue 33345 - Incorrect qualified symbol literal from kernel reader
117117
mirrors/constructors_test: CompileTimeError # Issue 31402 (Invocation arguments)
118118
mirrors/dart2js_mirrors_test: RuntimeError # 31916
119-
mirrors/deferred_mirrors_metadata_test: RuntimeError, CompileTimeError # Deferred loading kernel issue 28335.
120119
mirrors/deferred_type_test: CompileTimeError, RuntimeError
121120
mirrors/empty_test: Crash, RuntimeError
122121
mirrors/enum_test: RuntimeError # Issue 31402 (Invocation arguments)
@@ -140,7 +139,6 @@ mirrors/library_imports_metadata_test: RuntimeError # Issue 33098
140139
mirrors/library_imports_prefixed_show_hide_test: RuntimeError # Issue 33098
141140
mirrors/library_imports_prefixed_test: RuntimeError # Issue 33098
142141
mirrors/library_imports_shown_test: RuntimeError # Issue 33098
143-
mirrors/library_metadata_test: RuntimeError
144142
mirrors/load_library_test: RuntimeError
145143
mirrors/metadata_allowed_values_test/16: Skip # Flaky, crashes.
146144
mirrors/metadata_scope_test/none: RuntimeError

0 commit comments

Comments
 (0)