| 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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 static Dart_Handle CreateMirrorReference(Dart_Handle handle) { | 67 static Dart_Handle CreateMirrorReference(Dart_Handle handle) { |
| 68 Isolate* isolate = Isolate::Current(); | 68 Isolate* isolate = Isolate::Current(); |
| 69 DARTSCOPE(isolate); | 69 DARTSCOPE(isolate); |
| 70 const Object& referent = Object::Handle(isolate, Api::UnwrapHandle(handle)); | 70 const Object& referent = Object::Handle(isolate, Api::UnwrapHandle(handle)); |
| 71 const MirrorReference& reference = | 71 const MirrorReference& reference = |
| 72 MirrorReference::Handle(MirrorReference::New(referent)); | 72 MirrorReference::Handle(MirrorReference::New(referent)); |
| 73 return Api::NewHandle(isolate, reference.raw()); | 73 return Api::NewHandle(isolate, reference.raw()); |
| 74 } | 74 } |
| 75 | 75 |
| 76 | 76 |
| 77 static Dart_Handle CreateLazyMirror(Dart_Handle target); | |
| 78 | |
| 79 | |
| 80 static RawInstance* CreateParameterMirrorList(const Function& func) { | 77 static RawInstance* CreateParameterMirrorList(const Function& func) { |
| 81 HANDLESCOPE(Isolate::Current()); | 78 HANDLESCOPE(Isolate::Current()); |
| 82 const intptr_t param_cnt = func.num_fixed_parameters() - | 79 const intptr_t param_cnt = func.num_fixed_parameters() - |
| 83 func.NumImplicitParameters() + | 80 func.NumImplicitParameters() + |
| 84 func.NumOptionalParameters(); | 81 func.NumOptionalParameters(); |
| 85 const Array& results = Array::Handle(Array::New(param_cnt)); | 82 const Array& results = Array::Handle(Array::New(param_cnt)); |
| 86 const Array& args = Array::Handle(Array::New(3)); | 83 const Array& args = Array::Handle(Array::New(3)); |
| 87 args.SetAt(0, MirrorReference::Handle(MirrorReference::New(func))); | 84 args.SetAt(0, MirrorReference::Handle(MirrorReference::New(func))); |
| 88 Smi& pos = Smi::Handle(); | 85 Smi& pos = Smi::Handle(); |
| 89 Instance& param = Instance::Handle(); | 86 Instance& param = Instance::Handle(); |
| 90 for (intptr_t i = 0; i < param_cnt; i++) { | 87 for (intptr_t i = 0; i < param_cnt; i++) { |
| 91 pos ^= Smi::New(i); | 88 pos ^= Smi::New(i); |
| 92 args.SetAt(1, pos); | 89 args.SetAt(1, pos); |
| 93 args.SetAt(2, (i >= func.num_fixed_parameters()) ? | 90 args.SetAt(2, (i >= func.num_fixed_parameters()) ? |
| 94 Bool::True() : Bool::False()); | 91 Bool::True() : Bool::False()); |
| 95 param ^= CreateMirror(Symbols::_LocalParameterMirrorImpl(), args); | 92 param ^= CreateMirror(Symbols::_LocalParameterMirrorImpl(), args); |
| 96 results.SetAt(i, param); | 93 results.SetAt(i, param); |
| 97 } | 94 } |
| 98 results.MakeImmutable(); | 95 results.MakeImmutable(); |
| 99 return results.raw(); | 96 return results.raw(); |
| 100 } | 97 } |
| 101 | 98 |
| 102 | 99 |
| 103 static Dart_Handle CreateParameterMirrorListUsingApi(Dart_Handle func) { | |
| 104 ASSERT(Dart_IsFunction(func)); | |
| 105 Isolate* isolate = Isolate::Current(); | |
| 106 return Api::NewHandle( | |
| 107 isolate, CreateParameterMirrorList(Api::UnwrapFunctionHandle( | |
| 108 isolate, func))); | |
| 109 } | |
| 110 | |
| 111 | |
| 112 static Dart_Handle CreateLazyMirror(Dart_Handle target) { | |
| 113 if (Dart_IsNull(target) || Dart_IsError(target)) { | |
| 114 return target; | |
| 115 } | |
| 116 | |
| 117 if (Dart_IsLibrary(target)) { | |
| 118 Dart_Handle cls_name = NewString("_LazyLibraryMirror"); | |
| 119 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | |
| 120 Dart_Handle args[] = { Dart_LibraryUrl(target) }; | |
| 121 return Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | |
| 122 } | |
| 123 | |
| 124 if (Dart_IsClass(target)) { | |
| 125 if (Dart_ClassIsFunctionType(target)) { | |
| 126 Dart_Handle cls_name = NewString("_LazyFunctionTypeMirror"); | |
| 127 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | |
| 128 | |
| 129 Dart_Handle sig = Dart_ClassGetFunctionTypeSignature(target); | |
| 130 Dart_Handle return_type = Dart_FunctionReturnType(sig); | |
| 131 if (Dart_IsError(return_type)) { | |
| 132 return return_type; | |
| 133 } | |
| 134 | |
| 135 Dart_Handle args[] = { | |
| 136 CreateMirrorReference(target), | |
| 137 CreateLazyMirror(return_type), | |
| 138 CreateParameterMirrorListUsingApi(sig), | |
| 139 }; | |
| 140 return Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | |
| 141 } else { | |
| 142 Dart_Handle cls_name = NewString("_LazyTypeMirror"); | |
| 143 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | |
| 144 Dart_Handle lib = Dart_ClassGetLibrary(target); | |
| 145 Dart_Handle lib_url; | |
| 146 if (Dart_IsNull(lib)) { | |
| 147 lib_url = Dart_Null(); | |
| 148 } else { | |
| 149 lib_url = Dart_LibraryUrl(lib); | |
| 150 } | |
| 151 Dart_Handle args[] = { lib_url, Dart_ClassName(target) }; | |
| 152 return Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 if (Dart_IsTypeVariable(target)) { | |
| 157 Dart_Handle var_name = Dart_TypeVariableName(target); | |
| 158 Dart_Handle owner = Dart_TypeVariableOwner(target); | |
| 159 Dart_Handle owner_mirror = CreateLazyMirror(owner); | |
| 160 | |
| 161 Dart_Handle cls_name = NewString("_LazyTypeVariableMirror"); | |
| 162 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | |
| 163 | |
| 164 Dart_Handle args[] = { var_name, owner_mirror }; | |
| 165 return Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | |
| 166 } | |
| 167 | |
| 168 UNREACHABLE(); | |
| 169 return Dart_Null(); | |
| 170 } | |
| 171 | |
| 172 | |
| 173 static RawInstance* CreateTypeVariableMirror(const TypeParameter& param, | 100 static RawInstance* CreateTypeVariableMirror(const TypeParameter& param, |
| 174 const Instance& owner_mirror) { | 101 const Instance& owner_mirror) { |
| 175 const Array& args = Array::Handle(Array::New(3)); | 102 const Array& args = Array::Handle(Array::New(3)); |
| 176 args.SetAt(0, param); | 103 args.SetAt(0, param); |
| 177 args.SetAt(1, String::Handle(param.name())); | 104 args.SetAt(1, String::Handle(param.name())); |
| 178 args.SetAt(2, owner_mirror); | 105 args.SetAt(2, owner_mirror); |
| 179 return CreateMirror(Symbols::_LocalTypeVariableMirrorImpl(), args); | 106 return CreateMirror(Symbols::_LocalTypeVariableMirrorImpl(), args); |
| 180 } | 107 } |
| 181 | 108 |
| 182 | 109 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 194 type ^= args.TypeAt(i); | 121 type ^= args.TypeAt(i); |
| 195 ASSERT(type.IsTypeParameter()); | 122 ASSERT(type.IsTypeParameter()); |
| 196 name ^= type.name(); | 123 name ^= type.name(); |
| 197 result.SetAt(2 * i, name); | 124 result.SetAt(2 * i, name); |
| 198 result.SetAt(2 * i + 1, type); | 125 result.SetAt(2 * i + 1, type); |
| 199 } | 126 } |
| 200 return result.raw(); | 127 return result.raw(); |
| 201 } | 128 } |
| 202 | 129 |
| 203 | 130 |
| 204 static Dart_Handle CreateTypedefMirror(Dart_Handle cls, | 131 static RawInstance* CreateTypedefMirror(const Class& cls, |
| 205 Dart_Handle cls_name, | 132 const Instance& owner_mirror) { |
| 206 Dart_Handle owner_mirror) { | 133 const Array& args = Array::Handle(Array::New(3)); |
| 207 Dart_Handle mirror_cls_name = NewString("_LocalTypedefMirrorImpl"); | 134 args.SetAt(0, MirrorReference::Handle(MirrorReference::New(cls))); |
| 208 Dart_Handle mirror_type = Dart_GetType(MirrorLib(), mirror_cls_name, 0, NULL); | 135 args.SetAt(1, String::Handle(cls.UserVisibleName())); |
| 209 if (Dart_IsError(mirror_type)) { | 136 args.SetAt(2, owner_mirror); |
| 210 return mirror_type; | 137 return CreateMirror(Symbols::_LocalTypedefMirrorImpl(), args); |
| 211 } | |
| 212 | |
| 213 Dart_Handle referent = Dart_ClassGetTypedefReferent(cls); | |
| 214 if (Dart_IsError(referent)) { | |
| 215 return referent; | |
| 216 } | |
| 217 | |
| 218 Dart_Handle args[] = { | |
| 219 CreateMirrorReference(cls), | |
| 220 cls_name, | |
| 221 owner_mirror, | |
| 222 CreateLazyMirror(referent), | |
| 223 }; | |
| 224 Dart_Handle mirror = | |
| 225 Dart_New(mirror_type, Dart_Null(), ARRAY_SIZE(args), args); | |
| 226 return mirror; | |
| 227 } | 138 } |
| 228 | 139 |
| 229 | 140 |
| 141 static Dart_Handle CreateTypedefMirrorUsingApi(Dart_Handle cls, |
| 142 Dart_Handle cls_name, |
| 143 Dart_Handle owner_mirror) { |
| 144 Isolate* isolate = Isolate::Current(); |
| 145 return Api::NewHandle( |
| 146 isolate, CreateTypedefMirror( |
| 147 Api::UnwrapClassHandle(isolate, cls), |
| 148 Api::UnwrapInstanceHandle(isolate, owner_mirror))); |
| 149 } |
| 150 |
| 151 |
| 230 static Dart_Handle CreateClassMirrorUsingApi(Dart_Handle intf, | 152 static Dart_Handle CreateClassMirrorUsingApi(Dart_Handle intf, |
| 231 Dart_Handle intf_name, | 153 Dart_Handle intf_name, |
| 232 Dart_Handle lib_mirror) { | 154 Dart_Handle lib_mirror) { |
| 233 ASSERT(Dart_IsClass(intf)); | 155 ASSERT(Dart_IsClass(intf)); |
| 234 if (Dart_ClassIsTypedef(intf)) { | 156 if (Dart_ClassIsTypedef(intf)) { |
| 235 // This class is actually a typedef. Represent it specially in | 157 // This class is actually a typedef. Represent it specially in |
| 236 // reflection. | 158 // reflection. |
| 237 return CreateTypedefMirror(intf, intf_name, lib_mirror); | 159 return CreateTypedefMirrorUsingApi(intf, intf_name, lib_mirror); |
| 238 } | 160 } |
| 239 | 161 |
| 240 Dart_Handle cls_name = NewString("_LocalClassMirrorImpl"); | 162 Dart_Handle cls_name = NewString("_LocalClassMirrorImpl"); |
| 241 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | 163 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); |
| 242 if (Dart_IsError(type)) { | 164 if (Dart_IsError(type)) { |
| 243 return type; | 165 return type; |
| 244 } | 166 } |
| 245 | 167 |
| 246 Dart_Handle args[] = { | 168 Dart_Handle args[] = { |
| 247 CreateMirrorReference(intf), | 169 CreateMirrorReference(intf), |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 | 394 |
| 473 const Library& library = Library::Handle(klass.library()); | 395 const Library& library = Library::Handle(klass.library()); |
| 474 const Object& metadata = Object::Handle(library.GetMetadata(decl)); | 396 const Object& metadata = Object::Handle(library.GetMetadata(decl)); |
| 475 if (metadata.IsError()) { | 397 if (metadata.IsError()) { |
| 476 ThrowInvokeError(Error::Cast(metadata)); | 398 ThrowInvokeError(Error::Cast(metadata)); |
| 477 } | 399 } |
| 478 return metadata.raw(); | 400 return metadata.raw(); |
| 479 } | 401 } |
| 480 | 402 |
| 481 | 403 |
| 404 DEFINE_NATIVE_ENTRY(FunctionTypeMirror_parameters, 1) { |
| 405 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); |
| 406 const Class& cls = Class::Handle(ref.GetClassReferent()); |
| 407 const Function& func = Function::Handle(cls.signature_function()); |
| 408 return CreateParameterMirrorList(func); |
| 409 } |
| 410 |
| 411 |
| 412 DEFINE_NATIVE_ENTRY(FunctionTypeMirror_return_type, 1) { |
| 413 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); |
| 414 const Class& cls = Class::Handle(ref.GetClassReferent()); |
| 415 const Function& func = Function::Handle(cls.signature_function()); |
| 416 const AbstractType& return_type = AbstractType::Handle(func.result_type()); |
| 417 return CreateTypeMirror(return_type); |
| 418 } |
| 419 |
| 420 |
| 482 void HandleMirrorsMessage(Isolate* isolate, | 421 void HandleMirrorsMessage(Isolate* isolate, |
| 483 Dart_Port reply_port, | 422 Dart_Port reply_port, |
| 484 const Instance& message) { | 423 const Instance& message) { |
| 485 UNIMPLEMENTED(); | 424 UNIMPLEMENTED(); |
| 486 } | 425 } |
| 487 | 426 |
| 488 | 427 |
| 489 static bool FieldIsUninitialized(const Field& field) { | 428 static bool FieldIsUninitialized(const Field& field) { |
| 490 ASSERT(!field.IsNull()); | 429 ASSERT(!field.IsNull()); |
| 491 | 430 |
| (...skipping 803 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1295 DEFINE_NATIVE_ENTRY(MethodMirror_return_type, 1) { | 1234 DEFINE_NATIVE_ENTRY(MethodMirror_return_type, 1) { |
| 1296 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); | 1235 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); |
| 1297 const Function& func = Function::Handle(ref.GetFunctionReferent()); | 1236 const Function& func = Function::Handle(ref.GetFunctionReferent()); |
| 1298 // We handle constructors in Dart code. | 1237 // We handle constructors in Dart code. |
| 1299 ASSERT(!func.IsConstructor()); | 1238 ASSERT(!func.IsConstructor()); |
| 1300 const AbstractType& return_type = AbstractType::Handle(func.result_type()); | 1239 const AbstractType& return_type = AbstractType::Handle(func.result_type()); |
| 1301 return CreateTypeMirror(return_type); | 1240 return CreateTypeMirror(return_type); |
| 1302 } | 1241 } |
| 1303 | 1242 |
| 1304 | 1243 |
| 1244 DEFINE_NATIVE_ENTRY(TypedefMirror_referent, 1) { |
| 1245 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); |
| 1246 const Class& cls = Class::Handle(ref.GetClassReferent()); |
| 1247 const Function& sig_func = Function::Handle(cls.signature_function()); |
| 1248 const Class& sig_cls = Class::Handle(sig_func.signature_class()); |
| 1249 return MirrorReference::New(sig_cls); |
| 1250 } |
| 1251 |
| 1252 |
| 1305 DEFINE_NATIVE_ENTRY(ParameterMirror_type, 2) { | 1253 DEFINE_NATIVE_ENTRY(ParameterMirror_type, 2) { |
| 1306 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); | 1254 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); |
| 1307 GET_NON_NULL_NATIVE_ARGUMENT(Smi, pos, arguments->NativeArgAt(1)); | 1255 GET_NON_NULL_NATIVE_ARGUMENT(Smi, pos, arguments->NativeArgAt(1)); |
| 1308 const Function& func = Function::Handle(ref.GetFunctionReferent()); | 1256 const Function& func = Function::Handle(ref.GetFunctionReferent()); |
| 1309 const AbstractType& param_type = AbstractType::Handle(func.ParameterTypeAt( | 1257 const AbstractType& param_type = AbstractType::Handle(func.ParameterTypeAt( |
| 1310 func.NumImplicitParameters() + pos.Value())); | 1258 func.NumImplicitParameters() + pos.Value())); |
| 1311 return CreateTypeMirror(param_type); | 1259 return CreateTypeMirror(param_type); |
| 1312 } | 1260 } |
| 1313 | 1261 |
| 1314 | 1262 |
| 1315 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { | 1263 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { |
| 1316 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); | 1264 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); |
| 1317 const Field& field = Field::Handle(ref.GetFieldReferent()); | 1265 const Field& field = Field::Handle(ref.GetFieldReferent()); |
| 1318 | 1266 |
| 1319 const AbstractType& type = AbstractType::Handle(field.type()); | 1267 const AbstractType& type = AbstractType::Handle(field.type()); |
| 1320 return CreateTypeMirror(type); | 1268 return CreateTypeMirror(type); |
| 1321 } | 1269 } |
| 1322 | 1270 |
| 1323 } // namespace dart | 1271 } // namespace dart |
| OLD | NEW |