| 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 patch class Error { | 5 patch class Error { |
| 6 /* patch */ static String _objectToString(Object object) { | 6 /* patch */ static String _objectToString(Object object) { |
| 7 return Object._toString(object); | 7 return Object._toString(object); |
| 8 } | 8 } |
| 9 } | 9 } |
| 10 | 10 |
| 11 patch class NoSuchMethodError { | 11 patch class NoSuchMethodError { |
| 12 // The compiler emits a call to _throwNew when it cannot resolve a static | 12 // The compiler emits a call to _throwNew when it cannot resolve a static |
| 13 // method at compile time. The receiver is actually the literal class of the | 13 // method at compile time. The receiver is actually the literal class of the |
| 14 // unresolved method. | 14 // unresolved method. |
| 15 static void _throwNew(Object receiver, | 15 static void _throwNew(Object receiver, |
| 16 String memberName, | 16 String memberName, |
| 17 int invocation_type, |
| 17 List arguments, | 18 List arguments, |
| 18 List argumentNames, | 19 List argumentNames, |
| 19 List existingArgumentNames) { | 20 List existingArgumentNames) { |
| 20 int numNamedArguments = argumentNames == null ? 0 : argumentNames.length; | 21 int numNamedArguments = argumentNames == null ? 0 : argumentNames.length; |
| 21 int numPositionalArguments = arguments == null ? 0 : arguments.length; | 22 int numPositionalArguments = arguments == null ? 0 : arguments.length; |
| 22 numPositionalArguments -= numNamedArguments; | 23 numPositionalArguments -= numNamedArguments; |
| 23 List positionalArguments; | 24 List positionalArguments; |
| 24 if (numPositionalArguments == 0) { | 25 if (numPositionalArguments == 0) { |
| 25 positionalArguments = []; | 26 positionalArguments = []; |
| 26 } else { | 27 } else { |
| 27 positionalArguments = arguments.getRange(0, numPositionalArguments); | 28 positionalArguments = arguments.getRange(0, numPositionalArguments); |
| 28 } | 29 } |
| 29 Map<String, dynamic> namedArguments = new Map<String, dynamic>(); | 30 Map<String, dynamic> namedArguments = new Map<String, dynamic>(); |
| 30 for (int i = 0; i < numNamedArguments; i++) { | 31 for (int i = 0; i < numNamedArguments; i++) { |
| 31 var arg_value = arguments[numPositionalArguments + i]; | 32 var arg_value = arguments[numPositionalArguments + i]; |
| 32 namedArguments[argumentNames[i]] = arg_value; | 33 namedArguments[argumentNames[i]] = arg_value; |
| 33 } | 34 } |
| 34 throw new NoSuchMethodError(receiver, | 35 throw new NoSuchMethodError._withType(receiver, |
| 35 memberName, | 36 memberName, |
| 37 invocation_type, |
| 36 positionalArguments, | 38 positionalArguments, |
| 37 namedArguments, | 39 namedArguments, |
| 38 existingArgumentNames); | 40 existingArgumentNames); |
| 39 } | 41 } |
| 40 | 42 |
| 43 // Remember the type from the invocation mirror or static compilation |
| 44 // analysis when thrown directly with _throwNew. A negative value means |
| 45 // that no information is available. |
| 46 final int _invocation_type; |
| 47 |
| 41 const NoSuchMethodError(Object this._receiver, | 48 const NoSuchMethodError(Object this._receiver, |
| 42 String this._memberName, | 49 String this._memberName, |
| 43 List this._arguments, | 50 List this._arguments, |
| 44 Map<String,dynamic> this._namedArguments, | 51 Map<String,dynamic> this._namedArguments, |
| 45 [List existingArgumentNames = null]) | 52 [List existingArgumentNames = null]) |
| 53 : this._existingArgumentNames = existingArgumentNames, |
| 54 this._invocation_type = -1; |
| 55 |
| 56 const NoSuchMethodError._withType(Object this._receiver, |
| 57 String this._memberName, |
| 58 this._invocation_type, |
| 59 List this._arguments, |
| 60 Map<String,dynamic> this._namedArguments, |
| 61 [List existingArgumentNames = null]) |
| 46 : this._existingArgumentNames = existingArgumentNames; | 62 : this._existingArgumentNames = existingArgumentNames; |
| 47 | 63 |
| 64 |
| 65 String _developerMessage(args_mismatch) { |
| 66 if (_invocation_type < 0) { |
| 67 return ""; |
| 68 } |
| 69 var type = _invocation_type & _InvocationMirror._TYPE_MASK; |
| 70 var level = (_invocation_type >> _InvocationMirror._CALL_SHIFT) & |
| 71 _InvocationMirror._CALL_MASK; |
| 72 var type_str = |
| 73 (const ["method", "getter", "setter", "getter or setter"])[type]; |
| 74 var args_message = args_mismatch ? " with matching arguments" : ""; |
| 75 var msg; |
| 76 switch (level) { |
| 77 case _InvocationMirror._DYNAMIC: { |
| 78 if (_receiver == null) { |
| 79 msg = "The null object does not have a $type_str '$_memberName'" |
| 80 "$args_message."; |
| 81 } else { |
| 82 msg = "Class '${_receiver.runtimeType}' has no instance $type_str " |
| 83 "'$_memberName'$args_message."; |
| 84 } |
| 85 break; |
| 86 } |
| 87 case _InvocationMirror._STATIC: { |
| 88 msg = "No static $type_str '$_memberName' declared in class " |
| 89 "'$_receiver'."; |
| 90 break; |
| 91 } |
| 92 case _InvocationMirror._CONSTRUCTOR: { |
| 93 msg = "No constructor '$_memberName' declared in class '$_receiver'."; |
| 94 break; |
| 95 } |
| 96 case _InvocationMirror._TOP_LEVEL: { |
| 97 msg = "No top-level $type_str '$_memberName' declared."; |
| 98 break; |
| 99 } |
| 100 } |
| 101 return "$msg\n\n"; |
| 102 } |
| 103 |
| 48 /* patch */ String toString() { | 104 /* patch */ String toString() { |
| 49 StringBuffer actual_buf = new StringBuffer(); | 105 StringBuffer actual_buf = new StringBuffer(); |
| 50 int i = 0; | 106 int i = 0; |
| 51 if (_arguments != null) { | 107 if (_arguments != null) { |
| 52 for (; i < _arguments.length; i++) { | 108 for (; i < _arguments.length; i++) { |
| 53 if (i > 0) { | 109 if (i > 0) { |
| 54 actual_buf.add(", "); | 110 actual_buf.add(", "); |
| 55 } | 111 } |
| 56 actual_buf.add(Error.safeToString(_arguments[i])); | 112 actual_buf.add(Error.safeToString(_arguments[i])); |
| 57 } | 113 } |
| 58 } | 114 } |
| 59 if (_namedArguments != null) { | 115 if (_namedArguments != null) { |
| 60 _namedArguments.forEach((String key, var value) { | 116 _namedArguments.forEach((String key, var value) { |
| 61 if (i > 0) { | 117 if (i > 0) { |
| 62 actual_buf.add(", "); | 118 actual_buf.add(", "); |
| 63 } | 119 } |
| 64 actual_buf.add(key); | 120 actual_buf.add(key); |
| 65 actual_buf.add(": "); | 121 actual_buf.add(": "); |
| 66 actual_buf.add(Error.safeToString(value)); | 122 actual_buf.add(Error.safeToString(value)); |
| 67 i++; | 123 i++; |
| 68 }); | 124 }); |
| 69 } | 125 } |
| 70 StringBuffer msg_buf = new StringBuffer(); | 126 var args_mismatch = _existingArgumentNames != null; |
| 71 if (_existingArgumentNames == null) { | 127 StringBuffer msg_buf = new StringBuffer(_developerMessage(args_mismatch)); |
| 128 if (!args_mismatch) { |
| 72 msg_buf.add( | 129 msg_buf.add( |
| 73 "NoSuchMethodError : method not found: '$_memberName'\n" | 130 "NoSuchMethodError : method not found: '$_memberName'\n" |
| 74 "Receiver: ${Error.safeToString(_receiver)}\n" | 131 "Receiver: ${Error.safeToString(_receiver)}\n" |
| 75 "Arguments: [$actual_buf]"); | 132 "Arguments: [$actual_buf]"); |
| 76 } else { | 133 } else { |
| 77 String actualParameters = actual_buf.toString(); | 134 String actualParameters = actual_buf.toString(); |
| 78 StringBuffer formal_buf = new StringBuffer(); | 135 StringBuffer formal_buf = new StringBuffer(); |
| 79 for (int i = 0; i < _existingArgumentNames.length; i++) { | 136 for (int i = 0; i < _existingArgumentNames.length; i++) { |
| 80 if (i > 0) { | 137 if (i > 0) { |
| 81 formal_buf.add(", "); | 138 formal_buf.add(", "); |
| 82 } | 139 } |
| 83 formal_buf.add(_existingArgumentNames[i]); | 140 formal_buf.add(_existingArgumentNames[i]); |
| 84 } | 141 } |
| 85 String formalParameters = formal_buf.toString(); | 142 String formalParameters = formal_buf.toString(); |
| 86 msg_buf.add( | 143 msg_buf.add( |
| 87 "NoSuchMethodError: incorrect number of arguments passed to " | 144 "NoSuchMethodError: incorrect number of arguments passed to " |
| 88 "method named '$_memberName'\n" | 145 "method named '$_memberName'\n" |
| 89 "Receiver: ${Error.safeToString(_receiver)}\n" | 146 "Receiver: ${Error.safeToString(_receiver)}\n" |
| 90 "Tried calling: $_memberName($actualParameters)\n" | 147 "Tried calling: $_memberName($actualParameters)\n" |
| 91 "Found: $_memberName($formalParameters)"); | 148 "Found: $_memberName($formalParameters)"); |
| 92 } | 149 } |
| 93 return msg_buf.toString(); | 150 return msg_buf.toString(); |
| 94 } | 151 } |
| 95 } | 152 } |
| OLD | NEW |