Skip to content

Commit 9460b0d

Browse files
authored
[ffigen] Remove NativeExternalBindings.wrapperName (#2832)
1 parent 579b84d commit 9460b0d

20 files changed

+232
-305
lines changed

pkgs/ffigen/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 21.0.0
2+
3+
- __Breaking change__: Remove deprecated `wrapperName` field from
4+
`NativeExternalBindings`.
5+
16
## 20.1.1
27

38
- Update tests and examples now that package:objective_c is using native assets.

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class Library {
2727
required List<Binding> bindings,
2828
required Context context,
2929
}) => Library(
30-
name: context.config.wrapperName,
3130
description: context.config.wrapperDocComment,
3231
bindings: bindings,
3332
header: context.config.preamble,
@@ -43,7 +42,6 @@ class Library {
4342
);
4443

4544
factory Library({
46-
required String name,
4745
String? description,
4846
required List<Binding> bindings,
4947
String? header,

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:path/path.dart' as path;
6+
57
import '../code_generator.dart';
8+
import '../config_provider.dart';
69
import '../context.dart';
710
import '../visitor/ast.dart';
811

@@ -14,15 +17,12 @@ import 'writer.dart';
1417

1518
/// Built in functions used by the Objective C bindings.
1619
class ObjCBuiltInFunctions {
17-
ObjCBuiltInFunctions(
18-
this.context,
19-
this.wrapperName,
20-
this.generateForPackageObjectiveC,
21-
);
20+
ObjCBuiltInFunctions(this.context, this.generateForPackageObjectiveC)
21+
: libraryId = _libraryIdFromConfigHash(context.config);
2222

2323
final Context context;
24-
final String wrapperName;
2524
final bool generateForPackageObjectiveC;
25+
final String libraryId;
2626

2727
static const registerName = ObjCImport('registerName');
2828
static const getClass = ObjCImport('getClass');
@@ -149,9 +149,9 @@ class ObjCBuiltInFunctions {
149149
ObjCBlockWrapperFuncs? getBlockTrampolines(ObjCBlock block) {
150150
final (id, idHash) = _methodSigId(block.returnType, block.params);
151151
return _blockTrampolines[id] ??= ObjCBlockWrapperFuncs(
152-
_blockTrampolineFunc('_${wrapperName}_wrapListenerBlock_$idHash'),
152+
_blockTrampolineFunc('_${libraryId}_wrapListenerBlock_$idHash'),
153153
_blockTrampolineFunc(
154-
'_${wrapperName}_wrapBlockingBlock_$idHash',
154+
'_${libraryId}_wrapBlockingBlock_$idHash',
155155
blocking: true,
156156
),
157157
);
@@ -191,7 +191,7 @@ class ObjCBuiltInFunctions {
191191
final (id, idHash) = _methodSigId(block.returnType, block.params);
192192
return _protocolTrampolines[id] ??= ObjCProtocolMethodTrampoline(
193193
Func(
194-
name: '_${wrapperName}_protocolTrampoline_$idHash',
194+
name: '_${libraryId}_protocolTrampoline_$idHash',
195195
returnType: block.returnType,
196196
parameters: [
197197
Parameter(
@@ -215,6 +215,16 @@ class ObjCBuiltInFunctions {
215215
final baseType = type.typealiasType;
216216
return baseType is ObjCNullable && baseType.child is ObjCInstanceType;
217217
}
218+
219+
// A unique (but not human readable) ID for the generated library based on
220+
// a hash of parts of the config.
221+
static String _libraryIdFromConfigHash(Config config) => fnvHash32(
222+
[
223+
...config.entryPoints,
224+
config.output,
225+
config.outputObjC,
226+
].map((uri) => path.basename(uri.toFilePath())).join('\n'),
227+
).toRadixString(36);
218228
}
219229

220230
/// A native trampoline function for a listener block.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,11 @@ ${generateInstanceMethodBindings(w, this)}
329329
BindingString? toObjCBindingString(Writer w) {
330330
if (generateAsStub) return null;
331331

332-
final wrapName = context.objCBuiltInFunctions.wrapperName;
332+
final libraryId = context.objCBuiltInFunctions.libraryId;
333333
final mainString =
334334
'''
335335
336-
Protocol* _${wrapName}_$originalName(void) { return @protocol($originalName); }
336+
Protocol* _${libraryId}_$originalName(void) { return @protocol($originalName); }
337337
''';
338338

339339
return BindingString(

pkgs/ffigen/lib/src/config_provider/config.dart

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -709,16 +709,7 @@ final class NativeExternalBindings implements BindingStyle {
709709
/// If omitted, it will not be generated.
710710
final String? assetId;
711711

712-
/// The prefix for the generated Objective-C functions.
713-
// TODO(https://github.com/dart-lang/native/issues/2580): Remove this.
714-
@Deprecated('Will be replaced by a hash.')
715-
final String wrapperName;
716-
717-
const NativeExternalBindings({
718-
this.assetId,
719-
@Deprecated('Will be replaced by a hash.')
720-
this.wrapperName = 'NativeLibrary',
721-
});
712+
const NativeExternalBindings({this.assetId});
722713
}
723714

724715
/// Generate bindings which take a [DynamicLibrary] or [DynamicLibrary.lookup]
@@ -764,11 +755,8 @@ extension type Config(FfiGenerator ffiGen) implements FfiGenerator {
764755
bool get useDartHandle => ffiGen.useDartHandle;
765756
// ignore: deprecated_member_use_from_same_package
766757
Map<String, ImportedType> get importedTypesByUsr => ffiGen.importedTypesByUsr;
767-
String get wrapperName => switch (ffiGen.output.style) {
768-
final DynamicLibraryBindings e => e.wrapperName,
769-
// ignore: deprecated_member_use_from_same_package
770-
final NativeExternalBindings e => e.wrapperName,
771-
};
758+
String get wrapperName =>
759+
(ffiGen.output.style as DynamicLibraryBindings).wrapperName;
772760

773761
String? get wrapperDocComment => switch (ffiGen.output.style) {
774762
final DynamicLibraryBindings e => e.wrapperDocComment,

pkgs/ffigen/lib/src/config_provider/yaml_config.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,11 +1252,7 @@ final class YamlConfig {
12521252
preamble: preamble,
12531253
format: formatOutput,
12541254
style: ffiNativeConfig.enabled
1255-
? NativeExternalBindings(
1256-
assetId: ffiNativeConfig.assetId,
1257-
// ignore: deprecated_member_use_from_same_package
1258-
wrapperName: wrapperName,
1259-
)
1255+
? NativeExternalBindings(assetId: ffiNativeConfig.assetId)
12601256
: DynamicLibraryBindings(
12611257
wrapperName: wrapperName,
12621258
wrapperDocComment: wrapperDocComment,

pkgs/ffigen/lib/src/context.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class Context {
3636
cursorIndex = CursorIndex(logger) {
3737
objCBuiltInFunctions = ObjCBuiltInFunctions(
3838
this,
39-
config.wrapperName,
4039
// ignore: deprecated_member_use_from_same_package
4140
generator.objectiveC?.generateForPackageObjectiveC ?? false,
4241
);

pkgs/ffigen/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# BSD-style license that can be found in the LICENSE file.
44

55
name: ffigen
6-
version: 20.1.1
6+
version: 21.0.0-wip
77
description: >
88
Generator for FFI bindings, using LibClang to parse C, Objective-C, and Swift
99
files.

pkgs/ffigen/test/code_generator_tests/code_generator_test.dart

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ void main() {
5959
);
6060
final library = Library(
6161
context: nativeContext,
62-
name: 'Bindings',
6362
header: licenseHeader,
6463
bindings: transformBindings([
6564
Func(
@@ -131,7 +130,6 @@ void main() {
131130
final context = makeContext();
132131
final library = Library(
133132
context: context,
134-
name: 'Bindings',
135133
header: licenseHeader,
136134
bindings: transformBindings([
137135
Struct(
@@ -218,7 +216,6 @@ void main() {
218216
);
219217
final library = Library(
220218
context: context,
221-
name: 'Bindings',
222219
header: licenseHeader,
223220
bindings: transformBindings([
224221
structSome,
@@ -259,7 +256,6 @@ void main() {
259256

260257
final library = Library(
261258
context: nativeContext,
262-
name: 'Bindings',
263259
header: licenseHeader,
264260
bindings: transformBindings([
265261
Global(
@@ -304,7 +300,6 @@ void main() {
304300
final context = makeContext();
305301
final library = Library(
306302
context: context,
307-
name: 'Bindings',
308303
header: '$licenseHeader\n',
309304
bindings: transformBindings([
310305
MacroConstant(name: 'test1', rawType: 'int', rawValue: '20'),
@@ -318,7 +313,6 @@ void main() {
318313
final context = makeContext();
319314
final library = Library(
320315
context: context,
321-
name: 'Bindings',
322316
header: '$licenseHeader\n',
323317
bindings: transformBindings([
324318
EnumClass(
@@ -339,7 +333,6 @@ void main() {
339333
final context = makeContext();
340334
final library = Library(
341335
context: context,
342-
name: 'Bindings',
343336
header: '$licenseHeader\n',
344337
bindings: transformBindings([
345338
EnumClass(
@@ -392,7 +385,6 @@ void main() {
392385
);
393386
final library = Library(
394387
context: context,
395-
name: 'Bindings',
396388
header: '$licenseHeader\n',
397389
silenceEnumWarning: true,
398390
bindings: transformBindings([
@@ -477,7 +469,6 @@ void main() {
477469
);
478470
final lib = Library(
479471
context: context,
480-
name: 'Bindings',
481472
header: '$licenseHeader\n',
482473
silenceEnumWarning: true,
483474
bindings: transformBindings([
@@ -502,7 +493,6 @@ void main() {
502493
);
503494
final library = Library(
504495
context: context,
505-
name: 'init_dylib',
506496
header: '$licenseHeader\n// ignore_for_file: unused_element\n',
507497
bindings: transformBindings([
508498
Func(
@@ -558,7 +548,6 @@ void main() {
558548
);
559549
final library = Library(
560550
context: context,
561-
name: 'init_dylib',
562551
header: '$licenseHeader\n',
563552
bindings: transformBindings([
564553
Func(
@@ -583,7 +572,6 @@ void main() {
583572
final context = makeContext();
584573
final library = Library(
585574
context: context,
586-
name: 'Bindings',
587575
header: licenseHeader,
588576
bindings: transformBindings([
589577
Func(
@@ -612,7 +600,6 @@ void main() {
612600
final context = makeContext();
613601
final library = Library(
614602
context: context,
615-
name: 'Bindings',
616603
header: licenseHeader,
617604
bindings: transformBindings([
618605
Struct(
@@ -700,7 +687,6 @@ void main() {
700687
);
701688
final library = Library(
702689
context: context,
703-
name: 'Bindings',
704690
header: licenseHeader,
705691
bindings: transformBindings([
706692
struct1,
@@ -766,7 +752,6 @@ void main() {
766752
final struct3Typealias = Typealias(name: 'Struct3Typealias', type: struct3);
767753
final library = Library(
768754
context: context,
769-
name: 'Bindings',
770755
header: '$licenseHeader\n',
771756
bindings: transformBindings([
772757
Typealias(

pkgs/ffigen/test/collision_tests/decl_decl_collision_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ void main() {
2727
);
2828
final library = Library(
2929
context: context,
30-
name: 'Bindings',
3130
bindings: transformBindings([
3231
Struct(context: context, name: 'TestStruct'),
3332
Struct(context: context, name: 'TestStruct'),

0 commit comments

Comments
 (0)