ffi_tool 0.1.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 65

Overview #

GitHub Actions status

This library helps developers to generate dart:ffi bindings. You can contribute at github.com/dart-interop/ffi_tool.

The advantages of using this package (instead of writing hand-written code) are:

  • Often less boilerplate
  • Often more readable (e.g. "*void" instead of "Pointer
  • Support for Apple's ARC

Getting started #

In "pubspec.yaml", add:

dev_dependencies:
  ffi_tool: ^0.1.0

Run pub get.

You have to write a generator script (see examples below) in some file. For example, "tool/generate_example.dart".

Run the script with pub run tool/generate_example.dart.

Generator scripts #

C library #

import 'package:ffi_tool/c.dart';
import 'dart:io';

void main() {
  generateFile(File("lib/src/generated.dart"), library);
}

final library = Library(
  dynamicLibraryIdentifier: "dlForExampleLibrary",
  dynamicLibraryPath: "path/to/library",
  importedUris: [
    "package:ffi/ffi.dart",
  ],
  elements: <Element>[
    // C function
    Func(
      name: "Example",
      parameterTypes: ["int32", "float64", "*void"],
      returnType: "void",
    ),

    // C global variable
    Global(
      name: "ExampleGlobal",
      type: "Int32",
    ),
  ],
);

0.1.2 #

  • The package now works in the latest Dart SDK.
  • Removed cupertino_ffi stuff.

0.1.1 #

  • Added Objective-C support.

0.1.0 #

  • Initial release

example/example.dart

import 'package:ffi_tool/c.dart';
import 'dart:io';

void main() {
  generateFile(File("generated.dart"), library);
}

final library = Library(
  dynamicLibraryIdentifier: "dlForExampleLibrary",
  dynamicLibraryPath: "path/to/library",
  importedUris: [
    "package:ffi/ffi.dart",
  ],
  elements: [
    // A function
    Func(
      name: "Example",
      parameterTypes: ["int32", "float64", "*void"],
      returnType: "void",
    ),

    // A global
    Global(
      name: "ExampleGlobal",
      type: "int32",
    ),
  ],
);

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  ffi_tool: ^0.1.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:ffi_tool/c.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
30
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
65
Learn more about scoring.

We analyzed this package on Dec 9, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.12.21

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
meta ^1.1.0 1.1.8
Dev dependencies
ffi ^0.1.3
pedantic ^1.8.0
test ^1.6.0