• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ecmascript/ecma_string_table.h"
17 
18 #include "ecmascript/ecma_string-inl.h"
19 #include "ecmascript/ecma_vm.h"
20 #include "ecmascript/js_thread.h"
21 #include "ecmascript/mem/c_string.h"
22 #include "ecmascript/object_factory.h"
23 
24 namespace panda::ecmascript {
EcmaStringTable(const EcmaVM * vm)25 EcmaStringTable::EcmaStringTable(const EcmaVM *vm) : vm_(vm) {}
26 
GetString(const JSHandle<EcmaString> & firstString,const JSHandle<EcmaString> & secondString) const27 EcmaString *EcmaStringTable::GetString(const JSHandle<EcmaString> &firstString,
28                                        const JSHandle<EcmaString> &secondString) const
29 {
30     uint32_t hashCode = firstString->GetHashcode();
31     hashCode = secondString->ComputeHashcode(hashCode);
32     auto range = table_.equal_range(hashCode);
33     auto item = range.first;
34     for (; item != range.second; ++item) {
35         auto foundString = item->second;
36         if (foundString->EqualToSplicedString(*firstString, *secondString)) {
37             return foundString;
38         }
39     }
40     return nullptr;
41 }
42 
GetString(const uint8_t * utf8Data,uint32_t utf8Len,bool canBeCompress) const43 EcmaString *EcmaStringTable::GetString(const uint8_t *utf8Data, uint32_t utf8Len, bool canBeCompress) const
44 {
45     uint32_t hashCode = EcmaString::ComputeHashcodeUtf8(utf8Data, utf8Len, canBeCompress);
46     auto range = table_.equal_range(hashCode);
47     auto item = range.first;
48     for (; item != range.second; ++item) {
49         auto foundString = item->second;
50         if (EcmaString::StringsAreEqualUtf8(foundString, utf8Data, utf8Len, canBeCompress)) {
51             return foundString;
52         }
53     }
54     return nullptr;
55 }
56 
GetString(const uint16_t * utf16Data,uint32_t utf16Len) const57 EcmaString *EcmaStringTable::GetString(const uint16_t *utf16Data, uint32_t utf16Len) const
58 {
59     uint32_t hashCode = EcmaString::ComputeHashcodeUtf16(const_cast<uint16_t *>(utf16Data), utf16Len);
60     auto range = table_.equal_range(hashCode);
61     auto item = range.first;
62     for (; item != range.second; ++item) {
63         auto foundString = item->second;
64         if (EcmaString::StringsAreEqualUtf16(foundString, utf16Data, utf16Len)) {
65             return foundString;
66         }
67     }
68     return nullptr;
69 }
70 
GetString(EcmaString * string) const71 EcmaString *EcmaStringTable::GetString(EcmaString *string) const
72 {
73     auto range = table_.equal_range(string->GetHashcode());
74     auto item = range.first;
75     for (; item != range.second; ++item) {
76         auto foundString = item->second;
77         if (EcmaString::StringsAreEqual(foundString, string)) {
78             return foundString;
79         }
80     }
81     return nullptr;
82 }
83 
InternString(EcmaString * string)84 void EcmaStringTable::InternString(EcmaString *string)
85 {
86     if (string->IsInternString()) {
87         return;
88     }
89     table_.insert(std::pair<uint32_t, EcmaString *>(string->GetHashcode(), string));
90     string->SetIsInternString();
91 }
92 
InternEmptyString(EcmaString * emptyStr)93 void EcmaStringTable::InternEmptyString(EcmaString *emptyStr)
94 {
95     InternString(emptyStr);
96 }
97 
GetOrInternString(const JSHandle<EcmaString> & firstString,const JSHandle<EcmaString> & secondString)98 EcmaString *EcmaStringTable::GetOrInternString(const JSHandle<EcmaString> &firstString,
99                                                const JSHandle<EcmaString> &secondString)
100 {
101     EcmaString *concatString = GetString(firstString, secondString);
102     if (concatString != nullptr) {
103         return concatString;
104     }
105 
106     concatString = EcmaString::Concat(firstString, secondString, vm_);
107 
108     InternString(concatString);
109     return concatString;
110 }
111 
GetOrInternString(const uint8_t * utf8Data,uint32_t utf8Len,bool canBeCompress)112 EcmaString *EcmaStringTable::GetOrInternString(const uint8_t *utf8Data, uint32_t utf8Len, bool canBeCompress)
113 {
114     EcmaString *result = GetString(utf8Data, utf8Len, canBeCompress);
115     if (result != nullptr) {
116         return result;
117     }
118 
119     result = EcmaString::CreateFromUtf8(utf8Data, utf8Len, vm_, canBeCompress);
120     InternString(result);
121     return result;
122 }
123 
GetOrInternString(const uint16_t * utf16Data,uint32_t utf16Len,bool canBeCompress)124 EcmaString *EcmaStringTable::GetOrInternString(const uint16_t *utf16Data, uint32_t utf16Len, bool canBeCompress)
125 {
126     EcmaString *result = GetString(utf16Data, utf16Len);
127     if (result != nullptr) {
128         return result;
129     }
130 
131     result = EcmaString::CreateFromUtf16(utf16Data, utf16Len, vm_, canBeCompress);
132     InternString(result);
133     return result;
134 }
135 
GetOrInternString(EcmaString * string)136 EcmaString *EcmaStringTable::GetOrInternString(EcmaString *string)
137 {
138     if (string->IsInternString()) {
139         return string;
140     }
141 
142     EcmaString *result = GetString(string);
143     if (result != nullptr) {
144         return result;
145     }
146     InternString(string);
147     return string;
148 }
149 
SweepWeakReference(const WeakRootVisitor & visitor)150 void EcmaStringTable::SweepWeakReference(const WeakRootVisitor &visitor)
151 {
152     for (auto it = table_.begin(); it != table_.end();) {
153         auto *object = it->second;
154         auto fwd = visitor(object);
155         if (fwd == nullptr) {
156             LOG(DEBUG, GC) << "StringTable: delete string " << std::hex << object
157                            << ", val = " << ConvertToString(object);
158             table_.erase(it++);
159         } else if (fwd != object) {
160             it->second = static_cast<EcmaString *>(fwd);
161             ++it;
162             LOG(DEBUG, GC) << "StringTable: forward " << std::hex << object << " -> " << fwd;
163         } else {
164             ++it;
165         }
166     }
167 }
168 }  // namespace panda::ecmascript
169