executeSource function

ScriptExecutionResult executeSource(
  1. D4rt d4rt,
  2. String source,
  3. String basePath, {
  4. String scriptName = '__script__.dart',
  5. void log(
    1. String
    )?,
})

Execute a Dart script from source code with a basePath for import resolution.

This method:

  1. Recursively resolves all relative imports from the source
  2. Executes using D4rt.execute (replaces initialization)

d4rt The D4rt interpreter instance. source The Dart source code to execute. basePath Base directory path for resolving relative imports. scriptName Optional name for the script (defaults to 'script.dart'). log Optional logging function for debugging import resolution.

Returns a ScriptExecutionResult with the execution outcome.

Implementation

ScriptExecutionResult executeSource(
  D4rt d4rt,
  String source,
  String basePath, {
  String scriptName = '__script__.dart',
  void Function(String)? log,
}) {
  try {
    final libraryUri = 'file://$basePath/$scriptName';

    // Pre-resolve all imports into sources map
    final sources = <String, String>{};
    resolveImportsRecursively(source, libraryUri, sources, log);

    // Execute with sources
    final result = d4rt.execute(
      library: libraryUri,
      sources: sources,
      basePath: basePath,
      allowFileSystemImports: true,
    );

    return ScriptExecutionResult.success(result, sources.length);
  } catch (e, stackTrace) {
    return ScriptExecutionResult.failure(e.toString(), stackTrace: stackTrace);
  }
}