defineBridgeLazy method
Registers a bridged class as a deferred thunk keyed by name, plus a
parallel Type → thunk entry under nativeType for native-object
resolution. The BridgedClass is built (and memoized) only when name
or nativeType is first resolved — the substrate the generator's lazy
emission plugs into (import-optimization plan step #17).
In the common no-collision case the thunk is stored without building. A
genuine same-name collision (another bridge / enum / value already holds
name) resolves the new thunk eagerly so the displaced bridge can be
preserved for static/constructor fallback and the shadow list shares the
same instance the memo will serve. Collisions are rare, so this does not
regress the common lazy path.
Implementation
void defineBridgeLazy(
String name, Type nativeType, BridgedClass Function() thunk) {
final collides = _values.containsKey(name) ||
(_bridgedClassesRaw?.containsKey(name) ?? false) ||
_bridgedEnums.containsKey(name);
if (collides) {
// CHECK: Also collides with bridged enums / values.
Logger.warn(
"Redefining bridged class or colliding with existing definition: $name",
);
// Preserve a displaced same-name bridge so static/constructor resolution
// can fall back to it (two packages exporting an identically named class).
final built = thunk();
_recordShadowedBridge(name, _bridgedClassesRaw?[name], built);
thunk = () => built;
}
_bridgedClassesOrNew.putThunk(name, thunk);
_bridgedClassesLookupByTypeOrNew.putThunk(nativeType, thunk);
_invalidateResolutionCache();
Logger.debugLazy(() => "[Environment] Defined bridge for class: $name");
}