Skip to content

Commit

Permalink
Modify plugin_ffi and package_ffi template (flutter#143376)
Browse files Browse the repository at this point in the history
* Use `dart run` instead of `flutter pub run` in the documentation as it is now deprecated.
* Use `int64_t` instead of `intptr_t` for `sum` examples.
  • Loading branch information
HosseinYousefi authored Feb 15, 2024
1 parent dde76f4 commit 97ab92e
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Bundling is done by Flutter based on the output from `build.dart`.
To use the native code, bindings in Dart are needed.
To avoid writing these by hand, they are generated from the header file
(`src/{{projectName}}.h`) by `package:ffigen`.
Regenerate the bindings by running `flutter pub run ffigen --config ffigen.yaml`.
Regenerate the bindings by running `dart run ffigen --config ffigen.yaml`.

## Invoking native code

Expand Down
4 changes: 2 additions & 2 deletions packages/flutter_tools/templates/package_ffi/ffigen.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Run with `flutter pub run ffigen --config ffigen.yaml`.
# Run with `dart run ffigen --config ffigen.yaml`.
name: {{pluginDartClass}}Bindings
description: |
Bindings for `src/{{projectName}}.h`.

Regenerate bindings with `flutter pub run ffigen --config ffigen.yaml`.
Regenerate bindings with `dart run ffigen --config ffigen.yaml`.
output: 'lib/{{projectName}}_bindings_generated.dart'
headers:
entry-points:
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_tools/templates/plugin_ffi/README.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ The native build systems that are invoked by FFI (and method channel) plugins ar
To use the native code, bindings in Dart are needed.
To avoid writing these by hand, they are generated from the header file
(`src/{{projectName}}.h`) by `package:ffigen`.
Regenerate the bindings by running `flutter pub run ffigen --config ffigen.yaml`.
Regenerate the bindings by running `dart run ffigen --config ffigen.yaml`.

## Invoking native code

Expand Down
4 changes: 2 additions & 2 deletions packages/flutter_tools/templates/plugin_ffi/ffigen.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Run with `flutter pub run ffigen --config ffigen.yaml`.
# Run with `dart run ffigen --config ffigen.yaml`.
name: {{pluginDartClass}}Bindings
description: |
Bindings for `src/{{projectName}}.h`.

Regenerate bindings with `flutter pub run ffigen --config ffigen.yaml`.
Regenerate bindings with `dart run ffigen --config ffigen.yaml`.
output: 'lib/{{projectName}}_bindings_generated.dart'
headers:
entry-points:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'dart:ffi' as ffi;

/// Bindings for `src/{{projectName}}.h`.
///
/// Regenerate bindings with `flutter pub run ffigen --config ffigen.yaml`.
/// Regenerate bindings with `dart run ffigen --config ffigen.yaml`.
///
class {{pluginDartClass}}Bindings {
/// Holds the symbol lookup function.
Expand Down Expand Up @@ -43,7 +43,7 @@ class {{pluginDartClass}}Bindings {
}

late final _sumPtr =
_lookup<ffi.NativeFunction<ffi.IntPtr Function(ffi.IntPtr, ffi.IntPtr)>>(
_lookup<ffi.NativeFunction<ffi.Int Function(ffi.Int, ffi.Int)>>(
'sum');
late final _sum = _sumPtr.asFunction<int Function(int, int)>();

Expand All @@ -63,7 +63,7 @@ class {{pluginDartClass}}Bindings {
}

late final _sum_long_runningPtr =
_lookup<ffi.NativeFunction<ffi.IntPtr Function(ffi.IntPtr, ffi.IntPtr)>>(
_lookup<ffi.NativeFunction<ffi.Int Function(ffi.Int, ffi.Int)>>(
'sum_long_running');
late final _sum_long_running =
_sum_long_runningPtr.asFunction<int Function(int, int)>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
// For very short-lived functions, it is fine to call them on the main isolate.
// They will block the Dart execution while running the native function, so
// only do this for native functions which are guaranteed to be short-lived.
FFI_PLUGIN_EXPORT intptr_t sum(intptr_t a, intptr_t b) { return a + b; }
FFI_PLUGIN_EXPORT int sum(int a, int b) { return a + b; }

// A longer-lived native function, which occupies the thread calling it.
//
// Do not call these kind of native functions in the main isolate. They will
// block Dart execution. This will cause dropped frames in Flutter applications.
// Instead, call these native functions on a separate isolate.
FFI_PLUGIN_EXPORT intptr_t sum_long_running(intptr_t a, intptr_t b) {
FFI_PLUGIN_EXPORT int sum_long_running(int a, int b) {
// Simulate work.
#if _WIN32
Sleep(5000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
// For very short-lived functions, it is fine to call them on the main isolate.
// They will block the Dart execution while running the native function, so
// only do this for native functions which are guaranteed to be short-lived.
FFI_PLUGIN_EXPORT intptr_t sum(intptr_t a, intptr_t b);
FFI_PLUGIN_EXPORT int sum(int a, int b);

// A longer lived native function, which occupies the thread calling it.
//
// Do not call these kind of native functions in the main isolate. They will
// block Dart execution. This will cause dropped frames in Flutter applications.
// Instead, call these native functions on a separate isolate.
FFI_PLUGIN_EXPORT intptr_t sum_long_running(intptr_t a, intptr_t b);
FFI_PLUGIN_EXPORT int sum_long_running(int a, int b);

0 comments on commit 97ab92e

Please sign in to comment.