Bug 214160

Summary: [Bindings] Add default value support for union types in dictionary members
Product: WebKit Reporter: Chris Dumez <cdumez>
Component: BindingsAssignee: Chris Dumez <cdumez>
Status: RESOLVED FIXED    
Severity: Normal CC: cdumez, clark_wang, darin, esprehn+autocc, ews-watchlist, ggaren, kondapallykalyan, sam, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch
none
Patch
none
Patch none

Description Chris Dumez 2020-07-09 13:55:11 PDT
Add default value support for union types in dictionary members. This is needed for the WebAudio Specification:
- https://www.w3.org/TR/webaudio/#AudioContextOptions (AudioContextOptions.latencyHint)
Comment 1 Chris Dumez 2020-07-09 14:05:32 PDT
Created attachment 403916 [details]
Patch
Comment 2 Sam Weinig 2020-07-09 14:13:07 PDT
Comment on attachment 403916 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=403916&action=review

> Source/WebCore/bindings/scripts/CodeGeneratorJS.pm:2214
> +    # In case of union types, assume the type of the default value has same type as first type in the union.
> +    $type = (GetFlattenedMemberTypes($type))[0] if $type->isUnion and !($defaultValue eq "null" && $type->isNullable);

Is this a WebIDL requirement?
Comment 3 Chris Dumez 2020-07-09 14:14:51 PDT
(In reply to Sam Weinig from comment #2)
> Comment on attachment 403916 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=403916&action=review
> 
> > Source/WebCore/bindings/scripts/CodeGeneratorJS.pm:2214
> > +    # In case of union types, assume the type of the default value has same type as first type in the union.
> > +    $type = (GetFlattenedMemberTypes($type))[0] if $type->isUnion and !($defaultValue eq "null" && $type->isNullable);
> 
> Is this a WebIDL requirement?

Good question. I don't think so but I'll check the spec right now. I did this because trying to do better would likely be a decent amount more code and this unblocks our intern working on Web Audio.
Comment 4 Chris Dumez 2020-07-09 14:21:07 PDT
(In reply to Chris Dumez from comment #3)
> (In reply to Sam Weinig from comment #2)
> > Comment on attachment 403916 [details]
> > Patch
> > 
> > View in context:
> > https://bugs.webkit.org/attachment.cgi?id=403916&action=review
> > 
> > > Source/WebCore/bindings/scripts/CodeGeneratorJS.pm:2214
> > > +    # In case of union types, assume the type of the default value has same type as first type in the union.
> > > +    $type = (GetFlattenedMemberTypes($type))[0] if $type->isUnion and !($defaultValue eq "null" && $type->isNullable);
> > 
> > Is this a WebIDL requirement?
> 
> Good question. I don't think so but I'll check the spec right now. I did
> this because trying to do better would likely be a decent amount more code
> and this unblocks our intern working on Web Audio.

I don't see any such restriction in the Web IDL specification. This is a simplification on my part so that we can generate the correct code with minor or no IDL changes.
Comment 5 Sam Weinig 2020-07-09 14:32:28 PDT
(In reply to Chris Dumez from comment #4)
> (In reply to Chris Dumez from comment #3)
> > (In reply to Sam Weinig from comment #2)
> > > Comment on attachment 403916 [details]
> > > Patch
> > > 
> > > View in context:
> > > https://bugs.webkit.org/attachment.cgi?id=403916&action=review
> > > 
> > > > Source/WebCore/bindings/scripts/CodeGeneratorJS.pm:2214
> > > > +    # In case of union types, assume the type of the default value has same type as first type in the union.
> > > > +    $type = (GetFlattenedMemberTypes($type))[0] if $type->isUnion and !($defaultValue eq "null" && $type->isNullable);
> > > 
> > > Is this a WebIDL requirement?
> > 
> > Good question. I don't think so but I'll check the spec right now. I did
> > this because trying to do better would likely be a decent amount more code
> > and this unblocks our intern working on Web Audio.
> 
> I don't see any such restriction in the Web IDL specification. This is a
> simplification on my part so that we can generate the correct code with
> minor or no IDL changes.

Seems like this will lead to confusing error cases if someone doesn't know about this restriction. What do you see as the complexity of getting this fully supported?
Comment 6 Chris Dumez 2020-07-09 14:38:50 PDT
(In reply to Sam Weinig from comment #5)
> (In reply to Chris Dumez from comment #4)
> > (In reply to Chris Dumez from comment #3)
> > > (In reply to Sam Weinig from comment #2)
> > > > Comment on attachment 403916 [details]
> > > > Patch
> > > > 
> > > > View in context:
> > > > https://bugs.webkit.org/attachment.cgi?id=403916&action=review
> > > > 
> > > > > Source/WebCore/bindings/scripts/CodeGeneratorJS.pm:2214
> > > > > +    # In case of union types, assume the type of the default value has same type as first type in the union.
> > > > > +    $type = (GetFlattenedMemberTypes($type))[0] if $type->isUnion and !($defaultValue eq "null" && $type->isNullable);
> > > > 
> > > > Is this a WebIDL requirement?
> > > 
> > > Good question. I don't think so but I'll check the spec right now. I did
> > > this because trying to do better would likely be a decent amount more code
> > > and this unblocks our intern working on Web Audio.
> > 
> > I don't see any such restriction in the Web IDL specification. This is a
> > simplification on my part so that we can generate the correct code with
> > minor or no IDL changes.
> 
> Seems like this will lead to confusing error cases if someone doesn't know
> about this restriction.

But currently it does not work already. So my opinion is that this was a step in the right direction, even though definitely not perfect.

> What do you see as the complexity of getting this
> fully supported?

I honestly have no idea how do map a particular default value to a given type in the union. I guess we could implementation some kind of heuristic like:
- If it is quoted, then it is either a DOMString or enum
- If it is false/true, then it is a boolean
- If it is a number, then it is one of the IDL number types
- ...

Seems complex for such an edge case and would likely still not be perfect.
Comment 7 Chris Dumez 2020-07-09 14:44:08 PDT
Comment on attachment 403916 [details]
Patch

Let me try and come up with something slightly better.
Comment 8 Sam Weinig 2020-07-09 14:55:56 PDT
(In reply to Chris Dumez from comment #7)
> Comment on attachment 403916 [details]
> Patch
> 
> Let me try and come up with something slightly better.

These seem like the relevant sections:

https://heycam.github.io/webidl/#dfn-optional-argument-default-value
https://heycam.github.io/webidl/#dfn-constant

I think it should be a pretty small mapping as the grammar only allows the following for default values:


ConstValue ::
    BooleanLiteral
    FloatLiteral
    integer

DefaultValue ::
    ConstValue
    string
    [ ]
    { }
    null


string -> DOMString, ByteString, USVString, enum 
BooleanLiteral -> boolean
integer -> any of the ints
FloatLiteral -> any of the floats
[] -> " sequence type, a nullable type whose inner type is a sequence type or a union type or nullable union type that has a sequence type in its flattened member types"
{} -> " dictionary type, or a union type that has a dictionary type in its flattened member types."

And bail if it's ambiguous.
Comment 9 Chris Dumez 2020-07-09 15:04:39 PDT
Created attachment 403921 [details]
Patch
Comment 10 Chris Dumez 2020-07-09 15:06:45 PDT
Created attachment 403922 [details]
Patch
Comment 11 Chris Dumez 2020-07-09 15:16:43 PDT
Comment on attachment 403922 [details]
Patch

Ok. Hopefully less controversial now.
Comment 12 Sam Weinig 2020-07-09 15:25:04 PDT
Comment on attachment 403922 [details]
Patch

Thank you :)
Comment 13 EWS 2020-07-09 15:41:26 PDT
Committed r264201: <https://trac.webkit.org/changeset/264201>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 403922 [details].
Comment 14 Radar WebKit Bug Importer 2020-07-09 15:42:14 PDT
<rdar://problem/65295454>
Comment 15 Darin Adler 2020-07-10 12:23:09 PDT
Comment on attachment 403922 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=403922&action=review

> Source/WebCore/bindings/scripts/CodeGeneratorJS.pm:2227
>          return "nullptr" if $codeGenerator->IsWrapperType($type) || $codeGenerator->IsBufferSourceType($type);
> -        return "String()" if $codeGenerator->IsStringType($type);
> +        if ($codeGenerator->IsStringType($type)) {
> +            my $useAtomString = $type->extendedAttributes->{AtomString};
> +            return $useAtomString ? "nullAtom()" : "String()";
> +        }
>          return "WTF::nullopt";

Can we just use "{ }" for all of these?

> Source/WebCore/bindings/scripts/CodeGeneratorJS.pm:2251
> +                return $useAtomString ? "emptyAtom()" : "emptyString()";

Why not always use emptyAtom()?
Comment 16 Chris Dumez 2020-07-10 12:39:25 PDT
Comment on attachment 403922 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=403922&action=review

>> Source/WebCore/bindings/scripts/CodeGeneratorJS.pm:2251
>> +                return $useAtomString ? "emptyAtom()" : "emptyString()";
> 
> Why not always use emptyAtom()?

It is used in ternaries:
/Volumes/Work/WebKit/OpenSource/WebKitBuild/Release/DerivedSources/WebCore/JSHTMLMediaElement.cpp:1764:50: error: conditional expression is ambiguous; 'const WTF::AtomString' can be converted to 'typename Converter<IDLDOMString>::ReturnType' (aka 'WTF::String') and vice versa
    auto label = argument1.value().isUndefined() ? emptyAtom() : convert<IDLDOMString>(*lexicalGlobalObject, argument1.value());
Comment 17 Chris Dumez 2020-07-10 12:40:15 PDT
Comment on attachment 403922 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=403922&action=review

>> Source/WebCore/bindings/scripts/CodeGeneratorJS.pm:2227
>>          return "WTF::nullopt";
> 
> Can we just use "{ }" for all of these?

It is used in ternaries:
In file included from /Volumes/Work/WebKit/OpenSource/WebKitBuild/Release/DerivedSources/WebCore/unified-sources/UnifiedSource5.cpp:13:
/Volumes/Work/WebKit/OpenSource/WebKitBuild/Release/DerivedSources/WebCore/JSDOMImplementation.cpp:259:52: error: initializer list cannot be used on the right hand side of operator '?'
    auto title = argument0.value().isUndefined() ? { } : convert<IDLDOMString>(*lexicalGlobalObject, argument0.value());