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_STRING_TABLE_H 17 #define ECMASCRIPT_STRING_TABLE_H 18 19 #include "ecmascript/mem/c_containers.h" 20 #include "ecmascript/mem/space.h" 21 #include "ecmascript/mem/visitor.h" 22 23 namespace panda::ecmascript { 24 class EcmaString; 25 class EcmaVM; 26 27 class EcmaStringTable { 28 public: 29 explicit EcmaStringTable(const EcmaVM *vm); ~EcmaStringTable()30 virtual ~EcmaStringTable() 31 { 32 table_.clear(); 33 } 34 35 void InternEmptyString(EcmaString *emptyStr); 36 EcmaString *GetOrInternString(const JSHandle<EcmaString> &firstString, const JSHandle<EcmaString> &secondString); 37 EcmaString *GetOrInternString(const uint8_t *utf8Data, uint32_t utf8Len, bool canBeCompress); 38 EcmaString *CreateAndInternStringNonMovable(const uint8_t *utf8Data, uint32_t utf8Len); 39 EcmaString *GetOrInternString(const uint16_t *utf16Data, uint32_t utf16Len, bool canBeCompress); 40 EcmaString *GetOrInternString(EcmaString *string); 41 EcmaString *GetOrInternStringWithSpaceType(const uint8_t *utf8Data, uint32_t utf8Len, bool canBeCompress, 42 MemSpaceType type); 43 EcmaString *GetOrInternStringWithSpaceType(const uint16_t *utf16Data, uint32_t utf16Len, bool canBeCompress, 44 MemSpaceType type); 45 46 void SweepWeakReference(const WeakRootVisitor &visitor); 47 bool CheckStringTableValidity(); 48 private: 49 NO_COPY_SEMANTIC(EcmaStringTable); 50 NO_MOVE_SEMANTIC(EcmaStringTable); 51 52 EcmaString *GetString(const JSHandle<EcmaString> &firstString, const JSHandle<EcmaString> &secondString) const; 53 EcmaString *GetString(const uint8_t *utf8Data, uint32_t utf8Len, bool canBeCompress) const; 54 EcmaString *GetString(const uint16_t *utf16Data, uint32_t utf16Len) const; 55 EcmaString *GetString(EcmaString *string) const; 56 57 void InternString(EcmaString *string); 58 InsertStringIfNotExist(EcmaString * string)59 void InsertStringIfNotExist(EcmaString *string) 60 { 61 EcmaString *str = GetString(string); 62 if (str == nullptr) { 63 InternString(string); 64 } 65 } 66 67 CUnorderedMultiMap<uint32_t, EcmaString *> table_; 68 const EcmaVM *vm_{nullptr}; 69 friend class SnapshotProcessor; 70 }; 71 } // namespace panda::ecmascript 72 73 #endif // ECMASCRIPT_STRING_TABLE_H 74