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