registerBridges static method

void registerBridges(
  1. D4rt interpreter,
  2. String importPath, {
  3. bool onlyOwnPackage = false,
})

Registers all bridges with an interpreter.

importPath is the package import path that D4rt scripts will use to access these classes (e.g., 'package:my_package/my_package.dart').

With onlyOwnPackage (a sub-package barrel, subPackageBarrels), only the declarations whose source is in importPath's package are registered: a script importing that barrel sees what the package declares, not the whole module. (Without it, a module bridging one package:characters extension would put all of its classes under package:characters/characters.dart, which widgets.dart re-exports.)

Implementation

static void registerBridges(
  D4rt interpreter,
  String importPath, {
  bool onlyOwnPackage = false,
}) {
  final String? ownPrefix = onlyOwnPackage
      ? 'package:${importPath.substring('package:'.length).split('/').first}/'
      : null;
  bool include(String? sourceUri) =>
      ownPrefix == null ||
      (sourceUri != null && sourceUri.startsWith(ownPrefix));
  // Step #17 — register deferred factory thunks (not pre-built
  // BridgedClass objects): a script touching N of the M classes
  // materializes ≈N (each thunk builds its class on first resolve).
  final classThunks = bridgeClassThunks();
  final classTypes = bridgeClassTypes();
  final classSources = classSourceUris();
  for (final entry in classThunks.entries) {
    if (!include(classSources[entry.key])) continue;
    interpreter.registerBridgedClassLazy(
      entry.key,
      classTypes[entry.key]!,
      entry.value,
      importPath,
      sourceUri: classSources[entry.key],
    );
  }

  // Register the flattened native supertype table so
  // interpreted subclasses pass subtype checks against bridged
  // ancestors. Idempotent — safe to call per barrel.
  BridgedClass.registerSupertypes(classSupertypes());

  // Register global variables
  registerGlobalVariables(interpreter, importPath, include: include);

  // Register global functions with source URIs for deduplication
  final funcs = globalFunctions();
  final funcSources = globalFunctionSourceUris();
  final funcSigs = globalFunctionSignatures();
  for (final entry in funcs.entries) {
    if (!include(funcSources[entry.key])) continue;
    interpreter.registertopLevelFunction(
      entry.key,
      entry.value,
      importPath,
      sourceUri: funcSources[entry.key],
      signature: funcSigs[entry.key],
    );
  }

  // GEN-107: Register library re-exports
  for (final r in bridgeReExports()) {
    interpreter.registerLibraryReExport(
      r.source,
      r.target,
      show: r.show,
      hide: r.hide,
    );
  }
}