Skip to content

Commit 18ff5ce

Browse files
rakudramacommit-bot@chromium.org
authored andcommitted
[dart2js] new-rti: Implement general As/Check methods
TBR=fishythefish@google.com Change-Id: Ib2ac75dd3787401129a8b7420c60d59013d80f21 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/107824 Reviewed-by: Stephen Adams <sra@google.com> Commit-Queue: Stephen Adams <sra@google.com>
1 parent 90c88d9 commit 18ff5ce

File tree

1 file changed

+22
-5
lines changed
  • sdk/lib/_internal/js_runtime/lib

1 file changed

+22
-5
lines changed

sdk/lib/_internal/js_runtime/lib/rti.dart

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
364381
class _TypeError extends Error implements TypeError {
365382
final String message;
366383
_TypeError.fromMessage(this.message);

0 commit comments

Comments
 (0)