summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/PrimitiveString.cpp
blob: 063c518f4acaf0cfce40697b4be49086ceaaceb1 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/CharacterTypes.h>
#include <AK/Utf16View.h>
#include <LibJS/Runtime/PrimitiveString.h>
#include <LibJS/Runtime/VM.h>

namespace JS {

PrimitiveString::PrimitiveString(String string)
    : m_utf8_string(move(string))
    , m_has_utf8_string(true)
{
}

PrimitiveString::PrimitiveString(Utf16String string)
    : m_utf16_string(move(string))
    , m_has_utf16_string(true)
{
}

PrimitiveString::~PrimitiveString()
{
    vm().string_cache().remove(m_utf8_string);
}

String const& PrimitiveString::string() const
{
    if (!m_has_utf8_string) {
        m_utf8_string = m_utf16_string.to_utf8();
        m_has_utf8_string = true;
    }
    return m_utf8_string;
}

Utf16String const& PrimitiveString::utf16_string() const
{
    if (!m_has_utf16_string) {
        m_utf16_string = Utf16String(m_utf8_string);
        m_has_utf16_string = true;
    }
    return m_utf16_string;
}

Utf16View PrimitiveString::utf16_string_view() const
{
    return utf16_string().view();
}

PrimitiveString* js_string(Heap& heap, Utf16View const& view)
{
    return js_string(heap, Utf16String(view));
}

PrimitiveString* js_string(VM& vm, Utf16View const& view)
{
    return js_string(vm.heap(), view);
}

PrimitiveString* js_string(Heap& heap, Utf16String string)
{
    if (string.is_empty())
        return &heap.vm().empty_string();

    if (string.length_in_code_units() == 1) {
        u16 code_unit = string.code_unit_at(0);
        if (is_ascii(code_unit))
            return &heap.vm().single_ascii_character_string(static_cast<u8>(code_unit));
    }

    return heap.allocate_without_global_object<PrimitiveString>(move(string));
}

PrimitiveString* js_string(VM& vm, Utf16String string)
{
    return js_string(vm.heap(), move(string));
}

PrimitiveString* js_string(Heap& heap, String string)
{
    if (string.is_empty())
        return &heap.vm().empty_string();

    if (string.length() == 1) {
        auto ch = static_cast<u8>(string.characters()[0]);
        if (is_ascii(ch))
            return &heap.vm().single_ascii_character_string(ch);
    }

    auto& string_cache = heap.vm().string_cache();
    auto it = string_cache.find(string);
    if (it == string_cache.end()) {
        auto* new_string = heap.allocate_without_global_object<PrimitiveString>(string);
        string_cache.set(move(string), new_string);
        return new_string;
    }
    return it->value;
}

PrimitiveString* js_string(VM& vm, String string)
{
    return js_string(vm.heap(), move(string));
}

}