1/*
2 * Copyright (c) 2022-2023 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_ELEMENT_ACCESSOR_H
17#define ECMASCRIPT_ELEMENT_ACCESSOR_H
18
19#include "ecmascript/js_hclass.h"
20#include "ecmascript/js_tagged_value.h"
21#include "ecmascript/tagged_array.h"
22
23namespace panda {
24namespace ecmascript {
25// ElementAccessor intends to replace all .GetElements and share the common following methods.
26class ElementAccessor {
27public:
28    static JSTaggedValue PUBLIC_API Get(JSHandle<JSObject> receiver, uint32_t idx);
29    static JSTaggedValue Get(JSObject *receiver, uint32_t idx);
30    static JSTaggedValue PUBLIC_API FastGet(JSHandle<TaggedArray> elements, uint32_t idx, ElementsKind kind);
31
32    template<typename T>
33    static void Set(const JSThread *thread, JSHandle<JSObject> receiver, uint32_t idx, const JSHandle<T> &value,
34                    bool needTransition, ElementsKind extraKind = ElementsKind::NONE);
35
36    template<typename T>
37    static void FastSet(const JSThread *thread, JSHandle<TaggedArray> elements, uint32_t idx,
38                        const JSHandle<T> &value, ElementsKind kind);
39    static bool IsDictionaryMode(JSHandle<JSObject> receiver);
40    static bool IsDictionaryMode(JSObject *receiver);
41
42    static uint32_t GetElementsLength(JSHandle<JSObject> receiver);
43    static uint32_t GetElementsLength(JSObject *receiver);
44
45    static JSTaggedValue GetTaggedValueWithElementsKind(JSTaggedType rawValue, ElementsKind kind);
46
47    static void CopyJSArrayObject(const JSThread *thread, JSHandle<JSObject>srcObj, JSHandle<JSObject>dstObj,
48                                  uint32_t effectiveLength);
49    static void CopyJSArrayToTaggedArray(const JSThread *thread, JSHandle<JSObject>srcObj,
50                                         JSHandle<TaggedArray>dstElements, uint32_t effectiveLength);
51    static JSTaggedType PUBLIC_API ConvertTaggedValueWithElementsKind(JSTaggedValue rawValue, ElementsKind kind);
52private:
53};
54}  // namespace ecmascript
55}  // namespace panda
56#endif  // ECMASCRIPT_ELEMENT_ACCESSOR_H
57
58