IteratorOperations.h [plain text]
#pragma once
#include "JSCJSValue.h"
#include "JSObject.h"
#include "ThrowScope.h"
namespace JSC {
JSValue iteratorNext(ExecState*, JSValue iterator, JSValue);
JSValue iteratorNext(ExecState*, JSValue iterator);
JS_EXPORT_PRIVATE JSValue iteratorValue(ExecState*, JSValue iterator);
bool iteratorComplete(ExecState*, JSValue iterator);
JS_EXPORT_PRIVATE JSValue iteratorStep(ExecState*, JSValue iterator);
JS_EXPORT_PRIVATE void iteratorClose(ExecState*, JSValue iterator);
JS_EXPORT_PRIVATE JSObject* createIteratorResultObject(ExecState*, JSValue, bool done);
Structure* createIteratorResultObjectStructure(VM&, JSGlobalObject&);
JS_EXPORT_PRIVATE JSValue iteratorForIterable(ExecState*, JSValue iterable);
template <typename CallBackType>
void forEachInIterable(ExecState* exec, JSValue iterable, const CallBackType& callback)
{
auto& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue iterator = iteratorForIterable(exec, iterable);
RETURN_IF_EXCEPTION(scope, void());
while (true) {
JSValue next = iteratorStep(exec, iterator);
if (UNLIKELY(scope.exception()) || next.isFalse())
return;
JSValue nextValue = iteratorValue(exec, next);
RETURN_IF_EXCEPTION(scope, void());
callback(vm, exec, nextValue);
if (UNLIKELY(scope.exception())) {
scope.release();
iteratorClose(exec, iterator);
return;
}
}
}
}