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 bridged enums with source URIs for deduplication
  final enums = bridgedEnums();
  final enumSources = enumSourceUris();
  for (final enumDef in enums) {
    if (!include(enumSources[enumDef.name])) continue;
    interpreter.registerBridgedEnum(
      enumDef,
      importPath,
      sourceUri: enumSources[enumDef.name],
    );
  }

  // 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],
    );
  }

  // Register class aliases (typedef type aliases)
  final aliases = classAliases();
  for (final entry in aliases.entries) {
    // An alias follows the class it names; it has no source of its own.
    if (ownPrefix != null && !include(classSources[entry.value])) continue;
    interpreter.registerClassAlias(entry.key, entry.value, importPath);
  }

  // Register function typedefs for type resolution
  final typedefs = functionTypedefs();
  final typedefArity = functionTypedefArity();
  // Function typedefs carry no source; a sub-package barrel gets none.
  for (final name in typedefs) {
    if (ownPrefix != null) break;
    final arity = typedefArity[name];
    interpreter.registerFunctionTypedef(
      name,
      importPath,
      requiredPositional: arity?.required,
      maxPositional: arity?.max,
    );
  }

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