| OLD | NEW |
| 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" |
| 11 #include "vm/dart_entry.h" | 11 #include "vm/dart_entry.h" |
| 12 #include "vm/exceptions.h" | 12 #include "vm/exceptions.h" |
| 13 #include "vm/message.h" | 13 #include "vm/message.h" |
| 14 #include "vm/object_store.h" |
| 14 #include "vm/port.h" | 15 #include "vm/port.h" |
| 15 #include "vm/resolver.h" | 16 #include "vm/resolver.h" |
| 16 #include "vm/symbols.h" | 17 #include "vm/symbols.h" |
| 17 #include "lib/invocation_mirror.h" | 18 #include "lib/invocation_mirror.h" |
| 18 | 19 |
| 19 namespace dart { | 20 namespace dart { |
| 20 | 21 |
| 21 static RawInstance* CreateMirror(const String& mirror_class_name, | 22 static RawInstance* CreateMirror(const String& mirror_class_name, |
| 22 const Array& constructor_arguments) { | 23 const Array& constructor_arguments) { |
| 23 const Library& mirrors_lib = Library::Handle(Library::MirrorsLibrary()); | 24 const Library& mirrors_lib = Library::Handle(Library::MirrorsLibrary()); |
| (...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 599 }; | 600 }; |
| 600 Dart_Handle lib_mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | 601 Dart_Handle lib_mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); |
| 601 if (Dart_IsError(lib_mirror)) { | 602 if (Dart_IsError(lib_mirror)) { |
| 602 return lib_mirror; | 603 return lib_mirror; |
| 603 } | 604 } |
| 604 | 605 |
| 605 return lib_mirror; | 606 return lib_mirror; |
| 606 } | 607 } |
| 607 | 608 |
| 608 | 609 |
| 609 static Dart_Handle CreateLibrariesMap() { | |
| 610 // TODO(turnidge): This should be an immutable map. | |
| 611 Dart_Handle map = MapNew(); | |
| 612 | |
| 613 Dart_Handle lib_ids = Dart_GetLibraryIds(); | |
| 614 if (Dart_IsError(lib_ids)) { | |
| 615 return lib_ids; | |
| 616 } | |
| 617 intptr_t len; | |
| 618 Dart_Handle result = Dart_ListLength(lib_ids, &len); | |
| 619 if (Dart_IsError(result)) { | |
| 620 return result; | |
| 621 } | |
| 622 for (intptr_t i = 0; i < len; i++) { | |
| 623 Dart_Handle lib_id = Dart_ListGetAt(lib_ids, i); | |
| 624 int64_t id64; | |
| 625 Dart_IntegerToInt64(lib_id, &id64); | |
| 626 Dart_Handle lib_url = Dart_GetLibraryURL(id64); | |
| 627 if (Dart_IsError(lib_url)) { | |
| 628 return lib_url; | |
| 629 } | |
| 630 Dart_Handle lib = Dart_LookupLibrary(lib_url); | |
| 631 if (Dart_IsError(lib)) { | |
| 632 return lib; | |
| 633 } | |
| 634 Dart_Handle lib_mirror = CreateLibraryMirrorUsingApi(lib); | |
| 635 if (Dart_IsError(lib_mirror)) { | |
| 636 return lib_mirror; | |
| 637 } | |
| 638 result = MapAdd(map, lib_url, lib_mirror); | |
| 639 } | |
| 640 return map; | |
| 641 } | |
| 642 | |
| 643 | |
| 644 static Dart_Handle CreateIsolateMirror() { | |
| 645 Dart_Handle cls_name = NewString("_LocalIsolateMirrorImpl"); | |
| 646 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | |
| 647 if (Dart_IsError(type)) { | |
| 648 return type; | |
| 649 } | |
| 650 Dart_Handle args[] = { | |
| 651 Dart_DebugName(), | |
| 652 CreateLazyMirror(Dart_RootLibrary()), | |
| 653 }; | |
| 654 return Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | |
| 655 } | |
| 656 | |
| 657 | |
| 658 static Dart_Handle CreateMirrorSystem() { | |
| 659 Dart_Handle cls_name = NewString("_LocalMirrorSystemImpl"); | |
| 660 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | |
| 661 if (Dart_IsError(type)) { | |
| 662 return type; | |
| 663 } | |
| 664 | |
| 665 Dart_Handle libraries = CreateLibrariesMap(); | |
| 666 if (Dart_IsError(libraries)) { | |
| 667 return libraries; | |
| 668 } | |
| 669 | |
| 670 Dart_Handle args[] = { | |
| 671 libraries, | |
| 672 CreateIsolateMirror(), | |
| 673 }; | |
| 674 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | |
| 675 if (Dart_IsError(mirror)) { | |
| 676 return mirror; | |
| 677 } | |
| 678 | |
| 679 return mirror; | |
| 680 } | |
| 681 | |
| 682 | |
| 683 static Dart_Handle CreateNullMirror() { | 610 static Dart_Handle CreateNullMirror() { |
| 684 Dart_Handle cls_name = NewString("_LocalInstanceMirrorImpl"); | 611 Dart_Handle cls_name = NewString("_LocalInstanceMirrorImpl"); |
| 685 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | 612 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); |
| 686 if (Dart_IsError(type)) { | 613 if (Dart_IsError(type)) { |
| 687 return type; | 614 return type; |
| 688 } | 615 } |
| 689 | 616 |
| 690 // TODO(turnidge): This is wrong. The Null class is distinct from object. | 617 // TODO(turnidge): This is wrong. The Null class is distinct from object. |
| 691 Dart_Handle object_class = Dart_GetClass(CoreLib(), NewString("Object")); | 618 Dart_Handle object_class = Dart_GetClass(CoreLib(), NewString("Object")); |
| 692 | 619 |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 822 return CreateClassMirror(cls, Object::null_instance()); | 749 return CreateClassMirror(cls, Object::null_instance()); |
| 823 } else if (type.IsTypeParameter()) { | 750 } else if (type.IsTypeParameter()) { |
| 824 return CreateTypeVariableMirror(TypeParameter::Cast(type), | 751 return CreateTypeVariableMirror(TypeParameter::Cast(type), |
| 825 Object::null_instance()); | 752 Object::null_instance()); |
| 826 } | 753 } |
| 827 UNREACHABLE(); | 754 UNREACHABLE(); |
| 828 return Instance::null(); | 755 return Instance::null(); |
| 829 } | 756 } |
| 830 | 757 |
| 831 | 758 |
| 832 void NATIVE_ENTRY_FUNCTION(Mirrors_makeLocalMirrorSystem)( | 759 static RawInstance* CreateIsolateMirror() { |
| 833 Dart_NativeArguments args) { | 760 Isolate* isolate = Isolate::Current(); |
| 834 Dart_EnterScope(); | 761 const String& debug_name = String::Handle(String::New(isolate->name())); |
| 835 Dart_Handle mirrors = CreateMirrorSystem(); | 762 const Library& root_library = |
| 836 if (Dart_IsError(mirrors)) { | 763 Library::Handle(isolate, isolate->object_store()->root_library()); |
| 837 Dart_PropagateError(mirrors); | 764 const Instance& root_library_mirror = |
| 765 Instance::Handle(CreateLibraryMirror(root_library)); |
| 766 |
| 767 const Array& args = Array::Handle(Array::New(2)); |
| 768 args.SetAt(0, debug_name); |
| 769 args.SetAt(1, root_library_mirror); |
| 770 return CreateMirror(Symbols::_LocalIsolateMirrorImpl(), args); |
| 771 } |
| 772 |
| 773 |
| 774 static RawInstance* CreateMirrorSystem() { |
| 775 Isolate* isolate = Isolate::Current(); |
| 776 const GrowableObjectArray& libraries = |
| 777 GrowableObjectArray::Handle(isolate->object_store()->libraries()); |
| 778 |
| 779 const int num_libraries = libraries.Length(); |
| 780 const Array& library_mirrors = Array::Handle(Array::New(num_libraries)); |
| 781 Library& library = Library::Handle(); |
| 782 Instance& library_mirror = Instance::Handle(); |
| 783 |
| 784 for (int i = 0; i < num_libraries; i++) { |
| 785 library ^= libraries.At(i); |
| 786 library_mirror = CreateLibraryMirror(library); |
| 787 library_mirrors.SetAt(i, library_mirror); |
| 838 } | 788 } |
| 839 Dart_SetReturnValue(args, mirrors); | 789 |
| 840 Dart_ExitScope(); | 790 const Instance& isolate_mirror = Instance::Handle(CreateIsolateMirror()); |
| 791 |
| 792 const Array& args = Array::Handle(Array::New(2)); |
| 793 args.SetAt(0, library_mirrors); |
| 794 args.SetAt(1, isolate_mirror); |
| 795 return CreateMirror(Symbols::_LocalMirrorSystemImpl(), args); |
| 796 } |
| 797 |
| 798 |
| 799 DEFINE_NATIVE_ENTRY(Mirrors_makeLocalMirrorSystem, 0) { |
| 800 return CreateMirrorSystem(); |
| 841 } | 801 } |
| 842 | 802 |
| 843 | 803 |
| 844 void NATIVE_ENTRY_FUNCTION(Mirrors_makeLocalInstanceMirror)( | 804 void NATIVE_ENTRY_FUNCTION(Mirrors_makeLocalInstanceMirror)( |
| 845 Dart_NativeArguments args) { | 805 Dart_NativeArguments args) { |
| 846 Dart_EnterScope(); | 806 Dart_EnterScope(); |
| 847 Dart_Handle reflectee = Dart_GetNativeArgument(args, 0); | 807 Dart_Handle reflectee = Dart_GetNativeArgument(args, 0); |
| 848 Dart_Handle mirror = CreateInstanceMirror(reflectee); | 808 Dart_Handle mirror = CreateInstanceMirror(reflectee); |
| 849 if (Dart_IsError(mirror)) { | 809 if (Dart_IsError(mirror)) { |
| 850 Dart_PropagateError(mirror); | 810 Dart_PropagateError(mirror); |
| (...skipping 879 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1730 | 1690 |
| 1731 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { | 1691 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { |
| 1732 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); | 1692 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); |
| 1733 const Field& field = Field::Handle(ref.GetFieldReferent()); | 1693 const Field& field = Field::Handle(ref.GetFieldReferent()); |
| 1734 | 1694 |
| 1735 const AbstractType& type = AbstractType::Handle(field.type()); | 1695 const AbstractType& type = AbstractType::Handle(field.type()); |
| 1736 return CreateTypeMirror(type); | 1696 return CreateTypeMirror(type); |
| 1737 } | 1697 } |
| 1738 | 1698 |
| 1739 } // namespace dart | 1699 } // namespace dart |
| OLD | NEW |