libraryUnit method
Loads a module from the bundle by processing its AST.
This performs the full module loading sequence:
- Create module environment (enclosing global)
- Process import directives recursively
- Run declaration visitor (populate names)
- Run interpreter visitor (initialize values)
- Build exported environment
- Cache and return
The bundle module
unitaturias a LIBRARY: its parts merged in, the Dart rules for parts checked.
GitHub #21. A part (part of 'owner.dart';) is not a library: importing,
exporting or running it is refused, as Dart refuses it. A library's
part 'piece.dart'; makes the part's declarations its own - they share
its imports and its private names - so each part (a module of the bundle,
which AstBundler includes by following part directives) is checked to
name this library in its part of and to have no directive of its own
(Dart before enhanced parts), and its declarations are appended to the
library's. The part stays a module of the bundle, so a diagnostic still
finds a part's node in the part's own source.
Implementation
SCompilationUnit libraryUnit(Uri uri, SCompilationUnit unit) {
if (unit.directives.any((d) => d is SPartOfDirective)) {
throw SourceCodeD4rtException(
"'$uri' is a part of another library (it has a part-of directive), "
'not a library: only its library can be imported, exported or run.',
);
}
final parts = unit.directives.whereType<SPartDirective>().toList();
if (parts.isEmpty) return unit;
final declarations = [...unit.declarations];
for (final part in parts) {
final partString = _partUriString(part.uri);
if (partString == null) continue;
final partUri = uri.resolve(partString);
final partUnit = modules[partUri.toString()];
if (partUnit == null) {
throw SourceCodeD4rtException(
"The part '$partUri' of '$uri' is not in the bundle. Build the "
'bundle with its part directives followed.',
);
}
_checkPartOwner(uri, unit, partUri, partUnit);
declarations.addAll(partUnit.declarations);
}
return SCompilationUnit(
offset: unit.offset,
length: unit.length,
directives: unit.directives,
declarations: declarations,
);
}