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

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: 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 bool FieldIsUninitialized(const Field& field) {
403 ASSERT(!field.IsNull());
404
405 // Return getter method for uninitialized fields, rather than the
406 // field object, since the value in the field object will not be
407 // initialized until the first time the getter is invoked.
408 const Instance& value = Instance::Handle(field.value());
409 ASSERT(value.raw() != Object::transition_sentinel().raw());
410 return value.raw() == Object::sentinel().raw();
411 }
siva 2013/09/26 17:05:07 This function should probably be an instance metho
rmacnak 2013/09/26 17:49:39 Please also update the equivalent use in Dart_SetF
Michael Lippautz (Google) 2013/09/26 21:16:01 Done.(including Dart_GetField/_SetField)
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* ReflectivelyInvokeDynamicFunction(
rmacnak 2013/09/26 17:49:39 Let's drop the "Reflectively" for parallelism with
Michael Lippautz (Google) 2013/09/26 21:16:01 Done.
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
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
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 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
455 // getter Function. The getter function may either be in the
456 // library or in the field's owner class, depending.
siva 2013/09/26 17:05:07 This comment seems to be incomplete....
rmacnak 2013/09/26 17:49:39 To perform a reflective getter invocation, we need
Michael Lippautz (Google) 2013/09/26 21:16:01 Done.
457 const Field& field = Field::Handle(
458 library.LookupFieldAllowPrivate(getter_name));
459 Function& getter = Function::Handle();
460 if (field.IsNull()) {
461 // No field found and no ambiguity error. Check for a getter in the lib.
462 const String& internal_getter_name =
463 String::Handle(Field::GetterName(getter_name));
464 getter = library.LookupFunctionAllowPrivate(internal_getter_name);
465 if (getter.IsNull()) {
466 getter = library.LookupFunctionAllowPrivate(getter_name);
467 if (!getter.IsNull()) {
468 // Looking for a getter but found a regular method: closurize.
469 const Function& closure_function =
470 Function::Handle(getter.ImplicitClosureFunction());
471 return closure_function.ImplicitStaticClosure();
472 }
siva 2013/09/26 17:05:07 I saw some similar code in Ryan's CL maybe it need
rmacnak 2013/09/26 17:49:39 That code was moved here, not duplicated.
Michael Lippautz (Google) 2013/09/26 21:16:01 This is Ryan's code. It just moved here.
473 }
474 } else if (!field.IsNull() && FieldIsUninitialized(field)) {
siva 2013/09/26 17:05:07 At this point we know field.IsNull() is not true w
Michael Lippautz (Google) 2013/09/26 21:16:01 Done.
475 // A field was found. Check for a getter in the field's owner classs.
476 const Class& klass = Class::Handle(field.owner());
477 const String& internal_getter_name =
478 String::Handle(Field::GetterName(getter_name));
479 getter = klass.LookupStaticFunctionAllowPrivate(internal_getter_name);
480 }
481
482 if (!getter.IsNull() && getter.is_visible()) {
483 // Invoke the getter and return the result.
484 const Object& result = Object::Handle(
485 DartEntry::InvokeFunction(getter, Object::empty_array()));
486 if (result.IsError()) {
487 ThrowInvokeError(Error::Cast(result));
488 UNREACHABLE();
489 } else if (result.IsInstance()) {
490 return Instance::Cast(result).raw();
491 }
492 ASSERT(result.IsNull());
493 return Instance::null();
siva 2013/09/26 17:05:07 Maybe you should have a static helper method stati
Michael Lippautz (Google) 2013/09/26 21:16:01 Done.
494 }
495 if (!field.IsNull()) {
496 return field.value();
497 }
siva 2013/09/26 17:05:07 I think the code needs to be restructured as if (f
Michael Lippautz (Google) 2013/09/26 21:16:01 Done.
498 if (throw_nsm_if_absent) {
499 ThrowNoSuchMethod(Instance::null_instance(),
500 getter_name,
501 getter,
502 InvocationMirror::kTopLevel,
503 InvocationMirror::kGetter);
504 UNREACHABLE();
505 }
506 return Instance::null();
507 }
508
509
510 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() || FieldIsUninitialized(field)) {
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 return Instance::null();
540 }
541
542 // Invoke the getter and return the result.
543 Object& result = Object::Handle(
rmacnak 2013/09/26 17:49:39 const
Michael Lippautz (Google) 2013/09/26 21:16:01 Done.
544 DartEntry::InvokeFunction(getter, Object::empty_array()));
545 if (result.IsError()) {
546 ThrowInvokeError(Error::Cast(result));
547 UNREACHABLE();
548 } else if (result.IsInstance()) {
549 return Instance::Cast(result).raw();
550 }
551 ASSERT(result.IsNull());
552 return Instance::null();
siva 2013/09/26 17:05:07 ditto comment about returnResult
Michael Lippautz (Google) 2013/09/26 21:16:01 Done.
553 }
554 return field.value();
555 }
556
557
558
559
560 RawInstance* InvokeInstanceGetter(const Class& klass,
561 const Instance& reflectee,
562 const String& getter_name,
563 const bool throw_nsm_if_absent) {
564 String& internal_getter_name = String::Handle(Field::GetterName(getter_name));
565 Function& function = Function::Handle(
566 Resolver::ResolveDynamicAnyArgsAllowPrivate(klass, internal_getter_name));
567
568 if (!function.IsNull() || (function.IsNull() && throw_nsm_if_absent)) {
siva 2013/09/26 17:05:07 shouldn't this just be if (!function.IsNull() || t
Michael Lippautz (Google) 2013/09/26 21:16:01 Done.
569 const int kNumArgs = 1;
570 const Array& args = Array::Handle(Array::New(kNumArgs));
571 args.SetAt(0, reflectee);
572 const Array& args_descriptor =
573 Array::Handle(ArgumentsDescriptor::New(args.Length()));
574
575 return ReflectivelyInvokeDynamicFunction(reflectee,
576 function,
577 internal_getter_name,
578 args,
579 args_descriptor);
580 }
581 return Instance::null();
582 }
583
584
585 RawInstance* LookupFunctionOrFieldInLibraryPrefix(
586 const LibraryPrefix& prefix,
587 const String& lookup_name) {
588 Instance& result = Instance::Handle();
589 const Object& entry = Object::Handle(prefix.LookupObject(lookup_name));
590 if (!entry.IsNull() && 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.IsNull()) {
596 return result.raw();
597 }
siva 2013/09/26 17:05:07 why not just return result.raw();
Michael Lippautz (Google) 2013/09/26 21:16:01 Done.
598 } else if (!entry.IsNull() && entry.IsFunction()) {
siva 2013/09/26 17:05:07 why test of !entry.IsNull() twice? Structure as if
rmacnak 2013/09/26 17:49:39 Could it be a Class?
Michael Lippautz (Google) 2013/09/26 21:16:01 Can be a class. Restructured nonetheless.
599 const Function& func = Function::Cast(entry);
600 const Function& closure_function = Function::Handle(
601 func.ImplicitClosureFunction());
602 return closure_function.ImplicitStaticClosure();
603 }
604 return Instance::null();
605 }
606
607
608 RawInstance* LookupStaticFunctionOrFieldInClass(const Class& klass,
609 const String& lookup_name) {
610 Instance& result = Instance::Handle(
611 InvokeClassGetter(klass, lookup_name, false));
612 if (!result.IsNull()) {
613 return result.raw();
614 }
615
616 Function& func = Function::Handle();
617 Class& kls = Class::Handle(klass.raw());
rmacnak 2013/09/26 17:49:39 Consider 'lookup_class'
Michael Lippautz (Google) 2013/09/26 21:16:01 Done.
618 for (; func.IsNull() && !kls.IsNull(); kls = kls.SuperClass()) {
619 func ^= kls.LookupStaticFunctionAllowPrivate(lookup_name);
620 }
621 if (!func.IsNull()) {
622 const Function& closure_function = Function::Handle(
623 func.ImplicitClosureFunction());
siva 2013/09/26 17:05:07 ASSERT(!closure_function.IsNull());
Michael Lippautz (Google) 2013/09/26 21:16:01 Done.
624 return closure_function.ImplicitStaticClosure();
625 }
626
627 return Instance::null();
628 }
629
630
631 static RawInstance* LookupFunctionOrFieldInFunctionContext(
632 const Function& func,
633 const Context& ctx,
634 const String& lookup_name) {
635 const ContextScope& ctx_scope = ContextScope::Handle(func.context_scope());
636 intptr_t this_index = -1;
637
638 // Search local context.
639 String& name = String::Handle();
640 for (intptr_t i = 0; i < ctx_scope.num_variables(); i++) {
641 name ^= ctx_scope.NameAt(i);
642 if (name.Equals(lookup_name)) {
643 return ctx.At(i);
644 } else if (name.Equals(Symbols::This())) {
645 // Record instance index to search for the field in the instance
646 // afterwards.
647 this_index = i;
648 }
649 }
650
651 // Search the instance this function is attached to.
652 if (this_index >= 0) {
653 // Since we want the closurized version of a function, we can access, both,
654 // functions and fields through their internal getter name. If the internal
rmacnak 2013/09/26 17:49:39 implicit getter, not internal
Michael Lippautz (Google) 2013/09/26 21:16:01 Done.
655 // getter does not exist for the function, a method extractor will be
656 // created.
657 const String& internal_getter_name = String::Handle(
658 Field::GetterName(lookup_name));
659 const Class& owner = Class::Handle(func.Owner());
660 const Instance& receiver = Instance::Handle(ctx.At(this_index));
661 const Function& getter = Function::Handle(
662 Resolver::ResolveDynamicAnyArgsAllowPrivate(
663 owner, internal_getter_name));
664 if (!getter.IsNull()) {
665 const int kNumArgs = 1;
666 const Array& args = Array::Handle(Array::New(kNumArgs));
667 args.SetAt(0, receiver);
668 const Array& args_descriptor = Array::Handle(
669 ArgumentsDescriptor::New(args.Length()));
670
671 return ReflectivelyInvokeDynamicFunction(receiver,
672 getter,
673 internal_getter_name,
674 args,
675 args_descriptor);
676 }
677 }
678 return Instance::null();
679 }
680
681
682 RawInstance* LookupFunctionOrFieldInLibraryHelper(const Library& library,
683 const String& class_name,
684 const String& lookup_name) {
685 if (class_name.IsNull()) {
686 Instance& result = Instance::Handle(
687 InvokeLibraryGetter(library, lookup_name, false));
688 if (!result.IsNull()) {
689 return result.raw();
690 }
691 Function& func = Function::Handle(
692 library.LookupFunctionAllowPrivate(lookup_name));
693 if (!func.IsNull()) {
694 const Function& closure_function = Function::Handle(
695 func.ImplicitClosureFunction());
696 return closure_function.ImplicitStaticClosure();
697 }
698 } else {
699 Class& cls = Class::Handle(
700 library.LookupClassAllowPrivate(class_name));
701 if (!cls.IsNull()) {
702 return LookupStaticFunctionOrFieldInClass(cls, lookup_name);
703 }
704 }
705 return Instance::null();
706 }
707
708
709 RawInstance* LookupFunctionOrFieldInLibrary(const Library& library,
710 const String& class_name,
711 const String& lookup_name) {
712 Instance& result = Instance::Handle();
713 // Check current library.
714 result ^= LookupFunctionOrFieldInLibraryHelper(
715 library, class_name, lookup_name);
716 if (!result.IsNull()) {
717 return result.raw();
718 }
719 // Check all imports.
720 Library& lib_it = Library::Handle();
721 for (intptr_t i = 0; i < library.num_imports(); i++) {
722 lib_it ^= library.ImportLibraryAt(i);
723 result ^= LookupFunctionOrFieldInLibraryHelper(
724 lib_it, class_name, lookup_name);
725 if (!result.IsNull()) {
726 return result.raw();
727 }
728 }
729 return Instance::null();
730 }
731
732
402 DEFINE_NATIVE_ENTRY(Mirrors_makeLocalMirrorSystem, 0) { 733 DEFINE_NATIVE_ENTRY(Mirrors_makeLocalMirrorSystem, 0) {
403 return CreateMirrorSystem(); 734 return CreateMirrorSystem();
404 } 735 }
405 736
406 737
407 DEFINE_NATIVE_ENTRY(Mirrors_makeLocalClassMirror, 1) { 738 DEFINE_NATIVE_ENTRY(Mirrors_makeLocalClassMirror, 1) {
408 GET_NON_NULL_NATIVE_ARGUMENT(Type, type, arguments->NativeArgAt(0)); 739 GET_NON_NULL_NATIVE_ARGUMENT(Type, type, arguments->NativeArgAt(0));
409 const Class& cls = Class::Handle(type.type_class()); 740 const Class& cls = Class::Handle(type.type_class());
410 ASSERT(!cls.IsNull()); 741 ASSERT(!cls.IsNull());
411 if (cls.IsDynamicClass() || cls.IsVoidClass()) { 742 if (cls.IsDynamicClass() || cls.IsVoidClass()) {
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 830
500 DEFINE_NATIVE_ENTRY(FunctionTypeMirror_return_type, 1) { 831 DEFINE_NATIVE_ENTRY(FunctionTypeMirror_return_type, 1) {
501 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); 832 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0));
502 const Class& cls = Class::Handle(ref.GetClassReferent()); 833 const Class& cls = Class::Handle(ref.GetClassReferent());
503 const Function& func = Function::Handle(CallMethod(cls)); 834 const Function& func = Function::Handle(CallMethod(cls));
504 ASSERT(!func.IsNull()); 835 ASSERT(!func.IsNull());
505 return func.result_type(); 836 return func.result_type();
506 } 837 }
507 838
508 839
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) { 840 DEFINE_NATIVE_ENTRY(ClassMirror_library, 1) {
522 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); 841 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0));
523 const Class& klass = Class::Handle(ref.GetClassReferent()); 842 const Class& klass = Class::Handle(ref.GetClassReferent());
524 const Library& library = Library::Handle(klass.library()); 843 const Library& library = Library::Handle(klass.library());
525 ASSERT(!library.IsNull()); 844 ASSERT(!library.IsNull());
526 return CreateLibraryMirror(library); 845 return CreateLibraryMirror(library);
527 } 846 }
528 847
529 848
530 DEFINE_NATIVE_ENTRY(ClassMirror_supertype, 1) { 849 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(); 1069 ObjectStore* object_store = isolate->object_store();
751 const Class& cls = Class::Handle(isolate, object_store->object_class()); 1070 const Class& cls = Class::Handle(isolate, object_store->object_class());
752 const Function& function = 1071 const Function& function =
753 Function::Handle(isolate, cls.LookupDynamicFunction(Symbols::hashCode())); 1072 Function::Handle(isolate, cls.LookupDynamicFunction(Symbols::hashCode()));
754 const Array& args = Array::Handle(isolate, Array::New(1)); 1073 const Array& args = Array::Handle(isolate, Array::New(1));
755 args.SetAt(0, reflectee); 1074 args.SetAt(0, reflectee);
756 return DartEntry::InvokeFunction(function, args); 1075 return DartEntry::InvokeFunction(function, args);
757 } 1076 }
758 1077
759 1078
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) { 1079 DEFINE_NATIVE_ENTRY(InstanceMirror_invoke, 5) {
795 // Argument 0 is the mirror, which is unused by the native. It exists 1080 // 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 1081 // because this native is an instance method in order to be polymorphic
797 // with its cousins. 1082 // with its cousins.
798 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1)); 1083 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1));
799 GET_NON_NULL_NATIVE_ARGUMENT( 1084 GET_NON_NULL_NATIVE_ARGUMENT(
800 String, function_name, arguments->NativeArgAt(2)); 1085 String, function_name, arguments->NativeArgAt(2));
801 GET_NON_NULL_NATIVE_ARGUMENT(Array, args, arguments->NativeArgAt(3)); 1086 GET_NON_NULL_NATIVE_ARGUMENT(Array, args, arguments->NativeArgAt(3));
802 GET_NON_NULL_NATIVE_ARGUMENT(Array, arg_names, arguments->NativeArgAt(4)); 1087 GET_NON_NULL_NATIVE_ARGUMENT(Array, arg_names, arguments->NativeArgAt(4));
803 1088
(...skipping 11 matching lines...) Expand all
815 args_descriptor); 1100 args_descriptor);
816 } 1101 }
817 1102
818 1103
819 DEFINE_NATIVE_ENTRY(InstanceMirror_invokeGetter, 3) { 1104 DEFINE_NATIVE_ENTRY(InstanceMirror_invokeGetter, 3) {
820 // Argument 0 is the mirror, which is unused by the native. It exists 1105 // 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 1106 // because this native is an instance method in order to be polymorphic
822 // with its cousins. 1107 // with its cousins.
823 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1)); 1108 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1));
824 GET_NON_NULL_NATIVE_ARGUMENT(String, getter_name, arguments->NativeArgAt(2)); 1109 GET_NON_NULL_NATIVE_ARGUMENT(String, getter_name, arguments->NativeArgAt(2));
825
826 Class& klass = Class::Handle(reflectee.clazz()); 1110 Class& klass = Class::Handle(reflectee.clazz());
827 String& internal_getter_name = String::Handle(Field::GetterName(getter_name)); 1111 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 } 1112 }
843 1113
844 1114
845 DEFINE_NATIVE_ENTRY(InstanceMirror_invokeSetter, 4) { 1115 DEFINE_NATIVE_ENTRY(InstanceMirror_invokeSetter, 4) {
846 // Argument 0 is the mirror, which is unused by the native. It exists 1116 // 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 1117 // because this native is an instance method in order to be polymorphic
848 // with its cousins. 1118 // with its cousins.
849 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1)); 1119 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1));
850 GET_NON_NULL_NATIVE_ARGUMENT(String, setter_name, arguments->NativeArgAt(2)); 1120 GET_NON_NULL_NATIVE_ARGUMENT(String, setter_name, arguments->NativeArgAt(2));
851 GET_NATIVE_ARGUMENT(Instance, value, arguments->NativeArgAt(3)); 1121 GET_NATIVE_ARGUMENT(Instance, value, arguments->NativeArgAt(3));
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
902 const Object& result = 1172 const Object& result =
903 Object::Handle(DartEntry::InvokeClosure(args, args_descriptor)); 1173 Object::Handle(DartEntry::InvokeClosure(args, args_descriptor));
904 if (result.IsError()) { 1174 if (result.IsError()) {
905 ThrowInvokeError(Error::Cast(result)); 1175 ThrowInvokeError(Error::Cast(result));
906 UNREACHABLE(); 1176 UNREACHABLE();
907 } 1177 }
908 return result.raw(); 1178 return result.raw();
909 } 1179 }
910 1180
911 1181
1182 DEFINE_NATIVE_ENTRY(ClosureMirror_find_in_context, 2) {
1183 enum {
1184 kNoLookups = 0,
1185 kContext = 1,
1186 kStatic = 2,
1187 kLibraryPrefix = 4,
1188 kGlobal = 8
1189 };
1190
1191 GET_NON_NULL_NATIVE_ARGUMENT(Instance, closure, arguments->NativeArgAt(0));
1192 GET_NON_NULL_NATIVE_ARGUMENT(Array, lookup_parts, arguments->NativeArgAt(1));
1193 ASSERT(lookup_parts.Length() >= 1 && lookup_parts.Length() <= 3);
1194
1195 Function& function = Function::Handle();
1196 const bool callable = closure.IsCallable(&function, NULL);
1197 ASSERT(callable);
1198
1199 const int parts_len = lookup_parts.Length();
1200 // Lookup name is always the last part.
1201 const String& lookup_name = String::Handle(String::RawCast(
1202 lookup_parts.At(parts_len - 1)));
1203
1204 unsigned lookups = kNoLookups;
siva 2013/09/26 17:05:07 we don't normally used unsigned like this, why not
Michael Lippautz (Google) 2013/09/26 21:16:01 Not used anymore.
1205 String& part_name = String::Handle();
1206 Class& owner = Class::Handle(function.Owner());
1207 LibraryPrefix& prefix = LibraryPrefix::Handle();
1208 Library& this_library = Library::Handle(owner.library());
1209
1210 if (parts_len == 1) {
1211 // Could be either a field in context, an instance or static field of the
1212 // enclosing class, or a field in the current library or any imported
1213 // library.
1214 lookups = (kContext | kStatic | kGlobal);
1215 } else if (parts_len == 2) {
1216 // Could be either library.field or class.staticfield.
1217 part_name ^= lookup_parts.At(0);
1218 prefix ^= this_library.LookupLocalLibraryPrefix(part_name);
1219 lookups = prefix.IsNull() ? kGlobal : kLibraryPrefix;
1220 } else if (parts_len == 3) {
1221 // Can only be library.class.staticfield.
1222 part_name ^= lookup_parts.At(0);
1223 prefix ^= this_library.LookupLocalLibraryPrefix(part_name);
1224 if (prefix.IsNull()) {
1225 return Instance::null();
1226 }
1227 part_name ^= lookup_parts.At(1);
1228 owner ^= prefix.LookupClass(part_name);
1229 if (owner.IsNull()) {
1230 return Instance::null();
1231 }
1232 lookups = kStatic;
1233 } else {
1234 UNREACHABLE();
1235 }
1236
1237 Instance& result = Instance::Handle();
1238 if ((lookups & kContext) && result.IsNull()) {
rmacnak 2013/09/26 17:49:39 I'm suspicious of using null to indicate no-result
Michael Lippautz (Google) 2013/09/26 21:16:01 Done (see above).
1239 result ^= LookupFunctionOrFieldInFunctionContext(
1240 function, Context::Handle(Closure::context(closure)), lookup_name);
1241 }
1242 if ((lookups & kStatic) && result.IsNull() && !owner.IsTopLevel()) {
1243 result ^= LookupStaticFunctionOrFieldInClass(owner, lookup_name);
1244 }
1245 if ((lookups & kLibraryPrefix) && result.IsNull()) {
1246 result ^= LookupFunctionOrFieldInLibraryPrefix(prefix, lookup_name);
1247 }
1248 if ((lookups & kGlobal) && result.IsNull()) {
1249 result ^= LookupFunctionOrFieldInLibrary(this_library,
1250 part_name,
1251 lookup_name);
1252 }
siva 2013/09/26 17:05:07 I am not sure if this 'lookups' stuff is a good id
Michael Lippautz (Google) 2013/09/26 21:16:01 Inlined the lookup calls.
1253 if (!result.IsNull()) {
1254 return result.raw();
1255 }
1256 return Instance::null();
siva 2013/09/26 17:05:07 return result.raw(); is sufficient
Michael Lippautz (Google) 2013/09/26 21:16:01 Different now.
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