| OLD | NEW |
| 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 Loading... |
| 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 Loading... |
| 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( |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1242 // getter Function. The getter function may either be in the | 1236 // getter Function. The getter function may either be in the |
| 1243 // library or in the field's owner class, depending. | 1237 // library or in the field's owner class, depending. |
| 1244 const Field& field = Field::Handle( | 1238 const Field& field = Field::Handle( |
| 1245 library.LookupFieldAllowPrivate(getter_name)); | 1239 library.LookupFieldAllowPrivate(getter_name)); |
| 1246 Function& getter = Function::Handle(); | 1240 Function& getter = Function::Handle(); |
| 1247 if (field.IsNull()) { | 1241 if (field.IsNull()) { |
| 1248 // No field found and no ambiguity error. Check for a getter in the lib. | 1242 // No field found and no ambiguity error. Check for a getter in the lib. |
| 1249 const String& internal_getter_name = | 1243 const String& internal_getter_name = |
| 1250 String::Handle(Field::GetterName(getter_name)); | 1244 String::Handle(Field::GetterName(getter_name)); |
| 1251 getter = library.LookupFunctionAllowPrivate(internal_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 } |
| 1252 } else if (!field.IsNull() && FieldIsUninitialized(field)) { | 1255 } else if (!field.IsNull() && FieldIsUninitialized(field)) { |
| 1253 // A field was found. Check for a getter in the field's owner classs. | 1256 // A field was found. Check for a getter in the field's owner classs. |
| 1254 const Class& klass = Class::Handle(field.owner()); | 1257 const Class& klass = Class::Handle(field.owner()); |
| 1255 const String& internal_getter_name = | 1258 const String& internal_getter_name = |
| 1256 String::Handle(Field::GetterName(getter_name)); | 1259 String::Handle(Field::GetterName(getter_name)); |
| 1257 getter = klass.LookupStaticFunctionAllowPrivate(internal_getter_name); | 1260 getter = klass.LookupStaticFunctionAllowPrivate(internal_getter_name); |
| 1258 } | 1261 } |
| 1259 | 1262 |
| 1260 if (!getter.IsNull() && getter.is_visible()) { | 1263 if (!getter.IsNull() && getter.is_visible()) { |
| 1261 // Invoke the getter and return the result. | 1264 // Invoke the getter and return the result. |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1416 } | 1419 } |
| 1417 | 1420 |
| 1418 | 1421 |
| 1419 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { | 1422 DEFINE_NATIVE_ENTRY(VariableMirror_type, 1) { |
| 1420 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); | 1423 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); |
| 1421 const Field& field = Field::Handle(ref.GetFieldReferent()); | 1424 const Field& field = Field::Handle(ref.GetFieldReferent()); |
| 1422 return field.type(); | 1425 return field.type(); |
| 1423 } | 1426 } |
| 1424 | 1427 |
| 1425 } // namespace dart | 1428 } // namespace dart |
| OLD | NEW |