libraryUnit method

SCompilationUnit libraryUnit(
  1. Uri uri,
  2. SCompilationUnit unit
)

Loads a module from the bundle by processing its AST.

This performs the full module loading sequence:

  1. Create module environment (enclosing global)
  2. Process import directives recursively
  3. Run declaration visitor (populate names)
  4. Run interpreter visitor (initialize values)
  5. Build exported environment
  6. Cache and return The bundle module unit at uri as 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,
  );
}