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

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

Issue 112193002: VM: Make ObjectMirror.invoke support getters returning functions. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: sync Created 7 years 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 | tests/lib/lib.status » ('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/compiler.h" 8 #include "vm/compiler.h"
9 #include "vm/dart_entry.h" 9 #include "vm/dart_entry.h"
10 #include "vm/exceptions.h" 10 #include "vm/exceptions.h"
(...skipping 1308 matching lines...) Expand 10 before | Expand all | Expand 10 after
1319 GET_NON_NULL_NATIVE_ARGUMENT(Array, args, arguments->NativeArgAt(3)); 1319 GET_NON_NULL_NATIVE_ARGUMENT(Array, args, arguments->NativeArgAt(3));
1320 GET_NON_NULL_NATIVE_ARGUMENT(Array, arg_names, arguments->NativeArgAt(4)); 1320 GET_NON_NULL_NATIVE_ARGUMENT(Array, arg_names, arguments->NativeArgAt(4));
1321 1321
1322 Class& klass = Class::Handle(reflectee.clazz()); 1322 Class& klass = Class::Handle(reflectee.clazz());
1323 Function& function = Function::Handle( 1323 Function& function = Function::Handle(
1324 Resolver::ResolveDynamicAnyArgs(klass, function_name)); 1324 Resolver::ResolveDynamicAnyArgs(klass, function_name));
1325 1325
1326 const Array& args_descriptor = 1326 const Array& args_descriptor =
1327 Array::Handle(ArgumentsDescriptor::New(args.Length(), arg_names)); 1327 Array::Handle(ArgumentsDescriptor::New(args.Length(), arg_names));
1328 1328
1329 if (function.IsNull()) {
1330 // Didn't find a method: try to find a getter and invoke call on its result.
1331 const String& getter_name =
1332 String::Handle(Field::GetterName(function_name));
1333 function = Resolver::ResolveDynamicAnyArgs(klass, getter_name);
1334 if (!function.IsNull()) {
1335 ASSERT(function.kind() != RawFunction::kMethodExtractor);
1336 // Invoke the getter.
1337 const int kNumArgs = 1;
1338 const Array& getter_args = Array::Handle(Array::New(kNumArgs));
1339 getter_args.SetAt(0, reflectee);
1340 const Array& getter_args_descriptor =
1341 Array::Handle(ArgumentsDescriptor::New(getter_args.Length()));
1342 const Instance& getter_result = Instance::Handle(
1343 InvokeDynamicFunction(reflectee,
1344 function,
1345 getter_name,
1346 getter_args,
1347 getter_args_descriptor));
1348 // Replace the closure as the receiver in the arguments list.
1349 args.SetAt(0, getter_result);
1350 // Call the closure.
1351 const Object& call_result =
1352 Object::Handle(DartEntry::InvokeClosure(args, args_descriptor));
1353 if (call_result.IsError()) {
1354 ThrowInvokeError(Error::Cast(call_result));
1355 UNREACHABLE();
1356 }
1357 return call_result.raw();
1358 }
1359 }
1360
1361 // Found an ordinary method.
1329 return InvokeDynamicFunction(reflectee, 1362 return InvokeDynamicFunction(reflectee,
1330 function, 1363 function,
1331 function_name, 1364 function_name,
1332 args, 1365 args,
1333 args_descriptor); 1366 args_descriptor);
1334 } 1367 }
1335 1368
1336 1369
1337 DEFINE_NATIVE_ENTRY(InstanceMirror_invokeGetter, 3) { 1370 DEFINE_NATIVE_ENTRY(InstanceMirror_invokeGetter, 3) {
1338 // Argument 0 is the mirror, which is unused by the native. It exists 1371 // Argument 0 is the mirror, which is unused by the native. It exists
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
1501 // Argument 0 is the mirror, which is unused by the native. It exists 1534 // Argument 0 is the mirror, which is unused by the native. It exists
1502 // because this native is an instance method in order to be polymorphic 1535 // because this native is an instance method in order to be polymorphic
1503 // with its cousins. 1536 // with its cousins.
1504 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1)); 1537 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1));
1505 const Class& klass = Class::Handle(ref.GetClassReferent()); 1538 const Class& klass = Class::Handle(ref.GetClassReferent());
1506 GET_NON_NULL_NATIVE_ARGUMENT( 1539 GET_NON_NULL_NATIVE_ARGUMENT(
1507 String, function_name, arguments->NativeArgAt(2)); 1540 String, function_name, arguments->NativeArgAt(2));
1508 GET_NON_NULL_NATIVE_ARGUMENT(Array, args, arguments->NativeArgAt(3)); 1541 GET_NON_NULL_NATIVE_ARGUMENT(Array, args, arguments->NativeArgAt(3));
1509 GET_NON_NULL_NATIVE_ARGUMENT(Array, arg_names, arguments->NativeArgAt(4)); 1542 GET_NON_NULL_NATIVE_ARGUMENT(Array, arg_names, arguments->NativeArgAt(4));
1510 1543
1544 Function& function = Function::Handle(
1545 klass.LookupStaticFunction(function_name));
1546
1547 if (function.IsNull()) {
1548 // Didn't find a method: try to find a getter and invoke call on its result.
1549 const String& getter_name =
1550 String::Handle(Field::GetterName(function_name));
1551 function = klass.LookupStaticFunction(getter_name);
1552 if (!function.IsNull()) {
1553 // Invoke the getter.
1554 const Object& getter_result = Object::Handle(
1555 DartEntry::InvokeFunction(function, Object::empty_array()));
1556 if (getter_result.IsError()) {
1557 ThrowInvokeError(Error::Cast(getter_result));
1558 UNREACHABLE();
1559 }
1560 // Make room for the closure (receiver) in the argument list.
1561 int numArgs = args.Length();
1562 const Array& call_args = Array::Handle(Array::New(numArgs + 1));
1563 Object& temp = Object::Handle();
1564 for (int i = 0; i < numArgs; i++) {
1565 temp = args.At(i);
1566 call_args.SetAt(i + 1, temp);
1567 }
1568 call_args.SetAt(0, getter_result);
1569 const Array& call_args_descriptor_array =
1570 Array::Handle(ArgumentsDescriptor::New(call_args.Length(), arg_names));
1571 // Call the closure.
1572 const Object& call_result = Object::Handle(
1573 DartEntry::InvokeClosure(call_args, call_args_descriptor_array));
1574 if (call_result.IsError()) {
1575 ThrowInvokeError(Error::Cast(call_result));
1576 UNREACHABLE();
1577 }
1578 return call_result.raw();
1579 }
1580 }
1581
1511 const Array& args_descriptor_array = 1582 const Array& args_descriptor_array =
1512 Array::Handle(ArgumentsDescriptor::New(args.Length(), arg_names)); 1583 Array::Handle(ArgumentsDescriptor::New(args.Length(), arg_names));
1513 1584
1514 const Function& function = Function::Handle( 1585 ArgumentsDescriptor args_descriptor(args_descriptor_array);
1515 klass.LookupStaticFunction(function_name));
1516 1586
1517 ArgumentsDescriptor args_descriptor(args_descriptor_array);
1518 if (function.IsNull() || 1587 if (function.IsNull() ||
1519 !function.AreValidArguments(args_descriptor, NULL) || 1588 !function.AreValidArguments(args_descriptor, NULL) ||
1520 !function.is_visible()) { 1589 !function.is_visible()) {
1521 ThrowNoSuchMethod(AbstractType::Handle(klass.RareType()), 1590 ThrowNoSuchMethod(AbstractType::Handle(klass.RareType()),
1522 function_name, 1591 function_name,
1523 function, 1592 function,
1524 InvocationMirror::kStatic, 1593 InvocationMirror::kStatic,
1525 InvocationMirror::kMethod); 1594 InvocationMirror::kMethod);
1526 UNREACHABLE(); 1595 UNREACHABLE();
1527 } 1596 }
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
1751 // Argument 0 is the mirror, which is unused by the native. It exists 1820 // Argument 0 is the mirror, which is unused by the native. It exists
1752 // because this native is an instance method in order to be polymorphic 1821 // because this native is an instance method in order to be polymorphic
1753 // with its cousins. 1822 // with its cousins.
1754 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1)); 1823 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(1));
1755 const Library& library = Library::Handle(ref.GetLibraryReferent()); 1824 const Library& library = Library::Handle(ref.GetLibraryReferent());
1756 GET_NON_NULL_NATIVE_ARGUMENT( 1825 GET_NON_NULL_NATIVE_ARGUMENT(
1757 String, function_name, arguments->NativeArgAt(2)); 1826 String, function_name, arguments->NativeArgAt(2));
1758 GET_NON_NULL_NATIVE_ARGUMENT(Array, args, arguments->NativeArgAt(3)); 1827 GET_NON_NULL_NATIVE_ARGUMENT(Array, args, arguments->NativeArgAt(3));
1759 GET_NON_NULL_NATIVE_ARGUMENT(Array, arg_names, arguments->NativeArgAt(4)); 1828 GET_NON_NULL_NATIVE_ARGUMENT(Array, arg_names, arguments->NativeArgAt(4));
1760 1829
1830 Function& function = Function::Handle(
1831 library.LookupLocalFunction(function_name));
1832
1833 if (function.IsNull()) {
1834 // Didn't find a method: try to find a getter and invoke call on its result.
1835 const String& getter_name =
1836 String::Handle(Field::GetterName(function_name));
1837 function = library.LookupLocalFunction(getter_name);
1838 if (!function.IsNull()) {
1839 // Invoke getter.
1840 const Object& getter_result = Object::Handle(
1841 DartEntry::InvokeFunction(function, Object::empty_array()));
1842 if (getter_result.IsError()) {
1843 ThrowInvokeError(Error::Cast(getter_result));
1844 UNREACHABLE();
1845 }
1846 // Make room for the closure (receiver) in arguments.
1847 int numArgs = args.Length();
1848 const Array& call_args = Array::Handle(Array::New(numArgs + 1));
1849 Object& temp = Object::Handle();
1850 for (int i = 0; i < numArgs; i++) {
1851 temp = args.At(i);
1852 call_args.SetAt(i + 1, temp);
1853 }
1854 call_args.SetAt(0, getter_result);
1855 const Array& call_args_descriptor_array =
1856 Array::Handle(ArgumentsDescriptor::New(call_args.Length(), arg_names));
1857 // Call closure.
1858 const Object& call_result = Object::Handle(
1859 DartEntry::InvokeClosure(call_args, call_args_descriptor_array));
1860 if (call_result.IsError()) {
1861 ThrowInvokeError(Error::Cast(call_result));
1862 UNREACHABLE();
1863 }
1864 return call_result.raw();
1865 }
1866 }
1867
1761 const Array& args_descriptor_array = 1868 const Array& args_descriptor_array =
1762 Array::Handle(ArgumentsDescriptor::New(args.Length(), arg_names)); 1869 Array::Handle(ArgumentsDescriptor::New(args.Length(), arg_names));
1870 ArgumentsDescriptor args_descriptor(args_descriptor_array);
1763 1871
1764 const Function& function = Function::Handle(
1765 library.LookupLocalFunction(function_name));
1766
1767 ArgumentsDescriptor args_descriptor(args_descriptor_array);
1768 if (function.IsNull() || 1872 if (function.IsNull() ||
1769 !function.AreValidArguments(args_descriptor, NULL) || 1873 !function.AreValidArguments(args_descriptor, NULL) ||
1770 !function.is_visible()) { 1874 !function.is_visible()) {
1771 ThrowNoSuchMethod(Instance::null_instance(), 1875 ThrowNoSuchMethod(Instance::null_instance(),
1772 function_name, 1876 function_name,
1773 function, 1877 function,
1774 InvocationMirror::kTopLevel, 1878 InvocationMirror::kTopLevel,
1775 InvocationMirror::kMethod); 1879 InvocationMirror::kMethod);
1776 UNREACHABLE(); 1880 UNREACHABLE();
1777 } 1881 }
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
1945 2049
1946 DEFINE_NATIVE_ENTRY(VariableMirror_type, 2) { 2050 DEFINE_NATIVE_ENTRY(VariableMirror_type, 2) {
1947 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0)); 2051 GET_NON_NULL_NATIVE_ARGUMENT(MirrorReference, ref, arguments->NativeArgAt(0));
1948 const Field& field = Field::Handle(ref.GetFieldReferent()); 2052 const Field& field = Field::Handle(ref.GetFieldReferent());
1949 GET_NATIVE_ARGUMENT(AbstractType, instantiator, arguments->NativeArgAt(1)); 2053 GET_NATIVE_ARGUMENT(AbstractType, instantiator, arguments->NativeArgAt(1));
1950 const AbstractType& type = AbstractType::Handle(field.type()); 2054 const AbstractType& type = AbstractType::Handle(field.type());
1951 return InstantiateType(type, instantiator); 2055 return InstantiateType(type, instantiator);
1952 } 2056 }
1953 2057
1954 } // namespace dart 2058 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | tests/lib/lib.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698