• 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

81.82
/lib/src/code_generator/type.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 'writer.dart';
8

9
/// Type class for return types, variable types, etc.
10
///
11
/// Implementers should extend either Type, or BindingType if the type is also a
12
/// binding, and override at least getCType and toString.
13
abstract class Type {
14
  const Type();
149✔
15

16
  /// Get all dependencies of this type and save them in [dependencies].
17
  void addDependencies(Set<Binding> dependencies) {}
35✔
18

19
  /// Get base type for any type.
20
  ///
21
  /// E.g int** has base [Type] of int.
22
  /// double[2][3] has base [Type] of double.
23
  Type get baseType => this;
31✔
24

25
  /// Get base Array type.
26
  ///
27
  /// Returns itself if it's not an Array Type.
28
  Type get baseArrayType => this;
8✔
29

30
  /// Get base typealias type.
31
  ///
32
  /// Returns itself if it's not a Typealias.
33
  Type get typealiasType => this;
26✔
34

35
  /// Returns true if the type is a [Compound] and is incomplete.
36
  bool get isIncompleteCompound => false;
30✔
37

38
  /// Returns the C type of the Type. This is the FFI compatible type that is
39
  /// passed to native code.
40
  String getCType(Writer w) => throw 'No mapping for type: $this';
×
41

42
  /// Returns the Dart type of the Type. This is the type that is passed from
43
  /// FFI to Dart code.
44
  String getFfiDartType(Writer w) => getCType(w);
50✔
45

46
  /// Returns the user type of the Type. This is the type that is presented to
47
  /// users by the ffigened API to users. For C bindings this is always the same
48
  /// as getFfiDartType. For ObjC bindings this refers to the wrapper object.
49
  String getDartType(Writer w) => getFfiDartType(w);
4✔
50

51
  /// Returns whether the FFI dart type and C type string are same.
52
  bool get sameFfiDartAndCType;
53

54
  /// Returns whether the dart type and C type string are same.
55
  bool get sameDartAndCType => sameFfiDartAndCType;
×
56

57
  /// Returns the string representation of the Type, for debugging purposes
58
  /// only. This string should not be printed as generated code.
59
  @override
60
  String toString();
61

62
  /// Cache key used in various places to dedupe Types. By default this is just
63
  /// the hash of the Type, but in many cases this does not dedupe sufficiently.
64
  /// So Types that may be duplicated should override this to return a more
65
  /// specific key. Types that are already deduped don't need to override this.
66
  /// toString() is not a valid cache key as there may be name collisions.
67
  String cacheKey() => hashCode.toRadixString(36);
6✔
68

69
  /// Returns a string of code that creates a default value for this type. For
70
  /// example, for int types this returns the string '0'. A null return means
71
  /// that default values aren't supported for this type, eg void.
72
  String? getDefaultValue(Writer w, String nativeLib) => null;
2✔
73
}
74

75
/// Base class for all Type bindings.
76
///
77
/// Since Dart doesn't have multiple inheritance, this type exists so that we
78
/// don't have to reimplement the default methods in all the classes that want
79
/// to extend both NoLookUpBinding and Type.
80
abstract class BindingType extends NoLookUpBinding implements Type {
81
  BindingType({
33✔
82
    String? usr,
83
    String? originalName,
84
    required String name,
85
    String? dartDoc,
86
    bool isInternal = false,
87
  }) : super(
33✔
88
          usr: usr,
89
          originalName: originalName,
90
          name: name,
91
          dartDoc: dartDoc,
92
          isInternal: isInternal,
93
        );
94

95
  @override
22✔
96
  Type get baseType => this;
97

98
  @override
3✔
99
  Type get baseArrayType => this;
100

101
  @override
10✔
102
  Type get typealiasType => this;
103

104
  @override
5✔
105
  bool get isIncompleteCompound => false;
106

107
  @override
12✔
108
  String getFfiDartType(Writer w) => getCType(w);
12✔
109

110
  @override
2✔
111
  String getDartType(Writer w) => getFfiDartType(w);
2✔
112

113
  @override
×
114
  bool get sameDartAndCType => sameFfiDartAndCType;
×
115

116
  @override
2✔
117
  String toString() => originalName;
2✔
118

119
  @override
2✔
120
  String cacheKey() => hashCode.toRadixString(36);
4✔
121

122
  @override
×
123
  String? getDefaultValue(Writer w, String nativeLib) => null;
124
}
125

126
/// Represents an unimplemented type. Used as a marker, so that declarations
127
/// having these can exclude them.
128
class UnimplementedType extends Type {
129
  String reason;
130
  UnimplementedType(this.reason);
12✔
131

132
  @override
3✔
133
  String toString() => '(Unimplemented: $reason)';
6✔
134

135
  @override
×
136
  bool get sameFfiDartAndCType => true;
137
}
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