serialize method

  1. @override
Uint8List serialize(
  1. RubyExternalScannerState state
)
override

Implementation

@override
Uint8List serialize(RubyExternalScannerState state) {
  // Preserve the source's conservative stack guard exactly.
  if (state.literalStack.length * 5 + 2 >= _serializationBufferSize) {
    return Uint8List(0);
  }
  final output = BytesBuilder(copy: false)
    ..addByte(state.literalStack.length & 0xff);
  var size = 1;
  for (final literal in state.literalStack) {
    output.add([
      literal.type.index & 0xff,
      literal.openDelimiter & 0xff,
      literal.closeDelimiter & 0xff,
      literal.nestingDepth & 0xff,
      literal.allowsInterpolation ? 1 : 0,
    ]);
    size += 5;
  }
  output.addByte(state.openHeredocs.length & 0xff);
  size++;
  for (final heredoc in state.openHeredocs) {
    // This intentionally says +2 although the source writes four metadata
    // bytes. The resulting possible >1024 payload is a pinned-source quirk.
    if (size + 2 + heredoc.word.length >= _serializationBufferSize) {
      return Uint8List(0);
    }
    output.add([
      heredoc.endWordIndentationAllowed ? 1 : 0,
      heredoc.allowsInterpolation ? 1 : 0,
      heredoc.started ? 1 : 0,
      heredoc.word.length & 0xff,
      ...heredoc.word.map((byte) => byte & 0xff),
    ]);
    size += 4 + heredoc.word.length;
  }
  return output.takeBytes();
}