ExecState.h 5.02 KB
Newer Older
mjs's avatar
mjs committed
1
// -*- mode: c++; c-basic-offset: 4 -*-
ggaren's avatar
ggaren committed
2 3 4
/*
 *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
 *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
mjs's avatar
mjs committed
5
 *  Copyright (C) 2003, 2007 Apple Inc. All rights reserved.
ggaren's avatar
ggaren committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 *
 */

#ifndef ExecState_H
#define ExecState_H

#include "value.h"
#include "types.h"
mjs's avatar
mjs committed
29
#include "CommonIdentifiers.h"
mjs's avatar
mjs committed
30 31
#include "LabelStack.h"
#include "scope_chain.h"
ggaren's avatar
ggaren committed
32

mjs's avatar
mjs committed
33 34 35 36 37 38 39
namespace KJS  {

    enum CodeType {
        GlobalCode,
        EvalCode,
        FunctionCode,
    };
ggaren's avatar
ggaren committed
40
    
mjs's avatar
mjs committed
41 42
    class JSGlobalObject;
    class ScopeChain;
darin's avatar
darin committed
43 44 45
    class Interpreter;
    class FunctionImp;
    class GlobalFuncImp;
mjs's avatar
mjs committed
46
    class FunctionBodyNode;
aroben's avatar
aroben committed
47
    struct LocalStorageEntry;
darin's avatar
darin committed
48
    
ggaren's avatar
ggaren committed
49
    /**
mjs's avatar
mjs committed
50 51
     * Represents the current state of script execution. This is
     * passed as the first argument to most functions.
ggaren's avatar
ggaren committed
52
     */
mjs's avatar
mjs committed
53 54 55 56
    class ExecState {
        friend class Interpreter;
        friend class FunctionImp;
        friend class GlobalFuncImp;
mjs's avatar
mjs committed
57 58
    public:
        /**
mjs's avatar
mjs committed
59
         * Returns the interpreter currently running code
mjs's avatar
mjs committed
60
         */
mjs's avatar
mjs committed
61
        Interpreter* dynamicInterpreter() const { return m_interpreter; }
mjs's avatar
mjs committed
62 63
        
        /**
mjs's avatar
mjs committed
64
         * Returns the interpreter associated with the current scope's global object
mjs's avatar
mjs committed
65
         */
mjs's avatar
mjs committed
66 67 68 69 70 71 72 73
        Interpreter* lexicalInterpreter() const;
                
        void setException(JSValue* e) { m_exception = e; }
        void clearException() { m_exception = 0; }
        JSValue* exception() const { return m_exception; }
        JSValue** exceptionSlot() { return &m_exception; }
        bool hadException() const { return !!m_exception; }
        
74
        const ScopeChain& scopeChain() const { return m_scopeChain; }
mjs's avatar
mjs committed
75
        
mjs's avatar
mjs committed
76 77 78 79 80
        JSObject* variableObject() const { return m_variable; }
        void setVariableObject(JSObject* v) { m_variable = v; }
        
        JSObject* thisValue() const { return m_thisVal; }
        
mjs's avatar
mjs committed
81
        ExecState* callingExecState() { return m_callingExecState; }
mjs's avatar
mjs committed
82 83 84 85 86 87 88
        
        JSObject* activationObject() { return m_activation; }
        CodeType codeType() { return m_codeType; }
        FunctionBodyNode* currentBody() { return m_currentBody; }
        FunctionImp* function() const { return m_function; }
        const List* arguments() const { return m_arguments; }
        
89 90
        void pushScope(JSObject* s) { m_scopeChain.push(s); }
        void popScope() { m_scopeChain.pop(); }
mjs's avatar
mjs committed
91 92 93 94 95 96 97 98 99
        LabelStack* seenLabels() { return &ls; }
        
        void pushIteration() { m_iterationDepth++; }
        void popIteration() { m_iterationDepth--; }
        bool inIteration() const { return (m_iterationDepth > 0); }
        
        void pushSwitch() { m_switchDepth++; }
        void popSwitch() { m_switchDepth--; }
        bool inSwitch() const { return (m_switchDepth > 0); }
100 101

        void setGlobalObject(JSGlobalObject*);
mjs's avatar
mjs committed
102
        
mjs@apple.com's avatar
mjs@apple.com committed
103
        void markChildren(MarkStack&);
mjs's avatar
mjs committed
104
        
mjs's avatar
mjs committed
105 106 107
        // This is a workaround to avoid accessing the global variables for these identifiers in
        // important property lookup functions, to avoid taking PIC branches in Mach-O binaries
        const CommonIdentifiers& propertyNames() const { return *m_propertyNames; }
mjs's avatar
mjs committed
108 109 110 111

        LocalStorageEntry* localStorage() { return m_localStorageBuffer; }
        void updateLocalStorage();
    
mjs's avatar
mjs committed
112
    private:
mjs's avatar
mjs committed
113 114 115
        ExecState(Interpreter* interp, JSGlobalObject* glob, JSObject* thisV,
                  FunctionBodyNode* currentBody, CodeType type = GlobalCode,
                  ExecState* callingExecState = 0, FunctionImp* function = 0, const List* args = 0);
116
        ~ExecState();
mjs's avatar
mjs committed
117 118

        // ExecStates are always stack-allocated, and the garbage collector
mjs's avatar
mjs committed
119
        // marks the stack, so we don't need to protect the objects below from GC.
mjs's avatar
mjs committed
120

mjs's avatar
mjs committed
121
        Interpreter* m_interpreter;
mjs's avatar
mjs committed
122 123 124 125 126
        JSValue* m_exception;
        CommonIdentifiers* m_propertyNames;

        ExecState* m_callingExecState;
        ExecState* m_savedExecState;
mjs's avatar
mjs committed
127 128 129 130 131
        FunctionBodyNode* m_currentBody;
        
        FunctionImp* m_function;
        const List* m_arguments;
        JSObject* m_activation;
mjs's avatar
mjs committed
132 133
        LocalStorageEntry* m_localStorageBuffer;

134
        ScopeChain m_scopeChain;
mjs's avatar
mjs committed
135 136 137 138 139 140 141 142
        JSObject* m_variable;
        JSObject* m_thisVal;
        
        LabelStack ls;
        int m_iterationDepth;
        int m_switchDepth;
        CodeType m_codeType;
    };
ggaren's avatar
ggaren committed
143 144 145 146

} // namespace KJS

#endif // ExecState_H