-
Notifications
You must be signed in to change notification settings - Fork 163
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 pattern for feature detecting dictionary members #107
Comments
I'm a little scared of this idea since dictionaries right now are not "reified" in any way. Their names are for spec purposes only, and can be changed at will. They just represent normal JavaScript objects that authors pass in. Having there be a property on the global for each dictionary, which is going to be some type of... object? array? of supported property keys (if object, what is their value?) is pretty weird. I don't really have any other great solution though. Something like |
(None of this would be necessary if JS hadn't punted on the named-arguments thing. If we had named arguments like Python, this would all be way easier - methods would just throw if you passed a new argument in an old browser, like they do for new positional arguments today. Ugh.) To be specific, if you define a dictionary like:
This would define an
By making this happen automatically in IDL, we get reasonably dependable support, without needing to rely on impls (a) remembering to update their "is this supported?" code, and (b) not lying. This is similar to how |
Can you say why that's better than one of window.EventListenerOptions = new Set("foo", "bar");
window.EventListenerOptions = ["foo", "bar"]; ? |
We would definitely need to audit dictionary names in the platform to make sure that none of them have names that are likely to collide with real-world stuff.... Past that this is probably OK, but I agree it should not be raw object. And the reason it shouldn't be is that we don't want to things to go sideways if a dictionary has a "toString" member or whatnot. So a |
Sure, I don't have strong opinions on the exact shape. A Set works for me. |
FWIW we had this exact problem with Still, that seems like a lot of bloat on the window. |
I suppose we would only need this for dictionaries that are taken as inputs? Some specs have lots of dictionaries that are only ever returned to content. |
I kind of prefer the |
Just so long as it's something that can reasonably be done automagically by our IDL handlers. |
Yeah, it would totally be IDL-driven. We will probably need to start annotating dictionaries with something akin to Exposed. Otherwise IDL code will need to find where the dictionary is used in order to know what global it's supported on (maybe that's okay?). Another thing is that we should indeed probably not do this for dictionaries that are solely returned. It only makes sense for those accepted as an argument somewhere. That probably requires an annotation or again some finding "magic". |
Why don't we start by adding an extended-attribute that opts dictionaries into this behavior? We can try that out in a few cases, then revisit if/how to change the default. |
You can't do this without annotations in general: there are some APIs that take |
At the risk of sounding spoiled, I think I would expect the webidl compiler to do this automatically whenever it can (which should be most of the time?), and instead require annotation when using dictionaries in unobvious ways. I worry most spec writers would forget otherwise. |
Yeah, I don't see why return-only dictionaries are a problem here. It's not useful to feature-detect them (probably, tho I could imagine some cases), but if leaving them out means we have to affirmatively annotate dictionaries we want in, it's not worth the trouble - we should just put them all in. I'm fine with the dictionaries using the same |
FWIW, this problem applies to enum values in addition to dictionary members. |
Good point. Do enums live in the same or different namespace from dictionaries? |
Same namespace, because when you use it as an arg type, you just use the name. |
Ah, right. I... should probably address that in Bikeshed. (It treats all the name-definers as separate namespaces right now and won't warn you if names collide.) |
Isn't enum arg detection straightforward? |
Not if the only use of the enum is in a method argument (same as the dictionary issues we're discussing). |
Do we want to allow enumeration of supported dictionary/enumeration members? I can see arguments either way, without a compelling use case I'd probably prefer we keep this scoped to feature detection. |
The other thing I was wondering about is if we are going to expose dictionaries, should we attempt at normalizing their names somehow? I think we should do enums separately by the way. They are either ignored (setters) or the method throws, which makes them reasonably detectable. Apart from that, they would require a different API. |
And yes, agreed that we should keep it simple initially. |
@RByers unknown enum args in methods throw. |
I'm confused - enumerating the supported dictionary members is literally the request here. (Or at least, being able to ask if a given name is a supported dictionary member.) I'm 100% against anything attempting to be smarter such that it can no longer be trivially automated with no human intervention required.
Why would they require a different API? Afaict they'd have the identical "set of supported names for this type" API. |
@tabatkins well, e.g., do enums and dictionary share a namespace? It's also not clear to me why they would be the same API, since you can do much more with dictionaries than simple member checking going forward as I hinted earlier (e.g., seeing whether a member accepts a particular value). |
Right. There have been two main classes of APIs discussed:
My question was just whether we considered supporting enumeration in addition to feature detection a good or bad thing. I can certainly imagine cases where allowing enumeration causes more problems than benefits. If we don't have any good reason to want to support it, then we should probably prefer the 1) style over the 2) style as a result. |
Yes, this was asked by me and answered by bz immediately prior to your comment: #107 (comment) Everything that can be used as an argument type shares a namespace: interfaces, dictionaries, enums, and typedefs. I opened a Bikeshed bug to enforce that more thoroughly. |
Does this issue address or cover the same use cases as https://www.w3.org/Bugs/Public/show_bug.cgi?id=19936? If so, should we close it with a reference to this issue? |
The reason I hesitate, is I don't consider a dictionary to be a first class API. Someone once told me, "you don't have to use dictionaries. Use |
That decision isn't fixed in stone yet. It's part of this proposal: Maybe, but we also want to be able to share a file along with the other metadata at the same time (e.g., share a URL along with a screenshot). What I'm asking here, at a high level, is: should there be a standardized pattern for how to do this kind of sub-feature detection, or should each API build its own? The suggestion to use a separate |
Not sure if this was proposed yet, but how about stuffing the supported options onto the functions/methods that take them? (For dictionaries taken by multiple functions, or methods that exist on multiple objects, such as |
I'm working with a customer who wants this kind of detection (with an origin trial, FYI). |
Has there been any progress on this? We recently discussed w3c/performance-timeline#176 in a call and someone pointed out this issue, which would fix the problem for us. |
In an unrelated thread, @mkruisselbrink points out that it's possible to feature detect dictionary members by providing the method an object with a getter and seeing if it was read. |
Yep, that's the pattern I used when opening this issue. We continue to hear developers complain that they really don't consider that acceptable (complexity, obscurity, desire to reliably avoid exceptions, etc). |
Apologies for not actually reading the OP... |
I had an "exotic idea" come into my mind about this issue, I hope that after 5 years it's ok to propose such ideas, at least to make things move a bit. Anyway, the idea would be to expose a new global method (name can totally change, I'm really bad at naming) interface mixin WindowOrWorkerGlobalScope { // or any global context really
+ boolean methodSupports(Function method, any... arguments);
} Where web authors would pass directly the ECMAScript function object they have access to as the first argument and the arguments they'd like to test as the following arguments. For instance to test for Worker's globalThis.methodSupports(Worker, "" /* the URL param */, { type: "module" } /* the option param */) To avoid creating void objects only to pass the required arguments, maybe globalThis.methodSupports(EventTarget.prototype.addEventListener, null, null, { signal } ); would ignore both the event type and callback params but check if If the arguments length is bigger than the one expected, or if a dictionary member is not recognized, or if the value of an enum is not valid, the method should return I believe such a model would solve the issues outlined with the two proposed solutions so far:
However it also comes with its own questions (and many others I probably didn't see myself)
|
Random idea on my bed: constructors for dictionaries dictionary Foo {
(DOMString or boolean) bar;
} ;
// implicitly creates an interface-like constructor "bar" in Foo.prototype // typical existence check
new Foo({ bar: "bar" }).bar === "bar" // type support check; string in this case Problem: massive namespace pollution 🤔 |
If I were to randomly propose some function on a dictionary's prototype,
I'd propose that typeof(Foo.prototype.bar) returns "string" if it's a
string and 'undefined' if it's not a valid member variable.
…On Sat, Sep 11, 2021 at 5:00 AM Kagami Sascha Rosylight < ***@***.***> wrote:
Random idea on my bed: constructors for dictionaries
dictionary Foo {
(DOMString or boolean) bar;
} ;
// implicitly creates an interface-like constructor
"bar" in Foo.prototype // typical existence check
new Foo({ bar: "bar" }).bar === "bar" // type support check; string in this case
Problem: massive namespace pollution 🤔
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#107 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AADVM7O35ME5A5VD554HODLUBLA6LANCNFSM4CANVH2Q>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
This idea of a The most convincing argument against this model for me is that specs authors should be able to change these dictionaries in ways that would break this model. |
@Kaiido I quite like that idea, but it does seem like a lot of work for something that can be solved with throwing getters. And as you note it doesn't work for all the things you might want to find out support for. Based on that I'd still lean toward throwing getters and purpose-built |
@annevk But as has been said previously, "throwing getters" actually do not work or at least they only work in environments that do support an option that is alphabetically after the one you want to test. If you want to test them all, or the last one alphabetically, it will execute the method in environments that don't support this option. As to ...(💡) Unless we plan to add it on the methods directly? But this still sounds very exotic and I fear we'll still face the "why isn't there an |
As linked above, foolip/mdn-bcd-collector#1485 is about adding detection for parameters to mdn-bcd-collector, as I'm aware at least some data in BCD that is outdated because it's non-trivial to do this in a general way. Incorrect data in BCD does, inevitably, further hurt web developers (above and beyond their specific problems with feature detection). Clearly as a general purpose solution we can't rely on the alphabetically last dictionary member being supported (nor does that allow the last one to be tested). |
#107 (comment) makes sense to me, and still I'd prefer a general solution for automation purpose e.g. BCD mentioned above. My second random thought, which also covers signature and argument support detection: // Given this IDL:
interface Bar {
foo(Bar bar);
foo((boolean or DOMString) union);
foo(float f, unsigned short s);
foo(Baz baz);
};
dictionary Bar {
DOMString bar;
};
enum Baz { "baz", "" }; Foo.prototype.foo[Symbol.inspect];
inspectSignature(Foo.prototype.foo);
// returns:
// [
// { bar: String },
// [[Boolean, String]],
// [Number, Number]
// [["baz", ""]]
// ] (Not sure how to differentiate optional arguments though.) |
Many new APIs (and some new arguments to existing APIs) are relying on dictionaries. But doing feature detection of such members requires ugly and complex code like:
This increases the concern that new APIs will lead to sites being broken on older browsers because developers didn't understand or couldn't be bothered with the difficult feature detection.
In WICG/EventListenerOptions#31 @tabatkins proposed a mechanism whereby all dictionary types would automatically get a JS-exposed object with a property-per-member to enable consistent and easy feature detection.
Thoughts?
The text was updated successfully, but these errors were encountered: