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