commit | 0caa49523d7bb5892c411c598193f5e3f04f1430 | [log] [tgz] |
---|---|---|
author | Prerak Mann <31154435+mannprerak2@users.noreply.github.com> | Mon Jul 20 14:01:50 2020 +0530 |
committer | GitHub <noreply@github.com> | Mon Jul 20 10:31:50 2020 +0200 |
tree | 129880ff29e31cf37e14cd0ce8fd543cace6a2b9 | |
parent | 46ddca94b6f623590fe9f2ad7202cef250e554e2 [diff] |
Name conflict resolution with reserved Dart Keywords (#34) Closes #21. - Added reserved keyword conflict resolution to `UniqueNamer`. - Added tests (`collision_tests/reserved_word_collision_test.dart`) - Bug fix: UniqueNamers used by declarations are now reset to initial state before generating code. This ensures multiple calls to Library.generate will produce the same results
Experimental binding generator for FFI bindings.
For some header file example.h:
int sum(int a, int b);
Add configurations to Pubspec File:
ffigen: output: 'generated_bindings.dart' headers: - 'example.h'
Output (generated_bindings.dart).
class NativeLibrary { final DynamicLibrary _dylib; NativeLibrary(DynamicLibrary dynamicLibrary) : _dylib = dynamicLibrary; int sum(int a, int b) { _sum ??= _dylib.lookupFunction<_c_sum, _dart_sum>('sum'); return _sum(a, b); } _dart_sum _sum;; } typedef _c_sum = ffi.Int32 Function(Int32 a, Int32 b); typedef _dart_sum = int Function(int a,int b);
pubspec.yaml
.pubspec.yaml
or in a custom YAML file (see configurations).pub run ffigen
.A dynamic library for a wrapper to libclang needs to be generated as it is used by the parser submodule.
sudo apt-get install libclang-dev
.cd tool/wrapped_libclang
, then run dart build.dart
.cd tool\wrapped_libclang
, then run dart build.dart
.cd tool/wrapped_libclang
, then run dart build.dart
.Configurations can be provided in 2 ways-
pubspec.yaml
file under the key ffigen
.pub run ffigen --config config.yaml
The following configuration options are available-
Fixed size array‘s in structs aren’t currently supported by Dart. However we provide a workaround, using which array items can now be accessed using []
operator.
Here's a C structure from libclang-
typedef struct { unsigned long long data[3]; } CXFileUniqueID;
The generated code is -
class CXFileUniqueID extends ffi.Struct { @ffi.Uint64() int _unique_data_item_0; @ffi.Uint64() int _unique_data_item_1; @ffi.Uint64() int _unique_data_item_2; /// Helper for array `data`. ArrayHelper_CXFileUniqueID_data_level0 get data => ArrayHelper_CXFileUniqueID_data_level0(this, [3], 0, 0); } /// Helper for array `data` in struct `CXFileUniqueID`. class ArrayHelper_CXFileUniqueID_data_level0 { final CXFileUniqueID _struct; final List<int> dimensions; final int level; final int _absoluteIndex; int get length => dimensions[level]; ArrayHelper_CXFileUniqueID_data_level0( this._struct, this.dimensions, this.level, this._absoluteIndex); void _checkBounds(int index) { if (index >= length || index < 0) { throw RangeError( 'Dimension $level: index not in range 0..${length} exclusive.'); } } int operator [](int index) { _checkBounds(index); switch (_absoluteIndex + index) { case 0: return _struct._unique_data_item_0; case 1: return _struct._unique_data_item_1; case 2: return _struct._unique_data_item_2; default: throw Exception('Invalid Array Helper generated.'); } } void operator []=(int index, int value) { _checkBounds(index); switch (_absoluteIndex + index) { case 0: _struct._unique_data_item_0 = value; break; case 1: _struct._unique_data_item_1 = value; break; case 2: _struct._unique_data_item_2 = value; break; default: throw Exception('Invalid Array Helper generated.'); } } }
cd examples/<example_u_want_to_run>
, Run pub get
.pub run ffigen
.Dynamic library for some tests need to be built before running the examples.
cd test/native_test
.dart build_test_dylib.dart
.Run tests from the root of the package with pub run test
.