Skip to content

Commit 3f8c5db

Browse files
committed
Merge branch 'main' into ffigen_lib_refactor
2 parents 3fd5cdf + 41330aa commit 3f8c5db

31 files changed

+1212
-132
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2025, 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+
// dart format width=76
6+
7+
// snippet-start
8+
import 'dart:ffi';
9+
10+
void main() {
11+
final result = add(14, 28);
12+
print(result);
13+
}
14+
15+
@Native<Int Function(Int, Int)>(assetId: 'package:my_package/add.dart')
16+
external int add(int a, int b);
17+
18+
// snippet-end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2025, 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+
// snippet-start
6+
import 'package:code_assets/code_assets.dart';
7+
import 'package:hooks/hooks.dart';
8+
9+
void main(List<String> args) async {
10+
await build(args, (input, output) async {
11+
final packageName = input.packageName;
12+
final assetPath = input.outputDirectory.resolve('...');
13+
14+
output.assets.code.add(
15+
CodeAsset(
16+
package: packageName,
17+
name: '...',
18+
linkMode: DynamicLoadingBundled(),
19+
file: assetPath,
20+
),
21+
);
22+
});
23+
}
24+
25+
// snippet-end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2025, 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+
// dart format width=76
6+
7+
// snippet-start
8+
import 'package:code_assets/code_assets.dart';
9+
import 'package:test/test.dart';
10+
11+
void main() {
12+
test('test my build hook', () async {
13+
await testCodeBuildHook(
14+
mainMethod: (args) {},
15+
check: (input, output) {},
16+
);
17+
});
18+
}
19+
20+
// snippet-end

pkgs/code_assets/lib/code_assets.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
///
1313
/// Code assets can be added in a build hook as follows:
1414
///
15+
/// <!-- file://./../example/api/code_assets_snippet.dart -->
1516
/// ```dart
1617
/// import 'package:code_assets/code_assets.dart';
1718
/// import 'package:hooks/hooks.dart';

pkgs/code_assets/lib/src/code_assets/code_asset.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import 'syntax.g.dart';
1919
/// Native code assets can be accessed at runtime through native external
2020
/// functions via their asset [id]:
2121
///
22+
/// <!-- file://./../../../example/api/code_asset_snippet.dart -->
2223
/// ```dart
2324
/// import 'dart:ffi';
2425
///

pkgs/code_assets/lib/src/code_assets/testing.dart

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,19 @@ import 'os.dart';
2121
///
2222
/// This is intended to be used from tests, e.g.:
2323
///
24-
/// ```
25-
/// test('test my build hook', () async {
26-
/// await testCodeBuildHook(
27-
/// ...
28-
/// );
29-
/// });
24+
/// <!-- file://./../../../example/api/test_snippet.dart -->
25+
/// ```dart
26+
/// import 'package:code_assets/code_assets.dart';
27+
/// import 'package:test/test.dart';
28+
///
29+
/// void main() {
30+
/// test('test my build hook', () async {
31+
/// await testCodeBuildHook(
32+
/// mainMethod: (args) {},
33+
/// check: (input, output) {},
34+
/// );
35+
/// });
36+
/// }
3037
/// ```
3138
///
3239
/// The hook is run in isolation. No user-defines are read from the pubspec,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2025, 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+
// dart format width=76
6+
7+
// snippet-start
8+
import 'package:data_assets/data_assets.dart';
9+
import 'package:hooks/hooks.dart';
10+
11+
void main(List<String> args) async {
12+
await build(args, (input, output) async {
13+
final packageName = input.packageName;
14+
final assetPath = input.outputDirectory.resolve('...');
15+
16+
output.assets.data.add(
17+
DataAsset(package: packageName, name: '...', file: assetPath),
18+
);
19+
});
20+
}
21+
22+
// snippet-end

pkgs/data_assets/lib/data_assets.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
///
1313
/// Data assets can be added in a build hook as follows:
1414
///
15+
/// <!-- file://./../example/api/data_assets_snippet.dart -->
1516
/// ```dart
1617
/// import 'package:data_assets/data_assets.dart';
1718
/// import 'package:hooks/hooks.dart';
@@ -22,11 +23,7 @@
2223
/// final assetPath = input.outputDirectory.resolve('...');
2324
///
2425
/// output.assets.data.add(
25-
/// DataAsset(
26-
/// package: packageName,
27-
/// name: '...',
28-
/// file: assetPath,
29-
/// ),
26+
/// DataAsset(package: packageName, name: '...', file: assetPath),
3027
/// );
3128
/// });
3229
/// }

pkgs/ffigen/test/large_integration_tests/large_objc_test.dart

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,13 @@ void main() {
3939
bool randInclude(String kind, Declaration clazz, [String? method]) =>
4040
fnvHash32('$seed.$kind.${clazz.usr}.$method') <
4141
((1 << 32) * inclusionRatio);
42-
DeclarationFilters randomFilter(String kind) => DeclarationFilters(
43-
shouldInclude: (Declaration clazz) => randInclude(kind, clazz),
42+
DeclarationFilters randomFilter(
43+
String kind, [
44+
Set<String> forceIncludes = const {},
45+
]) => DeclarationFilters(
46+
shouldInclude: (Declaration clazz) =>
47+
forceIncludes.contains(clazz.originalName) ||
48+
randInclude(kind, clazz),
4449
shouldIncludeMember: (Declaration clazz, String method) =>
4550
randInclude('$kind.memb', clazz, method),
4651
);
@@ -57,6 +62,10 @@ void main() {
5762
'large_integration_tests',
5863
'large_objc_bindings.m',
5964
);
65+
66+
// TODO(https://github.com/dart-lang/native/issues/2517): Remove this.
67+
const forceIncludedProtocols = {'NSTextLocation'};
68+
6069
final config = FfiGen(
6170
Logger.root,
6271
wrapperName: 'LargeObjCLibrary',
@@ -85,7 +94,7 @@ void main() {
8594
globals: randomFilter('globals'),
8695
typedefs: randomFilter('typedefs'),
8796
objcInterfaces: randomFilter('objcInterfaces'),
88-
objcProtocols: randomFilter('objcProtocols'),
97+
objcProtocols: randomFilter('objcProtocols', forceIncludedProtocols),
8998
objcCategories: randomFilter('objcCategories'),
9099
externalVersions: ExternalVersions(
91100
ios: Versions(min: Version(12, 0, 0)),

pkgs/hooks/CONTRIBUTING.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## Dartdoc Comments Code Blocks
2+
3+
Every code block must have a language.
4+
5+
Every Dart code block must have an HTML comment pointing to its source.
6+
7+
Correct:
8+
9+
```
10+
<!-- file://./CONTRIBUTING.md -->
11+
```dart
12+
void main() {}
13+
```
14+
```
15+
16+
```
17+
```yaml
18+
foo: bar
19+
```
20+
```
21+
22+
Incorrect:
23+
24+
```
25+
```
26+
void main() {}
27+
```
28+
```
29+
30+
```
31+
```dart
32+
void main() {}
33+
```
34+
```
35+
36+
This is enforced by `pkgs/hooks/tool/update_snippets.dart`.

0 commit comments

Comments
 (0)