finalizeBridges method
Runs every extension callback registered via registerExtensions in registration order, then marks the runner as finalized.
execute / eval invoke this implicitly on first run if it has
not been called already, so embedders don't have to remember to
call it. Calling it explicitly first is supported and preferred
when an embedder needs deterministic timing (e.g. a constructor
of a Flutter helper that touches bridges before the first script
runs).
Idempotent per instance: repeat calls on the same interpreter return without re-running any callback — the contract is "run once, then frozen". After this call returns, registerExtensions throws StateError.
Step 11 — once per package per process. Extension callbacks live
on the process-global pooled bundles (_packagePool). A callback is
fired only the first time any interpreter finalizes the package,
guarded by _PackageBridgeBundle.extensionFired. A second interpreter
granted the same (already-pooled) package — e.g. via the canonical
if (providePackage(name) == false) { register…; registerExtensions… }
idiom, where the second instance skips both — never re-fires it.
Because the callback's effects (relaxers, interface proxies, generic
constructors) land in static D4 / BridgedClass registries shared
across instances, firing once is correct and sufficient.
Implementation
void finalizeBridges() {
if (_bridgesFinalized) return;
_bridgesFinalized = true;
// Callbacks live on the pooled bundles; fire this instance's registered
// packages in registration order, but only those not yet fired anywhere
// in the process (step 11 once-per-package-per-process guard).
for (final packageName in _extensionPackages) {
final bundle = _packagePool[packageName];
final callback = bundle?.extensionCallback;
if (callback == null) continue;
if (bundle!.extensionFired) {
Logger.debugLazy(
() =>
'[D4rt.finalizeBridges] Extensions for "$packageName" already '
'fired in this process — skipping.',
);
continue;
}
bundle.extensionFired = true;
Logger.debug(
'[D4rt.finalizeBridges] Running extensions for "$packageName"',
);
callback();
}
_maybeEnableUsageLogFromEnv();
}