executeFile function
Execute a Dart script file with fresh interpreter state.
The module loader resolves every import from disk under FilesystemPermission (see the library comment): READ on the script's own directory tree is granted for the run, and anything outside it needs a grant the host made.
d4rt The D4rt interpreter instance.
filePath Path to the Dart script file.
log Optional logging function.
Returns a ScriptExecutionResult with the execution outcome.
Implementation
ScriptExecutionResult executeFile(
D4rt d4rt,
String filePath, {
void Function(String)? log,
}) {
final file = File(filePath);
if (!file.existsSync()) {
return ScriptExecutionResult.failure('File not found: $filePath');
}
// The real path: the implicit grant is anchored where the script actually
// lives, so a symlinked entry point cannot widen it.
final fullPath = file.resolveSymbolicLinksSync();
final scriptDirectory = File(fullPath).parent.path;
final libraryUri = Uri.file(fullPath).toString();
try {
final source = File(fullPath).readAsStringSync();
log?.call('Executing $libraryUri');
final result = _withScriptDirectoryGrant(
d4rt,
scriptDirectory,
() => d4rt.execute(
library: libraryUri,
sources: {libraryUri: source},
basePath: scriptDirectory,
allowFileSystemImports: true,
),
);
return ScriptExecutionResult.success(result, d4rt.loadedSourceModuleCount);
} catch (e, stackTrace) {
return ScriptExecutionResult.failure(e.toString(), stackTrace: stackTrace);
}
}