• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ECMASCRIPT_BUILTINS_BUILTINS_ATOMICS_H
17 #define ECMASCRIPT_BUILTINS_BUILTINS_ATOMICS_H
18 
19 #include "ecmascript/base/builtins_base.h"
20 #include "ecmascript/js_dataview.h"
21 #include "ecmascript/waiter_list.h"
22 
23 namespace panda::ecmascript::builtins {
24 enum class WaitResult: uint8_t {OK = 0, NOT_EQ, TIME_OUT};
25 
26 class BuiltinsAtomics : public base::BuiltinsBase {
27 public:
28     // 25.4.2 Atomics.add ( typedArray, index, value )
29     static JSTaggedValue Add(EcmaRuntimeCallInfo *argv);
30     // 25.4.3 Atomics.and ( typedArray, index, value )
31     static JSTaggedValue And(EcmaRuntimeCallInfo *argv);
32     // 25.4.4 Atomics.compareExchange ( typedArray, index, expectedValue, replacementValue)
33     static JSTaggedValue CompareExchange(EcmaRuntimeCallInfo *argv);
34     // 25.4.5 Atomics.exchange ( typedArray, index, value )
35     static JSTaggedValue Exchange(EcmaRuntimeCallInfo *argv);
36     // 25.4.6 Atomics.isLockFree ( size )
37     static JSTaggedValue IsLockFree(EcmaRuntimeCallInfo *argv);
38     // 25.4.7 Atomics.load ( typedArray, index )
39     static JSTaggedValue Load(EcmaRuntimeCallInfo *argv);
40 	 // 25.4.8 Atomics.or ( typedArray, index, value )
41     static JSTaggedValue Or(EcmaRuntimeCallInfo *argv);
42     // 25.4.9 Atomics.store ( typedArray, index, value )
43     static JSTaggedValue Store(EcmaRuntimeCallInfo *argv);
44     // 25.4.10 Atomics.sub ( typedArray, index, value )
45     static JSTaggedValue Sub(EcmaRuntimeCallInfo *argv);
46     // 25.4.11 Atomics.wait ( typedArray, index, value, timeout)
47     static JSTaggedValue Wait(EcmaRuntimeCallInfo *argv);
48     // 25.4.12 Atomics.notify ( typedArray, index, count)
49     static JSTaggedValue Notify(EcmaRuntimeCallInfo *argv);
50     // 25.4.13 Atomics.xor ( typedArray, index, value )
51     static JSTaggedValue Xor(EcmaRuntimeCallInfo *argv);
52 
53 private:
54     static uint32_t Signal(JSHandle<JSTaggedValue> &arrayBuffer, const size_t &index, double wakeCount);
55     template <typename T>
56     static WaitResult DoWait(JSThread *thread, JSHandle<JSTaggedValue> &arrayBuffer,
57                              size_t index, T execpt, double timeout);
58     template<typename callbackfun>
59     static JSTaggedValue AtomicReadModifyWrite(JSThread *thread, const JSHandle<JSTaggedValue> &typedArray,
60                                                JSHandle<JSTaggedValue> &index, EcmaRuntimeCallInfo *argv,
61                                                const callbackfun &op);
62     template<typename callbackfun>
63     static JSTaggedValue AtomicReadModifyWriteCase(JSThread *thread, JSTaggedValue buffer, DataViewType type,
64                                                    uint32_t indexedPosition, EcmaRuntimeCallInfo *argv,
65                                                    const callbackfun &op);
66     template<typename callbackfun>
67     static JSTaggedValue HandleWithUint8(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
68                                          EcmaRuntimeCallInfo *argv, const callbackfun &op);
69     template<typename callbackfun>
70     static JSTaggedValue HandleWithInt8(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
71                                         EcmaRuntimeCallInfo *argv, const callbackfun &op);
72     template<typename callbackfun>
73     static JSTaggedValue HandleWithUint16(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
74                                           EcmaRuntimeCallInfo *argv, const callbackfun &op);
75     template<typename callbackfun>
76     static JSTaggedValue HandleWithInt16(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
77                                          EcmaRuntimeCallInfo *argv, const callbackfun &op);
78     template<typename callbackfun>
79     static JSTaggedValue HandleWithUint32(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
80                                           EcmaRuntimeCallInfo *argv, const callbackfun &op);
81     template<typename callbackfun>
82     static JSTaggedValue HandleWithInt32(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
83                                          EcmaRuntimeCallInfo *argv, const callbackfun &op);
84     template<typename callbackfun>
85     static JSTaggedValue HandleWithBigInt64(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
86                                             EcmaRuntimeCallInfo *argv, const callbackfun &op);
87     template<typename callbackfun>
88     static JSTaggedValue HandleWithBigUint64(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
89                                              EcmaRuntimeCallInfo *argv, const callbackfun &op);
90 
91     static constexpr int ARGS_NUMBER = 2;
92 };
93 }  // namespace panda::ecmascript::builtins
94 #endif  // ECMASCRIPT_BUILTINS_BUILTINS_MATH_H
95