weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 1999-2002 Harri Porten (porten@kde.org) |
| 3 | * Copyright (C) 2001 Peter Kelly (pmk@post.com) |
fpizlo@apple.com | 3cb36ea | 2015-10-05 19:35:32 +0000 | [diff] [blame^] | 4 | * Copyright (C) 2004, 2007, 2008, 2015 Apple Inc. All rights reserved. |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Library General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Library General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Library General Public License |
| 17 | * along with this library; see the file COPYING.LIB. If not, write to |
| 18 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 19 | * Boston, MA 02110-1301, USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include "config.h" |
| 24 | #include "JSString.h" |
| 25 | |
| 26 | #include "JSGlobalObject.h" |
barraclough@apple.com | 794f461 | 2010-08-18 07:41:22 +0000 | [diff] [blame] | 27 | #include "JSGlobalObjectFunctions.h" |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 28 | #include "JSObject.h" |
fpizlo@apple.com | fb7eff2 | 2014-02-11 01:45:50 +0000 | [diff] [blame] | 29 | #include "JSCInlines.h" |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 30 | #include "StringObject.h" |
| 31 | #include "StringPrototype.h" |
akling@apple.com | ede8a98 | 2014-07-08 22:36:59 +0000 | [diff] [blame] | 32 | #include "StrongInlines.h" |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 33 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 34 | namespace JSC { |
antti@apple.com | 6261f1f | 2010-12-07 11:11:08 +0000 | [diff] [blame] | 35 | |
akling@apple.com | 2de49b7 | 2014-07-30 22:26:22 +0000 | [diff] [blame] | 36 | const ClassInfo JSString::s_info = { "string", 0, 0, CREATE_METHOD_TABLE(JSString) }; |
oliver@apple.com | 23ce68f | 2011-04-25 21:21:28 +0000 | [diff] [blame] | 37 | |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 38 | void JSRopeString::RopeBuilder::expand() |
ggaren@apple.com | fbf6d9a | 2011-10-19 02:54:29 +0000 | [diff] [blame] | 39 | { |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 40 | ASSERT(m_index == JSRopeString::s_maxInternalRopeLength); |
ggaren@apple.com | fbf6d9a | 2011-10-19 02:54:29 +0000 | [diff] [blame] | 41 | JSString* jsString = m_jsString; |
fpizlo@apple.com | 75104a3 | 2014-04-15 23:33:11 +0000 | [diff] [blame] | 42 | RELEASE_ASSERT(jsString); |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 43 | m_jsString = jsStringBuilder(&m_vm); |
ggaren@apple.com | ffbe44d | 2011-10-19 19:45:35 +0000 | [diff] [blame] | 44 | m_index = 0; |
ggaren@apple.com | fbf6d9a | 2011-10-19 02:54:29 +0000 | [diff] [blame] | 45 | append(jsString); |
| 46 | } |
| 47 | |
mhahnenberg@apple.com | c58d54d | 2011-12-16 19:06:44 +0000 | [diff] [blame] | 48 | void JSString::destroy(JSCell* cell) |
ggaren@apple.com | fbf6d9a | 2011-10-19 02:54:29 +0000 | [diff] [blame] | 49 | { |
ggaren@apple.com | 72da811 | 2012-05-26 22:40:46 +0000 | [diff] [blame] | 50 | JSString* thisObject = static_cast<JSString*>(cell); |
mhahnenberg@apple.com | c58d54d | 2011-12-16 19:06:44 +0000 | [diff] [blame] | 51 | thisObject->JSString::~JSString(); |
ggaren@apple.com | fbf6d9a | 2011-10-19 02:54:29 +0000 | [diff] [blame] | 52 | } |
| 53 | |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 54 | void JSString::dumpToStream(const JSCell* cell, PrintStream& out) |
| 55 | { |
| 56 | const JSString* thisObject = jsCast<const JSString*>(cell); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 57 | out.printf("<%p, %s, [%u], ", thisObject, thisObject->className(), thisObject->length()); |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 58 | if (thisObject->isRope()) |
| 59 | out.printf("[rope]"); |
| 60 | else { |
| 61 | WTF::StringImpl* ourImpl = thisObject->m_value.impl(); |
| 62 | if (ourImpl->is8Bit()) |
| 63 | out.printf("[8 %p]", ourImpl->characters8()); |
| 64 | else |
| 65 | out.printf("[16 %p]", ourImpl->characters16()); |
| 66 | } |
| 67 | out.printf(">"); |
| 68 | } |
| 69 | |
ggaren@apple.com | fbf6d9a | 2011-10-19 02:54:29 +0000 | [diff] [blame] | 70 | void JSString::visitChildren(JSCell* cell, SlotVisitor& visitor) |
| 71 | { |
mhahnenberg@apple.com | 135f051 | 2011-11-11 20:40:10 +0000 | [diff] [blame] | 72 | JSString* thisObject = jsCast<JSString*>(cell); |
ggaren@apple.com | fbf6d9a | 2011-10-19 02:54:29 +0000 | [diff] [blame] | 73 | Base::visitChildren(thisObject, visitor); |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 74 | |
| 75 | if (thisObject->isRope()) |
| 76 | static_cast<JSRopeString*>(thisObject)->visitFibers(visitor); |
oliver@apple.com | 8cebf37 | 2013-07-25 04:03:46 +0000 | [diff] [blame] | 77 | else { |
| 78 | StringImpl* impl = thisObject->m_value.impl(); |
| 79 | ASSERT(impl); |
fpizlo@apple.com | 3cb36ea | 2015-10-05 19:35:32 +0000 | [diff] [blame^] | 80 | visitor.reportExtraMemoryVisited(impl->costDuringGC()); |
oliver@apple.com | 8cebf37 | 2013-07-25 04:03:46 +0000 | [diff] [blame] | 81 | } |
ggaren@apple.com | fbf6d9a | 2011-10-19 02:54:29 +0000 | [diff] [blame] | 82 | } |
| 83 | |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 84 | void JSRopeString::visitFibers(SlotVisitor& visitor) |
| 85 | { |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 86 | if (isSubstring()) { |
| 87 | visitor.append(&substringBase()); |
| 88 | return; |
| 89 | } |
| 90 | for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) |
| 91 | visitor.append(&fiber(i)); |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 92 | } |
| 93 | |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 94 | static const unsigned maxLengthForOnStackResolve = 2048; |
| 95 | |
| 96 | void JSRopeString::resolveRopeInternal8(LChar* buffer) const |
| 97 | { |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 98 | if (isSubstring()) { |
| 99 | StringImpl::copyChars( |
| 100 | buffer, substringBase()->m_value.characters8() + substringOffset(), m_length); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | resolveRopeInternal8NoSubstring(buffer); |
| 105 | } |
| 106 | |
| 107 | void JSRopeString::resolveRopeInternal8NoSubstring(LChar* buffer) const |
| 108 | { |
| 109 | for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) { |
| 110 | if (fiber(i)->isRope()) { |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 111 | resolveRopeSlowCase8(buffer); |
| 112 | return; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | LChar* position = buffer; |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 117 | for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) { |
| 118 | const StringImpl& fiberString = *fiber(i)->m_value.impl(); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 119 | unsigned length = fiberString.length(); |
| 120 | StringImpl::copyChars(position, fiberString.characters8(), length); |
| 121 | position += length; |
| 122 | } |
| 123 | ASSERT((buffer + m_length) == position); |
| 124 | } |
| 125 | |
| 126 | void JSRopeString::resolveRopeInternal16(UChar* buffer) const |
| 127 | { |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 128 | if (isSubstring()) { |
| 129 | StringImpl::copyChars( |
| 130 | buffer, substringBase()->m_value.characters16() + substringOffset(), m_length); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | resolveRopeInternal16NoSubstring(buffer); |
| 135 | } |
| 136 | |
| 137 | void JSRopeString::resolveRopeInternal16NoSubstring(UChar* buffer) const |
| 138 | { |
| 139 | for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) { |
| 140 | if (fiber(i)->isRope()) { |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 141 | resolveRopeSlowCase(buffer); |
| 142 | return; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | UChar* position = buffer; |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 147 | for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) { |
| 148 | const StringImpl& fiberString = *fiber(i)->m_value.impl(); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 149 | unsigned length = fiberString.length(); |
| 150 | if (fiberString.is8Bit()) |
| 151 | StringImpl::copyChars(position, fiberString.characters8(), length); |
| 152 | else |
| 153 | StringImpl::copyChars(position, fiberString.characters16(), length); |
| 154 | position += length; |
| 155 | } |
| 156 | ASSERT((buffer + m_length) == position); |
| 157 | } |
| 158 | |
| 159 | void JSRopeString::resolveRopeToAtomicString(ExecState* exec) const |
| 160 | { |
| 161 | if (m_length > maxLengthForOnStackResolve) { |
| 162 | resolveRope(exec); |
| 163 | m_value = AtomicString(m_value); |
akling@apple.com | a8c49de | 2014-08-18 21:33:01 +0000 | [diff] [blame] | 164 | setIs8Bit(m_value.impl()->is8Bit()); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | |
| 168 | if (is8Bit()) { |
| 169 | LChar buffer[maxLengthForOnStackResolve]; |
| 170 | resolveRopeInternal8(buffer); |
| 171 | m_value = AtomicString(buffer, m_length); |
akling@apple.com | a8c49de | 2014-08-18 21:33:01 +0000 | [diff] [blame] | 172 | setIs8Bit(m_value.impl()->is8Bit()); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 173 | } else { |
| 174 | UChar buffer[maxLengthForOnStackResolve]; |
| 175 | resolveRopeInternal16(buffer); |
| 176 | m_value = AtomicString(buffer, m_length); |
akling@apple.com | a8c49de | 2014-08-18 21:33:01 +0000 | [diff] [blame] | 177 | setIs8Bit(m_value.impl()->is8Bit()); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | clearFibers(); |
| 181 | |
| 182 | // If we resolved a string that didn't previously exist, notify the heap that we've grown. |
| 183 | if (m_value.impl()->hasOneRef()) |
ggaren@apple.com | e2ebb8c | 2015-03-11 21:29:57 +0000 | [diff] [blame] | 184 | Heap::heap(this)->reportExtraMemoryAllocated(m_value.impl()->cost()); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void JSRopeString::clearFibers() const |
| 188 | { |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 189 | for (size_t i = 0; i < s_maxInternalRopeLength; ++i) |
| 190 | u[i].number = 0; |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 191 | } |
| 192 | |
utatane.tea@gmail.com | e0741fb | 2015-06-02 17:36:16 +0000 | [diff] [blame] | 193 | RefPtr<AtomicStringImpl> JSRopeString::resolveRopeToExistingAtomicString(ExecState* exec) const |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 194 | { |
| 195 | if (m_length > maxLengthForOnStackResolve) { |
| 196 | resolveRope(exec); |
utatane.tea@gmail.com | e0741fb | 2015-06-02 17:36:16 +0000 | [diff] [blame] | 197 | if (RefPtr<AtomicStringImpl> existingAtomicString = AtomicStringImpl::lookUp(m_value.impl())) { |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 198 | m_value = *existingAtomicString; |
akling@apple.com | a8c49de | 2014-08-18 21:33:01 +0000 | [diff] [blame] | 199 | setIs8Bit(m_value.impl()->is8Bit()); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 200 | clearFibers(); |
| 201 | return existingAtomicString; |
| 202 | } |
| 203 | return nullptr; |
| 204 | } |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 205 | |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 206 | if (is8Bit()) { |
| 207 | LChar buffer[maxLengthForOnStackResolve]; |
| 208 | resolveRopeInternal8(buffer); |
utatane.tea@gmail.com | e0741fb | 2015-06-02 17:36:16 +0000 | [diff] [blame] | 209 | if (RefPtr<AtomicStringImpl> existingAtomicString = AtomicStringImpl::lookUp(buffer, m_length)) { |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 210 | m_value = *existingAtomicString; |
akling@apple.com | a8c49de | 2014-08-18 21:33:01 +0000 | [diff] [blame] | 211 | setIs8Bit(m_value.impl()->is8Bit()); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 212 | clearFibers(); |
| 213 | return existingAtomicString; |
| 214 | } |
| 215 | } else { |
| 216 | UChar buffer[maxLengthForOnStackResolve]; |
| 217 | resolveRopeInternal16(buffer); |
utatane.tea@gmail.com | e0741fb | 2015-06-02 17:36:16 +0000 | [diff] [blame] | 218 | if (RefPtr<AtomicStringImpl> existingAtomicString = AtomicStringImpl::lookUp(buffer, m_length)) { |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 219 | m_value = *existingAtomicString; |
akling@apple.com | a8c49de | 2014-08-18 21:33:01 +0000 | [diff] [blame] | 220 | setIs8Bit(m_value.impl()->is8Bit()); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 221 | clearFibers(); |
| 222 | return existingAtomicString; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | return nullptr; |
| 227 | } |
| 228 | |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 229 | void JSRopeString::resolveRope(ExecState* exec) const |
msaboff@apple.com | 8cae2b1 | 2011-04-21 17:23:16 +0000 | [diff] [blame] | 230 | { |
| 231 | ASSERT(isRope()); |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 232 | |
| 233 | if (isSubstring()) { |
| 234 | ASSERT(!substringBase()->isRope()); |
| 235 | m_value = substringBase()->m_value.substring(substringOffset(), m_length); |
| 236 | substringBase().clear(); |
| 237 | return; |
| 238 | } |
| 239 | |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 240 | if (is8Bit()) { |
| 241 | LChar* buffer; |
commit-queue@webkit.org | 1a15efe | 2012-02-28 16:02:02 +0000 | [diff] [blame] | 242 | if (RefPtr<StringImpl> newImpl = StringImpl::tryCreateUninitialized(m_length, buffer)) { |
ggaren@apple.com | e2ebb8c | 2015-03-11 21:29:57 +0000 | [diff] [blame] | 243 | Heap::heap(this)->reportExtraMemoryAllocated(newImpl->cost()); |
msaboff@apple.com | 0111dc8 | 2011-11-15 00:16:59 +0000 | [diff] [blame] | 244 | m_value = newImpl.release(); |
commit-queue@webkit.org | 1a15efe | 2012-02-28 16:02:02 +0000 | [diff] [blame] | 245 | } else { |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 246 | outOfMemory(exec); |
| 247 | return; |
| 248 | } |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 249 | resolveRopeInternal8NoSubstring(buffer); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 250 | clearFibers(); |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 251 | ASSERT(!isRope()); |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 252 | return; |
| 253 | } |
| 254 | |
msaboff@apple.com | 8cae2b1 | 2011-04-21 17:23:16 +0000 | [diff] [blame] | 255 | UChar* buffer; |
commit-queue@webkit.org | 1a15efe | 2012-02-28 16:02:02 +0000 | [diff] [blame] | 256 | if (RefPtr<StringImpl> newImpl = StringImpl::tryCreateUninitialized(m_length, buffer)) { |
ggaren@apple.com | e2ebb8c | 2015-03-11 21:29:57 +0000 | [diff] [blame] | 257 | Heap::heap(this)->reportExtraMemoryAllocated(newImpl->cost()); |
msaboff@apple.com | 0111dc8 | 2011-11-15 00:16:59 +0000 | [diff] [blame] | 258 | m_value = newImpl.release(); |
commit-queue@webkit.org | 1a15efe | 2012-02-28 16:02:02 +0000 | [diff] [blame] | 259 | } else { |
msaboff@apple.com | 8cae2b1 | 2011-04-21 17:23:16 +0000 | [diff] [blame] | 260 | outOfMemory(exec); |
| 261 | return; |
| 262 | } |
| 263 | |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 264 | resolveRopeInternal16NoSubstring(buffer); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 265 | clearFibers(); |
msaboff@apple.com | 8cae2b1 | 2011-04-21 17:23:16 +0000 | [diff] [blame] | 266 | ASSERT(!isRope()); |
| 267 | } |
| 268 | |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 269 | // Overview: These functions convert a JSString from holding a string in rope form |
benjamin@webkit.org | cff06e4 | 2012-08-30 21:23:51 +0000 | [diff] [blame] | 270 | // down to a simple String representation. It does so by building up the string |
barraclough@apple.com | 35d680c | 2009-12-04 02:15:18 +0000 | [diff] [blame] | 271 | // backwards, since we want to avoid recursion, we expect that the tree structure |
| 272 | // representing the rope is likely imbalanced with more nodes down the left side |
| 273 | // (since appending to the string is likely more common) - and as such resolving |
| 274 | // in this fashion should minimize work queue size. (If we built the queue forwards |
barraclough@apple.com | 4d95c9e | 2010-08-10 22:32:19 +0000 | [diff] [blame] | 275 | // we would likely have to place all of the constituent StringImpls into the |
barraclough@apple.com | 35d680c | 2009-12-04 02:15:18 +0000 | [diff] [blame] | 276 | // Vector before performing any concatenation, but by working backwards we likely |
| 277 | // only fill the queue with the number of substrings at any given level in a |
msaboff@apple.com | 8cae2b1 | 2011-04-21 17:23:16 +0000 | [diff] [blame] | 278 | // rope-of-ropes.) |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 279 | void JSRopeString::resolveRopeSlowCase8(LChar* buffer) const |
barraclough@apple.com | 35d680c | 2009-12-04 02:15:18 +0000 | [diff] [blame] | 280 | { |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 281 | LChar* position = buffer + m_length; // We will be working backwards over the rope. |
oliver@apple.com | 4e3f965 | 2013-04-08 04:14:50 +0000 | [diff] [blame] | 282 | Vector<JSString*, 32, UnsafeVectorOverflow> workQueue; // Putting strings into a Vector is only OK because there are no GC points in this method. |
ggaren@apple.com | ffbe44d | 2011-10-19 19:45:35 +0000 | [diff] [blame] | 283 | |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 284 | for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) |
| 285 | workQueue.append(fiber(i).get()); |
ggaren@apple.com | ffbe44d | 2011-10-19 19:45:35 +0000 | [diff] [blame] | 286 | |
| 287 | while (!workQueue.isEmpty()) { |
| 288 | JSString* currentFiber = workQueue.last(); |
| 289 | workQueue.removeLast(); |
akling@apple.com | 77ee6bf | 2014-05-05 06:10:03 +0000 | [diff] [blame] | 290 | |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 291 | const LChar* characters; |
| 292 | |
ggaren@apple.com | fbf6d9a | 2011-10-19 02:54:29 +0000 | [diff] [blame] | 293 | if (currentFiber->isRope()) { |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 294 | JSRopeString* currentFiberAsRope = static_cast<JSRopeString*>(currentFiber); |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 295 | if (!currentFiberAsRope->isSubstring()) { |
| 296 | for (size_t i = 0; i < s_maxInternalRopeLength && currentFiberAsRope->fiber(i); ++i) |
| 297 | workQueue.append(currentFiberAsRope->fiber(i).get()); |
| 298 | continue; |
| 299 | } |
| 300 | ASSERT(!currentFiberAsRope->substringBase()->isRope()); |
| 301 | characters = |
| 302 | currentFiberAsRope->substringBase()->m_value.characters8() + |
| 303 | currentFiberAsRope->substringOffset(); |
| 304 | } else |
| 305 | characters = currentFiber->m_value.characters8(); |
| 306 | |
| 307 | unsigned length = currentFiber->length(); |
ggaren@apple.com | ffbe44d | 2011-10-19 19:45:35 +0000 | [diff] [blame] | 308 | position -= length; |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 309 | StringImpl::copyChars(position, characters, length); |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | ASSERT(buffer == position); |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 313 | } |
| 314 | |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 315 | void JSRopeString::resolveRopeSlowCase(UChar* buffer) const |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 316 | { |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 317 | UChar* position = buffer + m_length; // We will be working backwards over the rope. |
oliver@apple.com | 4e3f965 | 2013-04-08 04:14:50 +0000 | [diff] [blame] | 318 | Vector<JSString*, 32, UnsafeVectorOverflow> workQueue; // These strings are kept alive by the parent rope, so using a Vector is OK. |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 319 | |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 320 | for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) |
| 321 | workQueue.append(fiber(i).get()); |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 322 | |
| 323 | while (!workQueue.isEmpty()) { |
| 324 | JSString* currentFiber = workQueue.last(); |
| 325 | workQueue.removeLast(); |
| 326 | |
| 327 | if (currentFiber->isRope()) { |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 328 | JSRopeString* currentFiberAsRope = static_cast<JSRopeString*>(currentFiber); |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 329 | if (currentFiberAsRope->isSubstring()) { |
| 330 | ASSERT(!currentFiberAsRope->substringBase()->isRope()); |
| 331 | StringImpl* string = static_cast<StringImpl*>( |
| 332 | currentFiberAsRope->substringBase()->m_value.impl()); |
| 333 | unsigned offset = currentFiberAsRope->substringOffset(); |
| 334 | unsigned length = currentFiberAsRope->length(); |
| 335 | position -= length; |
| 336 | if (string->is8Bit()) |
| 337 | StringImpl::copyChars(position, string->characters8() + offset, length); |
| 338 | else |
| 339 | StringImpl::copyChars(position, string->characters16() + offset, length); |
| 340 | continue; |
| 341 | } |
| 342 | for (size_t i = 0; i < s_maxInternalRopeLength && currentFiberAsRope->fiber(i); ++i) |
| 343 | workQueue.append(currentFiberAsRope->fiber(i).get()); |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 344 | continue; |
| 345 | } |
| 346 | |
| 347 | StringImpl* string = static_cast<StringImpl*>(currentFiber->m_value.impl()); |
| 348 | unsigned length = string->length(); |
| 349 | position -= length; |
msaboff@apple.com | f25bc16 | 2012-09-07 01:29:12 +0000 | [diff] [blame] | 350 | if (string->is8Bit()) |
| 351 | StringImpl::copyChars(position, string->characters8(), length); |
| 352 | else |
| 353 | StringImpl::copyChars(position, string->characters16(), length); |
barraclough@apple.com | 35d680c | 2009-12-04 02:15:18 +0000 | [diff] [blame] | 354 | } |
ggaren@apple.com | ffbe44d | 2011-10-19 19:45:35 +0000 | [diff] [blame] | 355 | |
| 356 | ASSERT(buffer == position); |
barraclough@apple.com | 35d680c | 2009-12-04 02:15:18 +0000 | [diff] [blame] | 357 | } |
msaboff@apple.com | 8cae2b1 | 2011-04-21 17:23:16 +0000 | [diff] [blame] | 358 | |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 359 | void JSRopeString::outOfMemory(ExecState* exec) const |
msaboff@apple.com | 8cae2b1 | 2011-04-21 17:23:16 +0000 | [diff] [blame] | 360 | { |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 361 | clearFibers(); |
commit-queue@webkit.org | 7914d5a | 2012-02-23 19:29:14 +0000 | [diff] [blame] | 362 | ASSERT(isRope()); |
benjamin@webkit.org | cff06e4 | 2012-08-30 21:23:51 +0000 | [diff] [blame] | 363 | ASSERT(m_value.isNull()); |
msaboff@apple.com | 8cae2b1 | 2011-04-21 17:23:16 +0000 | [diff] [blame] | 364 | if (exec) |
| 365 | throwOutOfMemoryError(exec); |
| 366 | } |
| 367 | |
ggaren@apple.com | dc067b6 | 2009-05-01 22:43:39 +0000 | [diff] [blame] | 368 | JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 369 | { |
weinig@apple.com | 2947a91 | 2008-07-07 02:49:29 +0000 | [diff] [blame] | 370 | return const_cast<JSString*>(this); |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 371 | } |
| 372 | |
mhahnenberg@apple.com | 061133e | 2011-09-27 19:53:49 +0000 | [diff] [blame] | 373 | bool JSString::getPrimitiveNumber(ExecState* exec, double& number, JSValue& result) const |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 374 | { |
barraclough@apple.com | 35d680c | 2009-12-04 02:15:18 +0000 | [diff] [blame] | 375 | result = this; |
akling@apple.com | 7e84ac5 | 2015-05-19 17:06:23 +0000 | [diff] [blame] | 376 | number = jsToNumber(view(exec)); |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 377 | return false; |
| 378 | } |
| 379 | |
barraclough@apple.com | b749f0b | 2009-12-07 23:14:04 +0000 | [diff] [blame] | 380 | double JSString::toNumber(ExecState* exec) const |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 381 | { |
akling@apple.com | 7e84ac5 | 2015-05-19 17:06:23 +0000 | [diff] [blame] | 382 | return jsToNumber(view(exec)); |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 383 | } |
| 384 | |
akling@apple.com | 019809c | 2013-10-06 18:16:48 +0000 | [diff] [blame] | 385 | inline StringObject* StringObject::create(VM& vm, JSGlobalObject* globalObject, JSString* string) |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 386 | { |
akling@apple.com | 019809c | 2013-10-06 18:16:48 +0000 | [diff] [blame] | 387 | StringObject* object = new (NotNull, allocateCell<StringObject>(vm.heap)) StringObject(vm, globalObject->stringObjectStructure()); |
| 388 | object->finishCreation(vm, string); |
mhahnenberg@apple.com | 7317a7f | 2011-09-09 21:43:14 +0000 | [diff] [blame] | 389 | return object; |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 390 | } |
| 391 | |
oliver@apple.com | 3b6dc57 | 2011-03-28 23:39:16 +0000 | [diff] [blame] | 392 | JSObject* JSString::toObject(ExecState* exec, JSGlobalObject* globalObject) const |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 393 | { |
akling@apple.com | 019809c | 2013-10-06 18:16:48 +0000 | [diff] [blame] | 394 | return StringObject::create(exec->vm(), globalObject, const_cast<JSString*>(this)); |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 395 | } |
| 396 | |
oliver@apple.com | e2fe4ce | 2013-07-25 03:59:41 +0000 | [diff] [blame] | 397 | JSValue JSString::toThis(JSCell* cell, ExecState* exec, ECMAMode ecmaMode) |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 398 | { |
oliver@apple.com | e2fe4ce | 2013-07-25 03:59:41 +0000 | [diff] [blame] | 399 | if (ecmaMode == StrictMode) |
| 400 | return cell; |
akling@apple.com | 019809c | 2013-10-06 18:16:48 +0000 | [diff] [blame] | 401 | return StringObject::create(exec->vm(), exec->lexicalGlobalObject(), jsCast<JSString*>(cell)); |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 402 | } |
| 403 | |
barraclough@apple.com | 38d3c75 | 2012-05-12 00:39:43 +0000 | [diff] [blame] | 404 | bool JSString::getStringPropertyDescriptor(ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor) |
oliver@apple.com | 4b4f785 | 2009-08-26 16:52:15 +0000 | [diff] [blame] | 405 | { |
| 406 | if (propertyName == exec->propertyNames().length) { |
oliver@apple.com | 5b67d9e | 2010-10-25 22:40:53 +0000 | [diff] [blame] | 407 | descriptor.setDescriptor(jsNumber(m_length), DontEnum | DontDelete | ReadOnly); |
oliver@apple.com | 4b4f785 | 2009-08-26 16:52:15 +0000 | [diff] [blame] | 408 | return true; |
| 409 | } |
| 410 | |
utatane.tea@gmail.com | c7c203c | 2015-04-06 12:40:33 +0000 | [diff] [blame] | 411 | Optional<uint32_t> index = parseIndex(propertyName); |
| 412 | if (index && index.value() < m_length) { |
| 413 | descriptor.setDescriptor(getIndex(exec, index.value()), DontDelete | ReadOnly); |
oliver@apple.com | 4b4f785 | 2009-08-26 16:52:15 +0000 | [diff] [blame] | 414 | return true; |
| 415 | } |
| 416 | |
| 417 | return false; |
| 418 | } |
| 419 | |
akling@apple.com | c3e85a6 | 2014-07-05 03:36:36 +0000 | [diff] [blame] | 420 | JSString* jsStringWithCacheSlowCase(VM& vm, StringImpl& stringImpl) |
| 421 | { |
akling@apple.com | 926b110 | 2015-03-10 00:09:39 +0000 | [diff] [blame] | 422 | if (JSString* string = vm.stringCache.get(&stringImpl)) |
| 423 | return string; |
| 424 | |
| 425 | JSString* string = jsString(&vm, String(stringImpl)); |
| 426 | vm.lastCachedString.set(vm, string); |
| 427 | return string; |
akling@apple.com | c3e85a6 | 2014-07-05 03:36:36 +0000 | [diff] [blame] | 428 | } |
| 429 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 430 | } // namespace JSC |