blob: 34813a2ed33e6cfc0311c8cf9106b68c521d3adc [file] [log] [blame]
weinig@apple.comefdce0f2008-06-30 20:52:03 +00001/*
2 * Copyright (C) 1999-2002 Harri Porten (porten@kde.org)
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
fpizlo@apple.com9b46f342017-05-11 23:05:01 +00004 * Copyright (C) 2004-2017 Apple Inc. All rights reserved.
weinig@apple.comefdce0f2008-06-30 20:52:03 +00005 *
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.com794f4612010-08-18 07:41:22 +000027#include "JSGlobalObjectFunctions.h"
weinig@apple.comefdce0f2008-06-30 20:52:03 +000028#include "JSObject.h"
fpizlo@apple.comfb7eff22014-02-11 01:45:50 +000029#include "JSCInlines.h"
weinig@apple.comefdce0f2008-06-30 20:52:03 +000030#include "StringObject.h"
31#include "StringPrototype.h"
akling@apple.comede8a982014-07-08 22:36:59 +000032#include "StrongInlines.h"
weinig@apple.comefdce0f2008-06-30 20:52:03 +000033
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +000034namespace JSC {
antti@apple.com6261f1f2010-12-07 11:11:08 +000035
utatane.tea@gmail.coma5544f12017-05-19 09:23:20 +000036const ClassInfo JSString::s_info = { "string", nullptr, nullptr, nullptr, CREATE_METHOD_TABLE(JSString) };
oliver@apple.com23ce68f2011-04-25 21:21:28 +000037
ggaren@apple.comc09535a2015-10-06 01:59:02 +000038Structure* JSString::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto)
39{
40 return Structure::create(vm, globalObject, proto, TypeInfo(StringType, StructureFlags), info());
41}
42
msaboff@apple.com6e74e922012-04-27 23:51:17 +000043void JSRopeString::RopeBuilder::expand()
ggaren@apple.comfbf6d9a2011-10-19 02:54:29 +000044{
msaboff@apple.com6e74e922012-04-27 23:51:17 +000045 ASSERT(m_index == JSRopeString::s_maxInternalRopeLength);
ggaren@apple.comfbf6d9a2011-10-19 02:54:29 +000046 JSString* jsString = m_jsString;
fpizlo@apple.com75104a32014-04-15 23:33:11 +000047 RELEASE_ASSERT(jsString);
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +000048 m_jsString = jsStringBuilder(&m_vm);
ggaren@apple.comffbe44d2011-10-19 19:45:35 +000049 m_index = 0;
ggaren@apple.comfbf6d9a2011-10-19 02:54:29 +000050 append(jsString);
51}
52
mhahnenberg@apple.comc58d54d2011-12-16 19:06:44 +000053void JSString::destroy(JSCell* cell)
ggaren@apple.comfbf6d9a2011-10-19 02:54:29 +000054{
darin@apple.comb674d3a2016-12-16 04:52:20 +000055 static_cast<JSString*>(cell)->JSString::~JSString();
ggaren@apple.comfbf6d9a2011-10-19 02:54:29 +000056}
57
mhahnenberg@apple.com040ef242014-04-05 20:05:04 +000058void JSString::dumpToStream(const JSCell* cell, PrintStream& out)
59{
keith_miller@apple.com45da7602017-01-27 01:47:52 +000060 VM& vm = *cell->vm();
mhahnenberg@apple.com040ef242014-04-05 20:05:04 +000061 const JSString* thisObject = jsCast<const JSString*>(cell);
keith_miller@apple.com45da7602017-01-27 01:47:52 +000062 out.printf("<%p, %s, [%u], ", thisObject, thisObject->className(vm), thisObject->length());
mhahnenberg@apple.com040ef242014-04-05 20:05:04 +000063 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.com0c264962016-08-15 21:52:22 +000075bool JSString::equalSlowCase(ExecState* exec, JSString* other) const
76{
mark.lam@apple.com451de992016-09-07 22:10:50 +000077 VM& vm = exec->vm();
78 auto scope = DECLARE_THROW_SCOPE(vm);
mark.lam@apple.com0c264962016-08-15 21:52:22 +000079 String str1 = value(exec);
80 String str2 = other->value(exec);
mark.lam@apple.come1ab17c2016-09-26 19:11:17 +000081 RETURN_IF_EXCEPTION(scope, false);
mark.lam@apple.com0c264962016-08-15 21:52:22 +000082 return WTF::equal(*str1.impl(), *str2.impl());
83}
84
commit-queue@webkit.org00eb52f2016-03-01 02:07:12 +000085size_t JSString::estimatedSize(JSCell* cell)
86{
darin@apple.comb674d3a2016-12-16 04:52:20 +000087 JSString* thisObject = asString(cell);
commit-queue@webkit.org00eb52f2016-03-01 02:07:12 +000088 if (thisObject->isRope())
89 return Base::estimatedSize(cell);
90 return Base::estimatedSize(cell) + thisObject->m_value.impl()->costDuringGC();
91}
92
ggaren@apple.comfbf6d9a2011-10-19 02:54:29 +000093void JSString::visitChildren(JSCell* cell, SlotVisitor& visitor)
94{
darin@apple.comb674d3a2016-12-16 04:52:20 +000095 JSString* thisObject = asString(cell);
ggaren@apple.comfbf6d9a2011-10-19 02:54:29 +000096 Base::visitChildren(thisObject, visitor);
msaboff@apple.com6e74e922012-04-27 23:51:17 +000097
98 if (thisObject->isRope())
99 static_cast<JSRopeString*>(thisObject)->visitFibers(visitor);
fpizlo@apple.com0ef43952016-12-08 22:14:50 +0000100 if (StringImpl* impl = thisObject->m_value.impl())
fpizlo@apple.com3cb36ea2015-10-05 19:35:32 +0000101 visitor.reportExtraMemoryVisited(impl->costDuringGC());
ggaren@apple.comfbf6d9a2011-10-19 02:54:29 +0000102}
103
msaboff@apple.com6e74e922012-04-27 23:51:17 +0000104void JSRopeString::visitFibers(SlotVisitor& visitor)
105{
fpizlo@apple.com4c6b8ad2014-07-22 21:08:50 +0000106 if (isSubstring()) {
fpizlo@apple.comf7240e02016-12-16 02:16:19 +0000107 visitor.append(substringBase());
fpizlo@apple.com4c6b8ad2014-07-22 21:08:50 +0000108 return;
109 }
110 for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i)
fpizlo@apple.comf7240e02016-12-16 02:16:19 +0000111 visitor.append(fiber(i));
msaboff@apple.com6e74e922012-04-27 23:51:17 +0000112}
113
akling@apple.com63e35432014-05-05 06:24:44 +0000114static const unsigned maxLengthForOnStackResolve = 2048;
115
116void JSRopeString::resolveRopeInternal8(LChar* buffer) const
117{
fpizlo@apple.com4c6b8ad2014-07-22 21:08:50 +0000118 if (isSubstring()) {
119 StringImpl::copyChars(
mark.lam@apple.com88445052016-10-25 22:19:37 +0000120 buffer, substringBase()->m_value.characters8() + substringOffset(), length());
fpizlo@apple.com4c6b8ad2014-07-22 21:08:50 +0000121 return;
122 }
123
124 resolveRopeInternal8NoSubstring(buffer);
125}
126
127void 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.com63e35432014-05-05 06:24:44 +0000131 resolveRopeSlowCase8(buffer);
132 return;
133 }
134 }
135
136 LChar* position = buffer;
fpizlo@apple.com4c6b8ad2014-07-22 21:08:50 +0000137 for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) {
138 const StringImpl& fiberString = *fiber(i)->m_value.impl();
akling@apple.com63e35432014-05-05 06:24:44 +0000139 unsigned length = fiberString.length();
140 StringImpl::copyChars(position, fiberString.characters8(), length);
141 position += length;
142 }
mark.lam@apple.com88445052016-10-25 22:19:37 +0000143 ASSERT((buffer + length()) == position);
akling@apple.com63e35432014-05-05 06:24:44 +0000144}
145
146void JSRopeString::resolveRopeInternal16(UChar* buffer) const
147{
fpizlo@apple.com4c6b8ad2014-07-22 21:08:50 +0000148 if (isSubstring()) {
149 StringImpl::copyChars(
mark.lam@apple.com88445052016-10-25 22:19:37 +0000150 buffer, substringBase()->m_value.characters16() + substringOffset(), length());
fpizlo@apple.com4c6b8ad2014-07-22 21:08:50 +0000151 return;
152 }
153
154 resolveRopeInternal16NoSubstring(buffer);
155}
156
157void 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.com63e35432014-05-05 06:24:44 +0000161 resolveRopeSlowCase(buffer);
162 return;
163 }
164 }
165
166 UChar* position = buffer;
fpizlo@apple.com4c6b8ad2014-07-22 21:08:50 +0000167 for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) {
168 const StringImpl& fiberString = *fiber(i)->m_value.impl();
akling@apple.com63e35432014-05-05 06:24:44 +0000169 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.com88445052016-10-25 22:19:37 +0000176 ASSERT((buffer + length()) == position);
akling@apple.com63e35432014-05-05 06:24:44 +0000177}
178
179void JSRopeString::resolveRopeToAtomicString(ExecState* exec) const
180{
mark.lam@apple.com88445052016-10-25 22:19:37 +0000181 if (length() > maxLengthForOnStackResolve) {
akling@apple.com63e35432014-05-05 06:24:44 +0000182 resolveRope(exec);
183 m_value = AtomicString(m_value);
akling@apple.coma8c49de2014-08-18 21:33:01 +0000184 setIs8Bit(m_value.impl()->is8Bit());
akling@apple.com63e35432014-05-05 06:24:44 +0000185 return;
186 }
187
188 if (is8Bit()) {
189 LChar buffer[maxLengthForOnStackResolve];
190 resolveRopeInternal8(buffer);
mark.lam@apple.com88445052016-10-25 22:19:37 +0000191 m_value = AtomicString(buffer, length());
akling@apple.coma8c49de2014-08-18 21:33:01 +0000192 setIs8Bit(m_value.impl()->is8Bit());
akling@apple.com63e35432014-05-05 06:24:44 +0000193 } else {
194 UChar buffer[maxLengthForOnStackResolve];
195 resolveRopeInternal16(buffer);
mark.lam@apple.com88445052016-10-25 22:19:37 +0000196 m_value = AtomicString(buffer, length());
akling@apple.coma8c49de2014-08-18 21:33:01 +0000197 setIs8Bit(m_value.impl()->is8Bit());
akling@apple.com63e35432014-05-05 06:24:44 +0000198 }
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.come2ebb8c2015-03-11 21:29:57 +0000204 Heap::heap(this)->reportExtraMemoryAllocated(m_value.impl()->cost());
akling@apple.com63e35432014-05-05 06:24:44 +0000205}
206
207void JSRopeString::clearFibers() const
208{
fpizlo@apple.com4c6b8ad2014-07-22 21:08:50 +0000209 for (size_t i = 0; i < s_maxInternalRopeLength; ++i)
210 u[i].number = 0;
akling@apple.com63e35432014-05-05 06:24:44 +0000211}
212
utatane.tea@gmail.come0741fb2015-06-02 17:36:16 +0000213RefPtr<AtomicStringImpl> JSRopeString::resolveRopeToExistingAtomicString(ExecState* exec) const
akling@apple.com63e35432014-05-05 06:24:44 +0000214{
mark.lam@apple.com88445052016-10-25 22:19:37 +0000215 if (length() > maxLengthForOnStackResolve) {
akling@apple.com63e35432014-05-05 06:24:44 +0000216 resolveRope(exec);
utatane.tea@gmail.come0741fb2015-06-02 17:36:16 +0000217 if (RefPtr<AtomicStringImpl> existingAtomicString = AtomicStringImpl::lookUp(m_value.impl())) {
akling@apple.com63e35432014-05-05 06:24:44 +0000218 m_value = *existingAtomicString;
akling@apple.coma8c49de2014-08-18 21:33:01 +0000219 setIs8Bit(m_value.impl()->is8Bit());
akling@apple.com63e35432014-05-05 06:24:44 +0000220 clearFibers();
221 return existingAtomicString;
222 }
223 return nullptr;
224 }
fpizlo@apple.com4c6b8ad2014-07-22 21:08:50 +0000225
akling@apple.com63e35432014-05-05 06:24:44 +0000226 if (is8Bit()) {
227 LChar buffer[maxLengthForOnStackResolve];
228 resolveRopeInternal8(buffer);
mark.lam@apple.com88445052016-10-25 22:19:37 +0000229 if (RefPtr<AtomicStringImpl> existingAtomicString = AtomicStringImpl::lookUp(buffer, length())) {
akling@apple.com63e35432014-05-05 06:24:44 +0000230 m_value = *existingAtomicString;
akling@apple.coma8c49de2014-08-18 21:33:01 +0000231 setIs8Bit(m_value.impl()->is8Bit());
akling@apple.com63e35432014-05-05 06:24:44 +0000232 clearFibers();
233 return existingAtomicString;
234 }
235 } else {
236 UChar buffer[maxLengthForOnStackResolve];
237 resolveRopeInternal16(buffer);
mark.lam@apple.com88445052016-10-25 22:19:37 +0000238 if (RefPtr<AtomicStringImpl> existingAtomicString = AtomicStringImpl::lookUp(buffer, length())) {
akling@apple.com63e35432014-05-05 06:24:44 +0000239 m_value = *existingAtomicString;
akling@apple.coma8c49de2014-08-18 21:33:01 +0000240 setIs8Bit(m_value.impl()->is8Bit());
akling@apple.com63e35432014-05-05 06:24:44 +0000241 clearFibers();
242 return existingAtomicString;
243 }
244 }
245
246 return nullptr;
247}
248
msaboff@apple.com6e74e922012-04-27 23:51:17 +0000249void JSRopeString::resolveRope(ExecState* exec) const
msaboff@apple.com8cae2b12011-04-21 17:23:16 +0000250{
251 ASSERT(isRope());
fpizlo@apple.com4c6b8ad2014-07-22 21:08:50 +0000252
253 if (isSubstring()) {
254 ASSERT(!substringBase()->isRope());
mark.lam@apple.com88445052016-10-25 22:19:37 +0000255 m_value = substringBase()->m_value.substringSharingImpl(substringOffset(), length());
fpizlo@apple.com4c6b8ad2014-07-22 21:08:50 +0000256 substringBase().clear();
257 return;
258 }
259
msaboff@apple.com203e7852011-11-14 23:51:06 +0000260 if (is8Bit()) {
261 LChar* buffer;
mark.lam@apple.com88445052016-10-25 22:19:37 +0000262 if (auto newImpl = StringImpl::tryCreateUninitialized(length(), buffer)) {
ggaren@apple.come2ebb8c2015-03-11 21:29:57 +0000263 Heap::heap(this)->reportExtraMemoryAllocated(newImpl->cost());
krollin@apple.com19cde092016-06-08 01:31:17 +0000264 m_value = WTFMove(newImpl);
commit-queue@webkit.org1a15efe2012-02-28 16:02:02 +0000265 } else {
msaboff@apple.com203e7852011-11-14 23:51:06 +0000266 outOfMemory(exec);
267 return;
268 }
fpizlo@apple.com4c6b8ad2014-07-22 21:08:50 +0000269 resolveRopeInternal8NoSubstring(buffer);
akling@apple.com63e35432014-05-05 06:24:44 +0000270 clearFibers();
msaboff@apple.com203e7852011-11-14 23:51:06 +0000271 ASSERT(!isRope());
msaboff@apple.com203e7852011-11-14 23:51:06 +0000272 return;
273 }
274
msaboff@apple.com8cae2b12011-04-21 17:23:16 +0000275 UChar* buffer;
mark.lam@apple.com88445052016-10-25 22:19:37 +0000276 if (auto newImpl = StringImpl::tryCreateUninitialized(length(), buffer)) {
ggaren@apple.come2ebb8c2015-03-11 21:29:57 +0000277 Heap::heap(this)->reportExtraMemoryAllocated(newImpl->cost());
krollin@apple.com19cde092016-06-08 01:31:17 +0000278 m_value = WTFMove(newImpl);
commit-queue@webkit.org1a15efe2012-02-28 16:02:02 +0000279 } else {
msaboff@apple.com8cae2b12011-04-21 17:23:16 +0000280 outOfMemory(exec);
281 return;
282 }
283
fpizlo@apple.com4c6b8ad2014-07-22 21:08:50 +0000284 resolveRopeInternal16NoSubstring(buffer);
akling@apple.com63e35432014-05-05 06:24:44 +0000285 clearFibers();
msaboff@apple.com8cae2b12011-04-21 17:23:16 +0000286 ASSERT(!isRope());
287}
288
msaboff@apple.com203e7852011-11-14 23:51:06 +0000289// Overview: These functions convert a JSString from holding a string in rope form
benjamin@webkit.orgcff06e42012-08-30 21:23:51 +0000290// down to a simple String representation. It does so by building up the string
barraclough@apple.com35d680c2009-12-04 02:15:18 +0000291// 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.com4d95c9e2010-08-10 22:32:19 +0000295// we would likely have to place all of the constituent StringImpls into the
barraclough@apple.com35d680c2009-12-04 02:15:18 +0000296// 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.com8cae2b12011-04-21 17:23:16 +0000298// rope-of-ropes.)
msaboff@apple.com6e74e922012-04-27 23:51:17 +0000299void JSRopeString::resolveRopeSlowCase8(LChar* buffer) const
barraclough@apple.com35d680c2009-12-04 02:15:18 +0000300{
mark.lam@apple.com88445052016-10-25 22:19:37 +0000301 LChar* position = buffer + length(); // We will be working backwards over the rope.
oliver@apple.com4e3f9652013-04-08 04:14:50 +0000302 Vector<JSString*, 32, UnsafeVectorOverflow> workQueue; // Putting strings into a Vector is only OK because there are no GC points in this method.
ggaren@apple.comffbe44d2011-10-19 19:45:35 +0000303
fpizlo@apple.com4c6b8ad2014-07-22 21:08:50 +0000304 for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i)
305 workQueue.append(fiber(i).get());
ggaren@apple.comffbe44d2011-10-19 19:45:35 +0000306
307 while (!workQueue.isEmpty()) {
308 JSString* currentFiber = workQueue.last();
309 workQueue.removeLast();
akling@apple.com77ee6bf2014-05-05 06:10:03 +0000310
fpizlo@apple.com4c6b8ad2014-07-22 21:08:50 +0000311 const LChar* characters;
312
ggaren@apple.comfbf6d9a2011-10-19 02:54:29 +0000313 if (currentFiber->isRope()) {
msaboff@apple.com6e74e922012-04-27 23:51:17 +0000314 JSRopeString* currentFiberAsRope = static_cast<JSRopeString*>(currentFiber);
fpizlo@apple.com4c6b8ad2014-07-22 21:08:50 +0000315 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.comffbe44d2011-10-19 19:45:35 +0000328 position -= length;
fpizlo@apple.com4c6b8ad2014-07-22 21:08:50 +0000329 StringImpl::copyChars(position, characters, length);
msaboff@apple.com203e7852011-11-14 23:51:06 +0000330 }
331
332 ASSERT(buffer == position);
msaboff@apple.com203e7852011-11-14 23:51:06 +0000333}
334
msaboff@apple.com6e74e922012-04-27 23:51:17 +0000335void JSRopeString::resolveRopeSlowCase(UChar* buffer) const
msaboff@apple.com203e7852011-11-14 23:51:06 +0000336{
mark.lam@apple.com88445052016-10-25 22:19:37 +0000337 UChar* position = buffer + length(); // We will be working backwards over the rope.
oliver@apple.com4e3f9652013-04-08 04:14:50 +0000338 Vector<JSString*, 32, UnsafeVectorOverflow> workQueue; // These strings are kept alive by the parent rope, so using a Vector is OK.
msaboff@apple.com203e7852011-11-14 23:51:06 +0000339
fpizlo@apple.com4c6b8ad2014-07-22 21:08:50 +0000340 for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i)
341 workQueue.append(fiber(i).get());
msaboff@apple.com203e7852011-11-14 23:51:06 +0000342
343 while (!workQueue.isEmpty()) {
344 JSString* currentFiber = workQueue.last();
345 workQueue.removeLast();
346
347 if (currentFiber->isRope()) {
msaboff@apple.com6e74e922012-04-27 23:51:17 +0000348 JSRopeString* currentFiberAsRope = static_cast<JSRopeString*>(currentFiber);
fpizlo@apple.com4c6b8ad2014-07-22 21:08:50 +0000349 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.com203e7852011-11-14 23:51:06 +0000364 continue;
365 }
366
367 StringImpl* string = static_cast<StringImpl*>(currentFiber->m_value.impl());
368 unsigned length = string->length();
369 position -= length;
msaboff@apple.comf25bc162012-09-07 01:29:12 +0000370 if (string->is8Bit())
371 StringImpl::copyChars(position, string->characters8(), length);
372 else
373 StringImpl::copyChars(position, string->characters16(), length);
barraclough@apple.com35d680c2009-12-04 02:15:18 +0000374 }
ggaren@apple.comffbe44d2011-10-19 19:45:35 +0000375
376 ASSERT(buffer == position);
barraclough@apple.com35d680c2009-12-04 02:15:18 +0000377}
msaboff@apple.com8cae2b12011-04-21 17:23:16 +0000378
msaboff@apple.com6e74e922012-04-27 23:51:17 +0000379void JSRopeString::outOfMemory(ExecState* exec) const
msaboff@apple.com8cae2b12011-04-21 17:23:16 +0000380{
mark.lam@apple.com284f4562016-08-30 20:54:54 +0000381 VM& vm = exec->vm();
382 auto scope = DECLARE_THROW_SCOPE(vm);
383
akling@apple.com63e35432014-05-05 06:24:44 +0000384 clearFibers();
commit-queue@webkit.org7914d5a2012-02-23 19:29:14 +0000385 ASSERT(isRope());
benjamin@webkit.orgcff06e42012-08-30 21:23:51 +0000386 ASSERT(m_value.isNull());
msaboff@apple.com8cae2b12011-04-21 17:23:16 +0000387 if (exec)
mark.lam@apple.com284f4562016-08-30 20:54:54 +0000388 throwOutOfMemoryError(exec, scope);
msaboff@apple.com8cae2b12011-04-21 17:23:16 +0000389}
390
ggaren@apple.comdc067b62009-05-01 22:43:39 +0000391JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const
weinig@apple.comefdce0f2008-06-30 20:52:03 +0000392{
weinig@apple.com2947a912008-07-07 02:49:29 +0000393 return const_cast<JSString*>(this);
weinig@apple.comefdce0f2008-06-30 20:52:03 +0000394}
395
mhahnenberg@apple.com061133e2011-09-27 19:53:49 +0000396bool JSString::getPrimitiveNumber(ExecState* exec, double& number, JSValue& result) const
weinig@apple.comefdce0f2008-06-30 20:52:03 +0000397{
fpizlo@apple.com9b46f342017-05-11 23:05:01 +0000398 VM& vm = exec->vm();
399 auto scope = DECLARE_THROW_SCOPE(vm);
400 StringView view = unsafeView(exec);
401 RETURN_IF_EXCEPTION(scope, false);
barraclough@apple.com35d680c2009-12-04 02:15:18 +0000402 result = this;
fpizlo@apple.com9b46f342017-05-11 23:05:01 +0000403 number = jsToNumber(view);
weinig@apple.comefdce0f2008-06-30 20:52:03 +0000404 return false;
405}
406
barraclough@apple.comb749f0b2009-12-07 23:14:04 +0000407double JSString::toNumber(ExecState* exec) const
weinig@apple.comefdce0f2008-06-30 20:52:03 +0000408{
fpizlo@apple.com9b46f342017-05-11 23:05:01 +0000409 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.comefdce0f2008-06-30 20:52:03 +0000414}
415
akling@apple.com019809c2013-10-06 18:16:48 +0000416inline StringObject* StringObject::create(VM& vm, JSGlobalObject* globalObject, JSString* string)
weinig@apple.comefdce0f2008-06-30 20:52:03 +0000417{
akling@apple.com019809c2013-10-06 18:16:48 +0000418 StringObject* object = new (NotNull, allocateCell<StringObject>(vm.heap)) StringObject(vm, globalObject->stringObjectStructure());
419 object->finishCreation(vm, string);
mhahnenberg@apple.com7317a7f2011-09-09 21:43:14 +0000420 return object;
weinig@apple.comefdce0f2008-06-30 20:52:03 +0000421}
422
oliver@apple.com3b6dc572011-03-28 23:39:16 +0000423JSObject* JSString::toObject(ExecState* exec, JSGlobalObject* globalObject) const
weinig@apple.comefdce0f2008-06-30 20:52:03 +0000424{
akling@apple.com019809c2013-10-06 18:16:48 +0000425 return StringObject::create(exec->vm(), globalObject, const_cast<JSString*>(this));
weinig@apple.comefdce0f2008-06-30 20:52:03 +0000426}
427
oliver@apple.come2fe4ce2013-07-25 03:59:41 +0000428JSValue JSString::toThis(JSCell* cell, ExecState* exec, ECMAMode ecmaMode)
weinig@apple.comefdce0f2008-06-30 20:52:03 +0000429{
oliver@apple.come2fe4ce2013-07-25 03:59:41 +0000430 if (ecmaMode == StrictMode)
431 return cell;
darin@apple.comb674d3a2016-12-16 04:52:20 +0000432 return StringObject::create(exec->vm(), exec->lexicalGlobalObject(), asString(cell));
weinig@apple.comefdce0f2008-06-30 20:52:03 +0000433}
434
barraclough@apple.com38d3c752012-05-12 00:39:43 +0000435bool JSString::getStringPropertyDescriptor(ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
oliver@apple.com4b4f7852009-08-26 16:52:15 +0000436{
437 if (propertyName == exec->propertyNames().length) {
mark.lam@apple.com88445052016-10-25 22:19:37 +0000438 descriptor.setDescriptor(jsNumber(length()), DontEnum | DontDelete | ReadOnly);
oliver@apple.com4b4f7852009-08-26 16:52:15 +0000439 return true;
440 }
441
utatane.tea@gmail.com43926962016-11-27 06:08:16 +0000442 std::optional<uint32_t> index = parseIndex(propertyName);
mark.lam@apple.com88445052016-10-25 22:19:37 +0000443 if (index && index.value() < length()) {
utatane.tea@gmail.comc7c203c2015-04-06 12:40:33 +0000444 descriptor.setDescriptor(getIndex(exec, index.value()), DontDelete | ReadOnly);
oliver@apple.com4b4f7852009-08-26 16:52:15 +0000445 return true;
446 }
447
448 return false;
449}
450
akling@apple.comc3e85a62014-07-05 03:36:36 +0000451JSString* jsStringWithCacheSlowCase(VM& vm, StringImpl& stringImpl)
452{
akling@apple.com926b1102015-03-10 00:09:39 +0000453 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.comc3e85a62014-07-05 03:36:36 +0000459}
460
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +0000461} // namespace JSC