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" |
| 11 #include "vm/dart_entry.h" | 11 #include "vm/dart_entry.h" |
| 12 #include "vm/exceptions.h" | 12 #include "vm/exceptions.h" |
| 13 #include "vm/message.h" | 13 #include "vm/message.h" |
| 14 #include "vm/port.h" | 14 #include "vm/port.h" |
| 15 #include "vm/resolver.h" | 15 #include "vm/resolver.h" |
| 16 #include "vm/symbols.h" | 16 #include "vm/symbols.h" |
| 17 | 17 |
| 18 namespace dart { | 18 namespace dart { |
| 19 | 19 |
| 20 static RawInstance* CreateMirror(const String& mirror_class_name, | |
| 21 const Array& constructor_arguments) { | |
| 22 const Library& mirrors_lib = Library::Handle(Library::MirrorsLibrary()); | |
| 23 const String& constructor_name = Symbols::Dot(); | |
| 24 | |
| 25 const Object& result = Object::Handle( | |
| 26 DartLibraryCalls::InstanceCreate(mirrors_lib, | |
| 27 mirror_class_name, | |
| 28 constructor_name, | |
| 29 constructor_arguments)); | |
| 30 ASSERT(result.IsInstance()); | |
| 31 return Instance::Cast(result).raw(); | |
| 32 } | |
| 33 | |
| 34 | |
| 20 inline Dart_Handle NewString(const char* str) { | 35 inline Dart_Handle NewString(const char* str) { |
| 21 return Dart_NewStringFromCString(str); | 36 return Dart_NewStringFromCString(str); |
| 22 } | 37 } |
| 23 | 38 |
| 24 | 39 |
| 25 DEFINE_NATIVE_ENTRY(Mirrors_isLocalPort, 1) { | 40 DEFINE_NATIVE_ENTRY(Mirrors_isLocalPort, 1) { |
| 26 GET_NON_NULL_NATIVE_ARGUMENT(Instance, port, arguments->NativeArgAt(0)); | 41 GET_NON_NULL_NATIVE_ARGUMENT(Instance, port, arguments->NativeArgAt(0)); |
| 27 | 42 |
| 28 // Get the port id from the SendPort instance. | 43 // Get the port id from the SendPort instance. |
| 29 const Object& id_obj = Object::Handle(DartLibraryCalls::PortGetId(port)); | 44 const Object& id_obj = Object::Handle(DartLibraryCalls::PortGetId(port)); |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 348 } | 363 } |
| 349 Dart_Handle result = Dart_ListSetAt(mirror_list, i, mirror); | 364 Dart_Handle result = Dart_ListSetAt(mirror_list, i, mirror); |
| 350 if (Dart_IsError(result)) { | 365 if (Dart_IsError(result)) { |
| 351 return result; | 366 return result; |
| 352 } | 367 } |
| 353 } | 368 } |
| 354 return mirror_list; | 369 return mirror_list; |
| 355 } | 370 } |
| 356 | 371 |
| 357 | 372 |
| 358 static Dart_Handle CreateTypeVariableMirror(Dart_Handle type_var, | 373 static Dart_Handle CreateTypeVariableMirrorUsingApi(Dart_Handle type_var, |
| 359 Dart_Handle type_var_name, | 374 Dart_Handle type_var_name, |
| 360 Dart_Handle owner_mirror) { | 375 Dart_Handle owner_mirror) { |
| 361 ASSERT(Dart_IsTypeVariable(type_var)); | 376 ASSERT(Dart_IsTypeVariable(type_var)); |
| 362 Dart_Handle cls_name = NewString("_LocalTypeVariableMirrorImpl"); | 377 Dart_Handle cls_name = NewString("_LocalTypeVariableMirrorImpl"); |
| 363 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | 378 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); |
| 364 if (Dart_IsError(type)) { | 379 if (Dart_IsError(type)) { |
| 365 return type; | 380 return type; |
| 366 } | 381 } |
| 367 | 382 |
| 368 Dart_Handle upper_bound = Dart_TypeVariableUpperBound(type_var); | 383 Dart_Handle upper_bound = Dart_TypeVariableUpperBound(type_var); |
| 369 if (Dart_IsError(upper_bound)) { | 384 if (Dart_IsError(upper_bound)) { |
| 370 return upper_bound; | 385 return upper_bound; |
| 371 } | 386 } |
| 372 | 387 |
| 373 Dart_Handle args[] = { | 388 Dart_Handle args[] = { |
| 374 CreateMirrorReference(type_var), | 389 CreateMirrorReference(type_var), |
| 375 type_var_name, | 390 type_var_name, |
| 376 owner_mirror, | 391 owner_mirror, |
| 377 CreateLazyMirror(upper_bound), | 392 CreateLazyMirror(upper_bound), |
| 378 }; | 393 }; |
| 379 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | 394 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); |
| 380 return mirror; | 395 return mirror; |
| 381 } | 396 } |
| 382 | 397 |
| 383 | 398 |
| 399 static RawInstance* CreateTypeVariableMirror(const TypeParameter& param, | |
| 400 const Instance& owner_mirror) { | |
| 401 Instance& retvalue = Instance::Handle(); | |
| 402 Dart_EnterScope(); | |
| 403 Isolate* isolate = Isolate::Current(); | |
| 404 Dart_Handle param_handle = Api::NewHandle(isolate, param.raw()); | |
| 405 if (Dart_IsError(param_handle)) { | |
| 406 Dart_PropagateError(param_handle); | |
| 407 } | |
| 408 Dart_Handle name_handle = Api::NewHandle(isolate, param.Name()); | |
| 409 if (Dart_IsError(name_handle)) { | |
| 410 Dart_PropagateError(name_handle); | |
| 411 } | |
| 412 // Until we get rid of lazy mirrors, we must have owners. | |
| 413 Dart_Handle owner_handle; | |
| 414 if (owner_mirror.IsNull()) { | |
| 415 owner_handle = Api::NewHandle(isolate, param.parameterized_class()); | |
| 416 if (Dart_IsError(owner_handle)) { | |
| 417 Dart_PropagateError(owner_handle); | |
| 418 } | |
| 419 owner_handle = CreateLazyMirror(owner_handle); | |
| 420 if (Dart_IsError(owner_handle)) { | |
| 421 Dart_PropagateError(owner_handle); | |
| 422 } | |
| 423 } else { | |
| 424 owner_handle = Api::NewHandle(isolate, owner_mirror.raw()); | |
| 425 if (Dart_IsError(owner_handle)) { | |
| 426 Dart_PropagateError(owner_handle); | |
| 427 } | |
| 428 } | |
| 429 // TODO(11742): At some point the handle calls will be replaced by inlined | |
| 430 // functionality. | |
| 431 Dart_Handle result = CreateTypeVariableMirrorUsingApi(param_handle, | |
| 432 name_handle, | |
| 433 owner_handle); | |
| 434 if (Dart_IsError(result)) { | |
| 435 Dart_PropagateError(result); | |
| 436 } | |
| 437 retvalue ^= Api::UnwrapHandle(result); | |
| 438 Dart_ExitScope(); | |
| 439 return retvalue.raw(); | |
| 440 } | |
| 441 | |
| 442 | |
| 384 static Dart_Handle CreateTypeVariableMap(Dart_Handle owner, | 443 static Dart_Handle CreateTypeVariableMap(Dart_Handle owner, |
| 385 Dart_Handle owner_mirror) { | 444 Dart_Handle owner_mirror) { |
| 386 ASSERT(Dart_IsClass(owner)); | 445 ASSERT(Dart_IsClass(owner)); |
| 387 // TODO(turnidge): This should be an immutable map. | 446 // TODO(turnidge): This should be an immutable map. |
| 388 Dart_Handle map = MapNew(); | 447 Dart_Handle map = MapNew(); |
| 389 if (Dart_IsError(map)) { | 448 if (Dart_IsError(map)) { |
| 390 return map; | 449 return map; |
| 391 } | 450 } |
| 392 | 451 |
| 393 Dart_Handle names = Dart_GetTypeVariableNames(owner); | 452 Dart_Handle names = Dart_GetTypeVariableNames(owner); |
| 394 if (Dart_IsError(names)) { | 453 if (Dart_IsError(names)) { |
| 395 return names; | 454 return names; |
| 396 } | 455 } |
| 397 intptr_t len; | 456 intptr_t len; |
| 398 Dart_Handle result = Dart_ListLength(names, &len); | 457 Dart_Handle result = Dart_ListLength(names, &len); |
| 399 if (Dart_IsError(result)) { | 458 if (Dart_IsError(result)) { |
| 400 return result; | 459 return result; |
| 401 } | 460 } |
| 402 for (intptr_t i = 0; i < len; i++) { | 461 for (intptr_t i = 0; i < len; i++) { |
| 403 Dart_Handle type_var_name = Dart_ListGetAt(names, i); | 462 Dart_Handle type_var_name = Dart_ListGetAt(names, i); |
| 404 Dart_Handle type_var = Dart_LookupTypeVariable(owner, type_var_name); | 463 Dart_Handle type_var = Dart_LookupTypeVariable(owner, type_var_name); |
| 405 if (Dart_IsError(type_var)) { | 464 if (Dart_IsError(type_var)) { |
| 406 return type_var; | 465 return type_var; |
| 407 } | 466 } |
| 408 ASSERT(!Dart_IsNull(type_var)); | 467 ASSERT(!Dart_IsNull(type_var)); |
| 409 Dart_Handle type_var_mirror = | 468 Dart_Handle type_var_mirror = |
| 410 CreateTypeVariableMirror(type_var, type_var_name, owner_mirror); | 469 CreateTypeVariableMirrorUsingApi(type_var, type_var_name, owner_mirror); |
| 411 if (Dart_IsError(type_var_mirror)) { | 470 if (Dart_IsError(type_var_mirror)) { |
| 412 return type_var_mirror; | 471 return type_var_mirror; |
| 413 } | 472 } |
| 414 result = MapAdd(map, type_var_name, type_var_mirror); | 473 result = MapAdd(map, type_var_name, type_var_mirror); |
| 415 if (Dart_IsError(result)) { | 474 if (Dart_IsError(result)) { |
| 416 return result; | 475 return result; |
| 417 } | 476 } |
| 418 } | 477 } |
| 419 return map; | 478 return map; |
| 420 } | 479 } |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 506 }; | 565 }; |
| 507 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | 566 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); |
| 508 return mirror; | 567 return mirror; |
| 509 } | 568 } |
| 510 | 569 |
| 511 | 570 |
| 512 static Dart_Handle CreateMethodMirrorUsingApi(Dart_Handle func, | 571 static Dart_Handle CreateMethodMirrorUsingApi(Dart_Handle func, |
| 513 Dart_Handle owner_mirror) { | 572 Dart_Handle owner_mirror) { |
| 514 // TODO(11742): Unwrapping is needed until the whole method is converted. | 573 // TODO(11742): Unwrapping is needed until the whole method is converted. |
| 515 Isolate* isolate = Isolate::Current(); | 574 Isolate* isolate = Isolate::Current(); |
| 516 DARTSCOPE(isolate); | |
| 517 const Function& func_obj = Api::UnwrapFunctionHandle(isolate, func); | 575 const Function& func_obj = Api::UnwrapFunctionHandle(isolate, func); |
| 518 | 576 |
| 519 Dart_Handle mirror_cls_name = NewString("_LocalMethodMirrorImpl"); | 577 Dart_Handle mirror_cls_name = NewString("_LocalMethodMirrorImpl"); |
| 520 Dart_Handle mirror_type = Dart_GetType(MirrorLib(), mirror_cls_name, 0, NULL); | 578 Dart_Handle mirror_type = Dart_GetType(MirrorLib(), mirror_cls_name, 0, NULL); |
| 521 if (Dart_IsError(mirror_type)) { | 579 if (Dart_IsError(mirror_type)) { |
| 522 return mirror_type; | 580 return mirror_type; |
| 523 } | 581 } |
| 524 | 582 |
| 525 Dart_Handle return_type = Dart_FunctionReturnType(func); | |
| 526 if (Dart_IsError(return_type)) { | |
| 527 return return_type; | |
| 528 } | |
| 529 | |
| 530 // TODO(turnidge): Implement constructor kinds (arguments 7 - 10). | 583 // TODO(turnidge): Implement constructor kinds (arguments 7 - 10). |
| 531 Dart_Handle args[] = { | 584 Dart_Handle args[] = { |
| 532 CreateMirrorReference(func), | 585 CreateMirrorReference(func), |
| 533 owner_mirror, | 586 owner_mirror, |
| 534 CreateParameterMirrorList(func), | 587 CreateParameterMirrorList(func), |
| 535 CreateLazyMirror(return_type), | |
| 536 func_obj.is_static() ? Api::True() : Api::False(), | 588 func_obj.is_static() ? Api::True() : Api::False(), |
| 537 func_obj.is_abstract() ? Api::True() : Api::False(), | 589 func_obj.is_abstract() ? Api::True() : Api::False(), |
| 538 func_obj.IsGetterFunction() ? Api::True() : Api::False(), | 590 func_obj.IsGetterFunction() ? Api::True() : Api::False(), |
| 539 func_obj.IsSetterFunction() ? Api::True() : Api::False(), | 591 func_obj.IsSetterFunction() ? Api::True() : Api::False(), |
| 540 func_obj.IsConstructor() ? Api::True() : Api::False(), | 592 func_obj.IsConstructor() ? Api::True() : Api::False(), |
| 541 Api::False(), | 593 Api::False(), |
| 542 Api::False(), | 594 Api::False(), |
| 543 Api::False(), | 595 Api::False(), |
| 544 Api::False() | 596 Api::False() |
| 545 }; | 597 }; |
| (...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1024 Dart_Handle result = CreateMethodMirrorUsingApi(func_handle, owner_handle); | 1076 Dart_Handle result = CreateMethodMirrorUsingApi(func_handle, owner_handle); |
| 1025 if (Dart_IsError(result)) { | 1077 if (Dart_IsError(result)) { |
| 1026 Dart_PropagateError(result); | 1078 Dart_PropagateError(result); |
| 1027 } | 1079 } |
| 1028 retvalue ^= Api::UnwrapHandle(result); | 1080 retvalue ^= Api::UnwrapHandle(result); |
| 1029 Dart_ExitScope(); | 1081 Dart_ExitScope(); |
| 1030 return retvalue.raw(); | 1082 return retvalue.raw(); |
| 1031 } | 1083 } |
| 1032 | 1084 |
| 1033 | 1085 |
| 1086 static RawInstance* CreateTypeMirror(const AbstractType& type) { | |
| 1087 ASSERT(!type.IsMalformed()); | |
| 1088 if (type.HasResolvedTypeClass()) { | |
| 1089 const Class& cls = Class::Handle(type.type_class()); | |
| 1090 // Handle void and dynamic types. | |
| 1091 if (cls.IsVoidClass()) { | |
| 1092 Array& args = Array::Handle(Array::New(1)); | |
| 1093 args.SetAt(0, Symbols::Void()); | |
| 1094 return CreateMirror(Symbols::_SpecialTypeMirrorImpl(), args); | |
|
siva
2013/07/19 22:59:01
This ends up creating a Mirror object everytime we
Michael Lippautz (Google)
2013/07/19 23:09:27
Added a TODO.
| |
| 1095 } else if (cls.IsDynamicClass()) { | |
| 1096 Array& args = Array::Handle(Array::New(1)); | |
| 1097 args.SetAt(0, Symbols::Dynamic()); | |
| 1098 return CreateMirror(Symbols::_SpecialTypeMirrorImpl(), args); | |
| 1099 } | |
| 1100 return CreateClassMirror(cls, Instance::Handle()); | |
|
siva
2013/07/19 22:59:01
Object::null_instance() here too instead of Instan
Michael Lippautz (Google)
2013/07/19 23:09:27
Done.
| |
| 1101 } else if (type.IsTypeParameter()) { | |
| 1102 return CreateTypeVariableMirror(TypeParameter::Cast(type), | |
| 1103 Object::null_instance()); | |
| 1104 } | |
| 1105 UNREACHABLE(); | |
| 1106 return Instance::null(); | |
| 1107 } | |
| 1108 | |
| 1109 | |
| 1034 void NATIVE_ENTRY_FUNCTION(Mirrors_makeLocalMirrorSystem)( | 1110 void NATIVE_ENTRY_FUNCTION(Mirrors_makeLocalMirrorSystem)( |
| 1035 Dart_NativeArguments args) { | 1111 Dart_NativeArguments args) { |
| 1036 Dart_EnterScope(); | 1112 Dart_EnterScope(); |
| 1037 Dart_Handle mirrors = CreateMirrorSystem(); | 1113 Dart_Handle mirrors = CreateMirrorSystem(); |
| 1038 if (Dart_IsError(mirrors)) { | 1114 if (Dart_IsError(mirrors)) { |
| 1039 Dart_PropagateError(mirrors); | 1115 Dart_PropagateError(mirrors); |
| 1040 } | 1116 } |
| 1041 Dart_SetReturnValue(args, mirrors); | 1117 Dart_SetReturnValue(args, mirrors); |
| 1042 Dart_ExitScope(); | 1118 Dart_ExitScope(); |
| 1043 } | 1119 } |
| (...skipping 779 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1823 return CreateMethodMirror(Function::Handle( | 1899 return CreateMethodMirror(Function::Handle( |
| 1824 func.parent_function()), Instance::Handle()); | 1900 func.parent_function()), Instance::Handle()); |
| 1825 } | 1901 } |
| 1826 const Class& owner = Class::Handle(func.Owner()); | 1902 const Class& owner = Class::Handle(func.Owner()); |
| 1827 if (owner.IsTopLevel()) { | 1903 if (owner.IsTopLevel()) { |
| 1828 return CreateLibraryMirror(Library::Handle(owner.library())); | 1904 return CreateLibraryMirror(Library::Handle(owner.library())); |
| 1829 } | 1905 } |
| 1830 return CreateClassMirror(owner, Instance::Handle()); | 1906 return CreateClassMirror(owner, Instance::Handle()); |
| 1831 } | 1907 } |
| 1832 | 1908 |
| 1909 | |
| 1910 DEFINE_NATIVE_ENTRY(MethodMirror_return_type, 1) { | |
| 1911 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); | |
| 1912 const Function& func = Function::Handle(ref.GetFunctionReferent()); | |
| 1913 // We handle constructors in Dart code. | |
| 1914 ASSERT(!func.IsConstructor()); | |
| 1915 const AbstractType& return_type = AbstractType::Handle(func.result_type()); | |
| 1916 return CreateTypeMirror(return_type); | |
| 1917 } | |
| 1918 | |
| 1833 } // namespace dart | 1919 } // namespace dart |
| OLD | NEW |