]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/StringRecursionChecker.h
JavaScriptCore-7600.1.4.11.8.tar.gz
[apple/javascriptcore.git] / runtime / StringRecursionChecker.h
CommitLineData
14957cd0
A
1/*
2 * Copyright (C) 2011 Apple Inc. All rights reserved.
3 *
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.
8 *
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.
13 *
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
17 *
18 */
19
20#ifndef StringRecursionChecker_h
21#define StringRecursionChecker_h
22
23#include "Interpreter.h"
93a37866
A
24#include <wtf/StackStats.h>
25#include <wtf/WTFThreadData.h>
14957cd0
A
26
27namespace JSC {
28
29class StringRecursionChecker {
30 WTF_MAKE_NONCOPYABLE(StringRecursionChecker);
31
32public:
33 StringRecursionChecker(ExecState*, JSObject* thisObject);
34 ~StringRecursionChecker();
35
6fe7ccc8 36 JSValue earlyReturnValue() const; // 0 if everything is OK, value to return for failure cases
14957cd0
A
37
38private:
6fe7ccc8
A
39 JSValue throwStackOverflowError();
40 JSValue emptyString();
41 JSValue performCheck();
14957cd0
A
42
43 ExecState* m_exec;
44 JSObject* m_thisObject;
6fe7ccc8 45 JSValue m_earlyReturnValue;
93a37866
A
46
47 StackStats::CheckPoint stackCheckpoint;
14957cd0
A
48};
49
6fe7ccc8 50inline JSValue StringRecursionChecker::performCheck()
14957cd0 51{
81345200
A
52 VM& vm = m_exec->vm();
53 if (!vm.isSafeToRecurse())
14957cd0 54 return throwStackOverflowError();
81345200 55 bool alreadyVisited = !vm.stringRecursionCheckVisitedObjects.add(m_thisObject).isNewEntry;
14957cd0
A
56 if (alreadyVisited)
57 return emptyString(); // Return empty string to avoid infinite recursion.
6fe7ccc8 58 return JSValue(); // Indicate success.
14957cd0
A
59}
60
61inline StringRecursionChecker::StringRecursionChecker(ExecState* exec, JSObject* thisObject)
62 : m_exec(exec)
63 , m_thisObject(thisObject)
64 , m_earlyReturnValue(performCheck())
65{
66}
67
6fe7ccc8 68inline JSValue StringRecursionChecker::earlyReturnValue() const
14957cd0
A
69{
70 return m_earlyReturnValue;
71}
72
73inline StringRecursionChecker::~StringRecursionChecker()
74{
75 if (m_earlyReturnValue)
76 return;
93a37866
A
77 ASSERT(m_exec->vm().stringRecursionCheckVisitedObjects.contains(m_thisObject));
78 m_exec->vm().stringRecursionCheckVisitedObjects.remove(m_thisObject);
14957cd0
A
79}
80
81}
82
83#endif