1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use super::*;
#[derive(Debug)]
pub(crate) struct GeneratedApiMethod {
pub comments: String,
pub signature: String,
pub implementation: String,
}
pub(crate) fn generate_api_func(
func: &IrFunc,
ir_file: &IrFile,
common_api2wire_body: &str,
) -> GeneratedApiFunc {
let raw_func_param_list = func
.inputs
.iter()
.map(|input| {
format!(
"{required}{} {} {default}",
input.ty.dart_api_type(),
input.name.dart_style(),
required = input.required_modifier(),
default = input.field_default(false),
)
})
.collect::<Vec<_>>();
let full_func_param_list = [raw_func_param_list, vec!["dynamic hint".to_owned()]].concat();
let prepare_args = func
.inputs
.iter()
.enumerate()
.map(|(index, input)| {
if let Primitive(IrTypePrimitive::Bool) = input.ty {
format!("var arg{index} = {};", input.name.dart_style())
} else {
let func = format!("api2wire_{}", input.ty.safe_ident());
format!(
"var arg{index} = {}{}({});",
if common_api2wire_body.contains(&func) {
""
} else {
"_platform."
},
func,
&input.name.dart_style()
)
}
})
.collect::<Vec<_>>();
let wire_param_list = [
if func.mode.has_port_argument() {
vec!["port_".to_owned()]
} else {
vec![]
},
(0..prepare_args.len())
.map(|index| format!("arg{index}"))
.collect_vec(),
]
.concat();
let func_expr = format!(
"{} {}({{ {} }})",
func.mode.dart_return_type(&func.output.dart_api_type()),
func.name.to_case(Case::Camel),
full_func_param_list.join(","),
);
let execute_func_name = match func.mode {
IrFuncMode::Normal => "_platform.executeNormal",
IrFuncMode::Sync => "_platform.executeSync",
IrFuncMode::Stream { .. } => "_platform.executeStream",
};
let const_meta_field_name = format!("k{}ConstMeta", func.name.to_case(Case::Pascal));
let signature = format!("{func_expr};");
let comments = dart_comments(&func.comments);
let task_common_args = format!(
"
constMeta: {},
argValues: [{}],
hint: hint,
",
const_meta_field_name,
func.inputs
.iter()
.map(|input| input.name.dart_style())
.collect::<Vec<_>>()
.join(", "),
);
let input_0 = func.inputs.get(0).as_ref().map(|x| &x.ty);
let input_0_struct_name = if let Some(StructRef(IrTypeStructRef { name, .. })) = &input_0 {
Some(name)
} else {
None
};
let f = FunctionName::deserialize(&func.name);
let func_output_struct_name = if let StructRef(IrTypeStructRef { name, .. }) = &func.output {
Some(name)
} else {
None
};
let parse_success_data = if (f.is_static_method()
&& f.struct_name().unwrap() == {
if let IrType::StructRef(IrTypeStructRef { name, .. }) = &func.output {
name.clone()
} else {
String::new()
}
})
|| (input_0_struct_name.is_some() && MethodNamingUtil::has_methods(input_0_struct_name.unwrap(), ir_file))
|| (func_output_struct_name.is_some()
&& MethodNamingUtil::has_methods(func_output_struct_name.unwrap(), ir_file))
{
format!("(d) => _wire2api_{}(d)", func.output.safe_ident())
} else {
format!("_wire2api_{}", func.output.safe_ident())
};
let is_sync = matches!(func.mode, IrFuncMode::Sync);
let implementation = format!(
"{} {{
{}
return {}({task}(
callFfi: ({args}) => _platform.inner.{}({}),
parseSuccessData: {},
{}
));}}",
func_expr,
prepare_args.join("\n"),
execute_func_name,
func.wire_func_name(),
wire_param_list.join(", "),
parse_success_data,
task_common_args,
task = if is_sync {
"FlutterRustBridgeSyncTask"
} else {
"FlutterRustBridgeTask"
},
args = if is_sync { "" } else { "port_" },
);
let companion_field_signature =
format!("FlutterRustBridgeTaskConstMeta get {const_meta_field_name};");
let companion_field_implementation = format!(
"
FlutterRustBridgeTaskConstMeta get {const_meta_field_name} => const FlutterRustBridgeTaskConstMeta(
debugName: \"{}\",
argNames: [{}],
);
",
func.name,
func.inputs
.iter()
.map(|input| format!("\"{}\"", input.name.dart_style()))
.collect::<Vec<_>>()
.join(", "),
);
GeneratedApiFunc {
signature,
implementation,
comments,
companion_field_signature,
companion_field_implementation,
}
}
pub(crate) fn generate_opaque_getters(ty: &IrType) -> GeneratedApiFunc {
let signature = format!(
"
DropFnType get dropOpaque{0};
ShareFnType get shareOpaque{0};
OpaqueTypeFinalizer get {0}Finalizer;
",
ty.dart_api_type(),
);
let implementation = format!(
"
DropFnType get dropOpaque{0} => _platform.inner.drop_opaque_{0};
ShareFnType get shareOpaque{0} => _platform.inner.share_opaque_{0};
OpaqueTypeFinalizer get {0}Finalizer => _platform.{0}Finalizer;
",
ty.dart_api_type()
);
GeneratedApiFunc {
signature,
implementation,
comments: String::new(),
companion_field_signature: String::new(),
companion_field_implementation: String::new(),
}
}