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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! WASM struct method code generation.
use crate::backends::wasm::type_map::WasmMapper;
use crate::codegen::type_mapper::TypeMapper;
use crate::codegen::{generators, naming::to_node_name, shared};
use crate::core::ir::{MethodDef, TypeDef, TypeRef};
use ahash::AHashSet;
use heck::ToPascalCase;
use super::functions::{emit_rustdoc, format_param_unused, wasm_wrap_return};
/// Generate a method binding for a struct method.
#[allow(clippy::too_many_arguments)]
pub(super) fn gen_method(
method: &MethodDef,
mapper: &WasmMapper,
type_name: &str,
core_import: &str,
opaque_types: &AHashSet<String>,
prefix: &str,
typ: &TypeDef,
mutex_types: &AHashSet<String>,
streaming_item_types: &ahash::AHashMap<String, String>,
) -> String {
// Check if the type has any RefMut methods (which means inner is wrapped in Mutex).
let has_mut_methods = typ
.methods
.iter()
.any(|m| matches!(m.receiver.as_ref(), Some(crate::core::ir::ReceiverKind::RefMut)));
let is_ref_mut_receiver = matches!(method.receiver.as_ref(), Some(crate::core::ir::ReceiverKind::RefMut));
// For opaque types, allow delegation of RefMut methods if the type is Mutex-wrapped.
// Arc<T> doesn't support &mut T directly, but Arc<Mutex<T>> does via lock().
let can_delegate_base = shared::can_auto_delegate(method, opaque_types);
let can_delegate = if is_ref_mut_receiver && has_mut_methods {
// RefMut methods are delegatable if the type has Mutex wrapping
!method.sanitized
&& method
.params
.iter()
.all(|p| !p.sanitized && crate::codegen::shared::is_delegatable_param(&p.ty, opaque_types))
&& crate::codegen::shared::is_opaque_delegatable_type(&method.return_type)
} else {
can_delegate_base
};
let params: Vec<String> = method
.params
.iter()
.map(|p| {
let ty = mapper.map_type(&p.ty);
let mapped_ty = if p.optional { format!("Option<{}>", ty) } else { ty };
format_param_unused(&p.name, &mapped_ty, !can_delegate && !method.is_async)
})
.collect();
let adapter_key_for_stream = format!("{}.{}", type_name, method.name);
let stream_item = streaming_item_types.get(&adapter_key_for_stream);
let return_type = if stream_item.is_some() {
// For streaming methods, return the iterator struct (not the item type).
// The iterator struct name is {PascalCaseMethodName}Iterator.
format!("{}Iterator", method.name.to_pascal_case())
} else {
mapper.map_type(&method.return_type)
};
let return_annotation = mapper.wrap_return(&return_type, method.error_type.is_some());
let js_name = to_node_name(&method.name);
let js_name_attr = if js_name != method.name {
format!("(js_name = \"{}\")", js_name)
} else {
String::new()
};
let mut attrs = emit_rustdoc(&method.doc);
// Per-item clippy suppression: too_many_arguments when >7 params (including &self for instance methods)
let effective_param_count = if method.is_static {
method.params.len()
} else {
method.params.len() + 1
};
if effective_param_count > 7 {
attrs.push_str("#[allow(clippy::too_many_arguments)]\n");
}
// Per-item clippy suppression: missing_errors_doc for Result-returning methods
if method.error_type.is_some() {
attrs.push_str("#[allow(clippy::missing_errors_doc)]\n");
}
// Per-item clippy suppression: should_implement_trait for trait-conflicting names
if generators::is_trait_method_name(&method.name) {
attrs.push_str("#[allow(clippy::should_implement_trait)]\n");
}
if method.is_async {
// For async methods with named params, use JsValue parameters to avoid _assertClass errors
let has_named = crate::codegen::generators::has_named_params(&method.params, opaque_types);
let async_params: Vec<String> = if has_named {
method
.params
.iter()
.map(|p| match &p.ty {
TypeRef::Named(name) if !opaque_types.contains(name.as_str()) => {
let mapped_ty = if p.optional {
"Option<wasm_bindgen::JsValue>".to_string()
} else {
"wasm_bindgen::JsValue".to_string()
};
format!("{}: {}", p.name, mapped_ty)
}
_ => {
let ty = mapper.map_type(&p.ty);
let mapped_ty = if p.optional { format!("Option<{}>", ty) } else { ty };
format!("{}: {}", p.name, mapped_ty)
}
})
.collect()
} else {
params.clone()
};
// Generate serde deserialization let-bindings for named non-opaque params
let mut serde_bindings = String::new();
if has_named {
for p in &method.params {
if let crate::core::ir::TypeRef::Named(name) = &p.ty {
if !opaque_types.contains(name.as_str()) {
let core_path = format!("{}::{}", core_import, name);
let err_conv = ".map_err(|e| wasm_bindgen::JsValue::from_str(&e.to_string()))";
if p.optional {
serde_bindings.push_str(&crate::backends::wasm::template_env::render(
"serde_named_optional",
minijinja::context! {
param_name => &p.name,
core_path => &core_path,
err_conv => &err_conv,
},
));
serde_bindings.push_str(" ");
} else {
// In methods context, we only have the current TypeDef, not the full API.
// We assume types without Default will be caught at generation time by validation.
// For now, conservatively pass false (require the param when undefined).
let has_default = false;
serde_bindings.push_str(&crate::backends::wasm::template_env::render(
"serde_named_required",
minijinja::context! {
param_name => &p.name,
core_path => &core_path,
err_conv => &err_conv,
has_default => has_default,
is_mut => p.is_mut,
},
));
serde_bindings.push_str(" ");
}
}
}
}
}
let let_bindings = serde_bindings;
let call_args = if let_bindings.is_empty() {
generators::gen_call_args(&method.params, opaque_types)
} else {
generators::gen_call_args_with_let_bindings(&method.params, opaque_types)
};
let is_opaque_type = opaque_types.contains(type_name);
let core_call = if is_opaque_type && has_mut_methods && !is_ref_mut_receiver {
// Opaque types whose inner is `Arc<Mutex<T>>` (the type has any `&mut self`
// method) must access through `.lock().unwrap()` even for immutable methods.
format!(
"self.inner.lock().unwrap().{method_name}({call_args})",
method_name = method.name
)
} else {
// Transparent (named-field) structs go through `From<WasmT> for T`. The
// previous unconditional Mutex branch broke any non-opaque type whose
// type had a sibling `&mut self` method (e.g. `WasmDocumentStructure`).
format!(
"{core_import}::{type_name}::from(self.clone()).{method_name}({call_args})",
method_name = method.name
)
};
let return_type_tf = to_turbofish_from(&return_type);
let return_expr = format!("{return_type_tf}::from(result)");
let body = crate::backends::wasm::template_env::render(
"gen_result_body",
minijinja::context! {
let_bindings => &let_bindings,
core_call => &core_call,
return_expr => &return_expr,
is_async => true,
map_wasm_error => method.error_type.is_some(),
map_js_error => false,
ok_return => true,
},
);
crate::backends::wasm::template_env::render(
"gen_instance_method",
minijinja::context! {
attrs => &attrs,
js_name_attr => &js_name_attr,
is_async => true,
method_name => &method.name,
params => async_params.join(", "),
return_annotation => &return_annotation,
body => body.trim_end(),
},
)
} else if method.is_static {
let body = if can_delegate {
// WASM does not use optional promotion, so use gen_named_let_bindings_no_promote.
let let_bindings = if crate::codegen::generators::has_named_params(&method.params, opaque_types) {
crate::codegen::generators::gen_named_let_bindings_no_promote(&method.params, opaque_types, core_import)
} else {
String::new()
};
// For lifetime-parameterized types, emit let bindings for String→Cow and
// JsValue→BTreeMap conversions, and rename borrowed→owned constructors.
let is_borrowed_to_owned = method.name.contains("borrowed_attributes");
let lifetime_bindings = if typ.has_lifetime_params {
let mut bindings = String::new();
for p in &method.params {
match &p.ty {
TypeRef::String => {
let template_name = if p.optional {
"lifetime_string_optional"
} else {
"lifetime_string_required"
};
bindings.push_str(&crate::backends::wasm::template_env::render(
template_name,
minijinja::context! {
param_name => &p.name,
},
));
bindings.push_str(" ");
}
TypeRef::Map(_, _) => {
// JsValue → BTreeMap: deserialize via serde_wasm_bindgen
bindings.push_str(&crate::backends::wasm::template_env::render(
"lifetime_map_required",
minijinja::context! {
param_name => &p.name,
},
));
bindings.push_str(" ");
}
TypeRef::Optional(inner) if matches!(inner.as_ref(), TypeRef::String) => {
bindings.push_str(&crate::backends::wasm::template_env::render(
"lifetime_string_optional",
minijinja::context! {
param_name => &p.name,
},
));
bindings.push_str(" ");
}
_ => {}
}
}
bindings
} else {
String::new()
};
let (call_args, actual_method_name) = if !lifetime_bindings.is_empty() {
// Adjust call args to use converted variables and drop & borrow for Map params
// when switching from borrowed→owned constructor.
let base_call_args = if let_bindings.is_empty() {
generators::gen_call_args(&method.params, opaque_types)
} else {
generators::gen_call_args_with_let_bindings(&method.params, opaque_types)
};
let mut adjusted = base_call_args;
for p in &method.params {
match &p.ty {
TypeRef::Map(_, _) => {
if is_borrowed_to_owned && p.is_ref {
adjusted = adjusted.replace(&format!("&{}", p.name), &format!("{}_converted", p.name));
} else {
adjusted = adjusted.replace(p.name.as_str(), &format!("{}_converted", p.name));
}
}
TypeRef::String => {
adjusted = adjusted.replace(p.name.as_str(), &format!("{}_converted", p.name));
}
TypeRef::Optional(inner) if matches!(inner.as_ref(), TypeRef::String) => {
adjusted = adjusted.replace(p.name.as_str(), &format!("{}_converted", p.name));
}
_ => {}
}
}
let method_name = if is_borrowed_to_owned {
method.name.replace("borrowed", "owned")
} else {
method.name.clone()
};
(adjusted, method_name)
} else {
let base_call_args = if let_bindings.is_empty() {
generators::gen_call_args(&method.params, opaque_types)
} else {
generators::gen_call_args_with_let_bindings(&method.params, opaque_types)
};
(base_call_args, method.name.clone())
};
let combined_let_bindings = format!("{let_bindings}{lifetime_bindings}");
let core_call = format!("{core_import}::{type_name}::{actual_method_name}({call_args})");
if method.error_type.is_some() {
let wrap = wasm_wrap_return(
"result",
&method.return_type,
type_name,
opaque_types,
false,
method.returns_ref,
method.returns_cow,
prefix,
mutex_types,
);
crate::backends::wasm::template_env::render(
"gen_result_body",
minijinja::context! {
let_bindings => &combined_let_bindings,
core_call => &core_call,
return_expr => &wrap,
is_async => false,
map_wasm_error => false,
map_js_error => true,
ok_return => true,
},
)
} else {
let return_expr = wasm_wrap_return(
&core_call,
&method.return_type,
type_name,
opaque_types,
false,
method.returns_ref,
method.returns_cow,
prefix,
mutex_types,
);
crate::backends::wasm::template_env::render(
"gen_direct_body",
minijinja::context! {
let_bindings => &combined_let_bindings,
return_expr => &return_expr,
},
)
}
} else {
super::functions::gen_wasm_unimplemented_body(
&method.return_type,
&method.name,
method.error_type.is_some(),
)
};
crate::backends::wasm::template_env::render(
"gen_static_method",
minijinja::context! {
attrs => &attrs,
js_name_attr => &js_name_attr,
method_name => &method.name,
params => params.join(", "),
return_annotation => &return_annotation,
body => body.trim_end(),
},
)
} else {
let body = if can_delegate {
// WASM does not use optional promotion, so use gen_named_let_bindings_no_promote.
let let_bindings = if crate::codegen::generators::has_named_params(&method.params, opaque_types) {
crate::codegen::generators::gen_named_let_bindings_no_promote(&method.params, opaque_types, core_import)
} else {
String::new()
};
let call_args = if let_bindings.is_empty() {
generators::gen_call_args(&method.params, opaque_types)
} else {
generators::gen_call_args_with_let_bindings(&method.params, opaque_types)
};
let is_opaque_type = opaque_types.contains(type_name);
let core_call = if is_opaque_type && has_mut_methods && !is_ref_mut_receiver {
// Opaque types whose inner is `Arc<Mutex<T>>` must lock for any method.
format!(
"self.inner.lock().unwrap().{method_name}({call_args})",
method_name = method.name
)
} else {
format!(
"{core_import}::{type_name}::from(self.clone()).{method_name}({call_args})",
method_name = method.name
)
};
if method.error_type.is_some() {
let wrap = wasm_wrap_return(
"result",
&method.return_type,
type_name,
opaque_types,
false,
method.returns_ref,
method.returns_cow,
prefix,
mutex_types,
);
crate::backends::wasm::template_env::render(
"gen_result_body",
minijinja::context! {
let_bindings => &let_bindings,
core_call => &core_call,
return_expr => &wrap,
is_async => false,
map_wasm_error => false,
map_js_error => true,
ok_return => true,
},
)
} else {
let return_expr = wasm_wrap_return(
&core_call,
&method.return_type,
type_name,
opaque_types,
false,
method.returns_ref,
method.returns_cow,
prefix,
mutex_types,
);
crate::backends::wasm::template_env::render(
"gen_direct_body",
minijinja::context! {
let_bindings => &let_bindings,
return_expr => &return_expr,
},
)
}
} else {
super::functions::gen_wasm_unimplemented_body(
&method.return_type,
&method.name,
method.error_type.is_some(),
)
};
crate::backends::wasm::template_env::render(
"gen_instance_method",
minijinja::context! {
attrs => &attrs,
js_name_attr => &js_name_attr,
is_async => false,
method_name => &method.name,
params => params.join(", "),
return_annotation => &return_annotation,
body => body.trim_end(),
},
)
}
}
/// Returns a type name in turbofish form for use before `::from(expr)`.
///
/// Rust requires turbofish when a type has generic parameters and sits before `::`:
/// `Vec<T>::from(x)` is a syntax error — `Vec::<T>::from(x)` is required.
/// Non-generic type names are returned unchanged.
fn to_turbofish_from(type_name: &str) -> String {
if let Some(idx) = type_name.find('<') {
format!("{}::{}", &type_name[..idx], &type_name[idx..])
} else {
type_name.to_string()
}
}