/* * 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 { // es6 25.4.1.3.2 Promise Resolve Functions JSTaggedValue BuiltinsPromiseHandler::Resolve(EcmaRuntimeCallInfo *argv) { ASSERT(argv); BUILTINS_API_TRACE(argv->GetThread(), PromiseHandler, Resolve); JSThread *thread = argv->GetThread(); [[maybe_unused]] EcmaHandleScope handleScope(thread); ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); auto ecmaVm = thread->GetEcmaVM(); JSHandle env = ecmaVm->GetGlobalEnv(); // 1. Assert: F has a [[Promise]] internal slot whose value is an Object. JSHandle resolve = JSHandle::Cast(GetConstructor(argv)); ASSERT_PRINT(resolve->GetPromise().IsECMAObject(), "Resolve: promise must be js object"); // 2. Let promise be the value of F's [[Promise]] internal slot. // 3. Let alreadyResolved be the value of F's [[AlreadyResolved]] internal slot. // 4. If alreadyResolved.[[value]] is true, return undefined. // 5. Set alreadyResolved.[[value]] to true. JSHandle resolvePromise(thread, resolve->GetPromise()); JSHandle alreadyResolved(thread, resolve->GetAlreadyResolved()); if (alreadyResolved->GetValue().IsTrue()) { return JSTaggedValue::Undefined(); } alreadyResolved->SetValue(thread, JSTaggedValue::True()); // 6. If SameValue(resolution, promise) is true, then // a. Let selfResolutionError be a newly created TypeError object. // b. Return RejectPromise(promise, selfResolutionError). JSHandle resolution = BuiltinsBase::GetCallArg(argv, 0); if (JSTaggedValue::SameValue(resolution.GetTaggedValue(), resolvePromise.GetTaggedValue())) { JSHandle resolutionError = factory->GetJSError(ErrorType::TYPE_ERROR, "Resolve: The promise and resolution cannot be the same.", StackCheck::NO); JSPromise::RejectPromise(thread, resolvePromise, JSHandle::Cast(resolutionError)); return JSTaggedValue::Undefined(); } // 7. If Type(resolution) is not Object, then // a. Return FulfillPromise(promise, resolution). if (!resolution.GetTaggedValue().IsECMAObject()) { JSPromise::FulfillPromise(thread, resolvePromise, resolution); return JSTaggedValue::Undefined(); } // 8. Let then be Get(resolution, "then"). // 9. If then is an abrupt completion, then // a. Return RejectPromise(promise, then.[[value]]). JSHandle thenKey(thread->GlobalConstants()->GetHandledPromiseThenString()); JSHandle thenValue = JSObject::GetProperty(thread, resolution, thenKey).GetValue(); if (thread->HasPendingException()) { if (!thenValue->IsJSError()) { thenValue = JSHandle(thread, thread->GetException()); } thread->ClearException(); return JSPromise::RejectPromise(thread, resolvePromise, thenValue); } // 10. Let thenAction be then.[[value]]. // 11. If IsCallable(thenAction) is false, then // a. Return FulfillPromise(promise, resolution). if (!thenValue->IsCallable()) { JSPromise::FulfillPromise(thread, resolvePromise, resolution); return JSTaggedValue::Undefined(); } // 12. Perform EnqueueJob ("PromiseJobs", PromiseResolveThenableJob, «promise, resolution, thenAction») JSHandle arguments = factory->NewTaggedArray(3); // 3: 3 means three args stored in array arguments->Set(thread, 0, resolvePromise); arguments->Set(thread, 1, resolution); arguments->Set(thread, 2, thenValue); // 2: 2 means index of array is 2 JSHandle promiseResolveThenableJob(env->GetPromiseResolveThenableJob()); JSHandle job = thread->GetCurrentEcmaContext()->GetMicroJobQueue(); job::MicroJobQueue::EnqueueJob(thread, job, job::QueueType::QUEUE_PROMISE, promiseResolveThenableJob, arguments); // 13. Return undefined. return JSTaggedValue::Undefined(); } // es6 25.4.1.3.1 Promise Reject Functions JSTaggedValue BuiltinsPromiseHandler::Reject(EcmaRuntimeCallInfo *argv) { ASSERT(argv); BUILTINS_API_TRACE(argv->GetThread(), PromiseHandler, Reject); JSThread *thread = argv->GetThread(); [[maybe_unused]] EcmaHandleScope handleScope(thread); // 1. Assert: F has a [[Promise]] internal slot whose value is an Object. JSHandle reject = JSHandle::Cast(GetConstructor(argv)); ASSERT_PRINT(reject->GetPromise().IsECMAObject(), "Reject: promise must be js object"); // 2. Let promise be the value of F's [[Promise]] internal slot. // 3. Let alreadyResolved be the value of F's [[AlreadyResolved]] internal slot. // 4. If alreadyResolved.[[value]] is true, return undefined. // 5. Set alreadyResolved.[[value]] to true. JSHandle rejectPromise(thread, reject->GetPromise()); JSHandle alreadyResolved(thread, reject->GetAlreadyResolved()); if (alreadyResolved->GetValue().IsTrue()) { return JSTaggedValue::Undefined(); } alreadyResolved->SetValue(thread, JSTaggedValue::True()); // 6. Return RejectPromise(promise, reason). JSHandle reason = GetCallArg(argv, 0); JSHandle result(thread, JSPromise::RejectPromise(thread, rejectPromise, reason)); return result.GetTaggedValue(); } // es6 25.4.1.5.1 GetCapabilitiesExecutor Functions JSTaggedValue BuiltinsPromiseHandler::Executor(EcmaRuntimeCallInfo *argv) { ASSERT(argv); BUILTINS_API_TRACE(argv->GetThread(), PromiseHandler, Executor); JSThread *thread = argv->GetThread(); [[maybe_unused]] EcmaHandleScope handleScope(thread); // 1. Assert: F has a [[Capability]] internal slot whose value is a PromiseCapability Record. JSHandle executor = JSHandle::Cast(GetConstructor(argv)); ASSERT_PRINT(executor->GetCapability().IsRecord(), "Executor: F has a [[Capability]] internal slot whose value is a PromiseCapability Record."); // 2. Let promiseCapability be the value of F's [[Capability]] internal slot. // 3. If promiseCapability.[[Resolve]] is not undefined, throw a TypeError exception. JSHandle promiseCapability(thread, executor->GetCapability()); if (!promiseCapability->GetResolve().IsUndefined()) { THROW_TYPE_ERROR_AND_RETURN(thread, "Executor: resolve should be undefine!", JSTaggedValue::Undefined()); } // 4. If promiseCapability.[[Reject]] is not undefined, throw a TypeError exception. if (!promiseCapability->GetReject().IsUndefined()) { THROW_TYPE_ERROR_AND_RETURN(thread, "Executor: reject should be undefine!", JSTaggedValue::Undefined()); } // 5. Set promiseCapability.[[Resolve]] to resolve. // 6. Set promiseCapability.[[Reject]] to reject. JSHandle resolve = GetCallArg(argv, 0); JSHandle reject = GetCallArg(argv, 1); promiseCapability->SetResolve(thread, resolve); promiseCapability->SetReject(thread, reject); // 7. Return undefined. return JSTaggedValue::Undefined(); } // es6 25.4.4.1.2 Promise.all Resolve Element Functions 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 func = JSHandle::Cast(GetConstructor(argv)); // 1. Let alreadyCalled be the value of F's [[AlreadyCalled]] internal slot. JSHandle alreadyCalled = JSHandle::Cast(JSHandle(thread, func->GetAlreadyCalled())); // 2. If alreadyCalled.[[value]] is true, return undefined. if (alreadyCalled->GetValue().IsTrue()) { return JSTaggedValue::Undefined(); } // 3. Set alreadyCalled.[[value]] to true. alreadyCalled->SetValue(thread, JSTaggedValue::True()); // 4. Let index be the value of F's [[Index]] internal slot. JSHandle index(thread, func->GetIndex()); // 5. Let values be the value of F's [[Values]] internal slot. JSHandle values = JSHandle::Cast(JSHandle(thread, func->GetValues())); // 6. Let promiseCapability be the value of F's [[Capabilities]] internal slot. JSHandle capa = JSHandle::Cast(JSHandle(thread, func->GetCapabilities())); // 7. Let remainingElementsCount be the value of F's [[RemainingElements]] internal slot. JSHandle remainCnt = JSHandle::Cast(JSHandle(thread, func->GetRemainingElements())); // 8. Set values[index] to x. JSHandle arrayValues = JSHandle::Cast(JSHandle(thread, values->GetValue())); arrayValues->Set(thread, JSTaggedValue::ToUint32(thread, index), GetCallArg(argv, 0).GetTaggedValue()); // 9. Set remainingElementsCount.[[value]] to remainingElementsCount.[[value]] - 1. remainCnt->SetValue(thread, --JSTaggedNumber(remainCnt->GetValue())); // 10. If remainingElementsCount.[[value]] is 0, if (remainCnt->GetValue().IsZero()) { // a. Let valuesArray be CreateArrayFromList(values). JSHandle jsArrayValues = JSArray::CreateArrayFromList(thread, arrayValues); // b. Return Call(promiseCapability.[[Resolve]], undefined, «valuesArray»). JSHandle capaResolve(thread, capa->GetResolve()); JSHandle 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); } // 11. Return undefined. return JSTaggedValue::Undefined(); } JSTaggedValue BuiltinsPromiseHandler::AsyncAwaitFulfilled(EcmaRuntimeCallInfo *argv) { ASSERT(argv); BUILTINS_API_TRACE(argv->GetThread(), PromiseHandler, AsyncAwaitFulfilled); [[maybe_unused]] EcmaHandleScope handleScope(argv->GetThread()); JSHandle value = GetCallArg(argv, 0); JSHandle 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 reason = GetCallArg(argv, 0); JSHandle func(GetConstructor(argv)); return JSAsyncAwaitStatusFunction::AsyncFunctionAwaitRejected(argv->GetThread(), func, reason).GetTaggedValue(); } JSTaggedValue BuiltinsPromiseHandler::valueThunkFunction(EcmaRuntimeCallInfo *argv) { BUILTINS_API_TRACE(argv->GetThread(), PromiseHandler, valueThunkFunction); JSHandle valueThunk = JSHandle::Cast(GetConstructor(argv)); return valueThunk->GetResult(); } JSTaggedValue BuiltinsPromiseHandler::throwerFunction(EcmaRuntimeCallInfo *argv) { JSThread *thread = argv->GetThread(); BUILTINS_API_TRACE(thread, PromiseHandler, throwerFunction); JSHandle thrower = JSHandle::Cast(GetConstructor(argv)); JSTaggedValue undefined = thread->GlobalConstants()->GetUndefined(); THROW_NEW_ERROR_AND_RETURN_VALUE(thread, thrower->GetResult(), undefined); } JSTaggedValue BuiltinsPromiseHandler::ThenFinally(EcmaRuntimeCallInfo *argv) { // 1. Let F be the active function object. JSThread *thread = argv->GetThread(); BUILTINS_API_TRACE(thread, PromiseHandler, ThenFinally); auto ecmaVm = thread->GetEcmaVM(); ObjectFactory *factory = ecmaVm->GetFactory(); const GlobalEnvConstants *globalConst = thread->GlobalConstants(); JSHandle thenFinally(GetConstructor(argv)); JSHandle value = BuiltinsBase::GetCallArg(argv, 0); // 2. Let onFinally be F.[[OnFinally]]. // 3. Assert: IsCallable(onFinally) is true. JSHandle onFinally(thread, thenFinally->GetOnFinally()); ASSERT_PRINT(onFinally->IsCallable(), "onFinally is not callable"); // 4. Let result be ? Call(onFinally, undefined). JSHandle undefined = globalConst->GetHandledUndefined(); EcmaRuntimeCallInfo *taggedInfo = EcmaInterpreter::NewRuntimeCallInfo(thread, onFinally, undefined, undefined, 0); JSTaggedValue result = JSFunction::Call(taggedInfo); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); JSHandle resultHandle(thread, result); // 5. Let C be F.[[Constructor]]. // 6. Assert: IsConstructor(C) is true. JSHandle thenFinallyConstructor(thread, thenFinally->GetConstructor()); ASSERT_PRINT(thenFinallyConstructor->IsConstructor(), "thenFinallyConstructor is not constructor"); // 7. Let promise be ? PromiseResolve(C, result). JSHandle promiseHandle = BuiltinsPromiseHandler::PromiseResolve(thread, thenFinallyConstructor, resultHandle); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); // 8. Let valueThunk be equivalent to a function that returns value. JSHandle valueThunk = factory->NewJSPromiseValueThunkFunction(); valueThunk->SetResult(thread, value); JSHandle thenKey(globalConst->GetHandledPromiseThenString()); EcmaRuntimeCallInfo *invokeInfo = EcmaInterpreter::NewRuntimeCallInfo(thread, undefined, promiseHandle, undefined, 1); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); invokeInfo->SetCallArg(valueThunk.GetTaggedValue()); // 9. Return ? Invoke(promise, "then", « valueThunk »). return JSFunction::Invoke(invokeInfo, thenKey); } JSTaggedValue BuiltinsPromiseHandler::CatchFinally(EcmaRuntimeCallInfo *argv) { // 1. Let F be the active function object. JSThread *thread = argv->GetThread(); BUILTINS_API_TRACE(thread, PromiseHandler, CatchFinally); auto ecmaVm = thread->GetEcmaVM(); ObjectFactory *factory = ecmaVm->GetFactory(); const GlobalEnvConstants *globalConst = thread->GlobalConstants(); JSHandle catchFinally(GetConstructor(argv)); // 2. Let onFinally be F.[[OnFinally]]. // 3. Assert: IsCallable(onFinally) is true. JSHandle onFinally(thread, catchFinally->GetOnFinally()); ASSERT_PRINT(onFinally->IsCallable(), "thenOnFinally is not callable"); // 4. Let result be ? Call(onFinally, undefined). JSHandle undefined = globalConst->GetHandledUndefined(); EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, onFinally, undefined, undefined, 0); JSTaggedValue result = JSFunction::Call(info); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); JSHandle resultHandle(thread, result); // 5. Let C be F.[[Constructor]]. // 6. Assert: IsConstructor(C) is true. JSHandle catchFinallyConstructor(thread, catchFinally->GetConstructor()); ASSERT_PRINT(catchFinallyConstructor->IsConstructor(), "catchFinallyConstructor is not constructor"); // 7. Let promise be ? PromiseResolve(C, result). JSHandle promiseHandle = BuiltinsPromiseHandler::PromiseResolve(thread, catchFinallyConstructor, resultHandle); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); // 8. Let thrower be equivalent to a function that throws reason. JSHandle reason = BuiltinsBase::GetCallArg(argv, 0); JSHandle thenKey(globalConst->GetHandledPromiseThenString()); JSHandle 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()); // 9. Return ? Invoke(promise, "then", « thrower »). return JSFunction::Invoke(invokeInfo, thenKey); } JSHandle BuiltinsPromiseHandler::PromiseResolve(JSThread *thread, const JSHandle &constructor, const JSHandle &xValue) { BUILTINS_API_TRACE(thread, PromiseHandler, PromiseResolve); const GlobalEnvConstants *globalConst = thread->GlobalConstants(); // 1. Assert: Type(C) is Object. ASSERT_PRINT(constructor->IsECMAObject(), "PromiseResolve : is not callable"); // 2. If IsPromise(x) is true, then if (xValue->IsJSPromise()) { // a. Let xConstructor be ? Get(x, "constructor"). JSHandle ctorKey(globalConst->GetHandledConstructorString()); JSHandle ctorValue = JSObject::GetProperty(thread, xValue, ctorKey).GetValue(); RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, ctorValue); // b. If SameValue(xConstructor, C) is true, return x. if (JSTaggedValue::SameValue(ctorValue, constructor)) { return xValue; } } // 3. Let promiseCapability be ? NewPromiseCapability(C). // 4. ReturnIfAbrupt(promiseCapability) JSHandle promiseCapability = JSPromise::NewPromiseCapability(thread, constructor); RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread); JSHandle promiseCapaHandle = JSHandle::Cast(promiseCapability); RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, promiseCapaHandle); // 6. Let resolveResult be Call(promiseCapability.[[Resolve]], undefined, «x»). // 7. ReturnIfAbrupt(resolveResult). JSHandle resolve(thread, promiseCapability->GetResolve()); JSHandle 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 resolveResultHandle(thread, resolveResult); RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, resolveResultHandle); // 8. Return promiseCapability.[[Promise]]. JSHandle promise(thread, promiseCapability->GetPromise()); return promise; } JSTaggedValue BuiltinsPromiseHandler::AllSettledResolveElementFunction(EcmaRuntimeCallInfo *argv) { // 1. Let F be the active function object. JSThread *thread = argv->GetThread(); BUILTINS_API_TRACE(thread, PromiseHandler, AllSettledResolveElementFunction); JSHandle env = thread->GetEcmaVM()->GetGlobalEnv(); const GlobalEnvConstants *globalConst = thread->GlobalConstants(); JSHandle resolveElement = JSHandle::Cast((GetConstructor(argv))); // 2. Let alreadyCalled be F.[[AlreadyCalled]]. JSHandle alreadyCalled = JSHandle::Cast(JSHandle(thread, resolveElement->GetAlreadyCalled())); // 3. If alreadyCalled.[[Value]] is true, return undefined. if (alreadyCalled->GetValue().IsTrue()) { return JSTaggedValue::Undefined(); } // 4. Set alreadyCalled.[[Value]] to true. alreadyCalled->SetValue(thread, JSTaggedValue::True()); // 5. Let index be F.[[Index]]. uint32_t index = resolveElement->GetIndex(); // 6. Let values be F.[[Values]]. JSHandle values = JSHandle::Cast(JSHandle(thread, resolveElement->GetValues())); // 7. Let promiseCapability be F.[[Capability]]. JSHandle capa = JSHandle::Cast(JSHandle(thread, resolveElement->GetCapability())); // 8. Let remainingElementsCount be F.[[RemainingElements]]. JSHandle remainCnt = JSHandle::Cast(JSHandle(thread, resolveElement->GetRemainingElements())); // 9. Let obj be ! OrdinaryObjectCreate(%Object.prototype%). JSHandle proto = env->GetObjectFunctionPrototype(); JSHandle obj = thread->GetEcmaVM()->GetFactory()->OrdinaryNewJSObjectCreate(proto); // 10. Perform ! CreateDataPropertyOrThrow(obj, "status", "fulfilled"). JSHandle statusKey = globalConst->GetHandledPromiseStatusString(); JSHandle fulfilledKey = globalConst->GetHandledPromiseFulfilledString(); JSObject::CreateDataPropertyOrThrow(thread, obj, statusKey, fulfilledKey); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); // 11. Perform ! CreateDataPropertyOrThrow(obj, "value", x). JSHandle valueKey = globalConst->GetHandledValueString(); JSHandle xValue = GetCallArg(argv, 0); JSObject::CreateDataPropertyOrThrow(thread, obj, valueKey, xValue); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); // 12. Set values[index] to obj. JSHandle arrayValues = JSHandle::Cast(JSHandle(thread, values->GetValue())); arrayValues->Set(thread, index, obj.GetTaggedValue()); // 13. Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1. remainCnt->SetValue(thread, --JSTaggedNumber(remainCnt->GetValue())); // 14. If remainingElementsCount.[[Value]] is 0, then if (remainCnt->GetValue().IsZero()) { // a. Let valuesArray be CreateArrayFromList(values). JSHandle jsArrayValues = JSArray::CreateArrayFromList(thread, arrayValues); // b. Return ? Call(promiseCapability.[[Resolve]], undefined, « valuesArray »). JSHandle capaResolve(thread, capa->GetResolve()); JSHandle 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); } // 15. Return undefined. return JSTaggedValue::Undefined(); } JSTaggedValue BuiltinsPromiseHandler::AllSettledRejectElementFunction(EcmaRuntimeCallInfo *argv) { // 1. Let F be the active function object. JSThread *thread = argv->GetThread(); BUILTINS_API_TRACE(thread, PromiseHandler, AllSettledRejectElementFunction); JSHandle env = thread->GetEcmaVM()->GetGlobalEnv(); const GlobalEnvConstants *globalConst = thread->GlobalConstants(); JSHandle rejectElement = JSHandle::Cast((GetConstructor(argv))); // 2. Let alreadyCalled be F.[[AlreadyCalled]]. JSHandle alreadyCalled = JSHandle::Cast(JSHandle(thread, rejectElement->GetAlreadyCalled())); // 3. If alreadyCalled.[[Value]] is true, return undefined. if (alreadyCalled->GetValue().IsTrue()) { return JSTaggedValue::Undefined(); } // 4. Set alreadyCalled.[[Value]] to true. alreadyCalled->SetValue(thread, JSTaggedValue::True()); // 5. Let index be F.[[Index]]. uint32_t index = rejectElement->GetIndex(); // 6. Let values be F.[[Values]]. JSHandle values = JSHandle::Cast(JSHandle(thread, rejectElement->GetValues())); // 7. Let promiseCapability be F.[[Capability]]. JSHandle capa = JSHandle::Cast(JSHandle(thread, rejectElement->GetCapability())); // 8. Let remainingElementsCount be F.[[RemainingElements]]. JSHandle remainCnt = JSHandle::Cast(JSHandle(thread, rejectElement->GetRemainingElements())); // 9. Let obj be ! OrdinaryObjectCreate(%Object.prototype%). JSHandle proto = env->GetObjectFunctionPrototype(); JSHandle obj = thread->GetEcmaVM()->GetFactory()->OrdinaryNewJSObjectCreate(proto); // 10. Perform ! CreateDataPropertyOrThrow(obj, "status", "rejected"). JSHandle statusKey = globalConst->GetHandledPromiseStatusString(); JSHandle rejectedKey = globalConst->GetHandledPromiseRejectedString(); JSObject::CreateDataPropertyOrThrow(thread, obj, statusKey, rejectedKey); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); // 11. Perform ! CreateDataPropertyOrThrow(obj, "reason", x). JSHandle xReason = GetCallArg(argv, 0); JSHandle reasonKey = globalConst->GetHandledPromiseReasonString(); JSObject::CreateDataPropertyOrThrow(thread, obj, reasonKey, xReason); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); // 12. Set values[index] to obj. JSHandle arrayValues = JSHandle::Cast(JSHandle(thread, values->GetValue())); arrayValues->Set(thread, index, obj.GetTaggedValue()); // 13. Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1. remainCnt->SetValue(thread, --JSTaggedNumber(remainCnt->GetValue())); // 14. If remainingElementsCount.[[Value]] is 0, then if (remainCnt->GetValue().IsZero()) { // a. Let valuesArray be CreateArrayFromList(values). JSHandle jsArrayValues = JSArray::CreateArrayFromList(thread, arrayValues); // b. Return ? Call(promiseCapability.[[Resolve]], undefined, « valuesArray »). JSHandle capaResolve(thread, capa->GetResolve()); JSHandle 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); } // 15. Return undefined. return JSTaggedValue::Undefined(); } JSTaggedValue BuiltinsPromiseHandler::AnyRejectElementFunction(EcmaRuntimeCallInfo *argv) { // 1. Let F be the active function object. JSThread *thread = argv->GetThread(); BUILTINS_API_TRACE(thread, PromiseHandler, AnyRejectElementFunction); ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); const GlobalEnvConstants *globalConst = thread->GlobalConstants(); JSHandle rejectElement = JSHandle::Cast((GetConstructor(argv))); // 2. If F.[[AlreadyCalled]] is true, return undefined. JSTaggedValue alreadyCalled = rejectElement->GetAlreadyCalled(); if (alreadyCalled.IsTrue()) { return JSTaggedValue::Undefined(); } // 3. Set F.[[AlreadyCalled]] to true. rejectElement->SetAlreadyCalled(thread, JSTaggedValue::True()); // 4. Let index be F.[[Index]]. uint32_t index = rejectElement->GetIndex(); // 5. Let errors be F.[[Errors]]. JSHandle errors = JSHandle::Cast(JSHandle(thread, rejectElement->GetErrors())); // 6. Let promiseCapability be F.[[Capability]]. JSHandle capa = JSHandle::Cast(JSHandle(thread, rejectElement->GetCapability())); // 7. Let remainingElementsCount be F.[[RemainingElements]]. JSHandle remainCnt = JSHandle::Cast(JSHandle(thread, rejectElement->GetRemainingElements())); // 8. Set errors[index] to x. JSHandle xValue = GetCallArg(argv, 0); JSHandle errorsArray = JSHandle::Cast(JSHandle(thread, errors->GetValue())); errorsArray->Set(thread, index, xValue.GetTaggedValue()); // 9. Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1. remainCnt->SetValue(thread, --JSTaggedNumber(remainCnt->GetValue())); // 10. If remainingElementsCount.[[Value]] is 0, then if (remainCnt->GetValue().IsZero()) { // a. Let error be a newly created AggregateError object. JSHandle error = factory->NewJSAggregateError(); // b. Perform ! DefinePropertyOrThrow(error, "errors", PropertyDescriptor { [[Configurable]]: true, // [[Enumerable]]: false, [[Writable]]: true, [[Value]]: ! CreateArrayFromList(errors) }). JSHandle errorsKey(thread, globalConst->GetErrorsString()); JSHandle errorsValue(JSArray::CreateArrayFromList(thread, errorsArray)); PropertyDescriptor msgDesc(thread, errorsValue, true, false, true); JSHandle errorTagged = JSHandle::Cast(error); JSTaggedValue::DefinePropertyOrThrow(thread, errorTagged, errorsKey, msgDesc); RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); // c. Return ? Call(promiseCapability.[[Reject]], undefined, « error »). JSHandle capaReject(thread, capa->GetReject()); JSHandle 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); } // 11. Return undefined. return JSTaggedValue::Undefined(); } } // namespace panda::ecmascript::builtins