1 | /* |
2 | * Copyright (C) 2008, 2010 Apple Inc. All Rights Reserved. |
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 | |
29 | #include "JSGlobalObject.h" |
30 | #include "JSString.h" |
31 | #include "JSCInlines.h" |
32 | #include <wtf/Noncopyable.h> |
33 | #include <wtf/text/StringImpl.h> |
34 | |
35 | namespace JSC { |
36 | |
37 | SmallStrings::SmallStrings() |
38 | { |
39 | COMPILE_ASSERT(singleCharacterStringCount == sizeof(m_singleCharacterStrings) / sizeof(m_singleCharacterStrings[0]), IsNumCharactersConstInSyncWithClassUsage); |
40 | |
41 | for (unsigned i = 0; i < singleCharacterStringCount; ++i) |
42 | m_singleCharacterStrings[i] = nullptr; |
43 | } |
44 | |
45 | void SmallStrings::initializeCommonStrings(VM& vm) |
46 | { |
47 | ASSERT(!m_emptyString); |
48 | m_emptyString = JSString::createEmptyString(vm); |
49 | ASSERT(m_needsToBeVisited); |
50 | |
51 | for (unsigned i = 0; i < singleCharacterStringCount; ++i) { |
52 | ASSERT(!m_singleCharacterStrings[i]); |
53 | const LChar string[] = { static_cast<LChar>(i) }; |
54 | m_singleCharacterStrings[i] = JSString::createHasOtherOwner(vm, AtomStringImpl::add(string, 1).releaseNonNull()); |
55 | ASSERT(m_needsToBeVisited); |
56 | } |
57 | |
58 | #define JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE(name) initialize(&vm, m_##name, #name); |
59 | JSC_COMMON_STRINGS_EACH_NAME(JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE) |
60 | #undef JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE |
61 | initialize(&vm, m_objectStringStart, "[object " ); |
62 | initialize(&vm, m_nullObjectString, "[object Null]" ); |
63 | initialize(&vm, m_undefinedObjectString, "[object Undefined]" ); |
64 | |
65 | setIsInitialized(true); |
66 | } |
67 | |
68 | void SmallStrings::visitStrongReferences(SlotVisitor& visitor) |
69 | { |
70 | m_needsToBeVisited = false; |
71 | visitor.appendUnbarriered(m_emptyString); |
72 | for (unsigned i = 0; i <= maxSingleCharacterString; ++i) |
73 | visitor.appendUnbarriered(m_singleCharacterStrings[i]); |
74 | #define JSC_COMMON_STRINGS_ATTRIBUTE_VISIT(name) visitor.appendUnbarriered(m_##name); |
75 | JSC_COMMON_STRINGS_EACH_NAME(JSC_COMMON_STRINGS_ATTRIBUTE_VISIT) |
76 | #undef JSC_COMMON_STRINGS_ATTRIBUTE_VISIT |
77 | visitor.appendUnbarriered(m_objectStringStart); |
78 | visitor.appendUnbarriered(m_nullObjectString); |
79 | visitor.appendUnbarriered(m_undefinedObjectString); |
80 | } |
81 | |
82 | SmallStrings::~SmallStrings() |
83 | { |
84 | } |
85 | |
86 | Ref<StringImpl> SmallStrings::singleCharacterStringRep(unsigned char character) |
87 | { |
88 | if (LIKELY(m_isInitialized)) |
89 | return *const_cast<StringImpl*>(m_singleCharacterStrings[character]->tryGetValueImpl()); |
90 | const LChar string[] = { static_cast<LChar>(character) }; |
91 | return AtomStringImpl::add(string, 1).releaseNonNull(); |
92 | } |
93 | |
94 | void SmallStrings::initialize(VM* vm, JSString*& string, const char* value) |
95 | { |
96 | string = JSString::create(*vm, AtomStringImpl::add(value).releaseNonNull()); |
97 | ASSERT(m_needsToBeVisited); |
98 | } |
99 | |
100 | } // namespace JSC |
101 | |