Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: runtime/lib/mirrors.cc

Issue 23633002: Implementation for ClosureMirror.findInContext. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed comments + rebase Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | runtime/lib/mirrors_impl.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "lib/invocation_mirror.h" 5 #include "lib/invocation_mirror.h"
6 #include "vm/bootstrap_natives.h" 6 #include "vm/bootstrap_natives.h"
7 #include "vm/class_finalizer.h" 7 #include "vm/class_finalizer.h"
8 #include "vm/dart_entry.h" 8 #include "vm/dart_entry.h"
9 #include "vm/exceptions.h" 9 #include "vm/exceptions.h"
10 #include "vm/object_store.h" 10 #include "vm/object_store.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 ThrowMirroredCompilationError(message); 47 ThrowMirroredCompilationError(message);
48 UNREACHABLE(); 48 UNREACHABLE();
49 } 49 }
50 Exceptions::PropagateError(error); 50 Exceptions::PropagateError(error);
51 UNREACHABLE(); 51 UNREACHABLE();
52 } 52 }
53 53
54 54
55 // Conventions: 55 // Conventions:
56 // * For throwing a NSM in a class klass we use its runtime type as receiver, 56 // * For throwing a NSM in a class klass we use its runtime type as receiver,
57 // i.e., RawTypeOfClass(klass). 57 // i.e., klass.RareType().
58 // * For throwing a NSM in a library, we just pass the null instance as 58 // * For throwing a NSM in a library, we just pass the null instance as
59 // receiver. 59 // receiver.
60 static void ThrowNoSuchMethod(const Instance& receiver, 60 static void ThrowNoSuchMethod(const Instance& receiver,
61 const String& function_name, 61 const String& function_name,
62 const Function& function, 62 const Function& function,
63 const InvocationMirror::Call call, 63 const InvocationMirror::Call call,
64 const InvocationMirror::Type type) { 64 const InvocationMirror::Type type) {
65 const Smi& invocation_type = Smi::Handle(Smi::New( 65 const Smi& invocation_type = Smi::Handle(Smi::New(
66 InvocationMirror::EncodeType(call, type))); 66 InvocationMirror::EncodeType(call, type)));
67 67
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 392
393 const Instance& isolate_mirror = Instance::Handle(CreateIsolateMirror()); 393 const Instance& isolate_mirror = Instance::Handle(CreateIsolateMirror());
394 394
395 const Array& args = Array::Handle(Array::New(2)); 395 const Array& args = Array::Handle(Array::New(2));
396 args.SetAt(0, library_mirrors); 396 args.SetAt(0, library_mirrors);
397 args.SetAt(1, isolate_mirror); 397 args.SetAt(1, isolate_mirror);
398 return CreateMirror(Symbols::_LocalMirrorSystemImpl(), args); 398 return CreateMirror(Symbols::_LocalMirrorSystemImpl(), args);
399 } 399 }
400 400
401 401
402 // Invoke the function, or noSuchMethod if it is null. Propagate any unhandled
403 // exceptions. Wrap and propagate any compilation errors.
404 static RawInstance* InvokeDynamicFunction(
405 const Instance& receiver,
406 const Function& function,
407 const String& target_name,
408 const Array& args,
409 const Array& args_descriptor_array) {
410 // Note "args" is already the internal arguments with the receiver as the
411 // first element.
412 Object& result = Object::Handle();
413
414 ArgumentsDescriptor args_descriptor(args_descriptor_array);
415 if (function.IsNull() ||
416 !function.is_visible() ||
417 !function.AreValidArguments(args_descriptor, NULL)) {
418 result = DartEntry::InvokeNoSuchMethod(receiver,
419 target_name,
420 args,
421 args_descriptor_array);
422 } else {
423 result = DartEntry::InvokeFunction(function,
424 args,
425 args_descriptor_array);
426 }
427
428 if (result.IsError()) {
rmacnak 2013/09/26 22:16:00 Could also use returnResult here.
Michael Lippautz (Google) 2013/09/26 22:40:50 Done.
429 ThrowInvokeError(Error::Cast(result));
430 UNREACHABLE();
431 } else if (result.IsInstance()) {
432 return Instance::Cast(result).raw();
433 }
434 ASSERT(result.IsNull());
435 return Instance::null();
436 }
437
438
439 static RawInstance* returnResult(const Object& result) {
440 if (result.IsError()) {
441 ThrowInvokeError(Error::Cast(result));
442 UNREACHABLE();
443 } else if (result.IsInstance()) {
444 return Instance::Cast(result).raw();
445 }
446 ASSERT(result.IsNull());
447 return Instance::null();
448 }
449
450
451 static RawInstance* InvokeLibraryGetter(const Library& library,
452 const String& getter_name,
453 const bool throw_nsm_if_absent) {
454 // To access a top-level we may need to use the Field or the getter Function.
455 // The getter function may either be in the library or in the field's owner
456 // class, depending on whether it was an actual getter, or an uninitialized
457 // field.
458 const Field& field = Field::Handle(
459 library.LookupFieldAllowPrivate(getter_name));
460 Function& getter = Function::Handle();
461 if (field.IsNull()) {
462 // No field found. Check for a getter in the lib.
463 const String& internal_getter_name =
464 String::Handle(Field::GetterName(getter_name));
465 getter = library.LookupFunctionAllowPrivate(internal_getter_name);
466 if (getter.IsNull()) {
467 getter = library.LookupFunctionAllowPrivate(getter_name);
468 if (!getter.IsNull()) {
469 // Looking for a getter but found a regular method: closurize.
470 const Function& closure_function =
471 Function::Handle(getter.ImplicitClosureFunction());
472 return closure_function.ImplicitStaticClosure();
473 }
474 }
475 } else {
476 if (field.IsUninitialized()) {
477 // A field was found. Check for a getter in the field's owner classs.
478 const Class& klass = Class::Handle(field.owner());
479 const String& internal_getter_name =
480 String::Handle(Field::GetterName(getter_name));
481 getter = klass.LookupStaticFunctionAllowPrivate(internal_getter_name);
482 } else {
483 return field.value();
484 }
485 }
486
487 if (!getter.IsNull() && getter.is_visible()) {
488 // Invoke the getter and return the result.
489 const Object& result = Object::Handle(
490 DartEntry::InvokeFunction(getter, Object::empty_array()));
491 return returnResult(result);
492 }
493
494 if (throw_nsm_if_absent) {
495 ThrowNoSuchMethod(Instance::null_instance(),
496 getter_name,
497 getter,
498 InvocationMirror::kTopLevel,
499 InvocationMirror::kGetter);
500 UNREACHABLE();
501 }
502
503 // Fall through case: Indicate that we didn't find any function or field using
504 // a special null class. This is different from a field being null and must
rmacnak 2013/09/26 22:16:00 special null instance. It has the same class as th
Michael Lippautz (Google) 2013/09/26 22:40:50 Done.
505 // not leak into Dartland.
506 return Object::sentinel().raw();
507 }
508
509
510 static RawInstance* InvokeClassGetter(const Class& klass,
511 const String& getter_name,
512 const bool throw_nsm_if_absent) {
513 // Note static fields do not have implicit getters.
514 const Field& field = Field::Handle(klass.LookupStaticField(getter_name));
515 if (field.IsNull() || field.IsUninitialized()) {
516 const String& internal_getter_name = String::Handle(
517 Field::GetterName(getter_name));
518 Function& getter = Function::Handle(
519 klass.LookupStaticFunctionAllowPrivate(internal_getter_name));
520
521 if (getter.IsNull() || !getter.is_visible()) {
522 if (getter.IsNull()) {
523 getter = klass.LookupStaticFunctionAllowPrivate(getter_name);
524 if (!getter.IsNull()) {
525 // Looking for a getter but found a regular method: closurize.
526 const Function& closure_function =
527 Function::Handle(getter.ImplicitClosureFunction());
528 return closure_function.ImplicitStaticClosure();
529 }
530 }
531 if (throw_nsm_if_absent) {
532 ThrowNoSuchMethod(AbstractType::Handle(klass.RareType()),
533 getter_name,
534 getter,
535 InvocationMirror::kStatic,
536 InvocationMirror::kGetter);
537 UNREACHABLE();
538 }
539 // Fall through case: Indicate that we didn't find any function or field
540 // using a special null class. This is different from a field being null
541 // and must not leak into Dartland.
542 return Object::sentinel().raw();
543 }
544
545 // Invoke the getter and return the result.
546 const Object& result = Object::Handle(
547 DartEntry::InvokeFunction(getter, Object::empty_array()));
548 return returnResult(result);
549 }
550 return field.value();
551 }
552
553
554
555
556 static RawInstance* InvokeInstanceGetter(const Class& klass,
557 const Instance& reflectee,
558 const String& getter_name,
559 const bool throw_nsm_if_absent) {
560 String& internal_getter_name = String::Handle(Field::GetterName(getter_name));
561 Function& function = Function::Handle(
562 Resolver::ResolveDynamicAnyArgsAllowPrivate(klass, internal_getter_name));
563
564 if (!function.IsNull() || throw_nsm_if_absent) {
565 const int kNumArgs = 1;
566 const Array& args = Array::Handle(Array::New(kNumArgs));
567 args.SetAt(0, reflectee);
568 const Array& args_descriptor =
569 Array::Handle(ArgumentsDescriptor::New(args.Length()));
570
571 return InvokeDynamicFunction(reflectee,
572 function,
573 internal_getter_name,
574 args,
575 args_descriptor);
576 }
577 // Fall through case: Indicate that we didn't find any function or field
578 // using a special null class. This is different from a field being null
579 // and must not leak into Dartland.
580 return Object::sentinel().raw();
581 }
582
583
584 static RawInstance* LookupFunctionOrFieldInLibraryPrefix(
585 const LibraryPrefix& prefix,
586 const String& lookup_name) {
587 Instance& result = Instance::Handle();
588 const Object& entry = Object::Handle(prefix.LookupObject(lookup_name));
589 if (!entry.IsNull()) {
590 if (entry.IsField()) {
591 const Field& field = Field::Cast(entry);
592 const Class& field_owner = Class::Handle(field.owner());
593 const Library& field_library = Library::Handle(field_owner.library());
594 result ^= InvokeLibraryGetter(field_library, lookup_name, false);
595 if (result.raw() != Object::sentinel().raw()) {
596 return result.raw();
597 }
598 } else if (entry.IsFunction()) {
599 const Function& func = Function::Cast(entry);
600 const Function& closure_function = Function::Handle(
601 func.ImplicitClosureFunction());
602 return closure_function.ImplicitStaticClosure();
603 }
604 }
605
606 // Fall through case: Indicate that we didn't find any function or field using
607 // a special null class. This is different from a field being null and must
608 // not leak into Dartland.
609 return Object::sentinel().raw();
610 }
611
612
613 static RawInstance* LookupStaticFunctionOrFieldInClass(
614 const Class& klass,
615 const String& lookup_name) {
616 Instance& result = Instance::Handle(
617 InvokeClassGetter(klass, lookup_name, false));
618 if (result.raw() != Object::sentinel().raw()) {
619 return result.raw();
620 }
621
622 Function& func = Function::Handle();
623 Class& lookup_class = Class::Handle(klass.raw());
624 while (func.IsNull() && !lookup_class.IsNull()) {
625 func ^= lookup_class.LookupStaticFunctionAllowPrivate(lookup_name);
626 lookup_class = lookup_class.SuperClass();
627 }
628 if (!func.IsNull()) {
629 const Function& closure_function = Function::Handle(
630 func.ImplicitClosureFunction());
631 ASSERT(!closure_function.IsNull());
632 return closure_function.ImplicitStaticClosure();
633 }
634
635 // Fall through case: Indicate that we didn't find any function or field using
636 // a special null class. This is different from a field being null and must
637 // not leak into Dartland.
638 return Object::sentinel().raw();
639 }
640
641
642 static RawInstance* LookupFunctionOrFieldInFunctionContext(
643 const Function& func,
644 const Context& ctx,
645 const String& lookup_name) {
646 const ContextScope& ctx_scope = ContextScope::Handle(func.context_scope());
647 intptr_t this_index = -1;
648
649 // Search local context.
650 String& name = String::Handle();
651 for (intptr_t i = 0; i < ctx_scope.num_variables(); i++) {
652 name ^= ctx_scope.NameAt(i);
653 if (name.Equals(lookup_name)) {
654 return ctx.At(i);
655 } else if (name.Equals(Symbols::This())) {
656 // Record instance index to search for the field in the instance
657 // afterwards.
658 this_index = i;
659 }
660 }
661
662 // Search the instance this function is attached to.
663 if (this_index >= 0) {
664 // Since we want the closurized version of a function, we can access, both,
665 // functions and fields through their implicit getter name. If the implicit
666 // getter does not exist for the function, a method extractor will be
667 // created.
668 const Class& owner = Class::Handle(func.Owner());
669 const Instance& receiver = Instance::Handle(ctx.At(this_index));
670 return InvokeInstanceGetter(owner, receiver, lookup_name, false);
671 }
672
673 // Fall through case: Indicate that we didn't find any function or field using
674 // a special null class. This is different from a field being null and must
675 // not leak into Dartland.
676 return Object::sentinel().raw();
677 }
678
679
680 static RawInstance* LookupFunctionOrFieldInLibraryHelper(
681 const Library& library,
682 const String& class_name,
683 const String& lookup_name) {
684 if (class_name.IsNull()) {
685 Instance& result = Instance::Handle(
686 InvokeLibraryGetter(library, lookup_name, false));
687 if (result.raw() != Object::sentinel().raw()) {
688 return result.raw();
689 }
690 Function& func = Function::Handle(
691 library.LookupFunctionAllowPrivate(lookup_name));
692 if (!func.IsNull()) {
693 const Function& closure_function = Function::Handle(
694 func.ImplicitClosureFunction());
695 return closure_function.ImplicitStaticClosure();
696 }
697 } else {
698 Class& cls = Class::Handle(
699 library.LookupClassAllowPrivate(class_name));
700 if (!cls.IsNull()) {
701 return LookupStaticFunctionOrFieldInClass(cls, lookup_name);
702 }
703 }
704
705 // Fall through case: Indicate that we didn't find any function or field using
706 // a special null class. This is different from a field being null and must
707 // not leak into Dartland.
708 return Object::sentinel().raw();
709 }
710
711
712 static RawInstance* LookupFunctionOrFieldInLibrary(const Library& library,
713 const String& class_name,
714 const String& lookup_name) {
715 Instance& result = Instance::Handle();
716 // Check current library.
717 result ^= LookupFunctionOrFieldInLibraryHelper(
718 library, class_name, lookup_name);
719 if (result.raw() != Object::sentinel().raw()) {
720 return result.raw();
721 }
722 // Check all imports.
723 Library& lib_it = Library::Handle();
724 for (intptr_t i = 0; i < library.num_imports(); i++) {
725 lib_it ^= library.ImportLibraryAt(i);
726 result ^= LookupFunctionOrFieldInLibraryHelper(
727 lib_it, class_name, lookup_name);
728 if (result.raw() != Object::sentinel().raw()) {
729 return result.raw();
730 }
731 }
732
733 // Fall through case: Indicate that we didn't find any function or field using
734 // a special null class. This is different from a function or field being
735 // null.
736 return Object::sentinel().raw();
737 }
738
739
402 DEFINE_NATIVE_ENTRY(Mirrors_makeLocalMirrorSystem, 0) { 740 DEFINE_NATIVE_ENTRY(Mirrors_makeLocalMirrorSystem, 0) {
403 return CreateMirrorSystem(); 741 return CreateMirrorSystem();
404 } 742 }
405 743
406 744
407 DEFINE_NATIVE_ENTRY(Mirrors_makeLocalClassMirror, 1) { 745 DEFINE_NATIVE_ENTRY(Mirrors_makeLocalClassMirror, 1) {
408 GET_NON_NULL_NATIVE_ARGUMENT(Type, type, arguments->NativeArgAt(0)); 746 GET_NON_NULL_NATIVE_ARGUMENT(Type, type, arguments->NativeArgAt(0));
409 const Class& cls = Class::Handle(type.type_class()); 747 const Class& cls = Class::Handle(type.type_class());
410 ASSERT(!cls.IsNull()); 748 ASSERT(!cls.IsNull());
411 if (cls.IsDynamicClass() || cls.IsVoidClass()) { 749 if (cls.IsDynamicClass() || cls.IsVoidClass()) {
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 837
500 DEFINE_NATIVE_ENTRY(FunctionTypeMirror_return_type, 1) { 838 DEFINE_NATIVE_ENTRY(FunctionTypeMirror_return_type, 1) {
501 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); 839 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0));
502 const Class& cls = Class::Handle(ref.GetClassReferent()); 840 const Class& cls = Class::Handle(ref.GetClassReferent());
503 const Function& func = Function::Handle(CallMethod(cls)); 841 const Function& func = Function::Handle(CallMethod(cls));
504 ASSERT(!func.IsNull()); 842 ASSERT(!func.IsNull());
505 return func.result_type(); 843 return func.result_type();
506 } 844 }
507 845
508 846
509 static bool FieldIsUninitialized(const Field& field) {
510 ASSERT(!field.IsNull());
511
512 // Return getter method for uninitialized fields, rather than the
513 // field object, since the value in the field object will not be
514 // initialized until the first time the getter is invoked.
515 const Instance& value = Instance::Handle(field.value());
516 ASSERT(value.raw() != Object::transition_sentinel().raw());
517 return value.raw() == Object::sentinel().raw();
518 }
519
520
521 DEFINE_NATIVE_ENTRY(ClassMirror_library, 1) { 847 DEFINE_NATIVE_ENTRY(ClassMirror_library, 1) {
522 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); 848 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0));
523 const Class& klass = Class::Handle(ref.GetClassReferent()); 849 const Class& klass = Class::Handle(ref.GetClassReferent());
524 const Library& library = Library::Handle(klass.library()); 850 const Library& library = Library::Handle(klass.library());
525 ASSERT(!library.IsNull()); 851 ASSERT(!library.IsNull());
526 return CreateLibraryMirror(library); 852 return CreateLibraryMirror(library);
527 } 853 }
528 854
529 855
530 DEFINE_NATIVE_ENTRY(ClassMirror_supertype, 1) { 856 DEFINE_NATIVE_ENTRY(ClassMirror_supertype, 1) {
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
750 ObjectStore* object_store = isolate->object_store(); 1076 ObjectStore* object_store = isolate->object_store();
751 const Class& cls = Class::Handle(isolate, object_store->object_class()); 1077 const Class& cls = Class::Handle(isolate, object_store->object_class());
752 const Function& function = 1078 const Function& function =
753 Function::Handle(isolate, cls.LookupDynamicFunction(Symbols::hashCode())); 1079 Function::Handle(isolate, cls.LookupDynamicFunction(Symbols::hashCode()));
754 const Array& args = Array::Handle(isolate, Array::New(1)); 1080 const Array& args = Array::Handle(isolate, Array::New(1));
755 args.SetAt(0, reflectee); 1081 args.SetAt(0, reflectee);
756 return DartEntry::InvokeFunction(function, args); 1082 return DartEntry::InvokeFunction(function, args);
757 } 1083 }
758 1084
759 1085
760 // Invoke the function, or noSuchMethod if it is null. Propagate any unhandled
761 // exceptions. Wrap and propagate any compilation errors.
762 static RawObject* ReflectivelyInvokeDynamicFunction(
763 const Instance& receiver,
764 const Function& function,
765 const String& target_name,
766 const Array& args,
767 const Array& args_descriptor_array) {
768 // Note "args" is already the internal arguments with the receiver as the
769 // first element.
770 Object& result = Object::Handle();
771
772 ArgumentsDescriptor args_descriptor(args_descriptor_array);
773 if (function.IsNull() ||
774 !function.is_visible() ||
775 !function.AreValidArguments(args_descriptor, NULL)) {
776 result = DartEntry::InvokeNoSuchMethod(receiver,
777 target_name,
778 args,
779 args_descriptor_array);
780 } else {
781 result = DartEntry::InvokeFunction(function,
782 args,
783 args_descriptor_array);
784 }
785
786 if (result.IsError()) {
787 ThrowInvokeError(Error::Cast(result));
788 UNREACHABLE();
789 }
790 return result.raw();
791 }
792
793
794 DEFINE_NATIVE_ENTRY(InstanceMirror_invoke, 5) { 1086 DEFINE_NATIVE_ENTRY(InstanceMirror_invoke, 5) {
795 // Argument 0 is the mirror, which is unused by the native. It exists 1087 // Argument 0 is the mirror, which is unused by the native. It exists
796 // because this native is an instance method in order to be polymorphic 1088 // because this native is an instance method in order to be polymorphic
797 // with its cousins. 1089 // with its cousins.
798 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1)); 1090 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1));
799 GET_NON_NULL_NATIVE_ARGUMENT( 1091 GET_NON_NULL_NATIVE_ARGUMENT(
800 String, function_name, arguments->NativeArgAt(2)); 1092 String, function_name, arguments->NativeArgAt(2));
801 GET_NON_NULL_NATIVE_ARGUMENT(Array, args, arguments->NativeArgAt(3)); 1093 GET_NON_NULL_NATIVE_ARGUMENT(Array, args, arguments->NativeArgAt(3));
802 GET_NON_NULL_NATIVE_ARGUMENT(Array, arg_names, arguments->NativeArgAt(4)); 1094 GET_NON_NULL_NATIVE_ARGUMENT(Array, arg_names, arguments->NativeArgAt(4));
803 1095
804 Class& klass = Class::Handle(reflectee.clazz()); 1096 Class& klass = Class::Handle(reflectee.clazz());
805 Function& function = Function::Handle( 1097 Function& function = Function::Handle(
806 Resolver::ResolveDynamicAnyArgsAllowPrivate(klass, function_name)); 1098 Resolver::ResolveDynamicAnyArgsAllowPrivate(klass, function_name));
807 1099
808 const Array& args_descriptor = 1100 const Array& args_descriptor =
809 Array::Handle(ArgumentsDescriptor::New(args.Length(), arg_names)); 1101 Array::Handle(ArgumentsDescriptor::New(args.Length(), arg_names));
810 1102
811 return ReflectivelyInvokeDynamicFunction(reflectee, 1103 return InvokeDynamicFunction(reflectee,
812 function, 1104 function,
813 function_name, 1105 function_name,
814 args, 1106 args,
815 args_descriptor); 1107 args_descriptor);
816 } 1108 }
817 1109
818 1110
819 DEFINE_NATIVE_ENTRY(InstanceMirror_invokeGetter, 3) { 1111 DEFINE_NATIVE_ENTRY(InstanceMirror_invokeGetter, 3) {
820 // Argument 0 is the mirror, which is unused by the native. It exists 1112 // Argument 0 is the mirror, which is unused by the native. It exists
821 // because this native is an instance method in order to be polymorphic 1113 // because this native is an instance method in order to be polymorphic
822 // with its cousins. 1114 // with its cousins.
823 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1)); 1115 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1));
824 GET_NON_NULL_NATIVE_ARGUMENT(String, getter_name, arguments->NativeArgAt(2)); 1116 GET_NON_NULL_NATIVE_ARGUMENT(String, getter_name, arguments->NativeArgAt(2));
825
826 Class& klass = Class::Handle(reflectee.clazz()); 1117 Class& klass = Class::Handle(reflectee.clazz());
827 String& internal_getter_name = String::Handle(Field::GetterName(getter_name)); 1118 return InvokeInstanceGetter(klass, reflectee, getter_name, true);
828 Function& function = Function::Handle(
829 Resolver::ResolveDynamicAnyArgsAllowPrivate(klass, internal_getter_name));
830
831 const int kNumArgs = 1;
832 const Array& args = Array::Handle(Array::New(kNumArgs));
833 args.SetAt(0, reflectee);
834 const Array& args_descriptor =
835 Array::Handle(ArgumentsDescriptor::New(args.Length()));
836
837 return ReflectivelyInvokeDynamicFunction(reflectee,
838 function,
839 internal_getter_name,
840 args,
841 args_descriptor);
842 } 1119 }
843 1120
844 1121
845 DEFINE_NATIVE_ENTRY(InstanceMirror_invokeSetter, 4) { 1122 DEFINE_NATIVE_ENTRY(InstanceMirror_invokeSetter, 4) {
846 // Argument 0 is the mirror, which is unused by the native. It exists 1123 // Argument 0 is the mirror, which is unused by the native. It exists
847 // because this native is an instance method in order to be polymorphic 1124 // because this native is an instance method in order to be polymorphic
848 // with its cousins. 1125 // with its cousins.
849 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1)); 1126 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1));
850 GET_NON_NULL_NATIVE_ARGUMENT(String, setter_name, arguments->NativeArgAt(2)); 1127 GET_NON_NULL_NATIVE_ARGUMENT(String, setter_name, arguments->NativeArgAt(2));
851 GET_NATIVE_ARGUMENT(Instance, value, arguments->NativeArgAt(3)); 1128 GET_NATIVE_ARGUMENT(Instance, value, arguments->NativeArgAt(3));
(...skipping 23 matching lines...) Expand all
875 } 1152 }
876 1153
877 // Invoke the setter and return the result. 1154 // Invoke the setter and return the result.
878 const int kNumArgs = 2; 1155 const int kNumArgs = 2;
879 const Array& args = Array::Handle(Array::New(kNumArgs)); 1156 const Array& args = Array::Handle(Array::New(kNumArgs));
880 args.SetAt(0, reflectee); 1157 args.SetAt(0, reflectee);
881 args.SetAt(1, value); 1158 args.SetAt(1, value);
882 const Array& args_descriptor = 1159 const Array& args_descriptor =
883 Array::Handle(ArgumentsDescriptor::New(args.Length())); 1160 Array::Handle(ArgumentsDescriptor::New(args.Length()));
884 1161
885 return ReflectivelyInvokeDynamicFunction(reflectee, 1162 return InvokeDynamicFunction(reflectee,
886 setter, 1163 setter,
887 internal_setter_name, 1164 internal_setter_name,
888 args, 1165 args,
889 args_descriptor); 1166 args_descriptor);
890 } 1167 }
891 1168
892 1169
893 DEFINE_NATIVE_ENTRY(ClosureMirror_apply, 3) { 1170 DEFINE_NATIVE_ENTRY(ClosureMirror_apply, 3) {
894 GET_NON_NULL_NATIVE_ARGUMENT(Instance, closure, arguments->NativeArgAt(0)); 1171 GET_NON_NULL_NATIVE_ARGUMENT(Instance, closure, arguments->NativeArgAt(0));
895 ASSERT(!closure.IsNull() && closure.IsCallable(NULL, NULL)); 1172 ASSERT(!closure.IsNull() && closure.IsCallable(NULL, NULL));
896 GET_NON_NULL_NATIVE_ARGUMENT(Array, args, arguments->NativeArgAt(1)); 1173 GET_NON_NULL_NATIVE_ARGUMENT(Array, args, arguments->NativeArgAt(1));
897 GET_NON_NULL_NATIVE_ARGUMENT(Array, arg_names, arguments->NativeArgAt(2)); 1174 GET_NON_NULL_NATIVE_ARGUMENT(Array, arg_names, arguments->NativeArgAt(2));
898 1175
899 const Array& args_descriptor = 1176 const Array& args_descriptor =
900 Array::Handle(ArgumentsDescriptor::New(args.Length(), arg_names)); 1177 Array::Handle(ArgumentsDescriptor::New(args.Length(), arg_names));
901 1178
902 const Object& result = 1179 const Object& result =
903 Object::Handle(DartEntry::InvokeClosure(args, args_descriptor)); 1180 Object::Handle(DartEntry::InvokeClosure(args, args_descriptor));
904 if (result.IsError()) { 1181 if (result.IsError()) {
905 ThrowInvokeError(Error::Cast(result)); 1182 ThrowInvokeError(Error::Cast(result));
906 UNREACHABLE(); 1183 UNREACHABLE();
907 } 1184 }
908 return result.raw(); 1185 return result.raw();
909 } 1186 }
910 1187
911 1188
1189 DEFINE_NATIVE_ENTRY(ClosureMirror_find_in_context, 2) {
1190 GET_NON_NULL_NATIVE_ARGUMENT(Instance, closure, arguments->NativeArgAt(0));
1191 GET_NON_NULL_NATIVE_ARGUMENT(Array, lookup_parts, arguments->NativeArgAt(1));
1192 ASSERT(lookup_parts.Length() >= 1 && lookup_parts.Length() <= 3);
1193
1194 Function& function = Function::Handle();
1195 const bool callable = closure.IsCallable(&function, NULL);
1196 ASSERT(callable);
1197
1198 const int parts_len = lookup_parts.Length();
1199 // Lookup name is always the last part.
1200 const String& lookup_name = String::Handle(String::RawCast(
1201 lookup_parts.At(parts_len - 1)));
1202
1203 String& part_name = String::Handle();
1204 Class& owner = Class::Handle(function.Owner());
1205 LibraryPrefix& prefix = LibraryPrefix::Handle();
1206 Library& this_library = Library::Handle(owner.library());
1207 Instance& result = Instance::Handle(Object::sentinel().raw());
1208 if (parts_len == 1) {
1209 // Could be either a field in context, an instance or static field of the
1210 // enclosing class, or a field in the current library or any imported
1211 // library.
1212 result ^= LookupFunctionOrFieldInFunctionContext(
1213 function, Context::Handle(Closure::context(closure)), lookup_name);
1214 if (result.raw() == Object::sentinel().raw()) {
1215 result ^= LookupStaticFunctionOrFieldInClass(owner, lookup_name);
1216 }
1217 if (result.raw() == Object::sentinel().raw()) {
1218 result ^= LookupFunctionOrFieldInLibrary(this_library,
1219 part_name,
1220 lookup_name);
1221 }
1222 } else if (parts_len == 2) {
1223 // Could be either library.field or class.staticfield.
1224 part_name ^= lookup_parts.At(0);
1225 prefix ^= this_library.LookupLocalLibraryPrefix(part_name);
1226 if (prefix.IsNull()) {
1227 result ^= LookupFunctionOrFieldInLibrary(this_library,
1228 part_name,
1229 lookup_name);
1230 } else {
1231 result ^= LookupFunctionOrFieldInLibraryPrefix(prefix, lookup_name);
1232 }
1233 } else if (parts_len == 3) {
1234 // Can only be library.class.staticfield.
1235 part_name ^= lookup_parts.At(0);
1236 prefix ^= this_library.LookupLocalLibraryPrefix(part_name);
1237 if (!prefix.IsNull()) {
1238 part_name ^= lookup_parts.At(1);
1239 owner ^= prefix.LookupClass(part_name);
1240 if (!owner.IsNull()) {
1241 result ^= LookupStaticFunctionOrFieldInClass(owner, lookup_name);
1242 }
1243 }
1244 } else {
1245 UNREACHABLE();
1246 }
1247
1248 const Array& result_tuple = Array::Handle(Array::New(2));
rmacnak 2013/09/26 22:16:00 Add comment that multiple return values is to dist
Michael Lippautz (Google) 2013/09/26 22:40:50 Done.
1249 if (result.raw() == Object::sentinel().raw()) {
1250 result_tuple.SetAt(0, Bool::False());
1251 // No need to set the value.
1252 } else {
1253 result_tuple.SetAt(0, Bool::True());
1254 result_tuple.SetAt(1, result);
1255 }
1256 return result_tuple.raw();
1257 }
1258
1259
912 DEFINE_NATIVE_ENTRY(ClosureMirror_function, 1) { 1260 DEFINE_NATIVE_ENTRY(ClosureMirror_function, 1) {
913 GET_NON_NULL_NATIVE_ARGUMENT(Instance, closure, arguments->NativeArgAt(0)); 1261 GET_NON_NULL_NATIVE_ARGUMENT(Instance, closure, arguments->NativeArgAt(0));
914 ASSERT(!closure.IsNull()); 1262 ASSERT(!closure.IsNull());
915 1263
916 Function& function = Function::Handle(); 1264 Function& function = Function::Handle();
917 bool callable = closure.IsCallable(&function, NULL); 1265 bool callable = closure.IsCallable(&function, NULL);
918 ASSERT(callable); 1266 ASSERT(callable);
919 1267
920 return CreateMethodMirror(function, Instance::null_instance()); 1268 return CreateMethodMirror(function, Instance::null_instance());
921 } 1269 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
961 } 1309 }
962 1310
963 1311
964 DEFINE_NATIVE_ENTRY(ClassMirror_invokeGetter, 3) { 1312 DEFINE_NATIVE_ENTRY(ClassMirror_invokeGetter, 3) {
965 // Argument 0 is the mirror, which is unused by the native. It exists 1313 // Argument 0 is the mirror, which is unused by the native. It exists
966 // because this native is an instance method in order to be polymorphic 1314 // because this native is an instance method in order to be polymorphic
967 // with its cousins. 1315 // with its cousins.
968 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1)); 1316 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1));
969 const Class& klass = Class::Handle(ref.GetClassReferent()); 1317 const Class& klass = Class::Handle(ref.GetClassReferent());
970 GET_NON_NULL_NATIVE_ARGUMENT(String, getter_name, arguments->NativeArgAt(2)); 1318 GET_NON_NULL_NATIVE_ARGUMENT(String, getter_name, arguments->NativeArgAt(2));
971 1319 return InvokeClassGetter(klass, getter_name, true);
972 // Note static fields do not have implicit getters.
973 const Field& field = Field::Handle(klass.LookupStaticField(getter_name));
974 if (field.IsNull() || FieldIsUninitialized(field)) {
975 const String& internal_getter_name = String::Handle(
976 Field::GetterName(getter_name));
977 Function& getter = Function::Handle(
978 klass.LookupStaticFunctionAllowPrivate(internal_getter_name));
979
980 if (getter.IsNull() || !getter.is_visible()) {
981 if (getter.IsNull()) {
982 getter = klass.LookupStaticFunctionAllowPrivate(getter_name);
983 if (!getter.IsNull()) {
984 // Looking for a getter but found a regular method: closurize.
985 const Function& closure_function =
986 Function::Handle(getter.ImplicitClosureFunction());
987 return closure_function.ImplicitStaticClosure();
988 }
989 }
990 ThrowNoSuchMethod(AbstractType::Handle(klass.RareType()),
991 getter_name,
992 getter,
993 InvocationMirror::kStatic,
994 InvocationMirror::kGetter);
995 UNREACHABLE();
996 }
997
998 // Invoke the getter and return the result.
999 Object& result = Object::Handle(
1000 DartEntry::InvokeFunction(getter, Object::empty_array()));
1001 if (result.IsError()) {
1002 ThrowInvokeError(Error::Cast(result));
1003 UNREACHABLE();
1004 }
1005 return result.raw();
1006 }
1007 return field.value();
1008 } 1320 }
1009 1321
1010 1322
1011 DEFINE_NATIVE_ENTRY(ClassMirror_invokeSetter, 4) { 1323 DEFINE_NATIVE_ENTRY(ClassMirror_invokeSetter, 4) {
1012 // Argument 0 is the mirror, which is unused by the native. It exists 1324 // Argument 0 is the mirror, which is unused by the native. It exists
1013 // because this native is an instance method in order to be polymorphic 1325 // because this native is an instance method in order to be polymorphic
1014 // with its cousins. 1326 // with its cousins.
1015 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1)); 1327 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1));
1016 const Class& klass = Class::Handle(ref.GetClassReferent()); 1328 const Class& klass = Class::Handle(ref.GetClassReferent());
1017 GET_NON_NULL_NATIVE_ARGUMENT(String, setter_name, arguments->NativeArgAt(2)); 1329 GET_NON_NULL_NATIVE_ARGUMENT(String, setter_name, arguments->NativeArgAt(2));
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
1224 } 1536 }
1225 1537
1226 1538
1227 DEFINE_NATIVE_ENTRY(LibraryMirror_invokeGetter, 3) { 1539 DEFINE_NATIVE_ENTRY(LibraryMirror_invokeGetter, 3) {
1228 // Argument 0 is the mirror, which is unused by the native. It exists 1540 // Argument 0 is the mirror, which is unused by the native. It exists
1229 // because this native is an instance method in order to be polymorphic 1541 // because this native is an instance method in order to be polymorphic
1230 // with its cousins. 1542 // with its cousins.
1231 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1)); 1543 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1));
1232 const Library& library = Library::Handle(ref.GetLibraryReferent()); 1544 const Library& library = Library::Handle(ref.GetLibraryReferent());
1233 GET_NON_NULL_NATIVE_ARGUMENT(String, getter_name, arguments->NativeArgAt(2)); 1545 GET_NON_NULL_NATIVE_ARGUMENT(String, getter_name, arguments->NativeArgAt(2));
1234 1546 return InvokeLibraryGetter(library, getter_name, true);
1235 // To access a top-level we may need to use the Field or the
1236 // getter Function. The getter function may either be in the
1237 // library or in the field's owner class, depending.
1238 const Field& field = Field::Handle(
1239 library.LookupFieldAllowPrivate(getter_name));
1240 Function& getter = Function::Handle();
1241 if (field.IsNull()) {
1242 // No field found and no ambiguity error. Check for a getter in the lib.
1243 const String& internal_getter_name =
1244 String::Handle(Field::GetterName(getter_name));
1245 getter = library.LookupFunctionAllowPrivate(internal_getter_name);
1246 if (getter.IsNull()) {
1247 getter = library.LookupFunctionAllowPrivate(getter_name);
1248 if (!getter.IsNull()) {
1249 // Looking for a getter but found a regular method: closurize.
1250 const Function& closure_function =
1251 Function::Handle(getter.ImplicitClosureFunction());
1252 return closure_function.ImplicitStaticClosure();
1253 }
1254 }
1255 } else if (!field.IsNull() && FieldIsUninitialized(field)) {
1256 // A field was found. Check for a getter in the field's owner classs.
1257 const Class& klass = Class::Handle(field.owner());
1258 const String& internal_getter_name =
1259 String::Handle(Field::GetterName(getter_name));
1260 getter = klass.LookupStaticFunctionAllowPrivate(internal_getter_name);
1261 }
1262
1263 if (!getter.IsNull() && getter.is_visible()) {
1264 // Invoke the getter and return the result.
1265 const Object& result = Object::Handle(
1266 DartEntry::InvokeFunction(getter, Object::empty_array()));
1267 if (result.IsError()) {
1268 ThrowInvokeError(Error::Cast(result));
1269 UNREACHABLE();
1270 }
1271 return result.raw();
1272 }
1273 if (!field.IsNull()) {
1274 return field.value();
1275 }
1276 ThrowNoSuchMethod(Instance::null_instance(),
1277 getter_name,
1278 getter,
1279 InvocationMirror::kTopLevel,
1280 InvocationMirror::kGetter);
1281 UNREACHABLE();
1282 return Instance::null();
1283 } 1547 }
1284 1548
1285 1549
1286 DEFINE_NATIVE_ENTRY(LibraryMirror_invokeSetter, 4) { 1550 DEFINE_NATIVE_ENTRY(LibraryMirror_invokeSetter, 4) {
1287 // Argument 0 is the mirror, which is unused by the native. It exists 1551 // Argument 0 is the mirror, which is unused by the native. It exists
1288 // because this native is an instance method in order to be polymorphic 1552 // because this native is an instance method in order to be polymorphic
1289 // with its cousins. 1553 // with its cousins.
1290 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1)); 1554 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1));
1291 const Library& library = Library::Handle(ref.GetLibraryReferent()); 1555 const Library& library = Library::Handle(ref.GetLibraryReferent());
1292 GET_NON_NULL_NATIVE_ARGUMENT(String, setter_name, arguments->NativeArgAt(2)); 1556 GET_NON_NULL_NATIVE_ARGUMENT(String, setter_name, arguments->NativeArgAt(2));
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
1419 } 1683 }
1420 1684
1421 1685
1422 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { 1686 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) {
1423 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); 1687 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0));
1424 const Field& field = Field::Handle(ref.GetFieldReferent()); 1688 const Field& field = Field::Handle(ref.GetFieldReferent());
1425 return field.type(); 1689 return field.type();
1426 } 1690 }
1427 1691
1428 } // namespace dart 1692 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/lib/mirrors_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698