Skip to content
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

Refactor C interop #5881

Merged
merged 4 commits into from
May 30, 2024
Merged

Refactor C interop #5881

merged 4 commits into from
May 30, 2024

Conversation

atsansone
Copy link
Contributor

Fixes #3894
Expands on this issue by updating links to code examples, updating style

@atsansone atsansone added the review.tech Awaiting Technical Review label May 29, 2024
@dart-github-bot
Copy link
Collaborator

dart-github-bot commented May 29, 2024

Visit the preview URL for this PR (updated for commit 5697550):

https://dart-dev--pr5881-fix-3894-g1ubcako.web.app

Copy link
Contributor

@dcharkes dcharkes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @atsansone!


The [`hello.dart` file]({{hw}}/hello.dart)
illustrates the steps for using `dart:ffi` to call a C function:
To learn how to call a C function using the `dart:ffi` package,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dart: is not a package, package: is a package. Did you mean to say library?

Once you understand the `hello_world` example,
consult the [other `dart:ffi` examples](#download-example-files).

## Bundle and load C libraries
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we mostly want to reference what is non-experimental here.

The work on native assets (which is currently an experiment in Dart and Flutter), will make bundling / distributing native code platform agnostic.

Edit: Ah we already have a section for native assets, maybe link it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already done on line 264

* It makes the native assets available at runtime through
* Build the native code or obtains the binaries
using a package's `build.dart` script.
* Bundle the native [`Asset`][] that the `build.dart` script reports.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new terminology is build hook, build.dart hook, or hook/build.dart. The hook was moved inside the hook/ directory, and we don't use "script" anymore.

### Experiment opt-in
* To learn about native assets in Dart FFI,
consult the `dart:ffi` API reference for [`Native`][] and [`DefaultAsset`][].
* To learn about the `build.dart` script,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

script -> hook

When a Dart or Flutter project depends on `package:native_add_library`,
the `build.dart` script will automatically be
invoked on `run`, `build`, and `test` commands.
it invokes the `build.dart` script on `run`, `build`, and `test` commands.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build.dart -> hook/build.dart

| [`src/native_add_library.c`][] | The C file containing the code for `add`. |
| [`lib/native_add_library.dart`][] | The Dart file that invokes the C function `add` in asset `package:native_add_library/native_add_library.dart` through FFI. (Note that _asset id_ defaults to the library uri.) |
| [`test/native_add_library_test.dart`][] | A Dart test using the native code. |
| [`hook/build.dart`][] | A script for compiling `src/native_add_library.c` and declaring the compiled asset with id `package:native_add_library/native_add_library.dart`. |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

script -> hook

@MaryaBelanger MaryaBelanger added review.await-update Awaiting Updates after Edits and removed review.tech Awaiting Technical Review labels May 30, 2024
@parlough parlough added review.copy Awaiting Copy Review and removed review.await-update Awaiting Updates after Edits labels May 30, 2024
@atsansone atsansone merged commit 01e996b into dart-lang:main May 30, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
review.copy Awaiting Copy Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix FFI example fails to find dependent package test_utils
5 participants