Chromium Code Reviews| 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 | 59 // TODO(turnidge): Add Map support to the dart embedding api instead |
| 60 // of implementing it here. | 60 // of implementing it here. |
| 61 static Dart_Handle CoreLib() { | 61 static Dart_Handle CoreLib() { |
|
rmacnak
2013/07/30 20:20:23
Dead code.
Michael Lippautz (Google)
2013/07/30 20:26:47
Done.
| |
| 62 Dart_Handle core_lib_name = NewString("dart:core"); | 62 Dart_Handle core_lib_name = NewString("dart:core"); |
| 63 return Dart_LookupLibrary(core_lib_name); | 63 return Dart_LookupLibrary(core_lib_name); |
| 64 } | 64 } |
| 65 | 65 |
| 66 | 66 |
| 67 static Dart_Handle MapNew() { | 67 static Dart_Handle MapNew() { |
|
rmacnak
2013/07/30 20:20:23
Dead code.
Michael Lippautz (Google)
2013/07/30 20:26:47
Done.
| |
| 68 // TODO(turnidge): Switch to an order-preserving map type. | 68 // TODO(turnidge): Switch to an order-preserving map type. |
| 69 Dart_Handle type = Dart_GetType(CoreLib(), NewString("Map"), 0, NULL); | 69 Dart_Handle type = Dart_GetType(CoreLib(), NewString("Map"), 0, NULL); |
| 70 if (Dart_IsError(type)) { | 70 if (Dart_IsError(type)) { |
| 71 return type; | 71 return type; |
| 72 } | 72 } |
| 73 return Dart_New(type, Dart_Null(), 0, NULL); | 73 return Dart_New(type, Dart_Null(), 0, NULL); |
| 74 } | 74 } |
| 75 | 75 |
| 76 | 76 |
| 77 static Dart_Handle MapAdd(Dart_Handle map, Dart_Handle key, Dart_Handle value) { | 77 static Dart_Handle MapAdd(Dart_Handle map, Dart_Handle key, Dart_Handle value) { |
|
rmacnak
2013/07/30 20:20:23
Dead code.
Michael Lippautz (Google)
2013/07/30 20:26:47
Done.
| |
| 78 Dart_Handle args[] = { key, value }; | 78 Dart_Handle args[] = { key, value }; |
| 79 return Dart_Invoke(map, NewString("[]="), ARRAY_SIZE(args), args); | 79 return Dart_Invoke(map, NewString("[]="), ARRAY_SIZE(args), args); |
| 80 } | 80 } |
| 81 | 81 |
| 82 | 82 |
| 83 static Dart_Handle MirrorLib() { | 83 static Dart_Handle MirrorLib() { |
| 84 Dart_Handle mirror_lib_name = NewString("dart:mirrors"); | 84 Dart_Handle mirror_lib_name = NewString("dart:mirrors"); |
| 85 return Dart_LookupLibrary(mirror_lib_name); | 85 return Dart_LookupLibrary(mirror_lib_name); |
| 86 } | 86 } |
| 87 | 87 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 | 187 |
| 188 Dart_Handle args[] = { var_name, owner_mirror }; | 188 Dart_Handle args[] = { var_name, owner_mirror }; |
| 189 return Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | 189 return Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); |
| 190 } | 190 } |
| 191 | 191 |
| 192 UNREACHABLE(); | 192 UNREACHABLE(); |
| 193 return Dart_Null(); | 193 return Dart_Null(); |
| 194 } | 194 } |
| 195 | 195 |
| 196 | 196 |
| 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, | 197 static RawInstance* CreateTypeVariableMirror(const TypeParameter& param, |
| 224 const Instance& owner_mirror) { | 198 const Instance& owner_mirror) { |
| 225 Instance& retvalue = Instance::Handle(); | 199 const Array& args = Array::Handle(Array::New(3)); |
| 226 Dart_EnterScope(); | 200 args.SetAt(0, param); |
| 227 Isolate* isolate = Isolate::Current(); | 201 args.SetAt(1, String::Handle(param.name())); |
| 228 Dart_Handle param_handle = Api::NewHandle(isolate, param.raw()); | 202 args.SetAt(2, owner_mirror); |
| 229 if (Dart_IsError(param_handle)) { | 203 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 } | 204 } |
| 265 | 205 |
| 266 | 206 |
| 267 static Dart_Handle CreateTypeVariableMap(Dart_Handle owner, | 207 // We create a list in native code and let Dart code create the type mirror |
| 268 Dart_Handle owner_mirror) { | 208 // object and the ordered map. |
| 269 ASSERT(Dart_IsClass(owner)); | 209 static RawInstance* CreateTypeVariableList(const Class& cls) { |
| 270 // TODO(turnidge): This should be an immutable map. | 210 TypeArguments& args = TypeArguments::Handle(cls.type_parameters()); |
|
rmacnak
2013/07/30 20:20:23
const
Michael Lippautz (Google)
2013/07/30 20:26:47
Done.
| |
| 271 Dart_Handle map = MapNew(); | 211 if (args.IsNull()) { |
| 272 if (Dart_IsError(map)) { | 212 return Object::empty_array().raw(); |
| 273 return map; | |
| 274 } | 213 } |
| 275 | 214 const Array& result = Array::Handle(Array::New(args.Length() * 2)); |
| 276 Dart_Handle names = Dart_GetTypeVariableNames(owner); | 215 TypeParameter& type = TypeParameter::Handle(); |
| 277 if (Dart_IsError(names)) { | 216 String& name = String::Handle(); |
| 278 return names; | 217 for (intptr_t i = 0; i < args.Length(); i++) { |
| 218 type ^= args.TypeAt(i); | |
| 219 ASSERT(type.IsTypeParameter()); | |
| 220 name ^= type.name(); | |
| 221 result.SetAt(2 * i, name); | |
| 222 result.SetAt(2 * i + 1, type); | |
| 279 } | 223 } |
| 280 intptr_t len; | 224 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 } | 225 } |
| 304 | 226 |
| 305 | 227 |
| 306 static Dart_Handle CreateTypedefMirror(Dart_Handle cls, | 228 static Dart_Handle CreateTypedefMirror(Dart_Handle cls, |
| 307 Dart_Handle cls_name, | 229 Dart_Handle cls_name, |
| 308 Dart_Handle owner_mirror) { | 230 Dart_Handle owner_mirror) { |
| 309 Dart_Handle mirror_cls_name = NewString("_LocalTypedefMirrorImpl"); | 231 Dart_Handle mirror_cls_name = NewString("_LocalTypedefMirrorImpl"); |
| 310 Dart_Handle mirror_type = Dart_GetType(MirrorLib(), mirror_cls_name, 0, NULL); | 232 Dart_Handle mirror_type = Dart_GetType(MirrorLib(), mirror_cls_name, 0, NULL); |
| 311 if (Dart_IsError(mirror_type)) { | 233 if (Dart_IsError(mirror_type)) { |
| 312 return mirror_type; | 234 return mirror_type; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 338 // reflection. | 260 // reflection. |
| 339 return CreateTypedefMirror(intf, intf_name, lib_mirror); | 261 return CreateTypedefMirror(intf, intf_name, lib_mirror); |
| 340 } | 262 } |
| 341 | 263 |
| 342 Dart_Handle cls_name = NewString("_LocalClassMirrorImpl"); | 264 Dart_Handle cls_name = NewString("_LocalClassMirrorImpl"); |
| 343 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | 265 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); |
| 344 if (Dart_IsError(type)) { | 266 if (Dart_IsError(type)) { |
| 345 return type; | 267 return type; |
| 346 } | 268 } |
| 347 | 269 |
| 348 Dart_Handle intf_mirror = CreateLazyMirror(intf); | 270 Dart_Handle intf_mirror = CreateLazyMirror(intf); |
|
rmacnak
2013/07/30 20:20:23
Dead code.
Michael Lippautz (Google)
2013/07/30 20:26:47
Done.
| |
| 349 if (Dart_IsError(intf_mirror)) { | 271 if (Dart_IsError(intf_mirror)) { |
| 350 return intf_mirror; | 272 return intf_mirror; |
| 351 } | 273 } |
| 352 Dart_Handle type_var_map = CreateTypeVariableMap(intf, intf_mirror); | |
| 353 if (Dart_IsError(type_var_map)) { | |
| 354 return type_var_map; | |
| 355 } | |
| 356 | 274 |
| 357 Dart_Handle args[] = { | 275 Dart_Handle args[] = { |
| 358 CreateMirrorReference(intf), | 276 CreateMirrorReference(intf), |
| 359 Dart_Null(), // "name" | 277 Dart_Null(), // "name" |
| 360 type_var_map, | |
| 361 }; | 278 }; |
| 362 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | 279 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); |
| 363 return mirror; | 280 return mirror; |
| 364 } | 281 } |
| 365 | 282 |
| 366 | 283 |
| 367 static RawInstance* CreateMethodMirror(const Function& func, | 284 static RawInstance* CreateMethodMirror(const Function& func, |
| 368 const Instance& owner_mirror) { | 285 const Instance& owner_mirror) { |
| 369 const Array& args = Array::Handle(Array::New(11)); | 286 const Array& args = Array::Handle(Array::New(11)); |
| 370 args.SetAt(0, MirrorReference::Handle(MirrorReference::New(func))); | 287 args.SetAt(0, MirrorReference::Handle(MirrorReference::New(func))); |
| (...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 795 member_mirror = CreateMethodMirror(func, owner_mirror); | 712 member_mirror = CreateMethodMirror(func, owner_mirror); |
| 796 member_mirrors.Add(member_mirror); | 713 member_mirrors.Add(member_mirror); |
| 797 } | 714 } |
| 798 } | 715 } |
| 799 } | 716 } |
| 800 | 717 |
| 801 return member_mirrors.raw(); | 718 return member_mirrors.raw(); |
| 802 } | 719 } |
| 803 | 720 |
| 804 | 721 |
| 722 DEFINE_NATIVE_ENTRY(ClassMirror_type_variables, 1) { | |
| 723 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); | |
| 724 const Class& klass = Class::Handle(ref.GetClassReferent()); | |
| 725 return CreateTypeVariableList(klass); | |
| 726 } | |
| 727 | |
| 728 | |
| 729 DEFINE_NATIVE_ENTRY(LocalTypeVariableMirror_owner, 1) { | |
| 730 GET_NON_NULL_NATIVE_ARGUMENT(TypeParameter, param, arguments->NativeArgAt(0)); | |
| 731 return CreateClassMirror(Class::Handle(param.parameterized_class()), | |
| 732 Instance::null_instance()); | |
| 733 } | |
| 734 | |
| 735 | |
| 736 DEFINE_NATIVE_ENTRY(LocalTypeVariableMirror_upper_bound, 1) { | |
| 737 GET_NON_NULL_NATIVE_ARGUMENT(TypeParameter, param, arguments->NativeArgAt(0)); | |
| 738 return CreateTypeMirror(AbstractType::Handle(param.bound())); | |
| 739 } | |
| 740 | |
| 741 | |
| 805 // Invoke the function, or noSuchMethod if it is null. Propagate any unhandled | 742 // Invoke the function, or noSuchMethod if it is null. Propagate any unhandled |
| 806 // exceptions. Wrap and propagate any compilation errors. | 743 // exceptions. Wrap and propagate any compilation errors. |
| 807 static RawObject* ReflectivelyInvokeDynamicFunction(const Instance& receiver, | 744 static RawObject* ReflectivelyInvokeDynamicFunction(const Instance& receiver, |
| 808 const Function& function, | 745 const Function& function, |
| 809 const String& target_name, | 746 const String& target_name, |
| 810 const Array& arguments) { | 747 const Array& arguments) { |
| 811 // Note "arguments" is already the internal arguments with the receiver as | 748 // Note "arguments" is already the internal arguments with the receiver as |
| 812 // the first element. | 749 // the first element. |
| 813 Object& result = Object::Handle(); | 750 Object& result = Object::Handle(); |
| 814 if (function.IsNull()) { | 751 if (function.IsNull()) { |
| (...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1441 | 1378 |
| 1442 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { | 1379 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { |
| 1443 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); | 1380 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); |
| 1444 const Field& field = Field::Handle(ref.GetFieldReferent()); | 1381 const Field& field = Field::Handle(ref.GetFieldReferent()); |
| 1445 | 1382 |
| 1446 const AbstractType& type = AbstractType::Handle(field.type()); | 1383 const AbstractType& type = AbstractType::Handle(field.type()); |
| 1447 return CreateTypeMirror(type); | 1384 return CreateTypeMirror(type); |
| 1448 } | 1385 } |
| 1449 | 1386 |
| 1450 } // namespace dart | 1387 } // namespace dart |
| OLD | NEW |