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 | |
ggaren@apple.com | c09535a | 2015-10-06 01:59:02 +0000 | [diff] [blame] | 38 | Structure* JSString::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto) |
| 39 | { |
| 40 | return Structure::create(vm, globalObject, proto, TypeInfo(StringType, StructureFlags), info()); |
| 41 | } |
| 42 | |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 43 | void JSRopeString::RopeBuilder::expand() |
ggaren@apple.com | fbf6d9a | 2011-10-19 02:54:29 +0000 | [diff] [blame] | 44 | { |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 45 | ASSERT(m_index == JSRopeString::s_maxInternalRopeLength); |
ggaren@apple.com | fbf6d9a | 2011-10-19 02:54:29 +0000 | [diff] [blame] | 46 | JSString* jsString = m_jsString; |
fpizlo@apple.com | 75104a3 | 2014-04-15 23:33:11 +0000 | [diff] [blame] | 47 | RELEASE_ASSERT(jsString); |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 48 | m_jsString = jsStringBuilder(&m_vm); |
ggaren@apple.com | ffbe44d | 2011-10-19 19:45:35 +0000 | [diff] [blame] | 49 | m_index = 0; |
ggaren@apple.com | fbf6d9a | 2011-10-19 02:54:29 +0000 | [diff] [blame] | 50 | append(jsString); |
| 51 | } |
| 52 | |
mhahnenberg@apple.com | c58d54d | 2011-12-16 19:06:44 +0000 | [diff] [blame] | 53 | void JSString::destroy(JSCell* cell) |
ggaren@apple.com | fbf6d9a | 2011-10-19 02:54:29 +0000 | [diff] [blame] | 54 | { |
ggaren@apple.com | 72da811 | 2012-05-26 22:40:46 +0000 | [diff] [blame] | 55 | JSString* thisObject = static_cast<JSString*>(cell); |
mhahnenberg@apple.com | c58d54d | 2011-12-16 19:06:44 +0000 | [diff] [blame] | 56 | thisObject->JSString::~JSString(); |
ggaren@apple.com | fbf6d9a | 2011-10-19 02:54:29 +0000 | [diff] [blame] | 57 | } |
| 58 | |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 59 | void JSString::dumpToStream(const JSCell* cell, PrintStream& out) |
| 60 | { |
| 61 | const JSString* thisObject = jsCast<const JSString*>(cell); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 62 | out.printf("<%p, %s, [%u], ", thisObject, thisObject->className(), thisObject->length()); |
mhahnenberg@apple.com | 040ef24 | 2014-04-05 20:05:04 +0000 | [diff] [blame] | 63 | if (thisObject->isRope()) |
| 64 | out.printf("[rope]"); |
| 65 | else { |
| 66 | WTF::StringImpl* ourImpl = thisObject->m_value.impl(); |
| 67 | if (ourImpl->is8Bit()) |
| 68 | out.printf("[8 %p]", ourImpl->characters8()); |
| 69 | else |
| 70 | out.printf("[16 %p]", ourImpl->characters16()); |
| 71 | } |
| 72 | out.printf(">"); |
| 73 | } |
| 74 | |
mark.lam@apple.com | 0c26496 | 2016-08-15 21:52:22 +0000 | [diff] [blame^] | 75 | bool JSString::equalSlowCase(ExecState* exec, JSString* other) const |
| 76 | { |
| 77 | String str1 = value(exec); |
| 78 | String str2 = other->value(exec); |
| 79 | if (exec->hadException()) |
| 80 | return false; |
| 81 | return WTF::equal(*str1.impl(), *str2.impl()); |
| 82 | } |
| 83 | |
commit-queue@webkit.org | 00eb52f | 2016-03-01 02:07:12 +0000 | [diff] [blame] | 84 | size_t JSString::estimatedSize(JSCell* cell) |
| 85 | { |
| 86 | JSString* thisObject = jsCast<JSString*>(cell); |
| 87 | if (thisObject->isRope()) |
| 88 | return Base::estimatedSize(cell); |
| 89 | return Base::estimatedSize(cell) + thisObject->m_value.impl()->costDuringGC(); |
| 90 | } |
| 91 | |
ggaren@apple.com | fbf6d9a | 2011-10-19 02:54:29 +0000 | [diff] [blame] | 92 | void JSString::visitChildren(JSCell* cell, SlotVisitor& visitor) |
| 93 | { |
mhahnenberg@apple.com | 135f051 | 2011-11-11 20:40:10 +0000 | [diff] [blame] | 94 | JSString* thisObject = jsCast<JSString*>(cell); |
ggaren@apple.com | fbf6d9a | 2011-10-19 02:54:29 +0000 | [diff] [blame] | 95 | Base::visitChildren(thisObject, visitor); |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 96 | |
| 97 | if (thisObject->isRope()) |
| 98 | static_cast<JSRopeString*>(thisObject)->visitFibers(visitor); |
oliver@apple.com | 8cebf37 | 2013-07-25 04:03:46 +0000 | [diff] [blame] | 99 | else { |
| 100 | StringImpl* impl = thisObject->m_value.impl(); |
| 101 | ASSERT(impl); |
fpizlo@apple.com | 3cb36ea | 2015-10-05 19:35:32 +0000 | [diff] [blame] | 102 | visitor.reportExtraMemoryVisited(impl->costDuringGC()); |
oliver@apple.com | 8cebf37 | 2013-07-25 04:03:46 +0000 | [diff] [blame] | 103 | } |
ggaren@apple.com | fbf6d9a | 2011-10-19 02:54:29 +0000 | [diff] [blame] | 104 | } |
| 105 | |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 106 | void JSRopeString::visitFibers(SlotVisitor& visitor) |
| 107 | { |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 108 | if (isSubstring()) { |
| 109 | visitor.append(&substringBase()); |
| 110 | return; |
| 111 | } |
| 112 | for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) |
| 113 | visitor.append(&fiber(i)); |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 114 | } |
| 115 | |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 116 | static const unsigned maxLengthForOnStackResolve = 2048; |
| 117 | |
| 118 | void JSRopeString::resolveRopeInternal8(LChar* buffer) const |
| 119 | { |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 120 | if (isSubstring()) { |
| 121 | StringImpl::copyChars( |
| 122 | buffer, substringBase()->m_value.characters8() + substringOffset(), m_length); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | resolveRopeInternal8NoSubstring(buffer); |
| 127 | } |
| 128 | |
| 129 | void JSRopeString::resolveRopeInternal8NoSubstring(LChar* buffer) const |
| 130 | { |
| 131 | for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) { |
| 132 | if (fiber(i)->isRope()) { |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 133 | resolveRopeSlowCase8(buffer); |
| 134 | return; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | LChar* position = buffer; |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 139 | for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) { |
| 140 | const StringImpl& fiberString = *fiber(i)->m_value.impl(); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 141 | unsigned length = fiberString.length(); |
| 142 | StringImpl::copyChars(position, fiberString.characters8(), length); |
| 143 | position += length; |
| 144 | } |
| 145 | ASSERT((buffer + m_length) == position); |
| 146 | } |
| 147 | |
| 148 | void JSRopeString::resolveRopeInternal16(UChar* buffer) const |
| 149 | { |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 150 | if (isSubstring()) { |
| 151 | StringImpl::copyChars( |
| 152 | buffer, substringBase()->m_value.characters16() + substringOffset(), m_length); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | resolveRopeInternal16NoSubstring(buffer); |
| 157 | } |
| 158 | |
| 159 | void JSRopeString::resolveRopeInternal16NoSubstring(UChar* buffer) const |
| 160 | { |
| 161 | for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) { |
| 162 | if (fiber(i)->isRope()) { |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 163 | resolveRopeSlowCase(buffer); |
| 164 | return; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | UChar* position = buffer; |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 169 | for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) { |
| 170 | const StringImpl& fiberString = *fiber(i)->m_value.impl(); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 171 | unsigned length = fiberString.length(); |
| 172 | if (fiberString.is8Bit()) |
| 173 | StringImpl::copyChars(position, fiberString.characters8(), length); |
| 174 | else |
| 175 | StringImpl::copyChars(position, fiberString.characters16(), length); |
| 176 | position += length; |
| 177 | } |
| 178 | ASSERT((buffer + m_length) == position); |
| 179 | } |
| 180 | |
| 181 | void JSRopeString::resolveRopeToAtomicString(ExecState* exec) const |
| 182 | { |
| 183 | if (m_length > maxLengthForOnStackResolve) { |
| 184 | resolveRope(exec); |
| 185 | m_value = AtomicString(m_value); |
akling@apple.com | a8c49de | 2014-08-18 21:33:01 +0000 | [diff] [blame] | 186 | setIs8Bit(m_value.impl()->is8Bit()); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 187 | return; |
| 188 | } |
| 189 | |
| 190 | if (is8Bit()) { |
| 191 | LChar buffer[maxLengthForOnStackResolve]; |
| 192 | resolveRopeInternal8(buffer); |
| 193 | m_value = AtomicString(buffer, m_length); |
akling@apple.com | a8c49de | 2014-08-18 21:33:01 +0000 | [diff] [blame] | 194 | setIs8Bit(m_value.impl()->is8Bit()); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 195 | } else { |
| 196 | UChar buffer[maxLengthForOnStackResolve]; |
| 197 | resolveRopeInternal16(buffer); |
| 198 | m_value = AtomicString(buffer, m_length); |
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 | } |
| 201 | |
| 202 | clearFibers(); |
| 203 | |
| 204 | // If we resolved a string that didn't previously exist, notify the heap that we've grown. |
| 205 | if (m_value.impl()->hasOneRef()) |
ggaren@apple.com | e2ebb8c | 2015-03-11 21:29:57 +0000 | [diff] [blame] | 206 | Heap::heap(this)->reportExtraMemoryAllocated(m_value.impl()->cost()); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void JSRopeString::clearFibers() const |
| 210 | { |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 211 | for (size_t i = 0; i < s_maxInternalRopeLength; ++i) |
| 212 | u[i].number = 0; |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 213 | } |
| 214 | |
utatane.tea@gmail.com | e0741fb | 2015-06-02 17:36:16 +0000 | [diff] [blame] | 215 | RefPtr<AtomicStringImpl> JSRopeString::resolveRopeToExistingAtomicString(ExecState* exec) const |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 216 | { |
| 217 | if (m_length > maxLengthForOnStackResolve) { |
| 218 | resolveRope(exec); |
utatane.tea@gmail.com | e0741fb | 2015-06-02 17:36:16 +0000 | [diff] [blame] | 219 | if (RefPtr<AtomicStringImpl> existingAtomicString = AtomicStringImpl::lookUp(m_value.impl())) { |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 220 | m_value = *existingAtomicString; |
akling@apple.com | a8c49de | 2014-08-18 21:33:01 +0000 | [diff] [blame] | 221 | setIs8Bit(m_value.impl()->is8Bit()); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 222 | clearFibers(); |
| 223 | return existingAtomicString; |
| 224 | } |
| 225 | return nullptr; |
| 226 | } |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 227 | |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 228 | if (is8Bit()) { |
| 229 | LChar buffer[maxLengthForOnStackResolve]; |
| 230 | resolveRopeInternal8(buffer); |
utatane.tea@gmail.com | e0741fb | 2015-06-02 17:36:16 +0000 | [diff] [blame] | 231 | if (RefPtr<AtomicStringImpl> existingAtomicString = AtomicStringImpl::lookUp(buffer, m_length)) { |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 232 | m_value = *existingAtomicString; |
akling@apple.com | a8c49de | 2014-08-18 21:33:01 +0000 | [diff] [blame] | 233 | setIs8Bit(m_value.impl()->is8Bit()); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 234 | clearFibers(); |
| 235 | return existingAtomicString; |
| 236 | } |
| 237 | } else { |
| 238 | UChar buffer[maxLengthForOnStackResolve]; |
| 239 | resolveRopeInternal16(buffer); |
utatane.tea@gmail.com | e0741fb | 2015-06-02 17:36:16 +0000 | [diff] [blame] | 240 | if (RefPtr<AtomicStringImpl> existingAtomicString = AtomicStringImpl::lookUp(buffer, m_length)) { |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 241 | m_value = *existingAtomicString; |
akling@apple.com | a8c49de | 2014-08-18 21:33:01 +0000 | [diff] [blame] | 242 | setIs8Bit(m_value.impl()->is8Bit()); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 243 | clearFibers(); |
| 244 | return existingAtomicString; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | return nullptr; |
| 249 | } |
| 250 | |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 251 | void JSRopeString::resolveRope(ExecState* exec) const |
msaboff@apple.com | 8cae2b1 | 2011-04-21 17:23:16 +0000 | [diff] [blame] | 252 | { |
| 253 | ASSERT(isRope()); |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 254 | |
| 255 | if (isSubstring()) { |
| 256 | ASSERT(!substringBase()->isRope()); |
akling@apple.com | 46f3fe3 | 2016-02-18 17:25:42 +0000 | [diff] [blame] | 257 | m_value = substringBase()->m_value.substringSharingImpl(substringOffset(), m_length); |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 258 | substringBase().clear(); |
| 259 | return; |
| 260 | } |
| 261 | |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 262 | if (is8Bit()) { |
| 263 | LChar* buffer; |
krollin@apple.com | 19cde09 | 2016-06-08 01:31:17 +0000 | [diff] [blame] | 264 | if (auto newImpl = StringImpl::tryCreateUninitialized(m_length, buffer)) { |
ggaren@apple.com | e2ebb8c | 2015-03-11 21:29:57 +0000 | [diff] [blame] | 265 | Heap::heap(this)->reportExtraMemoryAllocated(newImpl->cost()); |
krollin@apple.com | 19cde09 | 2016-06-08 01:31:17 +0000 | [diff] [blame] | 266 | m_value = WTFMove(newImpl); |
commit-queue@webkit.org | 1a15efe | 2012-02-28 16:02:02 +0000 | [diff] [blame] | 267 | } else { |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 268 | outOfMemory(exec); |
| 269 | return; |
| 270 | } |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 271 | resolveRopeInternal8NoSubstring(buffer); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 272 | clearFibers(); |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 273 | ASSERT(!isRope()); |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 274 | return; |
| 275 | } |
| 276 | |
msaboff@apple.com | 8cae2b1 | 2011-04-21 17:23:16 +0000 | [diff] [blame] | 277 | UChar* buffer; |
krollin@apple.com | 19cde09 | 2016-06-08 01:31:17 +0000 | [diff] [blame] | 278 | if (auto newImpl = StringImpl::tryCreateUninitialized(m_length, buffer)) { |
ggaren@apple.com | e2ebb8c | 2015-03-11 21:29:57 +0000 | [diff] [blame] | 279 | Heap::heap(this)->reportExtraMemoryAllocated(newImpl->cost()); |
krollin@apple.com | 19cde09 | 2016-06-08 01:31:17 +0000 | [diff] [blame] | 280 | m_value = WTFMove(newImpl); |
commit-queue@webkit.org | 1a15efe | 2012-02-28 16:02:02 +0000 | [diff] [blame] | 281 | } else { |
msaboff@apple.com | 8cae2b1 | 2011-04-21 17:23:16 +0000 | [diff] [blame] | 282 | outOfMemory(exec); |
| 283 | return; |
| 284 | } |
| 285 | |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 286 | resolveRopeInternal16NoSubstring(buffer); |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 287 | clearFibers(); |
msaboff@apple.com | 8cae2b1 | 2011-04-21 17:23:16 +0000 | [diff] [blame] | 288 | ASSERT(!isRope()); |
| 289 | } |
| 290 | |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 291 | // 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] | 292 | // 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] | 293 | // backwards, since we want to avoid recursion, we expect that the tree structure |
| 294 | // representing the rope is likely imbalanced with more nodes down the left side |
| 295 | // (since appending to the string is likely more common) - and as such resolving |
| 296 | // 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] | 297 | // 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] | 298 | // Vector before performing any concatenation, but by working backwards we likely |
| 299 | // 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] | 300 | // rope-of-ropes.) |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 301 | void JSRopeString::resolveRopeSlowCase8(LChar* buffer) const |
barraclough@apple.com | 35d680c | 2009-12-04 02:15:18 +0000 | [diff] [blame] | 302 | { |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 303 | 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] | 304 | 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] | 305 | |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 306 | for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) |
| 307 | workQueue.append(fiber(i).get()); |
ggaren@apple.com | ffbe44d | 2011-10-19 19:45:35 +0000 | [diff] [blame] | 308 | |
| 309 | while (!workQueue.isEmpty()) { |
| 310 | JSString* currentFiber = workQueue.last(); |
| 311 | workQueue.removeLast(); |
akling@apple.com | 77ee6bf | 2014-05-05 06:10:03 +0000 | [diff] [blame] | 312 | |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 313 | const LChar* characters; |
| 314 | |
ggaren@apple.com | fbf6d9a | 2011-10-19 02:54:29 +0000 | [diff] [blame] | 315 | if (currentFiber->isRope()) { |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 316 | JSRopeString* currentFiberAsRope = static_cast<JSRopeString*>(currentFiber); |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 317 | if (!currentFiberAsRope->isSubstring()) { |
| 318 | for (size_t i = 0; i < s_maxInternalRopeLength && currentFiberAsRope->fiber(i); ++i) |
| 319 | workQueue.append(currentFiberAsRope->fiber(i).get()); |
| 320 | continue; |
| 321 | } |
| 322 | ASSERT(!currentFiberAsRope->substringBase()->isRope()); |
| 323 | characters = |
| 324 | currentFiberAsRope->substringBase()->m_value.characters8() + |
| 325 | currentFiberAsRope->substringOffset(); |
| 326 | } else |
| 327 | characters = currentFiber->m_value.characters8(); |
| 328 | |
| 329 | unsigned length = currentFiber->length(); |
ggaren@apple.com | ffbe44d | 2011-10-19 19:45:35 +0000 | [diff] [blame] | 330 | position -= length; |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 331 | StringImpl::copyChars(position, characters, length); |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | ASSERT(buffer == position); |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 335 | } |
| 336 | |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 337 | void JSRopeString::resolveRopeSlowCase(UChar* buffer) const |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 338 | { |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 339 | 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] | 340 | 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] | 341 | |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 342 | for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) |
| 343 | workQueue.append(fiber(i).get()); |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 344 | |
| 345 | while (!workQueue.isEmpty()) { |
| 346 | JSString* currentFiber = workQueue.last(); |
| 347 | workQueue.removeLast(); |
| 348 | |
| 349 | if (currentFiber->isRope()) { |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 350 | JSRopeString* currentFiberAsRope = static_cast<JSRopeString*>(currentFiber); |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 351 | if (currentFiberAsRope->isSubstring()) { |
| 352 | ASSERT(!currentFiberAsRope->substringBase()->isRope()); |
| 353 | StringImpl* string = static_cast<StringImpl*>( |
| 354 | currentFiberAsRope->substringBase()->m_value.impl()); |
| 355 | unsigned offset = currentFiberAsRope->substringOffset(); |
| 356 | unsigned length = currentFiberAsRope->length(); |
| 357 | position -= length; |
| 358 | if (string->is8Bit()) |
| 359 | StringImpl::copyChars(position, string->characters8() + offset, length); |
| 360 | else |
| 361 | StringImpl::copyChars(position, string->characters16() + offset, length); |
| 362 | continue; |
| 363 | } |
| 364 | for (size_t i = 0; i < s_maxInternalRopeLength && currentFiberAsRope->fiber(i); ++i) |
| 365 | workQueue.append(currentFiberAsRope->fiber(i).get()); |
msaboff@apple.com | 203e785 | 2011-11-14 23:51:06 +0000 | [diff] [blame] | 366 | continue; |
| 367 | } |
| 368 | |
| 369 | StringImpl* string = static_cast<StringImpl*>(currentFiber->m_value.impl()); |
| 370 | unsigned length = string->length(); |
| 371 | position -= length; |
msaboff@apple.com | f25bc16 | 2012-09-07 01:29:12 +0000 | [diff] [blame] | 372 | if (string->is8Bit()) |
| 373 | StringImpl::copyChars(position, string->characters8(), length); |
| 374 | else |
| 375 | StringImpl::copyChars(position, string->characters16(), length); |
barraclough@apple.com | 35d680c | 2009-12-04 02:15:18 +0000 | [diff] [blame] | 376 | } |
ggaren@apple.com | ffbe44d | 2011-10-19 19:45:35 +0000 | [diff] [blame] | 377 | |
| 378 | ASSERT(buffer == position); |
barraclough@apple.com | 35d680c | 2009-12-04 02:15:18 +0000 | [diff] [blame] | 379 | } |
msaboff@apple.com | 8cae2b1 | 2011-04-21 17:23:16 +0000 | [diff] [blame] | 380 | |
msaboff@apple.com | 6e74e92 | 2012-04-27 23:51:17 +0000 | [diff] [blame] | 381 | void JSRopeString::outOfMemory(ExecState* exec) const |
msaboff@apple.com | 8cae2b1 | 2011-04-21 17:23:16 +0000 | [diff] [blame] | 382 | { |
akling@apple.com | 63e3543 | 2014-05-05 06:24:44 +0000 | [diff] [blame] | 383 | clearFibers(); |
commit-queue@webkit.org | 7914d5a | 2012-02-23 19:29:14 +0000 | [diff] [blame] | 384 | ASSERT(isRope()); |
benjamin@webkit.org | cff06e4 | 2012-08-30 21:23:51 +0000 | [diff] [blame] | 385 | ASSERT(m_value.isNull()); |
msaboff@apple.com | 8cae2b1 | 2011-04-21 17:23:16 +0000 | [diff] [blame] | 386 | if (exec) |
| 387 | throwOutOfMemoryError(exec); |
| 388 | } |
| 389 | |
ggaren@apple.com | dc067b6 | 2009-05-01 22:43:39 +0000 | [diff] [blame] | 390 | JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 391 | { |
weinig@apple.com | 2947a91 | 2008-07-07 02:49:29 +0000 | [diff] [blame] | 392 | return const_cast<JSString*>(this); |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 393 | } |
| 394 | |
mhahnenberg@apple.com | 061133e | 2011-09-27 19:53:49 +0000 | [diff] [blame] | 395 | bool JSString::getPrimitiveNumber(ExecState* exec, double& number, JSValue& result) const |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 396 | { |
barraclough@apple.com | 35d680c | 2009-12-04 02:15:18 +0000 | [diff] [blame] | 397 | result = this; |
akling@apple.com | c115b37 | 2015-12-19 02:32:46 +0000 | [diff] [blame] | 398 | number = jsToNumber(unsafeView(*exec)); |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 399 | return false; |
| 400 | } |
| 401 | |
barraclough@apple.com | b749f0b | 2009-12-07 23:14:04 +0000 | [diff] [blame] | 402 | double JSString::toNumber(ExecState* exec) const |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 403 | { |
akling@apple.com | c115b37 | 2015-12-19 02:32:46 +0000 | [diff] [blame] | 404 | return jsToNumber(unsafeView(*exec)); |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 405 | } |
| 406 | |
akling@apple.com | 019809c | 2013-10-06 18:16:48 +0000 | [diff] [blame] | 407 | inline StringObject* StringObject::create(VM& vm, JSGlobalObject* globalObject, JSString* string) |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 408 | { |
akling@apple.com | 019809c | 2013-10-06 18:16:48 +0000 | [diff] [blame] | 409 | StringObject* object = new (NotNull, allocateCell<StringObject>(vm.heap)) StringObject(vm, globalObject->stringObjectStructure()); |
| 410 | object->finishCreation(vm, string); |
mhahnenberg@apple.com | 7317a7f | 2011-09-09 21:43:14 +0000 | [diff] [blame] | 411 | return object; |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 412 | } |
| 413 | |
oliver@apple.com | 3b6dc57 | 2011-03-28 23:39:16 +0000 | [diff] [blame] | 414 | JSObject* JSString::toObject(ExecState* exec, JSGlobalObject* globalObject) const |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 415 | { |
akling@apple.com | 019809c | 2013-10-06 18:16:48 +0000 | [diff] [blame] | 416 | return StringObject::create(exec->vm(), globalObject, const_cast<JSString*>(this)); |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 417 | } |
| 418 | |
oliver@apple.com | e2fe4ce | 2013-07-25 03:59:41 +0000 | [diff] [blame] | 419 | JSValue JSString::toThis(JSCell* cell, ExecState* exec, ECMAMode ecmaMode) |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 420 | { |
oliver@apple.com | e2fe4ce | 2013-07-25 03:59:41 +0000 | [diff] [blame] | 421 | if (ecmaMode == StrictMode) |
| 422 | return cell; |
akling@apple.com | 019809c | 2013-10-06 18:16:48 +0000 | [diff] [blame] | 423 | return StringObject::create(exec->vm(), exec->lexicalGlobalObject(), jsCast<JSString*>(cell)); |
weinig@apple.com | efdce0f | 2008-06-30 20:52:03 +0000 | [diff] [blame] | 424 | } |
| 425 | |
barraclough@apple.com | 38d3c75 | 2012-05-12 00:39:43 +0000 | [diff] [blame] | 426 | bool JSString::getStringPropertyDescriptor(ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor) |
oliver@apple.com | 4b4f785 | 2009-08-26 16:52:15 +0000 | [diff] [blame] | 427 | { |
| 428 | if (propertyName == exec->propertyNames().length) { |
oliver@apple.com | 5b67d9e | 2010-10-25 22:40:53 +0000 | [diff] [blame] | 429 | descriptor.setDescriptor(jsNumber(m_length), DontEnum | DontDelete | ReadOnly); |
oliver@apple.com | 4b4f785 | 2009-08-26 16:52:15 +0000 | [diff] [blame] | 430 | return true; |
| 431 | } |
| 432 | |
utatane.tea@gmail.com | c7c203c | 2015-04-06 12:40:33 +0000 | [diff] [blame] | 433 | Optional<uint32_t> index = parseIndex(propertyName); |
| 434 | if (index && index.value() < m_length) { |
| 435 | descriptor.setDescriptor(getIndex(exec, index.value()), DontDelete | ReadOnly); |
oliver@apple.com | 4b4f785 | 2009-08-26 16:52:15 +0000 | [diff] [blame] | 436 | return true; |
| 437 | } |
| 438 | |
| 439 | return false; |
| 440 | } |
| 441 | |
akling@apple.com | c3e85a6 | 2014-07-05 03:36:36 +0000 | [diff] [blame] | 442 | JSString* jsStringWithCacheSlowCase(VM& vm, StringImpl& stringImpl) |
| 443 | { |
akling@apple.com | 926b110 | 2015-03-10 00:09:39 +0000 | [diff] [blame] | 444 | if (JSString* string = vm.stringCache.get(&stringImpl)) |
| 445 | return string; |
| 446 | |
| 447 | JSString* string = jsString(&vm, String(stringImpl)); |
| 448 | vm.lastCachedString.set(vm, string); |
| 449 | return string; |
akling@apple.com | c3e85a6 | 2014-07-05 03:36:36 +0000 | [diff] [blame] | 450 | } |
| 451 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 452 | } // namespace JSC |