• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dart-lang / ffigen / 6470767942

10 Oct 2023 02:29PM UTC coverage: 91.966% (-0.6%) from 92.593%
6470767942

push

github

web-flow
Refactor `sameFfiDartAndCType` to not require a `Writer` (#629)

40 of 40 new or added lines in 12 files covered. (100.0%)

3709 of 4033 relevant lines covered (91.97%)

28.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.49
/lib/src/code_generator/typealias.dart
1
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2
// for details. All rights reserved. Use of this source code is governed by a
3
// BSD-style license that can be found in the LICENSE file.
4

5
import 'package:ffigen/src/code_generator.dart';
6

7
import '../strings.dart' as strings;
8
import 'binding_string.dart';
9
import 'utils.dart';
10
import 'writer.dart';
11

12
/// A simple Typealias, Expands to -
13
///
14
/// ```dart
15
/// typedef $name = $type;
16
/// );
17
/// ```
18
class Typealias extends BindingType {
19
  final Type type;
20
  final bool _useDartType;
21

22
  factory Typealias({
19✔
23
    String? usr,
24
    String? originalName,
25
    String? dartDoc,
26
    required String name,
27
    required Type type,
28

29
    /// If true, the binding string uses Dart type instead of C type.
30
    ///
31
    /// E.g if C type is ffi.Void func(ffi.Int32), Dart type is void func(int).
32
    bool useDartType = false,
33
    bool isInternal = false,
34
  }) {
35
    final funcType = _getFunctionTypeFromPointer(type);
19✔
36
    if (funcType != null) {
37
      type = PointerType(NativeFunc(Typealias._(
18✔
38
        name: '${name}_function',
6✔
39
        type: funcType,
40
        useDartType: useDartType,
41
        isInternal: isInternal,
42
      )));
43
    }
44
    if ((originalName ?? name) == strings.objcInstanceType &&
19✔
45
        type is ObjCObjectPointer) {
2✔
46
      return ObjCInstanceType._(
2✔
47
        usr: usr,
48
        originalName: originalName,
49
        dartDoc: dartDoc,
50
        name: name,
51
        type: type,
52
        useDartType: useDartType,
53
        isInternal: isInternal,
54
      );
55
    }
56
    return Typealias._(
17✔
57
      usr: usr,
58
      originalName: originalName,
59
      dartDoc: dartDoc,
60
      name: name,
61
      type: type,
62
      useDartType: useDartType,
63
      isInternal: isInternal,
64
    );
65
  }
66

67
  Typealias._({
19✔
68
    String? usr,
69
    String? originalName,
70
    String? dartDoc,
71
    required String name,
72
    required this.type,
73
    bool useDartType = false,
74
    bool isInternal = false,
75
  })  : _useDartType = useDartType,
76
        super(
19✔
77
          usr: usr,
78
          name: name,
79
          dartDoc: dartDoc,
80
          originalName: originalName,
81
          isInternal: isInternal,
82
        );
83

84
  @override
19✔
85
  void addDependencies(Set<Binding> dependencies) {
86
    if (dependencies.contains(this)) return;
19✔
87

88
    dependencies.add(this);
19✔
89
    type.addDependencies(dependencies);
38✔
90
  }
91

92
  static FunctionType? _getFunctionTypeFromPointer(Type type) {
19✔
93
    if (type is! PointerType) return null;
19✔
94
    final pointee = type.child;
8✔
95
    if (pointee is! NativeFunc) return null;
8✔
96
    return pointee.type;
6✔
97
  }
98

99
  @override
17✔
100
  BindingString toBindingString(Writer w) {
101
    final sb = StringBuffer();
17✔
102
    if (dartDoc != null) {
17✔
103
      sb.write(makeDartDoc(dartDoc!));
6✔
104
    }
105
    sb.write('typedef $name = ');
51✔
106
    sb.write('${_useDartType ? type.getFfiDartType(w) : type.getCType(w)};\n');
89✔
107
    return BindingString(
17✔
108
        type: BindingStringType.typeDef, string: sb.toString());
17✔
109
  }
110

111
  @override
13✔
112
  Type get typealiasType => type.typealiasType;
26✔
113

114
  @override
13✔
115
  bool get isIncompleteCompound => type.isIncompleteCompound;
26✔
116

117
  @override
13✔
118
  String getCType(Writer w) => name;
13✔
119

120
  @override
11✔
121
  String getFfiDartType(Writer w) {
122
    // Typealias cannot be used by name in Dart types unless both the C and Dart
123
    // type of the underlying types are same.
124
    if (type.sameFfiDartAndCType) {
22✔
125
      return name;
10✔
126
    } else {
127
      return type.getFfiDartType(w);
6✔
128
    }
129
  }
130

131
  @override
6✔
132
  bool get sameFfiDartAndCType => type.sameFfiDartAndCType;
12✔
133

134
  @override
×
135
  bool get sameDartAndCType => type.sameDartAndCType;
×
136

137
  @override
2✔
138
  String cacheKey() => type.cacheKey();
4✔
139

140
  @override
×
141
  String? getDefaultValue(Writer w, String nativeLib) =>
142
      type.getDefaultValue(w, nativeLib);
×
143
}
144

145
/// Objective C's instancetype.
146
///
147
/// This is an alias for an NSObject* that is special cased in code generation.
148
/// It's only valid as the return type of a method, and always appears as the
149
/// enclosing class's type, even in inherited methods.
150
class ObjCInstanceType extends Typealias {
151
  ObjCInstanceType._({
2✔
152
    String? usr,
153
    String? originalName,
154
    String? dartDoc,
155
    required String name,
156
    required Type type,
157

158
    /// If true, the binding string uses Dart type instead of C type.
159
    ///
160
    /// E.g if C type is ffi.Void func(ffi.Int32), Dart type is void func(int).
161
    bool useDartType = false,
162
    bool isInternal = false,
163
  }) : super._(
2✔
164
          usr: usr,
165
          originalName: originalName,
166
          dartDoc: dartDoc,
167
          name: name,
168
          type: type,
169
          useDartType: useDartType,
170
          isInternal: isInternal,
171
        );
172
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc