js/src/vm/String-inl.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/vm/String-inl.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,347 @@
     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 +#ifndef vm_String_inl_h
    1.11 +#define vm_String_inl_h
    1.12 +
    1.13 +#include "vm/String.h"
    1.14 +
    1.15 +#include "mozilla/PodOperations.h"
    1.16 +
    1.17 +#include "jscntxt.h"
    1.18 +
    1.19 +#include "gc/Marking.h"
    1.20 +
    1.21 +#include "jsgcinlines.h"
    1.22 +
    1.23 +namespace js {
    1.24 +
    1.25 +template <AllowGC allowGC>
    1.26 +static MOZ_ALWAYS_INLINE JSInlineString *
    1.27 +NewFatInlineString(ThreadSafeContext *cx, JS::Latin1Chars chars)
    1.28 +{
    1.29 +    size_t len = chars.length();
    1.30 +    JS_ASSERT(JSFatInlineString::lengthFits(len));
    1.31 +    JSInlineString *str = JSInlineString::lengthFits(len)
    1.32 +                          ? JSInlineString::new_<allowGC>(cx)
    1.33 +                          : JSFatInlineString::new_<allowGC>(cx);
    1.34 +    if (!str)
    1.35 +        return nullptr;
    1.36 +
    1.37 +    jschar *p = str->init(len);
    1.38 +    for (size_t i = 0; i < len; ++i)
    1.39 +        p[i] = static_cast<jschar>(chars[i]);
    1.40 +    p[len] = '\0';
    1.41 +    return str;
    1.42 +}
    1.43 +
    1.44 +template <AllowGC allowGC>
    1.45 +static MOZ_ALWAYS_INLINE JSInlineString *
    1.46 +NewFatInlineString(ExclusiveContext *cx, JS::TwoByteChars chars)
    1.47 +{
    1.48 +    size_t len = chars.length();
    1.49 +
    1.50 +    /*
    1.51 +     * Don't bother trying to find a static atom; measurement shows that not
    1.52 +     * many get here (for one, Atomize is catching them).
    1.53 +     */
    1.54 +    JS_ASSERT(JSFatInlineString::lengthFits(len));
    1.55 +    JSInlineString *str = JSInlineString::lengthFits(len)
    1.56 +                          ? JSInlineString::new_<allowGC>(cx)
    1.57 +                          : JSFatInlineString::new_<allowGC>(cx);
    1.58 +    if (!str)
    1.59 +        return nullptr;
    1.60 +
    1.61 +    jschar *storage = str->init(len);
    1.62 +    mozilla::PodCopy(storage, chars.start().get(), len);
    1.63 +    storage[len] = 0;
    1.64 +    return str;
    1.65 +}
    1.66 +
    1.67 +static inline void
    1.68 +StringWriteBarrierPost(js::ThreadSafeContext *maybecx, JSString **strp)
    1.69 +{
    1.70 +}
    1.71 +
    1.72 +static inline void
    1.73 +StringWriteBarrierPostRemove(js::ThreadSafeContext *maybecx, JSString **strp)
    1.74 +{
    1.75 +}
    1.76 +
    1.77 +} /* namespace js */
    1.78 +
    1.79 +MOZ_ALWAYS_INLINE bool
    1.80 +JSString::validateLength(js::ThreadSafeContext *maybecx, size_t length)
    1.81 +{
    1.82 +    if (MOZ_UNLIKELY(length > JSString::MAX_LENGTH)) {
    1.83 +        js_ReportAllocationOverflow(maybecx);
    1.84 +        return false;
    1.85 +    }
    1.86 +
    1.87 +    return true;
    1.88 +}
    1.89 +
    1.90 +MOZ_ALWAYS_INLINE void
    1.91 +JSRope::init(js::ThreadSafeContext *cx, JSString *left, JSString *right, size_t length)
    1.92 +{
    1.93 +    d.lengthAndFlags = buildLengthAndFlags(length, ROPE_FLAGS);
    1.94 +    d.u1.left = left;
    1.95 +    d.s.u2.right = right;
    1.96 +    js::StringWriteBarrierPost(cx, &d.u1.left);
    1.97 +    js::StringWriteBarrierPost(cx, &d.s.u2.right);
    1.98 +}
    1.99 +
   1.100 +template <js::AllowGC allowGC>
   1.101 +MOZ_ALWAYS_INLINE JSRope *
   1.102 +JSRope::new_(js::ThreadSafeContext *cx,
   1.103 +             typename js::MaybeRooted<JSString*, allowGC>::HandleType left,
   1.104 +             typename js::MaybeRooted<JSString*, allowGC>::HandleType right,
   1.105 +             size_t length)
   1.106 +{
   1.107 +    if (!validateLength(cx, length))
   1.108 +        return nullptr;
   1.109 +    JSRope *str = (JSRope *) js_NewGCString<allowGC>(cx);
   1.110 +    if (!str)
   1.111 +        return nullptr;
   1.112 +    str->init(cx, left, right, length);
   1.113 +    return str;
   1.114 +}
   1.115 +
   1.116 +inline void
   1.117 +JSRope::markChildren(JSTracer *trc)
   1.118 +{
   1.119 +    js::gc::MarkStringUnbarriered(trc, &d.u1.left, "left child");
   1.120 +    js::gc::MarkStringUnbarriered(trc, &d.s.u2.right, "right child");
   1.121 +}
   1.122 +
   1.123 +MOZ_ALWAYS_INLINE void
   1.124 +JSDependentString::init(js::ThreadSafeContext *cx, JSLinearString *base, const jschar *chars,
   1.125 +                        size_t length)
   1.126 +{
   1.127 +    JS_ASSERT(!js::IsPoisonedPtr(base));
   1.128 +    d.lengthAndFlags = buildLengthAndFlags(length, DEPENDENT_FLAGS);
   1.129 +    d.u1.chars = chars;
   1.130 +    d.s.u2.base = base;
   1.131 +    js::StringWriteBarrierPost(cx, reinterpret_cast<JSString **>(&d.s.u2.base));
   1.132 +}
   1.133 +
   1.134 +MOZ_ALWAYS_INLINE JSLinearString *
   1.135 +JSDependentString::new_(js::ExclusiveContext *cx,
   1.136 +                        JSLinearString *baseArg, const jschar *chars, size_t length)
   1.137 +{
   1.138 +    /* Try to avoid long chains of dependent strings. */
   1.139 +    while (baseArg->isDependent())
   1.140 +        baseArg = baseArg->asDependent().base();
   1.141 +
   1.142 +    JS_ASSERT(baseArg->isFlat());
   1.143 +
   1.144 +    /*
   1.145 +     * The chars we are pointing into must be owned by something in the chain
   1.146 +     * of dependent or undepended strings kept alive by our base pointer.
   1.147 +     */
   1.148 +#ifdef DEBUG
   1.149 +    for (JSLinearString *b = baseArg; ; b = b->base()) {
   1.150 +        if (chars >= b->chars() && chars < b->chars() + b->length() &&
   1.151 +            length <= b->length() - (chars - b->chars()))
   1.152 +        {
   1.153 +            break;
   1.154 +        }
   1.155 +    }
   1.156 +#endif
   1.157 +
   1.158 +    /*
   1.159 +     * Do not create a string dependent on inline chars from another string,
   1.160 +     * both to avoid the awkward moving-GC hazard this introduces and because it
   1.161 +     * is more efficient to immediately undepend here.
   1.162 +     */
   1.163 +    if (JSFatInlineString::lengthFits(length))
   1.164 +        return js::NewFatInlineString<js::CanGC>(cx, JS::TwoByteChars(chars, length));
   1.165 +
   1.166 +    JSDependentString *str = (JSDependentString *)js_NewGCString<js::NoGC>(cx);
   1.167 +    if (str) {
   1.168 +        str->init(cx, baseArg, chars, length);
   1.169 +        return str;
   1.170 +    }
   1.171 +
   1.172 +    JS::Rooted<JSLinearString*> base(cx, baseArg);
   1.173 +
   1.174 +    str = (JSDependentString *)js_NewGCString<js::CanGC>(cx);
   1.175 +    if (!str)
   1.176 +        return nullptr;
   1.177 +    str->init(cx, base, chars, length);
   1.178 +    return str;
   1.179 +}
   1.180 +
   1.181 +inline void
   1.182 +JSString::markBase(JSTracer *trc)
   1.183 +{
   1.184 +    JS_ASSERT(hasBase());
   1.185 +    js::gc::MarkStringUnbarriered(trc, &d.s.u2.base, "base");
   1.186 +}
   1.187 +
   1.188 +MOZ_ALWAYS_INLINE void
   1.189 +JSFlatString::init(const jschar *chars, size_t length)
   1.190 +{
   1.191 +    d.lengthAndFlags = buildLengthAndFlags(length, FIXED_FLAGS);
   1.192 +    d.u1.chars = chars;
   1.193 +}
   1.194 +
   1.195 +template <js::AllowGC allowGC>
   1.196 +MOZ_ALWAYS_INLINE JSFlatString *
   1.197 +JSFlatString::new_(js::ThreadSafeContext *cx, const jschar *chars, size_t length)
   1.198 +{
   1.199 +    JS_ASSERT(chars[length] == jschar(0));
   1.200 +
   1.201 +    if (!validateLength(cx, length))
   1.202 +        return nullptr;
   1.203 +    JSFlatString *str = (JSFlatString *)js_NewGCString<allowGC>(cx);
   1.204 +    if (!str)
   1.205 +        return nullptr;
   1.206 +    str->init(chars, length);
   1.207 +    return str;
   1.208 +}
   1.209 +
   1.210 +inline js::PropertyName *
   1.211 +JSFlatString::toPropertyName(JSContext *cx)
   1.212 +{
   1.213 +#ifdef DEBUG
   1.214 +    uint32_t dummy;
   1.215 +    JS_ASSERT(!isIndex(&dummy));
   1.216 +#endif
   1.217 +    if (isAtom())
   1.218 +        return asAtom().asPropertyName();
   1.219 +    JSAtom *atom = js::AtomizeString(cx, this);
   1.220 +    if (!atom)
   1.221 +        return nullptr;
   1.222 +    return atom->asPropertyName();
   1.223 +}
   1.224 +
   1.225 +template <js::AllowGC allowGC>
   1.226 +MOZ_ALWAYS_INLINE JSInlineString *
   1.227 +JSInlineString::new_(js::ThreadSafeContext *cx)
   1.228 +{
   1.229 +    return (JSInlineString *)js_NewGCString<allowGC>(cx);
   1.230 +}
   1.231 +
   1.232 +MOZ_ALWAYS_INLINE jschar *
   1.233 +JSInlineString::init(size_t length)
   1.234 +{
   1.235 +    d.lengthAndFlags = buildLengthAndFlags(length, FIXED_FLAGS);
   1.236 +    d.u1.chars = d.inlineStorage;
   1.237 +    JS_ASSERT(lengthFits(length) || (isFatInline() && JSFatInlineString::lengthFits(length)));
   1.238 +    return d.inlineStorage;
   1.239 +}
   1.240 +
   1.241 +MOZ_ALWAYS_INLINE void
   1.242 +JSInlineString::resetLength(size_t length)
   1.243 +{
   1.244 +    d.lengthAndFlags = buildLengthAndFlags(length, FIXED_FLAGS);
   1.245 +    JS_ASSERT(lengthFits(length) || (isFatInline() && JSFatInlineString::lengthFits(length)));
   1.246 +}
   1.247 +
   1.248 +template <js::AllowGC allowGC>
   1.249 +MOZ_ALWAYS_INLINE JSFatInlineString *
   1.250 +JSFatInlineString::new_(js::ThreadSafeContext *cx)
   1.251 +{
   1.252 +    return js_NewGCFatInlineString<allowGC>(cx);
   1.253 +}
   1.254 +
   1.255 +MOZ_ALWAYS_INLINE void
   1.256 +JSExternalString::init(const jschar *chars, size_t length, const JSStringFinalizer *fin)
   1.257 +{
   1.258 +    JS_ASSERT(fin);
   1.259 +    JS_ASSERT(fin->finalize);
   1.260 +    d.lengthAndFlags = buildLengthAndFlags(length, FIXED_FLAGS);
   1.261 +    d.u1.chars = chars;
   1.262 +    d.s.u2.externalFinalizer = fin;
   1.263 +}
   1.264 +
   1.265 +MOZ_ALWAYS_INLINE JSExternalString *
   1.266 +JSExternalString::new_(JSContext *cx, const jschar *chars, size_t length,
   1.267 +                       const JSStringFinalizer *fin)
   1.268 +{
   1.269 +    JS_ASSERT(chars[length] == 0);
   1.270 +
   1.271 +    if (!validateLength(cx, length))
   1.272 +        return nullptr;
   1.273 +    JSExternalString *str = js_NewGCExternalString(cx);
   1.274 +    if (!str)
   1.275 +        return nullptr;
   1.276 +    str->init(chars, length, fin);
   1.277 +    cx->runtime()->updateMallocCounter(cx->zone(), (length + 1) * sizeof(jschar));
   1.278 +    return str;
   1.279 +}
   1.280 +
   1.281 +inline JSLinearString *
   1.282 +js::StaticStrings::getUnitStringForElement(JSContext *cx, JSString *str, size_t index)
   1.283 +{
   1.284 +    JS_ASSERT(index < str->length());
   1.285 +
   1.286 +    jschar c;
   1.287 +    if (!str->getChar(cx, index, &c))
   1.288 +        return nullptr;
   1.289 +    if (c < UNIT_STATIC_LIMIT)
   1.290 +        return getUnit(c);
   1.291 +    return js_NewDependentString(cx, str, index, 1);
   1.292 +}
   1.293 +
   1.294 +inline JSAtom *
   1.295 +js::StaticStrings::getLength2(jschar c1, jschar c2)
   1.296 +{
   1.297 +    JS_ASSERT(fitsInSmallChar(c1));
   1.298 +    JS_ASSERT(fitsInSmallChar(c2));
   1.299 +    size_t index = (((size_t)toSmallChar[c1]) << 6) + toSmallChar[c2];
   1.300 +    return length2StaticTable[index];
   1.301 +}
   1.302 +
   1.303 +MOZ_ALWAYS_INLINE void
   1.304 +JSString::finalize(js::FreeOp *fop)
   1.305 +{
   1.306 +    /* FatInline strings are in a different arena. */
   1.307 +    JS_ASSERT(getAllocKind() != js::gc::FINALIZE_FAT_INLINE_STRING);
   1.308 +
   1.309 +    if (isFlat())
   1.310 +        asFlat().finalize(fop);
   1.311 +    else
   1.312 +        JS_ASSERT(isDependent() || isRope());
   1.313 +}
   1.314 +
   1.315 +inline void
   1.316 +JSFlatString::finalize(js::FreeOp *fop)
   1.317 +{
   1.318 +    JS_ASSERT(getAllocKind() != js::gc::FINALIZE_FAT_INLINE_STRING);
   1.319 +
   1.320 +    if (chars() != d.inlineStorage)
   1.321 +        fop->free_(const_cast<jschar *>(chars()));
   1.322 +}
   1.323 +
   1.324 +inline void
   1.325 +JSFatInlineString::finalize(js::FreeOp *fop)
   1.326 +{
   1.327 +    JS_ASSERT(getAllocKind() == js::gc::FINALIZE_FAT_INLINE_STRING);
   1.328 +
   1.329 +    if (chars() != d.inlineStorage)
   1.330 +        fop->free_(const_cast<jschar *>(chars()));
   1.331 +}
   1.332 +
   1.333 +inline void
   1.334 +JSAtom::finalize(js::FreeOp *fop)
   1.335 +{
   1.336 +    JS_ASSERT(JSString::isAtom());
   1.337 +    JS_ASSERT(JSString::isFlat());
   1.338 +
   1.339 +    if (chars() != d.inlineStorage)
   1.340 +        fop->free_(const_cast<jschar *>(chars()));
   1.341 +}
   1.342 +
   1.343 +inline void
   1.344 +JSExternalString::finalize(js::FreeOp *fop)
   1.345 +{
   1.346 +    const JSStringFinalizer *fin = externalFinalizer();
   1.347 +    fin->finalize(fin, const_cast<jschar *>(chars()));
   1.348 +}
   1.349 +
   1.350 +#endif /* vm_String_inl_h */

mercurial