14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021 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_BYTE_ARRAY_H 174514f5e3Sopenharmony_ci#define ECMASCRIPT_BYTE_ARRAY_H 184514f5e3Sopenharmony_ci 194514f5e3Sopenharmony_ci#include "ecmascript/js_hclass.h" 204514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h" 214514f5e3Sopenharmony_ci#include "ecmascript/js_dataview.h" 224514f5e3Sopenharmony_ci 234514f5e3Sopenharmony_cinamespace panda::ecmascript { 244514f5e3Sopenharmony_ciclass ObjectFactory; 254514f5e3Sopenharmony_ciclass JSThread; 264514f5e3Sopenharmony_ci 274514f5e3Sopenharmony_ciclass ByteArray : public TaggedObject { 284514f5e3Sopenharmony_cipublic: 294514f5e3Sopenharmony_ci CAST_CHECK(ByteArray, IsByteArray); 304514f5e3Sopenharmony_ci 314514f5e3Sopenharmony_ci static inline size_t ComputeSize(size_t elemSize, uint32_t length) 324514f5e3Sopenharmony_ci { 334514f5e3Sopenharmony_ci ASSERT(elemSize != 0); 344514f5e3Sopenharmony_ci size_t size = DATA_OFFSET + elemSize * length; 354514f5e3Sopenharmony_ci return size; 364514f5e3Sopenharmony_ci } 374514f5e3Sopenharmony_ci 384514f5e3Sopenharmony_ci static inline size_t ComputeDataSize(size_t elemSize, uint32_t length) 394514f5e3Sopenharmony_ci { 404514f5e3Sopenharmony_ci ASSERT(elemSize != 0); 414514f5e3Sopenharmony_ci return elemSize * length; 424514f5e3Sopenharmony_ci } 434514f5e3Sopenharmony_ci 444514f5e3Sopenharmony_ci size_t GetPointerLength() 454514f5e3Sopenharmony_ci { 464514f5e3Sopenharmony_ci size_t byteSize = ComputeDataSize(GetArrayLength(), GetByteLength()); 474514f5e3Sopenharmony_ci return AlignUp(byteSize, static_cast<size_t>(MemAlignment::MEM_ALIGN_OBJECT)) / sizeof(JSTaggedType); 484514f5e3Sopenharmony_ci } 494514f5e3Sopenharmony_ci 504514f5e3Sopenharmony_ci inline void *GetData(uint32_t index = 0) const 514514f5e3Sopenharmony_ci { 524514f5e3Sopenharmony_ci return reinterpret_cast<void *>(ToUintPtr(this) + DATA_OFFSET + index * GetByteLength()); 534514f5e3Sopenharmony_ci } 544514f5e3Sopenharmony_ci 554514f5e3Sopenharmony_ci void Set(JSThread* thread, uint32_t idx, DataViewType type, JSTaggedType val, uint32_t offset = 0); 564514f5e3Sopenharmony_ci JSTaggedValue Get(JSThread *thread, uint32_t idx, DataViewType type, uint32_t offset = 0); 574514f5e3Sopenharmony_ci 584514f5e3Sopenharmony_ci static constexpr size_t ARRAY_LENGTH_OFFSET = TaggedObjectSize(); 594514f5e3Sopenharmony_ci ACCESSORS_PRIMITIVE_FIELD(ArrayLength, uint32_t, ARRAY_LENGTH_OFFSET, BYTE_LENGTH_OFFSET) 604514f5e3Sopenharmony_ci ACCESSORS_PRIMITIVE_FIELD(ByteLength, uint32_t, BYTE_LENGTH_OFFSET, LAST_OFFSET) 614514f5e3Sopenharmony_ci DEFINE_ALIGN_SIZE(LAST_OFFSET); 624514f5e3Sopenharmony_ci static constexpr size_t DATA_OFFSET = SIZE; // DATA_OFFSET equal to Empty ByteArray size 634514f5e3Sopenharmony_ci 644514f5e3Sopenharmony_ci DECL_VISIT_ARRAY(DATA_OFFSET, 0, GetPointerLength()); 654514f5e3Sopenharmony_ci DECL_DUMP() 664514f5e3Sopenharmony_ci 674514f5e3Sopenharmony_ciprivate: 684514f5e3Sopenharmony_ci friend class ObjectFactory; 694514f5e3Sopenharmony_ci}; 704514f5e3Sopenharmony_ci 714514f5e3Sopenharmony_cistatic_assert(ByteArray::ARRAY_LENGTH_OFFSET == sizeof(TaggedObject)); 724514f5e3Sopenharmony_cistatic_assert((ByteArray::DATA_OFFSET % static_cast<uint8_t>(MemAlignment::MEM_ALIGN_OBJECT)) == 0); 734514f5e3Sopenharmony_ci} // namespace panda::ecmascript 744514f5e3Sopenharmony_ci#endif // ECMASCRIPT_BYTE_ARRAY_H 75