| 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" |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 return type; | 89 return type; |
| 90 } | 90 } |
| 91 Dart_Handle result = Dart_ObjectIsType(object, type, is_mirror); | 91 Dart_Handle result = Dart_ObjectIsType(object, type, is_mirror); |
| 92 if (Dart_IsError(result)) { | 92 if (Dart_IsError(result)) { |
| 93 return result; | 93 return result; |
| 94 } | 94 } |
| 95 return Dart_True(); // Indicates success. Result is in is_mirror. | 95 return Dart_True(); // Indicates success. Result is in is_mirror. |
| 96 } | 96 } |
| 97 | 97 |
| 98 | 98 |
| 99 static void FreeVMReference(Dart_WeakPersistentHandle weak_ref, void* data) { | |
| 100 Dart_PersistentHandle perm_handle = | |
| 101 reinterpret_cast<Dart_PersistentHandle>(data); | |
| 102 Dart_DeletePersistentHandle(perm_handle); | |
| 103 Dart_DeleteWeakPersistentHandle(weak_ref); | |
| 104 } | |
| 105 | |
| 106 | |
| 107 static Dart_Handle CreateVMReference(Dart_Handle handle) { | |
| 108 // Create the VMReference object. | |
| 109 Dart_Handle cls_name = NewString("VMReference"); | |
| 110 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | |
| 111 if (Dart_IsError(type)) { | |
| 112 return type; | |
| 113 } | |
| 114 Dart_Handle vm_ref = Dart_New(type, Dart_Null(), 0, NULL); | |
| 115 if (Dart_IsError(vm_ref)) { | |
| 116 return vm_ref; | |
| 117 } | |
| 118 | |
| 119 // Allocate a persistent handle. | |
| 120 Dart_PersistentHandle perm_handle = Dart_NewPersistentHandle(handle); | |
| 121 ASSERT(perm_handle != NULL); | |
| 122 | |
| 123 // Store the persistent handle in the VMReference. | |
| 124 intptr_t perm_handle_value = reinterpret_cast<intptr_t>(perm_handle); | |
| 125 Dart_Handle result = | |
| 126 Dart_SetNativeInstanceField(vm_ref, 0, perm_handle_value); | |
| 127 if (Dart_IsError(result)) { | |
| 128 Dart_DeletePersistentHandle(perm_handle); | |
| 129 return result; | |
| 130 } | |
| 131 | |
| 132 // Create a weak reference. We use the callback to be informed when | |
| 133 // the VMReference is collected, so we can release the persistent | |
| 134 // handle. | |
| 135 void* perm_handle_data = reinterpret_cast<void*>(perm_handle); | |
| 136 Dart_WeakPersistentHandle weak_ref = | |
| 137 Dart_NewWeakPersistentHandle(vm_ref, perm_handle_data, FreeVMReference); | |
| 138 ASSERT(weak_ref != NULL); | |
| 139 | |
| 140 // Success. | |
| 141 return vm_ref; | |
| 142 } | |
| 143 | |
| 144 | |
| 145 // TODO(11742): Remove once there are no more users of the Dart_Handle-based | 99 // TODO(11742): Remove once there are no more users of the Dart_Handle-based |
| 146 // VMReferences. | 100 // VMReferences. |
| 147 static Dart_Handle CreateMirrorReference(Dart_Handle handle) { | 101 static Dart_Handle CreateMirrorReference(Dart_Handle handle) { |
| 148 Isolate* isolate = Isolate::Current(); | 102 Isolate* isolate = Isolate::Current(); |
| 149 DARTSCOPE(isolate); | 103 DARTSCOPE(isolate); |
| 150 const Object& referent = Object::Handle(isolate, Api::UnwrapHandle(handle)); | 104 const Object& referent = Object::Handle(isolate, Api::UnwrapHandle(handle)); |
| 151 const MirrorReference& reference = | 105 const MirrorReference& reference = |
| 152 MirrorReference::Handle(MirrorReference::New()); | 106 MirrorReference::Handle(MirrorReference::New()); |
| 153 reference.set_referent(referent); | 107 reference.set_referent(referent); |
| 154 return Api::NewHandle(isolate, reference.raw()); | 108 return Api::NewHandle(isolate, reference.raw()); |
| (...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 if (Dart_IsError(constructor_map)) { | 488 if (Dart_IsError(constructor_map)) { |
| 535 return constructor_map; | 489 return constructor_map; |
| 536 } | 490 } |
| 537 Dart_Handle type_var_map = CreateTypeVariableMap(intf, intf_mirror); | 491 Dart_Handle type_var_map = CreateTypeVariableMap(intf, intf_mirror); |
| 538 if (Dart_IsError(type_var_map)) { | 492 if (Dart_IsError(type_var_map)) { |
| 539 return type_var_map; | 493 return type_var_map; |
| 540 } | 494 } |
| 541 | 495 |
| 542 Dart_Handle args[] = { | 496 Dart_Handle args[] = { |
| 543 CreateMirrorReference(intf), | 497 CreateMirrorReference(intf), |
| 544 CreateVMReference(intf), | |
| 545 Dart_Null(), // "name" | 498 Dart_Null(), // "name" |
| 546 Dart_NewBoolean(Dart_IsClass(intf)), | 499 Dart_NewBoolean(Dart_IsClass(intf)), |
| 547 lib_mirror, | 500 lib_mirror, |
| 548 CreateLazyMirror(super_class), | 501 CreateLazyMirror(super_class), |
| 549 CreateImplementsList(intf), | 502 CreateImplementsList(intf), |
| 550 CreateLazyMirror(default_class), | 503 CreateLazyMirror(default_class), |
| 551 member_map, | 504 member_map, |
| 552 constructor_map, | 505 constructor_map, |
| 553 type_var_map, | 506 type_var_map, |
| 554 }; | 507 }; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 592 Api::False() | 545 Api::False() |
| 593 }; | 546 }; |
| 594 Dart_Handle mirror = | 547 Dart_Handle mirror = |
| 595 Dart_New(mirror_type, Dart_Null(), ARRAY_SIZE(args), args); | 548 Dart_New(mirror_type, Dart_Null(), ARRAY_SIZE(args), args); |
| 596 return mirror; | 549 return mirror; |
| 597 } | 550 } |
| 598 | 551 |
| 599 | 552 |
| 600 static Dart_Handle CreateVariableMirror(Dart_Handle var, | 553 static Dart_Handle CreateVariableMirror(Dart_Handle var, |
| 601 Dart_Handle var_name, | 554 Dart_Handle var_name, |
| 602 Dart_Handle lib_mirror) { | 555 Dart_Handle owner_mirror) { |
| 603 ASSERT(Dart_IsVariable(var)); | 556 ASSERT(Dart_IsVariable(var)); |
| 604 Dart_Handle cls_name = NewString("_LocalVariableMirrorImpl"); | 557 Dart_Handle cls_name = NewString("_LocalVariableMirrorImpl"); |
| 605 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | 558 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); |
| 606 if (Dart_IsError(type)) { | 559 if (Dart_IsError(type)) { |
| 607 return type; | 560 return type; |
| 608 } | 561 } |
| 609 | 562 |
| 610 bool is_static = false; | 563 bool is_static = false; |
| 611 bool is_final = false; | 564 bool is_final = false; |
| 612 | 565 |
| 613 Dart_Handle result = Dart_VariableIsStatic(var, &is_static); | 566 Dart_Handle result = Dart_VariableIsStatic(var, &is_static); |
| 614 if (Dart_IsError(result)) { | 567 if (Dart_IsError(result)) { |
| 615 return result; | 568 return result; |
| 616 } | 569 } |
| 617 result = Dart_VariableIsFinal(var, &is_final); | 570 result = Dart_VariableIsFinal(var, &is_final); |
| 618 if (Dart_IsError(result)) { | 571 if (Dart_IsError(result)) { |
| 619 return result; | 572 return result; |
| 620 } | 573 } |
| 621 | 574 |
| 622 Dart_Handle var_type = Dart_VariableType(var); | 575 Dart_Handle var_type = Dart_VariableType(var); |
| 623 if (Dart_IsError(var_type)) { | 576 if (Dart_IsError(var_type)) { |
| 624 return var_type; | 577 return var_type; |
| 625 } | 578 } |
| 626 | 579 |
| 627 Dart_Handle args[] = { | 580 Dart_Handle args[] = { |
| 581 CreateMirrorReference(var), |
| 628 var_name, | 582 var_name, |
| 629 lib_mirror, | 583 owner_mirror, |
| 630 CreateLazyMirror(var_type), | 584 CreateLazyMirror(var_type), |
| 631 Dart_NewBoolean(is_static), | 585 Dart_NewBoolean(is_static), |
| 632 Dart_NewBoolean(is_final), | 586 Dart_NewBoolean(is_final), |
| 633 }; | 587 }; |
| 634 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | 588 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); |
| 635 return mirror; | 589 return mirror; |
| 636 } | 590 } |
| 637 | 591 |
| 638 | 592 |
| 639 static Dart_Handle AddMemberClasses(Dart_Handle map, | 593 static Dart_Handle AddMemberClasses(Dart_Handle map, |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 842 Dart_Handle lazy_lib_mirror = CreateLazyMirror(lib); | 796 Dart_Handle lazy_lib_mirror = CreateLazyMirror(lib); |
| 843 if (Dart_IsError(lazy_lib_mirror)) { | 797 if (Dart_IsError(lazy_lib_mirror)) { |
| 844 return lazy_lib_mirror; | 798 return lazy_lib_mirror; |
| 845 } | 799 } |
| 846 Dart_Handle member_map = CreateMemberMap(lib, lazy_lib_mirror); | 800 Dart_Handle member_map = CreateMemberMap(lib, lazy_lib_mirror); |
| 847 if (Dart_IsError(member_map)) { | 801 if (Dart_IsError(member_map)) { |
| 848 return member_map; | 802 return member_map; |
| 849 } | 803 } |
| 850 Dart_Handle args[] = { | 804 Dart_Handle args[] = { |
| 851 CreateMirrorReference(lib), | 805 CreateMirrorReference(lib), |
| 852 CreateVMReference(lib), | |
| 853 Dart_LibraryName(lib), | 806 Dart_LibraryName(lib), |
| 854 Dart_LibraryUrl(lib), | 807 Dart_LibraryUrl(lib), |
| 855 member_map, | 808 member_map, |
| 856 }; | 809 }; |
| 857 Dart_Handle lib_mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | 810 Dart_Handle lib_mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); |
| 858 if (Dart_IsError(lib_mirror)) { | 811 if (Dart_IsError(lib_mirror)) { |
| 859 return lib_mirror; | 812 return lib_mirror; |
| 860 } | 813 } |
| 861 | 814 |
| 862 return lib_mirror; | 815 return lib_mirror; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 941 Dart_Handle cls_name = NewString("_LocalInstanceMirrorImpl"); | 894 Dart_Handle cls_name = NewString("_LocalInstanceMirrorImpl"); |
| 942 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | 895 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); |
| 943 if (Dart_IsError(type)) { | 896 if (Dart_IsError(type)) { |
| 944 return type; | 897 return type; |
| 945 } | 898 } |
| 946 | 899 |
| 947 // TODO(turnidge): This is wrong. The Null class is distinct from object. | 900 // TODO(turnidge): This is wrong. The Null class is distinct from object. |
| 948 Dart_Handle object_class = Dart_GetClass(CoreLib(), NewString("Object")); | 901 Dart_Handle object_class = Dart_GetClass(CoreLib(), NewString("Object")); |
| 949 | 902 |
| 950 Dart_Handle args[] = { | 903 Dart_Handle args[] = { |
| 951 CreateVMReference(Dart_Null()), | |
| 952 CreateLazyMirror(object_class), | 904 CreateLazyMirror(object_class), |
| 953 Dart_Null(), | 905 Dart_Null(), |
| 954 }; | 906 }; |
| 955 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | 907 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); |
| 956 return mirror; | 908 return mirror; |
| 957 } | 909 } |
| 958 | 910 |
| 959 | 911 |
| 960 static Dart_Handle CreateInstanceMirror(Dart_Handle instance) { | 912 static Dart_Handle CreateInstanceMirror(Dart_Handle instance) { |
| 961 if (Dart_IsNull(instance)) { | 913 if (Dart_IsNull(instance)) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 988 } | 940 } |
| 989 | 941 |
| 990 // TODO(turnidge): Pass the function owner here. This will require | 942 // TODO(turnidge): Pass the function owner here. This will require |
| 991 // us to support functions in CreateLazyMirror. | 943 // us to support functions in CreateLazyMirror. |
| 992 Dart_Handle func_mirror = | 944 Dart_Handle func_mirror = |
| 993 CreateMethodMirror(func, Dart_Null()); | 945 CreateMethodMirror(func, Dart_Null()); |
| 994 if (Dart_IsError(func_mirror)) { | 946 if (Dart_IsError(func_mirror)) { |
| 995 return func_mirror; | 947 return func_mirror; |
| 996 } | 948 } |
| 997 Dart_Handle args[] = { | 949 Dart_Handle args[] = { |
| 998 CreateVMReference(instance), | |
| 999 CreateLazyMirror(instance_cls), | 950 CreateLazyMirror(instance_cls), |
| 1000 instance, | 951 instance, |
| 1001 func_mirror, | 952 func_mirror, |
| 1002 }; | 953 }; |
| 1003 return Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | 954 return Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); |
| 1004 | 955 |
| 1005 } else { | 956 } else { |
| 1006 Dart_Handle cls_name = NewString("_LocalInstanceMirrorImpl"); | 957 Dart_Handle cls_name = NewString("_LocalInstanceMirrorImpl"); |
| 1007 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | 958 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); |
| 1008 if (Dart_IsError(type)) { | 959 if (Dart_IsError(type)) { |
| 1009 return type; | 960 return type; |
| 1010 } | 961 } |
| 1011 Dart_Handle args[] = { | 962 Dart_Handle args[] = { |
| 1012 CreateVMReference(instance), | |
| 1013 CreateLazyMirror(instance_cls), | 963 CreateLazyMirror(instance_cls), |
| 1014 instance, | 964 instance, |
| 1015 }; | 965 }; |
| 1016 return Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | 966 return Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); |
| 1017 } | 967 } |
| 1018 } | 968 } |
| 1019 | 969 |
| 1020 | 970 |
| 1021 void NATIVE_ENTRY_FUNCTION(Mirrors_makeLocalMirrorSystem)( | 971 void NATIVE_ENTRY_FUNCTION(Mirrors_makeLocalMirrorSystem)( |
| 1022 Dart_NativeArguments args) { | 972 Dart_NativeArguments args) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1073 name_handle, | 1023 name_handle, |
| 1074 lib_handle, | 1024 lib_handle, |
| 1075 lib_mirror); | 1025 lib_mirror); |
| 1076 if (Dart_IsError(result)) { | 1026 if (Dart_IsError(result)) { |
| 1077 Dart_PropagateError(result); | 1027 Dart_PropagateError(result); |
| 1078 } | 1028 } |
| 1079 Dart_SetReturnValue(args, result); | 1029 Dart_SetReturnValue(args, result); |
| 1080 Dart_ExitScope(); | 1030 Dart_ExitScope(); |
| 1081 } | 1031 } |
| 1082 | 1032 |
| 1083 void NATIVE_ENTRY_FUNCTION(Mirrors_metadata)(Dart_NativeArguments args) { | |
| 1084 Dart_EnterScope(); | |
| 1085 Dart_Handle mirror = Dart_GetNativeArgument(args, 0); | |
| 1086 | |
| 1087 Dart_Handle reflectee = UnwrapMirror(mirror); | |
| 1088 Dart_Handle result = Dart_GetMetadata(reflectee); | |
| 1089 if (Dart_IsError(result)) { | |
| 1090 Dart_PropagateError(result); | |
| 1091 } | |
| 1092 ASSERT(Dart_IsList(result)); | |
| 1093 Dart_SetReturnValue(args, result); | |
| 1094 Dart_ExitScope(); | |
| 1095 } | |
| 1096 | |
| 1097 | 1033 |
| 1034 DEFINE_NATIVE_ENTRY(DeclarationMirror_metadata, 1) { |
| 1035 const MirrorReference& decl_ref = |
| 1036 MirrorReference::CheckedHandle(arguments->NativeArgAt(0)); |
| 1037 const Object& decl = Object::Handle(decl_ref.referent()); |
| 1038 |
| 1039 Class& klass = Class::Handle(); |
| 1040 if (decl.IsClass()) { |
| 1041 klass ^= decl.raw(); |
| 1042 } else if (decl.IsFunction()) { |
| 1043 klass = Function::Cast(decl).origin(); |
| 1044 } else if (decl.IsField()) { |
| 1045 klass = Field::Cast(decl).origin(); |
| 1046 } else { |
| 1047 return Object::empty_array().raw(); |
| 1048 } |
| 1049 |
| 1050 const Library& library = Library::Handle(klass.library()); |
| 1051 return library.GetMetadata(decl); |
| 1052 } |
| 1053 |
| 1054 |
| 1098 void HandleMirrorsMessage(Isolate* isolate, | 1055 void HandleMirrorsMessage(Isolate* isolate, |
| 1099 Dart_Port reply_port, | 1056 Dart_Port reply_port, |
| 1100 const Instance& message) { | 1057 const Instance& message) { |
| 1101 UNIMPLEMENTED(); | 1058 UNIMPLEMENTED(); |
| 1102 } | 1059 } |
| 1103 | 1060 |
| 1104 | 1061 |
| 1105 // TODO(11742): This is transitional. | 1062 // TODO(11742): This is transitional. |
| 1106 static RawInstance* Reflect(const Instance& reflectee) { | 1063 static RawInstance* Reflect(const Instance& reflectee) { |
| 1107 Isolate* isolate = Isolate::Current(); | 1064 Isolate* isolate = Isolate::Current(); |
| (...skipping 677 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1785 | 1742 |
| 1786 DEFINE_NATIVE_ENTRY(MethodMirror_name, 1) { | 1743 DEFINE_NATIVE_ENTRY(MethodMirror_name, 1) { |
| 1787 const MirrorReference& func_ref = | 1744 const MirrorReference& func_ref = |
| 1788 MirrorReference::CheckedHandle(arguments->NativeArgAt(0)); | 1745 MirrorReference::CheckedHandle(arguments->NativeArgAt(0)); |
| 1789 Function& func = Function::Handle(); | 1746 Function& func = Function::Handle(); |
| 1790 func ^= func_ref.referent(); | 1747 func ^= func_ref.referent(); |
| 1791 return func.UserVisibleName(); | 1748 return func.UserVisibleName(); |
| 1792 } | 1749 } |
| 1793 | 1750 |
| 1794 } // namespace dart | 1751 } // namespace dart |
| OLD | NEW |