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

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

Issue 23441073: Implement closurization of regular methods in ObjectMirror.getField in the VM. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: rebase Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | runtime/lib/mirrors_impl.dart » ('j') | runtime/vm/object.cc » ('J')
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"
11 #include "vm/parser.h" 11 #include "vm/parser.h"
12 #include "vm/port.h" 12 #include "vm/port.h"
13 #include "vm/resolver.h"
13 #include "vm/symbols.h" 14 #include "vm/symbols.h"
14 15
15 namespace dart { 16 namespace dart {
16 17
17 static RawInstance* CreateMirror(const String& mirror_class_name, 18 static RawInstance* CreateMirror(const String& mirror_class_name,
18 const Array& constructor_arguments) { 19 const Array& constructor_arguments) {
19 const Library& mirrors_lib = Library::Handle(Library::MirrorsLibrary()); 20 const Library& mirrors_lib = Library::Handle(Library::MirrorsLibrary());
20 const String& constructor_name = Symbols::Dot(); 21 const String& constructor_name = Symbols::Dot();
21 22
22 const Object& result = Object::Handle( 23 const Object& result = Object::Handle(
(...skipping 759 matching lines...) Expand 10 before | Expand all | Expand 10 after
782 args_descriptor_array); 783 args_descriptor_array);
783 } 784 }
784 785
785 if (result.IsError()) { 786 if (result.IsError()) {
786 ThrowInvokeError(Error::Cast(result)); 787 ThrowInvokeError(Error::Cast(result));
787 UNREACHABLE(); 788 UNREACHABLE();
788 } 789 }
789 return result.raw(); 790 return result.raw();
790 } 791 }
791 792
793
792 DEFINE_NATIVE_ENTRY(InstanceMirror_invoke, 5) { 794 DEFINE_NATIVE_ENTRY(InstanceMirror_invoke, 5) {
793 // Argument 0 is the mirror, which is unused by the native. It exists 795 // Argument 0 is the mirror, which is unused by the native. It exists
794 // because this native is an instance method in order to be polymorphic 796 // because this native is an instance method in order to be polymorphic
795 // with its cousins. 797 // with its cousins.
796 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1)); 798 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1));
797 GET_NON_NULL_NATIVE_ARGUMENT( 799 GET_NON_NULL_NATIVE_ARGUMENT(
798 String, function_name, arguments->NativeArgAt(2)); 800 String, function_name, arguments->NativeArgAt(2));
799 GET_NON_NULL_NATIVE_ARGUMENT(Array, args, arguments->NativeArgAt(3)); 801 GET_NON_NULL_NATIVE_ARGUMENT(Array, args, arguments->NativeArgAt(3));
800 GET_NON_NULL_NATIVE_ARGUMENT(Array, arg_names, arguments->NativeArgAt(4)); 802 GET_NON_NULL_NATIVE_ARGUMENT(Array, arg_names, arguments->NativeArgAt(4));
801 803
804 Class& klass = Class::Handle(reflectee.clazz());
805 Function& function = Function::Handle(
806 Resolver::ResolveDynamicAnyArgsAllowPrivate(klass, function_name));
807
802 const Array& args_descriptor = 808 const Array& args_descriptor =
803 Array::Handle(ArgumentsDescriptor::New(args.Length(), arg_names)); 809 Array::Handle(ArgumentsDescriptor::New(args.Length(), arg_names));
804 810
805 Class& klass = Class::Handle(reflectee.clazz());
806 Function& function = Function::Handle();
807 while (!klass.IsNull()) {
808 function = klass.LookupDynamicFunctionAllowPrivate(function_name);
809 if (!function.IsNull()) {
810 break;
811 }
812 klass = klass.SuperClass();
813 }
814
815 return ReflectivelyInvokeDynamicFunction(reflectee, 811 return ReflectivelyInvokeDynamicFunction(reflectee,
816 function, 812 function,
817 function_name, 813 function_name,
818 args, 814 args,
819 args_descriptor); 815 args_descriptor);
820 } 816 }
821 817
822 818
823 DEFINE_NATIVE_ENTRY(InstanceMirror_invokeGetter, 3) { 819 DEFINE_NATIVE_ENTRY(InstanceMirror_invokeGetter, 3) {
824 // Argument 0 is the mirror, which is unused by the native. It exists 820 // Argument 0 is the mirror, which is unused by the native. It exists
825 // because this native is an instance method in order to be polymorphic 821 // because this native is an instance method in order to be polymorphic
826 // with its cousins. 822 // with its cousins.
827 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1)); 823 GET_NATIVE_ARGUMENT(Instance, reflectee, arguments->NativeArgAt(1));
828 GET_NON_NULL_NATIVE_ARGUMENT(String, getter_name, arguments->NativeArgAt(2)); 824 GET_NON_NULL_NATIVE_ARGUMENT(String, getter_name, arguments->NativeArgAt(2));
829 825
830 // Every instance field has a getter Function. Try to find the
831 // getter in any superclass and use that function to access the
832 // field.
833 // NB: We do not use Resolver::ResolveDynamic because we want to find private
834 // members.
835 Class& klass = Class::Handle(reflectee.clazz()); 826 Class& klass = Class::Handle(reflectee.clazz());
836 String& internal_getter_name = String::Handle(Field::GetterName(getter_name)); 827 String& internal_getter_name = String::Handle(Field::GetterName(getter_name));
837 Function& getter = Function::Handle(); 828 Function& function = Function::Handle(
838 while (!klass.IsNull()) { 829 Resolver::ResolveDynamicAnyArgsAllowPrivate(klass, internal_getter_name));
839 getter = klass.LookupDynamicFunctionAllowPrivate(internal_getter_name);
840 if (!getter.IsNull()) {
841 break;
842 }
843 klass = klass.SuperClass();
844 }
845 830
846 const int kNumArgs = 1; 831 const int kNumArgs = 1;
847 const Array& args = Array::Handle(Array::New(kNumArgs)); 832 const Array& args = Array::Handle(Array::New(kNumArgs));
848 args.SetAt(0, reflectee); 833 args.SetAt(0, reflectee);
849 const Array& args_descriptor = 834 const Array& args_descriptor =
850 Array::Handle(ArgumentsDescriptor::New(args.Length())); 835 Array::Handle(ArgumentsDescriptor::New(args.Length()));
851 836
852 return ReflectivelyInvokeDynamicFunction(reflectee, 837 return ReflectivelyInvokeDynamicFunction(reflectee,
853 getter, 838 function,
854 internal_getter_name, 839 internal_getter_name,
855 args, 840 args,
856 args_descriptor); 841 args_descriptor);
857 } 842 }
858 843
859 844
860 DEFINE_NATIVE_ENTRY(InstanceMirror_invokeSetter, 4) { 845 DEFINE_NATIVE_ENTRY(InstanceMirror_invokeSetter, 4) {
861 // Argument 0 is the mirror, which is unused by the native. It exists 846 // Argument 0 is the mirror, which is unused by the native. It exists
862 // because this native is an instance method in order to be polymorphic 847 // because this native is an instance method in order to be polymorphic
863 // with its cousins. 848 // with its cousins.
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
982 // with its cousins. 967 // with its cousins.
983 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1)); 968 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1));
984 const Class& klass = Class::Handle(ref.GetClassReferent()); 969 const Class& klass = Class::Handle(ref.GetClassReferent());
985 GET_NON_NULL_NATIVE_ARGUMENT(String, getter_name, arguments->NativeArgAt(2)); 970 GET_NON_NULL_NATIVE_ARGUMENT(String, getter_name, arguments->NativeArgAt(2));
986 971
987 // Note static fields do not have implicit getters. 972 // Note static fields do not have implicit getters.
988 const Field& field = Field::Handle(klass.LookupStaticField(getter_name)); 973 const Field& field = Field::Handle(klass.LookupStaticField(getter_name));
989 if (field.IsNull() || FieldIsUninitialized(field)) { 974 if (field.IsNull() || FieldIsUninitialized(field)) {
990 const String& internal_getter_name = String::Handle( 975 const String& internal_getter_name = String::Handle(
991 Field::GetterName(getter_name)); 976 Field::GetterName(getter_name));
992 const Function& getter = Function::Handle( 977 Function& getter = Function::Handle(
993 klass.LookupStaticFunctionAllowPrivate(internal_getter_name)); 978 klass.LookupStaticFunctionAllowPrivate(internal_getter_name));
994 979
995 if (getter.IsNull() || !getter.is_visible()) { 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 }
996 ThrowNoSuchMethod(AbstractType::Handle(klass.RareType()), 990 ThrowNoSuchMethod(AbstractType::Handle(klass.RareType()),
997 getter_name, 991 getter_name,
998 getter, 992 getter,
999 InvocationMirror::kStatic, 993 InvocationMirror::kStatic,
1000 InvocationMirror::kGetter); 994 InvocationMirror::kGetter);
1001 UNREACHABLE(); 995 UNREACHABLE();
1002 } 996 }
1003 997
1004 // Invoke the getter and return the result. 998 // Invoke the getter and return the result.
1005 Object& result = Object::Handle( 999 Object& result = Object::Handle(
1006 DartEntry::InvokeFunction(getter, Object::empty_array())); 1000 DartEntry::InvokeFunction(getter, Object::empty_array()));
1007 if (result.IsError()) { 1001 if (result.IsError()) {
1008 ThrowInvokeError(Error::Cast(result)); 1002 ThrowInvokeError(Error::Cast(result));
1009 UNREACHABLE(); 1003 UNREACHABLE();
1010 } 1004 }
1011 return result.raw(); 1005 return result.raw();
1012 } 1006 }
1007
siva 2013/09/25 20:17:57 extra blank line.
1013 return field.value(); 1008 return field.value();
1014 } 1009 }
1015 1010
1016 1011
1017 DEFINE_NATIVE_ENTRY(ClassMirror_invokeSetter, 4) { 1012 DEFINE_NATIVE_ENTRY(ClassMirror_invokeSetter, 4) {
1018 // Argument 0 is the mirror, which is unused by the native. It exists 1013 // Argument 0 is the mirror, which is unused by the native. It exists
1019 // because this native is an instance method in order to be polymorphic 1014 // because this native is an instance method in order to be polymorphic
1020 // with its cousins. 1015 // with its cousins.
1021 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1)); 1016 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1));
1022 const Class& klass = Class::Handle(ref.GetClassReferent()); 1017 const Class& klass = Class::Handle(ref.GetClassReferent());
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
1242 // getter Function. The getter function may either be in the 1237 // getter Function. The getter function may either be in the
1243 // library or in the field's owner class, depending. 1238 // library or in the field's owner class, depending.
1244 const Field& field = Field::Handle( 1239 const Field& field = Field::Handle(
1245 library.LookupFieldAllowPrivate(getter_name)); 1240 library.LookupFieldAllowPrivate(getter_name));
1246 Function& getter = Function::Handle(); 1241 Function& getter = Function::Handle();
1247 if (field.IsNull()) { 1242 if (field.IsNull()) {
1248 // No field found and no ambiguity error. Check for a getter in the lib. 1243 // No field found and no ambiguity error. Check for a getter in the lib.
1249 const String& internal_getter_name = 1244 const String& internal_getter_name =
1250 String::Handle(Field::GetterName(getter_name)); 1245 String::Handle(Field::GetterName(getter_name));
1251 getter = library.LookupFunctionAllowPrivate(internal_getter_name); 1246 getter = library.LookupFunctionAllowPrivate(internal_getter_name);
1247 if (getter.IsNull()) {
1248 getter = library.LookupFunctionAllowPrivate(getter_name);
1249 if (!getter.IsNull()) {
1250 // Looking for a getter but found a regular method: closurize.
1251 const Function& closure_function =
1252 Function::Handle(getter.ImplicitClosureFunction());
1253 return closure_function.ImplicitStaticClosure();
1254 }
1255 }
1252 } else if (!field.IsNull() && FieldIsUninitialized(field)) { 1256 } else if (!field.IsNull() && FieldIsUninitialized(field)) {
1253 // A field was found. Check for a getter in the field's owner classs. 1257 // A field was found. Check for a getter in the field's owner classs.
1254 const Class& klass = Class::Handle(field.owner()); 1258 const Class& klass = Class::Handle(field.owner());
1255 const String& internal_getter_name = 1259 const String& internal_getter_name =
1256 String::Handle(Field::GetterName(getter_name)); 1260 String::Handle(Field::GetterName(getter_name));
1257 getter = klass.LookupStaticFunctionAllowPrivate(internal_getter_name); 1261 getter = klass.LookupStaticFunctionAllowPrivate(internal_getter_name);
1258 } 1262 }
1259 1263
1260 if (!getter.IsNull() && getter.is_visible()) { 1264 if (!getter.IsNull() && getter.is_visible()) {
1261 // Invoke the getter and return the result. 1265 // Invoke the getter and return the result.
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
1416 } 1420 }
1417 1421
1418 1422
1419 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { 1423 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) {
1420 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); 1424 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0));
1421 const Field& field = Field::Handle(ref.GetFieldReferent()); 1425 const Field& field = Field::Handle(ref.GetFieldReferent());
1422 return field.type(); 1426 return field.type();
1423 } 1427 }
1424 1428
1425 } // namespace dart 1429 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/lib/mirrors_impl.dart » ('j') | runtime/vm/object.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698