Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sts=4 et sw=4 tw=99:
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 "vm/StringBuffer.h"
9 #include "jsobjinlines.h"
11 #include "vm/String-inl.h"
13 using namespace js;
15 jschar *
16 StringBuffer::extractWellSized()
17 {
18 size_t capacity = cb.capacity();
19 size_t length = cb.length();
21 jschar *buf = cb.extractRawBuffer();
22 if (!buf)
23 return nullptr;
25 /* For medium/big buffers, avoid wasting more than 1/4 of the memory. */
26 JS_ASSERT(capacity >= length);
27 if (length > CharBuffer::sMaxInlineStorage && capacity - length > length / 4) {
28 size_t bytes = sizeof(jschar) * (length + 1);
29 ExclusiveContext *cx = context();
30 jschar *tmp = (jschar *)cx->realloc_(buf, bytes);
31 if (!tmp) {
32 js_free(buf);
33 return nullptr;
34 }
35 buf = tmp;
36 }
38 return buf;
39 }
41 JSFlatString *
42 StringBuffer::finishString()
43 {
44 ExclusiveContext *cx = context();
45 if (cb.empty())
46 return cx->names().empty;
48 size_t length = cb.length();
49 if (!JSString::validateLength(cx, length))
50 return nullptr;
52 JS_STATIC_ASSERT(JSFatInlineString::MAX_FAT_INLINE_LENGTH < CharBuffer::InlineLength);
53 if (JSFatInlineString::lengthFits(length))
54 return NewFatInlineString<CanGC>(cx, TwoByteChars(cb.begin(), length));
56 if (!cb.append('\0'))
57 return nullptr;
59 jschar *buf = extractWellSized();
60 if (!buf)
61 return nullptr;
63 JSFlatString *str = js_NewString<CanGC>(cx, buf, length);
64 if (!str)
65 js_free(buf);
66 return str;
67 }
69 JSAtom *
70 StringBuffer::finishAtom()
71 {
72 ExclusiveContext *cx = context();
74 size_t length = cb.length();
75 if (length == 0)
76 return cx->names().empty;
78 JSAtom *atom = AtomizeChars(cx, cb.begin(), length);
79 cb.clear();
80 return atom;
81 }
83 bool
84 js::ValueToStringBufferSlow(JSContext *cx, const Value &arg, StringBuffer &sb)
85 {
86 RootedValue v(cx, arg);
87 if (!ToPrimitive(cx, JSTYPE_STRING, &v))
88 return false;
90 if (v.isString())
91 return sb.append(v.toString());
92 if (v.isNumber())
93 return NumberValueToStringBuffer(cx, v, sb);
94 if (v.isBoolean())
95 return BooleanToStringBuffer(v.toBoolean(), sb);
96 if (v.isNull())
97 return sb.append(cx->names().null);
98 JS_ASSERT(v.isUndefined());
99 return sb.append(cx->names().undefined);
100 }