summaryrefslogtreecommitdiff
path: root/js/src/vm/SharedImmutableStringsCache-inl.h
blob: da05444d7e63052b77061373d778e298ebbf69c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef vm_SharedImmutableStringsCache_inl_h
#define vm_SharedImmutableStringsCache_inl_h

#include "vm/SharedImmutableStringsCache.h"

namespace js {

template <typename IntoOwnedChars>
MOZ_MUST_USE mozilla::Maybe<SharedImmutableString>
SharedImmutableStringsCache::getOrCreate(const char* chars, size_t length,
                                         IntoOwnedChars intoOwnedChars)
{
    MOZ_ASSERT(inner_);
    MOZ_ASSERT(chars);
    Hasher::Lookup lookup(Hasher::hashLongString(chars, length), chars, length);

    auto locked = inner_->lock();
    if (!locked->set.initialized() && !locked->set.init())
        return mozilla::Nothing();

    auto entry = locked->set.lookupForAdd(lookup);
    if (!entry) {
        OwnedChars ownedChars(intoOwnedChars());
        if (!ownedChars)
            return mozilla::Nothing();
        MOZ_ASSERT(ownedChars.get() == chars ||
                   memcmp(ownedChars.get(), chars, length) == 0);
        auto box = StringBox::Create(mozilla::Move(ownedChars), length);
        if (!box || !locked->set.add(entry, mozilla::Move(box)))
            return mozilla::Nothing();
    }

    MOZ_ASSERT(entry && *entry);
    return mozilla::Some(SharedImmutableString(locked, entry->get()));
}

template <typename IntoOwnedTwoByteChars>
MOZ_MUST_USE mozilla::Maybe<SharedImmutableTwoByteString>
SharedImmutableStringsCache::getOrCreate(const char16_t* chars, size_t length,
                                         IntoOwnedTwoByteChars intoOwnedTwoByteChars) {
    MOZ_ASSERT(inner_);
    MOZ_ASSERT(chars);
    auto hash = Hasher::hashLongString(reinterpret_cast<const char*>(chars),
                                       length * sizeof(char16_t));
    Hasher::Lookup lookup(hash, chars, length);

    auto locked = inner_->lock();
    if (!locked->set.initialized() && !locked->set.init())
        return mozilla::Nothing();

    auto entry = locked->set.lookupForAdd(lookup);
    if (!entry) {
        OwnedTwoByteChars ownedTwoByteChars(intoOwnedTwoByteChars());
        if (!ownedTwoByteChars)
            return mozilla::Nothing();
        MOZ_ASSERT(ownedTwoByteChars.get() == chars ||
                   memcmp(ownedTwoByteChars.get(), chars, length * sizeof(char16_t)) == 0);
        OwnedChars ownedChars(reinterpret_cast<char*>(ownedTwoByteChars.release()));
        auto box = StringBox::Create(mozilla::Move(ownedChars), length * sizeof(char16_t));
        if (!box || !locked->set.add(entry, mozilla::Move(box)))
            return mozilla::Nothing();
    }

    MOZ_ASSERT(entry && *entry);
    return mozilla::Some(SharedImmutableTwoByteString(locked, entry->get()));
}

} // namespace js

#endif // vm_SharedImmutableStringsCache_inl_h