14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2022 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_ATOMICS_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_BUILTINS_BUILTINS_ATOMICS_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/base/builtins_base.h"
204514f5e3Sopenharmony_ci#include "ecmascript/js_dataview.h"
214514f5e3Sopenharmony_ci#include "ecmascript/waiter_list.h"
224514f5e3Sopenharmony_ci
234514f5e3Sopenharmony_ci// List of functions in Atomics.
244514f5e3Sopenharmony_ci// V(name, func, length, stubIndex)
254514f5e3Sopenharmony_ci// where BuiltinsAtomics::func refers to the native implementation of Atomics[name].
264514f5e3Sopenharmony_ci//       kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available.
274514f5e3Sopenharmony_ci// The following functions are not implemented yet:
284514f5e3Sopenharmony_ci//   - Atomics.waitAsync ( typedArray, index, value, timeout )
294514f5e3Sopenharmony_ci#define BUILTIN_ATOMICS_FUNCTIONS(V)                                                         \
304514f5e3Sopenharmony_ci    /* Atomics.add ( typedArray, index, value ) */                                          \
314514f5e3Sopenharmony_ci    V("add", Add, 3, INVALID)                                                               \
324514f5e3Sopenharmony_ci    /* Atomics.and ( typedArray, index, value ) */                                          \
334514f5e3Sopenharmony_ci    V("and", And, 3, INVALID)                                                               \
344514f5e3Sopenharmony_ci    /* Atomics.compareExchange ( typedArray, index, expectedValue, replacementValue ) */    \
354514f5e3Sopenharmony_ci    V("compareExchange", CompareExchange, 4, INVALID)                                       \
364514f5e3Sopenharmony_ci    /* Atomics.exchange ( typedArray, index, value ) */                                     \
374514f5e3Sopenharmony_ci    V("exchange", Exchange, 3, INVALID)                                                     \
384514f5e3Sopenharmony_ci    /* Atomics.isLockFree ( size ) */                                                       \
394514f5e3Sopenharmony_ci    V("isLockFree", IsLockFree, 1, INVALID)                                                 \
404514f5e3Sopenharmony_ci    /* Atomics.load ( typedArray, index ) */                                                \
414514f5e3Sopenharmony_ci    V("load", Load, 2, INVALID)                                                             \
424514f5e3Sopenharmony_ci    /* Atomics.notify ( typedArray, index, count ) */                                       \
434514f5e3Sopenharmony_ci    V("notify", Notify, 3, INVALID)                                                         \
444514f5e3Sopenharmony_ci    /* Atomics.or ( typedArray, index, value ) */                                           \
454514f5e3Sopenharmony_ci    V("or", Or, 3, INVALID)                                                                 \
464514f5e3Sopenharmony_ci    /* Atomics.store ( typedArray, index, value ) */                                        \
474514f5e3Sopenharmony_ci    V("store", Store, 3, INVALID)                                                           \
484514f5e3Sopenharmony_ci    /* Atomics.sub ( typedArray, index, value ) */                                          \
494514f5e3Sopenharmony_ci    V("sub", Sub, 3, INVALID)                                                               \
504514f5e3Sopenharmony_ci    /* Atomics.wait ( typedArray, index, value, timeout ) */                                \
514514f5e3Sopenharmony_ci    V("wait", Wait, 4, INVALID)                                                             \
524514f5e3Sopenharmony_ci    /* Atomics.xor ( typedArray, index, value ) */                                          \
534514f5e3Sopenharmony_ci    V("xor", Xor, 3, INVALID)
544514f5e3Sopenharmony_ci
554514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins {
564514f5e3Sopenharmony_cienum class WaitResult: uint8_t {OK = 0, NOT_EQ, TIME_OUT};
574514f5e3Sopenharmony_ci
584514f5e3Sopenharmony_ciclass BuiltinsAtomics : public base::BuiltinsBase {
594514f5e3Sopenharmony_cipublic:
604514f5e3Sopenharmony_ci    // 25.4.2 Atomics.add ( typedArray, index, value )
614514f5e3Sopenharmony_ci    static JSTaggedValue Add(EcmaRuntimeCallInfo *argv);
624514f5e3Sopenharmony_ci    // 25.4.3 Atomics.and ( typedArray, index, value )
634514f5e3Sopenharmony_ci    static JSTaggedValue And(EcmaRuntimeCallInfo *argv);
644514f5e3Sopenharmony_ci    // 25.4.4 Atomics.compareExchange ( typedArray, index, expectedValue, replacementValue)
654514f5e3Sopenharmony_ci    static JSTaggedValue CompareExchange(EcmaRuntimeCallInfo *argv);
664514f5e3Sopenharmony_ci    // 25.4.5 Atomics.exchange ( typedArray, index, value )
674514f5e3Sopenharmony_ci    static JSTaggedValue Exchange(EcmaRuntimeCallInfo *argv);
684514f5e3Sopenharmony_ci    // 25.4.6 Atomics.isLockFree ( size )
694514f5e3Sopenharmony_ci    static JSTaggedValue IsLockFree(EcmaRuntimeCallInfo *argv);
704514f5e3Sopenharmony_ci    // 25.4.7 Atomics.load ( typedArray, index )
714514f5e3Sopenharmony_ci    static JSTaggedValue Load(EcmaRuntimeCallInfo *argv);
724514f5e3Sopenharmony_ci	 // 25.4.8 Atomics.or ( typedArray, index, value )
734514f5e3Sopenharmony_ci    static JSTaggedValue Or(EcmaRuntimeCallInfo *argv);
744514f5e3Sopenharmony_ci    // 25.4.9 Atomics.store ( typedArray, index, value )
754514f5e3Sopenharmony_ci    static JSTaggedValue Store(EcmaRuntimeCallInfo *argv);
764514f5e3Sopenharmony_ci    // 25.4.10 Atomics.sub ( typedArray, index, value )
774514f5e3Sopenharmony_ci    static JSTaggedValue Sub(EcmaRuntimeCallInfo *argv);
784514f5e3Sopenharmony_ci    // 25.4.11 Atomics.wait ( typedArray, index, value, timeout)
794514f5e3Sopenharmony_ci    static JSTaggedValue Wait(EcmaRuntimeCallInfo *argv);
804514f5e3Sopenharmony_ci    // 25.4.12 Atomics.notify ( typedArray, index, count)
814514f5e3Sopenharmony_ci    static JSTaggedValue Notify(EcmaRuntimeCallInfo *argv);
824514f5e3Sopenharmony_ci    // 25.4.13 Atomics.xor ( typedArray, index, value )
834514f5e3Sopenharmony_ci    static JSTaggedValue Xor(EcmaRuntimeCallInfo *argv);
844514f5e3Sopenharmony_ci
854514f5e3Sopenharmony_ci    static Span<const base::BuiltinFunctionEntry> GetAtomicsFunctions()
864514f5e3Sopenharmony_ci    {
874514f5e3Sopenharmony_ci        return Span<const base::BuiltinFunctionEntry>(ATOMICS_FUNCTIONS);
884514f5e3Sopenharmony_ci    }
894514f5e3Sopenharmony_ci
904514f5e3Sopenharmony_ciprivate:
914514f5e3Sopenharmony_ci#define BUILTINS_ATOMICS_FUNCTION_ENTRY(name, method, length, id) \
924514f5e3Sopenharmony_ci    base::BuiltinFunctionEntry::Create(name, BuiltinsAtomics::method, length, kungfu::BuiltinsStubCSigns::id),
934514f5e3Sopenharmony_ci
944514f5e3Sopenharmony_ci    static constexpr std::array ATOMICS_FUNCTIONS  = {
954514f5e3Sopenharmony_ci        BUILTIN_ATOMICS_FUNCTIONS(BUILTINS_ATOMICS_FUNCTION_ENTRY)
964514f5e3Sopenharmony_ci    };
974514f5e3Sopenharmony_ci#undef BUILTINS_ATOMICS_FUNCTION_ENTRY
984514f5e3Sopenharmony_ci
994514f5e3Sopenharmony_ci    static uint32_t Signal(JSHandle<JSTaggedValue> &arrayBuffer, const size_t &index, double wakeCount);
1004514f5e3Sopenharmony_ci    template <typename T>
1014514f5e3Sopenharmony_ci    static WaitResult DoWait(JSThread *thread, JSHandle<JSTaggedValue> &arrayBuffer,
1024514f5e3Sopenharmony_ci                             size_t index, T execpt, double timeout);
1034514f5e3Sopenharmony_ci    template<typename callbackfun>
1044514f5e3Sopenharmony_ci    static JSTaggedValue AtomicReadModifyWrite(JSThread *thread, const JSHandle<JSTaggedValue> &typedArray,
1054514f5e3Sopenharmony_ci                                               JSHandle<JSTaggedValue> &index, EcmaRuntimeCallInfo *argv,
1064514f5e3Sopenharmony_ci                                               const callbackfun &op);
1074514f5e3Sopenharmony_ci    template<typename callbackfun>
1084514f5e3Sopenharmony_ci    static JSTaggedValue AtomicReadModifyWriteCase(JSThread *thread, JSTaggedValue buffer, DataViewType type,
1094514f5e3Sopenharmony_ci                                                   uint32_t indexedPosition, EcmaRuntimeCallInfo *argv,
1104514f5e3Sopenharmony_ci                                                   const callbackfun &op);
1114514f5e3Sopenharmony_ci    template<typename callbackfun>
1124514f5e3Sopenharmony_ci    static JSTaggedValue HandleWithUint8(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
1134514f5e3Sopenharmony_ci                                         EcmaRuntimeCallInfo *argv, const callbackfun &op, uint8_t &tag);
1144514f5e3Sopenharmony_ci    template<typename callbackfun>
1154514f5e3Sopenharmony_ci    static JSTaggedValue HandleWithInt8(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
1164514f5e3Sopenharmony_ci                                        EcmaRuntimeCallInfo *argv, const callbackfun &op, int8_t &tag);
1174514f5e3Sopenharmony_ci    template<typename callbackfun>
1184514f5e3Sopenharmony_ci    static JSTaggedValue HandleWithUint16(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
1194514f5e3Sopenharmony_ci                                          EcmaRuntimeCallInfo *argv, const callbackfun &op, uint16_t &tag);
1204514f5e3Sopenharmony_ci    template<typename callbackfun>
1214514f5e3Sopenharmony_ci    static JSTaggedValue HandleWithInt16(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
1224514f5e3Sopenharmony_ci                                         EcmaRuntimeCallInfo *argv, const callbackfun &op, int16_t &tag);
1234514f5e3Sopenharmony_ci    template<typename callbackfun>
1244514f5e3Sopenharmony_ci    static JSTaggedValue HandleWithUint32(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
1254514f5e3Sopenharmony_ci                                          EcmaRuntimeCallInfo *argv, const callbackfun &op, uint32_t &tag);
1264514f5e3Sopenharmony_ci    template<typename callbackfun>
1274514f5e3Sopenharmony_ci    static JSTaggedValue HandleWithInt32(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
1284514f5e3Sopenharmony_ci                                         EcmaRuntimeCallInfo *argv, const callbackfun &op, int32_t &tag);
1294514f5e3Sopenharmony_ci    template<typename callbackfun>
1304514f5e3Sopenharmony_ci    static JSTaggedValue HandleWithBigInt64(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
1314514f5e3Sopenharmony_ci        EcmaRuntimeCallInfo *argv, const callbackfun &op, int64_t &tag, bool &lossless);
1324514f5e3Sopenharmony_ci    template<typename callbackfun>
1334514f5e3Sopenharmony_ci    static JSTaggedValue HandleWithBigUint64(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
1344514f5e3Sopenharmony_ci        EcmaRuntimeCallInfo *argv, const callbackfun &op, uint64_t &tag, bool &lossless);
1354514f5e3Sopenharmony_ci
1364514f5e3Sopenharmony_ci    static constexpr int ARGS_NUMBER = 2;
1374514f5e3Sopenharmony_ci};
1384514f5e3Sopenharmony_ci}  // namespace panda::ecmascript::builtins
1394514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_BUILTINS_BUILTINS_MATH_H
140