Skip to content

Commit 6fe6fa3

Browse files
authored
[ffigen] Move mutable globals to Context (#2452)
1 parent 8ce7899 commit 6fe6fa3

File tree

92 files changed

+1103
-801
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+1103
-801
lines changed

pkgs/ffigen/lib/src/code_generator/library.dart

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,30 @@
44

55
import 'dart:io';
66

7-
import 'package:logging/logging.dart';
87
import 'package:yaml_edit/yaml_edit.dart';
98

109
import '../code_generator.dart';
1110
import '../code_generator/utils.dart';
1211
import '../config_provider/config.dart' show FfiGen;
1312
import '../config_provider/config_types.dart';
13+
import '../context.dart';
1414

1515
import 'writer.dart';
1616

17-
final _logger = Logger('ffigen.library');
18-
1917
/// Container for all Bindings.
2018
class Library {
2119
/// List of bindings in this library.
2220
final List<Binding> bindings;
2321

2422
final Writer writer;
23+
final Context context;
2524

26-
Library._(this.bindings, this.writer);
25+
Library._(this.bindings, this.writer, this.context);
2726

2827
static Library fromConfig({
2928
required FfiGen config,
3029
required List<Binding> bindings,
30+
required Context context,
3131
}) => Library(
3232
name: config.wrapperName,
3333
description: config.wrapperDocComment,
@@ -39,6 +39,7 @@ class Library {
3939
nativeEntryPoints: config.entryPoints
4040
.map((uri) => uri.toFilePath())
4141
.toList(),
42+
context: context,
4243
);
4344

4445
factory Library({
@@ -50,6 +51,7 @@ class Library {
5051
List<LibraryImport> libraryImports = const <LibraryImport>[],
5152
bool silenceEnumWarning = false,
5253
List<String> nativeEntryPoints = const <String>[],
54+
required Context context,
5355
}) {
5456
// Seperate bindings which require lookup.
5557
final lookupBindings = <LookUpBinding>[];
@@ -84,9 +86,10 @@ class Library {
8486
generateForPackageObjectiveC: generateForPackageObjectiveC,
8587
silenceEnumWarning: silenceEnumWarning,
8688
nativeEntryPoints: nativeEntryPoints,
89+
context: context,
8790
);
8891

89-
return Library._(bindings, writer);
92+
return Library._(bindings, writer, context);
9093
}
9194

9295
/// Generates [file] by generating C bindings.
@@ -102,7 +105,9 @@ class Library {
102105
file.absolute.path,
103106
], workingDirectory: file.parent.absolute.path);
104107
if (result.exitCode != 0) {
105-
_logger.severe('Formatting failed\n${result.stdout}\n${result.stderr}');
108+
context.logger.severe(
109+
'Formatting failed\n${result.stdout}\n${result.stderr}',
110+
);
106111
}
107112
}
108113
}

pkgs/ffigen/lib/src/code_generator/objc_block.dart

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,27 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import '../code_generator.dart';
6-
import '../header_parser/data.dart' show bindingsIndex;
6+
7+
import '../context.dart';
78
import '../visitor/ast.dart';
89

910
import 'binding_string.dart';
1011
import 'unique_namer.dart';
1112
import 'writer.dart';
1213

1314
class ObjCBlock extends BindingType {
14-
final ObjCBuiltInFunctions builtInFunctions;
15+
final Context context;
1516
final Type returnType;
1617
final List<Parameter> params;
1718
final bool returnsRetained;
1819
ObjCBlockWrapperFuncs? _blockWrappers;
1920
ObjCProtocolMethodTrampoline? protocolTrampoline;
2021

21-
factory ObjCBlock({
22+
factory ObjCBlock(
23+
Context context, {
2224
required Type returnType,
2325
required List<Parameter> params,
2426
required bool returnsRetained,
25-
required ObjCBuiltInFunctions builtInFunctions,
2627
}) {
2728
final renamedParams = [
2829
for (var i = 0; i < params.length; ++i)
@@ -35,39 +36,40 @@ class ObjCBlock extends BindingType {
3536

3637
final usr = _getBlockUsr(returnType, renamedParams, returnsRetained);
3738

38-
final oldBlock = bindingsIndex.getSeenObjCBlock(usr);
39+
final oldBlock = context.bindingsIndex.getSeenObjCBlock(usr);
3940
if (oldBlock != null) {
4041
return oldBlock;
4142
}
4243

4344
final block = ObjCBlock._(
45+
context,
4446
usr: usr,
4547
name: _getBlockName(returnType, renamedParams.map((a) => a.type)),
4648
returnType: returnType,
4749
params: renamedParams,
4850
returnsRetained: returnsRetained,
49-
builtInFunctions: builtInFunctions,
5051
);
51-
bindingsIndex.addObjCBlockToSeen(usr, block);
52+
context.bindingsIndex.addObjCBlockToSeen(usr, block);
5253

5354
return block;
5455
}
5556

56-
ObjCBlock._({
57+
ObjCBlock._(
58+
this.context, {
5759
required String super.usr,
5860
required super.name,
5961
required this.returnType,
6062
required this.params,
6163
required this.returnsRetained,
62-
required this.builtInFunctions,
6364
}) : super(originalName: name) {
6465
if (hasListener) {
65-
_blockWrappers = builtInFunctions.getBlockTrampolines(this);
66+
_blockWrappers = context.objCBuiltInFunctions.getBlockTrampolines(this);
6667
}
6768
}
6869

6970
void fillProtocolTrampoline() {
70-
protocolTrampoline ??= builtInFunctions.getProtocolMethodTrampoline(this);
71+
protocolTrampoline ??= context.objCBuiltInFunctions
72+
.getProtocolMethodTrampoline(this);
7173
}
7274

7375
// Generates a human readable name for the block based on the args and return

pkgs/ffigen/lib/src/code_generator/objc_category.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import '../code_generator.dart';
6+
import '../context.dart';
67
import '../visitor/ast.dart';
78

89
import 'binding_string.dart';
910
import 'utils.dart';
1011
import 'writer.dart';
1112

1213
class ObjCCategory extends NoLookUpBinding with ObjCMethods {
14+
@override
15+
final Context context;
1316
final ObjCInterface parent;
1417
final ObjCInternalGlobal classObject;
1518

@@ -21,7 +24,7 @@ class ObjCCategory extends NoLookUpBinding with ObjCMethods {
2124
String? name,
2225
required this.parent,
2326
super.dartDoc,
24-
required this.builtInFunctions,
27+
required this.context,
2528
}) : classObject = parent.classObject,
2629
super(name: name ?? originalName);
2730

@@ -35,10 +38,8 @@ class ObjCCategory extends NoLookUpBinding with ObjCMethods {
3538
}
3639

3740
@override
38-
bool get isObjCImport => builtInFunctions.isBuiltInCategory(originalName);
39-
40-
@override
41-
final ObjCBuiltInFunctions builtInFunctions;
41+
bool get isObjCImport =>
42+
context.objCBuiltInFunctions.isBuiltInCategory(originalName);
4243

4344
@override
4445
void sort() => sortMethods();

pkgs/ffigen/lib/src/code_generator/objc_interface.dart

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import '../code_generator.dart';
6+
import '../context.dart';
67
import '../header_parser/sub_parsers/api_availability.dart';
78
import '../visitor/ast.dart';
89

@@ -11,6 +12,8 @@ import 'utils.dart';
1112
import 'writer.dart';
1213

1314
class ObjCInterface extends BindingType with ObjCMethods {
15+
@override
16+
final Context context;
1417
ObjCInterface? superType;
1518
bool filled = false;
1619

@@ -23,9 +26,6 @@ class ObjCInterface extends BindingType with ObjCMethods {
2326
final subtypes = <ObjCInterface>[];
2427
final ApiAvailability apiAvailability;
2528

26-
@override
27-
final ObjCBuiltInFunctions builtInFunctions;
28-
2929
// Filled by ListBindingsVisitation.
3030
bool generateAsStub = false;
3131

@@ -35,27 +35,34 @@ class ObjCInterface extends BindingType with ObjCMethods {
3535
String? name,
3636
String? lookupName,
3737
super.dartDoc,
38-
required this.builtInFunctions,
3938
required this.apiAvailability,
39+
required this.context,
4040
}) : lookupName = lookupName ?? originalName,
4141
super(
4242
name:
43-
builtInFunctions.getBuiltInInterfaceName(originalName) ??
43+
context.objCBuiltInFunctions.getBuiltInInterfaceName(
44+
originalName,
45+
) ??
4446
name ??
4547
originalName,
4648
) {
4749
classObject = ObjCInternalGlobal(
4850
'_class_$originalName',
4951
(Writer w) => '${ObjCBuiltInFunctions.getClass.gen(w)}("$lookupName")',
5052
);
51-
_isKindOfClass = builtInFunctions.getSelObject('isKindOfClass:');
52-
_isKindOfClassMsgSend = builtInFunctions.getMsgSendFunc(BooleanType(), [
53-
Parameter(
54-
name: 'clazz',
55-
type: PointerType(objCObjectType),
56-
objCConsumed: false,
57-
),
58-
]);
53+
_isKindOfClass = context.objCBuiltInFunctions.getSelObject(
54+
'isKindOfClass:',
55+
);
56+
_isKindOfClassMsgSend = context.objCBuiltInFunctions.getMsgSendFunc(
57+
BooleanType(),
58+
[
59+
Parameter(
60+
name: 'clazz',
61+
type: PointerType(objCObjectType),
62+
objCConsumed: false,
63+
),
64+
],
65+
);
5966
}
6067

6168
void addProtocol(ObjCProtocol? proto) {
@@ -64,7 +71,8 @@ class ObjCInterface extends BindingType with ObjCMethods {
6471

6572
@override
6673
bool get isObjCImport =>
67-
builtInFunctions.getBuiltInInterfaceName(originalName) != null;
74+
context.objCBuiltInFunctions.getBuiltInInterfaceName(originalName) !=
75+
null;
6876

6977
@override
7078
void sort() => sortMethods();

pkgs/ffigen/lib/src/code_generator/objc_methods.dart

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@
44

55
import 'dart:collection';
66

7-
import 'package:logging/logging.dart';
8-
9-
import '../code_generator.dart';
7+
import '../context.dart';
108
import '../header_parser/sub_parsers/api_availability.dart';
119
import '../visitor/ast.dart';
12-
10+
import 'func.dart';
11+
import 'imports.dart';
12+
import 'native_type.dart';
13+
import 'objc_block.dart';
14+
import 'objc_built_in_functions.dart';
15+
import 'objc_interface.dart';
16+
import 'objc_nullable.dart';
17+
import 'pointer.dart';
18+
import 'type.dart';
19+
import 'typealias.dart';
1320
import 'unique_namer.dart';
1421
import 'utils.dart';
1522
import 'writer.dart';
1623

17-
final _logger = Logger('ffigen.code_generator.objc_methods');
18-
1924
mixin ObjCMethods {
2025
Map<String, ObjCMethod> _methods = <String, ObjCMethod>{};
2126
List<String> _order = <String>[];
@@ -26,7 +31,7 @@ mixin ObjCMethods {
2631

2732
String get originalName;
2833
String get name;
29-
ObjCBuiltInFunctions get builtInFunctions;
34+
Context get context;
3035

3136
void addMethod(ObjCMethod? method) {
3237
if (method == null) return;
@@ -67,7 +72,7 @@ mixin ObjCMethods {
6772

6873
// Check the duplicate is the same method.
6974
if (!newMethod.sameAs(oldMethod)) {
70-
_logger.severe(
75+
context.logger.severe(
7176
'Duplicate methods with different signatures: '
7277
'$originalName.${newMethod.originalName}',
7378
);
@@ -189,7 +194,7 @@ class ObjCProperty extends AstNode {
189194
}
190195

191196
class ObjCMethod extends AstNode {
192-
final ObjCBuiltInFunctions builtInFunctions;
197+
final Context context;
193198
final String? dartDoc;
194199
final String originalName;
195200
String name;
@@ -221,7 +226,7 @@ class ObjCMethod extends AstNode {
221226
}
222227

223228
ObjCMethod({
224-
required this.builtInFunctions,
229+
required this.context,
225230
required this.originalName,
226231
required this.name,
227232
this.property,
@@ -234,7 +239,7 @@ class ObjCMethod extends AstNode {
234239
required this.apiAvailability,
235240
List<Parameter>? params_,
236241
}) : params = params_ ?? [],
237-
selObject = builtInFunctions.getSelObject(originalName);
242+
selObject = context.objCBuiltInFunctions.getSelObject(originalName);
238243

239244
// Must be called after all params are added to the method.
240245
void finalizeParams() {
@@ -280,19 +285,19 @@ class ObjCMethod extends AstNode {
280285
bool get isInstanceMethod => !isClassMethod;
281286

282287
void fillMsgSend() {
283-
msgSend ??= builtInFunctions.getMsgSendFunc(returnType, params);
288+
msgSend ??= context.objCBuiltInFunctions.getMsgSendFunc(returnType, params);
284289
}
285290

286291
void fillProtocolBlock() {
287292
protocolBlock ??= ObjCBlock(
293+
context,
288294
returnType: returnType,
289295
params: [
290296
// First arg of the protocol block is a void pointer that we ignore.
291297
Parameter(name: '_', type: PointerType(voidType), objCConsumed: false),
292298
...params,
293299
],
294300
returnsRetained: returnsRetained,
295-
builtInFunctions: builtInFunctions,
296301
)..fillProtocolTrampoline();
297302
}
298303

0 commit comments

Comments
 (0)