script: Properly implement supportedAlgorithms of WebCrypto (#41563)

WebCrypto API has an internal object `supportedAlgorithms`
<https://w3c.github.io/webcrypto/#dfn-supportedAlgorithms> that maps the
all supported algorithms and operations to their desired IDL dictionary
types. It is mainly used by the "normalize an algorithm" algorithm
<https://w3c.github.io/webcrypto/#algorithm-normalization-normalize-an-algorithm>.
We currently implement it as a large `match` block in the
`normalize_algorithm` function.

This patch properly implements the internal object `supportedAlgorithms`
as a new enum type `SupportedAlgorithm`.

By doing so, we can reduce a lot of string comparison to enum matching,
which can be done faster. This patch also separates the dictionary
conversion away from the `match` block to make our code cleaner.
Furthermore, the `exportKey()` method can now utilize the new
`SupportedAlgorithm` to properly check against the algorithm
registration, instead of hard-coding an list of unsupported algorithms
by itself.

Testing: Refactoring. Existing tests suffice.

---------

Signed-off-by: Kingsley Yung <kingsley@kkoyung.dev>
This commit is contained in:
Kingsley Yung
2025-12-29 14:22:24 +08:00
committed by GitHub
parent 964ba44161
commit 7342ee0627
4 changed files with 514 additions and 689 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -405,7 +405,7 @@ pub(crate) fn import_key(
// set to hash and op set to digest.
let normalized_hash = normalize_algorithm(
cx,
&Operation::Digest,
Operation::Digest,
&AlgorithmIdentifier::String(DOMString::from(hash)),
can_gc,
)?;

View File

@@ -421,7 +421,7 @@ pub(crate) fn import_key(
// set to hash and op set to digest.
let normalized_hash = normalize_algorithm(
cx,
&Operation::Digest,
Operation::Digest,
&AlgorithmIdentifier::String(DOMString::from(hash)),
can_gc,
)?;

View File

@@ -385,7 +385,7 @@ pub(crate) fn import_key(
// set to hash and op set to digest.
let normalized_hash = normalize_algorithm(
cx,
&Operation::Digest,
Operation::Digest,
&AlgorithmIdentifier::String(DOMString::from(hash)),
can_gc,
)?;