visitStructDecl method

  1. @override
Uint8List visitStructDecl(
  1. StructDecl stmt
)
override

Implementation

@override
Uint8List visitStructDecl(StructDecl stmt) {
  final bytesBuilder = BytesBuilder();
  bytesBuilder.addByte(HTOpCode.structDecl);
  bytesBuilder.add(_parseIdentifier(stmt.id.id));
  bytesBuilder.addByte(stmt.isTopLevel ? 1 : 0);
  if (stmt.prototypeId != null) {
    bytesBuilder.addByte(1); // bool: hasPrototypeId
    bytesBuilder.add(_parseIdentifier(stmt.prototypeId!.id));
  } else {
    bytesBuilder.addByte(0); // bool: hasPrototypeId
  }
  bytesBuilder.addByte(stmt.mixinIds.length);
  for (final mixinId in stmt.mixinIds) {
    bytesBuilder.add(_parseIdentifier(mixinId.id));
  }
  final staticFields = <StructObjField>[];
  final fields = <StructObjField>[];
  for (final node in stmt.definition) {
    ASTNode initializer;
    if (node is FuncDecl) {
      FuncDecl initializer = node;
      final field = StructObjField(
          key: IdentifierExpr(
            initializer.internalName,
            isLocal: false,
          ),
          fieldValue: initializer);
      node.isStatic ? staticFields.add(field) : fields.add(field);
    } else if (node is VarDecl) {
      initializer = node.initializer ?? ASTLiteralNull();
      final field = StructObjField(
          key: IdentifierExpr(
            node.id.id,
            isLocal: false,
          ),
          fieldValue: initializer);
      node.isStatic ? staticFields.add(field) : fields.add(field);
    }
    // Other node type is ignored.
  }
  final staticBytes =
      compileAST(StructObjExpr(staticFields), endOfExec: true);
  final structBytes = compileAST(
      StructObjExpr(fields,
          id: IdentifierExpr(
            stmt.id.id,
            isLocal: false,
          ),
          prototypeId: stmt.prototypeId),
      endOfExec: true);
  bytesBuilder.add(_uint16(staticBytes.length));
  bytesBuilder.add(staticBytes);
  bytesBuilder.add(_uint16(structBytes.length));
  bytesBuilder.add(structBytes);
  return bytesBuilder.toBytes();
}