Bug 1885489 - Part 5: Add SnapshotIterator::readInt32(). r=iain
[gecko.git] / js / src / util / StringBuffer.cpp
blob03d9a7279cc3fa0cb79caca0b6cfb907bd238369
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/. */
7 #include "util/StringBuffer.h"
9 #include "mozilla/Latin1.h"
10 #include "mozilla/Range.h"
12 #include <algorithm>
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"
19 #include "vm/JSObject-inl.h"
20 #include "vm/StringType-inl.h"
22 using namespace js;
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 StringBufferAllocPolicy allocPolicy = cb.allocPolicy();
30 CharT* buf = cb.extractOrCopyRawBuffer();
31 if (!buf) {
32 return nullptr;
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;
49 buf = tmp;
52 return buf;
55 char16_t* StringBuffer::stealChars() {
56 if (isLatin1() && !inflateChars()) {
57 return nullptr;
60 return ExtractWellSized<char16_t>(twoByteChars());
63 bool StringBuffer::inflateChars() {
64 MOZ_ASSERT(isLatin1());
66 TwoByteCharBuffer twoByte(latin1Chars().allocPolicy());
69 * Note: we don't use Vector::capacity() because it always returns a
70 * value >= sInlineCapacity. Since Latin1CharBuffer::sInlineCapacity >
71 * TwoByteCharBuffer::sInlineCapacitychars, we'd always malloc here.
73 size_t capacity = std::max(reserved_, latin1Chars().length());
74 if (!twoByte.reserve(capacity)) {
75 return false;
78 twoByte.infallibleGrowByUninitialized(latin1Chars().length());
80 mozilla::ConvertLatin1toUtf16(mozilla::AsChars(latin1Chars()), twoByte);
82 cb.destroy();
83 cb.construct<TwoByteCharBuffer>(std::move(twoByte));
84 return true;
87 bool StringBuffer::append(const frontend::ParserAtomsTable& parserAtoms,
88 frontend::TaggedParserAtomIndex atom) {
89 return parserAtoms.appendTo(*this, atom);
92 template <typename CharT>
93 JSLinearString* StringBuffer::finishStringInternal(JSContext* cx,
94 gc::Heap heap) {
95 size_t len = length();
97 if (JSAtom* staticStr = cx->staticStrings().lookup(begin<CharT>(), len)) {
98 return staticStr;
101 if (JSInlineString::lengthFits<CharT>(len)) {
102 mozilla::Range<const CharT> range(begin<CharT>(), len);
103 return NewInlineString<CanGC>(cx, range);
106 UniquePtr<CharT[], JS::FreePolicy> buf(
107 ExtractWellSized<CharT>(chars<CharT>()));
109 if (!buf) {
110 return nullptr;
113 JSLinearString* str =
114 NewStringDontDeflate<CanGC>(cx, std::move(buf), len, heap);
115 if (!str) {
116 return nullptr;
119 return str;
122 JSLinearString* JSStringBuilder::finishString(gc::Heap heap) {
123 MOZ_ASSERT(maybeCx_);
125 size_t len = length();
126 if (len == 0) {
127 return maybeCx_->names().empty_;
130 if (MOZ_UNLIKELY(!JSString::validateLength(maybeCx_, len))) {
131 return nullptr;
134 static_assert(JSFatInlineString::MAX_LENGTH_TWO_BYTE <
135 TwoByteCharBuffer::InlineLength);
136 static_assert(JSFatInlineString::MAX_LENGTH_LATIN1 <
137 Latin1CharBuffer::InlineLength);
139 return isLatin1() ? finishStringInternal<Latin1Char>(maybeCx_, heap)
140 : finishStringInternal<char16_t>(maybeCx_, heap);
143 JSAtom* StringBuffer::finishAtom() {
144 MOZ_ASSERT(maybeCx_);
146 size_t len = length();
147 if (len == 0) {
148 return maybeCx_->names().empty_;
151 if (isLatin1()) {
152 JSAtom* atom = AtomizeChars(maybeCx_, latin1Chars().begin(), len);
153 latin1Chars().clear();
154 return atom;
157 JSAtom* atom = AtomizeChars(maybeCx_, twoByteChars().begin(), len);
158 twoByteChars().clear();
159 return atom;
162 frontend::TaggedParserAtomIndex StringBuffer::finishParserAtom(
163 frontend::ParserAtomsTable& parserAtoms, FrontendContext* fc) {
164 size_t len = length();
165 if (len == 0) {
166 return frontend::TaggedParserAtomIndex::WellKnown::empty();
169 if (isLatin1()) {
170 auto result = parserAtoms.internLatin1(fc, latin1Chars().begin(), len);
171 latin1Chars().clear();
172 return result;
175 auto result = parserAtoms.internChar16(fc, twoByteChars().begin(), len);
176 twoByteChars().clear();
177 return result;
180 bool js::ValueToStringBufferSlow(JSContext* cx, const Value& arg,
181 StringBuffer& sb) {
182 RootedValue v(cx, arg);
183 if (!ToPrimitive(cx, JSTYPE_STRING, &v)) {
184 return false;
187 if (v.isString()) {
188 return sb.append(v.toString());
190 if (v.isNumber()) {
191 return NumberValueToStringBuffer(v, sb);
193 if (v.isBoolean()) {
194 return BooleanToStringBuffer(v.toBoolean(), sb);
196 if (v.isNull()) {
197 return sb.append(cx->names().null);
199 if (v.isSymbol()) {
200 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
201 JSMSG_SYMBOL_TO_STRING);
202 return false;
204 if (v.isBigInt()) {
205 RootedBigInt i(cx, v.toBigInt());
206 JSLinearString* str = BigInt::toString<CanGC>(cx, i, 10);
207 if (!str) {
208 return false;
210 return sb.append(str);
212 MOZ_ASSERT(v.isUndefined());
213 return sb.append(cx->names().undefined);