Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: runtime/lib/mirrors.cc

Issue 20119002: Make LibraryMirror.members an internal native and lazy. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | runtime/lib/mirrors_impl.dart » ('j') | runtime/lib/mirrors_impl.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "include/dart_api.h" 5 #include "include/dart_api.h"
6 #include "include/dart_debugger_api.h" 6 #include "include/dart_debugger_api.h"
7 #include "include/dart_mirrors_api.h" 7 #include "include/dart_mirrors_api.h"
8 #include "vm/dart_api_impl.h" 8 #include "vm/dart_api_impl.h"
9 #include "vm/dart_api_state.h" // TODO(11742): Remove with CreateMirrorRef. 9 #include "vm/dart_api_state.h" // TODO(11742): Remove with CreateMirrorRef.
10 #include "vm/bootstrap_natives.h" 10 #include "vm/bootstrap_natives.h"
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 cls_name, 485 cls_name,
486 owner_mirror, 486 owner_mirror,
487 CreateLazyMirror(referent), 487 CreateLazyMirror(referent),
488 }; 488 };
489 Dart_Handle mirror = 489 Dart_Handle mirror =
490 Dart_New(mirror_type, Dart_Null(), ARRAY_SIZE(args), args); 490 Dart_New(mirror_type, Dart_Null(), ARRAY_SIZE(args), args);
491 return mirror; 491 return mirror;
492 } 492 }
493 493
494 494
495 static Dart_Handle CreateMemberMap(Dart_Handle owner, Dart_Handle owner_mirror);
496 static Dart_Handle CreateConstructorMap(Dart_Handle owner, 495 static Dart_Handle CreateConstructorMap(Dart_Handle owner,
497 Dart_Handle owner_mirror); 496 Dart_Handle owner_mirror);
498 497
499 static Dart_Handle CreateClassMirrorUsingApi(Dart_Handle intf, 498 static Dart_Handle CreateClassMirrorUsingApi(Dart_Handle intf,
500 Dart_Handle intf_name, 499 Dart_Handle intf_name,
501 Dart_Handle lib_mirror) { 500 Dart_Handle lib_mirror) {
502 ASSERT(Dart_IsClass(intf)); 501 ASSERT(Dart_IsClass(intf));
503 if (Dart_ClassIsTypedef(intf)) { 502 if (Dart_ClassIsTypedef(intf)) {
504 // This class is actually a typedef. Represent it specially in 503 // This class is actually a typedef. Represent it specially in
505 // reflection. 504 // reflection.
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
594 args.SetAt(1, name); 593 args.SetAt(1, name);
595 args.SetAt(2, owner_mirror); 594 args.SetAt(2, owner_mirror);
596 args.SetAt(3, Instance::Handle()); // Null for type. 595 args.SetAt(3, Instance::Handle()); // Null for type.
597 args.SetAt(4, field.is_static() ? Bool::True() : Bool::False()); 596 args.SetAt(4, field.is_static() ? Bool::True() : Bool::False());
598 args.SetAt(5, field.is_final() ? Bool::True() : Bool::False()); 597 args.SetAt(5, field.is_final() ? Bool::True() : Bool::False());
599 598
600 return CreateMirror(Symbols::_LocalVariableMirrorImpl(), args); 599 return CreateMirror(Symbols::_LocalVariableMirrorImpl(), args);
601 } 600 }
602 601
603 602
604 static Dart_Handle AddMemberClasses(Dart_Handle map,
605 Dart_Handle owner,
606 Dart_Handle owner_mirror) {
607 ASSERT(Dart_IsLibrary(owner));
608 Dart_Handle result;
609 Dart_Handle names = Dart_LibraryGetClassNames(owner);
610 if (Dart_IsError(names)) {
611 return names;
612 }
613 intptr_t len;
614 result = Dart_ListLength(names, &len);
615 if (Dart_IsError(result)) {
616 return result;
617 }
618 for (intptr_t i = 0; i < len; i++) {
619 Dart_Handle intf_name = Dart_ListGetAt(names, i);
620 Dart_Handle intf = Dart_GetClass(owner, intf_name);
621 if (Dart_IsError(intf)) {
622 return intf;
623 }
624 Dart_Handle intf_mirror =
625 CreateClassMirrorUsingApi(intf, intf_name, owner_mirror);
626 if (Dart_IsError(intf_mirror)) {
627 return intf_mirror;
628 }
629 result = MapAdd(map, intf_name, intf_mirror);
630 if (Dart_IsError(result)) {
631 return result;
632 }
633 }
634 return Dart_True();
635 }
636
637
638 static Dart_Handle AddMemberFunctions(Dart_Handle map,
639 Dart_Handle owner,
640 Dart_Handle owner_mirror) {
641 Dart_Handle result;
642 Dart_Handle names = Dart_GetFunctionNames(owner);
643 if (Dart_IsError(names)) {
644 return names;
645 }
646 intptr_t len;
647 result = Dart_ListLength(names, &len);
648 if (Dart_IsError(result)) {
649 return result;
650 }
651 for (intptr_t i = 0; i < len; i++) {
652 Dart_Handle func_name = Dart_ListGetAt(names, i);
653 Dart_Handle func = Dart_LookupFunction(owner, func_name);
654 if (Dart_IsError(func)) {
655 return func;
656 }
657 ASSERT(!Dart_IsNull(func));
658
659 bool is_constructor = false;
660 result = Dart_FunctionIsConstructor(func, &is_constructor);
661 if (Dart_IsError(result)) {
662 return result;
663 }
664 if (is_constructor) {
665 // Skip constructors.
666 continue;
667 }
668
669 Dart_Handle func_mirror = CreateMethodMirrorUsingApi(func, owner_mirror);
670 if (Dart_IsError(func_mirror)) {
671 return func_mirror;
672 }
673 result = MapAdd(map, func_name, func_mirror);
674 if (Dart_IsError(result)) {
675 return result;
676 }
677 }
678 return Dart_True();
679 }
680
681
682 static Dart_Handle AddConstructors(Dart_Handle map, 603 static Dart_Handle AddConstructors(Dart_Handle map,
683 Dart_Handle owner, 604 Dart_Handle owner,
684 Dart_Handle owner_mirror) { 605 Dart_Handle owner_mirror) {
685 Dart_Handle result; 606 Dart_Handle result;
686 Dart_Handle names = Dart_GetFunctionNames(owner); 607 Dart_Handle names = Dart_GetFunctionNames(owner);
687 if (Dart_IsError(names)) { 608 if (Dart_IsError(names)) {
688 return names; 609 return names;
689 } 610 }
690 intptr_t len; 611 intptr_t len;
691 result = Dart_ListLength(names, &len); 612 result = Dart_ListLength(names, &len);
(...skipping 24 matching lines...) Expand all
716 } 637 }
717 result = MapAdd(map, func_name, func_mirror); 638 result = MapAdd(map, func_name, func_mirror);
718 if (Dart_IsError(result)) { 639 if (Dart_IsError(result)) {
719 return result; 640 return result;
720 } 641 }
721 } 642 }
722 return Dart_True(); 643 return Dart_True();
723 } 644 }
724 645
725 646
726 static Dart_Handle AddMemberVariables(Dart_Handle map,
727 Dart_Handle owner,
728 Dart_Handle owner_mirror) {
729 Isolate* isolate = Isolate::Current();
730 Dart_Handle result;
731 Dart_Handle names = Dart_GetVariableNames(owner);
732 if (Dart_IsError(names)) {
733 return names;
734 }
735 intptr_t len;
736 result = Dart_ListLength(names, &len);
737 if (Dart_IsError(result)) {
738 return result;
739 }
740 for (intptr_t i = 0; i < len; i++) {
741 Dart_Handle var_name = Dart_ListGetAt(names, i);
742 Dart_Handle var = Dart_LookupVariable(owner, var_name);
743 if (Dart_IsError(var)) {
744 return var;
745 }
746 ASSERT(!Dart_IsNull(var));
747 ASSERT(Dart_IsVariable(var));
748 const Field& field = Api::UnwrapFieldHandle(isolate, var);
749 const Instance& owner_mirror_inst =
750 Api::UnwrapInstanceHandle(isolate, owner_mirror);
751 const Instance& var_mirror_inst =
752 Instance::Handle(CreateVariableMirror(field, owner_mirror_inst));
753 Dart_Handle var_mirror = Api::NewHandle(isolate, var_mirror_inst.raw());
754
755 result = MapAdd(map, var_name, var_mirror);
756 if (Dart_IsError(result)) {
757 return result;
758 }
759 }
760 return Dart_True();
761 }
762
763
764 static Dart_Handle CreateMemberMap(Dart_Handle owner,
765 Dart_Handle owner_mirror) {
766 // TODO(turnidge): This should be an immutable map.
767 if (Dart_IsError(owner_mirror)) {
768 return owner_mirror;
769 }
770 Dart_Handle result;
771 Dart_Handle map = MapNew();
772 if (Dart_IsLibrary(owner)) {
773 result = AddMemberClasses(map, owner, owner_mirror);
774 if (Dart_IsError(result)) {
775 return result;
776 }
777 }
778 result = AddMemberFunctions(map, owner, owner_mirror);
779 if (Dart_IsError(result)) {
780 return result;
781 }
782 result = AddMemberVariables(map, owner, owner_mirror);
783 if (Dart_IsError(result)) {
784 return result;
785 }
786 return map;
787 }
788
789
790 static Dart_Handle CreateConstructorMap(Dart_Handle owner, 647 static Dart_Handle CreateConstructorMap(Dart_Handle owner,
791 Dart_Handle owner_mirror) { 648 Dart_Handle owner_mirror) {
792 // TODO(turnidge): This should be an immutable map. 649 // TODO(turnidge): This should be an immutable map.
793 if (Dart_IsError(owner_mirror)) { 650 if (Dart_IsError(owner_mirror)) {
794 return owner_mirror; 651 return owner_mirror;
795 } 652 }
796 Dart_Handle result; 653 Dart_Handle result;
797 Dart_Handle map = MapNew(); 654 Dart_Handle map = MapNew();
798 result = AddConstructors(map, owner, owner_mirror); 655 result = AddConstructors(map, owner, owner_mirror);
799 if (Dart_IsError(result)) { 656 if (Dart_IsError(result)) {
800 return result; 657 return result;
801 } 658 }
802 return map; 659 return map;
803 } 660 }
804 661
805 662
806 static Dart_Handle CreateLibraryMirrorUsingApi(Dart_Handle lib) { 663 static Dart_Handle CreateLibraryMirrorUsingApi(Dart_Handle lib) {
807 Dart_Handle cls_name = NewString("_LocalLibraryMirrorImpl"); 664 Dart_Handle cls_name = NewString("_LocalLibraryMirrorImpl");
808 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); 665 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL);
809 if (Dart_IsError(type)) { 666 if (Dart_IsError(type)) {
810 return type; 667 return type;
811 } 668 }
812 Dart_Handle lazy_lib_mirror = CreateLazyMirror(lib); 669 Dart_Handle lazy_lib_mirror = CreateLazyMirror(lib);
813 if (Dart_IsError(lazy_lib_mirror)) { 670 if (Dart_IsError(lazy_lib_mirror)) {
814 return lazy_lib_mirror; 671 return lazy_lib_mirror;
815 } 672 }
816 Dart_Handle member_map = CreateMemberMap(lib, lazy_lib_mirror);
817 if (Dart_IsError(member_map)) {
818 return member_map;
819 }
820 Dart_Handle args[] = { 673 Dart_Handle args[] = {
821 CreateMirrorReference(lib), 674 CreateMirrorReference(lib),
822 Dart_LibraryName(lib), 675 Dart_LibraryName(lib),
823 Dart_LibraryUrl(lib), 676 Dart_LibraryUrl(lib),
824 member_map,
825 }; 677 };
826 Dart_Handle lib_mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); 678 Dart_Handle lib_mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args);
827 if (Dart_IsError(lib_mirror)) { 679 if (Dart_IsError(lib_mirror)) {
828 return lib_mirror; 680 return lib_mirror;
829 } 681 }
830 682
831 return lib_mirror; 683 return lib_mirror;
832 } 684 }
833 685
834 686
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after
1226 // initialized until the first time the getter is invoked. 1078 // initialized until the first time the getter is invoked.
1227 const Instance& value = Instance::Handle(field.value()); 1079 const Instance& value = Instance::Handle(field.value());
1228 ASSERT(value.raw() != Object::transition_sentinel().raw()); 1080 ASSERT(value.raw() != Object::transition_sentinel().raw());
1229 return value.raw() == Object::sentinel().raw(); 1081 return value.raw() == Object::sentinel().raw();
1230 } 1082 }
1231 1083
1232 1084
1233 DEFINE_NATIVE_ENTRY(ClassMirror_name, 1) { 1085 DEFINE_NATIVE_ENTRY(ClassMirror_name, 1) {
1234 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); 1086 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0));
1235 const Class& klass = Class::Handle(ref.GetClassReferent()); 1087 const Class& klass = Class::Handle(ref.GetClassReferent());
1236 return klass.Name(); 1088 return klass.UserVisibleName();
1237 } 1089 }
1238 1090
1239 1091
1240 DEFINE_NATIVE_ENTRY(ClassMirror_library, 1) { 1092 DEFINE_NATIVE_ENTRY(ClassMirror_library, 1) {
1241 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); 1093 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0));
1242 const Class& klass = Class::Handle(ref.GetClassReferent()); 1094 const Class& klass = Class::Handle(ref.GetClassReferent());
1243 return CreateLibraryMirror(Library::Handle(klass.library())); 1095 return CreateLibraryMirror(Library::Handle(klass.library()));
1244 } 1096 }
1245 1097
1246 1098
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1282 func.kind() == RawFunction::kSetterFunction) { 1134 func.kind() == RawFunction::kSetterFunction) {
1283 member_mirror = CreateMethodMirror(func, owner_mirror); 1135 member_mirror = CreateMethodMirror(func, owner_mirror);
1284 member_mirrors.Add(member_mirror); 1136 member_mirrors.Add(member_mirror);
1285 } 1137 }
1286 } 1138 }
1287 1139
1288 return member_mirrors.raw(); 1140 return member_mirrors.raw();
1289 } 1141 }
1290 1142
1291 1143
1144 DEFINE_NATIVE_ENTRY(LibraryMirror_members, 2) {
1145 GET_NON_NULL_NATIVE_ARGUMENT(Instance,
1146 owner_mirror,
1147 arguments->NativeArgAt(0));
1148 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1));
1149 const Library& library = Library::Handle(ref.GetLibraryReferent());
1150
1151 Instance& member_mirror = Instance::Handle();
1152 const GrowableObjectArray& member_mirrors =
1153 GrowableObjectArray::Handle(GrowableObjectArray::New());
1154
1155 Object& entry = Object::Handle();
1156 DictionaryIterator entries(library);
1157
1158 while (entries.HasNext()) {
1159 entry = entries.GetNext();
1160 if (entry.IsClass()) {
1161 const Class& klass = Class::Cast(entry);
1162 if (!klass.IsCanonicalSignatureClass()) {
1163 // The various implementations of public classes don't always have the
1164 // expected superinterfaces or other properties, so we filter them out.
1165 if (!RawObject::IsImplementationClassId(klass.id())) {
1166 member_mirror = CreateClassMirror(klass, owner_mirror);
1167 member_mirrors.Add(member_mirror);
1168 }
1169 }
1170 } else if (entry.IsField()) {
1171 const Field& field = Field::Cast(entry);
1172 member_mirror = CreateVariableMirror(field, owner_mirror);
1173 member_mirrors.Add(member_mirror);
1174 } else if (entry.IsFunction()) {
1175 const Function& func = Function::Cast(entry);
1176 if (func.kind() == RawFunction::kRegularFunction ||
1177 func.kind() == RawFunction::kGetterFunction ||
1178 func.kind() == RawFunction::kSetterFunction) {
1179 member_mirror = CreateMethodMirror(func, owner_mirror);
1180 member_mirrors.Add(member_mirror);
1181 }
1182 }
1183 }
1184
1185 return member_mirrors.raw();
1186 }
1187
1188
1292 // Invoke the function, or noSuchMethod if it is null. Propagate any unhandled 1189 // Invoke the function, or noSuchMethod if it is null. Propagate any unhandled
1293 // exceptions. Wrap and propagate any compilation errors. 1190 // exceptions. Wrap and propagate any compilation errors.
1294 static RawObject* ReflectivelyInvokeDynamicFunction(const Instance& receiver, 1191 static RawObject* ReflectivelyInvokeDynamicFunction(const Instance& receiver,
1295 const Function& function, 1192 const Function& function,
1296 const String& target_name, 1193 const String& target_name,
1297 const Array& arguments) { 1194 const Array& arguments) {
1298 // Note "arguments" is already the internal arguments with the receiver as 1195 // Note "arguments" is already the internal arguments with the receiver as
1299 // the first element. 1196 // the first element.
1300 Object& result = Object::Handle(); 1197 Object& result = Object::Handle();
1301 if (function.IsNull()) { 1198 if (function.IsNull()) {
(...skipping 610 matching lines...) Expand 10 before | Expand all | Expand 10 after
1912 1809
1913 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { 1810 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) {
1914 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); 1811 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0));
1915 const Field& field = Field::Handle(ref.GetFieldReferent()); 1812 const Field& field = Field::Handle(ref.GetFieldReferent());
1916 1813
1917 const AbstractType& type = AbstractType::Handle(field.type()); 1814 const AbstractType& type = AbstractType::Handle(field.type());
1918 return CreateTypeMirror(type); 1815 return CreateTypeMirror(type);
1919 } 1816 }
1920 1817
1921 } // namespace dart 1818 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/lib/mirrors_impl.dart » ('j') | runtime/lib/mirrors_impl.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698