@@ -335,21 +335,30 @@ bool _generalIsTestImplementation(object) {
335335
336336/// Called from generated code.
337337_generalAsCheckImplementation (object) {
338+ if (object == null ) return object;
338339 // This static method is installed on an Rti object as a JavaScript instance
339340 // method. The Rti object is 'this'.
340341 Rti testRti = _castToRti (JS ('' , 'this' ));
341- throw UnimplementedError (
342- '${Error .safeToString (object )} as ${_rtiToString (testRti , null )}' );
342+ Rti objectRti = instanceType (object);
343+ if (isSubtype (_theUniverse (), objectRti, testRti)) return object;
344+ var message = "${Error .safeToString (object )}:"
345+ " type '${_rtiToString (objectRti , null )}'"
346+ " is not a subtype of type '${_rtiToString (testRti , null )}'" ;
347+ throw new _CastError .fromMessage ('CastError: $message ' );
343348}
344349
345350/// Called from generated code.
346351_generalTypeCheckImplementation (object) {
352+ if (object == null ) return object;
347353 // This static method is installed on an Rti object as a JavaScript instance
348354 // method. The Rti object is 'this'.
349355 Rti testRti = _castToRti (JS ('' , 'this' ));
350- throw UnimplementedError (
351- '${Error .safeToString (object )} as ${_rtiToString (testRti , null )}'
352- ' (TypeError)' );
356+ Rti objectRti = instanceType (object);
357+ if (isSubtype (_theUniverse (), objectRti, testRti)) return object;
358+ var message = "${Error .safeToString (object )}:"
359+ " type '${_rtiToString (objectRti , null )}'"
360+ " is not a subtype of type '${_rtiToString (testRti , null )}'" ;
361+ throw new _TypeError .fromMessage ('TypeError: $message ' );
353362}
354363
355364/// Called from generated code.
@@ -361,6 +370,14 @@ checkTypeBound(Rti type, Rti bound, variable) {
361370 throw _TypeError .fromMessage ('TypeError: $message ' );
362371}
363372
373+ class _CastError extends Error implements CastError {
374+ final String message;
375+ _CastError .fromMessage (this .message);
376+
377+ @override
378+ String toString () => message;
379+ }
380+
364381class _TypeError extends Error implements TypeError {
365382 final String message;
366383 _TypeError .fromMessage (this .message);
0 commit comments