writeCodeUnit method

Uint8List writeCodeUnit(
  1. CodeUnit codeUnit
)

Encodes the AST of codeUnit, which must already be loaded.

Throws a StateError when codeUnit.root is null — the same contract as CodeUnit.generateCode.

Implementation

Uint8List writeCodeUnit(CodeUnit codeUnit) {
  var root = codeUnit.root;
  if (root == null) {
    throw StateError(
      "Can't write a binary AST for a `CodeUnit` that has not been loaded: "
      '${codeUnit.id} (${codeUnit.language})',
    );
  }

  return writeRoot(
    root,
    language: codeUnit.language,
    namespace: codeUnit.namespace ?? root.namespace,
    id: codeUnit.id,
  );
}