js/src/vm/StringBuffer.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/vm/StringBuffer.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,100 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99:
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "vm/StringBuffer.h"
    1.11 +
    1.12 +#include "jsobjinlines.h"
    1.13 +
    1.14 +#include "vm/String-inl.h"
    1.15 +
    1.16 +using namespace js;
    1.17 +
    1.18 +jschar *
    1.19 +StringBuffer::extractWellSized()
    1.20 +{
    1.21 +    size_t capacity = cb.capacity();
    1.22 +    size_t length = cb.length();
    1.23 +
    1.24 +    jschar *buf = cb.extractRawBuffer();
    1.25 +    if (!buf)
    1.26 +        return nullptr;
    1.27 +
    1.28 +    /* For medium/big buffers, avoid wasting more than 1/4 of the memory. */
    1.29 +    JS_ASSERT(capacity >= length);
    1.30 +    if (length > CharBuffer::sMaxInlineStorage && capacity - length > length / 4) {
    1.31 +        size_t bytes = sizeof(jschar) * (length + 1);
    1.32 +        ExclusiveContext *cx = context();
    1.33 +        jschar *tmp = (jschar *)cx->realloc_(buf, bytes);
    1.34 +        if (!tmp) {
    1.35 +            js_free(buf);
    1.36 +            return nullptr;
    1.37 +        }
    1.38 +        buf = tmp;
    1.39 +    }
    1.40 +
    1.41 +    return buf;
    1.42 +}
    1.43 +
    1.44 +JSFlatString *
    1.45 +StringBuffer::finishString()
    1.46 +{
    1.47 +    ExclusiveContext *cx = context();
    1.48 +    if (cb.empty())
    1.49 +        return cx->names().empty;
    1.50 +
    1.51 +    size_t length = cb.length();
    1.52 +    if (!JSString::validateLength(cx, length))
    1.53 +        return nullptr;
    1.54 +
    1.55 +    JS_STATIC_ASSERT(JSFatInlineString::MAX_FAT_INLINE_LENGTH < CharBuffer::InlineLength);
    1.56 +    if (JSFatInlineString::lengthFits(length))
    1.57 +        return NewFatInlineString<CanGC>(cx, TwoByteChars(cb.begin(), length));
    1.58 +
    1.59 +    if (!cb.append('\0'))
    1.60 +        return nullptr;
    1.61 +
    1.62 +    jschar *buf = extractWellSized();
    1.63 +    if (!buf)
    1.64 +        return nullptr;
    1.65 +
    1.66 +    JSFlatString *str = js_NewString<CanGC>(cx, buf, length);
    1.67 +    if (!str)
    1.68 +        js_free(buf);
    1.69 +    return str;
    1.70 +}
    1.71 +
    1.72 +JSAtom *
    1.73 +StringBuffer::finishAtom()
    1.74 +{
    1.75 +    ExclusiveContext *cx = context();
    1.76 +
    1.77 +    size_t length = cb.length();
    1.78 +    if (length == 0)
    1.79 +        return cx->names().empty;
    1.80 +
    1.81 +    JSAtom *atom = AtomizeChars(cx, cb.begin(), length);
    1.82 +    cb.clear();
    1.83 +    return atom;
    1.84 +}
    1.85 +
    1.86 +bool
    1.87 +js::ValueToStringBufferSlow(JSContext *cx, const Value &arg, StringBuffer &sb)
    1.88 +{
    1.89 +    RootedValue v(cx, arg);
    1.90 +    if (!ToPrimitive(cx, JSTYPE_STRING, &v))
    1.91 +        return false;
    1.92 +
    1.93 +    if (v.isString())
    1.94 +        return sb.append(v.toString());
    1.95 +    if (v.isNumber())
    1.96 +        return NumberValueToStringBuffer(cx, v, sb);
    1.97 +    if (v.isBoolean())
    1.98 +        return BooleanToStringBuffer(v.toBoolean(), sb);
    1.99 +    if (v.isNull())
   1.100 +        return sb.append(cx->names().null);
   1.101 +    JS_ASSERT(v.isUndefined());
   1.102 +    return sb.append(cx->names().undefined);
   1.103 +}

mercurial