• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_BYTE_ARRAY_H
17 #define ECMASCRIPT_BYTE_ARRAY_H
18 
19 #include "ecmascript/js_hclass.h"
20 #include "ecmascript/js_tagged_value.h"
21 #include "ecmascript/js_dataview.h"
22 
23 namespace panda::ecmascript {
24 class ObjectFactory;
25 class JSThread;
26 
27 class ByteArray : public TaggedObject {
28 public:
29     CAST_CHECK(ByteArray, IsByteArray);
30 
ComputeSize(size_t elemSize,uint32_t length)31     static inline size_t ComputeSize(size_t elemSize, uint32_t length)
32     {
33         ASSERT(elemSize != 0);
34         size_t size = DATA_OFFSET + elemSize * length;
35         return size;
36     }
37 
38     inline void *GetData(uint32_t index = 0) const
39     {
40         return reinterpret_cast<void *>(ToUintPtr(this) + DATA_OFFSET + index * GetByteLength());
41     }
42 
43     void Set(JSThread* thread, uint32_t idx, DataViewType type, JSTaggedType val, uint32_t offset = 0);
44     JSTaggedValue Get(JSThread *thread, uint32_t idx, DataViewType type, uint32_t offset = 0);
45 
46     static constexpr size_t ARRAY_LENGTH_OFFSET = TaggedObjectSize();
47     ACCESSORS_PRIMITIVE_FIELD(ArrayLength, uint32_t, ARRAY_LENGTH_OFFSET, BYTE_LENGTH_OFFSET)
48     ACCESSORS_PRIMITIVE_FIELD(ByteLength, uint32_t, BYTE_LENGTH_OFFSET, LAST_OFFSET)
49     DEFINE_ALIGN_SIZE(LAST_OFFSET);
50     static constexpr size_t DATA_OFFSET = SIZE;  // DATA_OFFSET equal to Empty ByteArray size
51 
52     DECL_DUMP()
53 
54 private:
55     friend class ObjectFactory;
56 };
57 
58 static_assert(ByteArray::ARRAY_LENGTH_OFFSET == sizeof(TaggedObject));
59 static_assert((ByteArray::DATA_OFFSET % static_cast<uint8_t>(MemAlignment::MEM_ALIGN_OBJECT)) == 0);
60 }  // namespace panda::ecmascript
61 #endif  // ECMASCRIPT_BYTE_ARRAY_H
62