-
Notifications
You must be signed in to change notification settings - Fork 67
[native_assets_cli] Add DartCApi
#1937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
+336
−0
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
pkgs/native_assets_builder/test/build_runner/dart_c_api_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) 2025, 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 'dart:ffi'; | ||
import 'dart:io'; | ||
|
||
import 'package:pub_semver/pub_semver.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../helpers.dart'; | ||
import 'helpers.dart'; | ||
|
||
const Timeout longTimeout = Timeout(Duration(minutes: 5)); | ||
|
||
void main() async { | ||
test('use_dart_api build', timeout: longTimeout, () async { | ||
await inTempDir((tempUri) async { | ||
await copyTestProjects(targetUri: tempUri); | ||
final packageUri = tempUri.resolve('use_dart_api/'); | ||
await runPubGet( | ||
workingDirectory: packageUri, | ||
logger: logger, | ||
); | ||
|
||
// Assume we're run from Dart SDK and try to find the include dir. | ||
final includeDirectory = dartExecutable.resolve('../include/'); | ||
final versionFile = includeDirectory.resolve('dart_version.h'); | ||
final versionContents = | ||
await File(versionFile.toFilePath()).readAsString(); | ||
final regex = RegExp( | ||
r'#define DART_API_DL_MAJOR_VERSION (\d+)\s*#define DART_API_DL_MINOR_VERSION (\d+)'); | ||
final match = regex.firstMatch(versionContents)!; | ||
final major = int.parse(match.group(1)!); | ||
final minor = int.parse(match.group(2)!); | ||
final dartCApi = DartCApi( | ||
includeDirectory: includeDirectory, | ||
version: Version(major, minor, 0), | ||
); | ||
|
||
// Run the build. | ||
final result = (await buildCodeAssets( | ||
packageUri, | ||
dartCApi: dartCApi, | ||
))!; | ||
final codeAsset = CodeAsset.fromEncoded(result.encodedAssets.single); | ||
|
||
// Check that we can load the dylib and run the init. | ||
final dylib = DynamicLibrary.open(codeAsset.file!.toFilePath()); | ||
final initDartApiDl = dylib.lookupFunction<IntPtr Function(Pointer<Void>), | ||
int Function(Pointer<Void>)>('InitDartApiDL'); | ||
final initResult = initDartApiDl(NativeApi.initializeApiDLData); | ||
expect(initResult, 0); | ||
}); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
pkgs/native_assets_builder/test_data/use_dart_api/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
An example that uses the [C API of the Dart VM]. | ||
|
||
The example shows how to pass an object from the Dart heap to native code and | ||
hold on to it via a `PersistentHandle`. For more documentation about handles, | ||
and the other C API features refer to the documentation in the header files. | ||
|
||
## Usage | ||
|
||
Run tests with `dart --enable-experiment=native-assets test`. | ||
|
||
[C API of the Dart VM]: https://github.com/dart-lang/sdk/tree/main/runtime/include |
20 changes: 20 additions & 0 deletions
20
pkgs/native_assets_builder/test_data/use_dart_api/ffigen.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Run with `flutter pub run ffigen --config ffigen.yaml`. | ||
name: NativeAddBindings | ||
description: | | ||
Bindings for `src/use_dart_api.h`. | ||
Regenerate bindings with `flutter pub run ffigen --config ffigen.yaml`. | ||
output: 'lib/src/use_dart_api_bindings_generated.dart' | ||
headers: | ||
entry-points: | ||
- 'src/use_dart_api.h' | ||
include-directives: | ||
- 'src/use_dart_api.h' | ||
preamble: | | ||
// Copyright (c) 2025, 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. | ||
comments: | ||
style: any | ||
length: full | ||
ffi-native: |
41 changes: 41 additions & 0 deletions
41
pkgs/native_assets_builder/test_data/use_dart_api/hook/build.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) 2025, 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 'package:logging/logging.dart'; | ||
import 'package:native_assets_cli/code_assets_builder.dart'; | ||
import 'package:native_assets_cli/native_assets_cli.dart'; | ||
import 'package:native_toolchain_c/native_toolchain_c.dart'; | ||
|
||
void main(List<String> arguments) async { | ||
await build(arguments, (input, output) async { | ||
final dartCApi = input.config.code.dartCApi; | ||
if (dartCApi == null) { | ||
throw UnsupportedError( | ||
'This doesn\'t work with access to the Dart C API!', | ||
); | ||
} | ||
|
||
final packageName = input.packageName; | ||
final cbuilder = CBuilder.library( | ||
name: packageName, | ||
assetName: 'src/${packageName}_bindings_generated.dart', | ||
sources: [ | ||
'src/$packageName.c', | ||
dartCApi.dartApiDlC.toFilePath(), | ||
], | ||
includes: [ | ||
dartCApi.includeDirectory.toFilePath(), | ||
], | ||
); | ||
await cbuilder.run( | ||
input: input, | ||
output: output, | ||
logger: Logger('') | ||
..level = Level.ALL | ||
..onRecord.listen((record) { | ||
print('${record.level.name}: ${record.time}: ${record.message}'); | ||
}), | ||
); | ||
}); | ||
} |
32 changes: 32 additions & 0 deletions
32
...native_assets_builder/test_data/use_dart_api/lib/src/use_dart_api_bindings_generated.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) 2025, 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. | ||
|
||
// AUTO GENERATED FILE, DO NOT EDIT. | ||
// | ||
// Generated by `package:ffigen`. | ||
// ignore_for_file: type=lint | ||
import 'dart:ffi' as ffi; | ||
|
||
@ffi.Native<ffi.Int32 Function(ffi.Int32, ffi.Int32)>(symbol: 'add') | ||
external int add( | ||
int a, | ||
int b, | ||
); | ||
|
||
@ffi.Native<ffi.IntPtr Function(ffi.Pointer<ffi.Void>)>(symbol: 'InitDartApiDL') | ||
external int InitDartApiDL( | ||
ffi.Pointer<ffi.Void> data, | ||
); | ||
|
||
@ffi.Native<ffi.Pointer<ffi.Void> Function(ffi.Handle)>( | ||
symbol: 'NewPersistentHandle') | ||
external ffi.Pointer<ffi.Void> NewPersistentHandle( | ||
Object non_persistent_handle, | ||
); | ||
|
||
@ffi.Native<ffi.Handle Function(ffi.Pointer<ffi.Void>)>( | ||
symbol: 'HandleFromPersistent') | ||
external Object HandleFromPersistent( | ||
ffi.Pointer<ffi.Void> persistent_handle, | ||
); |
5 changes: 5 additions & 0 deletions
5
pkgs/native_assets_builder/test_data/use_dart_api/lib/use_dart_api.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright (c) 2025, 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. | ||
|
||
export 'src/use_dart_api_bindings_generated.dart'; |
22 changes: 22 additions & 0 deletions
22
pkgs/native_assets_builder/test_data/use_dart_api/pubspec.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: use_dart_api | ||
description: Uses some functions from `dart_api_dl.h`. | ||
version: 0.1.0 | ||
|
||
publish_to: none | ||
|
||
environment: | ||
sdk: '>=3.3.0 <4.0.0' | ||
|
||
dependencies: | ||
logging: ^1.1.1 | ||
# native_assets_cli: ^0.11.0 | ||
native_assets_cli: | ||
path: ../../../native_assets_cli/ | ||
# native_toolchain_c: ^0.8.0 | ||
native_toolchain_c: | ||
path: ../../../native_toolchain_c/ | ||
|
||
dev_dependencies: | ||
ffigen: ^10.0.0 | ||
lints: ^3.0.0 | ||
test: ^1.23.1 |
17 changes: 17 additions & 0 deletions
17
pkgs/native_assets_builder/test_data/use_dart_api/src/use_dart_api.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) 2025, 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. | ||
|
||
#include "use_dart_api.h" | ||
|
||
int32_t add(int32_t a, int32_t b) { return a + b; } | ||
|
||
intptr_t InitDartApiDL(void *data) { return Dart_InitializeApiDL(data); } | ||
|
||
void *NewPersistentHandle(Dart_Handle non_persistent_handle) { | ||
return Dart_NewPersistentHandle_DL(non_persistent_handle); | ||
} | ||
|
||
Dart_Handle HandleFromPersistent(void *persistent_handle) { | ||
return Dart_HandleFromPersistent_DL(persistent_handle); | ||
} |
21 changes: 21 additions & 0 deletions
21
pkgs/native_assets_builder/test_data/use_dart_api/src/use_dart_api.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) 2025, 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. | ||
|
||
#include <stdint.h> | ||
|
||
#include "dart_api_dl.h" | ||
|
||
#if _WIN32 | ||
#define MYLIB_EXPORT __declspec(dllexport) | ||
#else | ||
#define MYLIB_EXPORT | ||
#endif | ||
|
||
MYLIB_EXPORT int32_t add(int32_t a, int32_t b); | ||
|
||
MYLIB_EXPORT intptr_t InitDartApiDL(void *data); | ||
|
||
MYLIB_EXPORT void *NewPersistentHandle(Dart_Handle non_persistent_handle); | ||
|
||
MYLIB_EXPORT Dart_Handle HandleFromPersistent(void *persistent_handle); |
18 changes: 18 additions & 0 deletions
18
pkgs/native_assets_builder/test_data/use_dart_api/test/use_dart_api_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) 2025, 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 'dart:ffi'; | ||
|
||
import 'package:test/test.dart'; | ||
import 'package:use_dart_api/use_dart_api.dart'; | ||
|
||
void main() { | ||
InitDartApiDL(NativeApi.initializeApiDLData); | ||
|
||
test('use dart_api_dl.h', () { | ||
const x = 42; | ||
final persistentHandle = NewPersistentHandle(x); | ||
HandleFromPersistent(persistentHandle); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This documentation might not be sufficient in the long wrong - how would I check that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One should check the major version is identical and the minor version is at least what you need.