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