3838//! // and start using it to write your own high level abstraction.
3939//! bindings.write_to_file("simple/lib/ffi.dart")?;
4040//! ```
41- use clang:: { Clang , Entity , EntityKind , Index , Type , TypeKind } ;
42- use log:: debug;
4341use std:: {
4442 collections:: HashMap ,
4543 fmt, fs,
4644 io:: { self , Write } ,
4745 path:: PathBuf ,
4846} ;
47+
48+ use clang:: { Clang , Entity , EntityKind , Index , Type , TypeKind } ;
49+ use log:: debug;
50+
51+ use config:: DynamicLibraryConfig ;
52+ use dart_source_writer:: { DartSourceWriter , ImportedUri } ;
53+ use errors:: CodegenError ;
54+ use func:: { Func , Param } ;
55+ use structure:: { Field , Struct } ;
56+
4957/// Bindgens config for loading `DynamicLibrary` on each Platform.
5058pub mod config;
51- use config:: DynamicLibraryConfig ;
5259
5360mod dart_source_writer;
54- use dart_source_writer:: { DartSourceWriter , ImportedUri } ;
5561
5662mod errors;
57- use errors:: CodegenError ;
5863
5964mod structure;
60- use structure:: { Field , Struct } ;
6165
6266mod func;
63- use func:: { Func , Param } ;
6467
6568/// Abstract over Func, Struct and Global.
6669trait Element {
@@ -73,10 +76,12 @@ trait Element {
7376 /// Used to Write the Current Element to the Final Source File
7477 fn generate_source ( & self , w : & mut DartSourceWriter ) -> io:: Result < ( ) > ;
7578}
79+
7680/// Dart Code Generator
7781pub struct Codegen {
7882 src_header : PathBuf ,
7983 lib_name : String ,
84+ allo_isolate : bool ,
8085 config : DynamicLibraryConfig ,
8186 elements : HashMap < String , Box < dyn Element > > ,
8287}
@@ -118,6 +123,23 @@ impl Codegen {
118123 self . elements . insert ( s. name ( ) . to_owned ( ) , Box :: new ( s) ) ;
119124 }
120125 }
126+ if self . allo_isolate {
127+ // push new element
128+ let params = vec ! [ Param :: new(
129+ Some ( "ptr" . to_string( ) ) ,
130+ "Pointer<NativeFunction<Int8 \
131+ Function(Int64, Pointer<Dart_CObject>)>>"
132+ . to_string( ) ,
133+ ) ] ;
134+ let func = Func :: new (
135+ "store_dart_post_cobject" . to_string ( ) ,
136+ None ,
137+ params,
138+ "void" . to_string ( ) ,
139+ ) ;
140+ self . elements
141+ . insert ( "store_dart_post_cobject" . to_string ( ) , Box :: new ( func) ) ;
142+ }
121143 debug ! ( "Generating Dart Source..." ) ;
122144 for el in self . elements . values ( ) {
123145 el. generate_source ( & mut dsw) ?;
@@ -299,13 +321,15 @@ impl fmt::Debug for Codegen {
299321 . finish ( )
300322 }
301323}
324+
302325/// The [`Codegen`] Builder
303326///
304327/// start by calling [`Codegen::builder()`]
305328#[ derive( Clone , Debug , Default ) ]
306329pub struct CodegenBuilder {
307330 src_header : PathBuf ,
308331 lib_name : String ,
332+ allo_isolate : bool ,
309333 config : Option < DynamicLibraryConfig > ,
310334}
311335
@@ -336,7 +360,10 @@ impl CodegenBuilder {
336360 ///
337361 /// This allow dart-bindgen to add the code required by allo-isolate
338362 /// i.e `store_dart_post_cobject` fn
339- pub const fn with_allo_isoate ( mut self ) -> Self { self }
363+ pub const fn with_allo_isolate ( mut self ) -> Self {
364+ self . allo_isolate = true ;
365+ self
366+ }
340367
341368 /// Consumes the builder and validate everyting, then create the [`Codegen`]
342369 pub fn build ( self ) -> Result < Codegen , CodegenError > {
@@ -353,6 +380,7 @@ impl CodegenBuilder {
353380 Ok ( Codegen {
354381 src_header : self . src_header ,
355382 lib_name : self . lib_name ,
383+ allo_isolate : self . allo_isolate ,
356384 config,
357385 elements : HashMap :: new ( ) ,
358386 } )
0 commit comments