Skip to content
This repository has been archived by the owner on Jan 28, 2024. It is now read-only.

Fix generated asset/assetId property for ffi-native #634

Merged
merged 7 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
generate a typedef for the `Function`.
- Use Dart wrapper types in args and returns of ObjCBlocks.
- Bump min SDK version to 3.2.0-114.0.dev.
- Renamed `asset` to `assetId` for `ffi-native`.

# 9.0.1

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ type-map:

```yaml
ffi-native:
asset: 'myasset' # Optional.
assetId: 'myasset' # Optional.
```
</td>
</tr>
Expand Down
4 changes: 2 additions & 2 deletions example/ffinative/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

name: NativeLibrary
ffi-native:
# asset: 'assetname' # (optional)
assetId: 'package:ffinative_example/generated_bindings.dart' # (optional)
description: Bindings to `headers/example.h`.
output: 'generated_bindings.dart'
output: 'lib/generated_bindings.dart'
headers:
entry-points:
- 'headers/example.h'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,44 @@
import 'dart:ffi' as ffi;

/// Adds 2 integers.
@ffi.Native<ffi.Int Function(ffi.Int, ffi.Int)>(symbol: 'sum')
@ffi.Native<ffi.Int Function(ffi.Int, ffi.Int)>(
symbol: 'sum', assetId: 'package:ffinative_example/generated_bindings.dart')
external int sum(
int a,
int b,
);

/// Subtracts 2 integers.
@ffi.Native<ffi.Int Function(ffi.Int, ffi.Int)>(symbol: 'subtract')
@ffi.Native<ffi.Int Function(ffi.Int, ffi.Int)>(
symbol: 'subtract',
assetId: 'package:ffinative_example/generated_bindings.dart')
external int subtract(
int a,
int b,
);

/// Multiplies 2 integers, returns pointer to an integer,.
@ffi.Native<ffi.Pointer<ffi.Int> Function(ffi.Int, ffi.Int)>(symbol: 'multiply')
@ffi.Native<ffi.Pointer<ffi.Int> Function(ffi.Int, ffi.Int)>(
symbol: 'multiply',
assetId: 'package:ffinative_example/generated_bindings.dart')
external ffi.Pointer<ffi.Int> multiply(
int a,
int b,
);

/// Divides 2 integers, returns pointer to a float.
@ffi.Native<ffi.Pointer<ffi.Float> Function(ffi.Int, ffi.Int)>(symbol: 'divide')
@ffi.Native<ffi.Pointer<ffi.Float> Function(ffi.Int, ffi.Int)>(
symbol: 'divide',
assetId: 'package:ffinative_example/generated_bindings.dart')
external ffi.Pointer<ffi.Float> divide(
int a,
int b,
);

/// Divides 2 floats, returns a pointer to double.
@ffi.Native<ffi.Pointer<ffi.Double> Function(ffi.Float, ffi.Float)>(
symbol: 'dividePrecision')
symbol: 'dividePrecision',
assetId: 'package:ffinative_example/generated_bindings.dart')
external ffi.Pointer<ffi.Double> dividePrecision(
double a,
double b,
Expand Down
4 changes: 2 additions & 2 deletions ffigen.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,12 @@
"type": "object",
"additionalProperties": false,
"properties": {
"asset": {
"assetId": {
"type": "string"
}
},
"required": [
"asset"
"assetId"
]
}
]
Expand Down
4 changes: 2 additions & 2 deletions lib/src/code_generator/func.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ class Func extends LookUpBinding {
functionType.getFfiDartType(w, writeArgumentNames: false);

if (ffiNativeConfig.enabled) {
final assetString = ffiNativeConfig.asset != null
? ", asset: '${ffiNativeConfig.asset}'"
final assetString = ffiNativeConfig.assetId != null
? ", assetId: '${ffiNativeConfig.assetId}'"
: '';
final isLeafString = isLeaf ? ', isLeaf: true' : '';
s.write(
Expand Down
4 changes: 2 additions & 2 deletions lib/src/config_provider/config_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,9 @@ class ObjCModulePrefixer {

class FfiNativeConfig {
final bool enabled;
final String? asset;
final String? assetId;

const FfiNativeConfig({required this.enabled, this.asset});
const FfiNativeConfig({required this.enabled, this.assetId});
}

class SymbolFile {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/config_provider/spec_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,6 @@ FfiNativeConfig ffiNativeExtractor(dynamic yamlConfig) {
final yamlMap = yamlConfig as Map?;
return FfiNativeConfig(
enabled: true,
asset: yamlMap?[strings.ffiNativeAsset] as String?,
assetId: yamlMap?[strings.ffiNativeAsset] as String?,
);
}
2 changes: 1 addition & 1 deletion lib/src/strings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ const doubleNaN = 'double.nan';
const dartHandleUsr = 'c:@S@_Dart_Handle';

const ffiNative = 'ffi-native';
const ffiNativeAsset = 'asset';
const ffiNativeAsset = 'assetId';

Directory? _tmpDir;

Expand Down
2 changes: 2 additions & 0 deletions test/native_test/native_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void main() {
rethrow;
}
});

test('bool', () {
expect(bindings.Function1Bool(true), false);
expect(bindings.Function1Bool(false), true);
Expand Down Expand Up @@ -112,6 +113,7 @@ void main() {
}
}
});

test('Array Workaround: Range Errors', () {
final struct1 = bindings.getStruct1();
// Index (get) above range.
Expand Down