Skip to content

Commit dbe57a1

Browse files
authored
[native_assets_cli] Add example using dart_api_dl.h (#858)
Example showing how to use `dart_api_dl.h` in native assets. Does **not** address yet: * #839
1 parent 01f3e06 commit dbe57a1

22 files changed

+5665
-2
lines changed

.github/workflows/native.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ jobs:
9292
- run: dart pub get -C example/native_add_library/
9393
if: ${{ matrix.package == 'native_assets_cli' }}
9494

95+
- run: dart pub get -C example/use_dart_api/
96+
if: ${{ matrix.package == 'native_assets_cli' }}
97+
9598
- run: dart analyze --fatal-infos
9699
# Run on dev to ensure we're not depending on deprecated SDK things.
97100

@@ -121,6 +124,10 @@ jobs:
121124
working-directory: pkgs/${{ matrix.package }}/example/native_add_app/bin/native_add_app/
122125
if: ${{ matrix.package == 'native_assets_cli' && matrix.sdk == 'dev' && !matrix.breaking-change }}
123126

127+
- run: dart --enable-experiment=native-assets test
128+
working-directory: pkgs/${{ matrix.package }}/example/use_dart_api/
129+
if: ${{ matrix.package == 'native_assets_cli' && matrix.sdk == 'dev' && !matrix.breaking-change }}
130+
124131
- name: Install coverage
125132
run: dart pub global activate coverage
126133
if: ${{ matrix.sdk == 'stable' && matrix.dependencies == 'published' }}

pkgs/native_assets_cli/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.3.3-wip
2+
3+
- Added [example/use_dart_api/](example/use_dart_api/) detailing how to use
4+
`dart_api_dl.h` from the Dart SDK in native code.
5+
16
## 0.3.2
27

38
- Fixed an issue where `Depenendencies.dependencies` could not be
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
An example that uses the [C API of the Dart VM].
2+
3+
The example shows how to pass an object from the Dart heap to native code and
4+
hold on to it via a `PersistentHandle`. For more documentation about handles,
5+
and the other C API features refer to the documentation in the header files.
6+
7+
## Usage
8+
9+
Run tests with `dart --enable-experiment=native-assets test`.
10+
11+
## Open TODOs
12+
13+
It would be better to receive a path the include folder containing the C API
14+
rather than having to copy it into the `src/` directory.
15+
16+
* https://github.com/dart-lang/native/issues/839
17+
18+
[C API of the Dart VM]: https://github.com/dart-lang/sdk/tree/main/runtime/include
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2023, 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:logging/logging.dart';
6+
import 'package:native_assets_cli/native_assets_cli.dart';
7+
import 'package:native_toolchain_c/native_toolchain_c.dart';
8+
9+
const packageName = 'use_dart_api';
10+
11+
void main(List<String> args) async {
12+
final buildConfig = await BuildConfig.fromArgs(args);
13+
final buildOutput = BuildOutput();
14+
final cbuilder = CBuilder.library(
15+
name: packageName,
16+
assetId: 'package:$packageName/src/${packageName}_bindings_generated.dart',
17+
sources: [
18+
'src/$packageName.c',
19+
'src/dart_api_dl.c',
20+
],
21+
);
22+
await cbuilder.run(
23+
buildConfig: buildConfig,
24+
buildOutput: buildOutput,
25+
logger: Logger('')
26+
..level = Level.ALL
27+
..onRecord.listen((record) {
28+
print('${record.level.name}: ${record.time}: ${record.message}');
29+
}),
30+
);
31+
await buildOutput.writeToFile(outDir: buildConfig.outDir);
32+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Run with `flutter pub run ffigen --config ffigen.yaml`.
2+
name: NativeAddBindings
3+
description: |
4+
Bindings for `src/use_dart_api.h`.
5+
6+
Regenerate bindings with `flutter pub run ffigen --config ffigen.yaml`.
7+
output: "lib/src/use_dart_api_bindings_generated.dart"
8+
headers:
9+
entry-points:
10+
- "src/use_dart_api.h"
11+
include-directives:
12+
- "src/use_dart_api.h"
13+
preamble: |
14+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
15+
// for details. All rights reserved. Use of this source code is governed by a
16+
// BSD-style license that can be found in the LICENSE file.
17+
comments:
18+
style: any
19+
length: full
20+
ffi-native:
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2023, 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+
// AUTO GENERATED FILE, DO NOT EDIT.
6+
//
7+
// Generated by `package:ffigen`.
8+
// ignore_for_file: type=lint
9+
import 'dart:ffi' as ffi;
10+
11+
@ffi.Native<ffi.Int32 Function(ffi.Int32, ffi.Int32)>(symbol: 'add')
12+
external int add(
13+
int a,
14+
int b,
15+
);
16+
17+
@ffi.Native<ffi.IntPtr Function(ffi.Pointer<ffi.Void>)>(symbol: 'InitDartApiDL')
18+
external int InitDartApiDL(
19+
ffi.Pointer<ffi.Void> data,
20+
);
21+
22+
@ffi.Native<ffi.Pointer<ffi.Void> Function(ffi.Handle)>(
23+
symbol: 'NewPersistentHandle')
24+
external ffi.Pointer<ffi.Void> NewPersistentHandle(
25+
Object non_persistent_handle,
26+
);
27+
28+
@ffi.Native<ffi.Handle Function(ffi.Pointer<ffi.Void>)>(
29+
symbol: 'HandleFromPersistent')
30+
external Object HandleFromPersistent(
31+
ffi.Pointer<ffi.Void> persistent_handle,
32+
);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright (c) 2023, 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+
export 'src/use_dart_api_bindings_generated.dart';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
- build.dart
2+
- ffigen.yaml
3+
- lib/native_add.dart
4+
- lib/src/native_add_bindings_generated.dart
5+
- lib/src/native_add.dart
6+
- pubspec.yaml
7+
- pubspec_overrides.yaml
8+
- src/native_add.c
9+
- src/native_add.h
10+
- test/native_add_test.dart
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: use_dart_api
2+
description: Uses some functions from `dart_api_dl.h`.
3+
version: 0.1.0
4+
5+
publish_to: none
6+
7+
environment:
8+
sdk: ">=3.0.0 <4.0.0"
9+
10+
dependencies:
11+
cli_config: ^0.1.1
12+
logging: ^1.1.1
13+
native_assets_cli: ^0.3.2
14+
native_toolchain_c: ^0.3.2
15+
16+
dev_dependencies:
17+
ffigen: ^10.0.0
18+
lints: ^3.0.0
19+
test: ^1.23.1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dependency_overrides:
2+
native_assets_cli:
3+
path: ../../../native_assets_cli/
4+
native_toolchain_c:
5+
path: ../../../native_toolchain_c/

0 commit comments

Comments
 (0)