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

dart-lang / ffigen / 6725753357

01 Nov 2023 10:20PM UTC coverage: 92.243% (+0.1%) from 92.094%
6725753357

Pull #638

github

web-flow
Merge 475bd4c30 into 9c3b631f7
Pull Request #638: Bump subosito/flutter-action from 2.10.0 to 2.12.0

3722 of 4035 relevant lines covered (92.24%)

28.37 hits per line

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

86.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
  String? _ffiDartAliasName;
21
  String? _dartAliasName;
22

23
  /// Creates a Typealias.
24
  ///
25
  /// If [genFfiDartType] is true, a binding is generated for the Ffi Dart type
26
  /// in addition to the C type. See [Type.getFfiDartType].
27
  factory Typealias({
19✔
28
    String? usr,
29
    String? originalName,
30
    String? dartDoc,
31
    required String name,
32
    required Type type,
33
    bool genFfiDartType = false,
34
    bool isInternal = false,
35
  }) {
36
    final funcType = _getFunctionTypeFromPointer(type);
19✔
37
    if (funcType != null) {
38
      type = PointerType(NativeFunc(Typealias._(
18✔
39
        name: '${name}Function',
6✔
40
        type: funcType,
41
        genFfiDartType: genFfiDartType,
42
        isInternal: isInternal,
43
      )));
44
    }
45
    if ((originalName ?? name) == strings.objcInstanceType &&
19✔
46
        type is ObjCObjectPointer) {
2✔
47
      return ObjCInstanceType._(
2✔
48
        usr: usr,
49
        originalName: originalName,
50
        dartDoc: dartDoc,
51
        name: name,
52
        type: type,
53
        genFfiDartType: genFfiDartType,
54
        isInternal: isInternal,
55
      );
56
    }
57
    return Typealias._(
17✔
58
      usr: usr,
59
      originalName: originalName,
60
      dartDoc: dartDoc,
61
      name: name,
62
      type: type,
63
      genFfiDartType: genFfiDartType,
64
      isInternal: isInternal,
65
    );
66
  }
67

68
  Typealias._({
19✔
69
    super.usr,
70
    super.originalName,
71
    super.dartDoc,
72
    required String name,
73
    required this.type,
74
    bool genFfiDartType = false,
75
    super.isInternal,
76
  })  : _ffiDartAliasName = genFfiDartType ? 'Dart$name' : null,
2✔
77
        _dartAliasName =
78
            (!genFfiDartType && type is! Typealias && !type.sameDartAndCType)
36✔
79
                ? 'Dart$name'
80
                : null,
12✔
81
        super(
19✔
82
          name: genFfiDartType ? 'Native$name' : name,
2✔
83
        );
84

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

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

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

100
  @override
17✔
101
  BindingString toBindingString(Writer w) {
102
    if (_ffiDartAliasName != null) {
17✔
103
      _ffiDartAliasName = w.topLevelUniqueNamer.makeUnique(_ffiDartAliasName!);
8✔
104
    }
105
    if (_dartAliasName != null) {
17✔
106
      _dartAliasName = w.topLevelUniqueNamer.makeUnique(_dartAliasName!);
44✔
107
    }
108

109
    final sb = StringBuffer();
17✔
110
    if (dartDoc != null) {
17✔
111
      sb.write(makeDartDoc(dartDoc!));
6✔
112
    }
113
    sb.write('typedef $name = ${type.getCType(w)};\n');
85✔
114
    if (_ffiDartAliasName != null) {
17✔
115
      sb.write('typedef $_ffiDartAliasName = ${type.getFfiDartType(w)};\n');
10✔
116
    }
117
    if (_dartAliasName != null) {
17✔
118
      sb.write('typedef $_dartAliasName = ${type.getDartType(w)};\n');
55✔
119
    }
120
    return BindingString(
17✔
121
        type: BindingStringType.typeDef, string: sb.toString());
17✔
122
  }
123

124
  @override
13✔
125
  Type get typealiasType => type.typealiasType;
26✔
126

127
  @override
13✔
128
  bool get isIncompleteCompound => type.isIncompleteCompound;
26✔
129

130
  @override
14✔
131
  String getCType(Writer w) => name;
14✔
132

133
  @override
12✔
134
  String getFfiDartType(Writer w) {
135
    if (_ffiDartAliasName != null) {
12✔
136
      return _ffiDartAliasName!;
2✔
137
    } else if (type.sameFfiDartAndCType) {
22✔
138
      return name;
10✔
139
    } else {
140
      return type.getFfiDartType(w);
6✔
141
    }
142
  }
143

144
  @override
2✔
145
  String getDartType(Writer w) {
146
    if (_dartAliasName != null) {
2✔
147
      return _dartAliasName!;
×
148
    } else if (type.sameDartAndCType) {
4✔
149
      return getFfiDartType(w);
2✔
150
    } else {
151
      return type.getDartType(w);
×
152
    }
153
  }
154

155
  @override
6✔
156
  bool get sameFfiDartAndCType => type.sameFfiDartAndCType;
12✔
157

158
  @override
1✔
159
  bool get sameDartAndCType => type.sameDartAndCType;
2✔
160

161
  @override
×
162
  String convertDartTypeToFfiDartType(
163
    Writer w,
164
    String value, {
165
    required bool objCRetain,
166
  }) =>
167
      type.convertDartTypeToFfiDartType(w, value, objCRetain: objCRetain);
×
168

169
  @override
×
170
  String convertFfiDartTypeToDartType(
171
    Writer w,
172
    String value,
173
    String library, {
174
    required bool objCRetain,
175
    String? objCEnclosingClass,
176
  }) =>
177
      type.convertFfiDartTypeToDartType(
×
178
        w,
179
        value,
180
        library,
181
        objCRetain: objCRetain,
182
        objCEnclosingClass: objCEnclosingClass,
183
      );
184

185
  @override
2✔
186
  String cacheKey() => type.cacheKey();
4✔
187

188
  @override
×
189
  String? getDefaultValue(Writer w, String nativeLib) =>
190
      type.getDefaultValue(w, nativeLib);
×
191
}
192

193
/// Objective C's instancetype.
194
///
195
/// This is an alias for an NSObject* that is special cased in code generation.
196
/// It's only valid as the return type of a method, and always appears as the
197
/// enclosing class's type, even in inherited methods.
198
class ObjCInstanceType extends Typealias {
199
  ObjCInstanceType._({
2✔
200
    super.usr,
201
    super.originalName,
202
    super.dartDoc,
203
    required super.name,
204
    required super.type,
205
    super.genFfiDartType,
206
    super.isInternal,
207
  }) : super._();
2✔
208

209
  @override
×
210
  String convertDartTypeToFfiDartType(
211
    Writer w,
212
    String value, {
213
    required bool objCRetain,
214
  }) =>
215
      ObjCInterface.generateGetId(value, objCRetain);
×
216

217
  @override
2✔
218
  String convertFfiDartTypeToDartType(
219
    Writer w,
220
    String value,
221
    String library, {
222
    required bool objCRetain,
223
    String? objCEnclosingClass,
224
  }) =>
225
      // objCEnclosingClass must be present, because instancetype can only
226
      // occur inside a class.
227
      ObjCInterface.generateConstructor(
2✔
228
          objCEnclosingClass!, value, library, objCRetain);
229
}
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