| 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 11 matching lines...) Expand all Loading... |
| 22 static RawInstance* CreateMirror(const String& mirror_class_name, | 22 static RawInstance* CreateMirror(const String& mirror_class_name, |
| 23 const Array& constructor_arguments) { | 23 const Array& constructor_arguments) { |
| 24 const Library& mirrors_lib = Library::Handle(Library::MirrorsLibrary()); | 24 const Library& mirrors_lib = Library::Handle(Library::MirrorsLibrary()); |
| 25 const String& constructor_name = Symbols::Dot(); | 25 const String& constructor_name = Symbols::Dot(); |
| 26 | 26 |
| 27 const Object& result = Object::Handle( | 27 const Object& result = Object::Handle( |
| 28 DartLibraryCalls::InstanceCreate(mirrors_lib, | 28 DartLibraryCalls::InstanceCreate(mirrors_lib, |
| 29 mirror_class_name, | 29 mirror_class_name, |
| 30 constructor_name, | 30 constructor_name, |
| 31 constructor_arguments)); | 31 constructor_arguments)); |
| 32 ASSERT(result.IsInstance()); | 32 ASSERT(!result.IsError()); |
| 33 return Instance::Cast(result).raw(); | 33 return Instance::Cast(result).raw(); |
| 34 } | 34 } |
| 35 | 35 |
| 36 | 36 |
| 37 inline Dart_Handle NewString(const char* str) { | 37 inline Dart_Handle NewString(const char* str) { |
| 38 return Dart_NewStringFromCString(str); | 38 return Dart_NewStringFromCString(str); |
| 39 } | 39 } |
| 40 | 40 |
| 41 | 41 |
| 42 DEFINE_NATIVE_ENTRY(Mirrors_isLocalPort, 1) { | 42 DEFINE_NATIVE_ENTRY(Mirrors_isLocalPort, 1) { |
| 43 GET_NON_NULL_NATIVE_ARGUMENT(Instance, port, arguments->NativeArgAt(0)); | 43 GET_NON_NULL_NATIVE_ARGUMENT(Instance, port, arguments->NativeArgAt(0)); |
| 44 | 44 |
| 45 // Get the port id from the SendPort instance. | 45 // Get the port id from the SendPort instance. |
| 46 const Object& id_obj = Object::Handle(DartLibraryCalls::PortGetId(port)); | 46 const Object& id_obj = Object::Handle(DartLibraryCalls::PortGetId(port)); |
| 47 if (id_obj.IsError()) { | 47 if (id_obj.IsError()) { |
| 48 Exceptions::PropagateError(Error::Cast(id_obj)); | 48 Exceptions::PropagateError(Error::Cast(id_obj)); |
| 49 UNREACHABLE(); | 49 UNREACHABLE(); |
| 50 } | 50 } |
| 51 ASSERT(id_obj.IsSmi() || id_obj.IsMint()); | 51 ASSERT(id_obj.IsSmi() || id_obj.IsMint()); |
| 52 Integer& id = Integer::Handle(); | 52 Integer& id = Integer::Handle(); |
| 53 id ^= id_obj.raw(); | 53 id ^= id_obj.raw(); |
| 54 Dart_Port port_id = static_cast<Dart_Port>(id.AsInt64Value()); | 54 Dart_Port port_id = static_cast<Dart_Port>(id.AsInt64Value()); |
| 55 return Bool::Get(PortMap::IsLocalPort(port_id)); | 55 return Bool::Get(PortMap::IsLocalPort(port_id)); |
| 56 } | 56 } |
| 57 | 57 |
| 58 | 58 |
| 59 // TODO(turnidge): Add Map support to the dart embedding api instead | |
| 60 // of implementing it here. | |
| 61 static Dart_Handle CoreLib() { | |
| 62 Dart_Handle core_lib_name = NewString("dart:core"); | |
| 63 return Dart_LookupLibrary(core_lib_name); | |
| 64 } | |
| 65 | |
| 66 | |
| 67 static Dart_Handle MapNew() { | |
| 68 // TODO(turnidge): Switch to an order-preserving map type. | |
| 69 Dart_Handle type = Dart_GetType(CoreLib(), NewString("Map"), 0, NULL); | |
| 70 if (Dart_IsError(type)) { | |
| 71 return type; | |
| 72 } | |
| 73 return Dart_New(type, Dart_Null(), 0, NULL); | |
| 74 } | |
| 75 | |
| 76 | |
| 77 static Dart_Handle MapAdd(Dart_Handle map, Dart_Handle key, Dart_Handle value) { | |
| 78 Dart_Handle args[] = { key, value }; | |
| 79 return Dart_Invoke(map, NewString("[]="), ARRAY_SIZE(args), args); | |
| 80 } | |
| 81 | |
| 82 | |
| 83 static Dart_Handle MirrorLib() { | 59 static Dart_Handle MirrorLib() { |
| 84 Dart_Handle mirror_lib_name = NewString("dart:mirrors"); | 60 Dart_Handle mirror_lib_name = NewString("dart:mirrors"); |
| 85 return Dart_LookupLibrary(mirror_lib_name); | 61 return Dart_LookupLibrary(mirror_lib_name); |
| 86 } | 62 } |
| 87 | 63 |
| 88 | 64 |
| 89 // TODO(11742): Remove once there are no more users of the Dart_Handle-based | 65 // TODO(11742): Remove once there are no more users of the Dart_Handle-based |
| 90 // VMReferences. | 66 // VMReferences. |
| 91 static Dart_Handle CreateMirrorReference(Dart_Handle handle) { | 67 static Dart_Handle CreateMirrorReference(Dart_Handle handle) { |
| 92 Isolate* isolate = Isolate::Current(); | 68 Isolate* isolate = Isolate::Current(); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 | 163 |
| 188 Dart_Handle args[] = { var_name, owner_mirror }; | 164 Dart_Handle args[] = { var_name, owner_mirror }; |
| 189 return Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | 165 return Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); |
| 190 } | 166 } |
| 191 | 167 |
| 192 UNREACHABLE(); | 168 UNREACHABLE(); |
| 193 return Dart_Null(); | 169 return Dart_Null(); |
| 194 } | 170 } |
| 195 | 171 |
| 196 | 172 |
| 197 static Dart_Handle CreateTypeVariableMirrorUsingApi(Dart_Handle type_var, | |
| 198 Dart_Handle type_var_name, | |
| 199 Dart_Handle owner_mirror) { | |
| 200 ASSERT(Dart_IsTypeVariable(type_var)); | |
| 201 Dart_Handle cls_name = NewString("_LocalTypeVariableMirrorImpl"); | |
| 202 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | |
| 203 if (Dart_IsError(type)) { | |
| 204 return type; | |
| 205 } | |
| 206 | |
| 207 Dart_Handle upper_bound = Dart_TypeVariableUpperBound(type_var); | |
| 208 if (Dart_IsError(upper_bound)) { | |
| 209 return upper_bound; | |
| 210 } | |
| 211 | |
| 212 Dart_Handle args[] = { | |
| 213 CreateMirrorReference(type_var), | |
| 214 type_var_name, | |
| 215 owner_mirror, | |
| 216 CreateLazyMirror(upper_bound), | |
| 217 }; | |
| 218 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | |
| 219 return mirror; | |
| 220 } | |
| 221 | |
| 222 | |
| 223 static RawInstance* CreateTypeVariableMirror(const TypeParameter& param, | 173 static RawInstance* CreateTypeVariableMirror(const TypeParameter& param, |
| 224 const Instance& owner_mirror) { | 174 const Instance& owner_mirror) { |
| 225 Instance& retvalue = Instance::Handle(); | 175 const Array& args = Array::Handle(Array::New(3)); |
| 226 Dart_EnterScope(); | 176 args.SetAt(0, param); |
| 227 Isolate* isolate = Isolate::Current(); | 177 args.SetAt(1, String::Handle(param.name())); |
| 228 Dart_Handle param_handle = Api::NewHandle(isolate, param.raw()); | 178 args.SetAt(2, owner_mirror); |
| 229 if (Dart_IsError(param_handle)) { | 179 return CreateMirror(Symbols::_LocalTypeVariableMirrorImpl(), args); |
| 230 Dart_PropagateError(param_handle); | |
| 231 } | |
| 232 Dart_Handle name_handle = Api::NewHandle(isolate, param.Name()); | |
| 233 if (Dart_IsError(name_handle)) { | |
| 234 Dart_PropagateError(name_handle); | |
| 235 } | |
| 236 // Until we get rid of lazy mirrors, we must have owners. | |
| 237 Dart_Handle owner_handle; | |
| 238 if (owner_mirror.IsNull()) { | |
| 239 owner_handle = Api::NewHandle(isolate, param.parameterized_class()); | |
| 240 if (Dart_IsError(owner_handle)) { | |
| 241 Dart_PropagateError(owner_handle); | |
| 242 } | |
| 243 owner_handle = CreateLazyMirror(owner_handle); | |
| 244 if (Dart_IsError(owner_handle)) { | |
| 245 Dart_PropagateError(owner_handle); | |
| 246 } | |
| 247 } else { | |
| 248 owner_handle = Api::NewHandle(isolate, owner_mirror.raw()); | |
| 249 if (Dart_IsError(owner_handle)) { | |
| 250 Dart_PropagateError(owner_handle); | |
| 251 } | |
| 252 } | |
| 253 // TODO(11742): At some point the handle calls will be replaced by inlined | |
| 254 // functionality. | |
| 255 Dart_Handle result = CreateTypeVariableMirrorUsingApi(param_handle, | |
| 256 name_handle, | |
| 257 owner_handle); | |
| 258 if (Dart_IsError(result)) { | |
| 259 Dart_PropagateError(result); | |
| 260 } | |
| 261 retvalue ^= Api::UnwrapHandle(result); | |
| 262 Dart_ExitScope(); | |
| 263 return retvalue.raw(); | |
| 264 } | 180 } |
| 265 | 181 |
| 266 | 182 |
| 267 static Dart_Handle CreateTypeVariableMap(Dart_Handle owner, | 183 // We create a list in native code and let Dart code create the type mirror |
| 268 Dart_Handle owner_mirror) { | 184 // object and the ordered map. |
| 269 ASSERT(Dart_IsClass(owner)); | 185 static RawInstance* CreateTypeVariableList(const Class& cls) { |
| 270 // TODO(turnidge): This should be an immutable map. | 186 const TypeArguments& args = TypeArguments::Handle(cls.type_parameters()); |
| 271 Dart_Handle map = MapNew(); | 187 if (args.IsNull()) { |
| 272 if (Dart_IsError(map)) { | 188 return Object::empty_array().raw(); |
| 273 return map; | |
| 274 } | 189 } |
| 275 | 190 const Array& result = Array::Handle(Array::New(args.Length() * 2)); |
| 276 Dart_Handle names = Dart_GetTypeVariableNames(owner); | 191 TypeParameter& type = TypeParameter::Handle(); |
| 277 if (Dart_IsError(names)) { | 192 String& name = String::Handle(); |
| 278 return names; | 193 for (intptr_t i = 0; i < args.Length(); i++) { |
| 194 type ^= args.TypeAt(i); |
| 195 ASSERT(type.IsTypeParameter()); |
| 196 name ^= type.name(); |
| 197 result.SetAt(2 * i, name); |
| 198 result.SetAt(2 * i + 1, type); |
| 279 } | 199 } |
| 280 intptr_t len; | 200 return result.raw(); |
| 281 Dart_Handle result = Dart_ListLength(names, &len); | |
| 282 if (Dart_IsError(result)) { | |
| 283 return result; | |
| 284 } | |
| 285 for (intptr_t i = 0; i < len; i++) { | |
| 286 Dart_Handle type_var_name = Dart_ListGetAt(names, i); | |
| 287 Dart_Handle type_var = Dart_LookupTypeVariable(owner, type_var_name); | |
| 288 if (Dart_IsError(type_var)) { | |
| 289 return type_var; | |
| 290 } | |
| 291 ASSERT(!Dart_IsNull(type_var)); | |
| 292 Dart_Handle type_var_mirror = | |
| 293 CreateTypeVariableMirrorUsingApi(type_var, type_var_name, owner_mirror); | |
| 294 if (Dart_IsError(type_var_mirror)) { | |
| 295 return type_var_mirror; | |
| 296 } | |
| 297 result = MapAdd(map, type_var_name, type_var_mirror); | |
| 298 if (Dart_IsError(result)) { | |
| 299 return result; | |
| 300 } | |
| 301 } | |
| 302 return map; | |
| 303 } | 201 } |
| 304 | 202 |
| 305 | 203 |
| 306 static Dart_Handle CreateTypedefMirror(Dart_Handle cls, | 204 static Dart_Handle CreateTypedefMirror(Dart_Handle cls, |
| 307 Dart_Handle cls_name, | 205 Dart_Handle cls_name, |
| 308 Dart_Handle owner_mirror) { | 206 Dart_Handle owner_mirror) { |
| 309 Dart_Handle mirror_cls_name = NewString("_LocalTypedefMirrorImpl"); | 207 Dart_Handle mirror_cls_name = NewString("_LocalTypedefMirrorImpl"); |
| 310 Dart_Handle mirror_type = Dart_GetType(MirrorLib(), mirror_cls_name, 0, NULL); | 208 Dart_Handle mirror_type = Dart_GetType(MirrorLib(), mirror_cls_name, 0, NULL); |
| 311 if (Dart_IsError(mirror_type)) { | 209 if (Dart_IsError(mirror_type)) { |
| 312 return mirror_type; | 210 return mirror_type; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 338 // reflection. | 236 // reflection. |
| 339 return CreateTypedefMirror(intf, intf_name, lib_mirror); | 237 return CreateTypedefMirror(intf, intf_name, lib_mirror); |
| 340 } | 238 } |
| 341 | 239 |
| 342 Dart_Handle cls_name = NewString("_LocalClassMirrorImpl"); | 240 Dart_Handle cls_name = NewString("_LocalClassMirrorImpl"); |
| 343 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | 241 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); |
| 344 if (Dart_IsError(type)) { | 242 if (Dart_IsError(type)) { |
| 345 return type; | 243 return type; |
| 346 } | 244 } |
| 347 | 245 |
| 348 Dart_Handle intf_mirror = CreateLazyMirror(intf); | |
| 349 if (Dart_IsError(intf_mirror)) { | |
| 350 return intf_mirror; | |
| 351 } | |
| 352 Dart_Handle type_var_map = CreateTypeVariableMap(intf, intf_mirror); | |
| 353 if (Dart_IsError(type_var_map)) { | |
| 354 return type_var_map; | |
| 355 } | |
| 356 | |
| 357 Dart_Handle args[] = { | 246 Dart_Handle args[] = { |
| 358 CreateMirrorReference(intf), | 247 CreateMirrorReference(intf), |
| 359 Dart_Null(), // "name" | 248 Dart_Null(), // "name" |
| 360 type_var_map, | |
| 361 }; | 249 }; |
| 362 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | 250 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); |
| 363 return mirror; | 251 return mirror; |
| 364 } | 252 } |
| 365 | 253 |
| 366 | 254 |
| 367 static RawInstance* CreateMethodMirror(const Function& func, | 255 static RawInstance* CreateMethodMirror(const Function& func, |
| 368 const Instance& owner_mirror) { | 256 const Instance& owner_mirror) { |
| 369 const Array& args = Array::Handle(Array::New(12)); | 257 const Array& args = Array::Handle(Array::New(12)); |
| 370 args.SetAt(0, MirrorReference::Handle(MirrorReference::New(func))); | 258 args.SetAt(0, MirrorReference::Handle(MirrorReference::New(func))); |
| (...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 767 member_mirror = CreateMethodMirror(func, owner_mirror); | 655 member_mirror = CreateMethodMirror(func, owner_mirror); |
| 768 member_mirrors.Add(member_mirror); | 656 member_mirrors.Add(member_mirror); |
| 769 } | 657 } |
| 770 } | 658 } |
| 771 } | 659 } |
| 772 | 660 |
| 773 return member_mirrors.raw(); | 661 return member_mirrors.raw(); |
| 774 } | 662 } |
| 775 | 663 |
| 776 | 664 |
| 665 DEFINE_NATIVE_ENTRY(ClassMirror_type_variables, 1) { |
| 666 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); |
| 667 const Class& klass = Class::Handle(ref.GetClassReferent()); |
| 668 return CreateTypeVariableList(klass); |
| 669 } |
| 670 |
| 671 |
| 672 DEFINE_NATIVE_ENTRY(LocalTypeVariableMirror_owner, 1) { |
| 673 GET_NON_NULL_NATIVE_ARGUMENT(TypeParameter, param, arguments->NativeArgAt(0)); |
| 674 return CreateClassMirror(Class::Handle(param.parameterized_class()), |
| 675 Instance::null_instance()); |
| 676 } |
| 677 |
| 678 |
| 679 DEFINE_NATIVE_ENTRY(LocalTypeVariableMirror_upper_bound, 1) { |
| 680 GET_NON_NULL_NATIVE_ARGUMENT(TypeParameter, param, arguments->NativeArgAt(0)); |
| 681 return CreateTypeMirror(AbstractType::Handle(param.bound())); |
| 682 } |
| 683 |
| 684 |
| 777 // Invoke the function, or noSuchMethod if it is null. Propagate any unhandled | 685 // Invoke the function, or noSuchMethod if it is null. Propagate any unhandled |
| 778 // exceptions. Wrap and propagate any compilation errors. | 686 // exceptions. Wrap and propagate any compilation errors. |
| 779 static RawObject* ReflectivelyInvokeDynamicFunction(const Instance& receiver, | 687 static RawObject* ReflectivelyInvokeDynamicFunction(const Instance& receiver, |
| 780 const Function& function, | 688 const Function& function, |
| 781 const String& target_name, | 689 const String& target_name, |
| 782 const Array& arguments) { | 690 const Array& arguments) { |
| 783 // Note "arguments" is already the internal arguments with the receiver as | 691 // Note "arguments" is already the internal arguments with the receiver as |
| 784 // the first element. | 692 // the first element. |
| 785 Object& result = Object::Handle(); | 693 Object& result = Object::Handle(); |
| 786 if (function.IsNull()) { | 694 if (function.IsNull()) { |
| (...skipping 619 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1406 | 1314 |
| 1407 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { | 1315 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { |
| 1408 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); | 1316 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); |
| 1409 const Field& field = Field::Handle(ref.GetFieldReferent()); | 1317 const Field& field = Field::Handle(ref.GetFieldReferent()); |
| 1410 | 1318 |
| 1411 const AbstractType& type = AbstractType::Handle(field.type()); | 1319 const AbstractType& type = AbstractType::Handle(field.type()); |
| 1412 return CreateTypeMirror(type); | 1320 return CreateTypeMirror(type); |
| 1413 } | 1321 } |
| 1414 | 1322 |
| 1415 } // namespace dart | 1323 } // namespace dart |
| OLD | NEW |