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

dart-lang / ffigen / 6488131502

11 Oct 2023 09:18PM UTC coverage: 92.264% (+0.3%) from 91.966%
6488131502

push

github

web-flow
Typedefs for Dart types (#625)

* WIP fix #386

* Fix tests

* fmt

* Update test expectations

* Update test expectations

* Fix analysis

* fmt

* Fix more tests

* WIP refactor

* Refactor `sameFfiDartAndCType` to not require a `Writer`

* Fix tests

* Fix analysis

* Update test expectations

* Format

* Fix typedef_test.dart

* Fix nits

* Fix tests

* Fmt

* Regenerate more test files

* Regen more tests

* Remove accidental commit

53 of 53 new or added lines in 3 files covered. (100.0%)

3733 of 4046 relevant lines covered (92.26%)

28.36 hits per line

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

93.55
/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
    String? usr,
70
    String? originalName,
71
    String? dartDoc,
72
    required String name,
73
    required this.type,
74
    bool genFfiDartType = false,
75
    bool isInternal = false,
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
          usr: usr,
83
          name: genFfiDartType ? 'Native$name' : name,
2✔
84
          dartDoc: dartDoc,
85
          originalName: originalName,
86
          isInternal: isInternal,
87
        );
88

89
  @override
19✔
90
  void addDependencies(Set<Binding> dependencies) {
91
    if (dependencies.contains(this)) return;
19✔
92

93
    dependencies.add(this);
19✔
94
    type.addDependencies(dependencies);
38✔
95
  }
96

97
  static FunctionType? _getFunctionTypeFromPointer(Type type) {
19✔
98
    if (type is! PointerType) return null;
19✔
99
    final pointee = type.child;
8✔
100
    if (pointee is! NativeFunc) return null;
8✔
101
    return pointee.type;
6✔
102
  }
103

104
  @override
17✔
105
  BindingString toBindingString(Writer w) {
106
    if (_ffiDartAliasName != null) {
17✔
107
      _ffiDartAliasName = w.topLevelUniqueNamer.makeUnique(_ffiDartAliasName!);
8✔
108
    }
109
    if (_dartAliasName != null) {
17✔
110
      _dartAliasName = w.topLevelUniqueNamer.makeUnique(_dartAliasName!);
44✔
111
    }
112

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

128
  @override
13✔
129
  Type get typealiasType => type.typealiasType;
26✔
130

131
  @override
13✔
132
  bool get isIncompleteCompound => type.isIncompleteCompound;
26✔
133

134
  @override
14✔
135
  String getCType(Writer w) => name;
14✔
136

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

148
  @override
×
149
  String getDartType(Writer w) => _dartAliasName ?? type.getDartType(w);
×
150

151
  @override
6✔
152
  bool get sameFfiDartAndCType => type.sameFfiDartAndCType;
12✔
153

154
  @override
1✔
155
  bool get sameDartAndCType => type.sameDartAndCType;
2✔
156

157
  @override
2✔
158
  String cacheKey() => type.cacheKey();
4✔
159

160
  @override
×
161
  String? getDefaultValue(Writer w, String nativeLib) =>
162
      type.getDefaultValue(w, nativeLib);
×
163
}
164

165
/// Objective C's instancetype.
166
///
167
/// This is an alias for an NSObject* that is special cased in code generation.
168
/// It's only valid as the return type of a method, and always appears as the
169
/// enclosing class's type, even in inherited methods.
170
class ObjCInstanceType extends Typealias {
171
  ObjCInstanceType._({
2✔
172
    String? usr,
173
    String? originalName,
174
    String? dartDoc,
175
    required String name,
176
    required Type type,
177
    bool genFfiDartType = false,
178
    bool isInternal = false,
179
  }) : super._(
2✔
180
          usr: usr,
181
          originalName: originalName,
182
          dartDoc: dartDoc,
183
          name: name,
184
          type: type,
185
          genFfiDartType: genFfiDartType,
186
          isInternal: isInternal,
187
        );
188
}
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