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

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: Aaaaaaaaaand one more round of comments 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 static RawInstance* ReturnResult(const Object& result) {
403 if (result.IsError()) {
404 ThrowInvokeError(Error::Cast(result));
405 UNREACHABLE();
406 }
407 if (result.IsInstance()) {
408 return Instance::Cast(result).raw();
409 }
410 ASSERT(result.IsNull());
411 return Instance::null();
412 }
413
414
415 // Invoke the function, or noSuchMethod if it is null. Propagate any unhandled
416 // exceptions. Wrap and propagate any compilation errors.
417 static RawInstance* InvokeDynamicFunction(
418 const Instance& receiver,
419 const Function& function,
420 const String& target_name,
421 const Array& args,
422 const Array& args_descriptor_array) {
423 // Note "args" is already the internal arguments with the receiver as the
424 // first element.
425 Object& result = Object::Handle();
426 ArgumentsDescriptor args_descriptor(args_descriptor_array);
427 if (function.IsNull() ||
428 !function.is_visible() ||
429 !function.AreValidArguments(args_descriptor, NULL)) {
430 result = DartEntry::InvokeNoSuchMethod(receiver,
431 target_name,
432 args,
433 args_descriptor_array);
434 } else {
435 result = DartEntry::InvokeFunction(function,
436 args,
437 args_descriptor_array);
438 }
439 return ReturnResult(result);
440 }
441
442
443 static RawInstance* InvokeLibraryGetter(const Library& library,
444 const String& getter_name,
445 const bool throw_nsm_if_absent) {
446 // To access a top-level we may need to use the Field or the getter Function.
447 // The getter function may either be in the library or in the field's owner
448 // class, depending on whether it was an actual getter, or an uninitialized
449 // field.
450 const Field& field = Field::Handle(
451 library.LookupFieldAllowPrivate(getter_name));
452 Function& getter = Function::Handle();
453 if (field.IsNull()) {
454 // No field found. Check for a getter in the lib.
455 const String& internal_getter_name =
456 String::Handle(Field::GetterName(getter_name));
457 getter = library.LookupFunctionAllowPrivate(internal_getter_name);
458 if (getter.IsNull()) {
459 getter = library.LookupFunctionAllowPrivate(getter_name);
460 if (!getter.IsNull()) {
461 // Looking for a getter but found a regular method: closurize it.
462 const Function& closure_function =
463 Function::Handle(getter.ImplicitClosureFunction());
464 return closure_function.ImplicitStaticClosure();
465 }
466 }
467 } else {
468 if (!field.IsUninitialized()) {
469 return field.value();
470 }
471 // An uninitialized field was found. Check for a getter in the field's
472 // owner classs.
473 const Class& klass = Class::Handle(field.owner());
474 const String& internal_getter_name =
475 String::Handle(Field::GetterName(getter_name));
476 getter = klass.LookupStaticFunctionAllowPrivate(internal_getter_name);
477 }
478
479 if (!getter.IsNull() && getter.is_visible()) {
480 // Invoke the getter and return the result.
481 const Object& result = Object::Handle(
482 DartEntry::InvokeFunction(getter, Object::empty_array()));
483 return ReturnResult(result);
484 }
485
486 if (throw_nsm_if_absent) {
487 ThrowNoSuchMethod(Instance::null_instance(),
488 getter_name,
489 getter,
490 InvocationMirror::kTopLevel,
491 InvocationMirror::kGetter);
492 UNREACHABLE();
493 }
494
495 // Fall through case: Indicate that we didn't find any function or field using
496 // a special null instance. This is different from a field being null. Callers
497 // make sure that this null does not leak into Dartland.
498 return Object::sentinel().raw();
499 }
500
501
502 static RawInstance* InvokeClassGetter(const Class& klass,
503 const String& getter_name,
504 const bool throw_nsm_if_absent) {
505 // Note static fields do not have implicit getters.
506 const Field& field = Field::Handle(klass.LookupStaticField(getter_name));
507 if (field.IsNull() || field.IsUninitialized()) {
508 const String& internal_getter_name = String::Handle(
509 Field::GetterName(getter_name));
510 Function& getter = Function::Handle(
511 klass.LookupStaticFunctionAllowPrivate(internal_getter_name));
512
513 if (getter.IsNull() || !getter.is_visible()) {
514 if (getter.IsNull()) {
515 getter = klass.LookupStaticFunctionAllowPrivate(getter_name);
516 if (!getter.IsNull()) {
517 // Looking for a getter but found a regular method: closurize it.
518 const Function& closure_function =
519 Function::Handle(getter.ImplicitClosureFunction());
520 return closure_function.ImplicitStaticClosure();
521 }
522 }
523 if (throw_nsm_if_absent) {
524 ThrowNoSuchMethod(AbstractType::Handle(klass.RareType()),
525 getter_name,
526 getter,
527 InvocationMirror::kStatic,
528 InvocationMirror::kGetter);
529 UNREACHABLE();
530 }
531 // Fall through case: Indicate that we didn't find any function or field
532 // using a special null instance. This is different from a field being
533 // null. Callers make sure that this null does not leak into Dartland.
534 return Object::sentinel().raw();
535 }
536
537 // Invoke the getter and return the result.
538 const Object& result = Object::Handle(
539 DartEntry::InvokeFunction(getter, Object::empty_array()));
540 return ReturnResult(result);
541 }
542 return field.value();
543 }
544
545
546
547
548 static RawInstance* InvokeInstanceGetter(const Class& klass,
549 const Instance& reflectee,
550 const String& getter_name,
551 const bool throw_nsm_if_absent) {
552 const String& internal_getter_name = String::Handle(
553 Field::GetterName(getter_name));
554 Function& function = Function::Handle(
555 Resolver::ResolveDynamicAnyArgsAllowPrivate(klass, internal_getter_name));
556
557 if (!function.IsNull() || throw_nsm_if_absent) {
558 const int kNumArgs = 1;
559 const Array& args = Array::Handle(Array::New(kNumArgs));
560 args.SetAt(0, reflectee);
561 const Array& args_descriptor =
562 Array::Handle(ArgumentsDescriptor::New(args.Length()));
563
564 // InvokeDynamic invokes NoSuchMethod if the provided function is null.
565 return InvokeDynamicFunction(reflectee,
566 function,
567 internal_getter_name,
568 args,
569 args_descriptor);
570 }
571
572 // Fall through case: Indicate that we didn't find any function or field using
573 // a special null instance. This is different from a field being null. Callers
574 // make sure that this null does not leak into Dartland.
575 return Object::sentinel().raw();
576 }
577
578
579 static RawInstance* LookupFunctionOrFieldInLibraryPrefix(
580 const LibraryPrefix& prefix,
581 const String& lookup_name) {
582 const Object& entry = Object::Handle(prefix.LookupObject(lookup_name));
583 if (!entry.IsNull()) {
584 if (entry.IsField()) {
585 const Field& field = Field::Cast(entry);
586 const Class& field_owner = Class::Handle(field.owner());
587 const Library& field_library = Library::Handle(field_owner.library());
588 const Instance& result = Instance::Handle(
589 InvokeLibraryGetter(field_library, lookup_name, false));
590 if (result.raw() != Object::sentinel().raw()) {
591 return result.raw();
592 }
593 } else if (entry.IsFunction()) {
594 const Function& func = Function::Cast(entry);
595 const Function& closure_function = Function::Handle(
596 func.ImplicitClosureFunction());
597 return closure_function.ImplicitStaticClosure();
598 }
599 }
600
601 // Fall through case: Indicate that we didn't find any function or field using
602 // a special null instance. This is different from a field being null. Callers
603 // make sure that this null does not leak into Dartland.
604 return Object::sentinel().raw();
605 }
606
607
608 static RawInstance* LookupStaticFunctionOrFieldInClass(
609 const Class& klass,
610 const String& lookup_name) {
611 Instance& result = Instance::Handle(
612 InvokeClassGetter(klass, lookup_name, false));
613 if (result.raw() != Object::sentinel().raw()) {
614 return result.raw();
615 }
616
617 Function& func = Function::Handle();
618 Class& lookup_class = Class::Handle(klass.raw());
619 while (func.IsNull() && !lookup_class.IsNull()) {
620 func ^= lookup_class.LookupStaticFunctionAllowPrivate(lookup_name);
621 lookup_class = lookup_class.SuperClass();
622 }
623 if (!func.IsNull()) {
624 const Function& closure_function = Function::Handle(
625 func.ImplicitClosureFunction());
626 ASSERT(!closure_function.IsNull());
627 return closure_function.ImplicitStaticClosure();
628 }
629
630 // Fall through case: Indicate that we didn't find any function or field using
631 // a special null instance. This is different from a field being null. Callers
632 // make sure that this null does not leak into Dartland.
633 return Object::sentinel().raw();
634 }
635
636
637 static RawInstance* LookupFunctionOrFieldInFunctionContext(
638 const Function& func,
639 const Context& ctx,
640 const String& lookup_name) {
641 const ContextScope& ctx_scope = ContextScope::Handle(func.context_scope());
642 intptr_t this_index = -1;
643
644 // Search local context.
645 String& name = String::Handle();
646 for (intptr_t i = 0; i < ctx_scope.num_variables(); i++) {
647 name ^= ctx_scope.NameAt(i);
648 if (name.Equals(lookup_name)) {
649 return ctx.At(i);
650 } else if (name.Equals(Symbols::This())) {
651 // Record instance index to search for the field in the instance
652 // afterwards.
653 this_index = i;
654 }
655 }
656
657 // Search the instance this function is attached to.
658 if (this_index >= 0) {
659 // Since we want the closurized version of a function, we can access, both,
660 // functions and fields through their implicit getter name. If the implicit
661 // getter does not exist for the function, a method extractor will be
662 // created.
663 const Class& owner = Class::Handle(func.Owner());
664 const Instance& receiver = Instance::Handle(ctx.At(this_index));
665 return InvokeInstanceGetter(owner, receiver, lookup_name, false);
666 }
667
668 // Fall through case: Indicate that we didn't find any function or field using
669 // a special null instance. This is different from a field being null. Callers
670 // make sure that this null does not leak into Dartland.
671 return Object::sentinel().raw();
672 }
673
674
675 static RawInstance* LookupFunctionOrFieldInLibraryHelper(
676 const Library& library,
677 const String& class_name,
678 const String& lookup_name) {
679 if (class_name.IsNull()) {
680 const Instance& result = Instance::Handle(
681 InvokeLibraryGetter(library, lookup_name, false));
682 if (result.raw() != Object::sentinel().raw()) {
683 return result.raw();
684 }
685 const Function& func = Function::Handle(
686 library.LookupFunctionAllowPrivate(lookup_name));
687 if (!func.IsNull()) {
688 const Function& closure_function = Function::Handle(
689 func.ImplicitClosureFunction());
690 return closure_function.ImplicitStaticClosure();
691 }
692 } else {
693 const Class& cls = Class::Handle(
694 library.LookupClassAllowPrivate(class_name));
695 if (!cls.IsNull()) {
696 return LookupStaticFunctionOrFieldInClass(cls, lookup_name);
697 }
698 }
699
700 // Fall through case: Indicate that we didn't find any function or field using
701 // a special null instance. This is different from a field being null. Callers
702 // make sure that this null does not leak into Dartland.
703 return Object::sentinel().raw();
704 }
705
706
707 static RawInstance* LookupFunctionOrFieldInLibrary(const Library& library,
708 const String& class_name,
709 const String& lookup_name) {
710 Instance& result = Instance::Handle();
711 // Check current library.
712 result ^= LookupFunctionOrFieldInLibraryHelper(
713 library, class_name, lookup_name);
714 if (result.raw() != Object::sentinel().raw()) {
715 return result.raw();
716 }
717 // Check all imports.
718 Library& lib_it = Library::Handle();
719 for (intptr_t i = 0; i < library.num_imports(); i++) {
720 lib_it ^= library.ImportLibraryAt(i);
721 result ^= LookupFunctionOrFieldInLibraryHelper(
722 lib_it, class_name, lookup_name);
723 if (result.raw() != Object::sentinel().raw()) {
724 return result.raw();
725 }
726 }
727
728 // Fall through case: Indicate that we didn't find any function or field using
729 // a special null instance. This is different from a field being null. Callers
730 // make sure that this null does not leak into Dartland.
731 return Object::sentinel().raw();
732 }
733
734
402 DEFINE_NATIVE_ENTRY(Mirrors_makeLocalMirrorSystem, 0) { 735 DEFINE_NATIVE_ENTRY(Mirrors_makeLocalMirrorSystem, 0) {
403 return CreateMirrorSystem(); 736 return CreateMirrorSystem();
404 } 737 }
405 738
406 739
407 DEFINE_NATIVE_ENTRY(Mirrors_makeLocalClassMirror, 1) { 740 DEFINE_NATIVE_ENTRY(Mirrors_makeLocalClassMirror, 1) {
408 GET_NON_NULL_NATIVE_ARGUMENT(Type, type, arguments->NativeArgAt(0)); 741 GET_NON_NULL_NATIVE_ARGUMENT(Type, type, arguments->NativeArgAt(0));
409 const Class& cls = Class::Handle(type.type_class()); 742 const Class& cls = Class::Handle(type.type_class());
410 ASSERT(!cls.IsNull()); 743 ASSERT(!cls.IsNull());
411 if (cls.IsDynamicClass() || cls.IsVoidClass()) { 744 if (cls.IsDynamicClass() || cls.IsVoidClass()) {
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 832
500 DEFINE_NATIVE_ENTRY(FunctionTypeMirror_return_type, 1) { 833 DEFINE_NATIVE_ENTRY(FunctionTypeMirror_return_type, 1) {
501 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); 834 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0));
502 const Class& cls = Class::Handle(ref.GetClassReferent()); 835 const Class& cls = Class::Handle(ref.GetClassReferent());
503 const Function& func = Function::Handle(CallMethod(cls)); 836 const Function& func = Function::Handle(CallMethod(cls));
504 ASSERT(!func.IsNull()); 837 ASSERT(!func.IsNull());
505 return func.result_type(); 838 return func.result_type();
506 } 839 }
507 840
508 841
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) { 842 DEFINE_NATIVE_ENTRY(ClassMirror_library, 1) {
522 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); 843 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0));
523 const Class& klass = Class::Handle(ref.GetClassReferent()); 844 const Class& klass = Class::Handle(ref.GetClassReferent());
524 const Library& library = Library::Handle(klass.library()); 845 const Library& library = Library::Handle(klass.library());
525 ASSERT(!library.IsNull()); 846 ASSERT(!library.IsNull());
526 return CreateLibraryMirror(library); 847 return CreateLibraryMirror(library);
527 } 848 }
528 849
529 850
530 DEFINE_NATIVE_ENTRY(ClassMirror_supertype, 1) { 851 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(); 1071 ObjectStore* object_store = isolate->object_store();
751 const Class& cls = Class::Handle(isolate, object_store->object_class()); 1072 const Class& cls = Class::Handle(isolate, object_store->object_class());
752 const Function& function = 1073 const Function& function =
753 Function::Handle(isolate, cls.LookupDynamicFunction(Symbols::hashCode())); 1074 Function::Handle(isolate, cls.LookupDynamicFunction(Symbols::hashCode()));
754 const Array& args = Array::Handle(isolate, Array::New(1)); 1075 const Array& args = Array::Handle(isolate, Array::New(1));
755 args.SetAt(0, reflectee); 1076 args.SetAt(0, reflectee);
756 return DartEntry::InvokeFunction(function, args); 1077 return DartEntry::InvokeFunction(function, args);
757 } 1078 }
758 1079
759 1080
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) { 1081 DEFINE_NATIVE_ENTRY(InstanceMirror_invoke, 5) {
795 // Argument 0 is the mirror, which is unused by the native. It exists 1082 // 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 1083 // because this native is an instance method in order to be polymorphic
797 // with its cousins. 1084 // with its cousins.
798 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1)); 1085 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1));
799 GET_NON_NULL_NATIVE_ARGUMENT( 1086 GET_NON_NULL_NATIVE_ARGUMENT(
800 String, function_name, arguments->NativeArgAt(2)); 1087 String, function_name, arguments->NativeArgAt(2));
801 GET_NON_NULL_NATIVE_ARGUMENT(Array, args, arguments->NativeArgAt(3)); 1088 GET_NON_NULL_NATIVE_ARGUMENT(Array, args, arguments->NativeArgAt(3));
802 GET_NON_NULL_NATIVE_ARGUMENT(Array, arg_names, arguments->NativeArgAt(4)); 1089 GET_NON_NULL_NATIVE_ARGUMENT(Array, arg_names, arguments->NativeArgAt(4));
803 1090
804 Class& klass = Class::Handle(reflectee.clazz()); 1091 Class& klass = Class::Handle(reflectee.clazz());
805 Function& function = Function::Handle( 1092 Function& function = Function::Handle(
806 Resolver::ResolveDynamicAnyArgsAllowPrivate(klass, function_name)); 1093 Resolver::ResolveDynamicAnyArgsAllowPrivate(klass, function_name));
807 1094
808 const Array& args_descriptor = 1095 const Array& args_descriptor =
809 Array::Handle(ArgumentsDescriptor::New(args.Length(), arg_names)); 1096 Array::Handle(ArgumentsDescriptor::New(args.Length(), arg_names));
810 1097
811 return ReflectivelyInvokeDynamicFunction(reflectee, 1098 return InvokeDynamicFunction(reflectee,
812 function, 1099 function,
813 function_name, 1100 function_name,
814 args, 1101 args,
815 args_descriptor); 1102 args_descriptor);
816 } 1103 }
817 1104
818 1105
819 DEFINE_NATIVE_ENTRY(InstanceMirror_invokeGetter, 3) { 1106 DEFINE_NATIVE_ENTRY(InstanceMirror_invokeGetter, 3) {
820 // Argument 0 is the mirror, which is unused by the native. It exists 1107 // 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 1108 // because this native is an instance method in order to be polymorphic
822 // with its cousins. 1109 // with its cousins.
823 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1)); 1110 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1));
824 GET_NON_NULL_NATIVE_ARGUMENT(String, getter_name, arguments->NativeArgAt(2)); 1111 GET_NON_NULL_NATIVE_ARGUMENT(String, getter_name, arguments->NativeArgAt(2));
825
826 Class& klass = Class::Handle(reflectee.clazz()); 1112 Class& klass = Class::Handle(reflectee.clazz());
827 String& internal_getter_name = String::Handle(Field::GetterName(getter_name)); 1113 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 } 1114 }
843 1115
844 1116
845 DEFINE_NATIVE_ENTRY(InstanceMirror_invokeSetter, 4) { 1117 DEFINE_NATIVE_ENTRY(InstanceMirror_invokeSetter, 4) {
846 // Argument 0 is the mirror, which is unused by the native. It exists 1118 // 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 1119 // because this native is an instance method in order to be polymorphic
848 // with its cousins. 1120 // with its cousins.
849 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1)); 1121 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1));
850 GET_NON_NULL_NATIVE_ARGUMENT(String, setter_name, arguments->NativeArgAt(2)); 1122 GET_NON_NULL_NATIVE_ARGUMENT(String, setter_name, arguments->NativeArgAt(2));
851 GET_NATIVE_ARGUMENT(Instance, value, arguments->NativeArgAt(3)); 1123 GET_NATIVE_ARGUMENT(Instance, value, arguments->NativeArgAt(3));
(...skipping 23 matching lines...) Expand all
875 } 1147 }
876 1148
877 // Invoke the setter and return the result. 1149 // Invoke the setter and return the result.
878 const int kNumArgs = 2; 1150 const int kNumArgs = 2;
879 const Array& args = Array::Handle(Array::New(kNumArgs)); 1151 const Array& args = Array::Handle(Array::New(kNumArgs));
880 args.SetAt(0, reflectee); 1152 args.SetAt(0, reflectee);
881 args.SetAt(1, value); 1153 args.SetAt(1, value);
882 const Array& args_descriptor = 1154 const Array& args_descriptor =
883 Array::Handle(ArgumentsDescriptor::New(args.Length())); 1155 Array::Handle(ArgumentsDescriptor::New(args.Length()));
884 1156
885 return ReflectivelyInvokeDynamicFunction(reflectee, 1157 return InvokeDynamicFunction(reflectee,
886 setter, 1158 setter,
887 internal_setter_name, 1159 internal_setter_name,
888 args, 1160 args,
889 args_descriptor); 1161 args_descriptor);
890 } 1162 }
891 1163
892 1164
893 DEFINE_NATIVE_ENTRY(ClosureMirror_apply, 3) { 1165 DEFINE_NATIVE_ENTRY(ClosureMirror_apply, 3) {
894 GET_NON_NULL_NATIVE_ARGUMENT(Instance, closure, arguments->NativeArgAt(0)); 1166 GET_NON_NULL_NATIVE_ARGUMENT(Instance, closure, arguments->NativeArgAt(0));
895 ASSERT(!closure.IsNull() && closure.IsCallable(NULL, NULL)); 1167 ASSERT(!closure.IsNull() && closure.IsCallable(NULL, NULL));
896 GET_NON_NULL_NATIVE_ARGUMENT(Array, args, arguments->NativeArgAt(1)); 1168 GET_NON_NULL_NATIVE_ARGUMENT(Array, args, arguments->NativeArgAt(1));
897 GET_NON_NULL_NATIVE_ARGUMENT(Array, arg_names, arguments->NativeArgAt(2)); 1169 GET_NON_NULL_NATIVE_ARGUMENT(Array, arg_names, arguments->NativeArgAt(2));
898 1170
899 const Array& args_descriptor = 1171 const Array& args_descriptor =
900 Array::Handle(ArgumentsDescriptor::New(args.Length(), arg_names)); 1172 Array::Handle(ArgumentsDescriptor::New(args.Length(), arg_names));
901 1173
902 const Object& result = 1174 const Object& result =
903 Object::Handle(DartEntry::InvokeClosure(args, args_descriptor)); 1175 Object::Handle(DartEntry::InvokeClosure(args, args_descriptor));
904 if (result.IsError()) { 1176 if (result.IsError()) {
905 ThrowInvokeError(Error::Cast(result)); 1177 ThrowInvokeError(Error::Cast(result));
906 UNREACHABLE(); 1178 UNREACHABLE();
907 } 1179 }
908 return result.raw(); 1180 return result.raw();
909 } 1181 }
910 1182
911 1183
1184 DEFINE_NATIVE_ENTRY(ClosureMirror_find_in_context, 2) {
1185 GET_NON_NULL_NATIVE_ARGUMENT(Instance, closure, arguments->NativeArgAt(0));
1186 GET_NON_NULL_NATIVE_ARGUMENT(Array, lookup_parts, arguments->NativeArgAt(1));
1187 ASSERT(lookup_parts.Length() >= 1 && lookup_parts.Length() <= 3);
1188
1189 Function& function = Function::Handle();
1190 const bool callable = closure.IsCallable(&function, NULL);
1191 ASSERT(callable);
1192
1193 const int parts_len = lookup_parts.Length();
1194 // Lookup name is always the last part.
1195 const String& lookup_name = String::Handle(String::RawCast(
1196 lookup_parts.At(parts_len - 1)));
1197
1198 String& part_name = String::Handle();
1199 Class& owner = Class::Handle(function.Owner());
1200 LibraryPrefix& prefix = LibraryPrefix::Handle();
1201 Library& this_library = Library::Handle(owner.library());
1202 Instance& result = Instance::Handle(Object::sentinel().raw());
1203 if (parts_len == 1) {
1204 // Could be either a field in context, an instance or static field of the
1205 // enclosing class, or a field in the current library or any imported
1206 // library.
1207 result ^= LookupFunctionOrFieldInFunctionContext(
1208 function, Context::Handle(Closure::context(closure)), lookup_name);
1209 if (result.raw() == Object::sentinel().raw()) {
1210 result ^= LookupStaticFunctionOrFieldInClass(owner, lookup_name);
1211 }
1212 if (result.raw() == Object::sentinel().raw()) {
1213 result ^= LookupFunctionOrFieldInLibrary(this_library,
1214 part_name,
1215 lookup_name);
1216 }
1217 } else if (parts_len == 2) {
1218 // Could be either library.field or class.staticfield.
1219 part_name ^= lookup_parts.At(0);
1220 prefix ^= this_library.LookupLocalLibraryPrefix(part_name);
1221 if (prefix.IsNull()) {
1222 result ^= LookupFunctionOrFieldInLibrary(this_library,
1223 part_name,
1224 lookup_name);
1225 } else {
1226 result ^= LookupFunctionOrFieldInLibraryPrefix(prefix, lookup_name);
1227 }
1228 } else {
1229 ASSERT(parts_len == 3);
1230 // Can only be library.class.staticfield.
1231 part_name ^= lookup_parts.At(0);
1232 prefix ^= this_library.LookupLocalLibraryPrefix(part_name);
1233 if (!prefix.IsNull()) {
1234 part_name ^= lookup_parts.At(1);
1235 owner ^= prefix.LookupClass(part_name);
1236 if (!owner.IsNull()) {
1237 result ^= LookupStaticFunctionOrFieldInClass(owner, lookup_name);
1238 }
1239 }
1240 }
1241
1242 // We return a tuple (list) where the first slot is a boolean indicates
1243 // whether we found a field or function and the second slot contains the
1244 // result. This is needed to distinguish between not finding a field and a
1245 // field containing null as value.
1246 const Array& result_tuple = Array::Handle(Array::New(2));
1247 if (result.raw() == Object::sentinel().raw()) {
1248 result_tuple.SetAt(0, Bool::False());
1249 // No need to set the value.
1250 } else {
1251 result_tuple.SetAt(0, Bool::True());
1252 result_tuple.SetAt(1, result);
1253 }
1254 return result_tuple.raw();
1255 }
1256
1257
912 DEFINE_NATIVE_ENTRY(ClosureMirror_function, 1) { 1258 DEFINE_NATIVE_ENTRY(ClosureMirror_function, 1) {
913 GET_NON_NULL_NATIVE_ARGUMENT(Instance, closure, arguments->NativeArgAt(0)); 1259 GET_NON_NULL_NATIVE_ARGUMENT(Instance, closure, arguments->NativeArgAt(0));
914 ASSERT(!closure.IsNull()); 1260 ASSERT(!closure.IsNull());
915 1261
916 Function& function = Function::Handle(); 1262 Function& function = Function::Handle();
917 bool callable = closure.IsCallable(&function, NULL); 1263 bool callable = closure.IsCallable(&function, NULL);
918 ASSERT(callable); 1264 ASSERT(callable);
919 1265
920 return CreateMethodMirror(function, Instance::null_instance()); 1266 return CreateMethodMirror(function, Instance::null_instance());
921 } 1267 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
961 } 1307 }
962 1308
963 1309
964 DEFINE_NATIVE_ENTRY(ClassMirror_invokeGetter, 3) { 1310 DEFINE_NATIVE_ENTRY(ClassMirror_invokeGetter, 3) {
965 // Argument 0 is the mirror, which is unused by the native. It exists 1311 // 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 1312 // because this native is an instance method in order to be polymorphic
967 // with its cousins. 1313 // with its cousins.
968 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1)); 1314 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1));
969 const Class& klass = Class::Handle(ref.GetClassReferent()); 1315 const Class& klass = Class::Handle(ref.GetClassReferent());
970 GET_NON_NULL_NATIVE_ARGUMENT(String, getter_name, arguments->NativeArgAt(2)); 1316 GET_NON_NULL_NATIVE_ARGUMENT(String, getter_name, arguments->NativeArgAt(2));
971 1317 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 } 1318 }
1009 1319
1010 1320
1011 DEFINE_NATIVE_ENTRY(ClassMirror_invokeSetter, 4) { 1321 DEFINE_NATIVE_ENTRY(ClassMirror_invokeSetter, 4) {
1012 // Argument 0 is the mirror, which is unused by the native. It exists 1322 // 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 1323 // because this native is an instance method in order to be polymorphic
1014 // with its cousins. 1324 // with its cousins.
1015 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1)); 1325 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1));
1016 const Class& klass = Class::Handle(ref.GetClassReferent()); 1326 const Class& klass = Class::Handle(ref.GetClassReferent());
1017 GET_NON_NULL_NATIVE_ARGUMENT(String, setter_name, arguments->NativeArgAt(2)); 1327 GET_NON_NULL_NATIVE_ARGUMENT(String, setter_name, arguments->NativeArgAt(2));
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
1224 } 1534 }
1225 1535
1226 1536
1227 DEFINE_NATIVE_ENTRY(LibraryMirror_invokeGetter, 3) { 1537 DEFINE_NATIVE_ENTRY(LibraryMirror_invokeGetter, 3) {
1228 // Argument 0 is the mirror, which is unused by the native. It exists 1538 // 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 1539 // because this native is an instance method in order to be polymorphic
1230 // with its cousins. 1540 // with its cousins.
1231 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1)); 1541 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1));
1232 const Library& library = Library::Handle(ref.GetLibraryReferent()); 1542 const Library& library = Library::Handle(ref.GetLibraryReferent());
1233 GET_NON_NULL_NATIVE_ARGUMENT(String, getter_name, arguments->NativeArgAt(2)); 1543 GET_NON_NULL_NATIVE_ARGUMENT(String, getter_name, arguments->NativeArgAt(2));
1234 1544 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 } 1545 }
1284 1546
1285 1547
1286 DEFINE_NATIVE_ENTRY(LibraryMirror_invokeSetter, 4) { 1548 DEFINE_NATIVE_ENTRY(LibraryMirror_invokeSetter, 4) {
1287 // Argument 0 is the mirror, which is unused by the native. It exists 1549 // 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 1550 // because this native is an instance method in order to be polymorphic
1289 // with its cousins. 1551 // with its cousins.
1290 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1)); 1552 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1));
1291 const Library& library = Library::Handle(ref.GetLibraryReferent()); 1553 const Library& library = Library::Handle(ref.GetLibraryReferent());
1292 GET_NON_NULL_NATIVE_ARGUMENT(String, setter_name, arguments->NativeArgAt(2)); 1554 GET_NON_NULL_NATIVE_ARGUMENT(String, setter_name, arguments->NativeArgAt(2));
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
1419 } 1681 }
1420 1682
1421 1683
1422 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { 1684 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) {
1423 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); 1685 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0));
1424 const Field& field = Field::Handle(ref.GetFieldReferent()); 1686 const Field& field = Field::Handle(ref.GetFieldReferent());
1425 return field.type(); 1687 return field.type();
1426 } 1688 }
1427 1689
1428 } // namespace dart 1690 } // 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