* Copyright (c) 2021-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/builtins/builtins_promise_handler.h"
#include "ecmascript/global_env.h"
#include "ecmascript/interpreter/interpreter.h"
#include "ecmascript/jobs/micro_job_queue.h"
#include "ecmascript/js_array.h"
#include "ecmascript/js_async_function.h"
#include "ecmascript/js_promise.h"
namespace panda::ecmascript::builtins {
JSTaggedValue BuiltinsPromiseHandler::Resolve(EcmaRuntimeCallInfo *argv)
{
ASSERT(argv);
BUILTINS_API_TRACE(argv->GetThread(), PromiseHandler, Resolve);
JSThread *thread = argv->GetThread();
[[maybe_unused]] EcmaHandleScope handleScope(thread);
JSHandle<JSPromiseReactionsFunction> resolve = JSHandle<JSPromiseReactionsFunction>::Cast(GetConstructor(argv));
ASSERT_PRINT(resolve->GetPromise(thread).IsECMAObject(), "Resolve: promise must be js object");
JSHandle<PromiseRecord> alreadyResolved(thread, resolve->GetAlreadyResolved(thread));
if (alreadyResolved->GetValue(thread).IsTrue()) {
return JSTaggedValue::Undefined();
}
alreadyResolved->SetValue(thread, JSTaggedValue::True());
JSHandle<JSPromise> resolvePromise(thread, resolve->GetPromise(thread));
JSHandle<JSTaggedValue> resolution = BuiltinsBase::GetCallArg(argv, 0);
return InnerResolve(thread, resolvePromise, resolution);
}
JSTaggedValue BuiltinsPromiseHandler::Reject(EcmaRuntimeCallInfo *argv)
{
ASSERT(argv);
BUILTINS_API_TRACE(argv->GetThread(), PromiseHandler, Reject);
JSThread *thread = argv->GetThread();
[[maybe_unused]] EcmaHandleScope handleScope(thread);
JSHandle<JSPromiseReactionsFunction> reject = JSHandle<JSPromiseReactionsFunction>::Cast(GetConstructor(argv));
ASSERT_PRINT(reject->GetPromise(thread).IsECMAObject(), "Reject: promise must be js object");
JSHandle<JSPromise> rejectPromise(thread, reject->GetPromise(thread));
JSHandle<PromiseRecord> alreadyResolved(thread, reject->GetAlreadyResolved(thread));
if (alreadyResolved->GetValue(thread).IsTrue()) {
return JSTaggedValue::Undefined();
}
alreadyResolved->SetValue(thread, JSTaggedValue::True());
JSHandle<JSTaggedValue> reason = GetCallArg(argv, 0);
JSHandle<JSTaggedValue> result(thread, JSPromise::RejectPromise(thread, rejectPromise, reason));
return result.GetTaggedValue();
}
JSTaggedValue BuiltinsPromiseHandler::Executor(EcmaRuntimeCallInfo *argv)
{
ASSERT(argv);
BUILTINS_API_TRACE(argv->GetThread(), PromiseHandler, Executor);
JSThread *thread = argv->GetThread();
[[maybe_unused]] EcmaHandleScope handleScope(thread);
JSHandle<JSPromiseExecutorFunction> executor = JSHandle<JSPromiseExecutorFunction>::Cast(GetConstructor(argv));
ASSERT_PRINT(executor->GetCapability(thread).IsRecord(),
"Executor: F has a [[Capability]] internal slot whose value is a PromiseCapability Record.");
JSHandle<PromiseCapability> promiseCapability(thread, executor->GetCapability(thread));
if (!promiseCapability->GetResolve(thread).IsUndefined()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "Executor: resolve should be undefine!", JSTaggedValue::Undefined());
}
if (!promiseCapability->GetReject(thread).IsUndefined()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "Executor: reject should be undefine!", JSTaggedValue::Undefined());
}
JSHandle<JSTaggedValue> resolve = GetCallArg(argv, 0);
JSHandle<JSTaggedValue> reject = GetCallArg(argv, 1);
promiseCapability->SetResolve(thread, resolve);
promiseCapability->SetReject(thread, reject);
return JSTaggedValue::Undefined();
}
JSTaggedValue BuiltinsPromiseHandler::ResolveElementFunction(EcmaRuntimeCallInfo *argv)
{
ASSERT(argv);
BUILTINS_API_TRACE(argv->GetThread(), PromiseHandler, ResolveElementFunction);
JSThread *thread = argv->GetThread();
[[maybe_unused]] EcmaHandleScope handleScope(thread);
const GlobalEnvConstants *globalConst = thread->GlobalConstants();
JSHandle<JSPromiseAllResolveElementFunction> func =
JSHandle<JSPromiseAllResolveElementFunction>::Cast(GetConstructor(argv));
JSHandle<PromiseRecord> alreadyCalled =
JSHandle<PromiseRecord>::Cast(JSHandle<JSTaggedValue>(thread, func->GetAlreadyCalled(thread)));
if (alreadyCalled->GetValue(thread).IsTrue()) {
return JSTaggedValue::Undefined();
}
alreadyCalled->SetValue(thread, JSTaggedValue::True());
JSHandle<JSTaggedValue> index(thread, func->GetIndex(thread));
JSHandle<PromiseRecord> values =
JSHandle<PromiseRecord>::Cast(JSHandle<JSTaggedValue>(thread, func->GetValues(thread)));
JSHandle<PromiseCapability> capa =
JSHandle<PromiseCapability>::Cast(JSHandle<JSTaggedValue>(thread, func->GetCapabilities(thread)));
JSHandle<PromiseRecord> remainCnt =
JSHandle<PromiseRecord>::Cast(JSHandle<JSTaggedValue>(thread, func->GetRemainingElements(thread)));
JSHandle<TaggedArray> arrayValues =
JSHandle<TaggedArray>::Cast(JSHandle<JSTaggedValue>(thread, values->GetValue(thread)));
arrayValues->Set(thread, JSTaggedValue::ToUint32(thread, index), GetCallArg(argv, 0).GetTaggedValue());
remainCnt->SetValue(thread, --JSTaggedNumber(remainCnt->GetValue(thread)));
if (remainCnt->GetValue(thread).IsZero()) {
JSHandle<JSArray> jsArrayValues = JSArray::CreateArrayFromList(thread, arrayValues);
JSHandle<JSTaggedValue> capaResolve(thread, capa->GetResolve(thread));
JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
EcmaRuntimeCallInfo *info =
EcmaInterpreter::NewRuntimeCallInfo(thread, capaResolve, undefined, undefined, 1);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
info->SetCallArg(jsArrayValues.GetTaggedValue());
return JSFunction::Call(info);
}
return JSTaggedValue::Undefined();
}
JSTaggedValue BuiltinsPromiseHandler::AsyncAwaitFulfilled(EcmaRuntimeCallInfo *argv)
{
ASSERT(argv);
BUILTINS_API_TRACE(argv->GetThread(), PromiseHandler, AsyncAwaitFulfilled);
[[maybe_unused]] EcmaHandleScope handleScope(argv->GetThread());
JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
JSHandle<JSAsyncAwaitStatusFunction> func(GetConstructor(argv));
return JSAsyncAwaitStatusFunction::AsyncFunctionAwaitFulfilled(argv->GetThread(), func, value).GetTaggedValue();
}
JSTaggedValue BuiltinsPromiseHandler::AsyncAwaitRejected(EcmaRuntimeCallInfo *argv)
{
ASSERT(argv);
BUILTINS_API_TRACE(argv->GetThread(), PromiseHandler, AsyncAwaitRejected);
[[maybe_unused]] EcmaHandleScope handleScope(argv->GetThread());
JSHandle<JSTaggedValue> reason = GetCallArg(argv, 0);
JSHandle<JSAsyncAwaitStatusFunction> func(GetConstructor(argv));
return JSAsyncAwaitStatusFunction::AsyncFunctionAwaitRejected(argv->GetThread(), func, reason).GetTaggedValue();
}
JSTaggedValue BuiltinsPromiseHandler::valueThunkFunction(EcmaRuntimeCallInfo *argv)
{
BUILTINS_API_TRACE(argv->GetThread(), PromiseHandler, valueThunkFunction);
JSHandle<JSPromiseValueThunkOrThrowerFunction> valueThunk =
JSHandle<JSPromiseValueThunkOrThrowerFunction>::Cast(GetConstructor(argv));
return valueThunk->GetResult(argv->GetThread());
}
JSTaggedValue BuiltinsPromiseHandler::throwerFunction(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, PromiseHandler, throwerFunction);
JSHandle<JSPromiseValueThunkOrThrowerFunction> thrower =
JSHandle<JSPromiseValueThunkOrThrowerFunction>::Cast(GetConstructor(argv));
JSTaggedValue undefined = thread->GlobalConstants()->GetUndefined();
THROW_NEW_ERROR_AND_RETURN_VALUE(thread, thrower->GetResult(thread), undefined);
}
JSTaggedValue BuiltinsPromiseHandler::ThenFinally(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, PromiseHandler, ThenFinally);
auto ecmaVm = thread->GetEcmaVM();
ObjectFactory *factory = ecmaVm->GetFactory();
const GlobalEnvConstants *globalConst = thread->GlobalConstants();
JSHandle<JSPromiseFinallyFunction> thenFinally(GetConstructor(argv));
JSHandle<JSTaggedValue> value = BuiltinsBase::GetCallArg(argv, 0);
JSHandle<JSTaggedValue> onFinally(thread, thenFinally->GetOnFinally(thread));
ASSERT_PRINT(onFinally->IsCallable(), "onFinally is not callable");
JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
EcmaRuntimeCallInfo *taggedInfo =
EcmaInterpreter::NewRuntimeCallInfo(thread, onFinally, undefined, undefined, 0);
JSTaggedValue result = JSFunction::Call(taggedInfo);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
JSHandle<JSTaggedValue> resultHandle(thread, result);
JSHandle<JSTaggedValue> thenFinallyConstructor(thread, thenFinally->GetConstructor(thread));
ASSERT_PRINT(thenFinallyConstructor->IsConstructor(), "thenFinallyConstructor is not constructor");
JSHandle<JSTaggedValue> promiseHandle =
BuiltinsPromiseHandler::PromiseResolve(thread, thenFinallyConstructor, resultHandle);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
JSHandle<JSPromiseValueThunkOrThrowerFunction> valueThunk =
factory->NewJSPromiseValueThunkFunction();
valueThunk->SetResult(thread, value);
JSHandle<JSTaggedValue> thenKey(globalConst->GetHandledPromiseThenString());
EcmaRuntimeCallInfo *invokeInfo =
EcmaInterpreter::NewRuntimeCallInfo(thread, undefined, promiseHandle, undefined, 1);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
invokeInfo->SetCallArg(valueThunk.GetTaggedValue());
return JSFunction::Invoke(invokeInfo, thenKey);
}
JSTaggedValue BuiltinsPromiseHandler::CatchFinally(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, PromiseHandler, CatchFinally);
auto ecmaVm = thread->GetEcmaVM();
ObjectFactory *factory = ecmaVm->GetFactory();
const GlobalEnvConstants *globalConst = thread->GlobalConstants();
JSHandle<JSPromiseFinallyFunction> catchFinally(GetConstructor(argv));
JSHandle<JSTaggedValue> onFinally(thread, catchFinally->GetOnFinally(thread));
ASSERT_PRINT(onFinally->IsCallable(), "thenOnFinally is not callable");
JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
EcmaRuntimeCallInfo *info =
EcmaInterpreter::NewRuntimeCallInfo(thread, onFinally, undefined, undefined, 0);
JSTaggedValue result = JSFunction::Call(info);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
JSHandle<JSTaggedValue> resultHandle(thread, result);
JSHandle<JSTaggedValue> catchFinallyConstructor(thread, catchFinally->GetConstructor(thread));
ASSERT_PRINT(catchFinallyConstructor->IsConstructor(), "catchFinallyConstructor is not constructor");
JSHandle<JSTaggedValue> promiseHandle =
BuiltinsPromiseHandler::PromiseResolve(thread, catchFinallyConstructor, resultHandle);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
JSHandle<JSTaggedValue> reason = BuiltinsBase::GetCallArg(argv, 0);
JSHandle<JSTaggedValue> thenKey(globalConst->GetHandledPromiseThenString());
JSHandle<JSPromiseValueThunkOrThrowerFunction> thrower =
factory->NewJSPromiseThrowerFunction();
thrower->SetResult(thread, reason);
EcmaRuntimeCallInfo *invokeInfo =
EcmaInterpreter::NewRuntimeCallInfo(thread, undefined, promiseHandle, undefined, 1);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
invokeInfo->SetCallArg(thrower.GetTaggedValue());
return JSFunction::Invoke(invokeInfo, thenKey);
}
JSHandle<JSTaggedValue> BuiltinsPromiseHandler::PromiseResolve(JSThread *thread,
const JSHandle<JSTaggedValue> &constructor,
const JSHandle<JSTaggedValue> &xValue)
{
BUILTINS_API_TRACE(thread, PromiseHandler, PromiseResolve);
const GlobalEnvConstants *globalConst = thread->GlobalConstants();
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
ASSERT_PRINT(constructor->IsECMAObject(), "PromiseResolve : is not callable");
if (xValue->IsJSPromise()) {
JSHandle<JSTaggedValue> ctorKey(globalConst->GetHandledConstructorString());
JSHandle<JSTaggedValue> ctorValue = JSObject::GetProperty(thread, xValue, ctorKey).GetValue();
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, ctorValue);
if (JSTaggedValue::SameValue(thread, ctorValue, constructor)) {
return xValue;
}
}
if (constructor == env->GetPromiseFunction()) {
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
JSHandle<JSPromise> promise = factory->NewJSPromise();
InnerResolve(thread, promise, xValue);
return JSHandle<JSTaggedValue>(thread, promise.GetTaggedValue());
}
JSHandle<PromiseCapability> promiseCapability = JSPromise::NewPromiseCapability(thread, constructor);
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
JSHandle<JSTaggedValue> promiseCapaHandle = JSHandle<JSTaggedValue>::Cast(promiseCapability);
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, promiseCapaHandle);
JSHandle<JSTaggedValue> resolve(thread, promiseCapability->GetResolve(thread));
JSHandle<JSTaggedValue> undefined(globalConst->GetHandledUndefined());
EcmaRuntimeCallInfo *info =
EcmaInterpreter::NewRuntimeCallInfo(thread, resolve, undefined, undefined, 1);
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, promiseCapaHandle);
info->SetCallArg(xValue.GetTaggedValue());
JSTaggedValue resolveResult = JSFunction::Call(info);
JSHandle<JSTaggedValue> resolveResultHandle(thread, resolveResult);
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, resolveResultHandle);
JSHandle<JSTaggedValue> promise(thread, promiseCapability->GetPromise(thread));
return promise;
}
JSTaggedValue BuiltinsPromiseHandler::AllSettledResolveElementFunction(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, PromiseHandler, AllSettledResolveElementFunction);
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
const GlobalEnvConstants *globalConst = thread->GlobalConstants();
JSHandle<JSPromiseAllSettledElementFunction> resolveElement =
JSHandle<JSPromiseAllSettledElementFunction>::Cast((GetConstructor(argv)));
JSHandle<PromiseRecord> alreadyCalled =
JSHandle<PromiseRecord>::Cast(JSHandle<JSTaggedValue>(thread, resolveElement->GetAlreadyCalled(thread)));
if (alreadyCalled->GetValue(thread).IsTrue()) {
return JSTaggedValue::Undefined();
}
alreadyCalled->SetValue(thread, JSTaggedValue::True());
uint32_t index = resolveElement->GetIndex();
JSHandle<PromiseRecord> values =
JSHandle<PromiseRecord>::Cast(JSHandle<JSTaggedValue>(thread, resolveElement->GetValues(thread)));
JSHandle<PromiseCapability> capa =
JSHandle<PromiseCapability>::Cast(JSHandle<JSTaggedValue>(thread, resolveElement->GetCapability(thread)));
JSHandle<PromiseRecord> remainCnt =
JSHandle<PromiseRecord>::Cast(JSHandle<JSTaggedValue>(thread, resolveElement->GetRemainingElements(thread)));
JSHandle<JSTaggedValue> proto = env->GetObjectFunctionPrototype();
JSHandle<JSObject> obj = thread->GetEcmaVM()->GetFactory()->OrdinaryNewJSObjectCreate(proto);
JSHandle<JSTaggedValue> statusKey = globalConst->GetHandledPromiseStatusString();
JSHandle<JSTaggedValue> fulfilledKey = globalConst->GetHandledPromiseFulfilledString();
JSObject::CreateDataPropertyOrThrow(thread, obj, statusKey, fulfilledKey);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
JSHandle<JSTaggedValue> valueKey = globalConst->GetHandledValueString();
JSHandle<JSTaggedValue> xValue = GetCallArg(argv, 0);
JSObject::CreateDataPropertyOrThrow(thread, obj, valueKey, xValue);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
JSHandle<TaggedArray> arrayValues =
JSHandle<TaggedArray>::Cast(JSHandle<JSTaggedValue>(thread, values->GetValue(thread)));
arrayValues->Set(thread, index, obj.GetTaggedValue());
remainCnt->SetValue(thread, --JSTaggedNumber(remainCnt->GetValue(thread)));
if (remainCnt->GetValue(thread).IsZero()) {
JSHandle<JSArray> jsArrayValues = JSArray::CreateArrayFromList(thread, arrayValues);
JSHandle<JSTaggedValue> capaResolve(thread, capa->GetResolve(thread));
JSHandle<JSTaggedValue> undefined(globalConst->GetHandledUndefined());
EcmaRuntimeCallInfo *info =
EcmaInterpreter::NewRuntimeCallInfo(thread, capaResolve, undefined, undefined, 1);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
info->SetCallArg(jsArrayValues.GetTaggedValue());
return JSFunction::Call(info);
}
return JSTaggedValue::Undefined();
}
JSTaggedValue BuiltinsPromiseHandler::AllSettledRejectElementFunction(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, PromiseHandler, AllSettledRejectElementFunction);
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
const GlobalEnvConstants *globalConst = thread->GlobalConstants();
JSHandle<JSPromiseAllSettledElementFunction> rejectElement =
JSHandle<JSPromiseAllSettledElementFunction>::Cast((GetConstructor(argv)));
JSHandle<PromiseRecord> alreadyCalled =
JSHandle<PromiseRecord>::Cast(JSHandle<JSTaggedValue>(thread, rejectElement->GetAlreadyCalled(thread)));
if (alreadyCalled->GetValue(thread).IsTrue()) {
return JSTaggedValue::Undefined();
}
alreadyCalled->SetValue(thread, JSTaggedValue::True());
uint32_t index = rejectElement->GetIndex();
JSHandle<PromiseRecord> values =
JSHandle<PromiseRecord>::Cast(JSHandle<JSTaggedValue>(thread, rejectElement->GetValues(thread)));
JSHandle<PromiseCapability> capa =
JSHandle<PromiseCapability>::Cast(JSHandle<JSTaggedValue>(thread, rejectElement->GetCapability(thread)));
JSHandle<PromiseRecord> remainCnt =
JSHandle<PromiseRecord>::Cast(JSHandle<JSTaggedValue>(thread, rejectElement->GetRemainingElements(thread)));
JSHandle<JSTaggedValue> proto = env->GetObjectFunctionPrototype();
JSHandle<JSObject> obj = thread->GetEcmaVM()->GetFactory()->OrdinaryNewJSObjectCreate(proto);
JSHandle<JSTaggedValue> statusKey = globalConst->GetHandledPromiseStatusString();
JSHandle<JSTaggedValue> rejectedKey = globalConst->GetHandledPromiseRejectedString();
JSObject::CreateDataPropertyOrThrow(thread, obj, statusKey, rejectedKey);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
JSHandle<JSTaggedValue> xReason = GetCallArg(argv, 0);
JSHandle<JSTaggedValue> reasonKey = globalConst->GetHandledPromiseReasonString();
JSObject::CreateDataPropertyOrThrow(thread, obj, reasonKey, xReason);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
JSHandle<TaggedArray> arrayValues =
JSHandle<TaggedArray>::Cast(JSHandle<JSTaggedValue>(thread, values->GetValue(thread)));
arrayValues->Set(thread, index, obj.GetTaggedValue());
remainCnt->SetValue(thread, --JSTaggedNumber(remainCnt->GetValue(thread)));
if (remainCnt->GetValue(thread).IsZero()) {
JSHandle<JSArray> jsArrayValues = JSArray::CreateArrayFromList(thread, arrayValues);
JSHandle<JSTaggedValue> capaResolve(thread, capa->GetResolve(thread));
JSHandle<JSTaggedValue> undefined(globalConst->GetHandledUndefined());
EcmaRuntimeCallInfo *info =
EcmaInterpreter::NewRuntimeCallInfo(thread, capaResolve, undefined, undefined, 1);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
info->SetCallArg(jsArrayValues.GetTaggedValue());
return JSFunction::Call(info);
}
return JSTaggedValue::Undefined();
}
JSTaggedValue BuiltinsPromiseHandler::AnyRejectElementFunction(EcmaRuntimeCallInfo *argv)
{
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, PromiseHandler, AnyRejectElementFunction);
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
const GlobalEnvConstants *globalConst = thread->GlobalConstants();
JSHandle<JSPromiseAnyRejectElementFunction> rejectElement =
JSHandle<JSPromiseAnyRejectElementFunction>::Cast((GetConstructor(argv)));
JSTaggedValue alreadyCalled = rejectElement->GetAlreadyCalled(thread);
if (alreadyCalled.IsTrue()) {
return JSTaggedValue::Undefined();
}
rejectElement->SetAlreadyCalled(thread, JSTaggedValue::True());
uint32_t index = rejectElement->GetIndex();
JSHandle<PromiseRecord> errors =
JSHandle<PromiseRecord>::Cast(JSHandle<JSTaggedValue>(thread, rejectElement->GetErrors(thread)));
JSHandle<PromiseCapability> capa =
JSHandle<PromiseCapability>::Cast(JSHandle<JSTaggedValue>(thread, rejectElement->GetCapability(thread)));
JSHandle<PromiseRecord> remainCnt =
JSHandle<PromiseRecord>::Cast(JSHandle<JSTaggedValue>(thread, rejectElement->GetRemainingElements(thread)));
JSHandle<JSTaggedValue> xValue = GetCallArg(argv, 0);
JSHandle<TaggedArray> errorsArray =
JSHandle<TaggedArray>::Cast(JSHandle<JSTaggedValue>(thread, errors->GetValue(thread)));
errorsArray->Set(thread, index, xValue.GetTaggedValue());
remainCnt->SetValue(thread, --JSTaggedNumber(remainCnt->GetValue(thread)));
if (remainCnt->GetValue(thread).IsZero()) {
JSHandle<JSObject> error = factory->NewJSAggregateError();
JSHandle<JSTaggedValue> errorsKey(thread, globalConst->GetErrorsString());
JSHandle<JSTaggedValue> errorsValue(JSArray::CreateArrayFromList(thread, errorsArray));
PropertyDescriptor msgDesc(thread, errorsValue, true, false, true);
JSHandle<JSTaggedValue> errorTagged = JSHandle<JSTaggedValue>::Cast(error);
JSTaggedValue::DefinePropertyOrThrow(thread, errorTagged, errorsKey, msgDesc);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
JSHandle<JSTaggedValue> capaReject(thread, capa->GetReject(thread));
JSHandle<JSTaggedValue> undefined(globalConst->GetHandledUndefined());
EcmaRuntimeCallInfo *info =
EcmaInterpreter::NewRuntimeCallInfo(thread, capaReject, undefined, undefined, 1);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
info->SetCallArg(error.GetTaggedValue());
return JSFunction::Call(info);
}
return JSTaggedValue::Undefined();
}
JSTaggedValue BuiltinsPromiseHandler::InnerResolve(JSThread *thread, const JSHandle<JSPromise> &promise,
const JSHandle<JSTaggedValue> &resolution)
{
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
auto ecmaVm = thread->GetEcmaVM();
JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
if (JSTaggedValue::SameValue(thread, resolution.GetTaggedValue(), promise.GetTaggedValue())) {
JSHandle<JSObject> resolutionError = factory->GetJSError(ErrorType::TYPE_ERROR,
"Resolve: The promise and resolution cannot be the same.", StackCheck::NO);
JSPromise::RejectPromise(thread, promise, JSHandle<JSTaggedValue>::Cast(resolutionError));
return JSTaggedValue::Undefined();
}
if (!resolution.GetTaggedValue().IsECMAObject()) {
JSPromise::FulfillPromise(thread, promise, resolution);
return JSTaggedValue::Undefined();
}
JSHandle<JSTaggedValue> thenKey(thread->GlobalConstants()->GetHandledPromiseThenString());
JSHandle<JSTaggedValue> thenValue = JSObject::GetProperty(thread, resolution, thenKey).GetValue();
if (thread->HasPendingException()) {
if (!thenValue->IsJSError()) {
thenValue = JSHandle<JSTaggedValue>(thread, thread->GetException());
}
thread->ClearExceptionAndExtraErrorMessage();
return JSPromise::RejectPromise(thread, promise, thenValue);
}
if (!thenValue->IsCallable()) {
JSPromise::FulfillPromise(thread, promise, resolution);
return JSTaggedValue::Undefined();
}
JSHandle<TaggedArray> arguments = factory->NewTaggedArray(3);
arguments->Set(thread, 0, promise);
arguments->Set(thread, 1, resolution);
arguments->Set(thread, 2, thenValue);
JSHandle<JSFunction> promiseResolveThenableJob(env->GetPromiseResolveThenableJob());
JSHandle<job::MicroJobQueue> job = thread->GetEcmaVM()->GetMicroJobQueue();
job::MicroJobQueue::EnqueueJob(thread, job, job::QueueType::QUEUE_PROMISE, promiseResolveThenableJob, arguments);
return JSTaggedValue::Undefined();
}
}