1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=8 sts=2 et sw=2 tw=80:
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef vm_SharedImmutableStringsCache_inl_h
8 #define vm_SharedImmutableStringsCache_inl_h
9 
10 #include "vm/SharedImmutableStringsCache.h"
11 
12 namespace js {
13 
14 template <typename IntoOwnedChars>
getOrCreate(const char * chars,size_t length,IntoOwnedChars intoOwnedChars)15 [[nodiscard]] SharedImmutableString SharedImmutableStringsCache::getOrCreate(
16     const char* chars, size_t length, IntoOwnedChars intoOwnedChars) {
17   MOZ_ASSERT(inner_);
18   MOZ_ASSERT(chars);
19   Hasher::Lookup lookup(Hasher::hashLongString(chars, length), chars, length);
20 
21   auto locked = inner_->lock();
22   auto entry = locked->set.lookupForAdd(lookup);
23   if (!entry) {
24     OwnedChars ownedChars(intoOwnedChars());
25     if (!ownedChars) {
26       return SharedImmutableString();
27     }
28     MOZ_ASSERT(ownedChars.get() == chars ||
29                memcmp(ownedChars.get(), chars, length) == 0);
30     auto box = StringBox::Create(std::move(ownedChars), length, this->inner_);
31     if (!box || !locked->set.add(entry, std::move(box))) {
32       return SharedImmutableString();
33     }
34   }
35 
36   MOZ_ASSERT(entry && *entry);
37   return SharedImmutableString(entry->get());
38 }
39 
40 template <typename IntoOwnedTwoByteChars>
41 [[nodiscard]] SharedImmutableTwoByteString
getOrCreate(const char16_t * chars,size_t length,IntoOwnedTwoByteChars intoOwnedTwoByteChars)42 SharedImmutableStringsCache::getOrCreate(
43     const char16_t* chars, size_t length,
44     IntoOwnedTwoByteChars intoOwnedTwoByteChars) {
45   MOZ_ASSERT(inner_);
46   MOZ_ASSERT(chars);
47   auto hash = Hasher::hashLongString(reinterpret_cast<const char*>(chars),
48                                      length * sizeof(char16_t));
49   Hasher::Lookup lookup(hash, chars, length);
50 
51   auto locked = inner_->lock();
52   auto entry = locked->set.lookupForAdd(lookup);
53   if (!entry) {
54     OwnedTwoByteChars ownedTwoByteChars(intoOwnedTwoByteChars());
55     if (!ownedTwoByteChars) {
56       return SharedImmutableTwoByteString();
57     }
58     MOZ_ASSERT(
59         ownedTwoByteChars.get() == chars ||
60         memcmp(ownedTwoByteChars.get(), chars, length * sizeof(char16_t)) == 0);
61     OwnedChars ownedChars(reinterpret_cast<char*>(ownedTwoByteChars.release()));
62     auto box = StringBox::Create(std::move(ownedChars),
63                                  length * sizeof(char16_t), this->inner_);
64     if (!box || !locked->set.add(entry, std::move(box))) {
65       return SharedImmutableTwoByteString();
66     }
67   }
68 
69   MOZ_ASSERT(entry && *entry);
70   return SharedImmutableTwoByteString(entry->get());
71 }
72 
73 }  // namespace js
74 
75 #endif  // vm_SharedImmutableStringsCache_inl_h
76