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

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