* Copyright (c) 2023-2024 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_async_from_sync_iterator.h"
#include "ecmascript/builtins/builtins_promise.h"
#include "ecmascript/builtins/builtins_promise_handler.h"
#include "ecmascript/global_env.h"
#include "ecmascript/interpreter/interpreter.h"
#include "ecmascript/js_iterator.h"
namespace panda::ecmascript {
JSHandle<JSTaggedValue> JSAsyncFromSyncIterator::CreateAsyncFromSyncIterator(JSThread *thread,
JSHandle<AsyncIteratorRecord> &syncIteratorRecord)
{
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
JSHandle<JSFunction> IterFunc(env->GetAsyncFromSyncIterator());
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
JSHandle<JSAsyncFromSyncIterator> asyncIterator(factory->NewJSObjectByConstructor(IterFunc));
asyncIterator->SetSyncIteratorRecord(thread, syncIteratorRecord);
JSHandle<JSTaggedValue> nextStr = thread->GlobalConstants()->GetHandledNextString();
JSHandle<JSTaggedValue> tmpAsyncIterator(thread, asyncIterator.GetTaggedValue());
JSHandle<JSTaggedValue> nextMethod = JSTaggedValue::GetProperty(thread, tmpAsyncIterator, nextStr).GetValue();
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
JSHandle<AsyncIteratorRecord> iteratorRecord = factory->NewAsyncIteratorRecord(tmpAsyncIterator, nextMethod, false);
return JSHandle<JSTaggedValue>(thread, iteratorRecord->GetIterator(thread));
}
JSTaggedValue JSAsyncFromSyncIterator::AsyncFromSyncIteratorContinuation(JSThread *thread,
JSHandle<JSTaggedValue> &result,
JSHandle<PromiseCapability> &promiseCapability)
{
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
bool done = JSIterator::IteratorComplete(thread, result);
JSHandle<JSTaggedValue> tmpDone(thread, JSTaggedValue(done));
RETURN_REJECT_PROMISE_IF_ABRUPT(thread, tmpDone, promiseCapability);
JSHandle<JSTaggedValue> value = JSIterator::IteratorValue(thread, result);
RETURN_REJECT_PROMISE_IF_ABRUPT(thread, value, promiseCapability);
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
JSHandle<JSTaggedValue> valueWrapper =
builtins::BuiltinsPromiseHandler::PromiseResolve(thread,
JSHandle<JSTaggedValue>::Cast(env->GetPromiseFunction()),
value);
RETURN_REJECT_PROMISE_IF_ABRUPT(thread, valueWrapper, promiseCapability);
JSHandle<JSObject> promise = JSHandle<JSObject>::Cast(valueWrapper);
JSHandle<JSAsyncFromSyncIterUnwarpFunction> onFulfilled = factory->NewJSAsyncFromSyncIterUnwarpFunction();
onFulfilled->SetDone(thread, JSTaggedValue(done));
JSHandle<JSTaggedValue> onFulRejected(thread, JSTaggedValue::Undefined());
#if ENABLE_LATEST_OPTIMIZATION
builtins::BuiltinsPromise::PerformPromiseThen(thread, JSHandle<JSPromise>::Cast(promise),
JSHandle<JSTaggedValue>::Cast(onFulfilled),
onFulRejected, JSHandle<JSTaggedValue>::Cast(promiseCapability));
#else
builtins::BuiltinsPromise::PerformPromiseThen(thread, JSHandle<JSPromise>::Cast(promise),
JSHandle<JSTaggedValue>::Cast(onFulfilled),
onFulRejected, promiseCapability);
#endif
return promiseCapability->GetPromise(thread);
}
JSTaggedValue JSAsyncFromSyncIterator::AsyncFromSyncIterUnwarpFunction(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
JSHandle<JSAsyncFromSyncIterUnwarpFunction> unwarpFunction =
JSHandle<JSAsyncFromSyncIterUnwarpFunction>::Cast((base::BuiltinsBase::GetConstructor(argv)));
JSHandle<JSTaggedValue> value = base::BuiltinsBase::GetCallArg(argv, 0);
bool done = unwarpFunction->GetDone(thread).ToBoolean();
return JSIterator::CreateIterResultObject(thread, value, done).GetTaggedValue();
}
}