visitCompilation method

  1. @override
Uint8List visitCompilation(
  1. ASTCompilation compilation
)
override

Implementation

@override
Uint8List visitCompilation(ASTCompilation compilation) {
  _currentConstantTable = HTGlobalConstantTable();

  final mainBytesBuilder = BytesBuilder();
  // hetu bytecode signature
  mainBytesBuilder.add(hetuSignatureData);
  mainBytesBuilder.add(_compileVersionString(kHetuVersion));
  if (compilation.version != null) {
    mainBytesBuilder.addByte(1); // bool: hasVersion
    mainBytesBuilder.add(_compileVersionString(compilation.version!));
  } else {
    mainBytesBuilder.addByte(0); // bool: hasVersion
  }
  final compiledAt = DateTime.now().toUtc();
  final DateFormat formatter = DateFormat('yyyy-MM-dd HH:mm:ss');
  final compiledAtString = formatter.format(compiledAt);
  mainBytesBuilder.add(_utf8String(compiledAtString));
  // entry file name
  mainBytesBuilder.add(_utf8String(compilation.entryFullname));
  // index: ResourceType
  mainBytesBuilder.addByte(compilation.entryResourceType.index);
  final sourceBytesBuilder = BytesBuilder();
  for (final value in compilation.values.values) {
    final bytes = value.accept(this);
    sourceBytesBuilder.add(bytes);
  }
  for (final value in compilation.sources.values) {
    final bytes = value.accept(this);
    sourceBytesBuilder.add(bytes);
  }
  final code = sourceBytesBuilder.toBytes();
  // const tables
  for (final type in _currentConstantTable.constants.keys) {
    final table = _currentConstantTable.constants[type]!;
    if (type == int) {
      mainBytesBuilder.addByte(HTOpCode.constIntTable);
      mainBytesBuilder.add(_uint16(table.length));
      for (final value in table) {
        mainBytesBuilder.add(_int64(value));
      }
    } else if (type == double) {
      mainBytesBuilder.addByte(HTOpCode.constFloatTable);
      mainBytesBuilder.add(_uint16(table.length));
      for (final value in table) {
        mainBytesBuilder.add(_float64(value));
      }
    } else if (type == String) {
      mainBytesBuilder.addByte(HTOpCode.constStringTable);
      mainBytesBuilder.add(_uint16(table.length));
      for (final value in table) {
        mainBytesBuilder.add(_utf8String(value));
      }
    } else {
      continue;
    }
  }
  mainBytesBuilder.add(code);
  return mainBytesBuilder.toBytes();
}