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 #include "ecmascript/tagged_array-inl.h" 23 24 namespace panda::ecmascript { 25 class EcmaString; 26 class EcmaVM; 27 class JSPandaFile; 28 29 class EcmaStringTable { 30 public: 31 explicit EcmaStringTable(const EcmaVM *vm); ~EcmaStringTable()32 virtual ~EcmaStringTable() 33 { 34 table_.clear(); 35 } 36 37 void InternEmptyString(EcmaString *emptyStr); 38 EcmaString *GetOrInternString(const JSHandle<EcmaString> &firstString, const JSHandle<EcmaString> &secondString); 39 EcmaString *GetOrInternString(const uint8_t *utf8Data, uint32_t utf8Len, bool canBeCompress); 40 EcmaString *CreateAndInternStringNonMovable(const uint8_t *utf8Data, uint32_t utf8Len); 41 EcmaString *GetOrInternString(const uint16_t *utf16Data, uint32_t utf16Len, bool canBeCompress); 42 EcmaString *GetOrInternString(EcmaString *string); 43 EcmaString *GetOrInternStringWithSpaceType(const uint8_t *utf8Data, uint32_t utf8Len, bool canBeCompress, 44 MemSpaceType type, bool isConstantString, uint32_t idOffset); 45 EcmaString *GetOrInternStringWithSpaceType(const uint8_t *utf8Data, uint32_t utf16Len, 46 MemSpaceType type); 47 EcmaString *TryGetInternString(EcmaString *string); 48 EcmaString *InsertStringToTable(const JSHandle<EcmaString> &strHandle); 49 50 void SweepWeakReference(const WeakRootVisitor &visitor); 51 bool CheckStringTableValidity(); 52 void RelocateConstantData(const JSPandaFile *jsPandaFile); 53 private: 54 NO_COPY_SEMANTIC(EcmaStringTable); 55 NO_MOVE_SEMANTIC(EcmaStringTable); 56 57 std::pair<EcmaString *, uint32_t> GetString( 58 const JSHandle<EcmaString> &firstString, const JSHandle<EcmaString> &secondString) const; 59 std::pair<EcmaString *, uint32_t> GetString(const uint8_t *utf8Data, uint32_t utf8Len, bool canBeCompress) const; 60 std::pair<EcmaString *, uint32_t> GetString(const uint16_t *utf16Data, uint32_t utf16Len) const; 61 EcmaString *GetString(EcmaString *string) const; 62 63 void InternString(EcmaString *string); 64 InsertStringIfNotExist(EcmaString * string)65 void InsertStringIfNotExist(EcmaString *string) 66 { 67 EcmaString *str = GetString(string); 68 if (str == nullptr) { 69 InternString(string); 70 } 71 } 72 73 CUnorderedMultiMap<uint32_t, EcmaString *> table_; 74 const EcmaVM *vm_{nullptr}; 75 friend class SnapshotProcessor; 76 friend class BaseDeserializer; 77 }; 78 79 class SingleCharTable : public TaggedArray { 80 public: Cast(TaggedObject * object)81 static SingleCharTable *Cast(TaggedObject *object) 82 { 83 return reinterpret_cast<SingleCharTable*>(object); 84 } 85 static void CreateSingleCharTable(JSThread *thread); GetStringFromSingleCharTable(int32_t ch)86 JSTaggedValue GetStringFromSingleCharTable(int32_t ch) 87 { 88 return Get(ch); 89 } 90 private: 91 static constexpr uint32_t MAX_ONEBYTE_CHARCODE = 128; // 0X00-0X7F 92 }; 93 } // namespace panda::ecmascript 94 95 #endif // ECMASCRIPT_STRING_TABLE_H 96