* Creates a converter for a Web IDL sequence type. * @see https://webidl.spec.whatwg.org/#es-sequence * @param {Converter} converter Element converter. * @returns {Converter}
(converter)
| 771 | * @returns {Converter} |
| 772 | */ |
| 773 | function createSequenceConverter(converter) { |
| 774 | return function(V, options = kEmptyObject) { |
| 775 | // Web IDL sequence conversion step 1: require an ECMA-262 Object. |
| 776 | if (type(V) !== 'Object') { |
| 777 | throw makeException( |
| 778 | 'cannot be converted to sequence.', |
| 779 | options); |
| 780 | } |
| 781 | |
| 782 | // Step 2: GetMethod(V, %Symbol.iterator%). |
| 783 | const method = V[SymbolIterator]; |
| 784 | // Step 3: throw if the iterator method is undefined, null, or not callable. |
| 785 | if (typeof method !== 'function') { |
| 786 | throw makeException( |
| 787 | 'cannot be converted to sequence.', |
| 788 | options); |
| 789 | } |
| 790 | |
| 791 | // Step 4 and create-sequence step 1: get the iterator record. |
| 792 | const iterator = FunctionPrototypeCall(method, V); |
| 793 | const nextMethod = iterator?.next; |
| 794 | if (typeof nextMethod !== 'function') { |
| 795 | throw makeException( |
| 796 | 'cannot be converted to sequence.', |
| 797 | options); |
| 798 | } |
| 799 | |
| 800 | // Create-sequence step 2: initialize i to 0. |
| 801 | const idlSequence = []; |
| 802 | while (true) { |
| 803 | // Step 3.1: IteratorStepValue(iteratorRecord). |
| 804 | const next = FunctionPrototypeCall(nextMethod, iterator); |
| 805 | if (type(next) !== 'Object') { |
| 806 | throw makeException( |
| 807 | 'cannot be converted to sequence.', |
| 808 | options); |
| 809 | } |
| 810 | // Step 3.2: IteratorComplete applies ToBoolean(done). |
| 811 | if (next.done) { |
| 812 | break; |
| 813 | } |
| 814 | // Step 3.3: convert next to an IDL value of type T. |
| 815 | const idlValue = converter( |
| 816 | next.value, |
| 817 | makeOptions(options, sequenceElementContext(idlSequence.length, options)), |
| 818 | ); |
| 819 | // Step 3.4: store the value and advance i. |
| 820 | ArrayPrototypePush(idlSequence, idlValue); |
| 821 | } |
| 822 | return idlSequence; |
| 823 | }; |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * Creates a converter for a Web IDL interface type. |
no test coverage detected
searching dependent graphs…