toBridgedClass method
Implementation
BridgedClass toBridgedClass(Type nativeType) {
// Resolution is split into TWO chain walks so that a PRECISE match in a
// far (enclosing) frame always beats a FUZZY name-prefix match in a near
// frame. Before this split the loop tried every strategy — including the
// loose G-DCLI-05 `startsWith` prefix fallback — within a single frame and
// returned on the first hit, so a nearer frame's fuzzy match short-circuited
// a more-correct precise match still waiting in an enclosing frame.
//
// Concrete failure (lazy-bridge substrate, step #17/#19): a
// `MappedListIterable<…>` returned by `List.map(...).toList()` has its
// precise `nativeNames` entry on the stdlib `Iterable` bridge, which (under
// the warm-parent / per-module split) lives in an *enclosing* frame. A
// nearer module frame carries the `Map` bridge but not `Iterable`, and
// `"MappedListIterable".startsWith("Map")` made the fuzzy fallback wrap it
// as `Map` — so `.toList()` failed. Walking all frames for precise matches
// first, then all frames for the fuzzy fallback, resolves it to `Iterable`.
// Mirror of tom_d4rt/lib/src/environment.dart.
final String nativeTypeNameFull = nativeType.toString();
// PASS A — precise matching across the whole scope chain: exact Type
// lookup, `_FooImpl → Foo` canonicalization, generic-base `name`/
// `nativeNames` match, suffix match, name-exact, and longest-nativeName
// prefix. Every strategy here is anchored on a declared bridge identity.
Environment? current = this;
while (current != null) {
BridgedClass? bridgedClass =
current._bridgedClassesLookupByType[nativeType];
String nativeTypeName = nativeTypeNameFull;
if (bridgedClass == null && (nativeTypeName.substring(0, 1) == '_')) {
if (nativeTypeName.endsWith('Impl')) {
nativeTypeName = nativeTypeName.substringBeforeLast('Impl');
}
// Cluster HASHSET FIX: when matching nativeNames by prefix, choose the
// LONGEST matching nativeName so a more-specific bridge (e.g. Iterator
// with `_HashSetIterator` in nativeNames) wins over a less-specific
// bridge whose nativeName is a shorter prefix (e.g. Set's `_HashSet`).
// Mirror of tom_d4rt/lib/src/environment.dart.
final cleanedName = nativeTypeName.substring(1).substringBefore('<');
bridgedClass = current._bridgedClassesLookupByType.entries
.firstWhereOrNull((e) => e.value.name == cleanedName)
?.value;
bridgedClass ??= _longestNativeNamePrefixMatch(current, nativeTypeName);
} else if (bridgedClass == null && nativeTypeName.contains('<')) {
// Extract the base type name before '<' for accurate matching.
// Using contains() was too broad — e.g., 'ListMapView<int>'.contains('View<')
// would falsely match the Flutter View widget bridge.
final baseTypeName = nativeTypeName.substring(
0,
nativeTypeName.indexOf('<'),
);
bridgedClass = current._bridgedClassesLookupByType.entries
.firstWhereOrNull(
(e) =>
baseTypeName == e.value.name ||
(e.value.nativeNames?.contains(baseTypeName) ?? false),
)
?.value;
// Suffix match fallback: e.g., CastList → List, ListIterator → Iterator
// Handles types that embed the bridge name as a suffix.
bridgedClass ??= current._bridgedClassesLookupByType.entries
.firstWhereOrNull(
(e) =>
e.value.name.length >= 3 &&
baseTypeName.endsWith(e.value.name) &&
baseTypeName.length > e.value.name.length,
)
?.value;
}
bridgedClass ??= current._bridgedClassesLookupByType.entries
.firstWhereOrNull((e) => e.value.name == nativeTypeName)
?.value;
// Cluster HASHSET FIX: pick the LONGEST nativeName prefix so a
// specific bridge wins over a less-specific bridge whose nativeName
// is a shorter prefix.
bridgedClass ??= _longestNativeNamePrefixMatch(current, nativeTypeName);
if (bridgedClass != null) {
return bridgedClass;
}
current = current._enclosing;
}
// PASS B — fuzzy fallback across the whole scope chain. G-DCLI-05 FIX:
// handle non-underscore implementation types like `ProgressBothImpl`, where
// a registered bridge name (`Progress`) is a prefix of the native type
// name. Runs only after every frame failed PASS A, so a precise match
// anywhere in the chain wins over this loose prefix match (see the method
// header for the `MappedListIterable` → `Map` false-positive this guards).
current = this;
while (current != null) {
final bridgedClass = current._bridgedClassesLookupByType.entries
.firstWhereOrNull((e) {
final bridgeName = e.value.name;
// Only match if the bridge name is a substantial prefix (>= 3 chars)
// and the native type name starts with it followed by more chars.
return bridgeName.length >= 3 &&
nativeTypeNameFull.startsWith(bridgeName) &&
nativeTypeNameFull.length > bridgeName.length;
})
?.value;
if (bridgedClass != null) {
Logger.debug(
"[Environment] Matched native type '$nativeTypeNameFull' to bridge '${bridgedClass.name}' via prefix matching",
);
return bridgedClass;
}
current = current._enclosing;
}
throw RuntimeD4rtException(
'Cannot bridge native object: No registered bridged class found for native type $nativeType.',
);
}