mirror of
https://github.com/dart-lang/sdk
synced 2024-09-05 00:13:50 +00:00
[dart2wasm] Implement NoSuchMethodError
.
Change-Id: I1a799255f93449fef011cb0bc9b92cd63f3437c7 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/250791 Reviewed-by: Aske Simon Christensen <askesc@google.com> Commit-Queue: Joshua Litt <joshualitt@google.com>
This commit is contained in:
parent
7c8981f627
commit
e1933cd4a0
|
@ -176,6 +176,9 @@ var dart2wasm = {
|
|||
getTimeZoneOffsetInSeconds: function(secondsSinceEpoch) {
|
||||
return new Date(secondsSinceEpoch * 1000).getTimezoneOffset() * 60;
|
||||
},
|
||||
jsonEncode: function(s) {
|
||||
return stringToDartString(JSON.stringify(stringFromDartString(s)));
|
||||
},
|
||||
};
|
||||
|
||||
function instantiate(filename, imports) {
|
||||
|
|
|
@ -15,6 +15,7 @@ import "dart:_internal"
|
|||
EfficientLengthIterable,
|
||||
FixedLengthListMixin,
|
||||
IterableElementError,
|
||||
jsonEncode,
|
||||
ListIterator,
|
||||
Lists,
|
||||
mix64,
|
||||
|
@ -29,6 +30,8 @@ import "dart:_internal"
|
|||
writeIntoOneByteString,
|
||||
writeIntoTwoByteString;
|
||||
|
||||
import "dart:_internal" as _internal show Symbol;
|
||||
|
||||
import "dart:collection"
|
||||
show
|
||||
HashMap,
|
||||
|
@ -50,3 +53,6 @@ import "dart:typed_data"
|
|||
import 'dart:wasm';
|
||||
|
||||
typedef _Smi = int; // For compatibility with VM patch files
|
||||
|
||||
String _symbolToString(Symbol s) =>
|
||||
_internal.Symbol.getName(s as _internal.Symbol);
|
||||
|
|
|
@ -8,13 +8,12 @@ external Never _throwObjectWithStackTrace(Object error, StackTrace stacktrace);
|
|||
class Error {
|
||||
@patch
|
||||
static String _objectToString(Object object) {
|
||||
return object.toString();
|
||||
return 'Instance of ${object._runtimeType}';
|
||||
}
|
||||
|
||||
@patch
|
||||
static String _stringToSafeString(String string) {
|
||||
// TODO(joshualitt): JSON encode string.
|
||||
return string;
|
||||
return jsonEncode(string);
|
||||
}
|
||||
|
||||
@patch
|
||||
|
@ -82,6 +81,71 @@ class _TypeError extends _Error implements TypeError {
|
|||
}
|
||||
}
|
||||
|
||||
@patch
|
||||
class NoSuchMethodError {
|
||||
final Object? _receiver;
|
||||
final Symbol _memberName;
|
||||
final List? _arguments;
|
||||
final Map<Symbol, dynamic>? _namedArguments;
|
||||
final List? _existingArgumentNames;
|
||||
|
||||
@patch
|
||||
factory NoSuchMethodError.withInvocation(
|
||||
Object? receiver, Invocation invocation) =>
|
||||
NoSuchMethodError(receiver, invocation.memberName,
|
||||
invocation.positionalArguments, invocation.namedArguments);
|
||||
|
||||
@patch
|
||||
NoSuchMethodError(Object? receiver, Symbol memberName,
|
||||
List? positionalArguments, Map<Symbol, dynamic>? namedArguments,
|
||||
[List? existingArgumentNames = null])
|
||||
: _receiver = receiver,
|
||||
_memberName = memberName,
|
||||
_arguments = positionalArguments,
|
||||
_namedArguments = namedArguments,
|
||||
_existingArgumentNames = existingArgumentNames;
|
||||
|
||||
@patch
|
||||
String toString() {
|
||||
StringBuffer sb = StringBuffer('');
|
||||
String comma = '';
|
||||
List? arguments = _arguments;
|
||||
if (arguments != null) {
|
||||
for (var argument in arguments) {
|
||||
sb.write(comma);
|
||||
sb.write(Error.safeToString(argument));
|
||||
comma = ', ';
|
||||
}
|
||||
}
|
||||
Map<Symbol, dynamic>? namedArguments = _namedArguments;
|
||||
if (namedArguments != null) {
|
||||
namedArguments.forEach((Symbol key, var value) {
|
||||
sb.write(comma);
|
||||
sb.write(_symbolToString(key));
|
||||
sb.write(": ");
|
||||
sb.write(Error.safeToString(value));
|
||||
comma = ', ';
|
||||
});
|
||||
}
|
||||
String memberName = _symbolToString(_memberName);
|
||||
String receiverText = Error.safeToString(_receiver);
|
||||
String actualParameters = '$sb';
|
||||
List? existingArgumentNames = _existingArgumentNames;
|
||||
if (existingArgumentNames == null) {
|
||||
return "NoSuchMethodError: method not found: '$memberName'\n"
|
||||
"Receiver: ${receiverText}\n"
|
||||
"Arguments: [$actualParameters]";
|
||||
} else {
|
||||
String formalParameters = existingArgumentNames.join(', ');
|
||||
return "NoSuchMethodError: incorrect number of arguments passed to "
|
||||
"method named '$memberName'\n"
|
||||
"Receiver: ${receiverText}\n"
|
||||
"Tried calling: $memberName($actualParameters)\n"
|
||||
"Found: $memberName($formalParameters)";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Used by Fasta to report a runtime error when a final field with an
|
||||
/// initializer is also initialized in a generative constructor.
|
||||
///
|
||||
|
|
|
@ -140,3 +140,6 @@ dynamic _callClosure2(dynamic closure, dynamic arg1, dynamic arg2) {
|
|||
// Schedule a callback from JS via setTimeout.
|
||||
@pragma("wasm:import", "dart2wasm.scheduleCallback")
|
||||
external void scheduleCallback(double millis, dynamic Function() callback);
|
||||
|
||||
@pragma("wasm:import", "dart2wasm.jsonEncode")
|
||||
external String jsonEncode(String object);
|
||||
|
|
Loading…
Reference in a new issue