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

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

Issue 20564002: Make ClassMirror.constructors an internal native and lazy. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 4 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') | no next file with comments »
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 CreateConstructorMap(Dart_Handle owner,
496 Dart_Handle owner_mirror);
497
498 static Dart_Handle CreateClassMirrorUsingApi(Dart_Handle intf, 495 static Dart_Handle CreateClassMirrorUsingApi(Dart_Handle intf,
499 Dart_Handle intf_name, 496 Dart_Handle intf_name,
500 Dart_Handle lib_mirror) { 497 Dart_Handle lib_mirror) {
501 ASSERT(Dart_IsClass(intf)); 498 ASSERT(Dart_IsClass(intf));
502 if (Dart_ClassIsTypedef(intf)) { 499 if (Dart_ClassIsTypedef(intf)) {
503 // This class is actually a typedef. Represent it specially in 500 // This class is actually a typedef. Represent it specially in
504 // reflection. 501 // reflection.
505 return CreateTypedefMirror(intf, intf_name, lib_mirror); 502 return CreateTypedefMirror(intf, intf_name, lib_mirror);
506 } 503 }
507 504
508 Dart_Handle cls_name = NewString("_LocalClassMirrorImpl"); 505 Dart_Handle cls_name = NewString("_LocalClassMirrorImpl");
509 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); 506 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL);
510 if (Dart_IsError(type)) { 507 if (Dart_IsError(type)) {
511 return type; 508 return type;
512 } 509 }
513 510
514 Dart_Handle super_class = Dart_Null(); 511 Dart_Handle super_class = Dart_Null();
515 // TODO(turnidge): Simplify code, now that default classes have been removed. 512 // TODO(turnidge): Simplify code, now that default classes have been removed.
516 Dart_Handle default_class = Dart_Null(); 513 Dart_Handle default_class = Dart_Null();
517 514
518 Dart_Handle intf_mirror = CreateLazyMirror(intf); 515 Dart_Handle intf_mirror = CreateLazyMirror(intf);
519 if (Dart_IsError(intf_mirror)) { 516 if (Dart_IsError(intf_mirror)) {
520 return intf_mirror; 517 return intf_mirror;
521 } 518 }
522 Dart_Handle constructor_map = CreateConstructorMap(intf, intf_mirror);
523 if (Dart_IsError(constructor_map)) {
524 return constructor_map;
525 }
526 Dart_Handle type_var_map = CreateTypeVariableMap(intf, intf_mirror); 519 Dart_Handle type_var_map = CreateTypeVariableMap(intf, intf_mirror);
527 if (Dart_IsError(type_var_map)) { 520 if (Dart_IsError(type_var_map)) {
528 return type_var_map; 521 return type_var_map;
529 } 522 }
530 523
531 Dart_Handle args[] = { 524 Dart_Handle args[] = {
532 CreateMirrorReference(intf), 525 CreateMirrorReference(intf),
533 Dart_Null(), // "name" 526 Dart_Null(), // "name"
534 Dart_NewBoolean(Dart_IsClass(intf)), 527 Dart_NewBoolean(Dart_IsClass(intf)),
535 lib_mirror, 528 lib_mirror,
536 super_class, 529 super_class,
537 CreateImplementsList(intf), 530 CreateImplementsList(intf),
538 CreateLazyMirror(default_class), 531 CreateLazyMirror(default_class),
539 constructor_map,
540 type_var_map, 532 type_var_map,
541 }; 533 };
542 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); 534 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args);
543 return mirror; 535 return mirror;
544 } 536 }
545 537
546 538
547 static RawInstance* CreateMethodMirror(const Function& func, 539 static RawInstance* CreateMethodMirror(const Function& func,
548 const Instance& owner_mirror) { 540 const Instance& owner_mirror) {
549 const Array& args = Array::Handle(Array::New(11)); 541 const Array& args = Array::Handle(Array::New(11));
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
583 args.SetAt(1, name); 575 args.SetAt(1, name);
584 args.SetAt(2, owner_mirror); 576 args.SetAt(2, owner_mirror);
585 args.SetAt(3, Instance::Handle()); // Null for type. 577 args.SetAt(3, Instance::Handle()); // Null for type.
586 args.SetAt(4, field.is_static() ? Bool::True() : Bool::False()); 578 args.SetAt(4, field.is_static() ? Bool::True() : Bool::False());
587 args.SetAt(5, field.is_final() ? Bool::True() : Bool::False()); 579 args.SetAt(5, field.is_final() ? Bool::True() : Bool::False());
588 580
589 return CreateMirror(Symbols::_LocalVariableMirrorImpl(), args); 581 return CreateMirror(Symbols::_LocalVariableMirrorImpl(), args);
590 } 582 }
591 583
592 584
593 static Dart_Handle AddConstructors(Dart_Handle map,
594 Dart_Handle owner,
595 Dart_Handle owner_mirror) {
596 Dart_Handle result;
597 Dart_Handle names = Dart_GetFunctionNames(owner);
598 if (Dart_IsError(names)) {
599 return names;
600 }
601 intptr_t len;
602 result = Dart_ListLength(names, &len);
603 if (Dart_IsError(result)) {
604 return result;
605 }
606 for (intptr_t i = 0; i < len; i++) {
607 Dart_Handle func_name = Dart_ListGetAt(names, i);
608 Dart_Handle func = Dart_LookupFunction(owner, func_name);
609 if (Dart_IsError(func)) {
610 return func;
611 }
612 ASSERT(!Dart_IsNull(func));
613
614 bool is_constructor = false;
615 result = Dart_FunctionIsConstructor(func, &is_constructor);
616 if (Dart_IsError(result)) {
617 return result;
618 }
619 if (!is_constructor) {
620 // Skip non-constructors.
621 continue;
622 }
623
624 Dart_Handle func_mirror = CreateMethodMirrorUsingApi(func, owner_mirror);
625 if (Dart_IsError(func_mirror)) {
626 return func_mirror;
627 }
628 result = MapAdd(map, func_name, func_mirror);
629 if (Dart_IsError(result)) {
630 return result;
631 }
632 }
633 return Dart_True();
634 }
635
636
637 static Dart_Handle CreateConstructorMap(Dart_Handle owner,
638 Dart_Handle owner_mirror) {
639 // TODO(turnidge): This should be an immutable map.
640 if (Dart_IsError(owner_mirror)) {
641 return owner_mirror;
642 }
643 Dart_Handle result;
644 Dart_Handle map = MapNew();
645 result = AddConstructors(map, owner, owner_mirror);
646 if (Dart_IsError(result)) {
647 return result;
648 }
649 return map;
650 }
651
652
653 static Dart_Handle CreateLibraryMirrorUsingApi(Dart_Handle lib) { 585 static Dart_Handle CreateLibraryMirrorUsingApi(Dart_Handle lib) {
654 Dart_Handle cls_name = NewString("_LocalLibraryMirrorImpl"); 586 Dart_Handle cls_name = NewString("_LocalLibraryMirrorImpl");
655 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); 587 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL);
656 if (Dart_IsError(type)) { 588 if (Dart_IsError(type)) {
657 return type; 589 return type;
658 } 590 }
659 Dart_Handle lazy_lib_mirror = CreateLazyMirror(lib); 591 Dart_Handle lazy_lib_mirror = CreateLazyMirror(lib);
660 if (Dart_IsError(lazy_lib_mirror)) { 592 if (Dart_IsError(lazy_lib_mirror)) {
661 return lazy_lib_mirror; 593 return lazy_lib_mirror;
662 } 594 }
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after
1038 return klass.super_type(); 970 return klass.super_type();
1039 } 971 }
1040 972
1041 DEFINE_NATIVE_ENTRY(ClassMirror_members, 2) { 973 DEFINE_NATIVE_ENTRY(ClassMirror_members, 2) {
1042 GET_NON_NULL_NATIVE_ARGUMENT(Instance, 974 GET_NON_NULL_NATIVE_ARGUMENT(Instance,
1043 owner_mirror, 975 owner_mirror,
1044 arguments->NativeArgAt(0)); 976 arguments->NativeArgAt(0));
1045 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1)); 977 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1));
1046 const Class& klass = Class::Handle(ref.GetClassReferent()); 978 const Class& klass = Class::Handle(ref.GetClassReferent());
1047 979
980 const Error& error = Error::Handle(klass.EnsureIsFinalized(isolate));
rmacnak 2013/07/26 00:44:28 If one reflects on the class before calling any of
981 if (!error.IsNull()) {
982 ThrowInvokeError(error);
983 }
984
1048 const Array& fields = Array::Handle(klass.fields()); 985 const Array& fields = Array::Handle(klass.fields());
1049 // Some special types like 'dynamic' have a null fields list, but they should 986 // Some special types like 'dynamic' have a null fields list, but they should
1050 // not wind up as the reflectees of ClassMirrors. 987 // not wind up as the reflectees of ClassMirrors.
1051 ASSERT(!fields.IsNull()); 988 ASSERT(!fields.IsNull());
1052 const intptr_t num_fields = fields.Length(); 989 const intptr_t num_fields = fields.Length();
1053 990
1054 const Array& functions = Array::Handle(klass.functions()); 991 const Array& functions = Array::Handle(klass.functions());
1055 // Some special types like 'dynamic' have a null functions list, but they 992 // Some special types like 'dynamic' have a null functions list, but they
1056 // should not wind up as the reflectees of ClassMirrors. 993 // should not wind up as the reflectees of ClassMirrors.
1057 ASSERT(!functions.IsNull()); 994 ASSERT(!functions.IsNull());
(...skipping 18 matching lines...) Expand all
1076 func.kind() == RawFunction::kSetterFunction) { 1013 func.kind() == RawFunction::kSetterFunction) {
1077 member_mirror = CreateMethodMirror(func, owner_mirror); 1014 member_mirror = CreateMethodMirror(func, owner_mirror);
1078 member_mirrors.Add(member_mirror); 1015 member_mirrors.Add(member_mirror);
1079 } 1016 }
1080 } 1017 }
1081 1018
1082 return member_mirrors.raw(); 1019 return member_mirrors.raw();
1083 } 1020 }
1084 1021
1085 1022
1023 DEFINE_NATIVE_ENTRY(ClassMirror_constructors, 2) {
1024 GET_NON_NULL_NATIVE_ARGUMENT(Instance,
1025 owner_mirror,
1026 arguments->NativeArgAt(0));
1027 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1));
1028 const Class& klass = Class::Handle(ref.GetClassReferent());
1029
1030 const Error& error = Error::Handle(klass.EnsureIsFinalized(isolate));
1031 if (!error.IsNull()) {
1032 ThrowInvokeError(error);
1033 }
1034
1035 const Array& functions = Array::Handle(klass.functions());
1036 // Some special types like 'dynamic' have a null functions list, but they
1037 // should not wind up as the reflectees of ClassMirrors.
siva 2013/07/26 17:16:01 Can you file a bug for the case of not being able
1038 ASSERT(!functions.IsNull());
1039 const intptr_t num_functions = functions.Length();
1040
1041 Instance& constructor_mirror = Instance::Handle();
1042 const GrowableObjectArray& constructor_mirrors = GrowableObjectArray::Handle(
1043 GrowableObjectArray::New(num_functions));
siva 2013/07/26 17:16:01 I still think this should be a plain array and not
1044
1045 Function& func = Function::Handle();
1046 for (intptr_t i = 0; i < num_functions; i++) {
1047 func ^= functions.At(i);
1048 if (func.kind() == RawFunction::kConstructor) {
1049 constructor_mirror = CreateMethodMirror(func, owner_mirror);
1050 constructor_mirrors.Add(constructor_mirror);
1051 }
1052 }
1053
1054 return constructor_mirrors.raw();
1055 }
1056
1057
1086 DEFINE_NATIVE_ENTRY(LibraryMirror_members, 2) { 1058 DEFINE_NATIVE_ENTRY(LibraryMirror_members, 2) {
1087 GET_NON_NULL_NATIVE_ARGUMENT(Instance, 1059 GET_NON_NULL_NATIVE_ARGUMENT(Instance,
1088 owner_mirror, 1060 owner_mirror,
1089 arguments->NativeArgAt(0)); 1061 arguments->NativeArgAt(0));
1090 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1)); 1062 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1));
1091 const Library& library = Library::Handle(ref.GetLibraryReferent()); 1063 const Library& library = Library::Handle(ref.GetLibraryReferent());
1092 1064
1093 Instance& member_mirror = Instance::Handle(); 1065 Instance& member_mirror = Instance::Handle();
1094 const GrowableObjectArray& member_mirrors = 1066 const GrowableObjectArray& member_mirrors =
1095 GrowableObjectArray::Handle(GrowableObjectArray::New()); 1067 GrowableObjectArray::Handle(GrowableObjectArray::New());
(...skipping 662 matching lines...) Expand 10 before | Expand all | Expand 10 after
1758 1730
1759 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { 1731 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) {
1760 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); 1732 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0));
1761 const Field& field = Field::Handle(ref.GetFieldReferent()); 1733 const Field& field = Field::Handle(ref.GetFieldReferent());
1762 1734
1763 const AbstractType& type = AbstractType::Handle(field.type()); 1735 const AbstractType& type = AbstractType::Handle(field.type());
1764 return CreateTypeMirror(type); 1736 return CreateTypeMirror(type);
1765 } 1737 }
1766 1738
1767 } // namespace dart 1739 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/lib/mirrors_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698