| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "util/StringBuilder.h" |
| 8 |
|
| 9 |
#include "mozilla/Latin1.h" |
| 10 |
#include "mozilla/Range.h" |
| 11 |
|
| 12 |
#include <algorithm> |
| 13 |
|
| 14 |
#include "frontend/ParserAtom.h" // frontend::{ParserAtomsTable, TaggedParserAtomIndex |
| 15 |
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_* |
| 16 |
#include "vm/BigIntType.h" |
| 17 |
#include "vm/StaticStrings.h" |
| 18 |
|
| 19 |
#include "vm/JSObject-inl.h" |
| 20 |
#include "vm/StringType-inl.h" |
| 21 |
|
| 22 |
using namespace js; |
| 23 |
|
| 24 |
template <typename CharT, class Buffer> |
| 25 |
static CharT* ExtractWellSized(Buffer& cb) { |
| 26 |
size_t capacity = cb.capacity(); |
| 27 |
size_t length = cb.length(); |
| 28 |
StringBuilderAllocPolicy allocPolicy = cb.allocPolicy(); |
| 29 |
|
| 30 |
CharT* buf = cb.extractOrCopyRawBuffer(); |
| 31 |
if (!buf) { |
| 32 |
return nullptr; |
| 33 |
} |
| 34 |
|
| 35 |
// For medium/big buffers, avoid wasting more than 1/4 of the memory. Very |
| 36 |
// small strings will not reach here because they will have been stored in a |
| 37 |
// JSInlineString. Don't bother shrinking the allocation unless at least 80 |
| 38 |
// bytes will be saved, which is a somewhat arbitrary number (though it does |
| 39 |
// correspond to a mozjemalloc size class.) |
| 40 |
MOZ_ASSERT(capacity >= length); |
| 41 |
constexpr size_t minCharsToReclaim = 80 / sizeof(CharT); |
| 42 |
if (capacity - length >= minCharsToReclaim && |
| 43 |
capacity - length > capacity / 4) { |
| 44 |
CharT* tmp = allocPolicy.pod_realloc<CharT>(buf, capacity, length); |
| 45 |
if (!tmp) { |
| 46 |
allocPolicy.free_(buf); |
| 47 |
return nullptr; |
| 48 |
} |
| 49 |
buf = tmp; |
| 50 |
} |
| 51 |
|
| 52 |
return buf; |
| 53 |
} |
| 54 |
|
| 55 |
char16_t* StringBuilder::stealChars() { |
| 56 |
// stealChars shouldn't be used with JSStringBuilder because JSStringBuilder |
| 57 |
// reserves space for the header bytes in the vector. |
| 58 |
MOZ_RELEASE_ASSERT(numHeaderChars_ == 0); |
| 59 |
|
| 60 |
if (isLatin1() && !inflateChars()) { |
| 61 |
return nullptr; |
| 62 |
} |
| 63 |
|
| 64 |
return ExtractWellSized<char16_t>(twoByteChars()); |
| 65 |
} |
| 66 |
|
| 67 |
bool StringBuilder::inflateChars() { |
| 68 |
MOZ_ASSERT(isLatin1()); |
| 69 |
|
| 70 |
TwoByteCharBuffer twoByte(latin1Chars().allocPolicy()); |
| 71 |
|
| 72 |
// Note: each char16_t is two bytes, so we need to change the number of header |
| 73 |
// characters. |
| 74 |
MOZ_ASSERT(numHeaderChars_ == 0 || |
| 75 |
numHeaderChars_ == numHeaderChars<Latin1Char>()); |
| 76 |
MOZ_ASSERT(latin1Chars().length() >= numHeaderChars_); |
| 77 |
size_t numHeaderCharsNew = |
| 78 |
numHeaderChars_ > 0 ? numHeaderChars<char16_t>() : 0; |
| 79 |
|
| 80 |
/* |
| 81 |
* Note: we don't use Vector::capacity() because it always returns a |
| 82 |
* value >= sInlineCapacity. Since Latin1CharBuffer::sInlineCapacity > |
| 83 |
* TwoByteCharBuffer::sInlineCapacitychars, we'd always malloc here. |
| 84 |
*/ |
| 85 |
size_t reserved = reservedExclHeader_ + numHeaderChars_; |
| 86 |
size_t capacity = std::max(reserved, latin1Chars().length()); |
| 87 |
capacity = capacity - numHeaderChars_ + numHeaderCharsNew; |
| 88 |
if (!twoByte.reserve(capacity)) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
twoByte.infallibleAppendN('\0', numHeaderCharsNew); |
| 93 |
|
| 94 |
auto charsSource = mozilla::AsChars(latin1Chars()).From(numHeaderChars_); |
| 95 |
twoByte.infallibleGrowByUninitialized(charsSource.Length()); |
| 96 |
|
| 97 |
auto charsDest = mozilla::Span<char16_t>(twoByte).From(numHeaderCharsNew); |
| 98 |
mozilla::ConvertLatin1toUtf16(charsSource, charsDest); |
| 99 |
|
| 100 |
MOZ_ASSERT(twoByte.length() == numHeaderCharsNew + length()); |
| 101 |
|
| 102 |
cb.destroy(); |
| 103 |
cb.construct<TwoByteCharBuffer>(std::move(twoByte)); |
| 104 |
numHeaderChars_ = numHeaderCharsNew; |
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
bool StringBuilder::append(const frontend::ParserAtomsTable& parserAtoms, |
| 109 |
frontend::TaggedParserAtomIndex atom) { |
| 110 |
return parserAtoms.appendTo(*this, atom); |
| 111 |
} |
| 112 |
|
| 113 |
template <typename CharT> |
| 114 |
JSLinearString* StringBuilder::finishStringInternal(JSContext* cx, |
| 115 |
gc::Heap heap) { |
| 116 |
// The Vector must include space for the mozilla::StringBuffer header. |
| 117 |
MOZ_ASSERT(numHeaderChars_ == numHeaderChars<CharT>()); |
| 118 |
#ifdef DEBUG |
| 119 |
auto isZeroChar = [](CharT c) { return c == '\0'; }; |
| 120 |
MOZ_ASSERT(std::all_of(chars<CharT>().begin(), |
| 121 |
chars<CharT>().begin() + numHeaderChars_, isZeroChar)); |
| 122 |
#endif |
| 123 |
|
| 124 |
size_t len = length(); |
| 125 |
|
| 126 |
if (JSAtom* staticStr = cx->staticStrings().lookup(begin<CharT>(), len)) { |
| 127 |
return staticStr; |
| 128 |
} |
| 129 |
|
| 130 |
if (JSInlineString::lengthFits<CharT>(len)) { |
| 131 |
mozilla::Range<const CharT> range(begin<CharT>(), len); |
| 132 |
return NewInlineString<CanGC>(cx, range); |
| 133 |
} |
| 134 |
|
| 135 |
// Use NewStringCopyNDontDeflate if the string is too short for a buffer, |
| 136 |
// because: |
| 137 |
// |
| 138 |
// (1) If the string is very short and fits in the Vector's inline storage, |
| 139 |
// we can potentially nursery-allocate the characters and avoid a malloc |
| 140 |
// call. |
| 141 |
// (2) ExtractWellSized often performs a realloc because we over-allocate in |
| 142 |
// StringBufferAllocPolicy. After this we'd still have to move/copy the |
| 143 |
// characters in memory to discard the space we reserved for the |
| 144 |
// mozilla::StringBuffer header. Because we have to copy the characters |
| 145 |
// anyway, use NewStringCopyNDontDeflate instead where we can allocate in |
| 146 |
// the nursery. |
| 147 |
if (len < JSString::MIN_BYTES_FOR_BUFFER / sizeof(CharT)) { |
| 148 |
return NewStringCopyNDontDeflate<CanGC>(cx, begin<CharT>(), len, heap); |
| 149 |
} |
| 150 |
|
| 151 |
if (MOZ_UNLIKELY(!mozilla::StringBuffer::IsValidLength<CharT>(len))) { |
| 152 |
ReportAllocationOverflow(cx); |
| 153 |
return nullptr; |
| 154 |
} |
| 155 |
|
| 156 |
// mozilla::StringBuffer requires a null terminator. |
| 157 |
auto& charsWithHeader = chars<CharT>(); |
| 158 |
if (!charsWithHeader.append('\0')) { |
| 159 |
return nullptr; |
| 160 |
} |
| 161 |
|
| 162 |
CharT* mem = ExtractWellSized<CharT>(charsWithHeader); |
| 163 |
if (!mem) { |
| 164 |
return nullptr; |
| 165 |
} |
| 166 |
// The Vector is now empty and may be used again, so re-reserve space for |
| 167 |
// the header. |
| 168 |
MOZ_ASSERT(charsWithHeader.empty()); |
| 169 |
MOZ_ALWAYS_TRUE(charsWithHeader.appendN('\0', numHeaderChars_)); |
| 170 |
|
| 171 |
// Initialize the StringBuffer header. |
| 172 |
RefPtr<mozilla::StringBuffer> buffer = |
| 173 |
mozilla::StringBuffer::ConstructInPlace(mem, (len + 1) * sizeof(CharT)); |
| 174 |
MOZ_ASSERT(buffer->Data() == mem + numHeaderChars_, |
| 175 |
"chars are where mozilla::StringBuffer expects them"); |
| 176 |
MOZ_ASSERT(static_cast<CharT*>(buffer->Data())[len] == '\0', |
| 177 |
"StringBuffer must be null-terminated"); |
| 178 |
|
| 179 |
Rooted<JSString::OwnedChars<CharT>> owned(cx, std::move(buffer), len); |
| 180 |
return JSLinearString::new_<CanGC, CharT>(cx, &owned, heap); |
| 181 |
} |
| 182 |
|
| 183 |
JSLinearString* JSStringBuilder::finishString(gc::Heap heap) { |
| 184 |
MOZ_ASSERT(maybeCx_); |
| 185 |
|
| 186 |
size_t len = length(); |
| 187 |
if (len == 0) { |
| 188 |
return maybeCx_->names().empty_; |
| 189 |
} |
| 190 |
|
| 191 |
if (MOZ_UNLIKELY(!JSString::validateLength(maybeCx_, len))) { |
| 192 |
return nullptr; |
| 193 |
} |
| 194 |
|
| 195 |
static_assert(JSFatInlineString::MAX_LENGTH_TWO_BYTE < |
| 196 |
TwoByteCharBuffer::InlineLength); |
| 197 |
static_assert(JSFatInlineString::MAX_LENGTH_LATIN1 < |
| 198 |
Latin1CharBuffer::InlineLength); |
| 199 |
|
| 200 |
return isLatin1() ? finishStringInternal<Latin1Char>(maybeCx_, heap) |
| 201 |
: finishStringInternal<char16_t>(maybeCx_, heap); |
| 202 |
} |
| 203 |
|
| 204 |
JSAtom* StringBuilder::finishAtom() { |
| 205 |
MOZ_ASSERT(maybeCx_); |
| 206 |
|
| 207 |
size_t len = length(); |
| 208 |
if (len == 0) { |
| 209 |
return maybeCx_->names().empty_; |
| 210 |
} |
| 211 |
|
| 212 |
JSAtom* atom = isLatin1() ? AtomizeChars(maybeCx_, rawLatin1Begin(), len) |
| 213 |
: AtomizeChars(maybeCx_, rawTwoByteBegin(), len); |
| 214 |
clear(); |
| 215 |
return atom; |
| 216 |
} |
| 217 |
|
| 218 |
frontend::TaggedParserAtomIndex StringBuilder::finishParserAtom( |
| 219 |
frontend::ParserAtomsTable& parserAtoms, FrontendContext* fc) { |
| 220 |
size_t len = length(); |
| 221 |
if (len == 0) { |
| 222 |
return frontend::TaggedParserAtomIndex::WellKnown::empty(); |
| 223 |
} |
| 224 |
|
| 225 |
auto result = isLatin1() |
| 226 |
? parserAtoms.internLatin1(fc, rawLatin1Begin(), len) |
| 227 |
: parserAtoms.internChar16(fc, rawTwoByteBegin(), len); |
| 228 |
clear(); |
| 229 |
return result; |
| 230 |
} |
| 231 |
|
| 232 |
bool js::ValueToStringBuilderSlow(JSContext* cx, const Value& arg, |
| 233 |
StringBuilder& sb) { |
| 234 |
RootedValue v(cx, arg); |
| 235 |
if (!ToPrimitive(cx, JSTYPE_STRING, &v)) { |
| 236 |
return false; |
| 237 |
} |
| 238 |
|
| 239 |
if (v.isString()) { |
| 240 |
return sb.append(v.toString()); |
| 241 |
} |
| 242 |
if (v.isNumber()) { |
| 243 |
return NumberValueToStringBuilder(v, sb); |
| 244 |
} |
| 245 |
if (v.isBoolean()) { |
| 246 |
return BooleanToStringBuilder(v.toBoolean(), sb); |
| 247 |
} |
| 248 |
if (v.isNull()) { |
| 249 |
return sb.append(cx->names().null); |
| 250 |
} |
| 251 |
if (v.isSymbol()) { |
| 252 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 253 |
JSMSG_SYMBOL_TO_STRING); |
| 254 |
return false; |
| 255 |
} |
| 256 |
if (v.isBigInt()) { |
| 257 |
RootedBigInt i(cx, v.toBigInt()); |
| 258 |
JSLinearString* str = BigInt::toString<CanGC>(cx, i, 10); |
| 259 |
if (!str) { |
| 260 |
return false; |
| 261 |
} |
| 262 |
return sb.append(str); |
| 263 |
} |
| 264 |
MOZ_ASSERT(v.isUndefined()); |
| 265 |
return sb.append(cx->names().undefined); |
| 266 |
} |