| 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 Dart_False(), | 577 Dart_False(), |
| 625 }; | 578 }; |
| 626 Dart_Handle mirror = | 579 Dart_Handle mirror = |
| 627 Dart_New(mirror_type, Dart_Null(), ARRAY_SIZE(args), args); | 580 Dart_New(mirror_type, Dart_Null(), ARRAY_SIZE(args), args); |
| 628 return mirror; | 581 return mirror; |
| 629 } | 582 } |
| 630 | 583 |
| 631 | 584 |
| 632 static Dart_Handle CreateVariableMirror(Dart_Handle var, | 585 static Dart_Handle CreateVariableMirror(Dart_Handle var, |
| 633 Dart_Handle var_name, | 586 Dart_Handle var_name, |
| 634 Dart_Handle lib_mirror) { | 587 Dart_Handle owner_mirror) { |
| 635 ASSERT(Dart_IsVariable(var)); | 588 ASSERT(Dart_IsVariable(var)); |
| 636 Dart_Handle cls_name = NewString("_LocalVariableMirrorImpl"); | 589 Dart_Handle cls_name = NewString("_LocalVariableMirrorImpl"); |
| 637 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | 590 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); |
| 638 if (Dart_IsError(type)) { | 591 if (Dart_IsError(type)) { |
| 639 return type; | 592 return type; |
| 640 } | 593 } |
| 641 | 594 |
| 642 bool is_static = false; | 595 bool is_static = false; |
| 643 bool is_final = false; | 596 bool is_final = false; |
| 644 | 597 |
| 645 Dart_Handle result = Dart_VariableIsStatic(var, &is_static); | 598 Dart_Handle result = Dart_VariableIsStatic(var, &is_static); |
| 646 if (Dart_IsError(result)) { | 599 if (Dart_IsError(result)) { |
| 647 return result; | 600 return result; |
| 648 } | 601 } |
| 649 result = Dart_VariableIsFinal(var, &is_final); | 602 result = Dart_VariableIsFinal(var, &is_final); |
| 650 if (Dart_IsError(result)) { | 603 if (Dart_IsError(result)) { |
| 651 return result; | 604 return result; |
| 652 } | 605 } |
| 653 | 606 |
| 654 Dart_Handle var_type = Dart_VariableType(var); | 607 Dart_Handle var_type = Dart_VariableType(var); |
| 655 if (Dart_IsError(var_type)) { | 608 if (Dart_IsError(var_type)) { |
| 656 return var_type; | 609 return var_type; |
| 657 } | 610 } |
| 658 | 611 |
| 659 Dart_Handle args[] = { | 612 Dart_Handle args[] = { |
| 613 CreateMirrorReference(var), |
| 660 var_name, | 614 var_name, |
| 661 lib_mirror, | 615 owner_mirror, |
| 662 CreateLazyMirror(var_type), | 616 CreateLazyMirror(var_type), |
| 663 Dart_NewBoolean(is_static), | 617 Dart_NewBoolean(is_static), |
| 664 Dart_NewBoolean(is_final), | 618 Dart_NewBoolean(is_final), |
| 665 }; | 619 }; |
| 666 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | 620 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); |
| 667 return mirror; | 621 return mirror; |
| 668 } | 622 } |
| 669 | 623 |
| 670 | 624 |
| 671 static Dart_Handle AddMemberClasses(Dart_Handle map, | 625 static Dart_Handle AddMemberClasses(Dart_Handle map, |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 874 Dart_Handle lazy_lib_mirror = CreateLazyMirror(lib); | 828 Dart_Handle lazy_lib_mirror = CreateLazyMirror(lib); |
| 875 if (Dart_IsError(lazy_lib_mirror)) { | 829 if (Dart_IsError(lazy_lib_mirror)) { |
| 876 return lazy_lib_mirror; | 830 return lazy_lib_mirror; |
| 877 } | 831 } |
| 878 Dart_Handle member_map = CreateMemberMap(lib, lazy_lib_mirror); | 832 Dart_Handle member_map = CreateMemberMap(lib, lazy_lib_mirror); |
| 879 if (Dart_IsError(member_map)) { | 833 if (Dart_IsError(member_map)) { |
| 880 return member_map; | 834 return member_map; |
| 881 } | 835 } |
| 882 Dart_Handle args[] = { | 836 Dart_Handle args[] = { |
| 883 CreateMirrorReference(lib), | 837 CreateMirrorReference(lib), |
| 884 CreateVMReference(lib), | |
| 885 Dart_LibraryName(lib), | 838 Dart_LibraryName(lib), |
| 886 Dart_LibraryUrl(lib), | 839 Dart_LibraryUrl(lib), |
| 887 member_map, | 840 member_map, |
| 888 }; | 841 }; |
| 889 Dart_Handle lib_mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | 842 Dart_Handle lib_mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); |
| 890 if (Dart_IsError(lib_mirror)) { | 843 if (Dart_IsError(lib_mirror)) { |
| 891 return lib_mirror; | 844 return lib_mirror; |
| 892 } | 845 } |
| 893 | 846 |
| 894 return lib_mirror; | 847 return lib_mirror; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 973 Dart_Handle cls_name = NewString("_LocalInstanceMirrorImpl"); | 926 Dart_Handle cls_name = NewString("_LocalInstanceMirrorImpl"); |
| 974 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | 927 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); |
| 975 if (Dart_IsError(type)) { | 928 if (Dart_IsError(type)) { |
| 976 return type; | 929 return type; |
| 977 } | 930 } |
| 978 | 931 |
| 979 // TODO(turnidge): This is wrong. The Null class is distinct from object. | 932 // TODO(turnidge): This is wrong. The Null class is distinct from object. |
| 980 Dart_Handle object_class = Dart_GetClass(CoreLib(), NewString("Object")); | 933 Dart_Handle object_class = Dart_GetClass(CoreLib(), NewString("Object")); |
| 981 | 934 |
| 982 Dart_Handle args[] = { | 935 Dart_Handle args[] = { |
| 983 CreateVMReference(Dart_Null()), | |
| 984 CreateLazyMirror(object_class), | 936 CreateLazyMirror(object_class), |
| 985 Dart_Null(), | 937 Dart_Null(), |
| 986 }; | 938 }; |
| 987 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | 939 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); |
| 988 return mirror; | 940 return mirror; |
| 989 } | 941 } |
| 990 | 942 |
| 991 | 943 |
| 992 static Dart_Handle CreateInstanceMirror(Dart_Handle instance) { | 944 static Dart_Handle CreateInstanceMirror(Dart_Handle instance) { |
| 993 if (Dart_IsNull(instance)) { | 945 if (Dart_IsNull(instance)) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 1020 } | 972 } |
| 1021 | 973 |
| 1022 // TODO(turnidge): Pass the function owner here. This will require | 974 // TODO(turnidge): Pass the function owner here. This will require |
| 1023 // us to support functions in CreateLazyMirror. | 975 // us to support functions in CreateLazyMirror. |
| 1024 Dart_Handle func_mirror = | 976 Dart_Handle func_mirror = |
| 1025 CreateMethodMirror(func, Dart_Null()); | 977 CreateMethodMirror(func, Dart_Null()); |
| 1026 if (Dart_IsError(func_mirror)) { | 978 if (Dart_IsError(func_mirror)) { |
| 1027 return func_mirror; | 979 return func_mirror; |
| 1028 } | 980 } |
| 1029 Dart_Handle args[] = { | 981 Dart_Handle args[] = { |
| 1030 CreateVMReference(instance), | |
| 1031 CreateLazyMirror(instance_cls), | 982 CreateLazyMirror(instance_cls), |
| 1032 instance, | 983 instance, |
| 1033 func_mirror, | 984 func_mirror, |
| 1034 }; | 985 }; |
| 1035 return Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | 986 return Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); |
| 1036 | 987 |
| 1037 } else { | 988 } else { |
| 1038 Dart_Handle cls_name = NewString("_LocalInstanceMirrorImpl"); | 989 Dart_Handle cls_name = NewString("_LocalInstanceMirrorImpl"); |
| 1039 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | 990 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); |
| 1040 if (Dart_IsError(type)) { | 991 if (Dart_IsError(type)) { |
| 1041 return type; | 992 return type; |
| 1042 } | 993 } |
| 1043 Dart_Handle args[] = { | 994 Dart_Handle args[] = { |
| 1044 CreateVMReference(instance), | |
| 1045 CreateLazyMirror(instance_cls), | 995 CreateLazyMirror(instance_cls), |
| 1046 instance, | 996 instance, |
| 1047 }; | 997 }; |
| 1048 return Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | 998 return Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); |
| 1049 } | 999 } |
| 1050 } | 1000 } |
| 1051 | 1001 |
| 1052 | 1002 |
| 1053 void NATIVE_ENTRY_FUNCTION(Mirrors_makeLocalMirrorSystem)( | 1003 void NATIVE_ENTRY_FUNCTION(Mirrors_makeLocalMirrorSystem)( |
| 1054 Dart_NativeArguments args) { | 1004 Dart_NativeArguments args) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1105 name_handle, | 1055 name_handle, |
| 1106 lib_handle, | 1056 lib_handle, |
| 1107 lib_mirror); | 1057 lib_mirror); |
| 1108 if (Dart_IsError(result)) { | 1058 if (Dart_IsError(result)) { |
| 1109 Dart_PropagateError(result); | 1059 Dart_PropagateError(result); |
| 1110 } | 1060 } |
| 1111 Dart_SetReturnValue(args, result); | 1061 Dart_SetReturnValue(args, result); |
| 1112 Dart_ExitScope(); | 1062 Dart_ExitScope(); |
| 1113 } | 1063 } |
| 1114 | 1064 |
| 1115 void NATIVE_ENTRY_FUNCTION(Mirrors_metadata)(Dart_NativeArguments args) { | |
| 1116 Dart_EnterScope(); | |
| 1117 Dart_Handle mirror = Dart_GetNativeArgument(args, 0); | |
| 1118 | |
| 1119 Dart_Handle reflectee = UnwrapMirror(mirror); | |
| 1120 Dart_Handle result = Dart_GetMetadata(reflectee); | |
| 1121 if (Dart_IsError(result)) { | |
| 1122 Dart_PropagateError(result); | |
| 1123 } | |
| 1124 ASSERT(Dart_IsList(result)); | |
| 1125 Dart_SetReturnValue(args, result); | |
| 1126 Dart_ExitScope(); | |
| 1127 } | |
| 1128 | |
| 1129 | 1065 |
| 1066 DEFINE_NATIVE_ENTRY(DeclarationMirror_metadata, 1) { |
| 1067 const MirrorReference& decl_ref = |
| 1068 MirrorReference::CheckedHandle(arguments->NativeArgAt(0)); |
| 1069 const Object& decl = Object::Handle(decl_ref.referent()); |
| 1070 |
| 1071 Class& klass = Class::Handle(); |
| 1072 if (decl.IsClass()) { |
| 1073 klass ^= decl.raw(); |
| 1074 } else if (decl.IsFunction()) { |
| 1075 klass = Function::Cast(decl).origin(); |
| 1076 } else if (decl.IsField()) { |
| 1077 klass = Field::Cast(decl).origin(); |
| 1078 } else { |
| 1079 return Object::empty_array().raw(); |
| 1080 } |
| 1081 |
| 1082 const Library& library = Library::Handle(klass.library()); |
| 1083 return library.GetMetadata(decl); |
| 1084 } |
| 1085 |
| 1086 |
| 1130 void HandleMirrorsMessage(Isolate* isolate, | 1087 void HandleMirrorsMessage(Isolate* isolate, |
| 1131 Dart_Port reply_port, | 1088 Dart_Port reply_port, |
| 1132 const Instance& message) { | 1089 const Instance& message) { |
| 1133 UNIMPLEMENTED(); | 1090 UNIMPLEMENTED(); |
| 1134 } | 1091 } |
| 1135 | 1092 |
| 1136 | 1093 |
| 1137 // TODO(11742): This is transitional. | 1094 // TODO(11742): This is transitional. |
| 1138 static RawInstance* Reflect(const Instance& reflectee) { | 1095 static RawInstance* Reflect(const Instance& reflectee) { |
| 1139 Isolate* isolate = Isolate::Current(); | 1096 Isolate* isolate = Isolate::Current(); |
| (...skipping 677 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1817 | 1774 |
| 1818 DEFINE_NATIVE_ENTRY(MethodMirror_name, 1) { | 1775 DEFINE_NATIVE_ENTRY(MethodMirror_name, 1) { |
| 1819 const MirrorReference& func_ref = | 1776 const MirrorReference& func_ref = |
| 1820 MirrorReference::CheckedHandle(arguments->NativeArgAt(0)); | 1777 MirrorReference::CheckedHandle(arguments->NativeArgAt(0)); |
| 1821 Function& func = Function::Handle(); | 1778 Function& func = Function::Handle(); |
| 1822 func ^= func_ref.referent(); | 1779 func ^= func_ref.referent(); |
| 1823 return func.UserVisibleName(); | 1780 return func.UserVisibleName(); |
| 1824 } | 1781 } |
| 1825 | 1782 |
| 1826 } // namespace dart | 1783 } // namespace dart |
| OLD | NEW |