isSubtypeOf method
Whether this class is a subtype of other: other itself, or a type
reached through extends, with or implements, at any depth.
GitHub #7: one walk over the whole interpreted supertype graph —
superclass, mixins and interfaces — each class visited once, so a
cyclic graph (Dart rejects one; this interpreter does not) ends. Against a
bridged other, every bridged supertype a visited class names
(bridgedSuperclass, bridgedMixins, bridgedInterfaces) is asked
BridgedClass.isSubtypeOf, which knows native identity and the
registered hierarchy. The walk used to stop at the superclass chain and
its mixins, so class C implements I with abstract class I implements Shape was not a Shape; and it accepted a bridged supertype whose NAME
matched, so a class implementing one library's Token passed for an
unrelated Token bridged over another native type. Names are never
compared.
Implementation
@override
bool isSubtypeOf(RuntimeType other, {Object? value}) {
if (other is! InterpretedClass && other is! BridgedClass) return false;
final visited = HashSet<InterpretedClass>.identity();
final pending = <InterpretedClass>[this];
while (pending.isNotEmpty) {
final klass = pending.removeLast();
if (!visited.add(klass)) continue;
if (identical(klass, other)) return true;
if (other is BridgedClass) {
for (final bridged in [
?klass.bridgedSuperclass,
...klass.bridgedMixins,
...klass.bridgedInterfaces,
]) {
if (identical(bridged, other) || bridged.isSubtypeOf(other)) {
return true;
}
}
}
pending
..addAll(klass.interfaces)
..addAll(klass.mixins);
if (klass.superclass case final superclass?) pending.add(superclass);
}
return false;
}