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 ASSERT(EcmaStringAccessor(firstString).IsLineOrConstantString());
31 ASSERT(EcmaStringAccessor(secondString).IsLineOrConstantString());
32 uint32_t hashCode = EcmaStringAccessor(firstString).GetHashcode();
33 hashCode = EcmaStringAccessor(secondString).ComputeHashcode(hashCode);
34 auto range = table_.equal_range(hashCode);
35 for (auto item = range.first; item != range.second; ++item) {
36 auto foundString = item->second;
37 if (EcmaStringAccessor(foundString).EqualToSplicedString(*firstString, *secondString)) {
38 return foundString;
39 }
40 }
41 return nullptr;
42 }
43
GetString(const uint8_t * utf8Data,uint32_t utf8Len,bool canBeCompress) const44 EcmaString *EcmaStringTable::GetString(const uint8_t *utf8Data, uint32_t utf8Len, bool canBeCompress) const
45 {
46 uint32_t hashCode = EcmaStringAccessor::ComputeHashcodeUtf8(utf8Data, utf8Len, canBeCompress);
47 auto range = table_.equal_range(hashCode);
48 for (auto item = range.first; item != range.second; ++item) {
49 auto foundString = item->second;
50 if (EcmaStringAccessor::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 = EcmaStringAccessor::ComputeHashcodeUtf16(const_cast<uint16_t *>(utf16Data), utf16Len);
60 auto range = table_.equal_range(hashCode);
61 for (auto item = range.first; item != range.second; ++item) {
62 auto foundString = item->second;
63 if (EcmaStringAccessor::StringsAreEqualUtf16(foundString, utf16Data, utf16Len)) {
64 return foundString;
65 }
66 }
67 return nullptr;
68 }
69
GetString(EcmaString * string) const70 EcmaString *EcmaStringTable::GetString(EcmaString *string) const
71 {
72 ASSERT(EcmaStringAccessor(string).IsLineOrConstantString());
73 auto hashcode = EcmaStringAccessor(string).GetHashcode();
74 auto range = table_.equal_range(hashcode);
75 for (auto item = range.first; item != range.second; ++item) {
76 auto foundString = item->second;
77 if (EcmaStringAccessor::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 (EcmaStringAccessor(string).IsInternString()) {
87 return;
88 }
89 // Strings in string table should not be in the young space.
90 ASSERT(!Region::ObjectAddressToRange(reinterpret_cast<TaggedObject *>(string))->InYoungSpace());
91 ASSERT(EcmaStringAccessor(string).IsLineOrConstantString());
92 auto hashcode = EcmaStringAccessor(string).GetHashcode();
93 table_.emplace(hashcode, string);
94 EcmaStringAccessor(string).SetInternString();
95 }
96
InternEmptyString(EcmaString * emptyStr)97 void EcmaStringTable::InternEmptyString(EcmaString *emptyStr)
98 {
99 InternString(emptyStr);
100 }
101
GetOrInternString(const JSHandle<EcmaString> & firstString,const JSHandle<EcmaString> & secondString)102 EcmaString *EcmaStringTable::GetOrInternString(const JSHandle<EcmaString> &firstString,
103 const JSHandle<EcmaString> &secondString)
104 {
105 auto firstFlat = JSHandle<EcmaString>(vm_->GetJSThread(), EcmaStringAccessor::Flatten(vm_, firstString));
106 auto secondFlat = JSHandle<EcmaString>(vm_->GetJSThread(), EcmaStringAccessor::Flatten(vm_, secondString));
107 EcmaString *concatString = GetString(firstFlat, secondFlat);
108 if (concatString != nullptr) {
109 return concatString;
110 }
111 JSHandle<EcmaString> concatHandle(vm_->GetJSThread(),
112 EcmaStringAccessor::Concat(vm_, firstFlat, secondFlat, MemSpaceType::OLD_SPACE));
113 concatString = EcmaStringAccessor::Flatten(vm_, concatHandle, MemSpaceType::OLD_SPACE);
114 InternString(concatString);
115 return concatString;
116 }
117
GetOrInternString(const uint8_t * utf8Data,uint32_t utf8Len,bool canBeCompress)118 EcmaString *EcmaStringTable::GetOrInternString(const uint8_t *utf8Data, uint32_t utf8Len, bool canBeCompress)
119 {
120 EcmaString *result = GetString(utf8Data, utf8Len, canBeCompress);
121 if (result != nullptr) {
122 return result;
123 }
124
125 result = EcmaStringAccessor::CreateFromUtf8(vm_, utf8Data, utf8Len, canBeCompress, MemSpaceType::OLD_SPACE);
126 InternString(result);
127 return result;
128 }
129
130 /*
131 This function is used to create global constant strings from non-movable sapce only.
132 It only inserts string into string-table and provides no string-table validity check.
133 */
CreateAndInternStringNonMovable(const uint8_t * utf8Data,uint32_t utf8Len)134 EcmaString *EcmaStringTable::CreateAndInternStringNonMovable(const uint8_t *utf8Data, uint32_t utf8Len)
135 {
136 EcmaString *result = GetString(utf8Data, utf8Len, true);
137 if (result != nullptr) {
138 return result;
139 }
140
141 result = EcmaStringAccessor::CreateFromUtf8(vm_, utf8Data, utf8Len, true, MemSpaceType::NON_MOVABLE);
142 InternString(result);
143 return result;
144 }
145
GetOrInternString(const uint16_t * utf16Data,uint32_t utf16Len,bool canBeCompress)146 EcmaString *EcmaStringTable::GetOrInternString(const uint16_t *utf16Data, uint32_t utf16Len, bool canBeCompress)
147 {
148 EcmaString *result = GetString(utf16Data, utf16Len);
149 if (result != nullptr) {
150 return result;
151 }
152
153 result = EcmaStringAccessor::CreateFromUtf16(vm_, utf16Data, utf16Len, canBeCompress, MemSpaceType::OLD_SPACE);
154 InternString(result);
155 return result;
156 }
157
GetOrInternString(EcmaString * string)158 EcmaString *EcmaStringTable::GetOrInternString(EcmaString *string)
159 {
160 if (EcmaStringAccessor(string).IsInternString()) {
161 return string;
162 }
163 JSHandle<EcmaString> strHandle(vm_->GetJSThread(), string);
164 // may gc
165 auto strFlat = EcmaStringAccessor::Flatten(vm_, strHandle, MemSpaceType::OLD_SPACE);
166 if (EcmaStringAccessor(strFlat).IsInternString()) {
167 return strFlat;
168 }
169 EcmaString *result = GetString(strFlat);
170 if (result != nullptr) {
171 return result;
172 }
173
174 if (EcmaStringAccessor(strFlat).IsLineOrConstantString()) {
175 Region *objectRegion = Region::ObjectAddressToRange(reinterpret_cast<TaggedObject *>(strFlat));
176 if (objectRegion->InYoungSpace()) {
177 JSHandle<EcmaString> resultHandle(vm_->GetJSThread(), strFlat);
178 strFlat = EcmaStringAccessor::CopyStringToOldSpace(vm_,
179 resultHandle, EcmaStringAccessor(strFlat).GetLength(), EcmaStringAccessor(strFlat).IsUtf8());
180 }
181 }
182 InternString(strFlat);
183 return strFlat;
184 }
185
GetOrInternStringWithSpaceType(const uint8_t * utf8Data,uint32_t utf8Len,bool canBeCompress,MemSpaceType type,bool isConstantString,uint32_t idOffset)186 EcmaString *EcmaStringTable::GetOrInternStringWithSpaceType(const uint8_t *utf8Data, uint32_t utf8Len,
187 bool canBeCompress, MemSpaceType type,
188 bool isConstantString, uint32_t idOffset)
189 {
190 EcmaString *result = GetString(utf8Data, utf8Len, canBeCompress);
191 if (result != nullptr) {
192 return result;
193 }
194 type = type == MemSpaceType::NON_MOVABLE ? MemSpaceType::NON_MOVABLE : MemSpaceType::OLD_SPACE;
195 if (canBeCompress) {
196 // Constant string will be created in this branch.
197 result = EcmaStringAccessor::CreateFromUtf8(vm_, utf8Data, utf8Len, canBeCompress, type, isConstantString,
198 idOffset);
199 } else {
200 result = EcmaStringAccessor::CreateFromUtf8(vm_, utf8Data, utf8Len, canBeCompress, type);
201 }
202 InternString(result);
203 return result;
204 }
205
GetOrInternStringWithSpaceType(const uint16_t * utf16Data,uint32_t utf16Len,bool canBeCompress,MemSpaceType type)206 EcmaString *EcmaStringTable::GetOrInternStringWithSpaceType(const uint16_t *utf16Data, uint32_t utf16Len,
207 bool canBeCompress, MemSpaceType type)
208 {
209 EcmaString *result = GetString(utf16Data, utf16Len);
210 if (result != nullptr) {
211 return result;
212 }
213 type = type == MemSpaceType::NON_MOVABLE ? MemSpaceType::NON_MOVABLE : MemSpaceType::OLD_SPACE;
214 result = EcmaStringAccessor::CreateFromUtf16(vm_, utf16Data, utf16Len, canBeCompress, type);
215 InternString(result);
216 return result;
217 }
218
SweepWeakReference(const WeakRootVisitor & visitor)219 void EcmaStringTable::SweepWeakReference(const WeakRootVisitor &visitor)
220 {
221 for (auto it = table_.begin(); it != table_.end();) {
222 // Strings in string table should not be in the young space. Only old gc will sweep string table.
223 auto *object = it->second;
224 auto fwd = visitor(object);
225 ASSERT(!Region::ObjectAddressToRange(object)->InYoungSpace());
226 if (fwd == nullptr) {
227 LOG_ECMA(VERBOSE) << "StringTable: delete string " << std::hex << object;
228 table_.erase(it++);
229 } else if (fwd != object) {
230 it->second = static_cast<EcmaString *>(fwd);
231 ++it;
232 LOG_ECMA(VERBOSE) << "StringTable: forward " << std::hex << object << " -> " << fwd;
233 } else {
234 ++it;
235 }
236 }
237 }
238
CheckStringTableValidity()239 bool EcmaStringTable::CheckStringTableValidity()
240 {
241 for (auto itemOuter = table_.begin(); itemOuter != table_.end(); ++itemOuter) {
242 auto outerString = itemOuter->second;
243 if (!EcmaStringAccessor(outerString).IsLineOrConstantString()) {
244 return false;
245 }
246 int counter = 0;
247 auto hashcode = EcmaStringAccessor(outerString).GetHashcode();
248 auto range = table_.equal_range(hashcode);
249 auto it = range.first;
250 for (; it != range.second; ++it) {
251 auto foundString = it->second;
252 if (EcmaStringAccessor::StringsAreEqual(foundString, outerString)) {
253 ++counter;
254 }
255 }
256 if (counter > 1) {
257 return false;
258 }
259 }
260 return true;
261 }
262 } // namespace panda::ecmascript
263