Bug 1895261 - Only generate an equality operator for WebIDL dictionaries when they have a [GenerateEqualityOperator] extended attribute. r=farre

This also extends the check for supported types to the types of inherited
members, and automatically generates an equality operator in the base classes if
needed. It also deletes the equality operator if a dictionary doesn't need
one but its parent does.

Differential Revision: https://phabricator.services.mozilla.com/D210846
This commit is contained in:
Peter Van der Beken
2024-05-21 14:20:35 +00:00
parent 71d60976b9
commit ec2123dc09
4 changed files with 159 additions and 14 deletions

View File

@@ -1903,6 +1903,90 @@ worker involved is a `ChromeWorker` or not. At the moment the only
possible caller types are `System` (representing system-principal
callers) and `NonSystem`.
### `[GenerateInit]`
When set on a dictionary it will add two `Init` methods to the generated C++
class with the following signatures:
``` cpp
bool Init(BindingCallContext& cx, JS::Handle<JS::Value> val, const char* sourceDescription="Value", bool passedToJSImpl=false);
bool Init(JSContext* cx_, JS::Handle<JS::Value> val, const char* sourceDescription="Value", bool passedToJSImpl=false);
```
These methods will initialize the dictionary from `val` by following WebIDL's
[JavaScript type mapping](https://webidl.spec.whatwg.org/#js-dictionary).
### `[GenerateInitFromJSON]`
When set on a dictionary it will add an `Init` method to the generated C++
class with the following signature:
``` cpp
bool Init(const nsAString& aJSON);
```
This extended attribute will only have an effect if all of the types of the
dictionary's members are representable in JSON (they are a string type, a
primitive type that's not an unrestricted float/double, a void type, or a
sequence, union, dictionary or record containing these types).
The method is expected to be called with a JSON string as input. The JSON string
will be parsed into a JavaScript value, and then the dictionary is initialized
with this value by following WebIDL's
[JavaScript type mapping](https://webidl.spec.whatwg.org/#js-dictionary).
Note: As a side-effect of how this is implemented it will also add the two
`Init` methods that would be added by a [`[GenerateInit]`](#generateinit)
extended attribute.
### `[GenerateToJSON]`
When set on a dictionary it will add a `ToJSON` method to the generated C++
class with the following signature:
``` cpp
bool ToJSON(nsAString& aJSON);
```
The method will generate a JSON representation of the dictionary members' values
in `aJSON` by converting the dictionary to a JavaScript object by following
WebIDL's [JavaScript type mapping](https://webidl.spec.whatwg.org/#js-dictionary)
and then converting that object to a JSON string.
The same restrictions on types applies as on
[`[GenerateInitFromJSON]`](#generateinitfromjson).
Note: As a side-effect of how this is implemented it will also add the
`ToObjectInternal` method that would be added by a
[`[GenerateConversionToJS]`](#generateconversiontojs) extended attribute.
### `[GenerateConversionToJS]`
When set on a dictionary it will add a `ToObjectInternal` method to the
generated C++ class with the following signature:
``` cpp
bool ToObjectInternal(JSContext* cx, JS::MutableHandle<JS::Value> rval);
```
The method will create a JavaScript object by following WebIDL's
[JavaScript type mapping](https://webidl.spec.whatwg.org/#js-dictionary).
### `[GenerateEqualityOperator]`
When set on a dictionary it will add an equality operator to the generated C++
class.
This is only allowed on dictionaries who only have members (own or inherited)
with string, primitive or enum types.
### `[Unsorted]`
When set on a dictionary the dictionary's members will not be sorted in
lexicographic order (which is specified by WebIDL).
This should only ever be used on internal APIs that are not exposed to the Web!
## Helper objects
The C++ side of the bindings uses a number of helper objects.