Skip to content
This repository was archived by the owner on Oct 10, 2023. It is now read-only.

Commit a64a28a

Browse files
author
Shady Khalifa
committed
add allo-isolate to the cli too
1 parent 00135b3 commit a64a28a

File tree

5 files changed

+367
-116
lines changed

5 files changed

+367
-116
lines changed

bin/cli.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ struct Args {
1313
/// for example if the input header is `foo.h` the output `foo.dart`
1414
#[argh(option, short = 'o')]
1515
output: Option<PathBuf>,
16+
/// integrate with `allo-isolate` package and generate the binding for
17+
/// `store_dart_post_cobject` function
18+
#[argh(switch)]
19+
allo_isolate: bool,
1620
/// the C library name (for docs)
1721
/// defaults for the name of the input header
1822
/// example if the input header is `foo.h` the name would be `libfoo`
@@ -102,11 +106,16 @@ fn main() -> Result<()> {
102106
.ok_or_else(|| anyhow!("Could't make the libname"))?,
103107
};
104108

105-
let codegen = Codegen::builder()
109+
let mut builder = Codegen::builder()
106110
.with_lib_name(name)
107111
.with_src_header(args.input)
108-
.with_config(config)
109-
.build()?;
112+
.with_config(config);
113+
114+
if args.allo_isolate {
115+
builder = builder.with_allo_isolate();
116+
}
117+
118+
let codegen = builder.build()?;
110119

111120
let bindings = codegen.generate()?;
112121
bindings.write_to_file(output_path)?;

src/lib.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,10 @@ use structure::{Field, Struct};
5656

5757
/// Bindgens config for loading `DynamicLibrary` on each Platform.
5858
pub mod config;
59-
6059
mod dart_source_writer;
61-
6260
mod errors;
63-
64-
mod structure;
65-
6661
mod func;
62+
mod structure;
6763

6864
/// Abstract over Func, Struct and Global.
6965
trait Element {
@@ -124,24 +120,28 @@ impl Codegen {
124120
}
125121
}
126122
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-
)];
134123
let func = Func::new(
135124
"store_dart_post_cobject".to_string(),
136125
None,
137-
params,
126+
vec![Param::new(
127+
Some("ptr".to_string()),
128+
"Pointer<NativeFunction<Int8 \
129+
Function(Int64, Pointer<Dart_CObject>)>>"
130+
.to_string(),
131+
)],
138132
"void".to_string(),
139133
);
134+
// insert new element
140135
self.elements
141136
.insert("store_dart_post_cobject".to_string(), Box::new(func));
142137
}
143138
debug!("Generating Dart Source...");
144-
for el in self.elements.values() {
139+
// trying to sort the elements to avoid useless changes in git for
140+
// example since HashMap is not `Ord`
141+
let mut elements: Vec<_> = self.elements.values().collect();
142+
elements.sort_by_key(|k| k.name());
143+
144+
for el in elements {
145145
el.generate_source(&mut dsw)?;
146146
}
147147
debug!("Done.");

tests/headers/simple.h

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/out/allo-isolate.dart

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
/// bindings for `libkeystore`
2+
3+
import 'dart:ffi';
4+
import 'dart:io';
5+
import 'package:ffi/ffi.dart' as ffi;
6+
7+
// ignore_for_file: unused_import, camel_case_types, non_constant_identifier_names
8+
final DynamicLibrary _dl = _open();
9+
DynamicLibrary _open() {
10+
if (Platform.isWindows) return DynamicLibrary.open('keystore.dll');
11+
if (Platform.isAndroid) return DynamicLibrary.open('libkeystore.so');
12+
if (Platform.isIOS) return DynamicLibrary.executable();
13+
throw UnsupportedError('This platform is not supported.');
14+
}
15+
16+
/// C function `account_destroy`.
17+
void account_destroy(
18+
Pointer account,
19+
) {
20+
_account_destroy(account);
21+
}
22+
final _account_destroy_Dart _account_destroy = _dl.lookupFunction<_account_destroy_C, _account_destroy_Dart>('account_destroy');
23+
typedef _account_destroy_C = Void Function(
24+
Pointer account,
25+
);
26+
typedef _account_destroy_Dart = void Function(
27+
Pointer account,
28+
);
29+
30+
/// C function `error_message_utf8`.
31+
int error_message_utf8(
32+
Pointer<ffi.Utf8> buf,
33+
int length,
34+
) {
35+
return _error_message_utf8(buf, length);
36+
}
37+
final _error_message_utf8_Dart _error_message_utf8 = _dl.lookupFunction<_error_message_utf8_C, _error_message_utf8_Dart>('error_message_utf8');
38+
typedef _error_message_utf8_C = Int32 Function(
39+
Pointer<ffi.Utf8> buf,
40+
Int32 length,
41+
);
42+
typedef _error_message_utf8_Dart = int Function(
43+
Pointer<ffi.Utf8> buf,
44+
int length,
45+
);
46+
47+
/// C function `keystore_account`.
48+
Pointer keystore_account(
49+
Pointer keystore,
50+
) {
51+
return _keystore_account(keystore);
52+
}
53+
final _keystore_account_Dart _keystore_account = _dl.lookupFunction<_keystore_account_C, _keystore_account_Dart>('keystore_account');
54+
typedef _keystore_account_C = Pointer Function(
55+
Pointer keystore,
56+
);
57+
typedef _keystore_account_Dart = Pointer Function(
58+
Pointer keystore,
59+
);
60+
61+
/// C function `keystore_destroy`.
62+
void keystore_destroy(
63+
Pointer keystore,
64+
) {
65+
_keystore_destroy(keystore);
66+
}
67+
final _keystore_destroy_Dart _keystore_destroy = _dl.lookupFunction<_keystore_destroy_C, _keystore_destroy_Dart>('keystore_destroy');
68+
typedef _keystore_destroy_C = Void Function(
69+
Pointer keystore,
70+
);
71+
typedef _keystore_destroy_Dart = void Function(
72+
Pointer keystore,
73+
);
74+
75+
/// C function `keystore_from_keyfile`.
76+
Pointer keystore_from_keyfile(
77+
Pointer<ffi.Utf8> path,
78+
) {
79+
return _keystore_from_keyfile(path);
80+
}
81+
final _keystore_from_keyfile_Dart _keystore_from_keyfile = _dl.lookupFunction<_keystore_from_keyfile_C, _keystore_from_keyfile_Dart>('keystore_from_keyfile');
82+
typedef _keystore_from_keyfile_C = Pointer Function(
83+
Pointer<ffi.Utf8> path,
84+
);
85+
typedef _keystore_from_keyfile_Dart = Pointer Function(
86+
Pointer<ffi.Utf8> path,
87+
);
88+
89+
/// C function `keystore_generate`.
90+
int keystore_generate(
91+
Pointer keystore,
92+
Pointer<ffi.Utf8> password,
93+
) {
94+
return _keystore_generate(keystore, password);
95+
}
96+
final _keystore_generate_Dart _keystore_generate = _dl.lookupFunction<_keystore_generate_C, _keystore_generate_Dart>('keystore_generate');
97+
typedef _keystore_generate_C = Int32 Function(
98+
Pointer keystore,
99+
Pointer<ffi.Utf8> password,
100+
);
101+
typedef _keystore_generate_Dart = int Function(
102+
Pointer keystore,
103+
Pointer<ffi.Utf8> password,
104+
);
105+
106+
/// C function `keystore_import`.
107+
int keystore_import(
108+
Pointer keystore,
109+
Pointer<ffi.Utf8> phrase,
110+
Pointer<ffi.Utf8> password,
111+
) {
112+
return _keystore_import(keystore, phrase, password);
113+
}
114+
final _keystore_import_Dart _keystore_import = _dl.lookupFunction<_keystore_import_C, _keystore_import_Dart>('keystore_import');
115+
typedef _keystore_import_C = Int32 Function(
116+
Pointer keystore,
117+
Pointer<ffi.Utf8> phrase,
118+
Pointer<ffi.Utf8> password,
119+
);
120+
typedef _keystore_import_Dart = int Function(
121+
Pointer keystore,
122+
Pointer<ffi.Utf8> phrase,
123+
Pointer<ffi.Utf8> password,
124+
);
125+
126+
/// C function `keystore_lock`.
127+
int keystore_lock(
128+
Pointer keystore,
129+
) {
130+
return _keystore_lock(keystore);
131+
}
132+
final _keystore_lock_Dart _keystore_lock = _dl.lookupFunction<_keystore_lock_C, _keystore_lock_Dart>('keystore_lock');
133+
typedef _keystore_lock_C = Int32 Function(
134+
Pointer keystore,
135+
);
136+
typedef _keystore_lock_Dart = int Function(
137+
Pointer keystore,
138+
);
139+
140+
/// C function `keystore_new`.
141+
Pointer keystore_new() {
142+
return _keystore_new();
143+
}
144+
final _keystore_new_Dart _keystore_new = _dl.lookupFunction<_keystore_new_C, _keystore_new_Dart>('keystore_new');
145+
typedef _keystore_new_C = Pointer Function();
146+
typedef _keystore_new_Dart = Pointer Function();
147+
148+
/// C function `keystore_paper_backup`.
149+
int keystore_paper_backup(
150+
Pointer keystore,
151+
) {
152+
return _keystore_paper_backup(keystore);
153+
}
154+
final _keystore_paper_backup_Dart _keystore_paper_backup = _dl.lookupFunction<_keystore_paper_backup_C, _keystore_paper_backup_Dart>('keystore_paper_backup');
155+
typedef _keystore_paper_backup_C = Int32 Function(
156+
Pointer keystore,
157+
);
158+
typedef _keystore_paper_backup_Dart = int Function(
159+
Pointer keystore,
160+
);
161+
162+
/// C function `keystore_phrase`.
163+
Pointer<ffi.Utf8> keystore_phrase(
164+
Pointer keystore,
165+
Pointer<ffi.Utf8> password,
166+
) {
167+
return _keystore_phrase(keystore, password);
168+
}
169+
final _keystore_phrase_Dart _keystore_phrase = _dl.lookupFunction<_keystore_phrase_C, _keystore_phrase_Dart>('keystore_phrase');
170+
typedef _keystore_phrase_C = Pointer<ffi.Utf8> Function(
171+
Pointer keystore,
172+
Pointer<ffi.Utf8> password,
173+
);
174+
typedef _keystore_phrase_Dart = Pointer<ffi.Utf8> Function(
175+
Pointer keystore,
176+
Pointer<ffi.Utf8> password,
177+
);
178+
179+
/// C function `keystore_set_paper_backup`.
180+
int keystore_set_paper_backup(
181+
Pointer keystore,
182+
) {
183+
return _keystore_set_paper_backup(keystore);
184+
}
185+
final _keystore_set_paper_backup_Dart _keystore_set_paper_backup = _dl.lookupFunction<_keystore_set_paper_backup_C, _keystore_set_paper_backup_Dart>('keystore_set_paper_backup');
186+
typedef _keystore_set_paper_backup_C = Int32 Function(
187+
Pointer keystore,
188+
);
189+
typedef _keystore_set_paper_backup_Dart = int Function(
190+
Pointer keystore,
191+
);
192+
193+
/// C function `keystore_status`.
194+
int keystore_status(
195+
Pointer keystore,
196+
) {
197+
return _keystore_status(keystore);
198+
}
199+
final _keystore_status_Dart _keystore_status = _dl.lookupFunction<_keystore_status_C, _keystore_status_Dart>('keystore_status');
200+
typedef _keystore_status_C = Int32 Function(
201+
Pointer keystore,
202+
);
203+
typedef _keystore_status_Dart = int Function(
204+
Pointer keystore,
205+
);
206+
207+
/// C function `keystore_unlock`.
208+
int keystore_unlock(
209+
Pointer keystore,
210+
Pointer<ffi.Utf8> password,
211+
) {
212+
return _keystore_unlock(keystore, password);
213+
}
214+
final _keystore_unlock_Dart _keystore_unlock = _dl.lookupFunction<_keystore_unlock_C, _keystore_unlock_Dart>('keystore_unlock');
215+
typedef _keystore_unlock_C = Int32 Function(
216+
Pointer keystore,
217+
Pointer<ffi.Utf8> password,
218+
);
219+
typedef _keystore_unlock_Dart = int Function(
220+
Pointer keystore,
221+
Pointer<ffi.Utf8> password,
222+
);
223+
224+
/// C function `last_error_length`.
225+
int last_error_length() {
226+
return _last_error_length();
227+
}
228+
final _last_error_length_Dart _last_error_length = _dl.lookupFunction<_last_error_length_C, _last_error_length_Dart>('last_error_length');
229+
typedef _last_error_length_C = Int32 Function();
230+
typedef _last_error_length_Dart = int Function();
231+
232+
/// C function `phrase_destroy`.
233+
void phrase_destroy(
234+
Pointer<ffi.Utf8> phrase,
235+
) {
236+
_phrase_destroy(phrase);
237+
}
238+
final _phrase_destroy_Dart _phrase_destroy = _dl.lookupFunction<_phrase_destroy_C, _phrase_destroy_Dart>('phrase_destroy');
239+
typedef _phrase_destroy_C = Void Function(
240+
Pointer<ffi.Utf8> phrase,
241+
);
242+
typedef _phrase_destroy_Dart = void Function(
243+
Pointer<ffi.Utf8> phrase,
244+
);
245+
246+
/// C function `store_dart_post_cobject`.
247+
void store_dart_post_cobject(
248+
Pointer<NativeFunction<Int8 Function(Int64, Pointer<Dart_CObject>)>> ptr,
249+
) {
250+
_store_dart_post_cobject(ptr);
251+
}
252+
final _store_dart_post_cobject_Dart _store_dart_post_cobject = _dl.lookupFunction<_store_dart_post_cobject_C, _store_dart_post_cobject_Dart>('store_dart_post_cobject');
253+
typedef _store_dart_post_cobject_C = Void Function(
254+
Pointer<NativeFunction<Int8 Function(Int64, Pointer<Dart_CObject>)>> ptr,
255+
);
256+
typedef _store_dart_post_cobject_Dart = void Function(
257+
Pointer<NativeFunction<Int8 Function(Int64, Pointer<Dart_CObject>)>> ptr,
258+
);

0 commit comments

Comments
 (0)