* 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_async_function.h"
#include "ecmascript/async_generator_helper.h"
#include "ecmascript/builtins/builtins_promise.h"
#include "ecmascript/builtins/builtins_promise_handler.h"
#include "ecmascript/debugger/js_debugger_manager.h"
#include "ecmascript/dfx/stackinfo/async_stack_trace.h"
#include "ecmascript/generator_helper.h"
#include "ecmascript/global_env.h"
#include "ecmascript/interpreter/interpreter.h"
#include "ecmascript/js_async_generator_object.h"
namespace panda::ecmascript {
using BuiltinsPromiseHandler = builtins::BuiltinsPromiseHandler;
using BuiltinsPromise = builtins::BuiltinsPromise;
void JSAsyncFunction::AsyncFunctionAwait(JSThread *thread, const JSHandle<JSTaggedValue> &asyncFuncObj,
const JSHandle<JSTaggedValue> &value)
{
auto vm = thread->GetEcmaVM();
ObjectFactory *factory = vm->GetFactory();
JSHandle<JSTaggedValue> asyncCtxt;
if (asyncFuncObj->IsAsyncGeneratorObject()) {
JSHandle<JSObject> obj = JSTaggedValue::ToObject(thread, asyncFuncObj);
RETURN_IF_ABRUPT_COMPLETION(thread);
JSHandle<JSAsyncGeneratorObject> asyncGen = JSHandle<JSAsyncGeneratorObject>::Cast(obj);
asyncCtxt = JSHandle<JSTaggedValue>(thread, asyncGen->GetGeneratorContext(thread));
} else {
JSHandle<JSObject> obj = JSTaggedValue::ToObject(thread, asyncFuncObj);
RETURN_IF_ABRUPT_COMPLETION(thread);
JSHandle<JSAsyncFuncObject> asyncFun = JSHandle<JSAsyncFuncObject>::Cast(obj);
asyncCtxt = JSHandle<JSTaggedValue>(thread, asyncFun->GetGeneratorContext(thread));
}
JSHandle<GlobalEnv> env = vm->GetGlobalEnv();
JSHandle<JSTaggedValue> promiseValue =
builtins::BuiltinsPromiseHandler::PromiseResolve(thread,
JSHandle<JSTaggedValue>::Cast(env->GetPromiseFunction()),
value);
RETURN_IF_ABRUPT_COMPLETION(thread);
JSHandle<JSAsyncAwaitStatusFunction> fulFunc = factory->NewJSAsyncAwaitStatusFunction(
MethodIndex::BUILTINS_PROMISE_HANDLER_ASYNC_AWAIT_FULFILLED);
JSHandle<JSAsyncAwaitStatusFunction> rejFunc = factory->NewJSAsyncAwaitStatusFunction(
MethodIndex::BUILTINS_PROMISE_HANDLER_ASYNC_AWAIT_REJECTED);
fulFunc->SetAsyncContext(thread, asyncCtxt);
rejFunc->SetAsyncContext(thread, asyncCtxt);
#ifdef ENABLE_NEXT_OPTIMIZATION
if (thread->GetEcmaVM()->GetJsDebuggerManager()->IsAsyncStackTrace() && asyncFuncObj->IsAsyncFuncObject()) {
JSHandle<JSAsyncFuncObject> asyncFun(thread,
JSHandle<GeneratorContext>::Cast(asyncCtxt)->GetGeneratorObject(thread));
thread->GetEcmaVM()->GetAsyncStackTrace()->InsertAsyncTaskStacks(
JSHandle<JSPromise>(thread, asyncFun->GetPromise(thread)), "await");
}
JSHandle<JSObject> promise = JSHandle<JSObject>::Cast(promiseValue);
BuiltinsPromise::PerformPromiseThen(thread, JSHandle<JSPromise>::Cast(promise),
JSHandle<JSTaggedValue>::Cast(fulFunc),
JSHandle<JSTaggedValue>::Cast(rejFunc),
thread->GlobalConstants()->GetHandledUndefined());
#else
JSHandle<PromiseCapability> tcap =
JSPromise::NewPromiseCapability(thread, JSHandle<JSTaggedValue>::Cast(env->GetPromiseFunction()));
RETURN_IF_ABRUPT_COMPLETION(thread);
JSHandle<JSPromise>(thread, tcap->GetPromise(thread))->SetPromiseIsHandled(true);
if (thread->GetEcmaVM()->GetJsDebuggerManager()->IsAsyncStackTrace()) {
thread->GetEcmaVM()->GetAsyncStackTrace()->InsertAsyncTaskStacks(
JSHandle<JSPromise>(thread, tcap->GetPromise(thread)), "await");
}
JSHandle<JSObject> promise = JSHandle<JSObject>::Cast(promiseValue);
BuiltinsPromise::PerformPromiseThen(thread, JSHandle<JSPromise>::Cast(promise),
JSHandle<JSTaggedValue>::Cast(fulFunc),
JSHandle<JSTaggedValue>::Cast(rejFunc), tcap);
#endif
}
JSHandle<JSTaggedValue> JSAsyncAwaitStatusFunction::AsyncFunctionAwaitFulfilled(
JSThread *thread, const JSHandle<JSAsyncAwaitStatusFunction> &func, const JSHandle<JSTaggedValue> &value)
{
JSHandle<GeneratorContext> asyncCtxt(thread, func->GetAsyncContext(thread));
JSHandle<JSTaggedValue> tagVal(thread, asyncCtxt->GetGeneratorObject(thread));
if (tagVal->IsAsyncGeneratorObject()) {
AsyncGeneratorHelper::Next(thread, asyncCtxt, value.GetTaggedValue());
return JSHandle<JSTaggedValue>(thread, JSTaggedValue::Undefined());
} else {
GeneratorHelper::Next(thread, asyncCtxt, value.GetTaggedValue());
return JSHandle<JSTaggedValue>(thread, JSTaggedValue::Undefined());
}
}
JSHandle<JSTaggedValue> JSAsyncAwaitStatusFunction::AsyncFunctionAwaitRejected(
JSThread *thread, const JSHandle<JSAsyncAwaitStatusFunction> &func, const JSHandle<JSTaggedValue> &reason)
{
JSHandle<GeneratorContext> asyncCtxt(thread, func->GetAsyncContext(thread));
JSHandle<JSTaggedValue> tagVal(thread, asyncCtxt->GetGeneratorObject(thread));
if (tagVal->IsAsyncGeneratorObject()) {
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
JSHandle<CompletionRecord> completionRecord =
factory->NewCompletionRecord(CompletionRecordType::THROW, reason);
AsyncGeneratorHelper::Throw(thread, asyncCtxt, completionRecord);
thread->SetException(JSTaggedValue::Undefined());
return JSHandle<JSTaggedValue>(thread, JSTaggedValue::Undefined());
} else {
JSHandle<JSObject> result = GeneratorHelper::Throw(thread, asyncCtxt, reason.GetTaggedValue());
thread->SetException(result.GetTaggedValue());
return JSHandle<JSTaggedValue>::Cast(result);
}
}
}