* Creates a converter for a Web IDL dictionary type. * @see https://webidl.spec.whatwg.org/#js-dictionary * @param {string} dictionaryName Dictionary identifier. * @param {DictionaryMember[]|DictionaryMember[][]} members Dictionary members, * either for a single dictionary or grouped from leas
( dictionaryName, members, )
| 686 | * @returns {Converter} |
| 687 | */ |
| 688 | function createDictionaryConverter( |
| 689 | dictionaryName, |
| 690 | members, |
| 691 | ) { |
| 692 | const compareMembers = (a, b) => { |
| 693 | if (a.key === b.key) { |
| 694 | return 0; |
| 695 | } |
| 696 | return a.key < b.key ? -1 : 1; |
| 697 | }; |
| 698 | |
| 699 | const dictionaries = ArrayIsArray(members[0]) ? members : [members]; |
| 700 | const sortedDictionaries = []; |
| 701 | |
| 702 | // Web IDL dictionary conversion steps 3-4 process inherited dictionaries |
| 703 | // from least-derived to most-derived and sort only within each dictionary. |
| 704 | // Callers with inheritance pass one member array per dictionary level. |
| 705 | for (let i = 0; i < dictionaries.length; i++) { |
| 706 | ArrayPrototypePush( |
| 707 | sortedDictionaries, |
| 708 | ArrayPrototypeToSorted(dictionaries[i], compareMembers), |
| 709 | ); |
| 710 | } |
| 711 | |
| 712 | return function(jsDict, options = kEmptyObject) { |
| 713 | // Step 1: reject non-object, non-null, non-undefined values. |
| 714 | if (jsDict != null && type(jsDict) !== 'Object') { |
| 715 | throw makeException( |
| 716 | 'cannot be converted to a dictionary', |
| 717 | options, |
| 718 | ); |
| 719 | } |
| 720 | |
| 721 | // Step 2: create the IDL dictionary value. |
| 722 | const idlDict = { __proto__: null }; |
| 723 | |
| 724 | // Steps 3-4: iterate each dictionary level, then its sorted members. |
| 725 | for (let i = 0; i < sortedDictionaries.length; i++) { |
| 726 | const sortedMembers = sortedDictionaries[i]; |
| 727 | for (let j = 0; j < sortedMembers.length; j++) { |
| 728 | const member = sortedMembers[j]; |
| 729 | // Step 4.1.1: get the dictionary member identifier. |
| 730 | const key = member.key; |
| 731 | // Steps 4.1.2-4.1.3: read the JavaScript member value. |
| 732 | const jsMemberValue = jsDict == null ? undefined : jsDict[key]; |
| 733 | |
| 734 | // Step 4.1.4: convert and store present member values. |
| 735 | if (jsMemberValue !== undefined) { |
| 736 | const converter = member.converter; |
| 737 | // Step 4.1.4.1: convert the JavaScript value to IDL. |
| 738 | const idlMemberValue = converter( |
| 739 | jsMemberValue, |
| 740 | makeOptions(options, dictionaryMemberContext(key, options)), |
| 741 | ); |
| 742 | // Validators are a Node.js extension after conversion. They let |
| 743 | // consumers reject known unsupported values while dictionary |
| 744 | // conversion still has precise member context. Web Crypto uses this |
| 745 | // so SubtleCrypto.supports() can make accurate decisions from |
no test coverage detected
searching dependent graphs…