@@ -527,13 +527,29 @@ impl<'a> IdlType<'a> {
527
527
IdlType :: Promise ( _idl_type) => js_sys ( "Promise" ) ,
528
528
IdlType :: Record ( _idl_type_from, _idl_type_to) => None ,
529
529
IdlType :: Union ( idl_types) => {
530
- // Handles union types in all places except operation argument types.
531
- // Currently treats them as object type, if possible.
532
- // TODO: add better support for union types here?
533
- // Approaches for it:
534
- // 1. Use strategy of finding the nearest common subclass (finding the best type
535
- // that is suitable for all values of this union)
536
- // 2. Generate enum with payload in Rust for each union type
530
+ // Note that most union types have already been expanded to
531
+ // their components via `flatten`. Unions in a return position
532
+ // or dictionary fields, however, haven't been flattened, which
533
+ // means we may need to conver them to a `syn` type.
534
+ //
535
+ // Currently this does a bit of a "poor man's" tree traversal by
536
+ // saying that if all union members are interfaces we can assume
537
+ // they've all got `Object` as a superclass, so we can take an
538
+ // object here. If any are not an interface though we
539
+ // pessimisitcally translate the union into a `JsValue`,
540
+ // absolutely anything. It's up to the application to figure out
541
+ // what to do with that.
542
+ //
543
+ // TODO: we should probably do a better job here translating
544
+ // unions to a single type. Two possible strategies could be:
545
+ //
546
+ // 1. Use strategy of finding the nearest common subclass
547
+ // (finding the best type that is suitable for all values of
548
+ // this union) instead of always assuming `Object`.
549
+ // 2. Generate enum with payload in Rust for each union type.
550
+ // Such an enum, however, might have a relatively high
551
+ // overhead in creating it from a JS value, but would be
552
+ // cheap to convert from a variant back to a JS value.
537
553
if idl_types
538
554
. iter ( )
539
555
. all ( |idl_type|
@@ -544,7 +560,7 @@ impl<'a> IdlType<'a> {
544
560
) {
545
561
IdlType :: Object . to_syn_type ( pos)
546
562
} else {
547
- None
563
+ IdlType :: Any . to_syn_type ( pos )
548
564
}
549
565
} ,
550
566
0 commit comments