Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need to adjust IDL to make overload definitions distinguishable? #21

Closed
tidoust opened this issue Jan 13, 2022 · 2 comments · Fixed by #22
Closed

Need to adjust IDL to make overload definitions distinguishable? #21

tidoust opened this issue Jan 13, 2022 · 2 comments · Fixed by #22

Comments

@tidoust
Copy link
Member

tidoust commented Jan 13, 2022

#20 takes care of merging overloaded definitions to meet one of the overloading requirements defined in Web IDL. Web IDL also requires that overload definitions be distinguishable.

The Autoplay Policy defines the following IDL:

callback HTMLMediaElementConstructor = HTMLMediaElement ();
callback AudioContextConstructor = AudioContext ();

[Exposed=Window]
partial interface Navigator {
  AutoplayPolicy getAutoplayPolicy(HTMLMediaElementConstructor mediaCtor);
  AutoplayPolicy getAutoplayPolicy(AudioContextConstructor audioCtor);
};

If I'm reading Web IDL correctly, two callback functions are not distinguishable (see the matrix in the Web IDL spec).

This can probaby be fixed with a union type instead:

callback HTMLMediaElementOrAudioContextConstructor = (HTMLMediaElement or AudioContext) ();

[Exposed=Window]
partial interface Navigator {
  AutoplayPolicy getAutoplayPolicy(HTMLMediaElementOrAudioContextConstructor ctor);
};

The Web IDL validator does not check these requirements, so I'm not 100% sure that the above analysis is correct. It may be worth checking with Web IDL experts. Cc @w3c/webidl-design for possible confirmation.

@domenic
Copy link

domenic commented Jan 13, 2022

The deeper issue here is there's no way to distinguish these two types, so such an overload---or union---is meaningless. JavaScript functions passed to your method don't signal their return value in some way spec code can distinguish. You can't know what a JavaScript function will return, until you call it!

Probably instead you want the callback to return object, and then you can do tests in prose accounting for the three cases of interest: the result of calling the callback implements HTMLMediaElement, the result implements AudioContext, or neither of those things are true.

@tidoust
Copy link
Member Author

tidoust commented Jan 17, 2022

Hmm, that means that browsers would need to call the constructor themselves to check the return type. That does not sound like very good design (arbitrary constructors may have side effects).

For context, the current design originates from #15 (comment)

@alastor0325, this may suggest that the spec needs to consider a different approach. Perhaps with an enum? Something like:

enum AutoplayPolicyMediaType { "audio", "video", "media" };

[Exposed=Window]
partial interface Navigator {
  AutoplayPolicy getAutoplayPolicy(AutoplayPolicyMediaType type);
};

alastor0325 added a commit to alastor0325/autoplay that referenced this issue Jan 21, 2022
… result, which replaces methods using constructor as an input.
alastor0325 added a commit to alastor0325/autoplay that referenced this issue Jan 25, 2022
github-actions bot added a commit to alastor0325/autoplay that referenced this issue Jan 25, 2022
…d on review suggestions.

SHA: f82f69c
Reason: push, by @alastor0325

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
alastor0325 added a commit to alastor0325/autoplay that referenced this issue Jan 25, 2022
github-actions bot added a commit to alastor0325/autoplay that referenced this issue Jan 25, 2022
SHA: 5167882
Reason: push, by @alastor0325

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
alastor0325 added a commit to alastor0325/autoplay that referenced this issue Jan 25, 2022
github-actions bot added a commit to alastor0325/autoplay that referenced this issue Jan 25, 2022
SHA: 0eed9a8
Reason: push, by @alastor0325

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
alastor0325 added a commit that referenced this issue Feb 2, 2022
Issue #21 - create a new enum type and use it to query the general result, which replaces methods using constructor as an input.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants