1/*
2 * Copyright (C) 2012-2017 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#pragma once
27
28#include "ExceptionHelpers.h"
29#include "JSCJSValue.h"
30#include "JSGlobalObject.h"
31
32namespace JSC {
33
34class JSStringJoiner {
35public:
36 JSStringJoiner(JSGlobalObject*, LChar separator, unsigned stringCount);
37 JSStringJoiner(JSGlobalObject*, StringView separator, unsigned stringCount);
38 ~JSStringJoiner();
39
40 void append(JSGlobalObject*, JSValue);
41 void appendNumber(VM&, int32_t);
42 void appendNumber(VM&, double);
43 bool appendWithoutSideEffects(JSGlobalObject*, JSValue);
44 void appendEmptyString();
45
46 JSValue join(JSGlobalObject*);
47
48private:
49 void append(StringViewWithUnderlyingString&&);
50 void append8Bit(const String&);
51 void appendLiteral(const Identifier&);
52 unsigned joinedLength(JSGlobalObject*) const;
53
54 LChar m_singleCharacterSeparator;
55 StringView m_separator;
56 Vector<StringViewWithUnderlyingString> m_strings;
57 Checked<unsigned, RecordOverflow> m_accumulatedStringsLength;
58 bool m_isAll8Bit { true };
59};
60
61inline JSStringJoiner::JSStringJoiner(JSGlobalObject* globalObject, StringView separator, unsigned stringCount)
62 : m_separator(separator)
63 , m_isAll8Bit(m_separator.is8Bit())
64{
65 VM& vm = globalObject->vm();
66 auto scope = DECLARE_THROW_SCOPE(vm);
67 if (UNLIKELY(!m_strings.tryReserveCapacity(stringCount)))
68 throwOutOfMemoryError(globalObject, scope);
69}
70
71inline JSStringJoiner::JSStringJoiner(JSGlobalObject* globalObject, LChar separator, unsigned stringCount)
72 : m_singleCharacterSeparator(separator)
73 , m_separator { &m_singleCharacterSeparator, 1 }
74{
75 VM& vm = globalObject->vm();
76 auto scope = DECLARE_THROW_SCOPE(vm);
77 if (UNLIKELY(!m_strings.tryReserveCapacity(stringCount)))
78 throwOutOfMemoryError(globalObject, scope);
79}
80
81ALWAYS_INLINE void JSStringJoiner::append(StringViewWithUnderlyingString&& string)
82{
83 m_accumulatedStringsLength += string.view.length();
84 m_isAll8Bit = m_isAll8Bit && string.view.is8Bit();
85 m_strings.uncheckedAppend(WTFMove(string));
86}
87
88ALWAYS_INLINE void JSStringJoiner::append8Bit(const String& string)
89{
90 ASSERT(string.is8Bit());
91 m_accumulatedStringsLength += string.length();
92 m_strings.uncheckedAppend({ string, string });
93}
94
95ALWAYS_INLINE void JSStringJoiner::appendLiteral(const Identifier& literal)
96{
97 m_accumulatedStringsLength += literal.length();
98 ASSERT(literal.string().is8Bit());
99 m_strings.uncheckedAppend({ literal.string(), { } });
100}
101
102ALWAYS_INLINE void JSStringJoiner::appendEmptyString()
103{
104 m_strings.uncheckedAppend({ { }, { } });
105}
106
107ALWAYS_INLINE bool JSStringJoiner::appendWithoutSideEffects(JSGlobalObject* globalObject, JSValue value)
108{
109 // The following code differs from using the result of JSValue::toString in the following ways:
110 // 1) It's inlined more than JSValue::toString is.
111 // 2) It includes conversion to WTF::String in a way that avoids allocating copies of substrings.
112 // 3) It doesn't create a JSString for numbers, true, or false.
113 // 4) It turns undefined and null into the empty string instead of "undefined" and "null".
114 // 5) It uses optimized code paths for all the cases known to be 8-bit and for the empty string.
115 // If we might make an effectful calls, return false. Otherwise return true.
116
117 if (value.isCell()) {
118 JSString* jsString;
119 if (!value.asCell()->isString())
120 return false;
121 jsString = asString(value);
122 append(jsString->viewWithUnderlyingString(globalObject));
123 return true;
124 }
125
126 if (value.isInt32()) {
127 appendNumber(globalObject->vm(), value.asInt32());
128 return true;
129 }
130 if (value.isDouble()) {
131 appendNumber(globalObject->vm(), value.asDouble());
132 return true;
133 }
134 if (value.isTrue()) {
135 append8Bit(globalObject->vm().propertyNames->trueKeyword.string());
136 return true;
137 }
138 if (value.isFalse()) {
139 append8Bit(globalObject->vm().propertyNames->falseKeyword.string());
140 return true;
141 }
142 ASSERT(value.isUndefinedOrNull());
143 appendEmptyString();
144 return true;
145}
146
147ALWAYS_INLINE void JSStringJoiner::append(JSGlobalObject* globalObject, JSValue value)
148{
149 if (!appendWithoutSideEffects(globalObject, value)) {
150 JSString* jsString = value.toString(globalObject);
151 append(jsString->viewWithUnderlyingString(globalObject));
152 }
153}
154
155ALWAYS_INLINE void JSStringJoiner::appendNumber(VM& vm, int32_t value)
156{
157 append8Bit(vm.numericStrings.add(value));
158}
159
160ALWAYS_INLINE void JSStringJoiner::appendNumber(VM& vm, double value)
161{
162 append8Bit(vm.numericStrings.add(value));
163}
164
165} // namespace JSC
166