|
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/. */ |
|
6 |
|
7 #include "vm/StringBuffer.h" |
|
8 |
|
9 #include "jsobjinlines.h" |
|
10 |
|
11 #include "vm/String-inl.h" |
|
12 |
|
13 using namespace js; |
|
14 |
|
15 jschar * |
|
16 StringBuffer::extractWellSized() |
|
17 { |
|
18 size_t capacity = cb.capacity(); |
|
19 size_t length = cb.length(); |
|
20 |
|
21 jschar *buf = cb.extractRawBuffer(); |
|
22 if (!buf) |
|
23 return nullptr; |
|
24 |
|
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 } |
|
37 |
|
38 return buf; |
|
39 } |
|
40 |
|
41 JSFlatString * |
|
42 StringBuffer::finishString() |
|
43 { |
|
44 ExclusiveContext *cx = context(); |
|
45 if (cb.empty()) |
|
46 return cx->names().empty; |
|
47 |
|
48 size_t length = cb.length(); |
|
49 if (!JSString::validateLength(cx, length)) |
|
50 return nullptr; |
|
51 |
|
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)); |
|
55 |
|
56 if (!cb.append('\0')) |
|
57 return nullptr; |
|
58 |
|
59 jschar *buf = extractWellSized(); |
|
60 if (!buf) |
|
61 return nullptr; |
|
62 |
|
63 JSFlatString *str = js_NewString<CanGC>(cx, buf, length); |
|
64 if (!str) |
|
65 js_free(buf); |
|
66 return str; |
|
67 } |
|
68 |
|
69 JSAtom * |
|
70 StringBuffer::finishAtom() |
|
71 { |
|
72 ExclusiveContext *cx = context(); |
|
73 |
|
74 size_t length = cb.length(); |
|
75 if (length == 0) |
|
76 return cx->names().empty; |
|
77 |
|
78 JSAtom *atom = AtomizeChars(cx, cb.begin(), length); |
|
79 cb.clear(); |
|
80 return atom; |
|
81 } |
|
82 |
|
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; |
|
89 |
|
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 } |