| 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 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 args.SetAt(6, func.IsConstructor() ? Bool::True() : Bool::False()); | 413 args.SetAt(6, func.IsConstructor() ? Bool::True() : Bool::False()); |
| 414 // TODO(mlippautz): Implement different constructor kinds. | 414 // TODO(mlippautz): Implement different constructor kinds. |
| 415 args.SetAt(7, Bool::False()); | 415 args.SetAt(7, Bool::False()); |
| 416 args.SetAt(8, Bool::False()); | 416 args.SetAt(8, Bool::False()); |
| 417 args.SetAt(9, Bool::False()); | 417 args.SetAt(9, Bool::False()); |
| 418 args.SetAt(10, Bool::False()); | 418 args.SetAt(10, Bool::False()); |
| 419 return CreateMirror(Symbols::_LocalMethodMirrorImpl(), args); | 419 return CreateMirror(Symbols::_LocalMethodMirrorImpl(), args); |
| 420 } | 420 } |
| 421 | 421 |
| 422 | 422 |
| 423 static Dart_Handle CreateMethodMirrorUsingApi(Dart_Handle func, | |
| 424 Dart_Handle owner_mirror) { | |
| 425 Isolate* isolate = Isolate::Current(); | |
| 426 return Api::NewHandle(isolate, CreateMethodMirror( | |
| 427 Api::UnwrapFunctionHandle(isolate, func), | |
| 428 Api::UnwrapInstanceHandle(isolate, owner_mirror))); | |
| 429 } | |
| 430 | |
| 431 static RawInstance* CreateVariableMirror(const Field& field, | 423 static RawInstance* CreateVariableMirror(const Field& field, |
| 432 const Instance& owner_mirror) { | 424 const Instance& owner_mirror) { |
| 433 const MirrorReference& field_ref = | 425 const MirrorReference& field_ref = |
| 434 MirrorReference::Handle(MirrorReference::New(field)); | 426 MirrorReference::Handle(MirrorReference::New(field)); |
| 435 | 427 |
| 436 const String& name = String::Handle(field.UserVisibleName()); | 428 const String& name = String::Handle(field.UserVisibleName()); |
| 437 | 429 |
| 438 const Array& args = Array::Handle(Array::New(6)); | 430 const Array& args = Array::Handle(Array::New(6)); |
| 439 args.SetAt(0, field_ref); | 431 args.SetAt(0, field_ref); |
| 440 args.SetAt(1, name); | 432 args.SetAt(1, name); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 }; | 530 }; |
| 539 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | 531 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); |
| 540 if (Dart_IsError(mirror)) { | 532 if (Dart_IsError(mirror)) { |
| 541 return mirror; | 533 return mirror; |
| 542 } | 534 } |
| 543 | 535 |
| 544 return mirror; | 536 return mirror; |
| 545 } | 537 } |
| 546 | 538 |
| 547 | 539 |
| 548 static Dart_Handle CreateNullMirror() { | |
| 549 Dart_Handle cls_name = NewString("_LocalInstanceMirrorImpl"); | |
| 550 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | |
| 551 if (Dart_IsError(type)) { | |
| 552 return type; | |
| 553 } | |
| 554 | |
| 555 // TODO(turnidge): This is wrong. The Null class is distinct from object. | |
| 556 Dart_Handle object_class = Dart_GetClass(CoreLib(), NewString("Object")); | |
| 557 | |
| 558 Dart_Handle args[] = { | |
| 559 CreateLazyMirror(object_class), | |
| 560 Dart_Null(), | |
| 561 }; | |
| 562 Dart_Handle mirror = Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | |
| 563 return mirror; | |
| 564 } | |
| 565 | |
| 566 | |
| 567 static Dart_Handle CreateInstanceMirror(Dart_Handle instance) { | |
| 568 if (Dart_IsNull(instance)) { | |
| 569 return CreateNullMirror(); | |
| 570 } | |
| 571 ASSERT(Dart_IsInstance(instance)); | |
| 572 | |
| 573 Dart_Handle instance_cls = Dart_InstanceGetClass(instance); | |
| 574 if (Dart_IsError(instance_cls)) { | |
| 575 return instance_cls; | |
| 576 } | |
| 577 | |
| 578 if (Dart_IsClosure(instance)) { | |
| 579 Dart_Handle cls_name = NewString("_LocalClosureMirrorImpl"); | |
| 580 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | |
| 581 if (Dart_IsError(type)) { | |
| 582 return type; | |
| 583 } | |
| 584 // We set the function field of ClosureMirrors outside of the constructor | |
| 585 // to break the mutual recursion. | |
| 586 Dart_Handle func = Dart_ClosureFunction(instance); | |
| 587 if (Dart_IsError(func)) { | |
| 588 return func; | |
| 589 } | |
| 590 | |
| 591 // TODO(turnidge): Why not use the real function name here? | |
| 592 Dart_Handle func_owner = Dart_FunctionOwner(func); | |
| 593 if (Dart_IsError(func_owner)) { | |
| 594 return func_owner; | |
| 595 } | |
| 596 | |
| 597 // TODO(turnidge): Pass the function owner here. This will require | |
| 598 // us to support functions in CreateLazyMirror. | |
| 599 Dart_Handle func_mirror = | |
| 600 CreateMethodMirrorUsingApi(func, Dart_Null()); | |
| 601 if (Dart_IsError(func_mirror)) { | |
| 602 return func_mirror; | |
| 603 } | |
| 604 Dart_Handle args[] = { | |
| 605 CreateLazyMirror(instance_cls), | |
| 606 instance, | |
| 607 func_mirror, | |
| 608 }; | |
| 609 return Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | |
| 610 | |
| 611 } else { | |
| 612 Dart_Handle cls_name = NewString("_LocalInstanceMirrorImpl"); | |
| 613 Dart_Handle type = Dart_GetType(MirrorLib(), cls_name, 0, NULL); | |
| 614 if (Dart_IsError(type)) { | |
| 615 return type; | |
| 616 } | |
| 617 Dart_Handle args[] = { | |
| 618 CreateLazyMirror(instance_cls), | |
| 619 instance, | |
| 620 }; | |
| 621 return Dart_New(type, Dart_Null(), ARRAY_SIZE(args), args); | |
| 622 } | |
| 623 } | |
| 624 | |
| 625 | |
| 626 static RawInstance* CreateClassMirror(const Class& cls, | 540 static RawInstance* CreateClassMirror(const Class& cls, |
| 627 const Instance& owner_mirror) { | 541 const Instance& owner_mirror) { |
| 628 Instance& retvalue = Instance::Handle(); | 542 Instance& retvalue = Instance::Handle(); |
| 629 Dart_EnterScope(); | 543 Dart_EnterScope(); |
| 630 Isolate* isolate = Isolate::Current(); | 544 Isolate* isolate = Isolate::Current(); |
| 631 Dart_Handle cls_handle = Api::NewHandle(isolate, cls.raw()); | 545 Dart_Handle cls_handle = Api::NewHandle(isolate, cls.raw()); |
| 632 if (Dart_IsError(cls_handle)) { | 546 if (Dart_IsError(cls_handle)) { |
| 633 Dart_PropagateError(cls_handle); | 547 Dart_PropagateError(cls_handle); |
| 634 } | 548 } |
| 635 Dart_Handle name_handle = Api::NewHandle(isolate, cls.Name()); | 549 Dart_Handle name_handle = Api::NewHandle(isolate, cls.Name()); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 699 Dart_EnterScope(); | 613 Dart_EnterScope(); |
| 700 Dart_Handle mirrors = CreateMirrorSystem(); | 614 Dart_Handle mirrors = CreateMirrorSystem(); |
| 701 if (Dart_IsError(mirrors)) { | 615 if (Dart_IsError(mirrors)) { |
| 702 Dart_PropagateError(mirrors); | 616 Dart_PropagateError(mirrors); |
| 703 } | 617 } |
| 704 Dart_SetReturnValue(args, mirrors); | 618 Dart_SetReturnValue(args, mirrors); |
| 705 Dart_ExitScope(); | 619 Dart_ExitScope(); |
| 706 } | 620 } |
| 707 | 621 |
| 708 | 622 |
| 709 void NATIVE_ENTRY_FUNCTION(Mirrors_makeLocalInstanceMirror)( | |
| 710 Dart_NativeArguments args) { | |
| 711 Dart_EnterScope(); | |
| 712 Dart_Handle reflectee = Dart_GetNativeArgument(args, 0); | |
| 713 Dart_Handle mirror = CreateInstanceMirror(reflectee); | |
| 714 if (Dart_IsError(mirror)) { | |
| 715 Dart_PropagateError(mirror); | |
| 716 } | |
| 717 Dart_SetReturnValue(args, mirror); | |
| 718 Dart_ExitScope(); | |
| 719 } | |
| 720 | |
| 721 | |
| 722 void NATIVE_ENTRY_FUNCTION(Mirrors_makeLocalClassMirror)( | 623 void NATIVE_ENTRY_FUNCTION(Mirrors_makeLocalClassMirror)( |
| 723 Dart_NativeArguments args) { | 624 Dart_NativeArguments args) { |
| 724 Dart_EnterScope(); | 625 Dart_EnterScope(); |
| 725 Isolate* isolate = Isolate::Current(); | 626 Isolate* isolate = Isolate::Current(); |
| 726 Dart_Handle key = Dart_GetNativeArgument(args, 0); | 627 Dart_Handle key = Dart_GetNativeArgument(args, 0); |
| 727 if (Dart_IsError(key)) { | 628 if (Dart_IsError(key)) { |
| 728 Dart_PropagateError(key); | 629 Dart_PropagateError(key); |
| 729 } | 630 } |
| 730 const Type& type = Api::UnwrapTypeHandle(isolate, key); | 631 const Type& type = Api::UnwrapTypeHandle(isolate, key); |
| 731 const Class& cls = Class::Handle(type.type_class()); | 632 const Class& cls = Class::Handle(type.type_class()); |
| (...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1127 | 1028 |
| 1128 obj = DartEntry::InvokeClosure(args); | 1029 obj = DartEntry::InvokeClosure(args); |
| 1129 if (obj.IsError()) { | 1030 if (obj.IsError()) { |
| 1130 ThrowInvokeError(Error::Cast(obj)); | 1031 ThrowInvokeError(Error::Cast(obj)); |
| 1131 UNREACHABLE(); | 1032 UNREACHABLE(); |
| 1132 } | 1033 } |
| 1133 return obj.raw(); | 1034 return obj.raw(); |
| 1134 } | 1035 } |
| 1135 | 1036 |
| 1136 | 1037 |
| 1038 DEFINE_NATIVE_ENTRY(ClosureMirror_function, 1) { |
| 1039 GET_NON_NULL_NATIVE_ARGUMENT(Instance, closure, arguments->NativeArgAt(0)); |
| 1040 ASSERT(closure.IsClosure()); |
| 1041 |
| 1042 const Function& func = Function::Handle(Closure::function(closure)); |
| 1043 return CreateMethodMirror(func, Instance::null_instance()); |
| 1044 } |
| 1045 |
| 1046 |
| 1137 static void ThrowNoSuchMethod(const Instance& receiver, | 1047 static void ThrowNoSuchMethod(const Instance& receiver, |
| 1138 const String& function_name, | 1048 const String& function_name, |
| 1139 const Function& function, | 1049 const Function& function, |
| 1140 const InvocationMirror::Call call, | 1050 const InvocationMirror::Call call, |
| 1141 const InvocationMirror::Type type) { | 1051 const InvocationMirror::Type type) { |
| 1142 const Smi& invocation_type = Smi::Handle(Smi::New( | 1052 const Smi& invocation_type = Smi::Handle(Smi::New( |
| 1143 InvocationMirror::EncodeType(call, type))); | 1053 InvocationMirror::EncodeType(call, type))); |
| 1144 | 1054 |
| 1145 const Array& args = Array::Handle(Array::New(6)); | 1055 const Array& args = Array::Handle(Array::New(6)); |
| 1146 args.SetAt(0, receiver); | 1056 args.SetAt(0, receiver); |
| (...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1595 | 1505 |
| 1596 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { | 1506 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { |
| 1597 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); | 1507 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); |
| 1598 const Field& field = Field::Handle(ref.GetFieldReferent()); | 1508 const Field& field = Field::Handle(ref.GetFieldReferent()); |
| 1599 | 1509 |
| 1600 const AbstractType& type = AbstractType::Handle(field.type()); | 1510 const AbstractType& type = AbstractType::Handle(field.type()); |
| 1601 return CreateTypeMirror(type); | 1511 return CreateTypeMirror(type); |
| 1602 } | 1512 } |
| 1603 | 1513 |
| 1604 } // namespace dart | 1514 } // namespace dart |
| OLD | NEW |