| 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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 return Dart_Invoke(map, NewString("[]="), ARRAY_SIZE(args), args); | 78 return Dart_Invoke(map, NewString("[]="), ARRAY_SIZE(args), args); |
| 79 } | 79 } |
| 80 | 80 |
| 81 | 81 |
| 82 static Dart_Handle MirrorLib() { | 82 static Dart_Handle MirrorLib() { |
| 83 Dart_Handle mirror_lib_name = NewString("dart:mirrors"); | 83 Dart_Handle mirror_lib_name = NewString("dart:mirrors"); |
| 84 return Dart_LookupLibrary(mirror_lib_name); | 84 return Dart_LookupLibrary(mirror_lib_name); |
| 85 } | 85 } |
| 86 | 86 |
| 87 | 87 |
| 88 static Dart_Handle IsMethodMirror(Dart_Handle object, bool* is_mirror) { | |
| 89 Dart_Handle cls_name = NewString("MethodMirror"); | |
| 90 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | |
| 91 if (Dart_IsError(type)) { | |
| 92 return type; | |
| 93 } | |
| 94 Dart_Handle result = Dart_ObjectIsType(object, type, is_mirror); | |
| 95 if (Dart_IsError(result)) { | |
| 96 return result; | |
| 97 } | |
| 98 return Dart_True(); // Indicates success. Result is in is_mirror. | |
| 99 } | |
| 100 | |
| 101 static Dart_Handle IsVariableMirror(Dart_Handle object, bool* is_mirror) { | |
| 102 Dart_Handle cls_name = NewString("VariableMirror"); | |
| 103 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | |
| 104 if (Dart_IsError(type)) { | |
| 105 return type; | |
| 106 } | |
| 107 Dart_Handle result = Dart_ObjectIsType(object, type, is_mirror); | |
| 108 if (Dart_IsError(result)) { | |
| 109 return result; | |
| 110 } | |
| 111 return Dart_True(); // Indicates success. Result is in is_mirror. | |
| 112 } | |
| 113 | |
| 114 | |
| 115 // TODO(11742): Remove once there are no more users of the Dart_Handle-based | 88 // TODO(11742): Remove once there are no more users of the Dart_Handle-based |
| 116 // VMReferences. | 89 // VMReferences. |
| 117 static Dart_Handle CreateMirrorReference(Dart_Handle handle) { | 90 static Dart_Handle CreateMirrorReference(Dart_Handle handle) { |
| 118 Isolate* isolate = Isolate::Current(); | 91 Isolate* isolate = Isolate::Current(); |
| 119 DARTSCOPE(isolate); | 92 DARTSCOPE(isolate); |
| 120 const Object& referent = Object::Handle(isolate, Api::UnwrapHandle(handle)); | 93 const Object& referent = Object::Handle(isolate, Api::UnwrapHandle(handle)); |
| 121 const MirrorReference& reference = | 94 const MirrorReference& reference = |
| 122 MirrorReference::Handle(MirrorReference::New(referent)); | 95 MirrorReference::Handle(MirrorReference::New(referent)); |
| 123 return Api::NewHandle(isolate, reference.raw()); | 96 return Api::NewHandle(isolate, reference.raw()); |
| 124 } | 97 } |
| 125 | 98 |
| 126 | 99 |
| 127 static Dart_Handle StringFromSymbol(Dart_Handle symbol) { | |
| 128 Dart_Handle result = Dart_Invoke(MirrorLib(), NewString("_n"), 1, &symbol); | |
| 129 return result; | |
| 130 } | |
| 131 | |
| 132 | |
| 133 static Dart_Handle UnwrapVMReference(Dart_Handle vm_ref) { | |
| 134 // Retrieve the persistent handle from the VMReference | |
| 135 intptr_t perm_handle_value = 0; | |
| 136 Dart_Handle result = | |
| 137 Dart_GetNativeInstanceField(vm_ref, 0, &perm_handle_value); | |
| 138 if (Dart_IsError(result)) { | |
| 139 return result; | |
| 140 } | |
| 141 Dart_PersistentHandle perm_handle = | |
| 142 reinterpret_cast<Dart_PersistentHandle>(perm_handle_value); | |
| 143 ASSERT(perm_handle != NULL); | |
| 144 Dart_Handle handle = Dart_HandleFromPersistent(perm_handle); | |
| 145 ASSERT(handle != NULL); | |
| 146 ASSERT(!Dart_IsError(handle)); | |
| 147 return handle; | |
| 148 } | |
| 149 | |
| 150 static Dart_Handle UnwrapMirror(Dart_Handle mirror); | |
| 151 | |
| 152 static Dart_Handle UnwrapObjectMirror(Dart_Handle mirror) { | |
| 153 Dart_Handle field_name = NewString("_reference"); | |
| 154 Dart_Handle vm_ref = Dart_GetField(mirror, field_name); | |
| 155 if (Dart_IsError(vm_ref)) { | |
| 156 return vm_ref; | |
| 157 } | |
| 158 return UnwrapVMReference(vm_ref); | |
| 159 } | |
| 160 | |
| 161 | |
| 162 static Dart_Handle UnwrapMethodMirror(Dart_Handle methodMirror) { | |
| 163 Dart_Handle namefield_name = NewString("simpleName"); | |
| 164 Dart_Handle name_ref = Dart_GetField(methodMirror, namefield_name); | |
| 165 if (Dart_IsError(name_ref)) { | |
| 166 return name_ref; | |
| 167 } | |
| 168 Dart_Handle ownerfield_name = NewString("_owner"); | |
| 169 Dart_Handle owner_mirror = Dart_GetField(methodMirror, ownerfield_name); | |
| 170 if (Dart_IsError(owner_mirror)) { | |
| 171 return owner_mirror; | |
| 172 } | |
| 173 Dart_Handle owner = UnwrapMirror(owner_mirror); | |
| 174 if (Dart_IsError(owner)) { | |
| 175 return owner; | |
| 176 } | |
| 177 Dart_Handle func = Dart_LookupFunction(owner, StringFromSymbol(name_ref)); | |
| 178 if (Dart_IsError(func)) { | |
| 179 return func; | |
| 180 } | |
| 181 ASSERT(!Dart_IsNull(func)); | |
| 182 return func; | |
| 183 } | |
| 184 | |
| 185 static Dart_Handle UnwrapVariableMirror(Dart_Handle variableMirror) { | |
| 186 Dart_Handle namefield_name = NewString("simpleName"); | |
| 187 Dart_Handle name_ref = Dart_GetField(variableMirror, namefield_name); | |
| 188 if (Dart_IsError(name_ref)) { | |
| 189 return name_ref; | |
| 190 } | |
| 191 Dart_Handle ownerfield_name = NewString("_owner"); | |
| 192 Dart_Handle owner_mirror = Dart_GetField(variableMirror, ownerfield_name); | |
| 193 ASSERT(!Dart_IsNull(owner_mirror)); | |
| 194 if (Dart_IsError(owner_mirror)) { | |
| 195 return owner_mirror; | |
| 196 } | |
| 197 Dart_Handle owner = UnwrapMirror(owner_mirror); | |
| 198 if (Dart_IsError(owner)) { | |
| 199 return owner; | |
| 200 } | |
| 201 Dart_Handle variable = | |
| 202 Dart_LookupVariable(owner, StringFromSymbol(name_ref)); | |
| 203 if (Dart_IsError(variable)) { | |
| 204 return variable; | |
| 205 } | |
| 206 ASSERT(!Dart_IsNull(variable)); | |
| 207 return variable; | |
| 208 } | |
| 209 | |
| 210 static Dart_Handle UnwrapMirror(Dart_Handle mirror) { | |
| 211 // Caveat Emptor: | |
| 212 // only works for ObjectMirrors, VariableMirrors and MethodMirrors | |
| 213 // and their subtypes | |
| 214 bool is_method_mirror = false; | |
| 215 Dart_Handle result = IsMethodMirror(mirror, &is_method_mirror); | |
| 216 if (Dart_IsError(result)) { | |
| 217 return result; | |
| 218 } | |
| 219 if (is_method_mirror) { | |
| 220 return UnwrapMethodMirror(mirror); | |
| 221 } | |
| 222 bool is_variable_mirror = false; | |
| 223 result = IsVariableMirror(mirror, &is_variable_mirror); | |
| 224 if (Dart_IsError(result)) { | |
| 225 return result; | |
| 226 } | |
| 227 if (is_variable_mirror) { | |
| 228 return UnwrapVariableMirror(mirror); | |
| 229 } | |
| 230 return UnwrapObjectMirror(mirror); | |
| 231 // will return nonsense if mirror is not an ObjectMirror | |
| 232 } | |
| 233 | |
| 234 | |
| 235 static Dart_Handle CreateLazyMirror(Dart_Handle target); | 100 static Dart_Handle CreateLazyMirror(Dart_Handle target); |
| 236 | 101 |
| 237 | 102 |
| 238 static RawInstance* CreateParameterMirrorList(const Function& func) { | 103 static RawInstance* CreateParameterMirrorList(const Function& func) { |
| 239 HANDLESCOPE(Isolate::Current()); | 104 HANDLESCOPE(Isolate::Current()); |
| 240 const intptr_t param_cnt = func.num_fixed_parameters() - | 105 const intptr_t param_cnt = func.num_fixed_parameters() - |
| 241 func.NumImplicitParameters() + | 106 func.NumImplicitParameters() + |
| 242 func.NumOptionalParameters(); | 107 func.NumOptionalParameters(); |
| 243 const Array& results = Array::Handle(Array::New(param_cnt)); | 108 const Array& results = Array::Handle(Array::New(param_cnt)); |
| 244 const Array& args = Array::Handle(Array::New(3)); | 109 const Array& args = Array::Handle(Array::New(3)); |
| (...skipping 1485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1730 | 1595 |
| 1731 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { | 1596 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { |
| 1732 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); | 1597 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); |
| 1733 const Field& field = Field::Handle(ref.GetFieldReferent()); | 1598 const Field& field = Field::Handle(ref.GetFieldReferent()); |
| 1734 | 1599 |
| 1735 const AbstractType& type = AbstractType::Handle(field.type()); | 1600 const AbstractType& type = AbstractType::Handle(field.type()); |
| 1736 return CreateTypeMirror(type); | 1601 return CreateTypeMirror(type); |
| 1737 } | 1602 } |
| 1738 | 1603 |
| 1739 } // namespace dart | 1604 } // namespace dart |
| OLD | NEW |