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