14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_BUILTINS_BUILTINS_PROMISE_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_BUILTINS_BUILTINS_PROMISE_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/base/builtins_base.h"
204514f5e3Sopenharmony_ci#include "ecmascript/ecma_runtime_call_info.h"
214514f5e3Sopenharmony_ci#include "ecmascript/js_handle.h"
224514f5e3Sopenharmony_ci#include "ecmascript/js_promise.h"
234514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h"
244514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h"
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_ci// List of functions in Promise, excluding the '@@' propertiex.
274514f5e3Sopenharmony_ci// V(name, func, length, stubIndex)
284514f5e3Sopenharmony_ci// where BuiltinsPromise::func refers to the native implementation of Promise[name].
294514f5e3Sopenharmony_ci//       kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available.
304514f5e3Sopenharmony_ci#define BUILTIN_PROMISE_FUNCTIONS(V)                \
314514f5e3Sopenharmony_ci    /* Promise.all ( iterable ) */                  \
324514f5e3Sopenharmony_ci    V("all",        All,        1, INVALID)         \
334514f5e3Sopenharmony_ci    /* Promise.allSettled ( iterable ) */           \
344514f5e3Sopenharmony_ci    V("allSettled", AllSettled, 1, INVALID)         \
354514f5e3Sopenharmony_ci    /* Promise.any ( iterable ) */                  \
364514f5e3Sopenharmony_ci    V("any",        Any,        1, INVALID)         \
374514f5e3Sopenharmony_ci    /* Promise.race ( iterable ) */                 \
384514f5e3Sopenharmony_ci    V("race",       Race,       1, INVALID)         \
394514f5e3Sopenharmony_ci    /* Promise.reject ( r ) */                      \
404514f5e3Sopenharmony_ci    V("reject",     Reject,     1, INVALID)         \
414514f5e3Sopenharmony_ci    /* Promise.resolve ( x ) */                     \
424514f5e3Sopenharmony_ci    V("resolve",    Resolve,    1, INVALID)
434514f5e3Sopenharmony_ci
444514f5e3Sopenharmony_ci// List of functions in Promise.prototype, excluding the constructor and '@@' properties.
454514f5e3Sopenharmony_ci// V(name, func, length, stubIndex)
464514f5e3Sopenharmony_ci// where BuiltinsPromise::func refers to the native implementation of Promise.prototype[name].
474514f5e3Sopenharmony_ci#define BUILTIN_PROMISE_PROTOTYPE_FUNCTIONS(V)                  \
484514f5e3Sopenharmony_ci    /* Promise.prototype.catch ( onRejected ) */                \
494514f5e3Sopenharmony_ci    V("catch",   Catch,   1, INVALID)                           \
504514f5e3Sopenharmony_ci    /* Promise.prototype.finally ( onFinally ) */               \
514514f5e3Sopenharmony_ci    V("finally", Finally, 1, INVALID)                           \
524514f5e3Sopenharmony_ci    /* Promise.prototype.then ( onFulfilled, onRejected ) */    \
534514f5e3Sopenharmony_ci    V("then",    Then,    2, INVALID)
544514f5e3Sopenharmony_ci
554514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins {
564514f5e3Sopenharmony_ciclass BuiltinsPromise : public base::BuiltinsBase {
574514f5e3Sopenharmony_cipublic:
584514f5e3Sopenharmony_ci    // 25.4.3.1 Promise ( executor )
594514f5e3Sopenharmony_ci    static JSTaggedValue PromiseConstructor(EcmaRuntimeCallInfo *argv);
604514f5e3Sopenharmony_ci    // 25.4.4.1 Promise.all ( iterable )
614514f5e3Sopenharmony_ci    static JSTaggedValue All(EcmaRuntimeCallInfo *argv);
624514f5e3Sopenharmony_ci    // 25.4.4.3 Promise.race ( iterable )
634514f5e3Sopenharmony_ci    static JSTaggedValue Race(EcmaRuntimeCallInfo *argv);
644514f5e3Sopenharmony_ci    // 25.4.4.4 Promise.reject ( r )
654514f5e3Sopenharmony_ci    static JSTaggedValue Reject(EcmaRuntimeCallInfo *argv);
664514f5e3Sopenharmony_ci    // 25.4.4.5 Promise.resolve ( x )
674514f5e3Sopenharmony_ci    static JSTaggedValue Resolve(EcmaRuntimeCallInfo *argv);
684514f5e3Sopenharmony_ci    // 25.4.4.6 get Promise [ @@species ]
694514f5e3Sopenharmony_ci    static JSTaggedValue GetSpecies(EcmaRuntimeCallInfo *argv);
704514f5e3Sopenharmony_ci    // 25.4.5.1 Promise.prototype.catch ( onRejected )
714514f5e3Sopenharmony_ci    static JSTaggedValue Catch(EcmaRuntimeCallInfo *argv);
724514f5e3Sopenharmony_ci    // 25.4.5.3 Promise.prototype.then ( onFulfilled , onRejected )
734514f5e3Sopenharmony_ci    static JSTaggedValue Then(EcmaRuntimeCallInfo *argv);
744514f5e3Sopenharmony_ci
754514f5e3Sopenharmony_ci    static JSTaggedValue PerformPromiseThen(JSThread *thread, const JSHandle<JSPromise> &promise,
764514f5e3Sopenharmony_ci                                            const JSHandle<JSTaggedValue> &onFulfilled,
774514f5e3Sopenharmony_ci                                            const JSHandle<JSTaggedValue> &onRejected,
784514f5e3Sopenharmony_ci                                            const JSHandle<PromiseCapability> &capability);
794514f5e3Sopenharmony_ci
804514f5e3Sopenharmony_ci    static JSTaggedValue Any(EcmaRuntimeCallInfo *argv);
814514f5e3Sopenharmony_ci
824514f5e3Sopenharmony_ci    static JSTaggedValue AllSettled(EcmaRuntimeCallInfo *argv);
834514f5e3Sopenharmony_ci
844514f5e3Sopenharmony_ci    static JSTaggedValue Finally(EcmaRuntimeCallInfo *argv);
854514f5e3Sopenharmony_ci
864514f5e3Sopenharmony_ci    static JSTaggedValue GetPromiseResolve(JSThread *thread, JSHandle<JSTaggedValue> promiseConstructor);
874514f5e3Sopenharmony_ci
884514f5e3Sopenharmony_ci    // Excluding the '@@' internal properties
894514f5e3Sopenharmony_ci    static Span<const base::BuiltinFunctionEntry> GetPromiseFunctions()
904514f5e3Sopenharmony_ci    {
914514f5e3Sopenharmony_ci        return Span<const base::BuiltinFunctionEntry>(PROMISE_FUNCTIONS);
924514f5e3Sopenharmony_ci    }
934514f5e3Sopenharmony_ci
944514f5e3Sopenharmony_ci    // Excluding the constructor and '@@' internal properties.
954514f5e3Sopenharmony_ci    static Span<const base::BuiltinFunctionEntry> GetPromisePrototypeFunctions()
964514f5e3Sopenharmony_ci    {
974514f5e3Sopenharmony_ci        return Span<const base::BuiltinFunctionEntry>(PROMISE_PROTOTYPE_FUNCTIONS);
984514f5e3Sopenharmony_ci    }
994514f5e3Sopenharmony_ci
1004514f5e3Sopenharmony_ciprivate:
1014514f5e3Sopenharmony_ci#define BUILTIN_PROMISE_FUNCTION_ENTRY(name, method, length, id) \
1024514f5e3Sopenharmony_ci    base::BuiltinFunctionEntry::Create(name, BuiltinsPromise::method, length, kungfu::BuiltinsStubCSigns::id),
1034514f5e3Sopenharmony_ci
1044514f5e3Sopenharmony_ci    static constexpr std::array PROMISE_FUNCTIONS  = {
1054514f5e3Sopenharmony_ci        BUILTIN_PROMISE_FUNCTIONS(BUILTIN_PROMISE_FUNCTION_ENTRY)
1064514f5e3Sopenharmony_ci    };
1074514f5e3Sopenharmony_ci    static constexpr std::array PROMISE_PROTOTYPE_FUNCTIONS = {
1084514f5e3Sopenharmony_ci        BUILTIN_PROMISE_PROTOTYPE_FUNCTIONS(BUILTIN_PROMISE_FUNCTION_ENTRY)
1094514f5e3Sopenharmony_ci    };
1104514f5e3Sopenharmony_ci#undef BUILTIN_PROMISE_FUNCTION_ENTRY
1114514f5e3Sopenharmony_ci
1124514f5e3Sopenharmony_ci    static JSTaggedValue PerformPromiseAll(JSThread *thread,
1134514f5e3Sopenharmony_ci                                           const JSHandle<PromiseIteratorRecord> &itRecord,
1144514f5e3Sopenharmony_ci                                           const JSHandle<JSTaggedValue> &ctor,
1154514f5e3Sopenharmony_ci                                           const JSHandle<PromiseCapability> &capa);
1164514f5e3Sopenharmony_ci
1174514f5e3Sopenharmony_ci    static JSHandle<CompletionRecord> PerformPromiseRace(JSThread *thread,
1184514f5e3Sopenharmony_ci                                                         const JSHandle<PromiseIteratorRecord> &iteratorRecord,
1194514f5e3Sopenharmony_ci                                                         const JSHandle<PromiseCapability> &capability,
1204514f5e3Sopenharmony_ci                                                         const JSHandle<JSTaggedValue> &constructor);
1214514f5e3Sopenharmony_ci
1224514f5e3Sopenharmony_ci    static JSHandle<CompletionRecord> PerformPromiseAllSettled(JSThread *thread,
1234514f5e3Sopenharmony_ci                                                               const JSHandle<PromiseIteratorRecord> &iterRecord,
1244514f5e3Sopenharmony_ci                                                               const JSHandle<JSTaggedValue> &constructor,
1254514f5e3Sopenharmony_ci                                                               const JSHandle<PromiseCapability> &resultCapa,
1264514f5e3Sopenharmony_ci                                                               const JSHandle<JSTaggedValue> &promiseResolve);
1274514f5e3Sopenharmony_ci
1284514f5e3Sopenharmony_ci    static JSHandle<CompletionRecord> PerformPromiseAny(JSThread *thread,
1294514f5e3Sopenharmony_ci                                                        const JSHandle<PromiseIteratorRecord> &iteratorRecord,
1304514f5e3Sopenharmony_ci                                                        const JSHandle<JSTaggedValue> &constructor,
1314514f5e3Sopenharmony_ci                                                        const JSHandle<PromiseCapability> &resultCapability,
1324514f5e3Sopenharmony_ci                                                        const JSHandle<JSTaggedValue> &promiseResolve);
1334514f5e3Sopenharmony_ci};
1344514f5e3Sopenharmony_ci}  // namespace panda::ecmascript::builtins
1354514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_BUILTINS_BUILTINS_PROMISE_H