2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #ifndef StringRecursionChecker_h
21 #define StringRecursionChecker_h
23 #include "Interpreter.h"
27 class StringRecursionChecker {
28 WTF_MAKE_NONCOPYABLE(StringRecursionChecker);
31 StringRecursionChecker(ExecState*, JSObject* thisObject);
32 ~StringRecursionChecker();
34 JSValue earlyReturnValue() const; // 0 if everything is OK, value to return for failure cases
37 JSValue throwStackOverflowError();
38 JSValue emptyString();
39 JSValue performCheck();
42 JSObject* m_thisObject;
43 JSValue m_earlyReturnValue;
46 inline JSValue StringRecursionChecker::performCheck()
48 int size = m_exec->globalData().stringRecursionCheckVisitedObjects.size();
49 if (size >= MaxSmallThreadReentryDepth && size >= m_exec->globalData().maxReentryDepth)
50 return throwStackOverflowError();
51 bool alreadyVisited = !m_exec->globalData().stringRecursionCheckVisitedObjects.add(m_thisObject).second;
53 return emptyString(); // Return empty string to avoid infinite recursion.
54 return JSValue(); // Indicate success.
57 inline StringRecursionChecker::StringRecursionChecker(ExecState* exec, JSObject* thisObject)
59 , m_thisObject(thisObject)
60 , m_earlyReturnValue(performCheck())
64 inline JSValue StringRecursionChecker::earlyReturnValue() const
66 return m_earlyReturnValue;
69 inline StringRecursionChecker::~StringRecursionChecker()
71 if (m_earlyReturnValue)
73 ASSERT(m_exec->globalData().stringRecursionCheckVisitedObjects.contains(m_thisObject));
74 m_exec->globalData().stringRecursionCheckVisitedObjects.remove(m_thisObject);