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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! # Object definitions for a `ComponentInterface`.
//!
//! This module converts "interface" definitions from UDL into [`Object`] structures
//! that can be added to a `ComponentInterface`, which are the main way we define stateful
//! objects with behaviour for a UniFFI Rust Component. An [`Object`] is an opaque handle
//! to some state on which methods can be invoked.
//!
//! (The terminology mismatch between "interface" and "object" is a historical artifact of
//! this tool prior to committing to WebIDL syntax).
//!
//! A declaration in the UDL like this:
//!
//! ```
//! # let ci = uniffi_bindgen::interface::ComponentInterface::from_webidl(r##"
//! # namespace example {};
//! interface Example {
//! constructor(string? name);
//! string my_name();
//! };
//! # "##)?;
//! # Ok::<(), anyhow::Error>(())
//! ```
//!
//! Will result in an [`Object`] member with one [`Constructor`] and one [`Method`] being added
//! to the resulting [`ComponentInterface`]:
//!
//! ```
//! # let ci = uniffi_bindgen::interface::ComponentInterface::from_webidl(r##"
//! # namespace example {};
//! # interface Example {
//! # constructor(string? name);
//! # string my_name();
//! # };
//! # "##)?;
//! let obj = ci.get_object_definition("Example").unwrap();
//! assert_eq!(obj.name(), "Example");
//! assert_eq!(obj.constructors().len(), 1);
//! assert_eq!(obj.constructors()[0].arguments()[0].name(), "name");
//! assert_eq!(obj.methods().len(),1 );
//! assert_eq!(obj.methods()[0].name(), "my_name");
//! # Ok::<(), anyhow::Error>(())
//! ```
//!
//! It's not necessary for all interfaces to have constructors.
//! ```
//! # let ci = uniffi_bindgen::interface::ComponentInterface::from_webidl(r##"
//! # namespace example {};
//! # interface Example {};
//! # "##)?;
//! let obj = ci.get_object_definition("Example").unwrap();
//! assert_eq!(obj.name(), "Example");
//! assert_eq!(obj.constructors().len(), 0);
//! # Ok::<(), anyhow::Error>(())
//! ```
use std::convert::TryFrom;
use std::{collections::HashSet, iter};
use anyhow::{bail, Result};
use uniffi_meta::Checksum;
use super::attributes::{Attribute, ConstructorAttributes, InterfaceAttributes, MethodAttributes};
use super::ffi::{FfiArgument, FfiFunction, FfiType};
use super::function::Argument;
use super::types::{Type, TypeIterator};
use super::{convert_type, APIConverter, ComponentInterface};
/// An "object" is an opaque type that can be instantiated and passed around by reference,
/// have methods called on it, and so on - basically your classic Object Oriented Programming
/// type of deal, except without elaborate inheritance hierarchies.
///
/// In UDL these correspond to the `interface` keyword.
///
/// At the FFI layer, objects are represented by an opaque integer handle and a set of functions
/// a common prefix. The object's constructors are functions that return new objects by handle,
/// and its methods are functions that take a handle as first argument. The foreign language
/// binding code is expected to stitch these functions back together into an appropriate class
/// definition (or that language's equivalent thereof).
///
/// TODO:
/// - maybe "Class" would be a better name than "Object" here?
#[derive(Debug, Clone, Checksum)]
pub struct Object {
pub(super) name: String,
pub(super) constructors: Vec<Constructor>,
pub(super) methods: Vec<Method>,
// We don't include the FFIFunc in the hash calculation, because:
// - it is entirely determined by the other fields,
// so excluding it is safe.
// - its `name` property includes a checksum derived from the very
// hash value we're trying to calculate here, so excluding it
// avoids a weird circular dependency in the calculation.
#[checksum_ignore]
pub(super) ffi_func_free: FfiFunction,
#[checksum_ignore]
pub(super) uses_deprecated_threadsafe_attribute: bool,
}
impl Object {
pub(super) fn new(name: String) -> Object {
Object {
name,
constructors: Default::default(),
methods: Default::default(),
ffi_func_free: Default::default(),
uses_deprecated_threadsafe_attribute: false,
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn type_(&self) -> Type {
Type::Object(self.name.clone())
}
pub fn constructors(&self) -> Vec<&Constructor> {
self.constructors.iter().collect()
}
pub fn primary_constructor(&self) -> Option<&Constructor> {
self.constructors
.iter()
.find(|cons| cons.is_primary_constructor())
}
pub fn alternate_constructors(&self) -> Vec<&Constructor> {
self.constructors
.iter()
.filter(|cons| !cons.is_primary_constructor())
.collect()
}
pub fn methods(&self) -> Vec<&Method> {
self.methods.iter().collect()
}
pub fn get_method(&self, name: &str) -> Method {
let matches: Vec<_> = self.methods.iter().filter(|m| m.name() == name).collect();
match matches.len() {
1 => matches[0].clone(),
n => panic!("{n} methods named {name}"),
}
}
pub fn ffi_object_free(&self) -> &FfiFunction {
&self.ffi_func_free
}
pub fn uses_deprecated_threadsafe_attribute(&self) -> bool {
self.uses_deprecated_threadsafe_attribute
}
pub fn iter_ffi_function_definitions(&self) -> impl Iterator<Item = &FfiFunction> {
iter::once(&self.ffi_func_free)
.chain(self.constructors.iter().map(|f| &f.ffi_func))
.chain(self.methods.iter().map(|f| &f.ffi_func))
}
pub fn derive_ffi_funcs(&mut self, ci_prefix: &str) -> Result<()> {
// The name is already set if the function is defined through a proc-macro invocation
// rather than in UDL. Don't overwrite it in that case.
if self.ffi_func_free.name().is_empty() {
self.ffi_func_free.name = format!("ffi_{ci_prefix}_{}_object_free", self.name);
}
self.ffi_func_free.arguments = vec![FfiArgument {
name: "ptr".to_string(),
type_: FfiType::RustArcPtr(self.name().to_string()),
}];
self.ffi_func_free.return_type = None;
for cons in self.constructors.iter_mut() {
cons.derive_ffi_func(ci_prefix, &self.name);
}
for meth in self.methods.iter_mut() {
meth.derive_ffi_func(ci_prefix, &self.name)?;
}
Ok(())
}
pub fn iter_types(&self) -> TypeIterator<'_> {
Box::new(
self.methods
.iter()
.map(Method::iter_types)
.chain(self.constructors.iter().map(Constructor::iter_types))
.flatten(),
)
}
}
impl APIConverter<Object> for weedle::InterfaceDefinition<'_> {
fn convert(&self, ci: &mut ComponentInterface) -> Result<Object> {
if self.inheritance.is_some() {
bail!("interface inheritance is not supported");
}
let mut object = Object::new(self.identifier.0.to_string());
let attributes = match &self.attributes {
Some(attrs) => InterfaceAttributes::try_from(attrs)?,
None => Default::default(),
};
object.uses_deprecated_threadsafe_attribute = attributes.threadsafe();
// Convert each member into a constructor or method, guarding against duplicate names.
let mut member_names = HashSet::new();
for member in &self.members.body {
match member {
weedle::interface::InterfaceMember::Constructor(t) => {
let cons: Constructor = t.convert(ci)?;
if !member_names.insert(cons.name.clone()) {
bail!("Duplicate interface member name: \"{}\"", cons.name())
}
object.constructors.push(cons);
}
weedle::interface::InterfaceMember::Operation(t) => {
let mut method: Method = t.convert(ci)?;
if !member_names.insert(method.name.clone()) {
bail!("Duplicate interface member name: \"{}\"", method.name())
}
method.object_name = object.name.clone();
object.methods.push(method);
}
_ => bail!("no support for interface member type {:?} yet", member),
}
}
Ok(object)
}
}
// Represents a constructor for an object type.
//
// In the FFI, this will be a function that returns a pointer to an instance
// of the corresponding object type.
#[derive(Debug, Clone, Checksum)]
pub struct Constructor {
pub(super) name: String,
pub(super) arguments: Vec<Argument>,
// We don't include the FFIFunc in the hash calculation, because:
// - it is entirely determined by the other fields,
// so excluding it is safe.
// - its `name` property includes a checksum derived from the very
// hash value we're trying to calculate here, so excluding it
// avoids a weird circular dependency in the calculation.
#[checksum_ignore]
pub(super) ffi_func: FfiFunction,
pub(super) attributes: ConstructorAttributes,
}
impl Constructor {
pub fn name(&self) -> &str {
&self.name
}
pub fn arguments(&self) -> Vec<&Argument> {
self.arguments.iter().collect()
}
pub fn full_arguments(&self) -> Vec<Argument> {
self.arguments.to_vec()
}
pub fn ffi_func(&self) -> &FfiFunction {
&self.ffi_func
}
pub fn throws(&self) -> bool {
self.attributes.get_throws_err().is_some()
}
pub fn throws_name(&self) -> Option<&str> {
self.attributes.get_throws_err()
}
pub fn throws_type(&self) -> Option<Type> {
self.attributes
.get_throws_err()
.map(|name| Type::Error(name.to_owned()))
}
pub fn is_primary_constructor(&self) -> bool {
self.name == "new"
}
fn derive_ffi_func(&mut self, ci_prefix: &str, obj_name: &str) {
self.ffi_func.name = format!("{ci_prefix}_{obj_name}_{}", self.name);
self.ffi_func.arguments = self.arguments.iter().map(Into::into).collect();
self.ffi_func.return_type = Some(FfiType::RustArcPtr(obj_name.to_string()));
}
pub fn iter_types(&self) -> TypeIterator<'_> {
Box::new(self.arguments.iter().flat_map(Argument::iter_types))
}
}
impl Default for Constructor {
fn default() -> Self {
Constructor {
name: String::from("new"),
arguments: Vec::new(),
ffi_func: Default::default(),
attributes: Default::default(),
}
}
}
impl APIConverter<Constructor> for weedle::interface::ConstructorInterfaceMember<'_> {
fn convert(&self, ci: &mut ComponentInterface) -> Result<Constructor> {
let attributes = match &self.attributes {
Some(attr) => ConstructorAttributes::try_from(attr)?,
None => Default::default(),
};
Ok(Constructor {
name: String::from(attributes.get_name().unwrap_or("new")),
arguments: self.args.body.list.convert(ci)?,
ffi_func: Default::default(),
attributes,
})
}
}
// Represents an instance method for an object type.
//
// The FFI will represent this as a function whose first/self argument is a
// `FfiType::RustArcPtr` to the instance.
#[derive(Debug, Clone, Checksum)]
pub struct Method {
pub(super) name: String,
pub(super) object_name: String,
pub(super) arguments: Vec<Argument>,
pub(super) return_type: Option<Type>,
// We don't include the FFIFunc in the hash calculation, because:
// - it is entirely determined by the other fields,
// so excluding it is safe.
// - its `name` property includes a checksum derived from the very
// hash value we're trying to calculate here, so excluding it
// avoids a weird circular dependency in the calculation.
#[checksum_ignore]
pub(super) ffi_func: FfiFunction,
pub(super) attributes: MethodAttributes,
}
impl Method {
pub fn name(&self) -> &str {
&self.name
}
pub fn arguments(&self) -> Vec<&Argument> {
self.arguments.iter().collect()
}
// Methods have a special implicit first argument for the object instance,
// hence `arguments` and `full_arguments` are different.
pub fn full_arguments(&self) -> Vec<Argument> {
vec![Argument {
name: "ptr".to_string(),
// TODO: ideally we'd get this via `ci.resolve_type_expression` so that it
// is contained in the proper `TypeUniverse`, but this works for now.
type_: Type::Object(self.object_name.clone()),
by_ref: !self.attributes.get_self_by_arc(),
optional: false,
default: None,
}]
.into_iter()
.chain(self.arguments.iter().cloned())
.collect()
}
pub fn return_type(&self) -> Option<&Type> {
self.return_type.as_ref()
}
pub fn ffi_func(&self) -> &FfiFunction {
&self.ffi_func
}
pub fn throws(&self) -> bool {
self.attributes.get_throws_err().is_some()
}
pub fn throws_name(&self) -> Option<&str> {
self.attributes.get_throws_err()
}
pub fn throws_type(&self) -> Option<Type> {
self.attributes
.get_throws_err()
.map(|name| Type::Error(name.to_owned()))
}
pub fn takes_self_by_arc(&self) -> bool {
self.attributes.get_self_by_arc()
}
pub fn derive_ffi_func(&mut self, ci_prefix: &str, obj_prefix: &str) -> Result<()> {
// The name is already set if the function is defined through a proc-macro invocation
// rather than in UDL. Don't overwrite it in that case.
if self.ffi_func.name.is_empty() {
self.ffi_func.name = format!("{ci_prefix}_{obj_prefix}_{}", self.name);
}
self.ffi_func.arguments = self.full_arguments().iter().map(Into::into).collect();
self.ffi_func.return_type = self.return_type.as_ref().map(Into::into);
Ok(())
}
pub fn iter_types(&self) -> TypeIterator<'_> {
Box::new(
self.arguments
.iter()
.flat_map(Argument::iter_types)
.chain(self.return_type.iter().flat_map(Type::iter_types)),
)
}
}
impl From<uniffi_meta::MethodMetadata> for Method {
fn from(meta: uniffi_meta::MethodMetadata) -> Self {
let ffi_name = meta.ffi_symbol_name();
let return_type = meta.return_type.map(|out| convert_type(&out));
let arguments = meta.inputs.into_iter().map(Into::into).collect();
let ffi_func = FfiFunction {
name: ffi_name,
..FfiFunction::default()
};
Self {
name: meta.name,
object_name: meta.self_name,
arguments,
return_type,
ffi_func,
attributes: meta.throws.map(Attribute::Throws).into_iter().collect(),
}
}
}
impl APIConverter<Method> for weedle::interface::OperationInterfaceMember<'_> {
fn convert(&self, ci: &mut ComponentInterface) -> Result<Method> {
if self.special.is_some() {
bail!("special operations not supported");
}
if self.modifier.is_some() {
bail!("method modifiers are not supported")
}
let return_type = ci.resolve_return_type_expression(&self.return_type)?;
Ok(Method {
name: match self.identifier {
None => bail!("anonymous methods are not supported {:?}", self),
Some(id) => {
let name = id.0.to_string();
if name == "new" {
bail!("the method name \"new\" is reserved for the default constructor");
}
name
}
},
// We don't know the name of the containing `Object` at this point, fill it in later.
object_name: Default::default(),
arguments: self.args.body.list.convert(ci)?,
return_type,
ffi_func: Default::default(),
attributes: MethodAttributes::try_from(self.attributes.as_ref())?,
})
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_that_all_argument_and_return_types_become_known() {
const UDL: &str = r#"
namespace test{};
interface Testing {
constructor(string? name, u16 age);
sequence<u32> code_points_of_name();
};
"#;
let ci = ComponentInterface::from_webidl(UDL).unwrap();
assert_eq!(ci.object_definitions().len(), 1);
ci.get_object_definition("Testing").unwrap();
assert_eq!(ci.iter_types().count(), 6);
assert!(ci.iter_types().any(|t| t.canonical_name() == "u16"));
assert!(ci.iter_types().any(|t| t.canonical_name() == "u32"));
assert!(ci.iter_types().any(|t| t.canonical_name() == "Sequenceu32"));
assert!(ci.iter_types().any(|t| t.canonical_name() == "string"));
assert!(ci
.iter_types()
.any(|t| t.canonical_name() == "Optionalstring"));
assert!(ci.iter_types().any(|t| t.canonical_name() == "TypeTesting"));
}
#[test]
fn test_alternate_constructors() {
const UDL: &str = r#"
namespace test{};
interface Testing {
constructor();
[Name=new_with_u32]
constructor(u32 v);
};
"#;
let ci = ComponentInterface::from_webidl(UDL).unwrap();
assert_eq!(ci.object_definitions().len(), 1);
let obj = ci.get_object_definition("Testing").unwrap();
assert!(obj.primary_constructor().is_some());
assert_eq!(obj.alternate_constructors().len(), 1);
assert_eq!(obj.methods().len(), 0);
let cons = obj.primary_constructor().unwrap();
assert_eq!(cons.name(), "new");
assert_eq!(cons.arguments.len(), 0);
assert_eq!(cons.ffi_func.arguments.len(), 0);
let cons = obj.alternate_constructors()[0];
assert_eq!(cons.name(), "new_with_u32");
assert_eq!(cons.arguments.len(), 1);
assert_eq!(cons.ffi_func.arguments.len(), 1);
}
#[test]
fn test_the_name_new_identifies_the_primary_constructor() {
const UDL: &str = r#"
namespace test{};
interface Testing {
[Name=newish]
constructor();
[Name=new]
constructor(u32 v);
};
"#;
let ci = ComponentInterface::from_webidl(UDL).unwrap();
assert_eq!(ci.object_definitions().len(), 1);
let obj = ci.get_object_definition("Testing").unwrap();
assert!(obj.primary_constructor().is_some());
assert_eq!(obj.alternate_constructors().len(), 1);
assert_eq!(obj.methods().len(), 0);
let cons = obj.primary_constructor().unwrap();
assert_eq!(cons.name(), "new");
assert_eq!(cons.arguments.len(), 1);
let cons = obj.alternate_constructors()[0];
assert_eq!(cons.name(), "newish");
assert_eq!(cons.arguments.len(), 0);
assert_eq!(cons.ffi_func.arguments.len(), 0);
}
#[test]
fn test_the_name_new_is_reserved_for_constructors() {
const UDL: &str = r#"
namespace test{};
interface Testing {
constructor();
void new(u32 v);
};
"#;
let err = ComponentInterface::from_webidl(UDL).unwrap_err();
assert_eq!(
err.to_string(),
"the method name \"new\" is reserved for the default constructor"
);
}
#[test]
fn test_duplicate_primary_constructors_not_allowed() {
const UDL: &str = r#"
namespace test{};
interface Testing {
constructor();
constructor(u32 v);
};
"#;
let err = ComponentInterface::from_webidl(UDL).unwrap_err();
assert_eq!(err.to_string(), "Duplicate interface member name: \"new\"");
const UDL2: &str = r#"
namespace test{};
interface Testing {
constructor();
[Name=new]
constructor(u32 v);
};
"#;
let err = ComponentInterface::from_webidl(UDL2).unwrap_err();
assert_eq!(err.to_string(), "Duplicate interface member name: \"new\"");
}
}
|