* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecmascript/js_iterator.h"
#include "ecmascript/interpreter/interpreter.h"
#include "ecmascript/js_async_from_sync_iterator.h"
#include "ecmascript/object_fast_operator-inl.h"
namespace panda::ecmascript {
JSTaggedValue JSIterator::IteratorCloseAndReturn(JSThread *thread, const JSHandle<JSTaggedValue> &iter)
{
ASSERT(thread->HasPendingException());
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
JSTaggedValue exception = thread->GetException();
JSHandle<JSTaggedValue> record = JSHandle<JSTaggedValue>(factory->NewCompletionRecord(CompletionRecordType::THROW,
JSHandle<JSTaggedValue>(thread, exception)));
JSHandle<JSTaggedValue> result = JSIterator::IteratorClose(thread, iter, record);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
if (result->IsCompletionRecord()) {
return CompletionRecord::Cast(result->GetTaggedObject())->GetValue(thread);
}
return result.GetTaggedValue();
}
JSHandle<JSTaggedValue> JSIterator::GetIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj)
{
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, obj);
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
JSHandle<JSTaggedValue> iteratorSymbol = thread->GlobalConstants()->GetHandledIteratorSymbol();
JSHandle<JSTaggedValue> func = JSObject::GetMethod(thread, obj, iteratorSymbol);
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, obj);
return GetIterator(thread, obj, func);
}
JSHandle<JSTaggedValue> JSIterator::GetIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj,
const JSHandle<JSTaggedValue> &method)
{
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, obj);
JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, method, obj, undefined, 0);
JSTaggedValue ret = JSFunction::Call(info);
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
JSHandle<JSTaggedValue> iter(thread, ret);
if (!iter->IsECMAObject()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "JSIterator::GetIterator: iter is not object", undefined);
}
return iter;
}
JSHandle<JSTaggedValue> JSIterator::GetAsyncIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj)
{
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
JSHandle<JSTaggedValue> asynciteratorSymbol = thread->GlobalConstants()->GetHandledAsyncIteratorSymbol();
JSHandle<JSTaggedValue> method = JSObject::GetMethod(thread, obj, asynciteratorSymbol);
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, obj);
if (method->IsUndefined()) {
JSHandle<JSTaggedValue> iteratorSymbol = thread->GlobalConstants()->GetHandledIteratorSymbol();
JSHandle<JSTaggedValue> func = JSObject::GetMethod(thread, obj, iteratorSymbol);
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
JSHandle<JSTaggedValue> syncIterator = GetIterator(thread, obj, func);
JSHandle<JSTaggedValue> nextStr = thread->GlobalConstants()->GetHandledNextString();
JSHandle<JSTaggedValue> nextMethod = JSTaggedValue::GetProperty(thread, syncIterator, nextStr).GetValue();
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
JSHandle<AsyncIteratorRecord> syncIteratorRecord =
factory->NewAsyncIteratorRecord(syncIterator, nextMethod, false);
JSHandle<JSTaggedValue> asyncIterator =
JSAsyncFromSyncIterator::CreateAsyncFromSyncIterator(thread, syncIteratorRecord);
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
return asyncIterator;
}
JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, method, obj, undefined, 0);
JSTaggedValue ret = JSFunction::Call(info);
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
JSHandle<JSTaggedValue> iterator(thread, ret);
if (!iterator->IsECMAObject()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "Is not object", undefined);
}
return iterator;
}
JSHandle<JSTaggedValue> JSIterator::IteratorNext(JSThread *thread, const JSHandle<JSTaggedValue> &iter)
{
const GlobalEnvConstants *globalConst = thread->GlobalConstants();
JSHandle<JSTaggedValue> key(globalConst->GetHandledNextString());
JSHandle<JSTaggedValue> next(JSObject::GetMethod(thread, iter, key));
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, next, iter, undefined, 0);
JSTaggedValue ret = JSFunction::Call(info);
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
if (!ret.IsECMAObject()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "Is not Object", undefined);
}
JSHandle<JSTaggedValue> result(thread, ret);
return result;
}
JSHandle<JSTaggedValue> JSIterator::IteratorNext(JSThread *thread, const JSHandle<JSTaggedValue> &iter,
const JSHandle<JSTaggedValue> &value)
{
const GlobalEnvConstants *globalConst = thread->GlobalConstants();
JSHandle<JSTaggedValue> key(globalConst->GetHandledNextString());
JSHandle<JSTaggedValue> next(JSObject::GetMethod(thread, iter, key));
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, next, iter, undefined, 1);
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
info->SetCallArg(value.GetTaggedValue());
JSTaggedValue ret = JSFunction::Call(info);
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
if (!ret.IsECMAObject()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "Is not Object", undefined);
}
JSHandle<JSTaggedValue> result(thread, ret);
return result;
}
JSHandle<JSTaggedValue> JSIterator::IteratorNext(JSThread *thread, const JSHandle<AsyncIteratorRecord> &iter,
const JSHandle<JSTaggedValue> &value)
{
const GlobalEnvConstants *globalConst = thread->GlobalConstants();
JSHandle<JSTaggedValue> iterator(thread, iter->GetIterator(thread));
JSHandle<JSTaggedValue> next(thread, iter->GetNextMethod(thread));
JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, next, iterator, undefined, 1);
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
info->SetCallArg(value.GetTaggedValue());
JSTaggedValue ret = JSFunction::Call(info);
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
if (!ret.IsECMAObject()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "Is not Object", undefined);
}
JSHandle<JSTaggedValue> result(thread, ret);
return result;
}
JSHandle<JSTaggedValue> JSIterator::IteratorNext(JSThread *thread, const JSHandle<AsyncIteratorRecord> &iter)
{
const GlobalEnvConstants *globalConst = thread->GlobalConstants();
JSHandle<JSTaggedValue> iterator(thread, iter->GetIterator(thread));
JSHandle<JSTaggedValue> next(thread, iter->GetNextMethod(thread));
JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, next, iterator, undefined, 0);
JSTaggedValue ret = JSFunction::Call(info);
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
if (!ret.IsECMAObject()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "Is not Object", undefined);
}
JSHandle<JSTaggedValue> result(thread, ret);
return result;
}
bool JSIterator::IteratorComplete(JSThread *thread, const JSHandle<JSTaggedValue> &iterResult)
{
ASSERT_PRINT(iterResult->IsECMAObject(), "iterResult must be JSObject");
JSHandle<JSTaggedValue> doneStr = thread->GlobalConstants()->GetHandledDoneString();
JSHandle<JSTaggedValue> done = JSTaggedValue::GetProperty(thread, iterResult, doneStr).GetValue();
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
return done->ToBoolean();
}
JSHandle<JSTaggedValue> JSIterator::IteratorValue(JSThread *thread, const JSHandle<JSTaggedValue> &iterResult)
{
ASSERT_PRINT(iterResult->IsECMAObject(), "iterResult must be JSObject");
JSHandle<JSTaggedValue> valueStr = thread->GlobalConstants()->GetHandledValueString();
JSHandle<JSTaggedValue> value = JSTaggedValue::GetProperty(thread, iterResult, valueStr).GetValue();
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
return value;
}
JSHandle<JSTaggedValue> JSIterator::IteratorStep(JSThread *thread, const JSHandle<JSTaggedValue> &iter)
{
JSHandle<JSTaggedValue> result = IteratorNext(thread, iter);
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, result);
bool done = IteratorComplete(thread, result);
JSHandle<JSTaggedValue> doneHandle(thread, JSTaggedValue(done));
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, doneHandle);
if (done) {
JSHandle<JSTaggedValue> falseHandle(thread, JSTaggedValue::False());
return falseHandle;
}
return result;
}
JSHandle<JSTaggedValue> JSIterator::IteratorClose(JSThread *thread, const JSHandle<JSTaggedValue> &iter,
const JSHandle<JSTaggedValue> &completion)
{
ASSERT_PRINT(iter->IsECMAObject(), "iter must be JSObject");
const GlobalEnvConstants *globalConst = thread->GlobalConstants();
JSHandle<JSTaggedValue> exceptionOnThread;
if (thread->HasPendingException()) {
exceptionOnThread = JSHandle<JSTaggedValue>(thread, thread->GetException());
thread->ClearExceptionAndExtraErrorMessage();
}
JSTaggedValue returnStr = globalConst->GetReturnString();
JSTaggedValue func = ObjectFastOperator::FastGetPropertyByName(thread, iter.GetTaggedValue(), returnStr);
JSHandle<JSTaggedValue> returnFunc(thread, func);
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, returnFunc);
if (returnFunc->IsUndefined() || returnFunc->IsNull()) {
if (!exceptionOnThread.IsEmpty()) {
thread->SetException(exceptionOnThread.GetTaggedValue());
}
return completion;
}
if (!returnFunc->IsCallable()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "return function is not Callable", returnFunc);
}
JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, returnFunc, iter, undefined, 0);
JSTaggedValue ret = JSFunction::Call(info);
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
if (!exceptionOnThread.IsEmpty()) {
thread->SetException(exceptionOnThread.GetTaggedValue());
}
JSHandle<JSTaggedValue> innerResult(thread, ret);
JSHandle<CompletionRecord> completionRecord(thread, globalConst->GetUndefined());
if (completion->IsCompletionRecord()) {
completionRecord = JSHandle<CompletionRecord>::Cast(completion);
}
if (!completionRecord.GetTaggedValue().IsUndefined() && completionRecord->IsThrow()) {
if (!exceptionOnThread.IsEmpty()) {
thread->SetException(exceptionOnThread.GetTaggedValue());
}
return completion;
}
if (thread->HasPendingException()) {
return innerResult;
}
if (!innerResult->IsECMAObject()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "Is not object", undefined);
}
if (!exceptionOnThread.IsEmpty()) {
thread->SetException(exceptionOnThread.GetTaggedValue());
}
return completion;
}
JSHandle<JSObject> JSIterator::CreateIterResultObject(JSThread *thread, const JSHandle<JSTaggedValue> &value, bool done)
{
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
JSHandle<JSHClass> klass = JSHandle<JSHClass>::Cast(env->GetIteratorResultClass());
JSHandle<JSObject> obj = factory->NewJSObject(klass);
obj->SetPropertyInlinedPropsWithSize<JSIterator::SIZE, VALUE_INLINE_PROPERTY_INDEX>(
thread, value.GetTaggedValue());
obj->SetPropertyInlinedPropsWithSize<JSIterator::SIZE, DONE_INLINE_PROPERTY_INDEX>(
thread, JSTaggedValue(done));
return obj;
}
}