-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathobjc_interface.dart
More file actions
289 lines (248 loc) · 8.01 KB
/
objc_interface.dart
File metadata and controls
289 lines (248 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import '../code_generator.dart';
import '../context.dart';
import '../header_parser/sub_parsers/api_availability.dart';
import '../visitor/ast.dart';
import 'binding_string.dart';
import 'scope.dart';
import 'utils.dart';
import 'writer.dart';
class ObjCInterface extends BindingType with ObjCMethods, HasLocalScope {
@override
final Context context;
ObjCInterface? superType;
bool filled = false;
final String lookupName;
late final ObjCInternalGlobal classObject;
late final ObjCInternalGlobal _isKindOfClass;
late final ObjCMsgSendFunc _isKindOfClassMsgSend;
final protocols = <ObjCProtocol>[];
final categories = <ObjCCategory>[];
final subtypes = <ObjCInterface>[];
final ApiAvailability apiAvailability;
final Set<String> swiftUnavailableSelectors = {};
// Filled by ListBindingsVisitation.
bool generateAsStub = false;
ObjCInterface({
super.usr,
required String super.originalName,
String? name,
String? lookupName,
super.dartDoc,
required this.apiAvailability,
required this.context,
}) : lookupName = lookupName ?? originalName,
super(
name:
context.objCBuiltInFunctions.getBuiltInInterfaceName(
originalName,
) ??
name ??
originalName,
) {
classObject = ObjCInternalGlobal(
'_class_$originalName',
() => '${ObjCBuiltInFunctions.getClass.gen(context)}("$lookupName")',
);
_isKindOfClass = context.objCBuiltInFunctions.getSelObject(
'isKindOfClass:',
);
_isKindOfClassMsgSend = context.objCBuiltInFunctions.getMsgSendFunc(
BooleanType(),
[
Parameter(
name: 'clazz',
type: PointerType(objCObjectType),
objCConsumed: false,
),
],
);
}
void addProtocol(ObjCProtocol? proto) {
if (proto != null) protocols.add(proto);
}
@override
bool get isObjCImport =>
context.objCBuiltInFunctions.getBuiltInInterfaceName(originalName) !=
null;
bool get unavailable => apiAvailability.availability == Availability.none;
@override
BindingString toBindingString(Writer w) {
final context = w.context;
final s = StringBuffer();
s.write('\n');
if (generateAsStub) {
s.write('''
/// WARNING: $name is a stub. To generate bindings for this class, include
/// $originalName in your config's objc-interfaces list.
///
''');
}
s.write(makeDartDoc(dartDoc));
final ctorBody = [
apiAvailability.runtimeCheck(
ObjCBuiltInFunctions.checkOsVersion.gen(context),
originalName,
),
if (!generateAsStub) 'assert(isA(object\$));',
].nonNulls.join('\n ');
final rawObjType = PointerType(objCObjectType).getCType(context);
final wrapObjType = ObjCBuiltInFunctions.objectBase.gen(context);
final protos = [
wrapObjType,
...[superType, ...protocols].nonNulls.map((p) => p.getDartType(context)),
];
s.write('''
extension type $name._($wrapObjType object\$) implements ${protos.join(',')} {
/// Constructs a [$name] that points to the same underlying object as [other].
$name.as($wrapObjType other) : object\$ = other {
$ctorBody
}
/// Constructs a [$name] that wraps the given raw object pointer.
$name.fromPointer($rawObjType other,
{bool retain = false, bool release = false}) :
object\$ = $wrapObjType(other, retain: retain, release: release) {
$ctorBody
}
${generateAsStub ? '' : _generateStaticMethods(w)}
}
''');
if (!generateAsStub) {
s.write('''
extension $name\$Methods on $name {
${generateInstanceMethodBindings(w, this)}
}
''');
}
return BindingString(
type: BindingStringType.objcInterface,
string: s.toString(),
);
}
String _generateStaticMethods(Writer w) {
final context = w.context;
final wrapObjType = ObjCBuiltInFunctions.objectBase.gen(context);
final s = StringBuffer();
final isKindOfClass = _isKindOfClassMsgSend.invoke(
context,
'obj.ref.pointer',
_isKindOfClass.name,
[classObject.name],
);
s.write('''
/// Returns whether [obj] is an instance of [$name].
static bool isA($wrapObjType? obj) => obj == null
? false
: $isKindOfClass;
''');
s.write(generateStaticMethodBindings(w, this));
final newMethod = methods
.where(
(ObjCMethod m) =>
m.isClassMethod &&
m.family == ObjCMethodFamily.new_ &&
m.params.isEmpty &&
m.originalName == 'new',
)
.firstOrNull;
if (newMethod != null &&
originalName != 'NSString' &&
!swiftUnavailableSelectors.contains('new')) {
s.write('''
/// Returns a new instance of $name constructed with the default `new` method.
$name() : this.as(${newMethod.name}().object\$);
''');
}
return s.toString();
}
@override
String getCType(Context context) =>
PointerType(objCObjectType).getCType(context);
@override
String getDartType(Context context) =>
isObjCImport ? '${context.libs.prefix(objcPkgImport)}.$name' : name;
@override
String getNativeType({String varName = ''}) => 'id $varName';
@override
String getObjCBlockSignatureType(Context context) => getDartType(context);
@override
bool get sameFfiDartAndCType => true;
@override
bool get sameDartAndCType => false;
@override
bool get sameDartAndFfiDartType => false;
@override
String convertDartTypeToFfiDartType(
Context context,
String value, {
required bool objCRetain,
required bool objCAutorelease,
}) => ObjCInterface.generateGetId(value, objCRetain, objCAutorelease);
static String generateGetId(
String value,
bool objCRetain,
bool objCAutorelease,
) => objCRetain
? (objCAutorelease
? '$value.ref.retainAndAutorelease()'
: '$value.ref.retainAndReturnPointer()')
: (objCAutorelease ? '$value.ref.autorelease()' : '$value.ref.pointer');
@override
String convertFfiDartTypeToDartType(
Context context,
String value, {
required bool objCRetain,
String? objCEnclosingClass,
}) => ObjCInterface.generateConstructor(
getDartType(context),
value,
objCRetain,
);
static String generateConstructor(
String className,
String value,
bool objCRetain,
) {
final ownershipFlags = 'retain: $objCRetain, release: true';
return '$className.fromPointer($value, $ownershipFlags)';
}
@override
String? generateRetain(String value) =>
'(__bridge id)(__bridge_retained void*)($value)';
@override
void visit(Visitation visitation) => visitation.visitObjCInterface(this);
// Set typeGraphOnly to true to skip iterating methods and other children, and
// just iterate the DAG of interfaces, categories, and protocols. This is
// useful for visitors that need to ensure super types are visited first.
@override
void visitChildren(Visitor visitor, {bool typeGraphOnly = false}) {
if (!typeGraphOnly) {
super.visitChildren(visitor);
visitor.visit(classObject);
visitor.visit(_isKindOfClass);
visitor.visit(_isKindOfClassMsgSend);
visitMethods(visitor);
visitor.visit(objcPkgImport);
// In the type DAG, categories link to their parent interface, not the
// other way around. So don't iterate these categories as part of the DAG.
visitor.visitAll(categories);
}
visitor.visit(superType);
visitor.visitAll(protocols);
// Note: Don't visit subtypes here, because they shouldn't affect transitive
// inclusion. Including an interface shouldn't auto-include all its
// subtypes, even as stubs.
}
@override
bool isSupertypeOf(Type other) {
other = other.typealiasType;
if (other is ObjCInterface) {
for (ObjCInterface? t = other; t != null; t = t.superType) {
if (t == this) return true;
}
}
return false;
}
}