Bug 189136
| Summary: | Omitting an optional argument when calling a WebIDL operation results in a "required member" TypeError if the argument type is a dictionary with required members | ||
|---|---|---|---|
| Product: | WebKit | Reporter: | Andy Estes <aestes> |
| Component: | Bindings | Assignee: | Nobody <webkit-unassigned> |
| Status: | NEW | ||
| Severity: | Normal | CC: | cdumez, darin, rniwa, sam, youennf |
| Priority: | P2 | ||
| Version: | WebKit Nightly Build | ||
| Hardware: | Unspecified | ||
| OS: | Unspecified | ||
| See Also: | https://bugs.webkit.org/show_bug.cgi?id=189100 | ||
Andy Estes
Payment Request defines a dictionary that has a required member:
dictionary PaymentMethodChangeEventInit : PaymentRequestUpdateEventInit {
required DOMString methodName;
object? methodDetails;
};
It also defines an interface (PaymentMethodChangeEvent) with a constructor that takes an optional PaymentMethodChangeEventInit:
Constructor(DOMString type, optional PaymentMethodChangeEventInit eventInitDict)
If you call the constructor, omitting the second argument, you get a TypeError about a missing required field (methodName).
Despite eventInitDict being optional, WebKit still generates code that tries to convert undefined to a PaymentMethodChangeEventInit struct:
auto eventInitDict = convert<IDLDictionary<PaymentMethodChangeEventInit>>(*state, state->argument(1));
And that code throws when converting undefined with a required member:
JSValue methodNameValue;
if (isNullOrUndefined)
methodNameValue = jsUndefined();
else {
methodNameValue = object->get(&state, Identifier::fromString(&state, "methodName"));
RETURN_IF_EXCEPTION(throwScope, { });
}
if (!methodNameValue.isUndefined()) {
result.methodName = convert<IDLDOMString>(state, methodNameValue);
RETURN_IF_EXCEPTION(throwScope, { });
} else {
throwRequiredMemberTypeError(state, throwScope, "methodName", "PaymentMethodChangeEventInit", "DOMString");
return { };
}
I don't know if it's legal per WebIDL to have optional arguments to structs with required members, but if it is then this seems like a bug in our bindings generation.
| Attachments | ||
|---|---|---|
| Add attachment proposed patch, testcase, etc. |
Andy Estes
CodeGeneratorJS.pm thinks that an optional dictionary without a default value has an implicit default value of []:
if ($argument->isOptional && !defined($argument->default)) {
# As per Web IDL, optional dictionary arguments are always considered to have a default value of an empty dictionary, unless otherwise specified.
$argument->default("[]") if $codeGenerator->IsDictionaryType($type);
But [] is an invalid value for a dictionary with required members.
I can't find where in the WebIDL spec it says this should be the default, but I'm not super familiar with it.
Darin Adler
The rule in question in the latest WebIDL draft SAYS this:
"If the type of an argument is a dictionary type or a union type that has a dictionary as one of its flattened member types, and that dictionary type and its ancestors have no required members, and the argument is either the final argument or is followed only by optional arguments, then the argument must be specified as optional. Such arguments are always considered to have a default value of an empty dictionary, unless otherwise specified."
Note that this rule does not apply to a dictionary with required members. So I think in C++ we need to represent such an argument using std::optional rather than by relying on a default value.
Chris Dumez
I filed:
https://github.com/w3c/payment-request/issues/770
I do not think we should have optional dictionary parameters with required members.