2017-08-12 16:48:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Yusuke Suzuki <utatane.tea@gmail.com>.
|
|
|
|
* Copyright (C) 2016-2017 Apple Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
|
|
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|
|
|
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "IteratorOperations.h"
|
|
|
|
|
|
|
|
#include "JSCInlines.h"
|
|
|
|
#include "ObjectConstructor.h"
|
|
|
|
|
|
|
|
namespace JSC {
|
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue iteratorNext(JSGlobalObject* globalObject, IterationRecord iterationRecord, JSValue argument)
|
2017-08-12 16:48:01 +00:00
|
|
|
{
|
2022-10-23 02:55:20 +00:00
|
|
|
VM& vm = globalObject->vm();
|
2017-08-12 16:48:01 +00:00
|
|
|
auto scope = DECLARE_THROW_SCOPE(vm);
|
|
|
|
|
2020-08-29 13:27:11 +00:00
|
|
|
JSValue iterator = iterationRecord.iterator;
|
|
|
|
JSValue nextFunction = iterationRecord.nextMethod;
|
2017-08-12 16:48:01 +00:00
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
auto nextFunctionCallData = getCallData(vm, nextFunction);
|
|
|
|
if (nextFunctionCallData.type == CallData::Type::None)
|
|
|
|
return throwTypeError(globalObject, scope);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
|
|
|
MarkedArgumentBuffer nextFunctionArguments;
|
2020-08-29 13:27:11 +00:00
|
|
|
if (!argument.isEmpty())
|
|
|
|
nextFunctionArguments.append(argument);
|
|
|
|
ASSERT(!nextFunctionArguments.hasOverflowed());
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue result = call(globalObject, nextFunction, nextFunctionCallData, iterator, nextFunctionArguments);
|
2017-08-12 16:48:01 +00:00
|
|
|
RETURN_IF_EXCEPTION(scope, JSValue());
|
|
|
|
|
|
|
|
if (!result.isObject())
|
2022-10-23 02:55:20 +00:00
|
|
|
return throwTypeError(globalObject, scope, "Iterator result interface is not an object."_s);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue iteratorValue(JSGlobalObject* globalObject, JSValue iterResult)
|
2017-08-12 16:48:01 +00:00
|
|
|
{
|
2022-10-23 02:55:20 +00:00
|
|
|
return iterResult.get(globalObject, globalObject->vm().propertyNames->value);
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
bool iteratorComplete(JSGlobalObject* globalObject, JSValue iterResult)
|
2017-08-12 16:48:01 +00:00
|
|
|
{
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue done = iterResult.get(globalObject, globalObject->vm().propertyNames->done);
|
|
|
|
return done.toBoolean(globalObject);
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue iteratorStep(JSGlobalObject* globalObject, IterationRecord iterationRecord)
|
2017-08-12 16:48:01 +00:00
|
|
|
{
|
2022-10-23 02:55:20 +00:00
|
|
|
VM& vm = globalObject->vm();
|
2017-08-12 16:48:01 +00:00
|
|
|
auto scope = DECLARE_THROW_SCOPE(vm);
|
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue result = iteratorNext(globalObject, iterationRecord);
|
2017-08-12 16:48:01 +00:00
|
|
|
RETURN_IF_EXCEPTION(scope, JSValue());
|
2022-10-23 02:55:20 +00:00
|
|
|
bool done = iteratorComplete(globalObject, result);
|
2017-08-12 16:48:01 +00:00
|
|
|
RETURN_IF_EXCEPTION(scope, JSValue());
|
|
|
|
if (done)
|
|
|
|
return jsBoolean(false);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
void iteratorClose(JSGlobalObject* globalObject, IterationRecord iterationRecord)
|
2017-08-12 16:48:01 +00:00
|
|
|
{
|
2022-10-23 02:55:20 +00:00
|
|
|
VM& vm = globalObject->vm();
|
2017-08-12 16:48:01 +00:00
|
|
|
auto throwScope = DECLARE_THROW_SCOPE(vm);
|
|
|
|
auto catchScope = DECLARE_CATCH_SCOPE(vm);
|
|
|
|
|
|
|
|
Exception* exception = nullptr;
|
|
|
|
if (UNLIKELY(catchScope.exception())) {
|
|
|
|
exception = catchScope.exception();
|
|
|
|
catchScope.clearException();
|
|
|
|
}
|
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue returnFunction = iterationRecord.iterator.get(globalObject, vm.propertyNames->returnKeyword);
|
|
|
|
if (UNLIKELY(throwScope.exception()) || returnFunction.isUndefinedOrNull()) {
|
2017-08-12 16:48:01 +00:00
|
|
|
if (exception)
|
2022-10-23 02:55:20 +00:00
|
|
|
throwException(globalObject, throwScope, exception);
|
2017-08-12 16:48:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
auto returnFunctionCallData = getCallData(vm, returnFunction);
|
|
|
|
if (returnFunctionCallData.type == CallData::Type::None) {
|
2017-08-12 16:48:01 +00:00
|
|
|
if (exception)
|
2022-10-23 02:55:20 +00:00
|
|
|
throwException(globalObject, throwScope, exception);
|
2017-08-12 16:48:01 +00:00
|
|
|
else
|
2022-10-23 02:55:20 +00:00
|
|
|
throwTypeError(globalObject, throwScope);
|
2017-08-12 16:48:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MarkedArgumentBuffer returnFunctionArguments;
|
2020-08-29 13:27:11 +00:00
|
|
|
ASSERT(!returnFunctionArguments.hasOverflowed());
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue innerResult = call(globalObject, returnFunction, returnFunctionCallData, iterationRecord.iterator, returnFunctionArguments);
|
2017-08-12 16:48:01 +00:00
|
|
|
|
|
|
|
if (exception) {
|
2022-10-23 02:55:20 +00:00
|
|
|
throwException(globalObject, throwScope, exception);
|
2017-08-12 16:48:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RETURN_IF_EXCEPTION(throwScope, void());
|
|
|
|
|
|
|
|
if (!innerResult.isObject()) {
|
2022-10-23 02:55:20 +00:00
|
|
|
throwTypeError(globalObject, throwScope, "Iterator result interface is not an object."_s);
|
2017-08-12 16:48:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
static constexpr PropertyOffset valuePropertyOffset = 0;
|
|
|
|
static constexpr PropertyOffset donePropertyOffset = 1;
|
2017-08-12 16:48:01 +00:00
|
|
|
|
|
|
|
Structure* createIteratorResultObjectStructure(VM& vm, JSGlobalObject& globalObject)
|
|
|
|
{
|
2020-08-29 13:27:11 +00:00
|
|
|
Structure* iteratorResultStructure = vm.structureCache.emptyObjectStructureForPrototype(&globalObject, globalObject.objectPrototype(), JSFinalObject::defaultInlineCapacity());
|
2017-08-12 16:48:01 +00:00
|
|
|
PropertyOffset offset;
|
|
|
|
iteratorResultStructure = Structure::addPropertyTransition(vm, iteratorResultStructure, vm.propertyNames->value, 0, offset);
|
|
|
|
RELEASE_ASSERT(offset == valuePropertyOffset);
|
2020-08-29 13:27:11 +00:00
|
|
|
iteratorResultStructure = Structure::addPropertyTransition(vm, iteratorResultStructure, vm.propertyNames->done, 0, offset);
|
|
|
|
RELEASE_ASSERT(offset == donePropertyOffset);
|
2017-08-12 16:48:01 +00:00
|
|
|
return iteratorResultStructure;
|
|
|
|
}
|
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSObject* createIteratorResultObject(JSGlobalObject* globalObject, JSValue value, bool done)
|
2017-08-12 16:48:01 +00:00
|
|
|
{
|
2022-10-23 02:55:20 +00:00
|
|
|
VM& vm = globalObject->vm();
|
|
|
|
JSObject* resultObject = constructEmptyObject(vm, globalObject->iteratorResultObjectStructure());
|
2017-08-12 16:48:01 +00:00
|
|
|
resultObject->putDirect(vm, valuePropertyOffset, value);
|
2020-08-29 13:27:11 +00:00
|
|
|
resultObject->putDirect(vm, donePropertyOffset, jsBoolean(done));
|
2017-08-12 16:48:01 +00:00
|
|
|
return resultObject;
|
|
|
|
}
|
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
bool hasIteratorMethod(JSGlobalObject* globalObject, JSValue value)
|
2018-01-03 05:16:05 +00:00
|
|
|
{
|
2022-10-23 02:55:20 +00:00
|
|
|
auto& vm = globalObject->vm();
|
2018-01-03 05:16:05 +00:00
|
|
|
auto scope = DECLARE_THROW_SCOPE(vm);
|
|
|
|
|
|
|
|
if (!value.isObject())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
JSObject* object = asObject(value);
|
|
|
|
CallData callData;
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue applyMethod = object->getMethod(globalObject, callData, vm.propertyNames->iteratorSymbol, "Symbol.iterator property should be callable"_s);
|
2018-01-03 05:16:05 +00:00
|
|
|
RETURN_IF_EXCEPTION(scope, false);
|
|
|
|
|
|
|
|
return !applyMethod.isUndefined();
|
|
|
|
}
|
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue iteratorMethod(JSGlobalObject* globalObject, JSObject* object)
|
2018-01-03 05:16:05 +00:00
|
|
|
{
|
2022-10-23 02:55:20 +00:00
|
|
|
auto& vm = globalObject->vm();
|
2018-01-03 05:16:05 +00:00
|
|
|
auto scope = DECLARE_THROW_SCOPE(vm);
|
|
|
|
|
|
|
|
CallData callData;
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue method = object->getMethod(globalObject, callData, vm.propertyNames->iteratorSymbol, "Symbol.iterator property should be callable"_s);
|
2018-01-03 05:16:05 +00:00
|
|
|
RETURN_IF_EXCEPTION(scope, jsUndefined());
|
|
|
|
|
|
|
|
return method;
|
|
|
|
}
|
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
IterationRecord iteratorForIterable(JSGlobalObject* globalObject, JSObject* object, JSValue iteratorMethod)
|
2018-01-03 05:16:05 +00:00
|
|
|
{
|
2022-10-23 02:55:20 +00:00
|
|
|
VM& vm = globalObject->vm();
|
2018-01-03 05:16:05 +00:00
|
|
|
auto scope = DECLARE_THROW_SCOPE(vm);
|
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
auto iteratorMethodCallData = getCallData(vm, iteratorMethod);
|
|
|
|
if (iteratorMethodCallData.type == CallData::Type::None) {
|
|
|
|
throwTypeError(globalObject, scope);
|
2018-01-03 05:16:05 +00:00
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
|
|
|
ArgList iteratorMethodArguments;
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue iterator = call(globalObject, iteratorMethod, iteratorMethodCallData, object, iteratorMethodArguments);
|
2018-01-03 05:16:05 +00:00
|
|
|
RETURN_IF_EXCEPTION(scope, { });
|
|
|
|
|
|
|
|
if (!iterator.isObject()) {
|
2022-10-23 02:55:20 +00:00
|
|
|
throwTypeError(globalObject, scope);
|
2018-01-03 05:16:05 +00:00
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue nextMethod = iterator.getObject()->get(globalObject, vm.propertyNames->next);
|
2020-08-29 13:27:11 +00:00
|
|
|
RETURN_IF_EXCEPTION(scope, { });
|
|
|
|
|
|
|
|
return { iterator, nextMethod };
|
2018-01-03 05:16:05 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
IterationRecord iteratorForIterable(JSGlobalObject* globalObject, JSValue iterable)
|
2017-08-12 16:48:01 +00:00
|
|
|
{
|
2022-10-23 02:55:20 +00:00
|
|
|
VM& vm = globalObject->vm();
|
2017-08-12 16:48:01 +00:00
|
|
|
auto scope = DECLARE_THROW_SCOPE(vm);
|
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue iteratorFunction = iterable.get(globalObject, vm.propertyNames->iteratorSymbol);
|
2020-08-29 13:27:11 +00:00
|
|
|
RETURN_IF_EXCEPTION(scope, { });
|
2017-08-12 16:48:01 +00:00
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
auto iteratorFunctionCallData = getCallData(vm, iteratorFunction);
|
|
|
|
if (iteratorFunctionCallData.type == CallData::Type::None) {
|
|
|
|
throwTypeError(globalObject, scope);
|
2020-08-29 13:27:11 +00:00
|
|
|
return { };
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ArgList iteratorFunctionArguments;
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue iterator = call(globalObject, iteratorFunction, iteratorFunctionCallData, iterable, iteratorFunctionArguments);
|
2020-08-29 13:27:11 +00:00
|
|
|
RETURN_IF_EXCEPTION(scope, { });
|
2017-08-12 16:48:01 +00:00
|
|
|
|
|
|
|
if (!iterator.isObject()) {
|
2022-10-23 02:55:20 +00:00
|
|
|
throwTypeError(globalObject, scope);
|
2020-08-29 13:27:11 +00:00
|
|
|
return { };
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 02:55:20 +00:00
|
|
|
JSValue nextMethod = iterator.getObject()->get(globalObject, vm.propertyNames->next);
|
2020-08-29 13:27:11 +00:00
|
|
|
RETURN_IF_EXCEPTION(scope, { });
|
|
|
|
|
|
|
|
return { iterator, nextMethod };
|
2017-08-12 16:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace JSC
|