LibJS: Implement Iterator.zip
This is from the Joint Iteration proposal: https://tc39.es/proposal-joint-iteration/
This commit is contained in:
parent
919f44f3a5
commit
aa7eb95f98
6 changed files with 628 additions and 3 deletions
|
|
@ -353,6 +353,7 @@ namespace JS {
|
|||
P(minute) \
|
||||
P(minutes) \
|
||||
P(minutesDisplay) \
|
||||
P(mode) \
|
||||
P(month) \
|
||||
P(monthCode) \
|
||||
P(months) \
|
||||
|
|
@ -385,6 +386,7 @@ namespace JS {
|
|||
P(opener) \
|
||||
P(overflow) \
|
||||
P(ownKeys) \
|
||||
P(padding) \
|
||||
P(padEnd) \
|
||||
P(padStart) \
|
||||
P(parse) \
|
||||
|
|
@ -610,6 +612,7 @@ namespace JS {
|
|||
P(yearOfWeek) \
|
||||
P(years) \
|
||||
P(yearsDisplay) \
|
||||
P(zip) \
|
||||
P(zonedDateTimeISO)
|
||||
|
||||
struct CommonPropertyNames {
|
||||
|
|
|
|||
|
|
@ -305,7 +305,8 @@
|
|||
M(URIMalformed, "URI malformed") /* LibWeb bindings */ \
|
||||
M(WrappedFunctionCallThrowCompletion, "Call of wrapped target function did not complete normally") \
|
||||
M(WrappedFunctionCopyNameAndLengthThrowCompletion, "Trying to copy target name and length did not complete normally") \
|
||||
M(YieldFromIteratorMissingThrowMethod, "yield* protocol violation: iterator must have a throw method")
|
||||
M(YieldFromIteratorMissingThrowMethod, "yield* protocol violation: iterator must have a throw method") \
|
||||
M(ZipIteratorNotEnoughResults, "Not enough iterator results in 'strict' mode")
|
||||
|
||||
namespace JS {
|
||||
|
||||
|
|
|
|||
|
|
@ -99,6 +99,24 @@ using IterationResultOrDone = Variant<IterationResult, IterationDone>;
|
|||
_temporary_try_or_close_result.release_value(); \
|
||||
})
|
||||
|
||||
// 4 IfAbruptCloseIterators ( value, iteratorRecords ), https://tc39.es/proposal-joint-iteration/#sec-ifabruptcloseiterators
|
||||
#define TRY_OR_CLOSE_ITERATORS(vm, iterator_records, expression) \
|
||||
({ \
|
||||
auto&& _temporary_try_or_close_result = (expression); \
|
||||
\
|
||||
/* 1. Assert: value is a Completion Record. */ \
|
||||
/* 2. If value is an abrupt completion, return ? IteratorCloseAll(iteratorRecords, value). */ \
|
||||
if (_temporary_try_or_close_result.is_error()) { \
|
||||
return iterator_close_all(vm, iterator_records, _temporary_try_or_close_result.release_error()); \
|
||||
} \
|
||||
\
|
||||
static_assert(!::AK::Detail::IsLvalueReference<decltype(_temporary_try_or_close_result.release_value())>, \
|
||||
"Do not return a reference from a fallible expression"); \
|
||||
\
|
||||
/* 3. Else, set value to value.[[Value]]. */ \
|
||||
_temporary_try_or_close_result.release_value(); \
|
||||
})
|
||||
|
||||
ThrowCompletionOr<GC::Ref<IteratorRecord>> get_iterator_direct(VM&, Object&);
|
||||
JS_API ThrowCompletionOr<IteratorRecordImpl> get_iterator_from_method_impl(VM&, Value, GC::Ref<FunctionObject>);
|
||||
JS_API ThrowCompletionOr<GC::Ref<IteratorRecord>> get_iterator_from_method(VM&, Value, GC::Ref<FunctionObject>);
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
/*
|
||||
* Copyright (c) 2023-2024, Tim Flynn <trflynn89@ladybird.org>
|
||||
* Copyright (c) 2023-2026, Tim Flynn <trflynn89@ladybird.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Enumerate.h>
|
||||
#include <LibJS/Runtime/AbstractOperations.h>
|
||||
#include <LibJS/Runtime/Array.h>
|
||||
#include <LibJS/Runtime/Intrinsics.h>
|
||||
#include <LibJS/Runtime/Iterator.h>
|
||||
#include <LibJS/Runtime/IteratorConstructor.h>
|
||||
|
|
@ -35,6 +37,7 @@ void IteratorConstructor::initialize(Realm& realm)
|
|||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||
define_native_function(realm, vm.names.concat, concat, 0, attr);
|
||||
define_native_function(realm, vm.names.from, from, 1, attr);
|
||||
define_native_function(realm, vm.names.zip, zip, 1, attr);
|
||||
|
||||
define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
|
||||
}
|
||||
|
|
@ -235,4 +238,404 @@ JS_DEFINE_NATIVE_FUNCTION(IteratorConstructor::from)
|
|||
return wrapper;
|
||||
}
|
||||
|
||||
enum class ZipMode {
|
||||
Shortest,
|
||||
Longest,
|
||||
Strict,
|
||||
};
|
||||
|
||||
class ZipIterator : public Cell {
|
||||
GC_CELL(ZipIterator, Cell);
|
||||
GC_DECLARE_ALLOCATOR(ZipIterator);
|
||||
|
||||
public:
|
||||
using FinishResults = GC::Function<Value(Realm&, ReadonlySpan<Value>)>;
|
||||
|
||||
ThrowCompletionOr<IteratorHelper::IterationResult> next(VM& vm)
|
||||
{
|
||||
// a. If iterCount = 0, return ReturnCompletion(undefined).
|
||||
if (m_iterators.is_empty())
|
||||
return IteratorHelper::IterationResult { js_undefined(), true };
|
||||
|
||||
// b. Repeat,
|
||||
|
||||
// i. Let results be a new empty List.
|
||||
GC::RootVector<Value> results { vm.heap() };
|
||||
|
||||
// ii. Assert: openIters is not empty.
|
||||
VERIFY(!m_open_iterators.is_empty());
|
||||
|
||||
// iii. For each integer i such that 0 ≤ i < iterCount, in ascending order, do
|
||||
for (auto [i, iterator] : enumerate(m_iterators)) {
|
||||
Optional<Value> result;
|
||||
|
||||
// 1. Let iter be iters[i].
|
||||
// 2. If iter is null, then
|
||||
if (!iterator) {
|
||||
// a. Assert: mode is "longest".
|
||||
VERIFY(m_mode == ZipMode::Longest);
|
||||
|
||||
// b. Let result be padding[i].
|
||||
result = m_padding[i];
|
||||
}
|
||||
// 3. Else,
|
||||
else {
|
||||
// a. Let result be Completion(IteratorStepValue(iter)).
|
||||
auto step_value_result = iterator_step_value(vm, *iterator);
|
||||
|
||||
// b. If result is an abrupt completion, then
|
||||
if (step_value_result.is_throw_completion()) {
|
||||
// i. Remove iter from openIters.
|
||||
remove_iterator_from_open_iterators(iterator);
|
||||
|
||||
// ii. Return ? IteratorCloseAll(openIters, result).
|
||||
return TRY(close_all_open_iterators(vm, step_value_result.release_error()));
|
||||
}
|
||||
|
||||
// c. Set result to ! result.
|
||||
result = step_value_result.release_value();
|
||||
|
||||
// d. If result is DONE, then
|
||||
if (!result.has_value()) {
|
||||
// i. Remove iter from openIters.
|
||||
remove_iterator_from_open_iterators(iterator);
|
||||
|
||||
switch (m_mode) {
|
||||
// ii. If mode is "shortest", then
|
||||
case ZipMode::Shortest:
|
||||
// i. Return ? IteratorCloseAll(openIters, ReturnCompletion(undefined)).
|
||||
return TRY(close_all_open_iterators(vm, js_undefined()));
|
||||
|
||||
// iii. Else if mode is "strict", then
|
||||
case ZipMode::Strict:
|
||||
// i. If i ≠ 0, then
|
||||
if (i != 0) {
|
||||
// i. Return ? IteratorCloseAll(openIters, ThrowCompletion(a newly created TypeError object)).
|
||||
return TRY(close_all_open_iterators(vm, vm.throw_completion<TypeError>(ErrorType::ZipIteratorNotEnoughResults)));
|
||||
}
|
||||
|
||||
// ii. For each integer k such that 1 ≤ k < iterCount, in ascending order, do
|
||||
for (auto iterator_k : m_iterators.span().slice(1)) {
|
||||
// i. Assert: iters[k] is not null.
|
||||
VERIFY(iterator_k);
|
||||
|
||||
// ii. Let open be Completion(IteratorStep(iters[k])).
|
||||
auto step_result = iterator_step(vm, *iterator_k);
|
||||
|
||||
// iii. If open is an abrupt completion, then
|
||||
if (step_result.is_throw_completion()) {
|
||||
// i. Remove iters[k] from openIters.
|
||||
remove_iterator_from_open_iterators(iterator_k);
|
||||
|
||||
// ii. Return ? IteratorCloseAll(openIters, open).
|
||||
return TRY(close_all_open_iterators(vm, step_result.release_error()));
|
||||
}
|
||||
|
||||
// iv. Set open to ! open.
|
||||
auto open = step_result.release_value();
|
||||
|
||||
// v. If open is DONE, then
|
||||
if (open.has<IterationDone>()) {
|
||||
// i. Remove iters[k] from openIters.
|
||||
remove_iterator_from_open_iterators(iterator_k);
|
||||
}
|
||||
// vi. Else,
|
||||
else {
|
||||
// i. Return ? IteratorCloseAll(openIters, ThrowCompletion(a newly created TypeError object)).
|
||||
return TRY(close_all_open_iterators(vm, vm.throw_completion<TypeError>(ErrorType::ZipIteratorNotEnoughResults)));
|
||||
}
|
||||
}
|
||||
|
||||
// iii. Return ReturnCompletion(undefined).
|
||||
return IteratorHelper::IterationResult { js_undefined(), true };
|
||||
|
||||
// iv. Else,
|
||||
case ZipMode::Longest:
|
||||
// i. Assert: mode is "longest".
|
||||
// ii. If openIters is empty, return ReturnCompletion(undefined).
|
||||
if (m_open_iterators.is_empty())
|
||||
return IteratorHelper::IterationResult { js_undefined(), true };
|
||||
|
||||
// iii. Set iters[i] to null.
|
||||
m_iterators[i] = nullptr;
|
||||
|
||||
// iv. Set result to padding[i].
|
||||
result = m_padding[i];
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Append result to results.
|
||||
results.append(result.release_value());
|
||||
}
|
||||
|
||||
// iv. Set results to finishResults(results).
|
||||
auto results_array = m_finish_results->function()(m_realm, results);
|
||||
|
||||
// v. Let completion be Completion(Yield(results)).
|
||||
return IteratorHelper::IterationResult { results_array, false };
|
||||
}
|
||||
|
||||
ThrowCompletionOr<Value> on_abrupt_completion(VM& vm, Completion const& completion) const
|
||||
{
|
||||
// vi. If completion is an abrupt completion, then
|
||||
// 1. Return ? IteratorCloseAll(openIters, completion).
|
||||
return TRY(iterator_close_all(vm, m_open_iterators, completion));
|
||||
}
|
||||
|
||||
ReadonlySpan<GC::Ref<IteratorRecord>> open_iterators() const { return m_open_iterators; }
|
||||
void set_finish_results(GC::Ref<FinishResults> finish_results) { m_finish_results = finish_results; }
|
||||
|
||||
void append_iterator(GC::Ref<IteratorRecord> iterator)
|
||||
{
|
||||
m_iterators.append(iterator);
|
||||
m_open_iterators.append(iterator);
|
||||
}
|
||||
|
||||
void append_padding(Value padding)
|
||||
{
|
||||
m_padding.append(padding);
|
||||
}
|
||||
|
||||
private:
|
||||
ZipIterator(Realm& realm, ZipMode mode)
|
||||
: m_realm(realm)
|
||||
, m_mode(mode)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void visit_edges(Visitor& visitor) override
|
||||
{
|
||||
Base::visit_edges(visitor);
|
||||
visitor.visit(m_realm);
|
||||
visitor.visit(m_iterators);
|
||||
visitor.visit(m_open_iterators);
|
||||
visitor.visit(m_padding);
|
||||
visitor.visit(m_finish_results);
|
||||
}
|
||||
|
||||
void remove_iterator_from_open_iterators(GC::Ptr<IteratorRecord> iterarator)
|
||||
{
|
||||
m_open_iterators.remove_first_matching([&](GC::Ref<IteratorRecord> candidate) {
|
||||
return candidate == iterarator;
|
||||
});
|
||||
}
|
||||
|
||||
ThrowCompletionOr<IteratorHelper::IterationResult> close_all_open_iterators(VM& vm, Completion completion) const
|
||||
{
|
||||
auto close_result = TRY(iterator_close_all(vm, m_open_iterators, completion));
|
||||
return IteratorHelper::IterationResult { close_result, true };
|
||||
}
|
||||
|
||||
GC::Ref<Realm> m_realm;
|
||||
|
||||
ZipMode m_mode { ZipMode::Shortest };
|
||||
|
||||
Vector<GC::Ptr<IteratorRecord>> m_iterators;
|
||||
Vector<GC::Ref<IteratorRecord>> m_open_iterators;
|
||||
|
||||
Vector<Value> m_padding;
|
||||
|
||||
GC::Ptr<FinishResults> m_finish_results;
|
||||
};
|
||||
|
||||
GC_DEFINE_ALLOCATOR(ZipIterator);
|
||||
|
||||
// 3 IteratorZip ( iters, mode, padding, finishResults ), https://tc39.es/proposal-joint-iteration/#sec-IteratorZip
|
||||
static GC::Ref<IteratorHelper> iterator_zip(Realm& realm, GC::Ref<ZipIterator> zip_iterator)
|
||||
{
|
||||
// 1. Let iterCount be the number of elements in iters.
|
||||
// 2. Let openIters be a copy of iters.
|
||||
|
||||
// 3. Let closure be a new Abstract Closure with no parameters that captures iters, iterCount, openIters, mode,
|
||||
// padding, and finishResults, and performs the following steps when called:
|
||||
auto closure = GC::create_function(realm.heap(), [zip_iterator](VM& vm, IteratorHelper&) -> ThrowCompletionOr<IteratorHelper::IterationResult> {
|
||||
return zip_iterator->next(vm);
|
||||
});
|
||||
auto abrupt_closure = GC::create_function(realm.heap(), [zip_iterator](VM& vm, Completion const& completion) -> ThrowCompletionOr<Value> {
|
||||
return zip_iterator->on_abrupt_completion(vm, completion);
|
||||
});
|
||||
|
||||
// 4. Let gen be CreateIteratorFromClosure(closure, "Iterator Helper", %IteratorHelperPrototype%, « [[UnderlyingIterators]] »).
|
||||
// 5. Set gen.[[UnderlyingIterators]] to openIters.
|
||||
// 6. Return gen.
|
||||
return IteratorHelper::create(realm, zip_iterator->open_iterators(), closure, abrupt_closure);
|
||||
}
|
||||
|
||||
static ThrowCompletionOr<ZipMode> get_zip_mode(VM& vm, Object const& options)
|
||||
{
|
||||
// 3. Let mode be ? Get(options, "mode").
|
||||
auto mode = TRY(options.get(vm.names.mode));
|
||||
|
||||
// 4. If mode is undefined, set mode to "shortest".
|
||||
if (mode.is_undefined())
|
||||
return ZipMode::Shortest;
|
||||
|
||||
// 5. If mode is not one of "shortest", "longest", or "strict", throw a TypeError exception.
|
||||
if (mode.is_string()) {
|
||||
auto mode_string = mode.as_string().utf8_string_view();
|
||||
|
||||
if (mode_string == "shortest"sv)
|
||||
return ZipMode::Shortest;
|
||||
if (mode_string == "longest"sv)
|
||||
return ZipMode::Longest;
|
||||
if (mode_string == "strict"sv)
|
||||
return ZipMode::Strict;
|
||||
}
|
||||
|
||||
return vm.throw_completion<TypeError>(ErrorType::OptionIsNotValidValue, mode, vm.names.mode);
|
||||
}
|
||||
|
||||
static ThrowCompletionOr<GC::Ptr<Object>> get_padding_option(VM& vm, Object const& options, ZipMode mode)
|
||||
{
|
||||
// 6. Let paddingOption be undefined.
|
||||
GC::Ptr<Object> padding_option;
|
||||
|
||||
// 7. If mode is "longest", then
|
||||
if (mode == ZipMode::Longest) {
|
||||
// a. Set paddingOption to ? Get(options, "padding").
|
||||
auto padding_value = TRY(options.get(vm.names.padding));
|
||||
|
||||
// b. If paddingOption is not undefined and paddingOption is not an Object, throw a TypeError exception.
|
||||
if (!padding_value.is_undefined()) {
|
||||
if (!padding_value.is_object())
|
||||
return vm.throw_completion<TypeError>(ErrorType::OptionIsNotValidValue, padding_value, vm.names.padding);
|
||||
|
||||
padding_option = padding_value.as_object();
|
||||
}
|
||||
}
|
||||
|
||||
return padding_option;
|
||||
}
|
||||
|
||||
// 1 Iterator.zip ( iterables [ , options ] ), https://tc39.es/proposal-joint-iteration/#sec-iterator.zip
|
||||
JS_DEFINE_NATIVE_FUNCTION(IteratorConstructor::zip)
|
||||
{
|
||||
auto& realm = *vm.current_realm();
|
||||
|
||||
auto iterables = vm.argument(0);
|
||||
auto options_value = vm.argument(1);
|
||||
|
||||
// 1. If iterables is not an Object, throw a TypeError exception.
|
||||
if (!iterables.is_object())
|
||||
return vm.throw_completion<TypeError>(ErrorType::NotAnObject, iterables);
|
||||
|
||||
// 2. Set options to ? GetOptionsObject(options).
|
||||
auto options = TRY(get_options_object(vm, options_value));
|
||||
|
||||
// 3. Let mode be ? Get(options, "mode").
|
||||
// 4. If mode is undefined, set mode to "shortest".
|
||||
// 5. If mode is not one of "shortest", "longest", or "strict", throw a TypeError exception.
|
||||
auto mode = TRY(get_zip_mode(vm, options));
|
||||
|
||||
// 6. Let paddingOption be undefined.
|
||||
// 7. If mode is "longest", then
|
||||
// a. Set paddingOption to ? Get(options, "padding").
|
||||
// b. If paddingOption is not undefined and paddingOption is not an Object, throw a TypeError exception.
|
||||
auto padding_option = TRY(get_padding_option(vm, options, mode));
|
||||
|
||||
// 8. Let iters be a new empty List.
|
||||
// 9. Let padding be a new empty List.
|
||||
auto zip_iterator = realm.create<ZipIterator>(realm, mode);
|
||||
|
||||
// 10. Let inputIter be ? GetIterator(iterables, SYNC).
|
||||
auto input_iterator = TRY(get_iterator(vm, iterables, IteratorHint::Sync));
|
||||
|
||||
// 11. Let next be NOT-STARTED.
|
||||
Optional<Value> next;
|
||||
|
||||
// 12. Repeat, while next is not DONE,
|
||||
do {
|
||||
// a. Set next to Completion(IteratorStepValue(inputIter)).
|
||||
// b. IfAbruptCloseIterators(next, iters).
|
||||
next = TRY_OR_CLOSE_ITERATORS(vm, zip_iterator->open_iterators(), iterator_step_value(vm, input_iterator));
|
||||
|
||||
// c. If next is not DONE, then
|
||||
if (next.has_value()) {
|
||||
// i. Let iter be Completion(GetIteratorFlattenable(next, REJECT-PRIMITIVES)).
|
||||
auto iterator = get_iterator_flattenable(vm, *next, PrimitiveHandling::RejectPrimitives);
|
||||
|
||||
// ii. IfAbruptCloseIterators(iter, the list-concatenation of « inputIter » and iters).
|
||||
if (iterator.is_error()) {
|
||||
// NB: We don't use TRY_OR_CLOSE_ITERATORS above in order to avoid creating a separate vector for the
|
||||
// IteratorCloseAll invocation. IteratorCloseAll would close the list in reverse order, which we
|
||||
// match here.
|
||||
auto error = iterator_close_all(vm, zip_iterator->open_iterators(), iterator.release_error());
|
||||
return iterator_close(vm, input_iterator, error);
|
||||
}
|
||||
|
||||
// iii. Append iter to iters.
|
||||
zip_iterator->append_iterator(iterator.release_value());
|
||||
}
|
||||
} while (next.has_value());
|
||||
|
||||
// 13. Let iterCount be the number of elements in iters.
|
||||
auto iterator_count = zip_iterator->open_iterators().size();
|
||||
|
||||
// 14. If mode is "longest", then
|
||||
if (mode == ZipMode::Longest) {
|
||||
// a. If paddingOption is undefined, then
|
||||
if (!padding_option) {
|
||||
// i. Perform the following steps iterCount times:
|
||||
for (size_t i = 0; i < iterator_count; ++i) {
|
||||
// 1. Append undefined to padding.
|
||||
zip_iterator->append_padding(js_undefined());
|
||||
}
|
||||
}
|
||||
// b. Else,
|
||||
else {
|
||||
// i. Let paddingIter be Completion(GetIterator(paddingOption, SYNC)).
|
||||
// ii. IfAbruptCloseIterators(paddingIter, iters).
|
||||
auto padding_iter = TRY_OR_CLOSE_ITERATORS(vm, zip_iterator->open_iterators(), get_iterator(vm, padding_option, IteratorHint::Sync));
|
||||
|
||||
// iii. Let usingIterator be true.
|
||||
auto using_iterator = true;
|
||||
|
||||
// iv. Perform the following steps iterCount times:
|
||||
for (size_t i = 0; i < iterator_count; ++i) {
|
||||
// 1. If usingIterator is true, then
|
||||
if (using_iterator) {
|
||||
// a. Set next to Completion(IteratorStepValue(paddingIter)).
|
||||
// b. IfAbruptCloseIterators(next, iters).
|
||||
next = TRY_OR_CLOSE_ITERATORS(vm, zip_iterator->open_iterators(), iterator_step_value(vm, padding_iter));
|
||||
|
||||
// c. If next is DONE, then
|
||||
if (!next.has_value()) {
|
||||
// i. Set usingIterator to false.
|
||||
using_iterator = false;
|
||||
}
|
||||
// d. Else,
|
||||
else {
|
||||
// i. Append next to padding.
|
||||
zip_iterator->append_padding(*next);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. If usingIterator is false, append undefined to padding.
|
||||
if (!using_iterator)
|
||||
zip_iterator->append_padding(js_undefined());
|
||||
}
|
||||
|
||||
// v. If usingIterator is true, then
|
||||
if (using_iterator) {
|
||||
// 1. Let completion be Completion(IteratorClose(paddingIter, NormalCompletion(UNUSED))).
|
||||
// 2. IfAbruptCloseIterators(completion, iters).
|
||||
TRY_OR_CLOSE_ITERATORS(vm, zip_iterator->open_iterators(), iterator_close(vm, padding_iter, normal_completion(js_undefined())));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 15. Let finishResults be a new Abstract Closure with parameters (results) that captures nothing and performs the
|
||||
// following steps when called:
|
||||
zip_iterator->set_finish_results(GC::create_function(vm.heap(), [](Realm& realm, ReadonlySpan<Value> results) -> Value {
|
||||
// a. Return CreateArrayFromList(results).
|
||||
return Array::create_from(realm, results);
|
||||
}));
|
||||
|
||||
// 16. Return IteratorZip(iters, mode, padding, finishResults).
|
||||
return iterator_zip(realm, zip_iterator);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
||||
* Copyright (c) 2023-2026, Tim Flynn <trflynn89@ladybird.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
|
@ -28,6 +28,7 @@ private:
|
|||
|
||||
JS_DECLARE_NATIVE_FUNCTION(concat);
|
||||
JS_DECLARE_NATIVE_FUNCTION(from);
|
||||
JS_DECLARE_NATIVE_FUNCTION(zip);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
|||
199
Tests/LibJS/Runtime/builtins/Iterator/Iterator.zip.js
Normal file
199
Tests/LibJS/Runtime/builtins/Iterator/Iterator.zip.js
Normal file
|
|
@ -0,0 +1,199 @@
|
|||
describe("errors", () => {
|
||||
test("called with non-Object", () => {
|
||||
expect(() => {
|
||||
Iterator.zip(Symbol.hasInstance);
|
||||
}).toThrowWithMessage(TypeError, "Symbol(Symbol.hasInstance) is not an object");
|
||||
|
||||
expect(() => {
|
||||
Iterator.zip({}, Symbol.hasInstance);
|
||||
}).toThrowWithMessage(TypeError, "Options is not an object");
|
||||
});
|
||||
|
||||
test("mode is not valid", () => {
|
||||
expect(() => {
|
||||
Iterator.zip([], { mode: Symbol.hasInstance });
|
||||
}).toThrowWithMessage(TypeError, "Symbol(Symbol.hasInstance) is not a valid value for option mode");
|
||||
expect(() => {
|
||||
Iterator.zip([], { mode: "foo" });
|
||||
}).toThrowWithMessage(TypeError, "foo is not a valid value for option mode");
|
||||
});
|
||||
|
||||
test("padding is not valid", () => {
|
||||
expect(() => {
|
||||
Iterator.zip([], { mode: "longest", padding: Symbol.hasInstance });
|
||||
}).toThrowWithMessage(TypeError, "Symbol(Symbol.hasInstance) is not a valid value for option padding");
|
||||
});
|
||||
|
||||
test("@@iterator is not callable", () => {
|
||||
const iterable = {};
|
||||
iterable[Symbol.iterator] = 12389;
|
||||
|
||||
expect(() => {
|
||||
Iterator.zip([iterable]);
|
||||
}).toThrowWithMessage(TypeError, "12389 is not a function");
|
||||
});
|
||||
|
||||
test("@@iterator throws an exception", () => {
|
||||
function TestError() {}
|
||||
|
||||
const iterable = {};
|
||||
iterable[Symbol.iterator] = () => {
|
||||
throw new TestError();
|
||||
};
|
||||
|
||||
expect(() => {
|
||||
Iterator.zip([iterable]);
|
||||
}).toThrow(TestError);
|
||||
});
|
||||
|
||||
test("@@iterator returns a non-Object", () => {
|
||||
const iterable = {};
|
||||
iterable[Symbol.iterator] = () => {
|
||||
return Symbol.hasInstance;
|
||||
};
|
||||
|
||||
expect(() => {
|
||||
Iterator.zip([iterable]);
|
||||
}).toThrowWithMessage(TypeError, "Symbol(Symbol.hasInstance) is not an object");
|
||||
});
|
||||
|
||||
test("strict mode with unbalanced iterator values", () => {
|
||||
expect(() => {
|
||||
Iterator.zip([[0], []], { mode: "strict" }).toArray();
|
||||
}).toThrowWithMessage(TypeError, "Not enough iterator results in 'strict' mode");
|
||||
|
||||
expect(() => {
|
||||
Iterator.zip([[], [2]], { mode: "strict" }).toArray();
|
||||
}).toThrowWithMessage(TypeError, "Not enough iterator results in 'strict' mode");
|
||||
|
||||
expect(() => {
|
||||
Iterator.zip([[0, 1], [2]], { mode: "strict" }).toArray();
|
||||
}).toThrowWithMessage(TypeError, "Not enough iterator results in 'strict' mode");
|
||||
|
||||
expect(() => {
|
||||
Iterator.zip([[0], [2, 3]], { mode: "strict" }).toArray();
|
||||
}).toThrowWithMessage(TypeError, "Not enough iterator results in 'strict' mode");
|
||||
});
|
||||
});
|
||||
|
||||
describe("normal behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Iterator.zip).toHaveLength(1);
|
||||
});
|
||||
|
||||
let result;
|
||||
|
||||
test("mode=shortest", () => {
|
||||
result = Iterator.zip([], { mode: "shortest" });
|
||||
expect(result.toArray()).toEqual([]);
|
||||
|
||||
result = Iterator.zip([[]], { mode: "shortest" });
|
||||
expect(result.toArray()).toEqual([]);
|
||||
|
||||
result = Iterator.zip([[0]], { mode: "shortest" });
|
||||
expect(result.toArray()).toEqual([[0]]);
|
||||
|
||||
result = Iterator.zip([[0], []], { mode: "shortest" });
|
||||
expect(result.toArray()).toEqual([]);
|
||||
|
||||
result = Iterator.zip([[0], [2]], { mode: "shortest" });
|
||||
expect(result.toArray()).toEqual([[0, 2]]);
|
||||
|
||||
result = Iterator.zip([[0], [2, 3]], { mode: "shortest" });
|
||||
expect(result.toArray()).toEqual([[0, 2]]);
|
||||
|
||||
result = Iterator.zip([[0, 1], [2]], { mode: "shortest" });
|
||||
expect(result.toArray()).toEqual([[0, 2]]);
|
||||
|
||||
result = Iterator.zip(
|
||||
[
|
||||
[0, 1],
|
||||
[2, 3],
|
||||
],
|
||||
{ mode: "shortest" }
|
||||
);
|
||||
expect(result.toArray()).toEqual([
|
||||
[0, 2],
|
||||
[1, 3],
|
||||
]);
|
||||
});
|
||||
|
||||
test("mode=longest", () => {
|
||||
result = Iterator.zip([], { mode: "longest" });
|
||||
expect(result.toArray()).toEqual([]);
|
||||
|
||||
result = Iterator.zip([[]], { mode: "longest" });
|
||||
expect(result.toArray()).toEqual([]);
|
||||
|
||||
result = Iterator.zip([[0]], { mode: "longest" });
|
||||
expect(result.toArray()).toEqual([[0]]);
|
||||
|
||||
result = Iterator.zip([[0], []], { mode: "longest" });
|
||||
expect(result.toArray()).toEqual([[0, undefined]]);
|
||||
|
||||
result = Iterator.zip([[0], []], { mode: "longest", padding: [undefined, 12389] });
|
||||
expect(result.toArray()).toEqual([[0, 12389]]);
|
||||
|
||||
result = Iterator.zip([[0], [2]], { mode: "longest" });
|
||||
expect(result.toArray()).toEqual([[0, 2]]);
|
||||
|
||||
result = Iterator.zip([[0], [2, 3]], { mode: "longest" });
|
||||
expect(result.toArray()).toEqual([
|
||||
[0, 2],
|
||||
[undefined, 3],
|
||||
]);
|
||||
|
||||
result = Iterator.zip([[0], [2, 3]], { mode: "longest", padding: [12389] });
|
||||
expect(result.toArray()).toEqual([
|
||||
[0, 2],
|
||||
[12389, 3],
|
||||
]);
|
||||
|
||||
result = Iterator.zip([[0, 1], [2]], { mode: "longest" });
|
||||
expect(result.toArray()).toEqual([
|
||||
[0, 2],
|
||||
[1, undefined],
|
||||
]);
|
||||
|
||||
result = Iterator.zip([[0, 1], [2]], { mode: "longest", padding: [undefined, 12389] });
|
||||
expect(result.toArray()).toEqual([
|
||||
[0, 2],
|
||||
[1, 12389],
|
||||
]);
|
||||
|
||||
result = Iterator.zip(
|
||||
[
|
||||
[0, 1],
|
||||
[2, 3],
|
||||
],
|
||||
{ mode: "longest" }
|
||||
);
|
||||
expect(result.toArray()).toEqual([
|
||||
[0, 2],
|
||||
[1, 3],
|
||||
]);
|
||||
});
|
||||
|
||||
test("mode=strict", () => {
|
||||
result = Iterator.zip([], { mode: "strict" });
|
||||
expect(result.toArray()).toEqual([]);
|
||||
|
||||
result = Iterator.zip([[]], { mode: "strict" });
|
||||
expect(result.toArray()).toEqual([]);
|
||||
|
||||
result = Iterator.zip([[0]], { mode: "strict" });
|
||||
expect(result.toArray()).toEqual([[0]]);
|
||||
|
||||
result = Iterator.zip([[0], [2]], { mode: "strict" });
|
||||
expect(result.toArray()).toEqual([[0, 2]]);
|
||||
|
||||
result = Iterator.zip([
|
||||
[0, 1],
|
||||
[2, 3],
|
||||
]);
|
||||
expect(result.toArray()).toEqual([
|
||||
[0, 2],
|
||||
[1, 3],
|
||||
]);
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue