Skip to content

Merge ffi package #895

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

Merged
merged 78 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
9c4020f
[ffi] Initial commit
mit-mit Sep 5, 2019
fd18bd8
[ffi] Repo config
mit-mit Sep 5, 2019
6c8b5c5
[ffi] Repo config
mit-mit Sep 5, 2019
7f527a9
[ffi] Add travis (https://github.com/dart-lang/ffi/issues/1)
mit-mit Sep 5, 2019
9263c71
[ffi] Add travis badge (https://github.com/dart-lang/ffi/issues/2)
mit-mit Sep 5, 2019
efc9acb
[ffi] Helpers for null-terminated Utf8 (https://github.com/dart-lang/…
sjindel-google Sep 9, 2019
12512d5
[ffi] Add initial utf16 support (https://github.com/dart-lang/ffi/iss…
mit-mit Sep 9, 2019
7a34b27
[ffi] Allocation utilities. (https://github.com/dart-lang/ffi/issues/7)
sjindel-google Oct 9, 2019
584c470
[ffi] Fix travis (https://github.com/dart-lang/ffi/issues/8)
mit-mit Oct 10, 2019
9f5a475
[ffi] Expand readme (https://github.com/dart-lang/ffi/issues/10)
mit-mit Oct 10, 2019
e04e828
[ffi] Bump incremental version after adding allocation routines. (htt…
sjindel-google Oct 10, 2019
439db9c
[ffi] Remove type argument from structs (https://github.com/dart-lang…
dcharkes Oct 15, 2019
34f9da0
[ffi] Noop to trigger git syncing (https://github.com/dart-lang/ffi/i…
dcharkes Oct 15, 2019
37cff3f
[ffi] Remove references to asExternalTypedData (https://github.com/da…
sjindel-google Oct 15, 2019
d1f189d
[ffi] bump version (https://github.com/dart-lang/ffi/issues/15)
sjindel-google Oct 15, 2019
5ffdf4f
[ffi] Update pubspec to sdk >= 2.6.0-dev.8.2 (https://github.com/dart…
dcharkes Oct 24, 2019
d5ee229
[ffi] Undo bump to 0.1.4 (https://github.com/dart-lang/ffi/issues/19)
dcharkes Oct 31, 2019
fff1918
[ffi] Update SDK constraint to 2.6.0 stable and release 0.1.3 stable …
dcharkes Nov 5, 2019
cf6603c
[ffi] Update utf16.dart (https://github.com/dart-lang/ffi/issues/33)
Mar 27, 2020
0d85a86
[ffi] Delete surrogate-related subtests to be compatible match breaki…
askeksa May 4, 2020
1b772b0
[ffi] Cleanup metadata (https://github.com/dart-lang/ffi/issues/45)
mit-mit May 15, 2020
672365c
[ffi] Update sdk constraint to enable NNBD strong mode
dcharkes May 18, 2020
f458f37
[ffi] Adjust documentation to match new UTF-8 encoder/decoder behavio…
askeksa Aug 13, 2020
d7d5dc7
[ffi] Merge null safety branch to master (https://github.com/dart-lan…
mit-mit Nov 16, 2020
e02da07
[ffi] Tweak the docs (https://github.com/dart-lang/ffi/issues/63)
jpnurmi Nov 18, 2020
48ee55c
[ffi] Utf8.fromUtf8(): allow passing the length (https://github.com/d…
jpnurmi Nov 18, 2020
4832e4d
[ffi] Minor Utf8.strlen() improvement (https://github.com/dart-lang/f…
jpnurmi Nov 18, 2020
9e2a469
[ffi] Bump the version and update changelog (https://github.com/dart-…
jpnurmi Nov 18, 2020
99536de
[ffi] Migrate to GitHub Actions (https://github.com/dart-lang/ffi/iss…
athomas Jan 6, 2021
fcfc656
[ffi] Migrate Utf8 and Utf16 to Opaque and remove Utf8.toString
dcharkes Jan 6, 2021
bfea911
[ffi] 0.3.0-dev.0 Allocator and Opaque (https://github.com/dart-lang/…
dcharkes Feb 1, 2021
8865000
[ffi] Merge commit 'fcfc656'
athomas Feb 2, 2021
c4ca294
[ffi] Release for 2.12.0-259.8.beta (https://github.com/dart-lang/ffi…
dcharkes Feb 3, 2021
5d02bbb
[ffi] Remove assumption that machine word size implies Smi size. (htt…
rmacnak-google Feb 9, 2021
6d88c19
[ffi] Add `allocate` and `free` back in to ease migration (https://gi…
dcharkes Feb 9, 2021
ae8122f
[ffi] ignore: non_constant_type_argument_warning (https://github.com/…
dcharkes Feb 9, 2021
11ca1fc
[ffi] Export `allocate` and `free` (https://github.com/dart-lang/ffi/…
dcharkes Feb 10, 2021
28a0771
[ffi] Change Utf8 and Ut16 interfaces to extension methods (https://g…
dcharkes Feb 11, 2021
e653531
[ffi] Bump version to 1.0 (https://github.com/dart-lang/ffi/issues/78)
dcharkes Feb 11, 2021
058d693
[ffi] Merge commit '672365c' to master
athomas Mar 1, 2021
2b3f185
[ffi] Update LICENSE (https://github.com/dart-lang/ffi/issues/94)
franklinyow Apr 6, 2021
9263522
[ffi] Update CI (https://github.com/dart-lang/ffi/issues/99)
kevmoo Apr 27, 2021
56f9d04
[ffi] Add Arena Allocator (https://github.com/dart-lang/ffi/issues/103)
dcharkes May 25, 2021
445b13a
[ffi] Move to package:lints (https://github.com/dart-lang/ffi/issues/…
mit-mit May 25, 2021
1efc52f
[ffi] Check that receiver is not nullptr on Pointer extension methods…
dcharkes May 27, 2021
4ddb72c
[ffi] Fixed unhandled exception in `withZoneArena` (https://github.co…
dcharkes Jun 4, 2021
7337e9e
[ffi] Common C integer types (https://github.com/dart-lang/ffi/issues…
dcharkes Jan 20, 2022
bd7bbdb
[ffi] Enable and fix new strict analysis options (https://github.com/…
kevmoo Feb 1, 2022
d93441f
[ffi] Bump version to 1.2.0 (https://github.com/dart-lang/ffi/issues/…
dcharkes Feb 3, 2022
8698dc1
[ffi] Update pubspec.yaml (https://github.com/dart-lang/ffi/issues/139)
devoncarew Apr 19, 2022
be88bb2
[ffi] Revert "Common C integer types (https://github.com/dart-lang/ff…
dcharkes May 13, 2022
a205437
[ffi] Update README.md (https://github.com/dart-lang/ffi/issues/143)
devoncarew May 13, 2022
cfa9eaa
[ffi] Replace HeapFree with CoTaskMemAlloc (https://github.com/dart-…
May 27, 2022
d518fe4
[ffi] Zero memory after test (https://github.com/dart-lang/ffi/issues…
Jun 19, 2022
24487a6
[ffi] Fix typo in doc comment (https://github.com/dart-lang/ffi/issue…
brookman Aug 19, 2022
f643f4c
[ffi] blast_repo fixes (https://github.com/dart-lang/ffi/issues/165)
kevmoo Nov 9, 2022
525c64f
[ffi] Add links to API reference and examples to README (https://gith…
dcharkes Nov 21, 2022
6de3664
[ffi] Bump actions/checkout from 3.1.0 to 3.2.0 (https://github.com/d…
dependabot[bot] Jan 2, 2023
48d40bb
[ffi] Bump actions/checkout from 3.2.0 to 3.3.0 (https://github.com/d…
dependabot[bot] Feb 1, 2023
259ad91
[ffi] Bump dart-lang/setup-dart from 1.3 to 1.4 (https://github.com/d…
dependabot[bot] Feb 1, 2023
6d1624d
[ffi] Bump SDK constraint to 4.0.0 (https://github.com/dart-lang/ffi/…
dcharkes Feb 20, 2023
5336aed
[ffi] Bump actions/checkout from 3.3.0 to 3.5.0 (https://github.com/d…
dependabot[bot] Apr 3, 2023
d7082a4
[ffi] Bump dart-lang/setup-dart from 1.4.0 to 1.5.0 (https://github.c…
dependabot[bot] Apr 3, 2023
86d2425
[ffi] test zero termination of Utf8 string (https://github.com/dart-l…
dcharkes Apr 3, 2023
58b1fcb
[ffi] Bump actions/checkout from 3.5.0 to 3.5.2 (https://github.com/d…
dependabot[bot] May 1, 2023
71e4003
[ffi] Add topics in pubspec.yaml (https://github.com/dart-lang/ffi/is…
mit-mit May 9, 2023
ca44d28
[ffi] Rev pubspec and update changelog (https://github.com/dart-lang/…
devoncarew May 9, 2023
76a53ac
[ffi] Rename `master` branch to `main` (https://github.com/dart-lang/…
dcharkes May 17, 2023
7db243c
[ffi] Bump actions/checkout from 3.5.2 to 3.5.3 (https://github.com/d…
dependabot[bot] Jul 1, 2023
6bd0935
[ffi] Expose pointer to free from allocators (https://github.com/dart…
mraleph Aug 8, 2023
d4d5cda
[ffi] Bump actions/checkout from 3.5.3 to 3.6.0 (https://github.com/d…
dependabot[bot] Sep 1, 2023
627fab1
[ffi] Bump actions/checkout from 3.6.0 to 4.1.0 (https://github.com/d…
dependabot[bot] Oct 1, 2023
0177c4b
[ffi] Bump dart-lang/setup-dart from 1.5.0 to 1.5.1 (https://github.c…
dependabot[bot] Oct 3, 2023
122acc4
[ffi] Bump actions/checkout from 4.1.0 to 4.1.1 (https://github.com/d…
dependabot[bot] Nov 1, 2023
d583571
[ffi] Bump dart-lang/setup-dart from 1.5.1 to 1.6.0 (https://github.c…
dependabot[bot] Nov 1, 2023
eb22263
[ffi] Migrate away from elementAt (https://github.com/dart-lang/ffi/i…
dcharkes Jan 15, 2024
7f8cadd
Merge package:ffi into shared native repository
dcharkes Jan 16, 2024
9c3fc4c
Fix paths in workflow
dcharkes Jan 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions .github/workflows/ffi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: Dart CI

on:
# Run on PRs and pushes to the default branch.
push:
branches: [main]
paths:
- '.github/workflows/ffi.yaml'
- 'pkgs/ffi/**'
pull_request:
branches: [main]
paths:
- '.github/workflows/ffi.yaml'
- 'pkgs/ffi/**'
schedule:
- cron: "0 0 * * 0"

env:
PUB_ENVIRONMENT: bot.github

jobs:
# Check code formatting and static analysis on a single OS (linux)
# against Dart dev.
analyze:
runs-on: ubuntu-latest
defaults:
run:
working-directory: pkgs/ffi/
strategy:
fail-fast: false
matrix:
sdk: [dev]
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
- uses: dart-lang/setup-dart@b64355ae6ca0b5d484f0106a033dd1388965d06d
with:
sdk: ${{ matrix.sdk }}
- id: install
name: Install dependencies
run: dart pub get
- name: Check formatting
run: dart format --output=none --set-exit-if-changed .
if: always() && steps.install.outcome == 'success'
- name: Analyze code
run: dart analyze --fatal-infos
if: always() && steps.install.outcome == 'success'

# Run tests on a matrix consisting of two dimensions:
# 1. OS: ubuntu-latest, (macos-latest, windows-latest)
# 2. release channel: dev
test:
needs: analyze
runs-on: ${{ matrix.os }}
defaults:
run:
working-directory: pkgs/ffi/
strategy:
fail-fast: false
matrix:
# Add macos-latest and/or windows-latest if relevant for this package.
os: [ubuntu-latest]
sdk: [beta, dev]
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
- uses: dart-lang/setup-dart@b64355ae6ca0b5d484f0106a033dd1388965d06d
with:
sdk: ${{ matrix.sdk }}
- id: install
name: Install dependencies
run: dart pub get
- name: Run VM tests
run: dart test --platform vm
if: always() && steps.install.outcome == 'success'
3 changes: 3 additions & 0 deletions pkgs/ffi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.dart_tool
.packages
pubspec.lock
6 changes: 6 additions & 0 deletions pkgs/ffi/AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Below is a list of people and organizations that have contributed
# to the Dart project. Names should be added to the list like so:
#
# Name/Organization <email address>

Google LLC
146 changes: 146 additions & 0 deletions pkgs/ffi/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
## 2.1.1

- Require Dart 3.3.0 or greater.
- Migrate `elementAt` use to `operator +`.

## 2.1.0

- Require Dart 3.0.0 or greater.
- Expose native equivalent to `free` (`nativeFree`) from `malloc` and
`calloc` allocators.

## 2.0.2

- Fixed a typo in a doc comment.
- Added package topics to the pubspec file.

## 2.0.1

- Only zero out memory on successful allocation on Windows.
- Upgrade test dev dependency.

## 2.0.0

- Switch Windows memory allocation to use `CoTaskMemAlloc` and `CoTaskMemFree`,
which will enable support for `NativeFinalizer`.
- Require Dart 2.17.0 or greater.

## 1.2.1

Revert added common C integer types as ABI-specific integers.
Instead, these are available in Dart 2.17.

## 1.2.0 (retracted)

This release requires Dart `2.16.0` or greater.

## 1.2.0-dev.0

Added common C integer types as ABI-specific integers. These common C integer
types will make their way into `dart:ffi` in 2.17 and be deprecated from this
package. Having them in this package enables using them in Dart 2.16.

This pre-release requires Dart `2.16.0-118.0.dev` or greater.

## 1.1.2

Fixed unhandled exception in `withZoneArena` (#107).

## 1.1.1

Adds a sanity check to `Pointer<Utf8>` and `Pointer<Utf16>` extension methods
that receiver is not `nullptr`.

## 1.1.0

Adds the `arena` allocator.

Moves from static analysis with lints in package:pedantic to package:lints.

## 1.0.0

Bumping the version of this package to `1.0.0`.

Removes all deprecated methods, use `0.3.0-nullsafety.3` for migration.

## 0.3.1-nullsafety.0

Deprecates the static methods on `Utf8` and `Utf16` and introduces
extension methods to replace them.

## 0.3.0-nullsafety.3

Adds back in deprecated `allocate` and `free` to ease migration.
These will be removed in the next release.

This pre-release requires Dart `2.12.0-259.9.beta` or greater.

## 0.3.0-nullsafety.1

This pre-release requires Dart `2.12.0-259.8.beta` or greater.

Note that this pre-release does _not_ work in Flutter versions containing Dart
`2.12.0-260.0.dev` - `2.12.0-264.0.dev`.
Using `Allocator.call` throws a `NoSuchMethodError` in these versions.
See [Flutter Engine #23954](https://github.com/flutter/engine/pull/23954) for more info.

## 0.3.0-nullsafety.0

Changes `Utf8` and `Utf16` to extend `Opaque` instead of `Struct`.
This means `.ref` is no longer available and `Pointer<Utf(..)>` should be used.
See [breaking change #44622](https://github.com/dart-lang/sdk/issues/44622) for more info.

Removes `allocate` and `free`.
Instead, introduces `calloc` which implements the new `Allocator` interface.
See [breaking change #44621](https://github.com/dart-lang/sdk/issues/44621) for more info.

This pre-release requires Dart `2.12.0-265.0.dev` or greater.

## 0.2.0-nullsafety.1

Adds an optional named `length` argument to `Utf8.fromUtf8()`.

## 0.2.0-nullsafety.0

Pre-release (non-stable) release supporting null safety.
Requires Dart 2.12.0 or greater.

## 0.1.3

Stable release incorporating all the previous dev release changes.

Bump SDK constraint to `>= 2.6.0`.

## 0.1.3-dev.4

Bump SDK constraint to `>= 2.6.0-dev.8.2` which contains the new API of `dart:ffi`.

## 0.1.3-dev.3

Replace use of deprecated `asExternalTypedData` with `asTypedList`.

## 0.1.3-dev.2

Incorporate struct API changes, drop type argument of structs.

## 0.1.3-dev.1

* Adds top-level `allocate<T>()` and `free()` methods which can be used as a
replacement for the deprecated `Pointer.allocate<T>()` and `Pointer.free()`
members in `dart:ffi`.

## 0.1.1+2

* Expand readme

## 0.1.1+1

* Fix documentation link

## 0.1.1

* Add basic Utf16 support

## 0.1.0

* Initial release supporting Utf8
27 changes: 27 additions & 0 deletions pkgs/ffi/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright 2019, the Dart project authors.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Google LLC nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11 changes: 11 additions & 0 deletions pkgs/ffi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[![Build Status](https://github.com/dart-lang/ffi/workflows/Dart%20CI/badge.svg)](https://github.com/dart-lang/ffi/actions?query=workflow%3A"Dart+CI")
[![pub package](https://img.shields.io/pub/v/ffi.svg)](https://pub.dev/packages/ffi)
[![package publisher](https://img.shields.io/pub/publisher/ffi.svg)](https://pub.dev/packages/ffi/publisher)

Utilities for working with Foreign Function Interface (FFI) code, incl.
converting between Dart strings and C strings encoded with UTF-8 and UTF-16.

Please see the [API reference](https://pub.dev/documentation/ffi/latest/ffi/ffi-library.html) for more documentation and the [tests](https://github.com/dart-lang/ffi/tree/main/test) for example usage.

For additional details about Dart FFI (`dart:ffi`), see
https://dart.dev/guides/libraries/c-interop.
6 changes: 6 additions & 0 deletions pkgs/ffi/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include: package:lints/recommended.yaml

analyzer:
language:
strict-casts: true
strict-inference: true
18 changes: 18 additions & 0 deletions pkgs/ffi/example/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'dart:ffi';

import 'package:ffi/ffi.dart';

void main() {
// Allocate and free some native memory with calloc and free.
final pointer = calloc<Uint8>();
pointer.value = 3;
print(pointer.value);
calloc.free(pointer);

// Use the Utf8 helper to encode zero-terminated UTF-8 strings in native memory.
final String myString = '😎👿💬';
final Pointer<Utf8> charPointer = myString.toNativeUtf8();
print('First byte is: ${charPointer.cast<Uint8>().value}');
print(charPointer.toDartString());
calloc.free(charPointer);
}
8 changes: 8 additions & 0 deletions pkgs/ffi/lib/ffi.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) 2019, 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/allocation.dart' show calloc, malloc;
export 'src/arena.dart';
export 'src/utf8.dart';
export 'src/utf16.dart';
Loading