js/src/vm/StringBuffer.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "vm/StringBuffer.h"
michael@0 8
michael@0 9 #include "jsobjinlines.h"
michael@0 10
michael@0 11 #include "vm/String-inl.h"
michael@0 12
michael@0 13 using namespace js;
michael@0 14
michael@0 15 jschar *
michael@0 16 StringBuffer::extractWellSized()
michael@0 17 {
michael@0 18 size_t capacity = cb.capacity();
michael@0 19 size_t length = cb.length();
michael@0 20
michael@0 21 jschar *buf = cb.extractRawBuffer();
michael@0 22 if (!buf)
michael@0 23 return nullptr;
michael@0 24
michael@0 25 /* For medium/big buffers, avoid wasting more than 1/4 of the memory. */
michael@0 26 JS_ASSERT(capacity >= length);
michael@0 27 if (length > CharBuffer::sMaxInlineStorage && capacity - length > length / 4) {
michael@0 28 size_t bytes = sizeof(jschar) * (length + 1);
michael@0 29 ExclusiveContext *cx = context();
michael@0 30 jschar *tmp = (jschar *)cx->realloc_(buf, bytes);
michael@0 31 if (!tmp) {
michael@0 32 js_free(buf);
michael@0 33 return nullptr;
michael@0 34 }
michael@0 35 buf = tmp;
michael@0 36 }
michael@0 37
michael@0 38 return buf;
michael@0 39 }
michael@0 40
michael@0 41 JSFlatString *
michael@0 42 StringBuffer::finishString()
michael@0 43 {
michael@0 44 ExclusiveContext *cx = context();
michael@0 45 if (cb.empty())
michael@0 46 return cx->names().empty;
michael@0 47
michael@0 48 size_t length = cb.length();
michael@0 49 if (!JSString::validateLength(cx, length))
michael@0 50 return nullptr;
michael@0 51
michael@0 52 JS_STATIC_ASSERT(JSFatInlineString::MAX_FAT_INLINE_LENGTH < CharBuffer::InlineLength);
michael@0 53 if (JSFatInlineString::lengthFits(length))
michael@0 54 return NewFatInlineString<CanGC>(cx, TwoByteChars(cb.begin(), length));
michael@0 55
michael@0 56 if (!cb.append('\0'))
michael@0 57 return nullptr;
michael@0 58
michael@0 59 jschar *buf = extractWellSized();
michael@0 60 if (!buf)
michael@0 61 return nullptr;
michael@0 62
michael@0 63 JSFlatString *str = js_NewString<CanGC>(cx, buf, length);
michael@0 64 if (!str)
michael@0 65 js_free(buf);
michael@0 66 return str;
michael@0 67 }
michael@0 68
michael@0 69 JSAtom *
michael@0 70 StringBuffer::finishAtom()
michael@0 71 {
michael@0 72 ExclusiveContext *cx = context();
michael@0 73
michael@0 74 size_t length = cb.length();
michael@0 75 if (length == 0)
michael@0 76 return cx->names().empty;
michael@0 77
michael@0 78 JSAtom *atom = AtomizeChars(cx, cb.begin(), length);
michael@0 79 cb.clear();
michael@0 80 return atom;
michael@0 81 }
michael@0 82
michael@0 83 bool
michael@0 84 js::ValueToStringBufferSlow(JSContext *cx, const Value &arg, StringBuffer &sb)
michael@0 85 {
michael@0 86 RootedValue v(cx, arg);
michael@0 87 if (!ToPrimitive(cx, JSTYPE_STRING, &v))
michael@0 88 return false;
michael@0 89
michael@0 90 if (v.isString())
michael@0 91 return sb.append(v.toString());
michael@0 92 if (v.isNumber())
michael@0 93 return NumberValueToStringBuffer(cx, v, sb);
michael@0 94 if (v.isBoolean())
michael@0 95 return BooleanToStringBuffer(v.toBoolean(), sb);
michael@0 96 if (v.isNull())
michael@0 97 return sb.append(cx->names().null);
michael@0 98 JS_ASSERT(v.isUndefined());
michael@0 99 return sb.append(cx->names().undefined);
michael@0 100 }

mercurial