]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/SmallStrings.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / SmallStrings.cpp
CommitLineData
9dae56ea 1/*
14957cd0 2 * Copyright (C) 2008, 2010 Apple Inc. All Rights Reserved.
9dae56ea
A
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "SmallStrings.h"
28
6fe7ccc8 29#include "HeapRootVisitor.h"
9dae56ea
A
30#include "JSGlobalObject.h"
31#include "JSString.h"
81345200 32#include "JSCInlines.h"
9dae56ea 33#include <wtf/Noncopyable.h>
93a37866 34#include <wtf/text/StringImpl.h>
9dae56ea
A
35
36namespace JSC {
9dae56ea 37
14957cd0
A
38class SmallStringsStorage {
39 WTF_MAKE_NONCOPYABLE(SmallStringsStorage); WTF_MAKE_FAST_ALLOCATED;
9dae56ea
A
40public:
41 SmallStringsStorage();
42
14957cd0
A
43 StringImpl* rep(unsigned char character)
44 {
45 return m_reps[character].get();
46 }
9dae56ea
A
47
48private:
14957cd0
A
49 static const unsigned singleCharacterStringCount = maxSingleCharacterString + 1;
50
51 RefPtr<StringImpl> m_reps[singleCharacterStringCount];
9dae56ea
A
52};
53
54SmallStringsStorage::SmallStringsStorage()
55{
6fe7ccc8 56 LChar* characterBuffer = 0;
14957cd0
A
57 RefPtr<StringImpl> baseString = StringImpl::createUninitialized(singleCharacterStringCount, characterBuffer);
58 for (unsigned i = 0; i < singleCharacterStringCount; ++i) {
f9bf01c6 59 characterBuffer[i] = i;
ed1e77d3 60 m_reps[i] = AtomicStringImpl::add(PassRefPtr<StringImpl>(StringImpl::createSubstringSharingImpl(baseString, i, 1)).get());
9dae56ea
A
61 }
62}
63
64SmallStrings::SmallStrings()
6fe7ccc8
A
65 : m_emptyString(0)
66#define JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE(name) , m_##name(0)
67 JSC_COMMON_STRINGS_EACH_NAME(JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE)
68#undef JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE
81345200
A
69 , m_nullObjectString(nullptr)
70 , m_undefinedObjectString(nullptr)
ed1e77d3 71 , m_needsToBeVisited(true)
9dae56ea 72{
14957cd0 73 COMPILE_ASSERT(singleCharacterStringCount == sizeof(m_singleCharacterStrings) / sizeof(m_singleCharacterStrings[0]), IsNumCharactersConstInSyncWithClassUsage);
9dae56ea 74
6fe7ccc8
A
75 for (unsigned i = 0; i < singleCharacterStringCount; ++i)
76 m_singleCharacterStrings[i] = 0;
9dae56ea
A
77}
78
93a37866
A
79void SmallStrings::initializeCommonStrings(VM& vm)
80{
81 createEmptyString(&vm);
81345200
A
82 for (unsigned i = 0; i <= maxSingleCharacterString; ++i)
83 createSingleCharacterString(&vm, i);
93a37866
A
84#define JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE(name) initialize(&vm, m_##name, #name);
85 JSC_COMMON_STRINGS_EACH_NAME(JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE)
86#undef JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE
81345200
A
87 initialize(&vm, m_nullObjectString, "[object Null]");
88 initialize(&vm, m_undefinedObjectString, "[object Undefined]");
93a37866
A
89}
90
91void SmallStrings::visitStrongReferences(SlotVisitor& visitor)
92{
ed1e77d3 93 m_needsToBeVisited = false;
93a37866 94 visitor.appendUnbarrieredPointer(&m_emptyString);
81345200
A
95 for (unsigned i = 0; i <= maxSingleCharacterString; ++i)
96 visitor.appendUnbarrieredPointer(m_singleCharacterStrings + i);
93a37866
A
97#define JSC_COMMON_STRINGS_ATTRIBUTE_VISIT(name) visitor.appendUnbarrieredPointer(&m_##name);
98 JSC_COMMON_STRINGS_EACH_NAME(JSC_COMMON_STRINGS_ATTRIBUTE_VISIT)
99#undef JSC_COMMON_STRINGS_ATTRIBUTE_VISIT
81345200
A
100 visitor.appendUnbarrieredPointer(&m_nullObjectString);
101 visitor.appendUnbarrieredPointer(&m_undefinedObjectString);
93a37866
A
102}
103
6fe7ccc8 104SmallStrings::~SmallStrings()
9dae56ea 105{
9dae56ea
A
106}
107
93a37866 108void SmallStrings::createEmptyString(VM* vm)
9dae56ea
A
109{
110 ASSERT(!m_emptyString);
93a37866 111 m_emptyString = JSString::createHasOtherOwner(*vm, StringImpl::empty());
ed1e77d3 112 m_needsToBeVisited = true;
9dae56ea
A
113}
114
93a37866 115void SmallStrings::createSingleCharacterString(VM* vm, unsigned char character)
9dae56ea
A
116{
117 if (!m_storage)
ed1e77d3 118 m_storage = std::make_unique<SmallStringsStorage>();
9dae56ea 119 ASSERT(!m_singleCharacterStrings[character]);
93a37866 120 m_singleCharacterStrings[character] = JSString::createHasOtherOwner(*vm, PassRefPtr<StringImpl>(m_storage->rep(character)));
ed1e77d3 121 m_needsToBeVisited = true;
9dae56ea
A
122}
123
14957cd0 124StringImpl* SmallStrings::singleCharacterStringRep(unsigned char character)
9dae56ea
A
125{
126 if (!m_storage)
ed1e77d3 127 m_storage = std::make_unique<SmallStringsStorage>();
9dae56ea
A
128 return m_storage->rep(character);
129}
130
ed1e77d3 131void SmallStrings::initialize(VM* vm, JSString*& string, const char* value)
6fe7ccc8 132{
ed1e77d3
A
133 string = JSString::create(*vm, Identifier::fromString(vm, value).impl());
134 m_needsToBeVisited = true;
6fe7ccc8
A
135}
136
9dae56ea 137} // namespace JSC