* Copyright (c) 2022 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.
*/
#ifndef ECMASCRIPT_BUILTINS_BUILTINS_ATOMICS_H
#define ECMASCRIPT_BUILTINS_BUILTINS_ATOMICS_H
#include "ecmascript/base/builtins_base.h"
#include "ecmascript/js_dataview.h"
#include "ecmascript/waiter_list.h"
#define BUILTIN_ATOMICS_FUNCTIONS(V) \
\
V("add", Add, 3, INVALID) \
\
V("and", And, 3, INVALID) \
\
V("compareExchange", CompareExchange, 4, INVALID) \
\
V("exchange", Exchange, 3, INVALID) \
\
V("isLockFree", IsLockFree, 1, INVALID) \
\
V("load", Load, 2, INVALID) \
\
V("notify", Notify, 3, INVALID) \
\
V("or", Or, 3, INVALID) \
\
V("store", Store, 3, INVALID) \
\
V("sub", Sub, 3, INVALID) \
\
V("wait", Wait, 4, INVALID) \
\
V("xor", Xor, 3, INVALID)
namespace panda::ecmascript::builtins {
enum class WaitResult: uint8_t {OK = 0, NOT_EQ, TIME_OUT};
class BuiltinsAtomics : public base::BuiltinsBase {
public:
static JSTaggedValue Add(EcmaRuntimeCallInfo *argv);
static JSTaggedValue And(EcmaRuntimeCallInfo *argv);
static JSTaggedValue CompareExchange(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Exchange(EcmaRuntimeCallInfo *argv);
static JSTaggedValue IsLockFree(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Load(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Or(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Store(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Sub(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Wait(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Notify(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Xor(EcmaRuntimeCallInfo *argv);
static Span<const base::BuiltinFunctionEntry> GetAtomicsFunctions()
{
return Span<const base::BuiltinFunctionEntry>(ATOMICS_FUNCTIONS);
}
private:
#define BUILTINS_ATOMICS_FUNCTION_ENTRY(name, method, length, id) \
base::BuiltinFunctionEntry::Create(name, BuiltinsAtomics::method, length, BUILTINS_STUB_ID(id)),
static constexpr std::array ATOMICS_FUNCTIONS = {
BUILTIN_ATOMICS_FUNCTIONS(BUILTINS_ATOMICS_FUNCTION_ENTRY)
};
#undef BUILTINS_ATOMICS_FUNCTION_ENTRY
static uint32_t Signal(JSThread *thread, JSHandle<JSTaggedValue> &arrayBuffer,
const size_t &index, double wakeCount);
template <typename T>
static WaitResult DoWait(JSThread *thread, JSHandle<JSTaggedValue> &arrayBuffer,
size_t index, T execpt, double timeout);
template<typename callbackfun>
static JSTaggedValue AtomicReadModifyWrite(JSThread *thread, const JSHandle<JSTaggedValue> &typedArray,
JSHandle<JSTaggedValue> &index, EcmaRuntimeCallInfo *argv,
const callbackfun &op);
template<typename callbackfun>
static JSTaggedValue AtomicReadModifyWriteCase(JSThread *thread, JSTaggedValue buffer, DataViewType type,
uint32_t indexedPosition, EcmaRuntimeCallInfo *argv,
const callbackfun &op);
template<typename callbackfun>
static JSTaggedValue HandleWithUint8(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
EcmaRuntimeCallInfo *argv, const callbackfun &op, uint8_t &tag);
template<typename callbackfun>
static JSTaggedValue HandleWithInt8(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
EcmaRuntimeCallInfo *argv, const callbackfun &op, int8_t &tag);
template<typename callbackfun>
static JSTaggedValue HandleWithUint16(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
EcmaRuntimeCallInfo *argv, const callbackfun &op, uint16_t &tag);
template<typename callbackfun>
static JSTaggedValue HandleWithInt16(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
EcmaRuntimeCallInfo *argv, const callbackfun &op, int16_t &tag);
template<typename callbackfun>
static JSTaggedValue HandleWithUint32(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
EcmaRuntimeCallInfo *argv, const callbackfun &op, uint32_t &tag);
template<typename callbackfun>
static JSTaggedValue HandleWithInt32(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
EcmaRuntimeCallInfo *argv, const callbackfun &op, int32_t &tag);
template<typename callbackfun>
static JSTaggedValue HandleWithBigInt64(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
EcmaRuntimeCallInfo *argv, const callbackfun &op, int64_t &tag, bool &lossless);
template<typename callbackfun>
static JSTaggedValue HandleWithBigUint64(JSThread *thread, uint32_t size, uint8_t *block, uint32_t indexedPosition,
EcmaRuntimeCallInfo *argv, const callbackfun &op, uint64_t &tag, bool &lossless);
static constexpr int ARGS_NUMBER = 2;
};
}
#endif