1/*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef StackVisitor_h
27#define StackVisitor_h
28
29#include <wtf/text/WTFString.h>
30
31namespace JSC {
32
33struct CodeOrigin;
34struct InlineCallFrame;
35
36class Arguments;
37class CodeBlock;
38class ExecState;
39class JSFunction;
40class JSObject;
41class JSScope;
42class Register;
43
44typedef ExecState CallFrame;
45
46class StackVisitor {
47public:
48    class Frame {
49    public:
50        enum CodeType {
51            Global,
52            Eval,
53            Function,
54            Native
55        };
56
57        size_t index() const { return m_index; }
58        size_t argumentCountIncludingThis() const { return m_argumentCountIncludingThis; }
59        CallFrame* callerFrame() const { return m_callerFrame; }
60        JSObject* callee() const { return m_callee; }
61        JSScope* scope() const { return m_scope; }
62        CodeBlock* codeBlock() const { return m_codeBlock; }
63        unsigned bytecodeOffset() const { return m_bytecodeOffset; }
64#if ENABLE(DFG_JIT)
65        InlineCallFrame* inlineCallFrame() const { return m_inlineCallFrame; }
66#endif
67
68        bool isJSFrame() const { return !!codeBlock(); }
69#if ENABLE(DFG_JIT)
70        bool isInlinedFrame() const { return !!m_inlineCallFrame; }
71#endif
72
73        JS_EXPORT_PRIVATE String functionName();
74        JS_EXPORT_PRIVATE String sourceURL();
75        JS_EXPORT_PRIVATE String toString();
76
77        CodeType codeType() const;
78        JS_EXPORT_PRIVATE void computeLineAndColumn(unsigned& line, unsigned& column);
79
80        Arguments* createArguments();
81        Arguments* existingArguments();
82        CallFrame* callFrame() const { return m_callFrame; }
83
84#ifndef NDEBUG
85        JS_EXPORT_PRIVATE void print(int indentLevel);
86#endif
87
88    private:
89        Frame() { }
90        ~Frame() { }
91
92        void retrieveExpressionInfo(int& divot, int& startOffset, int& endOffset, unsigned& line, unsigned& column);
93        void setToEnd();
94
95        size_t m_index;
96        size_t m_argumentCountIncludingThis;
97        CallFrame* m_callerFrame;
98        JSObject* m_callee;
99        JSScope* m_scope;
100        CodeBlock* m_codeBlock;
101        unsigned m_bytecodeOffset;
102#if ENABLE(DFG_JIT)
103        InlineCallFrame* m_inlineCallFrame;
104#endif
105        CallFrame* m_callFrame;
106
107        friend class StackVisitor;
108    };
109
110    enum Status {
111        Continue = 0,
112        Done = 1
113    };
114
115    // StackVisitor::visit() expects a Functor that implements the following method:
116    //     Status operator()(StackVisitor&);
117
118    template <typename Functor>
119    static void visit(CallFrame* startFrame, Functor& functor)
120    {
121        StackVisitor visitor(startFrame);
122        while (visitor->callFrame()) {
123            Status status = functor(visitor);
124            if (status != Continue)
125                break;
126            visitor.gotoNextFrame();
127        }
128    }
129
130    Frame& operator*() { return m_frame; }
131    ALWAYS_INLINE Frame* operator->() { return &m_frame; }
132
133private:
134    JS_EXPORT_PRIVATE StackVisitor(CallFrame* startFrame);
135
136    JS_EXPORT_PRIVATE void gotoNextFrame();
137
138    void readFrame(CallFrame*);
139    void readNonInlinedFrame(CallFrame*, CodeOrigin* = 0);
140#if ENABLE(DFG_JIT)
141    void readInlinedFrame(CallFrame*, CodeOrigin*);
142#endif
143
144    Frame m_frame;
145};
146
147} // namespace JSC
148
149#endif // StackVisitor_h
150
151