executeBundle method

dynamic executeBundle(
  1. AstBundle bundle, {
  2. String? entryPoint,
  3. String name = 'main',
  4. List<Object?>? positionalArgs,
  5. Map<String, Object?>? namedArgs,
})

Execute an AstBundle with full import resolution.

Unlike execute, this method supports cross-module imports by creating an AstModuleLoader that resolves imports from the bundle's module map.

The entry point module is determined by:

  1. The entryPoint parameter (if provided)
  2. The bundle's AstBundle.entryPointUri

Example

final runner = D4rtRunner();
// register bridges...
final bundle = AstBundle.fromFile('app.d4rtbundle');
final result = runner.executeBundle(bundle);

Implementation

dynamic executeBundle(
  AstBundle bundle, {
  String? entryPoint,
  String name = 'main',
  List<Object?>? positionalArgs,
  Map<String, Object?>? namedArgs,
}) {
  // Determine entry point URI
  final entryUri = entryPoint ?? bundle.entryPointUri;
  final entryAst = bundle.modules[entryUri];
  if (entryAst == null) {
    throw ArgumentD4rtException(
      'Entry point "$entryUri" not found in bundle. '
      'Available: ${bundle.modules.keys.join(", ")}',
    );
  }

  // Initialize environment
  InterpretedFunction.clearParentMap();
  final executionEnvironment = _initEnvironment();

  // Create AstModuleLoader for import resolution
  final moduleLoader = AstModuleLoader(
    modules: bundle.modules,
    globalEnvironment: executionEnvironment,
    runner: this,
  );
  moduleLoader.currentLibrary = Uri.parse(entryUri);

  Logger.debug(
    '[D4rtRunner.executeBundle] Entry point: $entryUri '
    '(${bundle.modules.length} modules in bundle)',
  );

  // Execute with full module context
  return _executeInEnvironment(
    compilationUnit: entryAst,
    executionEnvironment: executionEnvironment,
    moduleContext: moduleLoader,
    name: name,
    positionalArgs: positionalArgs,
    namedArgs: namedArgs,
  );
}