From e3c634fdd05b5eefc41d7952adffa0bdb41099fa Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Fri, 16 Apr 2021 23:54:41 +0300 Subject: LibJS: Implement initializing a TypedArray from an iterable object Based on these specifications (which required IterableToList as well): https://tc39.es/ecma262/#sec-typedarray https://tc39.es/ecma262/#sec-initializetypedarrayfromlist --- .../Libraries/LibJS/Runtime/IteratorOperations.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp') diff --git a/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp b/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp index 6dd9259203..0b80ab0625 100644 --- a/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/IteratorOperations.cpp @@ -92,6 +92,21 @@ void iterator_close([[maybe_unused]] Object& iterator) TODO(); } +MarkedValueList iterable_to_list(GlobalObject& global_object, Value iterable, Value method) +{ + auto& vm = global_object.vm(); + MarkedValueList values(vm.heap()); + get_iterator_values( + global_object, iterable, [&](auto value) { + if (vm.exception()) + return IterationDecision::Break; + values.append(value); + return IterationDecision::Continue; + }, + method); + return values; +} + Value create_iterator_result_object(GlobalObject& global_object, Value value, bool done) { auto& vm = global_object.vm(); @@ -101,11 +116,11 @@ Value create_iterator_result_object(GlobalObject& global_object, Value value, bo return object; } -void get_iterator_values(GlobalObject& global_object, Value value, AK::Function callback) +void get_iterator_values(GlobalObject& global_object, Value value, AK::Function callback, Value method) { auto& vm = global_object.vm(); - auto iterator = get_iterator(global_object, value); + auto iterator = get_iterator(global_object, value, "sync", method); if (!iterator) return; -- cgit v1.2.3